blob: 988454ac947b9d0b0dcdca95087b1b0d6a71c650 [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) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700337 auto layer =
Marissa Wallfda30bb2018-10-12 11:34:28 -0700338 client->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
Chia-I Wu718daf82017-10-20 11:57:17 -0700339 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
340
Vishnu Nair60356342018-11-13 13:00:45 -0800341 Transaction t;
342 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
343 // If we are creating a color layer, set its crop since its size will be ignored.
344 if (flags == ISurfaceComposerClient::eFXSurfaceColor) {
345 t.setCrop_legacy(layer, Rect(0, 0, width, height));
346 }
347
348 status_t error = t.apply();
Chia-I Wu718daf82017-10-20 11:57:17 -0700349 if (error != NO_ERROR) {
350 ADD_FAILURE() << "failed to initialize SurfaceControl";
351 layer.clear();
352 }
353
354 return layer;
355 }
356
Marissa Wallfda30bb2018-10-12 11:34:28 -0700357 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
358 uint32_t flags = 0) {
359 return createLayer(mClient, name, width, height, flags);
360 }
361
Marissa Wall61c58622018-07-18 10:12:20 -0700362 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700363 // wait for previous transactions (such as setSize) to complete
364 Transaction().apply(true);
365
366 ANativeWindow_Buffer buffer = {};
367 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
368
369 return buffer;
370 }
371
Marissa Wall61c58622018-07-18 10:12:20 -0700372 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700373 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
374
375 // wait for the newly posted buffer to be latched
376 waitForLayerBuffers();
377 }
378
Marissa Wall61c58622018-07-18 10:12:20 -0700379 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
380 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700381 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700382 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
383 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
384 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700385 }
386
Marissa Wall61c58622018-07-18 10:12:20 -0700387 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
388 int32_t bufferWidth, int32_t bufferHeight) {
389 sp<GraphicBuffer> buffer =
390 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
391 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
392 BufferUsage::COMPOSER_OVERLAY,
393 "test");
394 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
395 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
396 }
397
398 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
399 int32_t bufferWidth, int32_t bufferHeight) {
400 switch (mLayerType) {
401 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
402 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
403 break;
404 case ISurfaceComposerClient::eFXSurfaceBufferState:
405 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
406 break;
407 default:
408 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
409 }
410 }
411
412 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
413 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700414 const Color& topRight, const Color& bottomLeft,
415 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700416 switch (mLayerType) {
417 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
418 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
419 bottomLeft, bottomRight);
420 break;
421 case ISurfaceComposerClient::eFXSurfaceBufferState:
422 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
423 bottomLeft, bottomRight);
424 break;
425 default:
426 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
427 }
428 }
429
430 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
431 int32_t bufferHeight, const Color& topLeft,
432 const Color& topRight, const Color& bottomLeft,
433 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700434 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700435 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
436 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700437
Marissa Wall61c58622018-07-18 10:12:20 -0700438 const int32_t halfW = bufferWidth / 2;
439 const int32_t halfH = bufferHeight / 2;
440 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
441 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
442 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
443 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
444 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700445
Marissa Wall61c58622018-07-18 10:12:20 -0700446 postBufferQueueLayerBuffer(layer);
447 }
448
449 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
450 int32_t bufferHeight, const Color& topLeft,
451 const Color& topRight, const Color& bottomLeft,
452 const Color& bottomRight) {
453 sp<GraphicBuffer> buffer =
454 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
455 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
456 BufferUsage::COMPOSER_OVERLAY,
457 "test");
458
459 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
460
461 const int32_t halfW = bufferWidth / 2;
462 const int32_t halfH = bufferHeight / 2;
463 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
464 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
465 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
466 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
467
468 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700469 }
470
chaviw0e3479f2018-09-10 16:49:30 -0700471 std::unique_ptr<ScreenCapture> screenshot() {
472 std::unique_ptr<ScreenCapture> screenshot;
473 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700474 return screenshot;
475 }
476
477 sp<SurfaceComposerClient> mClient;
478
479 sp<IBinder> mDisplay;
480 uint32_t mDisplayWidth;
481 uint32_t mDisplayHeight;
482 uint32_t mDisplayLayerStack;
483
484 // leave room for ~256 layers
485 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
486
Marissa Wall61c58622018-07-18 10:12:20 -0700487 void setPositionWithResizeHelper(uint32_t layerType);
488 void setSizeBasicHelper(uint32_t layerType);
489 void setMatrixWithResizeHelper(uint32_t layerType);
490
chaviw0e3479f2018-09-10 16:49:30 -0700491 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700492 bool mColorManagementUsed;
493
Chia-I Wu718daf82017-10-20 11:57:17 -0700494private:
495 void SetUpDisplay() {
496 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
497 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
498
499 // get display width/height
500 DisplayInfo info;
501 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
502 mDisplayWidth = info.w;
503 mDisplayHeight = info.h;
504
505 // After a new buffer is queued, SurfaceFlinger is notified and will
506 // latch the new buffer on next vsync. Let's heuristically wait for 3
507 // vsyncs.
508 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
509
510 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700511
512 mBlackBgSurface = mClient->createSurface(String8("BaseSurface"), mDisplayWidth,
513 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
514 ISurfaceComposerClient::eFXSurfaceColor);
515
Chia-I Wu718daf82017-10-20 11:57:17 -0700516 // set layer stack (b/68888219)
517 Transaction t;
518 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800519 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700520 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
521 t.setColor(mBlackBgSurface, half3{0, 0, 0});
522 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700523 t.apply();
524 }
525
chaviw0e3479f2018-09-10 16:49:30 -0700526 void waitForLayerBuffers() {
527 // Request an empty transaction to get applied synchronously to ensure the buffer is
528 // latched.
529 Transaction().apply(true);
530 usleep(mBufferPostDelay);
531 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700532
533 int32_t mBufferPostDelay;
534};
535
Marissa Wall61c58622018-07-18 10:12:20 -0700536class LayerTypeTransactionTest : public LayerTransactionTest,
537 public ::testing::WithParamInterface<uint32_t> {
538public:
539 LayerTypeTransactionTest() { mLayerType = GetParam(); }
540
541 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
542 uint32_t flags = 0) override {
543 // if the flags already have a layer type specified, return an error
544 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
545 return nullptr;
546 }
547 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType);
548 }
549
550 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
551 int32_t bufferHeight) {
552 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
553 bufferWidth, bufferHeight));
554 }
555
556 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
557 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
558 const Color& bottomLeft, const Color& bottomRight) {
559 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
560 bufferWidth, bufferHeight,
561 topLeft, topRight,
562 bottomLeft, bottomRight));
563 }
564
565protected:
566 uint32_t mLayerType;
567};
568
569INSTANTIATE_TEST_CASE_P(
570 LayerTypeTransactionTests, LayerTypeTransactionTest,
571 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
572 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
573
574TEST_P(LayerTypeTransactionTest, SetPositionBasic) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700575 sp<SurfaceControl> layer;
576 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700577 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700578
579 {
580 SCOPED_TRACE("default position");
581 auto shot = screenshot();
582 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
583 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
584 }
585
586 Transaction().setPosition(layer, 5, 10).apply();
587 {
588 SCOPED_TRACE("new position");
589 auto shot = screenshot();
590 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
591 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
592 }
593}
594
Marissa Wall61c58622018-07-18 10:12:20 -0700595TEST_P(LayerTypeTransactionTest, SetPositionRounding) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700596 sp<SurfaceControl> layer;
597 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700598 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700599
600 // GLES requires only 4 bits of subpixel precision during rasterization
601 // XXX GLES composition does not match HWC composition due to precision
602 // loss (b/69315223)
603 const float epsilon = 1.0f / 16.0f;
604 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
605 {
606 SCOPED_TRACE("rounding down");
607 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
608 }
609
610 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
611 {
612 SCOPED_TRACE("rounding up");
613 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
614 }
615}
616
Marissa Wall61c58622018-07-18 10:12:20 -0700617TEST_P(LayerTypeTransactionTest, SetPositionOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700618 sp<SurfaceControl> layer;
619 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700620 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700621
622 Transaction().setPosition(layer, -32, -32).apply();
623 {
624 SCOPED_TRACE("negative coordinates");
625 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
626 }
627
628 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
629 {
630 SCOPED_TRACE("positive coordinates");
631 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
632 }
633}
634
Marissa Wall61c58622018-07-18 10:12:20 -0700635TEST_P(LayerTypeTransactionTest, SetPositionPartiallyOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700636 sp<SurfaceControl> layer;
637 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700638 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700639
640 // partially out of bounds
641 Transaction().setPosition(layer, -30, -30).apply();
642 {
643 SCOPED_TRACE("negative coordinates");
644 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
645 }
646
647 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
648 {
649 SCOPED_TRACE("positive coordinates");
650 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
651 mDisplayHeight),
652 Color::RED);
653 }
654}
655
Marissa Wall61c58622018-07-18 10:12:20 -0700656void LayerTransactionTest::setPositionWithResizeHelper(uint32_t layerType) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700657 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700658 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
659 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700660
661 // setPosition is applied immediately by default, with or without resize
662 // pending
663 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
664 {
665 SCOPED_TRACE("resize pending");
666 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700667 Rect rect;
668 switch (layerType) {
669 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
670 rect = {5, 10, 37, 42};
671 break;
672 case ISurfaceComposerClient::eFXSurfaceBufferState:
673 rect = {5, 10, 69, 74};
674 break;
675 default:
676 ASSERT_FALSE(true) << "Unsupported layer type";
677 }
678
679 shot->expectColor(rect, Color::RED);
680 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700681 }
682
Marissa Wall61c58622018-07-18 10:12:20 -0700683 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700684 {
685 SCOPED_TRACE("resize applied");
686 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
687 }
688}
689
Marissa Wall61c58622018-07-18 10:12:20 -0700690TEST_F(LayerTransactionTest, SetPositionWithResize_BufferQueue) {
691 ASSERT_NO_FATAL_FAILURE(
692 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
693}
694
695TEST_F(LayerTransactionTest, SetPositionWithResize_BufferState) {
696 ASSERT_NO_FATAL_FAILURE(
697 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
698}
699
700TEST_F(LayerTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700701 sp<SurfaceControl> layer;
702 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700703 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700704
705 // request setPosition to be applied with the next resize
706 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
707 {
708 SCOPED_TRACE("new position pending");
709 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
710 }
711
712 Transaction().setPosition(layer, 15, 20).apply();
713 {
714 SCOPED_TRACE("pending new position modified");
715 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
716 }
717
718 Transaction().setSize(layer, 64, 64).apply();
719 {
720 SCOPED_TRACE("resize pending");
721 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
722 }
723
724 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700725 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700726 {
727 SCOPED_TRACE("new position applied");
728 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
729 }
730}
731
Marissa Wall61c58622018-07-18 10:12:20 -0700732TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700733 sp<SurfaceControl> layer;
734 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700735 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700736
737 // setPosition is not immediate even with SCALE_TO_WINDOW override
738 Transaction()
739 .setPosition(layer, 5, 10)
740 .setSize(layer, 64, 64)
741 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
742 .setGeometryAppliesWithResize(layer)
743 .apply();
744 {
745 SCOPED_TRACE("new position pending");
746 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
747 }
748
Marissa Wall61c58622018-07-18 10:12:20 -0700749 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700750 {
751 SCOPED_TRACE("new position applied");
752 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
753 }
754}
755
Marissa Wall61c58622018-07-18 10:12:20 -0700756void LayerTransactionTest::setSizeBasicHelper(uint32_t layerType) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700757 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700758 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
759 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700760
761 Transaction().setSize(layer, 64, 64).apply();
762 {
763 SCOPED_TRACE("resize pending");
764 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700765 Rect rect;
766 switch (layerType) {
767 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
768 rect = {0, 0, 32, 32};
769 break;
770 case ISurfaceComposerClient::eFXSurfaceBufferState:
771 rect = {0, 0, 64, 64};
772 break;
773 default:
774 ASSERT_FALSE(true) << "Unsupported layer type";
775 }
776 shot->expectColor(rect, Color::RED);
777 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700778 }
779
Marissa Wall61c58622018-07-18 10:12:20 -0700780 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700781 {
782 SCOPED_TRACE("resize applied");
783 auto shot = screenshot();
784 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
785 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
786 }
787}
788
Marissa Wall61c58622018-07-18 10:12:20 -0700789TEST_F(LayerTransactionTest, SetSizeBasic_BufferQueue) {
790 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue);
791}
792
793TEST_F(LayerTransactionTest, SetSizeBasic_BufferState) {
794 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState);
795}
796
797TEST_P(LayerTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700798 // cannot test robustness against invalid sizes (zero or really huge)
799}
800
Marissa Wall61c58622018-07-18 10:12:20 -0700801TEST_P(LayerTypeTransactionTest, SetSizeWithScaleToWindow) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700802 sp<SurfaceControl> layer;
803 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700804 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700805
806 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
807 Transaction()
808 .setSize(layer, 64, 64)
809 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
810 .apply();
811 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
812}
813
Marissa Wall61c58622018-07-18 10:12:20 -0700814TEST_P(LayerTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700815 sp<SurfaceControl> layerR;
816 sp<SurfaceControl> layerG;
817 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700818 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700819 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700820 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700821
822 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
823 {
824 SCOPED_TRACE("layerR");
825 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
826 }
827
828 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
829 {
830 SCOPED_TRACE("layerG");
831 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
832 }
833}
834
Marissa Wall61c58622018-07-18 10:12:20 -0700835TEST_P(LayerTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700836 sp<SurfaceControl> parent =
837 LayerTransactionTest::createLayer("Parent", mDisplayWidth, mDisplayHeight,
838 ISurfaceComposerClient::eFXSurfaceContainer);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700839 sp<SurfaceControl> layerR;
840 sp<SurfaceControl> layerG;
841 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700842 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700843 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700844 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700845
chaviw0e3479f2018-09-10 16:49:30 -0700846 Transaction()
847 .reparent(layerR, parent->getHandle())
848 .reparent(layerG, parent->getHandle())
849 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700850 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
851 {
852 SCOPED_TRACE("layerR");
chaviw0e3479f2018-09-10 16:49:30 -0700853 auto shot = screenshot();
854 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700855 }
856
857 Transaction().setLayer(layerR, -3).apply();
858 {
859 SCOPED_TRACE("layerG");
chaviw0e3479f2018-09-10 16:49:30 -0700860 auto shot = screenshot();
861 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700862 }
863}
864
Marissa Wall61c58622018-07-18 10:12:20 -0700865TEST_P(LayerTypeTransactionTest, SetRelativeZBasic) {
Chia-I Wu49313302017-10-31 10:14:40 -0700866 sp<SurfaceControl> layerR;
867 sp<SurfaceControl> layerG;
868 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700869 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700870 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700871 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700872
873 Transaction()
874 .setPosition(layerG, 16, 16)
875 .setRelativeLayer(layerG, layerR->getHandle(), 1)
876 .apply();
877 {
878 SCOPED_TRACE("layerG above");
879 auto shot = screenshot();
880 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
881 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
882 }
883
884 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
885 {
886 SCOPED_TRACE("layerG below");
887 auto shot = screenshot();
888 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
889 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
890 }
891}
892
Marissa Wall61c58622018-07-18 10:12:20 -0700893TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700894 sp<SurfaceControl> parent =
895 LayerTransactionTest::createLayer("Parent", mDisplayWidth, mDisplayHeight,
896 ISurfaceComposerClient::eFXSurfaceContainer);
Chia-I Wuec2d9852017-11-21 09:21:01 -0800897 sp<SurfaceControl> layerR;
898 sp<SurfaceControl> layerG;
899 sp<SurfaceControl> layerB;
900 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700901 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800902 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700903 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800904 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700905 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800906
chaviw0e3479f2018-09-10 16:49:30 -0700907 Transaction()
908 .reparent(layerB, parent->getHandle())
909 .apply();
910
Chia-I Wuec2d9852017-11-21 09:21:01 -0800911 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
912 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
913
chaviw0e3479f2018-09-10 16:49:30 -0700914 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800915 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700916 sp<IBinder> parentHandle = parent->getHandle();
917 ScreenCapture::captureLayers(&screenshot, parentHandle);
Chia-I Wuec2d9852017-11-21 09:21:01 -0800918 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
919}
920
Marissa Wall61c58622018-07-18 10:12:20 -0700921TEST_P(LayerTypeTransactionTest, SetRelativeZGroup) {
Chia-I Wu49313302017-10-31 10:14:40 -0700922 sp<SurfaceControl> layerR;
923 sp<SurfaceControl> layerG;
924 sp<SurfaceControl> layerB;
925 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700926 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700927 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700928 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700929 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700930 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700931
932 // layerR = 0, layerG = layerR + 3, layerB = 2
933 Transaction()
934 .setPosition(layerG, 8, 8)
935 .setRelativeLayer(layerG, layerR->getHandle(), 3)
936 .setPosition(layerB, 16, 16)
937 .setLayer(layerB, mLayerZBase + 2)
938 .apply();
939 {
940 SCOPED_TRACE("(layerR < layerG) < layerB");
941 auto shot = screenshot();
942 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
943 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
944 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
945 }
946
947 // layerR = 4, layerG = layerR + 3, layerB = 2
948 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
949 {
950 SCOPED_TRACE("layerB < (layerR < layerG)");
951 auto shot = screenshot();
952 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
953 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
954 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
955 }
956
957 // layerR = 4, layerG = layerR - 3, layerB = 2
958 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
959 {
960 SCOPED_TRACE("layerB < (layerG < layerR)");
961 auto shot = screenshot();
962 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
963 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
964 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
965 }
966
967 // restore to absolute z
968 // layerR = 4, layerG = 0, layerB = 2
969 Transaction().setLayer(layerG, mLayerZBase).apply();
970 {
971 SCOPED_TRACE("layerG < layerB < layerR");
972 auto shot = screenshot();
973 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
974 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
975 }
976
977 // layerR should not affect layerG anymore
978 // layerR = 1, layerG = 0, layerB = 2
979 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
980 {
981 SCOPED_TRACE("layerG < layerR < layerB");
982 auto shot = screenshot();
983 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
984 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
985 }
986}
987
Marissa Wall61c58622018-07-18 10:12:20 -0700988TEST_P(LayerTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -0700989 sp<SurfaceControl> layerR;
990 sp<SurfaceControl> layerG;
991
992 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700993 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700994 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700995 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700996
997 Transaction()
998 .setPosition(layerG, 16, 16)
999 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1000 .apply();
1001
1002 mClient->destroySurface(layerG->getHandle());
1003 // layerG should have been removed
1004 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1005}
1006
Marissa Wall61c58622018-07-18 10:12:20 -07001007TEST_P(LayerTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001008 sp<SurfaceControl> layer;
1009 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001010 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001011
1012 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
1013 {
1014 SCOPED_TRACE("layer hidden");
1015 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1016 }
1017
1018 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1019 {
1020 SCOPED_TRACE("layer shown");
1021 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1022 }
1023}
1024
Marissa Wall61c58622018-07-18 10:12:20 -07001025TEST_P(LayerTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001026 const Color translucentRed = {100, 0, 0, 100};
1027 sp<SurfaceControl> layerR;
1028 sp<SurfaceControl> layerG;
1029 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001030 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001031 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001032 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001033
1034 Transaction()
1035 .setLayer(layerR, mLayerZBase + 1)
1036 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1037 .apply();
1038 {
1039 SCOPED_TRACE("layerR opaque");
1040 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
1041 }
1042
1043 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1044 {
1045 SCOPED_TRACE("layerR translucent");
1046 const uint8_t g = uint8_t(255 - translucentRed.a);
1047 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
1048 }
1049}
1050
Marissa Wall61c58622018-07-18 10:12:20 -07001051TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001052 sp<SurfaceControl> layer;
1053 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001054 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001055
1056 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001057 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001058 Transaction()
1059 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1060 .apply(true);
1061 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001062 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001063
1064 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1065 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001066 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001067}
1068
Marissa Wall61c58622018-07-18 10:12:20 -07001069TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001070 const Rect top(0, 0, 32, 16);
1071 const Rect bottom(0, 16, 32, 32);
1072 sp<SurfaceControl> layer;
1073 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1074
1075 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001076 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1077 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1078 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001079 // setTransparentRegionHint always applies to the following buffer
1080 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001081 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001082 {
1083 SCOPED_TRACE("top transparent");
1084 auto shot = screenshot();
1085 shot->expectColor(top, Color::BLACK);
1086 shot->expectColor(bottom, Color::RED);
1087 }
1088
1089 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1090 {
1091 SCOPED_TRACE("transparent region hint pending");
1092 auto shot = screenshot();
1093 shot->expectColor(top, Color::BLACK);
1094 shot->expectColor(bottom, Color::RED);
1095 }
1096
Marissa Wall61c58622018-07-18 10:12:20 -07001097 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1098 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1099 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1100 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001101 {
1102 SCOPED_TRACE("bottom transparent");
1103 auto shot = screenshot();
1104 shot->expectColor(top, Color::RED);
1105 shot->expectColor(bottom, Color::BLACK);
1106 }
1107}
1108
Marissa Wall61c58622018-07-18 10:12:20 -07001109TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1110 const Rect top(0, 0, 32, 16);
1111 const Rect bottom(0, 16, 32, 32);
1112 sp<SurfaceControl> layer;
1113 ASSERT_NO_FATAL_FAILURE(
1114 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1115
1116 sp<GraphicBuffer> buffer =
1117 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1118 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1119 BufferUsage::COMPOSER_OVERLAY,
1120 "test");
1121
1122 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1123 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1124 Transaction()
1125 .setTransparentRegionHint(layer, Region(top))
1126 .setBuffer(layer, buffer)
1127 .setSize(layer, 32, 32)
1128 .apply();
1129 {
1130 SCOPED_TRACE("top transparent");
1131 auto shot = screenshot();
1132 shot->expectColor(top, Color::BLACK);
1133 shot->expectColor(bottom, Color::RED);
1134 }
1135
1136 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1137 {
1138 SCOPED_TRACE("transparent region hint intermediate");
1139 auto shot = screenshot();
1140 shot->expectColor(top, Color::BLACK);
1141 shot->expectColor(bottom, Color::BLACK);
1142 }
1143
1144 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1145 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1146 BufferUsage::COMPOSER_OVERLAY,
1147 "test");
1148
1149 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1150 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
1151 Transaction().setBuffer(layer, buffer).setSize(layer, 32, 32).apply();
1152 {
1153 SCOPED_TRACE("bottom transparent");
1154 auto shot = screenshot();
1155 shot->expectColor(top, Color::RED);
1156 shot->expectColor(bottom, Color::BLACK);
1157 }
1158}
1159
1160TEST_P(LayerTypeTransactionTest, SetTransparentRegionHintOutOfBounds) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001161 sp<SurfaceControl> layerTransparent;
1162 sp<SurfaceControl> layerR;
1163 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1164 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1165
1166 // check that transparent region hint is bound by the layer size
1167 Transaction()
1168 .setTransparentRegionHint(layerTransparent,
1169 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
1170 .setPosition(layerR, 16, 16)
1171 .setLayer(layerR, mLayerZBase + 1)
1172 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001173 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1174 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001175 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1176}
1177
Marissa Wall61c58622018-07-18 10:12:20 -07001178TEST_P(LayerTypeTransactionTest, SetAlphaBasic) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001179 sp<SurfaceControl> layer1;
1180 sp<SurfaceControl> layer2;
1181 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
1182 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001183 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}, 32, 32));
1184 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001185
1186 Transaction()
1187 .setAlpha(layer1, 0.25f)
1188 .setAlpha(layer2, 0.75f)
1189 .setPosition(layer2, 16, 0)
1190 .setLayer(layer2, mLayerZBase + 1)
1191 .apply();
1192 {
1193 auto shot = screenshot();
1194 uint8_t r = 16; // 64 * 0.25f
1195 uint8_t g = 48; // 64 * 0.75f
1196 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1197 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1198
1199 r /= 4; // r * (1.0f - 0.75f)
1200 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1201 }
1202}
1203
Marissa Wall61c58622018-07-18 10:12:20 -07001204TEST_P(LayerTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001205 const Color color = {64, 0, 0, 255};
1206 sp<SurfaceControl> layer;
1207 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001208 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001209
1210 Transaction().setAlpha(layer, 2.0f).apply();
1211 {
1212 SCOPED_TRACE("clamped to 1.0f");
1213 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1214 }
1215
1216 Transaction().setAlpha(layer, -1.0f).apply();
1217 {
1218 SCOPED_TRACE("clamped to 0.0f");
1219 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1220 }
1221}
1222
Chia-I Wue4ef6102017-11-01 15:16:35 -07001223TEST_F(LayerTransactionTest, SetColorBasic) {
1224 sp<SurfaceControl> bufferLayer;
1225 sp<SurfaceControl> colorLayer;
1226 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001227 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001228 ASSERT_NO_FATAL_FAILURE(
1229 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1230
1231 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
1232 {
1233 SCOPED_TRACE("default color");
1234 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1235 }
1236
1237 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1238 const Color expected = {15, 51, 85, 255};
1239 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1240 // channel) should be less than one
1241 const uint8_t tolerance = 1;
1242 Transaction().setColor(colorLayer, color).apply();
1243 {
1244 SCOPED_TRACE("new color");
1245 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1246 }
1247}
1248
1249TEST_F(LayerTransactionTest, SetColorClamped) {
1250 sp<SurfaceControl> colorLayer;
1251 ASSERT_NO_FATAL_FAILURE(
1252 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1253
1254 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
1255 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1256}
1257
1258TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1259 sp<SurfaceControl> bufferLayer;
1260 sp<SurfaceControl> colorLayer;
1261 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001262 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001263 ASSERT_NO_FATAL_FAILURE(
1264 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1265
1266 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1267 const float alpha = 0.25f;
1268 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1269 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1270 // channel) should be less than one
1271 const uint8_t tolerance = 1;
1272 Transaction()
1273 .setColor(colorLayer, color)
1274 .setAlpha(colorLayer, alpha)
1275 .setLayer(colorLayer, mLayerZBase + 1)
1276 .apply();
1277 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1278 tolerance);
1279}
1280
Adrian Roosb7a96502018-04-08 11:38:55 -07001281TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1282 sp<SurfaceControl> bufferLayer;
1283 sp<SurfaceControl> parentLayer;
1284 sp<SurfaceControl> colorLayer;
1285 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1286 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001287 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Adrian Roosb7a96502018-04-08 11:38:55 -07001288 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer(
1289 "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1290
1291 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1292 const float alpha = 0.25f;
1293 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1294 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1295 // channel) should be less than one
1296 const uint8_t tolerance = 1;
1297 Transaction()
1298 .reparent(colorLayer, parentLayer->getHandle())
1299 .setColor(colorLayer, color)
1300 .setAlpha(parentLayer, alpha)
1301 .setLayer(parentLayer, mLayerZBase + 1)
1302 .apply();
1303 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1304 tolerance);
1305}
1306
Marissa Wall61c58622018-07-18 10:12:20 -07001307TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001308 sp<SurfaceControl> bufferLayer;
1309 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001310 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001311
1312 // color is ignored
1313 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1314 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1315}
1316
Marissa Wall61c58622018-07-18 10:12:20 -07001317TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001318 sp<SurfaceControl> layer;
1319 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001320 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001321
1322 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1323 {
1324 SCOPED_TRACE("non-existing layer stack");
1325 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1326 }
1327
1328 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1329 {
1330 SCOPED_TRACE("original layer stack");
1331 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1332 }
1333}
1334
Marissa Wall61c58622018-07-18 10:12:20 -07001335TEST_P(LayerTypeTransactionTest, SetMatrixBasic) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001336 sp<SurfaceControl> layer;
1337 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1338 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001339 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001340
1341 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1342 {
1343 SCOPED_TRACE("IDENTITY");
1344 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1345 Color::WHITE);
1346 }
1347
1348 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1349 {
1350 SCOPED_TRACE("FLIP_H");
1351 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1352 Color::BLUE);
1353 }
1354
1355 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1356 {
1357 SCOPED_TRACE("FLIP_V");
1358 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1359 Color::GREEN);
1360 }
1361
1362 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1363 {
1364 SCOPED_TRACE("ROT_90");
1365 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1366 Color::GREEN);
1367 }
1368
1369 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1370 {
1371 SCOPED_TRACE("SCALE");
1372 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1373 Color::WHITE, true /* filtered */);
1374 }
1375}
1376
Marissa Wall61c58622018-07-18 10:12:20 -07001377TEST_P(LayerTypeTransactionTest, SetMatrixRot45) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001378 sp<SurfaceControl> layer;
1379 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1380 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001381 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001382
1383 const float rot = M_SQRT1_2; // 45 degrees
1384 const float trans = M_SQRT2 * 16.0f;
1385 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1386
1387 auto shot = screenshot();
1388 // check a 8x8 region inside each color
1389 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1390 const int32_t halfL = 4;
1391 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1392 };
1393 const int32_t unit = int32_t(trans / 2);
1394 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1395 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1396 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1397 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1398}
1399
Marissa Wall61c58622018-07-18 10:12:20 -07001400void LayerTransactionTest::setMatrixWithResizeHelper(uint32_t layerType) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001401 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07001402 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
1403 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001404
1405 // setMatrix is applied after any pending resize, unlike setPosition
1406 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1407 {
1408 SCOPED_TRACE("resize pending");
1409 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -07001410 Rect rect;
1411 switch (layerType) {
1412 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1413 rect = {0, 0, 32, 32};
1414 break;
1415 case ISurfaceComposerClient::eFXSurfaceBufferState:
1416 rect = {0, 0, 128, 128};
1417 break;
1418 default:
1419 ASSERT_FALSE(true) << "Unsupported layer type";
1420 }
1421 shot->expectColor(rect, Color::RED);
1422 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001423 }
1424
Marissa Wall61c58622018-07-18 10:12:20 -07001425 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001426 {
1427 SCOPED_TRACE("resize applied");
1428 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1429 }
1430}
1431
Marissa Wall61c58622018-07-18 10:12:20 -07001432TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
1433 ASSERT_NO_FATAL_FAILURE(
1434 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1435}
1436
1437TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferState) {
1438 ASSERT_NO_FATAL_FAILURE(
1439 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1440}
1441
1442TEST_P(LayerTypeTransactionTest, SetMatrixWithScaleToWindow) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001443 sp<SurfaceControl> layer;
1444 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001445 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001446
1447 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1448 Transaction()
1449 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1450 .setSize(layer, 64, 64)
1451 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1452 .apply();
1453 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1454}
1455
Marissa Wall61c58622018-07-18 10:12:20 -07001456TEST_P(LayerTypeTransactionTest, SetOverrideScalingModeBasic) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001457 sp<SurfaceControl> layer;
1458 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1459 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001460 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001461
1462 // XXX SCALE_CROP is not respected; calling setSize and
1463 // setOverrideScalingMode in separate transactions does not work
1464 // (b/69315456)
1465 Transaction()
1466 .setSize(layer, 64, 16)
1467 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1468 .apply();
1469 {
1470 SCOPED_TRACE("SCALE_TO_WINDOW");
1471 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1472 Color::WHITE, true /* filtered */);
1473 }
1474}
1475
Dan Stoza000dd012018-08-01 13:31:52 -07001476TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1477 sp<SurfaceControl> layer;
1478 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1479
1480 sp<IBinder> handle = layer->getHandle();
1481 ASSERT_TRUE(handle != nullptr);
1482
1483 FrameStats frameStats;
1484 mClient->getLayerFrameStats(handle, &frameStats);
1485
1486 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1487}
1488
Marissa Wall61c58622018-07-18 10:12:20 -07001489TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001490 sp<SurfaceControl> layer;
1491 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001492 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001493 const Rect crop(8, 8, 24, 24);
1494
Marissa Wallf58c14b2018-07-24 10:50:43 -07001495 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001496 auto shot = screenshot();
1497 shot->expectColor(crop, Color::RED);
1498 shot->expectBorder(crop, Color::BLACK);
1499}
1500
Marissa Wall61c58622018-07-18 10:12:20 -07001501TEST_F(LayerTransactionTest, SetCropBasic_BufferState) {
1502 sp<SurfaceControl> layer;
1503 ASSERT_NO_FATAL_FAILURE(
1504 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1505 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1506 const Rect crop(8, 8, 24, 24);
1507
1508 Transaction().setCrop(layer, crop).apply();
1509 auto shot = screenshot();
1510 shot->expectColor(crop, Color::RED);
1511 shot->expectBorder(crop, Color::BLACK);
1512}
1513
1514TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001515 sp<SurfaceControl> layer;
1516 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001517 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001518
1519 {
1520 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001521 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001522 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1523 }
1524
1525 {
1526 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001527 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001528 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1529 }
1530}
1531
Marissa Wall61c58622018-07-18 10:12:20 -07001532TEST_F(LayerTransactionTest, SetCropEmpty_BufferState) {
1533 sp<SurfaceControl> layer;
1534 ASSERT_NO_FATAL_FAILURE(
1535 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1536 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1537
1538 {
1539 SCOPED_TRACE("empty rect");
1540 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1541 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1542 }
1543
1544 {
1545 SCOPED_TRACE("negative rect");
1546 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1547 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1548 }
1549}
1550
1551TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001552 sp<SurfaceControl> layer;
1553 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001554 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001555
Marissa Wallf58c14b2018-07-24 10:50:43 -07001556 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001557 auto shot = screenshot();
1558 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1559 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1560}
1561
Marissa Wall61c58622018-07-18 10:12:20 -07001562TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1563 sp<SurfaceControl> layer;
1564 ASSERT_NO_FATAL_FAILURE(
1565 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1566 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1567
1568 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1569 auto shot = screenshot();
1570 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1571 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1572}
1573
1574TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001575 sp<SurfaceControl> layer;
1576 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001577 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001578
1579 const Point position(32, 32);
1580 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001581 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001582 auto shot = screenshot();
1583 shot->expectColor(crop + position, Color::RED);
1584 shot->expectBorder(crop + position, Color::BLACK);
1585}
1586
Marissa Wall61c58622018-07-18 10:12:20 -07001587TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1588 sp<SurfaceControl> layer;
1589 ASSERT_NO_FATAL_FAILURE(
1590 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1591 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1592
1593 const Point position(32, 32);
1594 const Rect crop(8, 8, 24, 24);
1595 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1596 auto shot = screenshot();
1597 shot->expectColor(crop + position, Color::RED);
1598 shot->expectBorder(crop + position, Color::BLACK);
1599}
1600
1601TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001602 sp<SurfaceControl> layer;
1603 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001604 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001605
1606 // crop is affected by matrix
1607 Transaction()
1608 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001609 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001610 .apply();
1611 auto shot = screenshot();
1612 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1613 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1614}
1615
Marissa Wall61c58622018-07-18 10:12:20 -07001616TEST_F(LayerTransactionTest, SetCropWithScale_BufferState) {
1617 sp<SurfaceControl> layer;
1618 ASSERT_NO_FATAL_FAILURE(
1619 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1620 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1621
1622 // crop is affected by matrix
1623 Transaction()
1624 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1625 .setCrop(layer, Rect(8, 8, 24, 24))
1626 .apply();
1627 auto shot = screenshot();
1628 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1629 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1630}
1631
1632TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001633 sp<SurfaceControl> layer;
1634 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001635 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001636
Marissa Wallf58c14b2018-07-24 10:50:43 -07001637 // setCrop_legacy is applied immediately by default, with or without resize pending
1638 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001639 {
1640 SCOPED_TRACE("resize pending");
1641 auto shot = screenshot();
1642 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1643 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1644 }
1645
Marissa Wall61c58622018-07-18 10:12:20 -07001646 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001647 {
1648 SCOPED_TRACE("resize applied");
1649 auto shot = screenshot();
1650 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1651 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1652 }
1653}
1654
Marissa Wall61c58622018-07-18 10:12:20 -07001655TEST_F(LayerTransactionTest, SetCropWithResize_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 // setCrop_legacy is applied immediately by default, with or without resize pending
1662 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1663 {
1664 SCOPED_TRACE("new buffer pending");
1665 auto shot = screenshot();
1666 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1667 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1668 }
1669
1670 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1671 {
1672 SCOPED_TRACE("new buffer");
1673 auto shot = screenshot();
1674 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1675 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1676 }
1677}
1678
1679TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001680 sp<SurfaceControl> layer;
1681 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001682 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001683
Marissa Wallf58c14b2018-07-24 10:50:43 -07001684 // request setCrop_legacy to be applied with the next resize
1685 Transaction()
1686 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1687 .setGeometryAppliesWithResize(layer)
1688 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001689 {
1690 SCOPED_TRACE("waiting for next resize");
1691 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1692 }
1693
Marissa Wallf58c14b2018-07-24 10:50:43 -07001694 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001695 {
1696 SCOPED_TRACE("pending crop modified");
1697 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1698 }
1699
1700 Transaction().setSize(layer, 16, 16).apply();
1701 {
1702 SCOPED_TRACE("resize pending");
1703 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1704 }
1705
1706 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001707 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001708 {
1709 SCOPED_TRACE("new crop applied");
1710 auto shot = screenshot();
1711 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1712 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1713 }
1714}
1715
Marissa Wall61c58622018-07-18 10:12:20 -07001716TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferState) {
1717 sp<SurfaceControl> layer;
1718 ASSERT_NO_FATAL_FAILURE(
1719 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1720 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1721
1722 // request setCrop_legacy to be applied with the next resize
1723 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1724 {
1725 SCOPED_TRACE("set crop 1");
1726 screenshot()->expectColor(Rect(8, 8, 24, 24), Color::RED);
1727 }
1728
1729 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1730 {
1731 SCOPED_TRACE("set crop 2");
1732 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1733 }
1734
1735 Transaction().setSize(layer, 16, 16).apply();
1736 {
1737 SCOPED_TRACE("resize");
1738 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1739 }
1740
1741 // finally resize
1742 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1743 {
1744 SCOPED_TRACE("new buffer");
1745 auto shot = screenshot();
1746 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1747 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1748 }
1749}
1750
1751TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001752 sp<SurfaceControl> layer;
1753 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001754 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001755
Marissa Wallf58c14b2018-07-24 10:50:43 -07001756 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001757 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001758 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001759 .setSize(layer, 16, 16)
1760 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1761 .setGeometryAppliesWithResize(layer)
1762 .apply();
1763 {
1764 SCOPED_TRACE("new crop pending");
1765 auto shot = screenshot();
1766 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1767 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1768 }
1769
1770 // XXX crop is never latched without other geometry change (b/69315677)
1771 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001772 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001773 Transaction().setPosition(layer, 0, 0).apply();
1774 {
1775 SCOPED_TRACE("new crop applied");
1776 auto shot = screenshot();
1777 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1778 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1779 }
1780}
1781
Marissa Wall61c58622018-07-18 10:12:20 -07001782TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferState) {
1783 sp<SurfaceControl> layer;
1784 ASSERT_NO_FATAL_FAILURE(
1785 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1786 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1787
1788 // all properties are applied immediate so setGeometryAppliesWithResize has no effect
1789 Transaction()
1790 .setCrop(layer, Rect(4, 4, 12, 12))
1791 .setSize(layer, 16, 16)
1792 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1793 .setGeometryAppliesWithResize(layer)
1794 .apply();
1795 {
1796 SCOPED_TRACE("new crop pending");
1797 auto shot = screenshot();
1798 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1799 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1800 }
1801
1802 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1803 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1804 Transaction().setPosition(layer, 0, 0).apply();
1805 {
1806 SCOPED_TRACE("new crop applied");
1807 auto shot = screenshot();
1808 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1809 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1810 }
1811}
1812
Marissa Wall61c58622018-07-18 10:12:20 -07001813TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1814 sp<SurfaceControl> layer;
1815 ASSERT_NO_FATAL_FAILURE(
1816 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1817
1818 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1819
1820 auto shot = screenshot();
1821 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1822 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1823}
1824
1825TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1826 sp<SurfaceControl> layer;
1827 ASSERT_NO_FATAL_FAILURE(
1828 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1829
1830 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1831
1832 {
1833 SCOPED_TRACE("set buffer 1");
1834 auto shot = screenshot();
1835 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1836 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1837 }
1838
1839 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
1840
1841 {
1842 SCOPED_TRACE("set buffer 2");
1843 auto shot = screenshot();
1844 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1845 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1846 }
1847
1848 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1849
1850 {
1851 SCOPED_TRACE("set buffer 3");
1852 auto shot = screenshot();
1853 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1854 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1855 }
1856}
1857
1858TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
1859 sp<SurfaceControl> layer1;
1860 ASSERT_NO_FATAL_FAILURE(
1861 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
1862
1863 sp<SurfaceControl> layer2;
1864 ASSERT_NO_FATAL_FAILURE(
1865 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1866
1867 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
1868
1869 {
1870 SCOPED_TRACE("set layer 1 buffer red");
1871 auto shot = screenshot();
1872 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1873 }
1874
1875 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
1876
1877 {
1878 SCOPED_TRACE("set layer 2 buffer blue");
1879 auto shot = screenshot();
1880 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1881 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
1882 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
1883 }
1884
1885 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
1886 {
1887 SCOPED_TRACE("set layer 1 buffer green");
1888 auto shot = screenshot();
1889 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1890 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1891 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1892 }
1893
1894 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
1895
1896 {
1897 SCOPED_TRACE("set layer 2 buffer white");
1898 auto shot = screenshot();
1899 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
1900 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1901 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1902 }
1903}
1904
1905TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
1906 sp<SurfaceControl> layer;
1907 ASSERT_NO_FATAL_FAILURE(
1908 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1909
1910 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1911 Color::BLUE, Color::WHITE));
1912
1913 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90).apply();
1914
1915 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1916 Color::GREEN, true /* filtered */);
1917}
1918
1919TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
1920 sp<SurfaceControl> layer;
1921 ASSERT_NO_FATAL_FAILURE(
1922 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1923
1924 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1925 Color::BLUE, Color::WHITE));
1926
1927 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H).apply();
1928
1929 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1930 Color::BLUE, true /* filtered */);
1931}
1932
1933TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
1934 sp<SurfaceControl> layer;
1935 ASSERT_NO_FATAL_FAILURE(
1936 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1937
1938 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1939 Color::BLUE, Color::WHITE));
1940
1941 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V).apply();
1942
1943 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1944 Color::GREEN, true /* filtered */);
1945}
1946
1947TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
1948 sp<SurfaceControl> layer;
1949 ASSERT_NO_FATAL_FAILURE(
1950 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1951
1952 Transaction().setTransformToDisplayInverse(layer, false).apply();
1953
1954 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
1955
1956 Transaction().setTransformToDisplayInverse(layer, true).apply();
1957}
1958
1959TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
1960 sp<SurfaceControl> layer;
1961 ASSERT_NO_FATAL_FAILURE(
1962 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1963
1964 sp<GraphicBuffer> buffer =
1965 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1966 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1967 BufferUsage::COMPOSER_OVERLAY,
1968 "test");
1969 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1970
Marissa Wallfda30bb2018-10-12 11:34:28 -07001971 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07001972
1973 Transaction()
1974 .setBuffer(layer, buffer)
1975 .setAcquireFence(layer, fence)
1976 .setSize(layer, 32, 32)
1977 .apply();
1978
1979 auto shot = screenshot();
1980 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1981 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1982}
1983
1984TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
1985 sp<SurfaceControl> layer;
1986 ASSERT_NO_FATAL_FAILURE(
1987 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1988
1989 sp<GraphicBuffer> buffer =
1990 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1991 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1992 BufferUsage::COMPOSER_OVERLAY,
1993 "test");
1994 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1995
1996 Transaction()
1997 .setBuffer(layer, buffer)
1998 .setDataspace(layer, ui::Dataspace::UNKNOWN)
1999 .setSize(layer, 32, 32)
2000 .apply();
2001
2002 auto shot = screenshot();
2003 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2004 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2005}
2006
2007TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
2008 sp<SurfaceControl> layer;
2009 ASSERT_NO_FATAL_FAILURE(
2010 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2011
2012 sp<GraphicBuffer> buffer =
2013 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2014 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2015 BufferUsage::COMPOSER_OVERLAY,
2016 "test");
2017 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2018
2019 HdrMetadata hdrMetadata;
2020 hdrMetadata.validTypes = 0;
2021 Transaction()
2022 .setBuffer(layer, buffer)
2023 .setHdrMetadata(layer, hdrMetadata)
2024 .setSize(layer, 32, 32)
2025 .apply();
2026
2027 auto shot = screenshot();
2028 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2029 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2030}
2031
2032TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2033 sp<SurfaceControl> layer;
2034 ASSERT_NO_FATAL_FAILURE(
2035 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2036
2037 sp<GraphicBuffer> buffer =
2038 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2039 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2040 BufferUsage::COMPOSER_OVERLAY,
2041 "test");
2042 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2043
2044 Region region;
2045 region.set(32, 32);
2046 Transaction()
2047 .setBuffer(layer, buffer)
2048 .setSurfaceDamageRegion(layer, region)
2049 .setSize(layer, 32, 32)
2050 .apply();
2051
2052 auto shot = screenshot();
2053 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2054 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2055}
2056
2057TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2058 sp<SurfaceControl> layer;
2059 ASSERT_NO_FATAL_FAILURE(
2060 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2061
2062 sp<GraphicBuffer> buffer =
2063 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2064 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2065 BufferUsage::COMPOSER_OVERLAY,
2066 "test");
2067 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2068
2069 Transaction()
2070 .setBuffer(layer, buffer)
2071 .setApi(layer, NATIVE_WINDOW_API_CPU)
2072 .setSize(layer, 32, 32)
2073 .apply();
2074
2075 auto shot = screenshot();
2076 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2077 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2078}
2079
2080TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2081 sp<SurfaceControl> layer;
2082 ASSERT_NO_FATAL_FAILURE(
2083 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2084
2085 // verify this doesn't cause a crash
2086 Transaction().setSidebandStream(layer, nullptr).apply();
2087}
2088
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002089class ColorTransformHelper {
2090public:
2091 static void DegammaColorSingle(half& s) {
2092 if (s <= 0.03928f)
2093 s = s / 12.92f;
2094 else
2095 s = pow((s + 0.055f) / 1.055f, 2.4f);
2096 }
2097
2098 static void DegammaColor(half3& color) {
2099 DegammaColorSingle(color.r);
2100 DegammaColorSingle(color.g);
2101 DegammaColorSingle(color.b);
2102 }
2103
2104 static void GammaColorSingle(half& s) {
2105 if (s <= 0.0031308f) {
2106 s = s * 12.92f;
2107 } else {
2108 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2109 }
2110 }
2111
2112 static void GammaColor(half3& color) {
2113 GammaColorSingle(color.r);
2114 GammaColorSingle(color.g);
2115 GammaColorSingle(color.b);
2116 }
2117
2118 static void applyMatrix(half3& color, const mat3& mat) {
2119 half3 ret = half3(0);
2120
2121 for (int i = 0; i < 3; i++) {
2122 for (int j = 0; j < 3; j++) {
2123 ret[i] = ret[i] + color[j] * mat[j][i];
2124 }
2125 }
2126 color = ret;
2127 }
2128};
2129
Peiyong Lind3788632018-09-18 16:01:31 -07002130TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2131 sp<SurfaceControl> colorLayer;
2132 ASSERT_NO_FATAL_FAILURE(
2133 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
2134
2135 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
2136 {
2137 SCOPED_TRACE("default color");
2138 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2139 }
2140
2141 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002142 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002143 mat3 matrix;
2144 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2145 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2146 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002147
2148 // degamma before applying the matrix
2149 if (mColorManagementUsed) {
2150 ColorTransformHelper::DegammaColor(expected);
2151 }
2152
2153 ColorTransformHelper::applyMatrix(expected, matrix);
2154
2155 if (mColorManagementUsed) {
2156 ColorTransformHelper::GammaColor(expected);
2157 }
2158
2159 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2160 uint8_t(expected.b * 255), 255};
2161
2162 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2163 // channel) should be less than one
2164 const uint8_t tolerance = 1;
2165
Peiyong Lind3788632018-09-18 16:01:31 -07002166 Transaction().setColor(colorLayer, color)
2167 .setColorTransform(colorLayer, matrix, vec3()).apply();
2168 {
2169 SCOPED_TRACE("new color");
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002170 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002171 }
2172}
2173
Marissa Wallfda30bb2018-10-12 11:34:28 -07002174class ExpectedResult {
2175public:
2176 enum Transaction {
2177 NOT_PRESENTED = 0,
2178 PRESENTED,
2179 };
2180
2181 enum Buffer {
2182 NOT_ACQUIRED = 0,
2183 ACQUIRED,
2184 };
2185
2186 enum PreviousBuffer {
2187 NOT_RELEASED = 0,
2188 RELEASED,
2189 };
2190
2191 void reset() {
2192 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2193 mExpectedSurfaceResults.clear();
2194 }
2195
2196 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
2197 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2198 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2199 mTransactionResult = transactionResult;
2200 mExpectedSurfaceResults.emplace(std::piecewise_construct,
2201 std::forward_as_tuple(layer->getHandle()),
2202 std::forward_as_tuple(bufferResult, previousBufferResult));
2203 }
2204
2205 void addSurfaces(ExpectedResult::Transaction transactionResult,
2206 const std::vector<sp<SurfaceControl>>& layers,
2207 ExpectedResult::Buffer bufferResult = NOT_ACQUIRED,
2208 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2209 for (const auto& layer : layers) {
2210 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
2211 }
2212 }
2213
2214 void verifyTransactionStats(const TransactionStats& transactionStats) const {
2215 const auto& [latchTime, presentTime, surfaceStats] = transactionStats;
2216 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
2217 ASSERT_GE(latchTime, 0) << "bad latch time";
2218 ASSERT_GE(presentTime, 0) << "bad present time";
2219 } else {
2220 ASSERT_EQ(presentTime, -1) << "transaction shouldn't have been presented";
2221 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
2222 }
2223
2224 ASSERT_EQ(surfaceStats.size(), mExpectedSurfaceResults.size())
2225 << "wrong number of surfaces";
2226
2227 for (const auto& stats : surfaceStats) {
2228 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
2229 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
2230 << "unexpected surface control";
2231 expectedSurfaceResult->second.verifySurfaceStats(stats, latchTime);
2232 }
2233 }
2234
2235private:
2236 class ExpectedSurfaceResult {
2237 public:
2238 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
2239 ExpectedResult::PreviousBuffer previousBufferResult)
2240 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
2241
2242 void verifySurfaceStats(const SurfaceStats& surfaceStats, nsecs_t latchTime) const {
2243 const auto& [surfaceControl, acquireTime, releasePreviousBuffer] = surfaceStats;
2244
2245 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
2246 << "bad acquire time";
2247 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
2248 ASSERT_EQ(releasePreviousBuffer,
2249 mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED)
2250 << "bad previous buffer released";
2251 }
2252
2253 private:
2254 ExpectedResult::Buffer mBufferResult;
2255 ExpectedResult::PreviousBuffer mPreviousBufferResult;
2256 };
2257
2258 struct IBinderHash {
2259 std::size_t operator()(const sp<IBinder>& strongPointer) const {
2260 return std::hash<IBinder*>{}(strongPointer.get());
2261 }
2262 };
2263 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2264 std::unordered_map<sp<IBinder>, ExpectedSurfaceResult, IBinderHash> mExpectedSurfaceResults;
2265};
2266
2267class CallbackHelper {
2268public:
2269 static void function(void* callbackContext, const TransactionStats& transactionStats) {
2270 if (!callbackContext) {
2271 ALOGE("failed to get callback context");
2272 }
2273 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
2274 std::lock_guard lock(helper->mMutex);
2275 helper->mTransactionStatsQueue.push(transactionStats);
2276 helper->mConditionVariable.notify_all();
2277 }
2278
2279 void getTransactionStats(TransactionStats* outStats) {
2280 std::unique_lock lock(mMutex);
2281
2282 if (mTransactionStatsQueue.empty()) {
2283 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
2284 std::cv_status::timeout)
2285 << "did not receive callback";
2286 }
2287
2288 *outStats = std::move(mTransactionStatsQueue.front());
2289 mTransactionStatsQueue.pop();
2290 }
2291
2292 void verifyFinalState() {
2293 // Wait to see if there are extra callbacks
2294 std::this_thread::sleep_for(500ms);
2295
2296 std::lock_guard lock(mMutex);
2297 EXPECT_EQ(mTransactionStatsQueue.size(), 0) << "extra callbacks received";
2298 mTransactionStatsQueue = {};
2299 }
2300
2301 void* getContext() { return static_cast<void*>(this); }
2302
2303 std::mutex mMutex;
2304 std::condition_variable mConditionVariable;
2305 std::queue<TransactionStats> mTransactionStatsQueue;
2306};
2307
2308class LayerCallbackTest : public LayerTransactionTest {
2309protected:
2310 virtual sp<SurfaceControl> createBufferStateLayer() {
2311 return createLayer(mClient, "test", mWidth, mHeight,
2312 ISurfaceComposerClient::eFXSurfaceBufferState);
2313 }
2314
2315 virtual void fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
2316 const sp<SurfaceControl>& layer = nullptr) {
2317 if (layer) {
2318 sp<GraphicBuffer> buffer =
2319 new GraphicBuffer(mWidth, mHeight, PIXEL_FORMAT_RGBA_8888, 1,
2320 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2321 BufferUsage::COMPOSER_OVERLAY |
2322 BufferUsage::GPU_TEXTURE,
2323 "test");
2324 fillGraphicBufferColor(buffer, Rect(0, 0, mWidth, mHeight), Color::RED);
2325
2326 sp<Fence> fence = new Fence(-1);
2327
2328 transaction.setBuffer(layer, buffer)
2329 .setAcquireFence(layer, fence)
2330 .setSize(layer, mWidth, mHeight);
2331 }
2332
2333 transaction.addTransactionCompletedCallback(callbackHelper->function,
2334 callbackHelper->getContext());
2335 }
2336
2337 void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2338 bool finalState = false) {
2339 TransactionStats transactionStats;
2340 ASSERT_NO_FATAL_FAILURE(helper.getTransactionStats(&transactionStats));
2341 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyTransactionStats(transactionStats));
2342
2343 if (finalState) {
2344 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2345 }
2346 }
2347
2348 void waitForCallbacks(CallbackHelper& helper,
2349 const std::vector<ExpectedResult>& expectedResults,
2350 bool finalState = false) {
2351 for (const auto& expectedResult : expectedResults) {
2352 waitForCallback(helper, expectedResult);
2353 }
2354 if (finalState) {
2355 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2356 }
2357 }
2358
2359 uint32_t mWidth = 32;
2360 uint32_t mHeight = 32;
2361};
2362
2363TEST_F(LayerCallbackTest, Basic) {
2364 sp<SurfaceControl> layer;
2365 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2366
2367 Transaction transaction;
2368 CallbackHelper callback;
2369 fillTransaction(transaction, &callback, layer);
2370
2371 transaction.apply();
2372
2373 ExpectedResult expected;
2374 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2375 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2376}
2377
2378TEST_F(LayerCallbackTest, NoBuffer) {
2379 sp<SurfaceControl> layer;
2380 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2381
2382 Transaction transaction;
2383 CallbackHelper callback;
2384 fillTransaction(transaction, &callback);
2385
2386 transaction.setPosition(layer, mWidth, mHeight).apply();
2387
2388 ExpectedResult expected;
2389 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2390 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2391}
2392
2393TEST_F(LayerCallbackTest, NoStateChange) {
2394 Transaction transaction;
2395 CallbackHelper callback;
2396 fillTransaction(transaction, &callback);
2397
2398 transaction.apply();
2399
2400 ExpectedResult expected;
2401 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2402}
2403
2404TEST_F(LayerCallbackTest, OffScreen) {
2405 sp<SurfaceControl> layer;
2406 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2407
2408 Transaction transaction;
2409 CallbackHelper callback;
2410 fillTransaction(transaction, &callback, layer);
2411
2412 transaction.setPosition(layer, -100, -100).apply();
2413
2414 ExpectedResult expected;
2415 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2416 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2417}
2418
2419TEST_F(LayerCallbackTest, Merge) {
2420 sp<SurfaceControl> layer1, layer2;
2421 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2422 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2423
2424 Transaction transaction1, transaction2;
2425 CallbackHelper callback1, callback2;
2426 fillTransaction(transaction1, &callback1, layer1);
2427 fillTransaction(transaction2, &callback2, layer2);
2428
2429 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2430
2431 ExpectedResult expected;
2432 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2433 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2434 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2435}
2436
2437TEST_F(LayerCallbackTest, Merge_SameCallback) {
2438 sp<SurfaceControl> layer1, layer2;
2439 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2440 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2441
2442 Transaction transaction1, transaction2;
2443 CallbackHelper callback;
2444 fillTransaction(transaction1, &callback, layer1);
2445 fillTransaction(transaction2, &callback, layer2);
2446
2447 transaction2.merge(std::move(transaction1)).apply();
2448
2449 ExpectedResult expected;
2450 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2451 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2452 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2453}
2454
2455TEST_F(LayerCallbackTest, Merge_SameLayer) {
2456 sp<SurfaceControl> layer;
2457 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2458
2459 Transaction transaction1, transaction2;
2460 CallbackHelper callback1, callback2;
2461 fillTransaction(transaction1, &callback1, layer);
2462 fillTransaction(transaction2, &callback2, layer);
2463
2464 transaction2.merge(std::move(transaction1)).apply();
2465
2466 ExpectedResult expected;
2467 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2468 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2469 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2470}
2471
2472TEST_F(LayerCallbackTest, Merge_SingleBuffer) {
2473 sp<SurfaceControl> layer1, layer2;
2474 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2475 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2476
2477 Transaction transaction1, transaction2;
2478 CallbackHelper callback1, callback2;
2479 fillTransaction(transaction1, &callback1, layer1);
2480 fillTransaction(transaction2, &callback2);
2481
2482 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2483
2484 ExpectedResult expected;
2485 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2486 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2487 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2488}
2489
2490TEST_F(LayerCallbackTest, Merge_DifferentClients) {
2491 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2492 client2(new SurfaceComposerClient);
2493
2494 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2495 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2496
2497 sp<SurfaceControl> layer1, layer2;
2498 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2499 ISurfaceComposerClient::eFXSurfaceBufferState));
2500 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2501 ISurfaceComposerClient::eFXSurfaceBufferState));
2502
2503 Transaction transaction1, transaction2;
2504 CallbackHelper callback1, callback2;
2505 fillTransaction(transaction1, &callback1, layer1);
2506 fillTransaction(transaction2, &callback2, layer2);
2507
2508 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2509
2510 ExpectedResult expected;
2511 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2512 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2513 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2514}
2515
2516TEST_F(LayerCallbackTest, MultipleTransactions) {
2517 sp<SurfaceControl> layer;
2518 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2519
2520 Transaction transaction;
2521 CallbackHelper callback;
2522 for (size_t i = 0; i < 10; i++) {
2523 fillTransaction(transaction, &callback, layer);
2524
2525 transaction.apply();
2526
2527 ExpectedResult expected;
2528 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2529 ExpectedResult::Buffer::NOT_ACQUIRED,
2530 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2531 : ExpectedResult::PreviousBuffer::RELEASED);
2532 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2533 }
2534 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2535}
2536
2537TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
2538 sp<SurfaceControl> layer;
2539 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2540
2541 Transaction transaction;
2542 CallbackHelper callback;
2543 for (size_t i = 0; i < 10; i++) {
2544 ExpectedResult expected;
2545
2546 if (i == 0) {
2547 fillTransaction(transaction, &callback, layer);
2548 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2549 } else {
2550 fillTransaction(transaction, &callback);
2551 }
2552
2553 transaction.apply();
2554
2555 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2556 }
2557 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2558}
2559
2560TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
2561 sp<SurfaceControl> layer;
2562 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2563
2564 Transaction transaction;
2565 CallbackHelper callback;
2566 for (size_t i = 0; i < 10; i++) {
2567 if (i == 0) {
2568 fillTransaction(transaction, &callback, layer);
2569 } else {
2570 fillTransaction(transaction, &callback);
2571 }
2572
2573 transaction.setPosition(layer, mWidth, mHeight).apply();
2574
2575 ExpectedResult expected;
2576 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
2577 : ExpectedResult::Transaction::NOT_PRESENTED,
2578 layer);
2579 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
2580 }
2581 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
2582}
2583
2584TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
2585 sp<SurfaceControl> layer1, layer2;
2586 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2587 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2588
2589 Transaction transaction1, transaction2;
2590 CallbackHelper callback1, callback2;
2591 for (size_t i = 0; i < 10; i++) {
2592 fillTransaction(transaction1, &callback1, layer1);
2593 fillTransaction(transaction2, &callback2, layer2);
2594
2595 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2596
2597 ExpectedResult expected;
2598 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2599 ExpectedResult::Buffer::NOT_ACQUIRED,
2600 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2601 : ExpectedResult::PreviousBuffer::RELEASED);
2602 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2603 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2604 }
2605 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2606 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2607}
2608
2609TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
2610 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2611 client2(new SurfaceComposerClient);
2612 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2613 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2614
2615 sp<SurfaceControl> layer1, layer2;
2616 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2617 ISurfaceComposerClient::eFXSurfaceBufferState));
2618 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2619 ISurfaceComposerClient::eFXSurfaceBufferState));
2620
2621 Transaction transaction1, transaction2;
2622 CallbackHelper callback1, callback2;
2623 for (size_t i = 0; i < 10; i++) {
2624 fillTransaction(transaction1, &callback1, layer1);
2625 fillTransaction(transaction2, &callback2, layer2);
2626
2627 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2628
2629 ExpectedResult expected;
2630 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2631 ExpectedResult::Buffer::NOT_ACQUIRED,
2632 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
2633 : ExpectedResult::PreviousBuffer::RELEASED);
2634 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
2635 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
2636 }
2637 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
2638 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
2639}
2640
2641TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
2642 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2643 client2(new SurfaceComposerClient);
2644 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2645 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2646
2647 sp<SurfaceControl> layer1, layer2;
2648 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2649 ISurfaceComposerClient::eFXSurfaceBufferState));
2650 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2651 ISurfaceComposerClient::eFXSurfaceBufferState));
2652
2653 Transaction transaction1, transaction2;
2654 CallbackHelper callback1, callback2;
2655
2656 // Normal call to set up test
2657 fillTransaction(transaction1, &callback1, layer1);
2658 fillTransaction(transaction2, &callback2, layer2);
2659
2660 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2661
2662 ExpectedResult expected;
2663 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2664 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2665 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2666 expected.reset();
2667
2668 // Test
2669 fillTransaction(transaction1, &callback1);
2670 fillTransaction(transaction2, &callback2);
2671
2672 transaction2.merge(std::move(transaction1)).apply();
2673
2674 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2675 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2676}
2677
2678TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
2679 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2680 client2(new SurfaceComposerClient);
2681
2682 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2683 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2684
2685 sp<SurfaceControl> layer1, layer2;
2686 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", mWidth, mHeight,
2687 ISurfaceComposerClient::eFXSurfaceBufferState));
2688 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", mWidth, mHeight,
2689 ISurfaceComposerClient::eFXSurfaceBufferState));
2690
2691 Transaction transaction1, transaction2;
2692 CallbackHelper callback1, callback2;
2693
2694 // Normal call to set up test
2695 fillTransaction(transaction1, &callback1, layer1);
2696 fillTransaction(transaction2, &callback2, layer2);
2697
2698 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2699
2700 ExpectedResult expected;
2701 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2702 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2703 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2704 expected.reset();
2705
2706 // Test
2707 fillTransaction(transaction1, &callback1);
2708 fillTransaction(transaction2, &callback2);
2709
2710 transaction2.setPosition(layer2, mWidth, mHeight).merge(std::move(transaction1)).apply();
2711
2712 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2);
2713 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2714 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2715}
2716
2717TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
2718 sp<SurfaceControl> layer;
2719 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2720
2721 Transaction transaction;
2722 CallbackHelper callback;
2723 std::vector<ExpectedResult> expectedResults(50);
2724 ExpectedResult::PreviousBuffer previousBufferResult =
2725 ExpectedResult::PreviousBuffer::NOT_RELEASED;
2726 for (auto& expected : expectedResults) {
2727 expected.reset();
2728 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2729 ExpectedResult::Buffer::NOT_ACQUIRED, previousBufferResult);
2730 previousBufferResult = ExpectedResult::PreviousBuffer::RELEASED;
2731
2732 fillTransaction(transaction, &callback, layer);
2733
2734 transaction.apply();
2735 std::this_thread::sleep_for(200ms);
2736 }
2737 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2738}
2739
2740TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
2741 sp<SurfaceControl> layer;
2742 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2743
2744 Transaction transaction;
2745 CallbackHelper callback;
2746 std::vector<ExpectedResult> expectedResults(50);
2747 bool first = true;
2748 for (auto& expected : expectedResults) {
2749 expected.reset();
2750
2751 if (first) {
2752 fillTransaction(transaction, &callback, layer);
2753 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2754 first = false;
2755 } else {
2756 fillTransaction(transaction, &callback);
2757 }
2758
2759 transaction.apply();
2760 std::this_thread::sleep_for(200ms);
2761 }
2762 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2763}
2764
2765TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
2766 sp<SurfaceControl> layer;
2767 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2768
2769 // Normal call to set up test
2770 Transaction transaction;
2771 CallbackHelper callback;
2772 fillTransaction(transaction, &callback, layer);
2773
2774 transaction.setPosition(layer, mWidth, mHeight).apply();
2775
2776 ExpectedResult expectedResult;
2777 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2778 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
2779
2780 // Test
2781 std::vector<ExpectedResult> expectedResults(50);
2782 for (auto& expected : expectedResults) {
2783 expected.reset();
2784 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer);
2785
2786 fillTransaction(transaction, &callback);
2787
2788 transaction.setPosition(layer, mWidth, mHeight).apply();
2789
2790 std::this_thread::sleep_for(200ms);
2791 }
2792 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
2793}
2794
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002795class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002796protected:
2797 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07002798 LayerTransactionTest::SetUp();
2799 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002800
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002801 sp<IBinder> display(
2802 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07002803 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002804 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07002805
2806 ssize_t displayWidth = info.w;
2807 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002808
2809 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07002810 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
2811 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002812 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002813 ASSERT_TRUE(mBGSurfaceControl->isValid());
2814 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
2815
2816 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07002817 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
2818
Peiyong Lin566a3b42018-01-09 18:22:43 -08002819 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002820 ASSERT_TRUE(mFGSurfaceControl->isValid());
2821
2822 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2823
2824 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07002825 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002826 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002827 ASSERT_TRUE(mSyncSurfaceControl->isValid());
2828
2829 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2830
Robert Carr4cdc58f2017-08-23 14:22:20 -07002831 asTransaction([&](Transaction& t) {
2832 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002833
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002834 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07002835
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002836 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
2837 .setPosition(mFGSurfaceControl, 64, 64)
2838 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002839
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002840 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
2841 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
2842 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002843 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002844 }
2845
2846 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07002847 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002848 mBGSurfaceControl = 0;
2849 mFGSurfaceControl = 0;
2850 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002851 }
2852
2853 void waitForPostedBuffers() {
2854 // Since the sync surface is in synchronous mode (i.e. double buffered)
2855 // posting three buffers to it should ensure that at least two
2856 // SurfaceFlinger::handlePageFlip calls have been made, which should
2857 // guaranteed that a buffer posted to another Surface has been retired.
2858 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2859 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2860 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2861 }
2862
Robert Carr4cdc58f2017-08-23 14:22:20 -07002863 void asTransaction(const std::function<void(Transaction&)>& exec) {
2864 Transaction t;
2865 exec(t);
2866 t.apply(true);
2867 }
2868
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002869 sp<SurfaceControl> mBGSurfaceControl;
2870 sp<SurfaceControl> mFGSurfaceControl;
2871
2872 // This surface is used to ensure that the buffers posted to
2873 // mFGSurfaceControl have been picked up by SurfaceFlinger.
2874 sp<SurfaceControl> mSyncSurfaceControl;
2875};
2876
Robert Carr7f619b22017-11-06 12:56:35 -08002877TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08002878
chaviw0e3479f2018-09-10 16:49:30 -07002879 std::unique_ptr<ScreenCapture> sc;
2880
2881 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08002882 fillSurfaceRGBA8(relative, 10, 10, 10);
2883 waitForPostedBuffers();
2884
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002885 Transaction{}
2886 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08002887 .setPosition(relative, 64, 64)
2888 .apply();
2889
2890 {
2891 // The relative should be on top of the FG control.
2892 ScreenCapture::captureScreen(&sc);
2893 sc->checkPixel(64, 64, 10, 10, 10);
2894 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002895 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002896
2897 {
2898 // Nothing should change at this point.
2899 ScreenCapture::captureScreen(&sc);
2900 sc->checkPixel(64, 64, 10, 10, 10);
2901 }
2902
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002903 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002904
2905 {
2906 // Ensure that the relative was actually hidden, rather than
2907 // being left in the detached but visible state.
2908 ScreenCapture::captureScreen(&sc);
2909 sc->expectFGColor(64, 64);
2910 }
2911}
2912
Robert Carr8d5227b2017-03-16 15:41:03 -07002913class GeometryLatchingTest : public LayerUpdateTest {
2914protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002915 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07002916 SCOPED_TRACE(trace);
2917 ScreenCapture::captureScreen(&sc);
2918 // We find the leading edge of the FG surface.
2919 sc->expectFGColor(127, 127);
2920 sc->expectBGColor(128, 128);
2921 }
Robert Carr7bf247e2017-05-18 14:02:49 -07002922
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002923 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07002924
2925 void unlockFGBuffer() {
2926 sp<Surface> s = mFGSurfaceControl->getSurface();
2927 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
2928 waitForPostedBuffers();
2929 }
2930
Robert Carr8d5227b2017-03-16 15:41:03 -07002931 void completeFGResize() {
2932 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2933 waitForPostedBuffers();
2934 }
2935 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002936 asTransaction([&](Transaction& t) {
2937 t.setSize(mFGSurfaceControl, 64, 64);
2938 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002939 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002940 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002941
2942 EXPECT_INITIAL_STATE("After restoring initial state");
2943 }
chaviw0e3479f2018-09-10 16:49:30 -07002944 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07002945};
2946
Robert Carr8d5227b2017-03-16 15:41:03 -07002947class CropLatchingTest : public GeometryLatchingTest {
2948protected:
2949 void EXPECT_CROPPED_STATE(const char* trace) {
2950 SCOPED_TRACE(trace);
2951 ScreenCapture::captureScreen(&sc);
2952 // The edge should be moved back one pixel by our crop.
2953 sc->expectFGColor(126, 126);
2954 sc->expectBGColor(127, 127);
2955 sc->expectBGColor(128, 128);
2956 }
chaviw59f5c562017-06-28 16:39:06 -07002957
2958 void EXPECT_RESIZE_STATE(const char* trace) {
2959 SCOPED_TRACE(trace);
2960 ScreenCapture::captureScreen(&sc);
2961 // The FG is now resized too 128,128 at 64,64
2962 sc->expectFGColor(64, 64);
2963 sc->expectFGColor(191, 191);
2964 sc->expectBGColor(192, 192);
2965 }
Robert Carr8d5227b2017-03-16 15:41:03 -07002966};
2967
Pablo Ceballos05289c22016-04-14 15:49:55 -07002968TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07002969 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07002970 {
2971 SCOPED_TRACE("before anything");
2972 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002973 sc->expectBGColor(32, 32);
2974 sc->expectFGColor(96, 96);
2975 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002976 }
2977
2978 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07002979 asTransaction([&](Transaction& t) {
2980 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002981 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2982 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002983 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002984
Robert Carr4cdc58f2017-08-23 14:22:20 -07002985 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002986 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002987 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2988 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002989 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002990
2991 {
2992 SCOPED_TRACE("before any trigger");
2993 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002994 sc->expectBGColor(32, 32);
2995 sc->expectFGColor(96, 96);
2996 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002997 }
2998
2999 // should trigger the first deferred transaction, but not the second one
3000 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3001 {
3002 SCOPED_TRACE("after first trigger");
3003 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003004 sc->expectBGColor(32, 32);
3005 sc->checkPixel(96, 96, 162, 63, 96);
3006 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003007 }
3008
3009 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003010 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003011
3012 // trigger the second deferred transaction
3013 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3014 {
3015 SCOPED_TRACE("after second trigger");
3016 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003017 sc->expectBGColor(32, 32);
3018 sc->expectBGColor(96, 96);
3019 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003020 }
3021}
3022
Robert Carre392b552017-09-19 12:16:05 -07003023TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003024 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003025
3026 sp<SurfaceControl> childNoBuffer =
chaviw0e3479f2018-09-10 16:49:30 -07003027 mClient->createSurface(String8("Bufferless child"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003028 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3029 sp<SurfaceControl> childBuffer =
chaviw0e3479f2018-09-10 16:49:30 -07003030 mClient->createSurface(String8("Buffered child"), 20, 20,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003031 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003032 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003033 SurfaceComposerClient::Transaction{}
3034 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3035 .show(childNoBuffer)
3036 .show(childBuffer)
3037 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003038 {
3039 ScreenCapture::captureScreen(&sc);
3040 sc->expectChildColor(73, 73);
3041 sc->expectFGColor(74, 74);
3042 }
Vishnu Nair60356342018-11-13 13:00:45 -08003043 SurfaceComposerClient::Transaction{}
3044 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3045 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003046 {
3047 ScreenCapture::captureScreen(&sc);
3048 sc->expectChildColor(73, 73);
3049 sc->expectChildColor(74, 74);
3050 }
3051}
3052
Robert Carr2c5f6d22017-09-26 12:30:35 -07003053TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003054 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003055 {
3056 SCOPED_TRACE("before move");
3057 ScreenCapture::captureScreen(&sc);
3058 sc->expectBGColor(0, 12);
3059 sc->expectFGColor(75, 75);
3060 sc->expectBGColor(145, 145);
3061 }
3062
3063 Transaction t1, t2;
3064 t1.setPosition(mFGSurfaceControl, 128, 128);
3065 t2.setPosition(mFGSurfaceControl, 0, 0);
3066 // We expect that the position update from t2 now
3067 // overwrites the position update from t1.
3068 t1.merge(std::move(t2));
3069 t1.apply();
3070
3071 {
3072 ScreenCapture::captureScreen(&sc);
3073 sc->expectFGColor(1, 1);
3074 }
3075}
3076
Robert Carr1f0a16a2016-10-24 16:27:39 -07003077class ChildLayerTest : public LayerUpdateTest {
3078protected:
3079 void SetUp() override {
3080 LayerUpdateTest::SetUp();
chaviw0e3479f2018-09-10 16:49:30 -07003081 mChild = mClient->createSurface(String8("Child surface"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003082 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003083 fillSurfaceRGBA8(mChild, 200, 200, 200);
3084
3085 {
3086 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003087 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003088 mCapture->expectChildColor(64, 64);
3089 }
3090 }
3091 void TearDown() override {
3092 LayerUpdateTest::TearDown();
3093 mChild = 0;
3094 }
3095
3096 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003097 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003098};
3099
3100TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003101 asTransaction([&](Transaction& t) {
3102 t.show(mChild);
3103 t.setPosition(mChild, 10, 10);
3104 t.setPosition(mFGSurfaceControl, 64, 64);
3105 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003106
3107 {
chaviw0e3479f2018-09-10 16:49:30 -07003108 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003109 // Top left of foreground must now be visible
3110 mCapture->expectFGColor(64, 64);
3111 // But 10 pixels in we should see the child surface
3112 mCapture->expectChildColor(74, 74);
3113 // And 10 more pixels we should be back to the foreground surface
3114 mCapture->expectFGColor(84, 84);
3115 }
3116
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003117 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003118
3119 {
chaviw0e3479f2018-09-10 16:49:30 -07003120 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003121 // Top left of foreground should now be at 0, 0
3122 mCapture->expectFGColor(0, 0);
3123 // But 10 pixels in we should see the child surface
3124 mCapture->expectChildColor(10, 10);
3125 // And 10 more pixels we should be back to the foreground surface
3126 mCapture->expectFGColor(20, 20);
3127 }
3128}
3129
Robert Carr41b08b52017-06-01 16:11:34 -07003130TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003131 asTransaction([&](Transaction& t) {
3132 t.show(mChild);
3133 t.setPosition(mChild, 0, 0);
3134 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003135 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003136 });
Robert Carr41b08b52017-06-01 16:11:34 -07003137
3138 {
chaviw0e3479f2018-09-10 16:49:30 -07003139 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07003140 mCapture->expectChildColor(0, 0);
3141 mCapture->expectChildColor(4, 4);
3142 mCapture->expectBGColor(5, 5);
3143 }
3144}
3145
Robert Carr1f0a16a2016-10-24 16:27:39 -07003146TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003147 asTransaction([&](Transaction& t) {
3148 t.show(mChild);
3149 t.setPosition(mFGSurfaceControl, 0, 0);
3150 t.setPosition(mChild, 63, 63);
3151 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003152
3153 {
chaviw0e3479f2018-09-10 16:49:30 -07003154 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003155 mCapture->expectFGColor(0, 0);
3156 // Last pixel in foreground should now be the child.
3157 mCapture->expectChildColor(63, 63);
3158 // But the child should be constrained and the next pixel
3159 // must be the background
3160 mCapture->expectBGColor(64, 64);
3161 }
3162}
3163
3164TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003165 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003166
3167 // Find the boundary between the parent and child
3168 {
chaviw0e3479f2018-09-10 16:49:30 -07003169 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003170 mCapture->expectChildColor(9, 9);
3171 mCapture->expectFGColor(10, 10);
3172 }
3173
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003174 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003175
3176 // The boundary should be twice as far from the origin now.
3177 // The pixels from the last test should all be child now
3178 {
chaviw0e3479f2018-09-10 16:49:30 -07003179 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003180 mCapture->expectChildColor(9, 9);
3181 mCapture->expectChildColor(10, 10);
3182 mCapture->expectChildColor(19, 19);
3183 mCapture->expectFGColor(20, 20);
3184 }
3185}
Robert Carr9524cb32017-02-13 11:32:32 -08003186
Robert Carr6452f122017-03-21 10:41:29 -07003187TEST_F(ChildLayerTest, ChildLayerAlpha) {
3188 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
3189 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
3190 fillSurfaceRGBA8(mChild, 0, 254, 0);
3191 waitForPostedBuffers();
3192
Robert Carr4cdc58f2017-08-23 14:22:20 -07003193 asTransaction([&](Transaction& t) {
3194 t.show(mChild);
3195 t.setPosition(mChild, 0, 0);
3196 t.setPosition(mFGSurfaceControl, 0, 0);
3197 });
Robert Carr6452f122017-03-21 10:41:29 -07003198
3199 {
chaviw0e3479f2018-09-10 16:49:30 -07003200 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003201 // Unblended child color
3202 mCapture->checkPixel(0, 0, 0, 254, 0);
3203 }
3204
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003205 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003206
3207 {
chaviw0e3479f2018-09-10 16:49:30 -07003208 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003209 // Child and BG blended.
3210 mCapture->checkPixel(0, 0, 127, 127, 0);
3211 }
3212
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003213 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003214
3215 {
chaviw0e3479f2018-09-10 16:49:30 -07003216 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003217 // Child and BG blended.
3218 mCapture->checkPixel(0, 0, 95, 64, 95);
3219 }
3220}
3221
Robert Carr9524cb32017-02-13 11:32:32 -08003222TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003223 asTransaction([&](Transaction& t) {
3224 t.show(mChild);
3225 t.setPosition(mChild, 10, 10);
3226 t.setPosition(mFGSurfaceControl, 64, 64);
3227 });
Robert Carr9524cb32017-02-13 11:32:32 -08003228
3229 {
chaviw0e3479f2018-09-10 16:49:30 -07003230 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003231 // Top left of foreground must now be visible
3232 mCapture->expectFGColor(64, 64);
3233 // But 10 pixels in we should see the child surface
3234 mCapture->expectChildColor(74, 74);
3235 // And 10 more pixels we should be back to the foreground surface
3236 mCapture->expectFGColor(84, 84);
3237 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003238
3239 asTransaction([&](Transaction& t) {
3240 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
3241 });
3242
Robert Carr9524cb32017-02-13 11:32:32 -08003243 {
chaviw0e3479f2018-09-10 16:49:30 -07003244 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003245 mCapture->expectFGColor(64, 64);
3246 // In reparenting we should have exposed the entire foreground surface.
3247 mCapture->expectFGColor(74, 74);
3248 // And the child layer should now begin at 10, 10 (since the BG
3249 // layer is at (0, 0)).
3250 mCapture->expectBGColor(9, 9);
3251 mCapture->expectChildColor(10, 10);
3252 }
3253}
3254
Robert Carr2e102c92018-10-23 12:11:15 -07003255TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
3256 sp<SurfaceControl> mGrandChild =
3257 mClient->createSurface(String8("Grand Child"), 10, 10,
3258 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
3259 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
3260
3261 {
3262 SCOPED_TRACE("Grandchild visible");
3263 ScreenCapture::captureScreen(&mCapture);
3264 mCapture->checkPixel(64, 64, 111, 111, 111);
3265 }
3266
3267 mChild->clear();
3268
3269 {
3270 SCOPED_TRACE("After destroying child");
3271 ScreenCapture::captureScreen(&mCapture);
3272 mCapture->expectFGColor(64, 64);
3273 }
3274
3275 asTransaction([&](Transaction& t) {
3276 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
3277 });
3278
3279 {
3280 SCOPED_TRACE("After reparenting grandchild");
3281 ScreenCapture::captureScreen(&mCapture);
3282 mCapture->checkPixel(64, 64, 111, 111, 111);
3283 }
3284}
3285
chaviw161410b02017-07-27 10:46:08 -07003286TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003287 asTransaction([&](Transaction& t) {
3288 t.show(mChild);
3289 t.setPosition(mChild, 10, 10);
3290 t.setPosition(mFGSurfaceControl, 64, 64);
3291 });
Robert Carr9524cb32017-02-13 11:32:32 -08003292
3293 {
chaviw0e3479f2018-09-10 16:49:30 -07003294 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003295 // Top left of foreground must now be visible
3296 mCapture->expectFGColor(64, 64);
3297 // But 10 pixels in we should see the child surface
3298 mCapture->expectChildColor(74, 74);
3299 // And 10 more pixels we should be back to the foreground surface
3300 mCapture->expectFGColor(84, 84);
3301 }
3302
chaviw0e3479f2018-09-10 16:49:30 -07003303
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003304 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08003305
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003306 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08003307
chaviw161410b02017-07-27 10:46:08 -07003308 // Since the child has the same client as the parent, it will not get
3309 // detached and will be hidden.
3310 {
chaviw0e3479f2018-09-10 16:49:30 -07003311 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003312 mCapture->expectFGColor(64, 64);
3313 mCapture->expectFGColor(74, 74);
3314 mCapture->expectFGColor(84, 84);
3315 }
3316}
3317
3318TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
3319 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003320 sp<SurfaceControl> mChildNewClient =
3321 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
3322 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07003323
Peiyong Lin566a3b42018-01-09 18:22:43 -08003324 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07003325 ASSERT_TRUE(mChildNewClient->isValid());
3326
3327 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
3328
Robert Carr4cdc58f2017-08-23 14:22:20 -07003329 asTransaction([&](Transaction& t) {
3330 t.hide(mChild);
3331 t.show(mChildNewClient);
3332 t.setPosition(mChildNewClient, 10, 10);
3333 t.setPosition(mFGSurfaceControl, 64, 64);
3334 });
chaviw161410b02017-07-27 10:46:08 -07003335
3336 {
chaviw0e3479f2018-09-10 16:49:30 -07003337 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07003338 // Top left of foreground must now be visible
3339 mCapture->expectFGColor(64, 64);
3340 // But 10 pixels in we should see the child surface
3341 mCapture->expectChildColor(74, 74);
3342 // And 10 more pixels we should be back to the foreground surface
3343 mCapture->expectFGColor(84, 84);
3344 }
3345
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003346 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07003347
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003348 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07003349
Robert Carr9524cb32017-02-13 11:32:32 -08003350 // Nothing should have changed.
3351 {
chaviw0e3479f2018-09-10 16:49:30 -07003352 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08003353 mCapture->expectFGColor(64, 64);
3354 mCapture->expectChildColor(74, 74);
3355 mCapture->expectFGColor(84, 84);
3356 }
3357}
3358
Robert Carr9b429f42017-04-17 14:56:57 -07003359TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003360 asTransaction([&](Transaction& t) {
3361 t.show(mChild);
3362 t.setPosition(mChild, 0, 0);
3363 t.setPosition(mFGSurfaceControl, 0, 0);
3364 });
Robert Carr9b429f42017-04-17 14:56:57 -07003365
3366 {
chaviw0e3479f2018-09-10 16:49:30 -07003367 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003368 // We've positioned the child in the top left.
3369 mCapture->expectChildColor(0, 0);
3370 // But it's only 10x10.
3371 mCapture->expectFGColor(10, 10);
3372 }
3373
Robert Carr4cdc58f2017-08-23 14:22:20 -07003374 asTransaction([&](Transaction& t) {
3375 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
3376 // We cause scaling by 2.
3377 t.setSize(mFGSurfaceControl, 128, 128);
3378 });
Robert Carr9b429f42017-04-17 14:56:57 -07003379
3380 {
chaviw0e3479f2018-09-10 16:49:30 -07003381 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07003382 // We've positioned the child in the top left.
3383 mCapture->expectChildColor(0, 0);
3384 mCapture->expectChildColor(10, 10);
3385 mCapture->expectChildColor(19, 19);
3386 // And now it should be scaled all the way to 20x20
3387 mCapture->expectFGColor(20, 20);
3388 }
3389}
3390
Robert Carr1725eee2017-04-26 18:32:15 -07003391// Regression test for b/37673612
3392TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003393 asTransaction([&](Transaction& t) {
3394 t.show(mChild);
3395 t.setPosition(mChild, 0, 0);
3396 t.setPosition(mFGSurfaceControl, 0, 0);
3397 });
Robert Carr1725eee2017-04-26 18:32:15 -07003398
3399 {
chaviw0e3479f2018-09-10 16:49:30 -07003400 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003401 // We've positioned the child in the top left.
3402 mCapture->expectChildColor(0, 0);
3403 // But it's only 10x10.
3404 mCapture->expectFGColor(10, 10);
3405 }
Robert Carr1725eee2017-04-26 18:32:15 -07003406 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
3407 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003408 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07003409 sp<Surface> s = mFGSurfaceControl->getSurface();
3410 auto anw = static_cast<ANativeWindow*>(s.get());
3411 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
3412 native_window_set_buffers_dimensions(anw, 64, 128);
3413 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3414 waitForPostedBuffers();
3415
3416 {
3417 // The child should still be in the same place and not have any strange scaling as in
3418 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07003419 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07003420 mCapture->expectChildColor(0, 0);
3421 mCapture->expectFGColor(10, 10);
3422 }
3423}
3424
Dan Stoza412903f2017-04-27 13:42:17 -07003425TEST_F(ChildLayerTest, Bug36858924) {
3426 // Destroy the child layer
3427 mChild.clear();
3428
3429 // Now recreate it as hidden
chaviw0e3479f2018-09-10 16:49:30 -07003430 mChild = mClient->createSurface(String8("Child surface"), 10, 10,
Dan Stoza412903f2017-04-27 13:42:17 -07003431 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
3432 mFGSurfaceControl.get());
3433
3434 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07003435 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003436 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
3437 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003438 t.show(mChild);
3439 });
Dan Stoza412903f2017-04-27 13:42:17 -07003440
3441 // Render the foreground surface a few times
3442 //
3443 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
3444 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
3445 // never acquire/release the first buffer
3446 ALOGI("Filling 1");
3447 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3448 ALOGI("Filling 2");
3449 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
3450 ALOGI("Filling 3");
3451 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
3452 ALOGI("Filling 4");
3453 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
3454}
3455
chaviwf1961f72017-09-18 16:41:07 -07003456TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003457 asTransaction([&](Transaction& t) {
3458 t.show(mChild);
3459 t.setPosition(mChild, 10, 10);
3460 t.setPosition(mFGSurfaceControl, 64, 64);
3461 });
chaviw06178942017-07-27 10:25:59 -07003462
3463 {
chaviw0e3479f2018-09-10 16:49:30 -07003464 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003465 // Top left of foreground must now be visible
3466 mCapture->expectFGColor(64, 64);
3467 // But 10 pixels in we should see the child surface
3468 mCapture->expectChildColor(74, 74);
3469 // And 10 more pixels we should be back to the foreground surface
3470 mCapture->expectFGColor(84, 84);
3471 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07003472
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003473 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07003474
chaviw06178942017-07-27 10:25:59 -07003475 {
chaviw0e3479f2018-09-10 16:49:30 -07003476 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07003477 mCapture->expectFGColor(64, 64);
3478 // In reparenting we should have exposed the entire foreground surface.
3479 mCapture->expectFGColor(74, 74);
3480 // And the child layer should now begin at 10, 10 (since the BG
3481 // layer is at (0, 0)).
3482 mCapture->expectBGColor(9, 9);
3483 mCapture->expectChildColor(10, 10);
3484 }
3485}
3486
chaviwf1961f72017-09-18 16:41:07 -07003487TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003488 asTransaction([&](Transaction& t) {
3489 t.show(mChild);
3490 t.setPosition(mChild, 10, 10);
3491 t.setPosition(mFGSurfaceControl, 64, 64);
3492 });
chaviwf1961f72017-09-18 16:41:07 -07003493
3494 {
chaviw0e3479f2018-09-10 16:49:30 -07003495 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003496 // Top left of foreground must now be visible
3497 mCapture->expectFGColor(64, 64);
3498 // But 10 pixels in we should see the child surface
3499 mCapture->expectChildColor(74, 74);
3500 // And 10 more pixels we should be back to the foreground surface
3501 mCapture->expectFGColor(84, 84);
3502 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003503 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07003504 {
chaviw0e3479f2018-09-10 16:49:30 -07003505 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003506 // Nothing should have changed.
3507 mCapture->expectFGColor(64, 64);
3508 mCapture->expectChildColor(74, 74);
3509 mCapture->expectFGColor(84, 84);
3510 }
3511}
3512
3513TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07003514 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003515 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07003516 ASSERT_TRUE(newSurface->isValid());
3517
3518 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003519 asTransaction([&](Transaction& t) {
3520 t.hide(mChild);
3521 t.show(newSurface);
3522 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003523 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003524 t.setPosition(mFGSurfaceControl, 64, 64);
3525 });
chaviwf1961f72017-09-18 16:41:07 -07003526
3527 {
chaviw0e3479f2018-09-10 16:49:30 -07003528 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003529 // Top left of foreground must now be visible
3530 mCapture->expectFGColor(64, 64);
3531 // At 10, 10 we should see the new surface
3532 mCapture->checkPixel(10, 10, 63, 195, 63);
3533 }
3534
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003535 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07003536
3537 {
chaviw0e3479f2018-09-10 16:49:30 -07003538 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07003539 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
3540 // mFGSurface, putting it at 74, 74.
3541 mCapture->expectFGColor(64, 64);
3542 mCapture->checkPixel(74, 74, 63, 195, 63);
3543 mCapture->expectFGColor(84, 84);
3544 }
3545}
3546
chaviwc9674332017-08-28 12:32:18 -07003547TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003548 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07003549 mClient->createSurface(String8("Grandchild surface"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003550 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07003551 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3552
3553 {
chaviw0e3479f2018-09-10 16:49:30 -07003554 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07003555 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
3556 // which begins at 64, 64
3557 mCapture->checkPixel(64, 64, 50, 50, 50);
3558 }
3559}
3560
Robert Carr503c7042017-09-27 15:06:08 -07003561TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07003562 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07003563 fillSurfaceRGBA8(relative, 255, 255, 255);
3564
3565 Transaction t;
3566 t.setLayer(relative, INT32_MAX)
3567 .setRelativeLayer(mChild, relative->getHandle(), 1)
3568 .setPosition(mFGSurfaceControl, 0, 0)
3569 .apply(true);
3570
3571 // We expect that the child should have been elevated above our
3572 // INT_MAX layer even though it's not a child of it.
3573 {
chaviw0e3479f2018-09-10 16:49:30 -07003574 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07003575 mCapture->expectChildColor(0, 0);
3576 mCapture->expectChildColor(9, 9);
3577 mCapture->checkPixel(10, 10, 255, 255, 255);
3578 }
3579}
Vishnu Nair60356342018-11-13 13:00:45 -08003580class BoundlessLayerTest : public LayerUpdateTest {
3581protected:
3582 std::unique_ptr<ScreenCapture> mCapture;
3583};
3584
3585// Verify setting a size on a buffer layer has no effect.
3586TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
3587 sp<SurfaceControl> bufferLayer =
3588 mClient->createSurface(String8("BufferLayer"), 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
3589 mFGSurfaceControl.get());
3590 ASSERT_TRUE(bufferLayer != nullptr);
3591 ASSERT_TRUE(bufferLayer->isValid());
3592 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
3593 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
3594 {
3595 mCapture = screenshot();
3596 // Top left of background must now be visible
3597 mCapture->expectBGColor(0, 0);
3598 // Foreground Surface bounds must be color layer
3599 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
3600 // Buffer layer should not extend past buffer bounds
3601 mCapture->expectFGColor(95, 95);
3602 }
3603}
3604
3605// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
3606// which will crop the color layer.
3607TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
3608 sp<SurfaceControl> colorLayer =
3609 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3610 ISurfaceComposerClient::eFXSurfaceColor,
3611 mFGSurfaceControl.get());
3612 ASSERT_TRUE(colorLayer != nullptr);
3613 ASSERT_TRUE(colorLayer->isValid());
3614 asTransaction([&](Transaction& t) {
3615 t.setColor(colorLayer, half3{0, 0, 0});
3616 t.show(colorLayer);
3617 });
3618 {
3619 mCapture = screenshot();
3620 // Top left of background must now be visible
3621 mCapture->expectBGColor(0, 0);
3622 // Foreground Surface bounds must be color layer
3623 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3624 // Color layer should not extend past foreground bounds
3625 mCapture->expectBGColor(129, 129);
3626 }
3627}
3628
3629// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
3630// a crop which will be used to crop the color layer.
3631TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
3632 sp<SurfaceControl> cropLayer =
3633 mClient->createSurface(String8("CropLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3634 0 /* flags */, mFGSurfaceControl.get());
3635 ASSERT_TRUE(cropLayer != nullptr);
3636 ASSERT_TRUE(cropLayer->isValid());
3637 sp<SurfaceControl> colorLayer =
3638 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3639 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
3640 ASSERT_TRUE(colorLayer != nullptr);
3641 ASSERT_TRUE(colorLayer->isValid());
3642 asTransaction([&](Transaction& t) {
3643 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
3644 t.setColor(colorLayer, half3{0, 0, 0});
3645 t.show(cropLayer);
3646 t.show(colorLayer);
3647 });
3648 {
3649 mCapture = screenshot();
3650 // Top left of background must now be visible
3651 mCapture->expectBGColor(0, 0);
3652 // Top left of foreground must now be visible
3653 mCapture->expectFGColor(64, 64);
3654 // 5 pixels from the foreground we should see the child surface
3655 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
3656 // 10 pixels from the foreground we should be back to the foreground surface
3657 mCapture->expectFGColor(74, 74);
3658 }
3659}
3660
3661// Verify for boundless layer with no children, their transforms have no effect.
3662TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
3663 sp<SurfaceControl> colorLayer =
3664 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3665 ISurfaceComposerClient::eFXSurfaceColor,
3666 mFGSurfaceControl.get());
3667 ASSERT_TRUE(colorLayer != nullptr);
3668 ASSERT_TRUE(colorLayer->isValid());
3669 asTransaction([&](Transaction& t) {
3670 t.setPosition(colorLayer, 320, 320);
3671 t.setMatrix(colorLayer, 2, 0, 0, 2);
3672 t.setColor(colorLayer, half3{0, 0, 0});
3673 t.show(colorLayer);
3674 });
3675 {
3676 mCapture = screenshot();
3677 // Top left of background must now be visible
3678 mCapture->expectBGColor(0, 0);
3679 // Foreground Surface bounds must be color layer
3680 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3681 // Color layer should not extend past foreground bounds
3682 mCapture->expectBGColor(129, 129);
3683 }
3684}
3685
3686// Verify for boundless layer with children, their transforms have an effect.
3687TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
3688 sp<SurfaceControl> boundlessLayerRightShift =
3689 mClient->createSurface(String8("BoundlessLayerRightShift"), 0, 0,
3690 PIXEL_FORMAT_RGBA_8888, 0 /* flags */, mFGSurfaceControl.get());
3691 ASSERT_TRUE(boundlessLayerRightShift != nullptr);
3692 ASSERT_TRUE(boundlessLayerRightShift->isValid());
3693 sp<SurfaceControl> boundlessLayerDownShift =
3694 mClient->createSurface(String8("BoundlessLayerLeftShift"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3695 0 /* flags */, boundlessLayerRightShift.get());
3696 ASSERT_TRUE(boundlessLayerDownShift != nullptr);
3697 ASSERT_TRUE(boundlessLayerDownShift->isValid());
3698 sp<SurfaceControl> colorLayer =
3699 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3700 ISurfaceComposerClient::eFXSurfaceColor,
3701 boundlessLayerDownShift.get());
3702 ASSERT_TRUE(colorLayer != nullptr);
3703 ASSERT_TRUE(colorLayer->isValid());
3704 asTransaction([&](Transaction& t) {
3705 t.setPosition(boundlessLayerRightShift, 32, 0);
3706 t.show(boundlessLayerRightShift);
3707 t.setPosition(boundlessLayerDownShift, 0, 32);
3708 t.show(boundlessLayerDownShift);
3709 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3710 t.setColor(colorLayer, half3{0, 0, 0});
3711 t.show(colorLayer);
3712 });
3713 {
3714 mCapture = screenshot();
3715 // Top left of background must now be visible
3716 mCapture->expectBGColor(0, 0);
3717 // Top left of foreground must now be visible
3718 mCapture->expectFGColor(64, 64);
3719 // Foreground Surface bounds must be color layer
3720 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
3721 // Color layer should not extend past foreground bounds
3722 mCapture->expectBGColor(129, 129);
3723 }
3724}
3725
3726// Verify child layers do not get clipped if they temporarily move into the negative
3727// coordinate space as the result of an intermediate transformation.
3728TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
3729 sp<SurfaceControl> boundlessLayer =
3730 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3731 0 /* flags */, mFGSurfaceControl.get());
3732 ASSERT_TRUE(boundlessLayer != nullptr);
3733 ASSERT_TRUE(boundlessLayer->isValid());
3734 sp<SurfaceControl> colorLayer =
3735 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3736 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
3737 ASSERT_TRUE(colorLayer != nullptr);
3738 ASSERT_TRUE(colorLayer->isValid());
3739 asTransaction([&](Transaction& t) {
3740 // shift child layer off bounds. If this layer was not boundless, we will
3741 // expect the child layer to be cropped.
3742 t.setPosition(boundlessLayer, 32, 32);
3743 t.show(boundlessLayer);
3744 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3745 // undo shift by parent
3746 t.setPosition(colorLayer, -32, -32);
3747 t.setColor(colorLayer, half3{0, 0, 0});
3748 t.show(colorLayer);
3749 });
3750 {
3751 mCapture = screenshot();
3752 // Top left of background must now be visible
3753 mCapture->expectBGColor(0, 0);
3754 // Foreground Surface bounds must be color layer
3755 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
3756 // Color layer should not extend past foreground bounds
3757 mCapture->expectBGColor(129, 129);
3758 }
3759}
3760
3761// Verify for boundless root layers with children, their transforms have an effect.
3762TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
3763 sp<SurfaceControl> rootBoundlessLayer =
3764 mClient->createSurface(String8("RootBoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3765 0 /* flags */);
3766 ASSERT_TRUE(rootBoundlessLayer != nullptr);
3767 ASSERT_TRUE(rootBoundlessLayer->isValid());
3768 sp<SurfaceControl> colorLayer =
3769 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
3770 ISurfaceComposerClient::eFXSurfaceColor,
3771 rootBoundlessLayer.get());
3772 ASSERT_TRUE(colorLayer != nullptr);
3773 ASSERT_TRUE(colorLayer->isValid());
3774 asTransaction([&](Transaction& t) {
3775 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
3776 t.setPosition(rootBoundlessLayer, 32, 32);
3777 t.show(rootBoundlessLayer);
3778 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
3779 t.setColor(colorLayer, half3{0, 0, 0});
3780 t.show(colorLayer);
3781 t.hide(mFGSurfaceControl);
3782 });
3783 {
3784 mCapture = screenshot();
3785 // Top left of background must now be visible
3786 mCapture->expectBGColor(0, 0);
3787 // Top left of foreground must now be visible
3788 mCapture->expectBGColor(31, 31);
3789 // Foreground Surface bounds must be color layer
3790 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
3791 // Color layer should not extend past foreground bounds
3792 mCapture->expectBGColor(97, 97);
3793 }
3794}
Robert Carr503c7042017-09-27 15:06:08 -07003795
chaviwa76b2712017-09-20 12:02:26 -07003796class ScreenCaptureTest : public LayerUpdateTest {
3797protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003798 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07003799};
3800
3801TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
3802 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003803 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003804 mCapture->expectBGColor(0, 0);
3805 // Doesn't capture FG layer which is at 64, 64
3806 mCapture->expectBGColor(64, 64);
3807}
3808
3809TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
3810 auto fgHandle = mFGSurfaceControl->getHandle();
3811
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003812 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003813 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003814 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003815 fillSurfaceRGBA8(child, 200, 200, 200);
3816
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003817 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003818
3819 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003820 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003821 mCapture->expectFGColor(10, 10);
3822 mCapture->expectChildColor(0, 0);
3823}
3824
Robert Carr578038f2018-03-09 12:25:24 -08003825TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
3826 auto fgHandle = mFGSurfaceControl->getHandle();
3827
3828 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003829 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Robert Carr578038f2018-03-09 12:25:24 -08003830 0, mFGSurfaceControl.get());
3831 fillSurfaceRGBA8(child, 200, 200, 200);
3832
3833 SurfaceComposerClient::Transaction().show(child).apply(true);
3834
3835 // Captures mFGSurfaceControl's child
3836 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3837 mCapture->checkPixel(10, 10, 0, 0, 0);
3838 mCapture->expectChildColor(0, 0);
3839}
3840
chaviw50da5042018-04-09 13:49:37 -07003841TEST_F(ScreenCaptureTest, CaptureTransparent) {
3842 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003843 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw50da5042018-04-09 13:49:37 -07003844 0, mFGSurfaceControl.get());
3845
3846 fillSurfaceRGBA8(child, 200, 200, 200);
3847
3848 SurfaceComposerClient::Transaction().show(child).apply(true);
3849
3850 auto childHandle = child->getHandle();
3851
3852 // Captures child
3853 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
3854 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
3855 // Area outside of child's bounds is transparent.
3856 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
3857}
3858
chaviw4b129c22018-04-09 16:19:43 -07003859TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
3860 auto fgHandle = mFGSurfaceControl->getHandle();
3861
3862 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003863 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw4b129c22018-04-09 16:19:43 -07003864 0, mFGSurfaceControl.get());
chaviw0e3479f2018-09-10 16:49:30 -07003865 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07003866 fillSurfaceRGBA8(child, 200, 200, 200);
3867 fillSurfaceRGBA8(relative, 100, 100, 100);
3868
3869 SurfaceComposerClient::Transaction()
3870 .show(child)
3871 // Set relative layer above fg layer so should be shown above when computing all layers.
3872 .setRelativeLayer(relative, fgHandle, 1)
3873 .show(relative)
3874 .apply(true);
3875
3876 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
3877 ScreenCapture::captureLayers(&mCapture, fgHandle);
3878 mCapture->expectFGColor(10, 10);
3879 mCapture->expectChildColor(0, 0);
3880}
3881
3882TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
3883 auto fgHandle = mFGSurfaceControl->getHandle();
3884
3885 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003886 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw4b129c22018-04-09 16:19:43 -07003887 0, mFGSurfaceControl.get());
3888 sp<SurfaceControl> relative =
chaviw0e3479f2018-09-10 16:49:30 -07003889 mClient->createSurface(String8("Relative surface"), 10, 10,
chaviw4b129c22018-04-09 16:19:43 -07003890 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3891 fillSurfaceRGBA8(child, 200, 200, 200);
3892 fillSurfaceRGBA8(relative, 100, 100, 100);
3893
3894 SurfaceComposerClient::Transaction()
3895 .show(child)
3896 // Set relative layer below fg layer but relative to child layer so it should be shown
3897 // above child layer.
3898 .setLayer(relative, -1)
3899 .setRelativeLayer(relative, child->getHandle(), 1)
3900 .show(relative)
3901 .apply(true);
3902
3903 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
3904 // relative value should be taken into account, placing it above child layer.
3905 ScreenCapture::captureLayers(&mCapture, fgHandle);
3906 mCapture->expectFGColor(10, 10);
3907 // Relative layer is showing on top of child layer
3908 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
3909}
Robert Carr578038f2018-03-09 12:25:24 -08003910
3911// In the following tests we verify successful skipping of a parent layer,
3912// so we use the same verification logic and only change how we mutate
3913// the parent layer to verify that various properties are ignored.
3914class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
3915public:
3916 void SetUp() override {
3917 LayerUpdateTest::SetUp();
3918
3919 mChild =
chaviw0e3479f2018-09-10 16:49:30 -07003920 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Robert Carr578038f2018-03-09 12:25:24 -08003921 0, mFGSurfaceControl.get());
3922 fillSurfaceRGBA8(mChild, 200, 200, 200);
3923
3924 SurfaceComposerClient::Transaction().show(mChild).apply(true);
3925 }
3926
3927 void verify() {
3928 auto fgHandle = mFGSurfaceControl->getHandle();
3929 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3930 mCapture->checkPixel(10, 10, 0, 0, 0);
3931 mCapture->expectChildColor(0, 0);
3932 }
3933
3934 std::unique_ptr<ScreenCapture> mCapture;
3935 sp<SurfaceControl> mChild;
3936};
3937
3938TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
3939
3940 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
3941
3942 // Even though the parent is hidden we should still capture the child.
3943 verify();
3944}
3945
3946TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003947 SurfaceComposerClient::Transaction()
3948 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
3949 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08003950
3951 // Even though the parent is cropped out we should still capture the child.
3952 verify();
3953}
3954
3955TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
3956
3957 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
3958
3959 // We should not inherit the parent scaling.
3960 verify();
3961}
3962
Robert Carr15eae092018-03-23 13:43:53 -07003963TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
3964 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
3965
3966 // Even though the parent is hidden we should still capture the child.
3967 verify();
3968
3969 // Verify everything was properly hidden when rendering the full-screen.
3970 screenshot()->expectBGColor(0,0);
3971}
3972
3973
chaviwa76b2712017-09-20 12:02:26 -07003974TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
3975 auto fgHandle = mFGSurfaceControl->getHandle();
3976
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003977 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003978 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003979 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003980 fillSurfaceRGBA8(child, 200, 200, 200);
3981
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003982 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07003983 mClient->createSurface(String8("Grandchild surface"), 5, 5,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003984 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07003985
3986 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3987 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003988 .show(child)
3989 .setPosition(grandchild, 5, 5)
3990 .show(grandchild)
3991 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003992
3993 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003994 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003995 mCapture->expectFGColor(10, 10);
3996 mCapture->expectChildColor(0, 0);
3997 mCapture->checkPixel(5, 5, 50, 50, 50);
3998}
3999
4000TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004001 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07004002 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004003 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004004 fillSurfaceRGBA8(child, 200, 200, 200);
4005 auto childHandle = child->getHandle();
4006
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004007 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004008
4009 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004010 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07004011 mCapture->expectChildColor(0, 0);
4012 mCapture->expectChildColor(9, 9);
4013}
4014
4015TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004016 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07004017 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004018 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004019 fillSurfaceRGBA8(child, 200, 200, 200);
4020 auto childHandle = child->getHandle();
4021
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004022 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07004023 mClient->createSurface(String8("Grandchild surface"), 5, 5,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004024 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004025 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4026
4027 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004028 .show(child)
4029 .setPosition(grandchild, 5, 5)
4030 .show(grandchild)
4031 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004032
4033 auto grandchildHandle = grandchild->getHandle();
4034
4035 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004036 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07004037 mCapture->checkPixel(0, 0, 50, 50, 50);
4038 mCapture->checkPixel(4, 4, 50, 50, 50);
4039}
4040
chaviw7206d492017-11-10 16:16:12 -08004041TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07004042 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004043 sp<SurfaceControl> blueLayer =
chaviw0e3479f2018-09-10 16:49:30 -07004044 mClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004045 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004046
Marissa Wall61c58622018-07-18 10:12:20 -07004047 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4048 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004049
4050 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004051 .setLayer(redLayer, INT32_MAX - 1)
4052 .show(redLayer)
4053 .show(blueLayer)
4054 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004055
4056 auto redLayerHandle = redLayer->getHandle();
4057
4058 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004059 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4060 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4061 // red area below the blue area
4062 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4063 // red area to the right of the blue area
4064 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004065
4066 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004067 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08004068 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
4069 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004070 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08004071 mCapture->checkPixel(30, 30, 0, 0, 0);
4072}
4073
4074TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07004075 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004076 sp<SurfaceControl> blueLayer =
chaviw0e3479f2018-09-10 16:49:30 -07004077 mClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004078 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004079
Marissa Wall61c58622018-07-18 10:12:20 -07004080 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4081 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004082
4083 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004084 .setLayer(redLayer, INT32_MAX - 1)
4085 .show(redLayer)
4086 .show(blueLayer)
4087 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004088
4089 auto redLayerHandle = redLayer->getHandle();
4090
4091 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004092 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4093 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4094 // red area below the blue area
4095 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4096 // red area to the right of the blue area
4097 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004098
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004099 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08004100 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004101 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
4102 // red area below the blue area
4103 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
4104 // red area to the right of the blue area
4105 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004106 mCapture->checkPixel(30, 30, 0, 0, 0);
4107}
4108
4109TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004110 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
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));
chaviw7206d492017-11-10 16:16:12 -08004113
4114 auto redLayerHandle = redLayer->getHandle();
chaviw0e3479f2018-09-10 16:49:30 -07004115 mClient->destroySurface(redLayerHandle);
chaviw7206d492017-11-10 16:16:12 -08004116 SurfaceComposerClient::Transaction().apply(true);
4117
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004118 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08004119
4120 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004121 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
4122 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08004123}
4124
chaviw8e3fe5d2018-02-22 10:55:42 -08004125
4126class DereferenceSurfaceControlTest : public LayerTransactionTest {
4127protected:
4128 void SetUp() override {
4129 LayerTransactionTest::SetUp();
4130 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004131 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004132 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004133 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004134 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
4135 {
4136 SCOPED_TRACE("before anything");
4137 auto shot = screenshot();
4138 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4139 }
4140 }
4141 void TearDown() override {
4142 LayerTransactionTest::TearDown();
4143 bgLayer = 0;
4144 fgLayer = 0;
4145 }
4146
4147 sp<SurfaceControl> bgLayer;
4148 sp<SurfaceControl> fgLayer;
4149};
4150
4151TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
4152 fgLayer = nullptr;
4153 {
4154 SCOPED_TRACE("after setting null");
4155 auto shot = screenshot();
4156 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
4157 }
4158}
4159
4160TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
4161 auto transaction = Transaction().show(fgLayer);
4162 fgLayer = nullptr;
4163 {
4164 SCOPED_TRACE("after setting null");
4165 auto shot = screenshot();
4166 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4167 }
4168}
4169
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004170} // namespace android