blob: 3af98e5c9b80de1c6fb9bcc1274c9acd28f51af5 [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>
18#include <functional>
19#include <limits>
20#include <ostream>
21
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070022#include <gtest/gtest.h>
23
Michael Lentine5a16a622015-05-21 13:48:24 -070024#include <android/native_window.h>
25
Mathias Agopian90ac7992012-02-25 18:48:35 -080026#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070027#include <gui/LayerState.h>
28
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/Surface.h>
30#include <gui/SurfaceComposerClient.h>
31#include <private/gui/ComposerService.h>
32
Mathias Agopianc666cae2012-07-25 18:56:13 -070033#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070034#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070035#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070036
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070037#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070038#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070039
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040namespace android {
41
Chia-I Wu718daf82017-10-20 11:57:17 -070042namespace {
43
44struct Color {
45 uint8_t r;
46 uint8_t g;
47 uint8_t b;
48 uint8_t a;
49
50 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070051 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070052 static const Color BLUE;
Chia-I Wu93853fe2017-11-02 08:30:27 -070053 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070054 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070055 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070056};
57
58const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070059const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070060const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070061const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070062const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070063const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070064
Marissa Wall61c58622018-07-18 10:12:20 -070065using android::hardware::graphics::common::V1_1::BufferUsage;
66
Chia-I Wu718daf82017-10-20 11:57:17 -070067std::ostream& operator<<(std::ostream& os, const Color& color) {
68 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
69 return os;
70}
71
72// Fill a region with the specified color.
Marissa Wall61c58622018-07-18 10:12:20 -070073void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
74 const Color& color) {
75 Rect r(0, 0, buffer.width, buffer.height);
76 if (!r.intersect(rect, &r)) {
77 return;
Chia-I Wu718daf82017-10-20 11:57:17 -070078 }
79
Marissa Wall61c58622018-07-18 10:12:20 -070080 int32_t width = r.right - r.left;
81 int32_t height = r.bottom - r.top;
82
83 for (int32_t row = 0; row < height; row++) {
84 uint8_t* dst =
85 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
86 for (int32_t column = 0; column < width; column++) {
Chia-I Wu718daf82017-10-20 11:57:17 -070087 dst[0] = color.r;
88 dst[1] = color.g;
89 dst[2] = color.b;
90 dst[3] = color.a;
91 dst += 4;
92 }
93 }
94}
95
Marissa Wall61c58622018-07-18 10:12:20 -070096// Fill a region with the specified color.
97void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
98 Rect r(0, 0, buffer->width, buffer->height);
99 if (!r.intersect(rect, &r)) {
100 return;
101 }
102
103 int32_t width = r.right - r.left;
104 int32_t height = r.bottom - r.top;
105
106 uint8_t* pixels;
107 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
108 reinterpret_cast<void**>(&pixels));
109
110 for (int32_t row = 0; row < height; row++) {
111 uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
112 for (int32_t column = 0; column < width; column++) {
113 dst[0] = color.r;
114 dst[1] = color.g;
115 dst[2] = color.b;
116 dst[3] = color.a;
117 dst += 4;
118 }
119 }
120 buffer->unlock();
121}
122
Chia-I Wu718daf82017-10-20 11:57:17 -0700123// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000124void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700125 const Color& color, uint8_t tolerance) {
126 int32_t x = rect.left;
127 int32_t y = rect.top;
128 int32_t width = rect.right - rect.left;
129 int32_t height = rect.bottom - rect.top;
130
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000131 int32_t bufferWidth = int32_t(outBuffer->getWidth());
132 int32_t bufferHeight = int32_t(outBuffer->getHeight());
133 if (x + width > bufferWidth) {
134 x = std::min(x, bufferWidth);
135 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700136 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000137 if (y + height > bufferHeight) {
138 y = std::min(y, bufferHeight);
139 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700140 }
141
142 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
143 uint8_t tmp = a >= b ? a - b : b - a;
144 return tmp <= tolerance;
145 };
146 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000147 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700148 for (int32_t i = 0; i < width; i++) {
149 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
150 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
151 << "pixel @ (" << x + i << ", " << y + j << "): "
152 << "expected (" << color << "), "
153 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
154 src += 4;
155 }
156 }
157}
158
159} // anonymous namespace
160
Robert Carr4cdc58f2017-08-23 14:22:20 -0700161using Transaction = SurfaceComposerClient::Transaction;
162
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700163// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700164static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
165 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800166 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700167 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800168 ASSERT_TRUE(s != nullptr);
169 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800170 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700171 for (int y = 0; y < outBuffer.height; y++) {
172 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700173 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700174 pixel[0] = r;
175 pixel[1] = g;
176 pixel[2] = b;
177 pixel[3] = 255;
178 }
179 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700180 if (unlock) {
181 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
182 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700183}
184
185// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
186// individual pixel values for testing purposes.
187class ScreenCapture : public RefBase {
188public:
chaviw0e3479f2018-09-10 16:49:30 -0700189 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700190 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700191 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700192 SurfaceComposerClient::Transaction().apply(true);
193
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000194 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700195 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -0700196 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, false));
197 *sc = std::make_unique<ScreenCapture>(outBuffer);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000198 }
199
200 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
201 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
202 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
203 SurfaceComposerClient::Transaction().apply(true);
204
205 sp<GraphicBuffer> outBuffer;
206 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
207 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700208 }
209
Robert Carr578038f2018-03-09 12:25:24 -0800210 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
211 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
212 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
213 SurfaceComposerClient::Transaction().apply(true);
214
215 sp<GraphicBuffer> outBuffer;
216 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
217 *sc = std::make_unique<ScreenCapture>(outBuffer);
218 }
219
Chia-I Wu718daf82017-10-20 11:57:17 -0700220 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000221 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
222 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700223 }
224
225 void expectBorder(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());
Chia-I Wu718daf82017-10-20 11:57:17 -0700227 const bool leftBorder = rect.left > 0;
228 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000229 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
230 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700231
232 if (topBorder) {
233 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
234 if (leftBorder) {
235 top.left -= 1;
236 }
237 if (rightBorder) {
238 top.right += 1;
239 }
240 expectColor(top, color, tolerance);
241 }
242 if (leftBorder) {
243 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
244 expectColor(left, color, tolerance);
245 }
246 if (rightBorder) {
247 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
248 expectColor(right, color, tolerance);
249 }
250 if (bottomBorder) {
251 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
252 if (leftBorder) {
253 bottom.left -= 1;
254 }
255 if (rightBorder) {
256 bottom.right += 1;
257 }
258 expectColor(bottom, color, tolerance);
259 }
260 }
261
Chia-I Wu93853fe2017-11-02 08:30:27 -0700262 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
263 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
264 uint8_t tolerance = 0) {
265 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
266
267 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
268 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
269 // avoid checking borders due to unspecified filtering behavior
270 const int32_t offsetX = filtered ? 2 : 0;
271 const int32_t offsetY = filtered ? 2 : 0;
272 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
273 tolerance);
274 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
275 tolerance);
276 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
277 tolerance);
278 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
279 bottomRight, tolerance);
280 }
281
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700282 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000283 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
284 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700285 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
286 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700287 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
288 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700289 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700290 }
291 }
292
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700293 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700294
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700295 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700296
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700297 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700298
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000299 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
300 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700301 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700302
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000303 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700304
305private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000306 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800307 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700308};
309
Chia-I Wu718daf82017-10-20 11:57:17 -0700310class LayerTransactionTest : public ::testing::Test {
311protected:
312 void SetUp() override {
313 mClient = new SurfaceComposerClient;
314 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
315
316 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
317 }
318
chaviw0e3479f2018-09-10 16:49:30 -0700319 virtual void TearDown() {
320 mBlackBgSurface = 0;
321 mClient->dispose();
322 mClient = 0;
323 }
324
Marissa Wall61c58622018-07-18 10:12:20 -0700325 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
326 uint32_t flags = 0) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700327 auto layer =
328 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
329 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
330
331 status_t error = Transaction()
332 .setLayerStack(layer, mDisplayLayerStack)
333 .setLayer(layer, mLayerZBase)
334 .apply();
335 if (error != NO_ERROR) {
336 ADD_FAILURE() << "failed to initialize SurfaceControl";
337 layer.clear();
338 }
339
340 return layer;
341 }
342
Marissa Wall61c58622018-07-18 10:12:20 -0700343 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700344 // wait for previous transactions (such as setSize) to complete
345 Transaction().apply(true);
346
347 ANativeWindow_Buffer buffer = {};
348 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
349
350 return buffer;
351 }
352
Marissa Wall61c58622018-07-18 10:12:20 -0700353 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700354 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
355
356 // wait for the newly posted buffer to be latched
357 waitForLayerBuffers();
358 }
359
Marissa Wall61c58622018-07-18 10:12:20 -0700360 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
361 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700362 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700363 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
364 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
365 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700366 }
367
Marissa Wall61c58622018-07-18 10:12:20 -0700368 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
369 int32_t bufferWidth, int32_t bufferHeight) {
370 sp<GraphicBuffer> buffer =
371 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
372 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
373 BufferUsage::COMPOSER_OVERLAY,
374 "test");
375 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
376 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
377 }
378
379 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
380 int32_t bufferWidth, int32_t bufferHeight) {
381 switch (mLayerType) {
382 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
383 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
384 break;
385 case ISurfaceComposerClient::eFXSurfaceBufferState:
386 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
387 break;
388 default:
389 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
390 }
391 }
392
393 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
394 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700395 const Color& topRight, const Color& bottomLeft,
396 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700397 switch (mLayerType) {
398 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
399 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
400 bottomLeft, bottomRight);
401 break;
402 case ISurfaceComposerClient::eFXSurfaceBufferState:
403 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
404 bottomLeft, bottomRight);
405 break;
406 default:
407 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
408 }
409 }
410
411 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
412 int32_t bufferHeight, const Color& topLeft,
413 const Color& topRight, const Color& bottomLeft,
414 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700415 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700416 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
417 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700418
Marissa Wall61c58622018-07-18 10:12:20 -0700419 const int32_t halfW = bufferWidth / 2;
420 const int32_t halfH = bufferHeight / 2;
421 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
422 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
423 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
424 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
425 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700426
Marissa Wall61c58622018-07-18 10:12:20 -0700427 postBufferQueueLayerBuffer(layer);
428 }
429
430 virtual void fillBufferStateLayerQuadrant(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) {
434 sp<GraphicBuffer> buffer =
435 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
436 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
437 BufferUsage::COMPOSER_OVERLAY,
438 "test");
439
440 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
441
442 const int32_t halfW = bufferWidth / 2;
443 const int32_t halfH = bufferHeight / 2;
444 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
445 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
446 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
447 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
448
449 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700450 }
451
chaviw0e3479f2018-09-10 16:49:30 -0700452 std::unique_ptr<ScreenCapture> screenshot() {
453 std::unique_ptr<ScreenCapture> screenshot;
454 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700455 return screenshot;
456 }
457
458 sp<SurfaceComposerClient> mClient;
459
460 sp<IBinder> mDisplay;
461 uint32_t mDisplayWidth;
462 uint32_t mDisplayHeight;
463 uint32_t mDisplayLayerStack;
464
465 // leave room for ~256 layers
466 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
467
Marissa Wall61c58622018-07-18 10:12:20 -0700468 void setPositionWithResizeHelper(uint32_t layerType);
469 void setSizeBasicHelper(uint32_t layerType);
470 void setMatrixWithResizeHelper(uint32_t layerType);
471
chaviw0e3479f2018-09-10 16:49:30 -0700472 sp<SurfaceControl> mBlackBgSurface;
Chia-I Wu718daf82017-10-20 11:57:17 -0700473private:
474 void SetUpDisplay() {
475 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
476 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
477
478 // get display width/height
479 DisplayInfo info;
480 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
481 mDisplayWidth = info.w;
482 mDisplayHeight = info.h;
483
484 // After a new buffer is queued, SurfaceFlinger is notified and will
485 // latch the new buffer on next vsync. Let's heuristically wait for 3
486 // vsyncs.
487 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
488
489 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700490
491 mBlackBgSurface = mClient->createSurface(String8("BaseSurface"), mDisplayWidth,
492 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
493 ISurfaceComposerClient::eFXSurfaceColor);
494
Chia-I Wu718daf82017-10-20 11:57:17 -0700495 // set layer stack (b/68888219)
496 Transaction t;
497 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
chaviw0e3479f2018-09-10 16:49:30 -0700498 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
499 t.setColor(mBlackBgSurface, half3{0, 0, 0});
500 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700501 t.apply();
502 }
503
chaviw0e3479f2018-09-10 16:49:30 -0700504 void waitForLayerBuffers() {
505 // Request an empty transaction to get applied synchronously to ensure the buffer is
506 // latched.
507 Transaction().apply(true);
508 usleep(mBufferPostDelay);
509 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700510
511 int32_t mBufferPostDelay;
512};
513
Marissa Wall61c58622018-07-18 10:12:20 -0700514class LayerTypeTransactionTest : public LayerTransactionTest,
515 public ::testing::WithParamInterface<uint32_t> {
516public:
517 LayerTypeTransactionTest() { mLayerType = GetParam(); }
518
519 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
520 uint32_t flags = 0) override {
521 // if the flags already have a layer type specified, return an error
522 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
523 return nullptr;
524 }
525 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType);
526 }
527
528 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
529 int32_t bufferHeight) {
530 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
531 bufferWidth, bufferHeight));
532 }
533
534 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
535 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
536 const Color& bottomLeft, const Color& bottomRight) {
537 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
538 bufferWidth, bufferHeight,
539 topLeft, topRight,
540 bottomLeft, bottomRight));
541 }
542
543protected:
544 uint32_t mLayerType;
545};
546
547INSTANTIATE_TEST_CASE_P(
548 LayerTypeTransactionTests, LayerTypeTransactionTest,
549 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
550 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
551
552TEST_P(LayerTypeTransactionTest, SetPositionBasic) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700553 sp<SurfaceControl> layer;
554 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700555 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700556
557 {
558 SCOPED_TRACE("default position");
559 auto shot = screenshot();
560 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
561 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
562 }
563
564 Transaction().setPosition(layer, 5, 10).apply();
565 {
566 SCOPED_TRACE("new position");
567 auto shot = screenshot();
568 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
569 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
570 }
571}
572
Marissa Wall61c58622018-07-18 10:12:20 -0700573TEST_P(LayerTypeTransactionTest, SetPositionRounding) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700574 sp<SurfaceControl> layer;
575 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700576 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700577
578 // GLES requires only 4 bits of subpixel precision during rasterization
579 // XXX GLES composition does not match HWC composition due to precision
580 // loss (b/69315223)
581 const float epsilon = 1.0f / 16.0f;
582 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
583 {
584 SCOPED_TRACE("rounding down");
585 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
586 }
587
588 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
589 {
590 SCOPED_TRACE("rounding up");
591 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
592 }
593}
594
Marissa Wall61c58622018-07-18 10:12:20 -0700595TEST_P(LayerTypeTransactionTest, SetPositionOutOfBounds) {
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 Transaction().setPosition(layer, -32, -32).apply();
601 {
602 SCOPED_TRACE("negative coordinates");
603 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
604 }
605
606 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
607 {
608 SCOPED_TRACE("positive coordinates");
609 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
610 }
611}
612
Marissa Wall61c58622018-07-18 10:12:20 -0700613TEST_P(LayerTypeTransactionTest, SetPositionPartiallyOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700614 sp<SurfaceControl> layer;
615 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700616 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700617
618 // partially out of bounds
619 Transaction().setPosition(layer, -30, -30).apply();
620 {
621 SCOPED_TRACE("negative coordinates");
622 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
623 }
624
625 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
626 {
627 SCOPED_TRACE("positive coordinates");
628 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
629 mDisplayHeight),
630 Color::RED);
631 }
632}
633
Marissa Wall61c58622018-07-18 10:12:20 -0700634void LayerTransactionTest::setPositionWithResizeHelper(uint32_t layerType) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700635 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700636 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
637 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700638
639 // setPosition is applied immediately by default, with or without resize
640 // pending
641 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
642 {
643 SCOPED_TRACE("resize pending");
644 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700645 Rect rect;
646 switch (layerType) {
647 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
648 rect = {5, 10, 37, 42};
649 break;
650 case ISurfaceComposerClient::eFXSurfaceBufferState:
651 rect = {5, 10, 69, 74};
652 break;
653 default:
654 ASSERT_FALSE(true) << "Unsupported layer type";
655 }
656
657 shot->expectColor(rect, Color::RED);
658 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700659 }
660
Marissa Wall61c58622018-07-18 10:12:20 -0700661 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700662 {
663 SCOPED_TRACE("resize applied");
664 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
665 }
666}
667
Marissa Wall61c58622018-07-18 10:12:20 -0700668TEST_F(LayerTransactionTest, SetPositionWithResize_BufferQueue) {
669 ASSERT_NO_FATAL_FAILURE(
670 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
671}
672
673TEST_F(LayerTransactionTest, SetPositionWithResize_BufferState) {
674 ASSERT_NO_FATAL_FAILURE(
675 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
676}
677
678TEST_F(LayerTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700679 sp<SurfaceControl> layer;
680 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700681 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700682
683 // request setPosition to be applied with the next resize
684 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
685 {
686 SCOPED_TRACE("new position pending");
687 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
688 }
689
690 Transaction().setPosition(layer, 15, 20).apply();
691 {
692 SCOPED_TRACE("pending new position modified");
693 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
694 }
695
696 Transaction().setSize(layer, 64, 64).apply();
697 {
698 SCOPED_TRACE("resize pending");
699 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
700 }
701
702 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700703 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700704 {
705 SCOPED_TRACE("new position applied");
706 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
707 }
708}
709
Marissa Wall61c58622018-07-18 10:12:20 -0700710TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700711 sp<SurfaceControl> layer;
712 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700713 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700714
715 // setPosition is not immediate even with SCALE_TO_WINDOW override
716 Transaction()
717 .setPosition(layer, 5, 10)
718 .setSize(layer, 64, 64)
719 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
720 .setGeometryAppliesWithResize(layer)
721 .apply();
722 {
723 SCOPED_TRACE("new position pending");
724 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
725 }
726
Marissa Wall61c58622018-07-18 10:12:20 -0700727 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700728 {
729 SCOPED_TRACE("new position applied");
730 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
731 }
732}
733
Marissa Wall61c58622018-07-18 10:12:20 -0700734void LayerTransactionTest::setSizeBasicHelper(uint32_t layerType) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700735 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700736 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
737 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700738
739 Transaction().setSize(layer, 64, 64).apply();
740 {
741 SCOPED_TRACE("resize pending");
742 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700743 Rect rect;
744 switch (layerType) {
745 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
746 rect = {0, 0, 32, 32};
747 break;
748 case ISurfaceComposerClient::eFXSurfaceBufferState:
749 rect = {0, 0, 64, 64};
750 break;
751 default:
752 ASSERT_FALSE(true) << "Unsupported layer type";
753 }
754 shot->expectColor(rect, Color::RED);
755 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700756 }
757
Marissa Wall61c58622018-07-18 10:12:20 -0700758 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700759 {
760 SCOPED_TRACE("resize applied");
761 auto shot = screenshot();
762 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
763 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
764 }
765}
766
Marissa Wall61c58622018-07-18 10:12:20 -0700767TEST_F(LayerTransactionTest, SetSizeBasic_BufferQueue) {
768 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue);
769}
770
771TEST_F(LayerTransactionTest, SetSizeBasic_BufferState) {
772 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState);
773}
774
775TEST_P(LayerTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700776 // cannot test robustness against invalid sizes (zero or really huge)
777}
778
Marissa Wall61c58622018-07-18 10:12:20 -0700779TEST_P(LayerTypeTransactionTest, SetSizeWithScaleToWindow) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700780 sp<SurfaceControl> layer;
781 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700782 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700783
784 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
785 Transaction()
786 .setSize(layer, 64, 64)
787 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
788 .apply();
789 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
790}
791
Marissa Wall61c58622018-07-18 10:12:20 -0700792TEST_P(LayerTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700793 sp<SurfaceControl> layerR;
794 sp<SurfaceControl> layerG;
795 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700796 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700797 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700798 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700799
800 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
801 {
802 SCOPED_TRACE("layerR");
803 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
804 }
805
806 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
807 {
808 SCOPED_TRACE("layerG");
809 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
810 }
811}
812
Marissa Wall61c58622018-07-18 10:12:20 -0700813TEST_P(LayerTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700814 sp<SurfaceControl> parent =
815 LayerTransactionTest::createLayer("Parent", mDisplayWidth, mDisplayHeight,
816 ISurfaceComposerClient::eFXSurfaceContainer);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700817 sp<SurfaceControl> layerR;
818 sp<SurfaceControl> layerG;
819 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700820 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700821 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700822 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700823
chaviw0e3479f2018-09-10 16:49:30 -0700824 Transaction()
825 .reparent(layerR, parent->getHandle())
826 .reparent(layerG, parent->getHandle())
827 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700828 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
829 {
830 SCOPED_TRACE("layerR");
chaviw0e3479f2018-09-10 16:49:30 -0700831 auto shot = screenshot();
832 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700833 }
834
835 Transaction().setLayer(layerR, -3).apply();
836 {
837 SCOPED_TRACE("layerG");
chaviw0e3479f2018-09-10 16:49:30 -0700838 auto shot = screenshot();
839 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700840 }
841}
842
Marissa Wall61c58622018-07-18 10:12:20 -0700843TEST_P(LayerTypeTransactionTest, SetRelativeZBasic) {
Chia-I Wu49313302017-10-31 10:14:40 -0700844 sp<SurfaceControl> layerR;
845 sp<SurfaceControl> layerG;
846 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700847 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700848 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700849 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700850
851 Transaction()
852 .setPosition(layerG, 16, 16)
853 .setRelativeLayer(layerG, layerR->getHandle(), 1)
854 .apply();
855 {
856 SCOPED_TRACE("layerG above");
857 auto shot = screenshot();
858 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
859 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
860 }
861
862 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
863 {
864 SCOPED_TRACE("layerG below");
865 auto shot = screenshot();
866 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
867 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
868 }
869}
870
Marissa Wall61c58622018-07-18 10:12:20 -0700871TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700872 sp<SurfaceControl> parent =
873 LayerTransactionTest::createLayer("Parent", mDisplayWidth, mDisplayHeight,
874 ISurfaceComposerClient::eFXSurfaceContainer);
Chia-I Wuec2d9852017-11-21 09:21:01 -0800875 sp<SurfaceControl> layerR;
876 sp<SurfaceControl> layerG;
877 sp<SurfaceControl> layerB;
878 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700879 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800880 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700881 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800882 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700883 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800884
chaviw0e3479f2018-09-10 16:49:30 -0700885 Transaction()
886 .reparent(layerB, parent->getHandle())
887 .apply();
888
Chia-I Wuec2d9852017-11-21 09:21:01 -0800889 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
890 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
891
chaviw0e3479f2018-09-10 16:49:30 -0700892 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800893 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700894 sp<IBinder> parentHandle = parent->getHandle();
895 ScreenCapture::captureLayers(&screenshot, parentHandle);
Chia-I Wuec2d9852017-11-21 09:21:01 -0800896 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
897}
898
Marissa Wall61c58622018-07-18 10:12:20 -0700899TEST_P(LayerTypeTransactionTest, SetRelativeZGroup) {
Chia-I Wu49313302017-10-31 10:14:40 -0700900 sp<SurfaceControl> layerR;
901 sp<SurfaceControl> layerG;
902 sp<SurfaceControl> layerB;
903 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700904 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700905 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700906 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700907 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700908 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700909
910 // layerR = 0, layerG = layerR + 3, layerB = 2
911 Transaction()
912 .setPosition(layerG, 8, 8)
913 .setRelativeLayer(layerG, layerR->getHandle(), 3)
914 .setPosition(layerB, 16, 16)
915 .setLayer(layerB, mLayerZBase + 2)
916 .apply();
917 {
918 SCOPED_TRACE("(layerR < layerG) < layerB");
919 auto shot = screenshot();
920 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
921 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
922 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
923 }
924
925 // layerR = 4, layerG = layerR + 3, layerB = 2
926 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
927 {
928 SCOPED_TRACE("layerB < (layerR < layerG)");
929 auto shot = screenshot();
930 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
931 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
932 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
933 }
934
935 // layerR = 4, layerG = layerR - 3, layerB = 2
936 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
937 {
938 SCOPED_TRACE("layerB < (layerG < layerR)");
939 auto shot = screenshot();
940 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
941 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
942 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
943 }
944
945 // restore to absolute z
946 // layerR = 4, layerG = 0, layerB = 2
947 Transaction().setLayer(layerG, mLayerZBase).apply();
948 {
949 SCOPED_TRACE("layerG < layerB < layerR");
950 auto shot = screenshot();
951 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
952 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
953 }
954
955 // layerR should not affect layerG anymore
956 // layerR = 1, layerG = 0, layerB = 2
957 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
958 {
959 SCOPED_TRACE("layerG < layerR < layerB");
960 auto shot = screenshot();
961 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
962 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
963 }
964}
965
Marissa Wall61c58622018-07-18 10:12:20 -0700966TEST_P(LayerTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -0700967 sp<SurfaceControl> layerR;
968 sp<SurfaceControl> layerG;
969
970 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700971 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700972 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700973 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700974
975 Transaction()
976 .setPosition(layerG, 16, 16)
977 .setRelativeLayer(layerG, layerR->getHandle(), 1)
978 .apply();
979
980 mClient->destroySurface(layerG->getHandle());
981 // layerG should have been removed
982 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
983}
984
Marissa Wall61c58622018-07-18 10:12:20 -0700985TEST_P(LayerTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -0700986 sp<SurfaceControl> layer;
987 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700988 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700989
990 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
991 {
992 SCOPED_TRACE("layer hidden");
993 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
994 }
995
996 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
997 {
998 SCOPED_TRACE("layer shown");
999 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1000 }
1001}
1002
Marissa Wall61c58622018-07-18 10:12:20 -07001003TEST_P(LayerTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001004 const Color translucentRed = {100, 0, 0, 100};
1005 sp<SurfaceControl> layerR;
1006 sp<SurfaceControl> layerG;
1007 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001008 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001009 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001010 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001011
1012 Transaction()
1013 .setLayer(layerR, mLayerZBase + 1)
1014 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1015 .apply();
1016 {
1017 SCOPED_TRACE("layerR opaque");
1018 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
1019 }
1020
1021 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1022 {
1023 SCOPED_TRACE("layerR translucent");
1024 const uint8_t g = uint8_t(255 - translucentRed.a);
1025 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
1026 }
1027}
1028
Marissa Wall61c58622018-07-18 10:12:20 -07001029TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001030 sp<SurfaceControl> layer;
1031 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001032 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001033
1034 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001035 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001036 Transaction()
1037 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1038 .apply(true);
1039 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001040 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001041
1042 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1043 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001044 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001045}
1046
Marissa Wall61c58622018-07-18 10:12:20 -07001047TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001048 const Rect top(0, 0, 32, 16);
1049 const Rect bottom(0, 16, 32, 32);
1050 sp<SurfaceControl> layer;
1051 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1052
1053 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001054 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1055 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1056 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001057 // setTransparentRegionHint always applies to the following buffer
1058 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001059 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001060 {
1061 SCOPED_TRACE("top transparent");
1062 auto shot = screenshot();
1063 shot->expectColor(top, Color::BLACK);
1064 shot->expectColor(bottom, Color::RED);
1065 }
1066
1067 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1068 {
1069 SCOPED_TRACE("transparent region hint pending");
1070 auto shot = screenshot();
1071 shot->expectColor(top, Color::BLACK);
1072 shot->expectColor(bottom, Color::RED);
1073 }
1074
Marissa Wall61c58622018-07-18 10:12:20 -07001075 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1076 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1077 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1078 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001079 {
1080 SCOPED_TRACE("bottom transparent");
1081 auto shot = screenshot();
1082 shot->expectColor(top, Color::RED);
1083 shot->expectColor(bottom, Color::BLACK);
1084 }
1085}
1086
Marissa Wall61c58622018-07-18 10:12:20 -07001087TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1088 const Rect top(0, 0, 32, 16);
1089 const Rect bottom(0, 16, 32, 32);
1090 sp<SurfaceControl> layer;
1091 ASSERT_NO_FATAL_FAILURE(
1092 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1093
1094 sp<GraphicBuffer> buffer =
1095 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1096 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1097 BufferUsage::COMPOSER_OVERLAY,
1098 "test");
1099
1100 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1101 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1102 Transaction()
1103 .setTransparentRegionHint(layer, Region(top))
1104 .setBuffer(layer, buffer)
1105 .setSize(layer, 32, 32)
1106 .apply();
1107 {
1108 SCOPED_TRACE("top transparent");
1109 auto shot = screenshot();
1110 shot->expectColor(top, Color::BLACK);
1111 shot->expectColor(bottom, Color::RED);
1112 }
1113
1114 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1115 {
1116 SCOPED_TRACE("transparent region hint intermediate");
1117 auto shot = screenshot();
1118 shot->expectColor(top, Color::BLACK);
1119 shot->expectColor(bottom, Color::BLACK);
1120 }
1121
1122 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1123 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1124 BufferUsage::COMPOSER_OVERLAY,
1125 "test");
1126
1127 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1128 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
1129 Transaction().setBuffer(layer, buffer).setSize(layer, 32, 32).apply();
1130 {
1131 SCOPED_TRACE("bottom transparent");
1132 auto shot = screenshot();
1133 shot->expectColor(top, Color::RED);
1134 shot->expectColor(bottom, Color::BLACK);
1135 }
1136}
1137
1138TEST_P(LayerTypeTransactionTest, SetTransparentRegionHintOutOfBounds) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001139 sp<SurfaceControl> layerTransparent;
1140 sp<SurfaceControl> layerR;
1141 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1142 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1143
1144 // check that transparent region hint is bound by the layer size
1145 Transaction()
1146 .setTransparentRegionHint(layerTransparent,
1147 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
1148 .setPosition(layerR, 16, 16)
1149 .setLayer(layerR, mLayerZBase + 1)
1150 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001151 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1152 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001153 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1154}
1155
Marissa Wall61c58622018-07-18 10:12:20 -07001156TEST_P(LayerTypeTransactionTest, SetAlphaBasic) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001157 sp<SurfaceControl> layer1;
1158 sp<SurfaceControl> layer2;
1159 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
1160 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001161 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}, 32, 32));
1162 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001163
1164 Transaction()
1165 .setAlpha(layer1, 0.25f)
1166 .setAlpha(layer2, 0.75f)
1167 .setPosition(layer2, 16, 0)
1168 .setLayer(layer2, mLayerZBase + 1)
1169 .apply();
1170 {
1171 auto shot = screenshot();
1172 uint8_t r = 16; // 64 * 0.25f
1173 uint8_t g = 48; // 64 * 0.75f
1174 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1175 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1176
1177 r /= 4; // r * (1.0f - 0.75f)
1178 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1179 }
1180}
1181
Marissa Wall61c58622018-07-18 10:12:20 -07001182TEST_P(LayerTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001183 const Color color = {64, 0, 0, 255};
1184 sp<SurfaceControl> layer;
1185 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001186 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001187
1188 Transaction().setAlpha(layer, 2.0f).apply();
1189 {
1190 SCOPED_TRACE("clamped to 1.0f");
1191 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1192 }
1193
1194 Transaction().setAlpha(layer, -1.0f).apply();
1195 {
1196 SCOPED_TRACE("clamped to 0.0f");
1197 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1198 }
1199}
1200
Chia-I Wue4ef6102017-11-01 15:16:35 -07001201TEST_F(LayerTransactionTest, SetColorBasic) {
1202 sp<SurfaceControl> bufferLayer;
1203 sp<SurfaceControl> colorLayer;
1204 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001205 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001206 ASSERT_NO_FATAL_FAILURE(
1207 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1208
1209 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
1210 {
1211 SCOPED_TRACE("default color");
1212 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1213 }
1214
1215 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1216 const Color expected = {15, 51, 85, 255};
1217 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1218 // channel) should be less than one
1219 const uint8_t tolerance = 1;
1220 Transaction().setColor(colorLayer, color).apply();
1221 {
1222 SCOPED_TRACE("new color");
1223 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1224 }
1225}
1226
1227TEST_F(LayerTransactionTest, SetColorClamped) {
1228 sp<SurfaceControl> colorLayer;
1229 ASSERT_NO_FATAL_FAILURE(
1230 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1231
1232 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
1233 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1234}
1235
1236TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1237 sp<SurfaceControl> bufferLayer;
1238 sp<SurfaceControl> colorLayer;
1239 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001240 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001241 ASSERT_NO_FATAL_FAILURE(
1242 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1243
1244 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1245 const float alpha = 0.25f;
1246 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1247 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1248 // channel) should be less than one
1249 const uint8_t tolerance = 1;
1250 Transaction()
1251 .setColor(colorLayer, color)
1252 .setAlpha(colorLayer, alpha)
1253 .setLayer(colorLayer, mLayerZBase + 1)
1254 .apply();
1255 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1256 tolerance);
1257}
1258
Adrian Roosb7a96502018-04-08 11:38:55 -07001259TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1260 sp<SurfaceControl> bufferLayer;
1261 sp<SurfaceControl> parentLayer;
1262 sp<SurfaceControl> colorLayer;
1263 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1264 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001265 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Adrian Roosb7a96502018-04-08 11:38:55 -07001266 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer(
1267 "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1268
1269 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1270 const float alpha = 0.25f;
1271 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1272 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1273 // channel) should be less than one
1274 const uint8_t tolerance = 1;
1275 Transaction()
1276 .reparent(colorLayer, parentLayer->getHandle())
1277 .setColor(colorLayer, color)
1278 .setAlpha(parentLayer, alpha)
1279 .setLayer(parentLayer, mLayerZBase + 1)
1280 .apply();
1281 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1282 tolerance);
1283}
1284
Marissa Wall61c58622018-07-18 10:12:20 -07001285TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001286 sp<SurfaceControl> bufferLayer;
1287 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001288 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001289
1290 // color is ignored
1291 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1292 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1293}
1294
Marissa Wall61c58622018-07-18 10:12:20 -07001295TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001296 sp<SurfaceControl> layer;
1297 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001298 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001299
1300 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1301 {
1302 SCOPED_TRACE("non-existing layer stack");
1303 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1304 }
1305
1306 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1307 {
1308 SCOPED_TRACE("original layer stack");
1309 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1310 }
1311}
1312
Marissa Wall61c58622018-07-18 10:12:20 -07001313TEST_P(LayerTypeTransactionTest, SetMatrixBasic) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001314 sp<SurfaceControl> layer;
1315 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1316 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001317 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001318
1319 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1320 {
1321 SCOPED_TRACE("IDENTITY");
1322 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1323 Color::WHITE);
1324 }
1325
1326 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1327 {
1328 SCOPED_TRACE("FLIP_H");
1329 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1330 Color::BLUE);
1331 }
1332
1333 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1334 {
1335 SCOPED_TRACE("FLIP_V");
1336 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1337 Color::GREEN);
1338 }
1339
1340 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1341 {
1342 SCOPED_TRACE("ROT_90");
1343 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1344 Color::GREEN);
1345 }
1346
1347 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1348 {
1349 SCOPED_TRACE("SCALE");
1350 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1351 Color::WHITE, true /* filtered */);
1352 }
1353}
1354
Marissa Wall61c58622018-07-18 10:12:20 -07001355TEST_P(LayerTypeTransactionTest, SetMatrixRot45) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001356 sp<SurfaceControl> layer;
1357 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1358 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001359 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001360
1361 const float rot = M_SQRT1_2; // 45 degrees
1362 const float trans = M_SQRT2 * 16.0f;
1363 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1364
1365 auto shot = screenshot();
1366 // check a 8x8 region inside each color
1367 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1368 const int32_t halfL = 4;
1369 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1370 };
1371 const int32_t unit = int32_t(trans / 2);
1372 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1373 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1374 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1375 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1376}
1377
Marissa Wall61c58622018-07-18 10:12:20 -07001378void LayerTransactionTest::setMatrixWithResizeHelper(uint32_t layerType) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001379 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07001380 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
1381 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001382
1383 // setMatrix is applied after any pending resize, unlike setPosition
1384 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1385 {
1386 SCOPED_TRACE("resize pending");
1387 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -07001388 Rect rect;
1389 switch (layerType) {
1390 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1391 rect = {0, 0, 32, 32};
1392 break;
1393 case ISurfaceComposerClient::eFXSurfaceBufferState:
1394 rect = {0, 0, 128, 128};
1395 break;
1396 default:
1397 ASSERT_FALSE(true) << "Unsupported layer type";
1398 }
1399 shot->expectColor(rect, Color::RED);
1400 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001401 }
1402
Marissa Wall61c58622018-07-18 10:12:20 -07001403 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001404 {
1405 SCOPED_TRACE("resize applied");
1406 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1407 }
1408}
1409
Marissa Wall61c58622018-07-18 10:12:20 -07001410TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
1411 ASSERT_NO_FATAL_FAILURE(
1412 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1413}
1414
1415TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferState) {
1416 ASSERT_NO_FATAL_FAILURE(
1417 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1418}
1419
1420TEST_P(LayerTypeTransactionTest, SetMatrixWithScaleToWindow) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001421 sp<SurfaceControl> layer;
1422 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001423 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001424
1425 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1426 Transaction()
1427 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1428 .setSize(layer, 64, 64)
1429 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1430 .apply();
1431 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1432}
1433
Marissa Wall61c58622018-07-18 10:12:20 -07001434TEST_P(LayerTypeTransactionTest, SetOverrideScalingModeBasic) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001435 sp<SurfaceControl> layer;
1436 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1437 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001438 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001439
1440 // XXX SCALE_CROP is not respected; calling setSize and
1441 // setOverrideScalingMode in separate transactions does not work
1442 // (b/69315456)
1443 Transaction()
1444 .setSize(layer, 64, 16)
1445 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1446 .apply();
1447 {
1448 SCOPED_TRACE("SCALE_TO_WINDOW");
1449 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1450 Color::WHITE, true /* filtered */);
1451 }
1452}
1453
Dan Stoza000dd012018-08-01 13:31:52 -07001454TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1455 sp<SurfaceControl> layer;
1456 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1457
1458 sp<IBinder> handle = layer->getHandle();
1459 ASSERT_TRUE(handle != nullptr);
1460
1461 FrameStats frameStats;
1462 mClient->getLayerFrameStats(handle, &frameStats);
1463
1464 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1465}
1466
Marissa Wall61c58622018-07-18 10:12:20 -07001467TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001468 sp<SurfaceControl> layer;
1469 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001470 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001471 const Rect crop(8, 8, 24, 24);
1472
Marissa Wallf58c14b2018-07-24 10:50:43 -07001473 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001474 auto shot = screenshot();
1475 shot->expectColor(crop, Color::RED);
1476 shot->expectBorder(crop, Color::BLACK);
1477}
1478
Marissa Wall61c58622018-07-18 10:12:20 -07001479TEST_F(LayerTransactionTest, SetCropBasic_BufferState) {
1480 sp<SurfaceControl> layer;
1481 ASSERT_NO_FATAL_FAILURE(
1482 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1483 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1484 const Rect crop(8, 8, 24, 24);
1485
1486 Transaction().setCrop(layer, crop).apply();
1487 auto shot = screenshot();
1488 shot->expectColor(crop, Color::RED);
1489 shot->expectBorder(crop, Color::BLACK);
1490}
1491
1492TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001493 sp<SurfaceControl> layer;
1494 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001495 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001496
1497 {
1498 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001499 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001500 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1501 }
1502
1503 {
1504 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001505 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001506 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1507 }
1508}
1509
Marissa Wall61c58622018-07-18 10:12:20 -07001510TEST_F(LayerTransactionTest, SetCropEmpty_BufferState) {
1511 sp<SurfaceControl> layer;
1512 ASSERT_NO_FATAL_FAILURE(
1513 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1514 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1515
1516 {
1517 SCOPED_TRACE("empty rect");
1518 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1519 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1520 }
1521
1522 {
1523 SCOPED_TRACE("negative rect");
1524 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1525 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1526 }
1527}
1528
1529TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001530 sp<SurfaceControl> layer;
1531 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001532 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001533
Marissa Wallf58c14b2018-07-24 10:50:43 -07001534 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001535 auto shot = screenshot();
1536 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1537 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1538}
1539
Marissa Wall61c58622018-07-18 10:12:20 -07001540TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1541 sp<SurfaceControl> layer;
1542 ASSERT_NO_FATAL_FAILURE(
1543 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1544 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1545
1546 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1547 auto shot = screenshot();
1548 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1549 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1550}
1551
1552TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001553 sp<SurfaceControl> layer;
1554 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001555 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001556
1557 const Point position(32, 32);
1558 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001559 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001560 auto shot = screenshot();
1561 shot->expectColor(crop + position, Color::RED);
1562 shot->expectBorder(crop + position, Color::BLACK);
1563}
1564
Marissa Wall61c58622018-07-18 10:12:20 -07001565TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1566 sp<SurfaceControl> layer;
1567 ASSERT_NO_FATAL_FAILURE(
1568 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1569 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1570
1571 const Point position(32, 32);
1572 const Rect crop(8, 8, 24, 24);
1573 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1574 auto shot = screenshot();
1575 shot->expectColor(crop + position, Color::RED);
1576 shot->expectBorder(crop + position, Color::BLACK);
1577}
1578
1579TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001580 sp<SurfaceControl> layer;
1581 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001582 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001583
1584 // crop is affected by matrix
1585 Transaction()
1586 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001587 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001588 .apply();
1589 auto shot = screenshot();
1590 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1591 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1592}
1593
Marissa Wall61c58622018-07-18 10:12:20 -07001594TEST_F(LayerTransactionTest, SetCropWithScale_BufferState) {
1595 sp<SurfaceControl> layer;
1596 ASSERT_NO_FATAL_FAILURE(
1597 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1598 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1599
1600 // crop is affected by matrix
1601 Transaction()
1602 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1603 .setCrop(layer, Rect(8, 8, 24, 24))
1604 .apply();
1605 auto shot = screenshot();
1606 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1607 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1608}
1609
1610TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001611 sp<SurfaceControl> layer;
1612 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001613 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001614
Marissa Wallf58c14b2018-07-24 10:50:43 -07001615 // setCrop_legacy is applied immediately by default, with or without resize pending
1616 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001617 {
1618 SCOPED_TRACE("resize pending");
1619 auto shot = screenshot();
1620 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1621 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1622 }
1623
Marissa Wall61c58622018-07-18 10:12:20 -07001624 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001625 {
1626 SCOPED_TRACE("resize applied");
1627 auto shot = screenshot();
1628 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1629 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1630 }
1631}
1632
Marissa Wall61c58622018-07-18 10:12:20 -07001633TEST_F(LayerTransactionTest, SetCropWithResize_BufferState) {
1634 sp<SurfaceControl> layer;
1635 ASSERT_NO_FATAL_FAILURE(
1636 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1637 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1638
1639 // setCrop_legacy is applied immediately by default, with or without resize pending
1640 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1641 {
1642 SCOPED_TRACE("new buffer pending");
1643 auto shot = screenshot();
1644 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1645 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1646 }
1647
1648 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1649 {
1650 SCOPED_TRACE("new buffer");
1651 auto shot = screenshot();
1652 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1653 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1654 }
1655}
1656
1657TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001658 sp<SurfaceControl> layer;
1659 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001660 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001661
Marissa Wallf58c14b2018-07-24 10:50:43 -07001662 // request setCrop_legacy to be applied with the next resize
1663 Transaction()
1664 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1665 .setGeometryAppliesWithResize(layer)
1666 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001667 {
1668 SCOPED_TRACE("waiting for next resize");
1669 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1670 }
1671
Marissa Wallf58c14b2018-07-24 10:50:43 -07001672 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001673 {
1674 SCOPED_TRACE("pending crop modified");
1675 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1676 }
1677
1678 Transaction().setSize(layer, 16, 16).apply();
1679 {
1680 SCOPED_TRACE("resize pending");
1681 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1682 }
1683
1684 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001685 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001686 {
1687 SCOPED_TRACE("new crop applied");
1688 auto shot = screenshot();
1689 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1690 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1691 }
1692}
1693
Marissa Wall61c58622018-07-18 10:12:20 -07001694TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferState) {
1695 sp<SurfaceControl> layer;
1696 ASSERT_NO_FATAL_FAILURE(
1697 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1698 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1699
1700 // request setCrop_legacy to be applied with the next resize
1701 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1702 {
1703 SCOPED_TRACE("set crop 1");
1704 screenshot()->expectColor(Rect(8, 8, 24, 24), Color::RED);
1705 }
1706
1707 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1708 {
1709 SCOPED_TRACE("set crop 2");
1710 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1711 }
1712
1713 Transaction().setSize(layer, 16, 16).apply();
1714 {
1715 SCOPED_TRACE("resize");
1716 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1717 }
1718
1719 // finally resize
1720 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1721 {
1722 SCOPED_TRACE("new buffer");
1723 auto shot = screenshot();
1724 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1725 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1726 }
1727}
1728
1729TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001730 sp<SurfaceControl> layer;
1731 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001732 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001733
Marissa Wallf58c14b2018-07-24 10:50:43 -07001734 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001735 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001736 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001737 .setSize(layer, 16, 16)
1738 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1739 .setGeometryAppliesWithResize(layer)
1740 .apply();
1741 {
1742 SCOPED_TRACE("new crop pending");
1743 auto shot = screenshot();
1744 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1745 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1746 }
1747
1748 // XXX crop is never latched without other geometry change (b/69315677)
1749 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001750 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001751 Transaction().setPosition(layer, 0, 0).apply();
1752 {
1753 SCOPED_TRACE("new crop applied");
1754 auto shot = screenshot();
1755 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1756 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1757 }
1758}
1759
Marissa Wall61c58622018-07-18 10:12:20 -07001760TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferState) {
1761 sp<SurfaceControl> layer;
1762 ASSERT_NO_FATAL_FAILURE(
1763 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1764 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1765
1766 // all properties are applied immediate so setGeometryAppliesWithResize has no effect
1767 Transaction()
1768 .setCrop(layer, Rect(4, 4, 12, 12))
1769 .setSize(layer, 16, 16)
1770 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1771 .setGeometryAppliesWithResize(layer)
1772 .apply();
1773 {
1774 SCOPED_TRACE("new crop pending");
1775 auto shot = screenshot();
1776 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1777 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1778 }
1779
1780 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1781 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1782 Transaction().setPosition(layer, 0, 0).apply();
1783 {
1784 SCOPED_TRACE("new crop applied");
1785 auto shot = screenshot();
1786 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1787 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1788 }
1789}
1790
Marissa Wall61c58622018-07-18 10:12:20 -07001791TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1792 sp<SurfaceControl> layer;
1793 ASSERT_NO_FATAL_FAILURE(
1794 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1795
1796 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1797
1798 auto shot = screenshot();
1799 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1800 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1801}
1802
1803TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1804 sp<SurfaceControl> layer;
1805 ASSERT_NO_FATAL_FAILURE(
1806 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1807
1808 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1809
1810 {
1811 SCOPED_TRACE("set buffer 1");
1812 auto shot = screenshot();
1813 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1814 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1815 }
1816
1817 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
1818
1819 {
1820 SCOPED_TRACE("set buffer 2");
1821 auto shot = screenshot();
1822 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1823 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1824 }
1825
1826 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1827
1828 {
1829 SCOPED_TRACE("set buffer 3");
1830 auto shot = screenshot();
1831 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1832 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1833 }
1834}
1835
1836TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
1837 sp<SurfaceControl> layer1;
1838 ASSERT_NO_FATAL_FAILURE(
1839 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
1840
1841 sp<SurfaceControl> layer2;
1842 ASSERT_NO_FATAL_FAILURE(
1843 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1844
1845 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
1846
1847 {
1848 SCOPED_TRACE("set layer 1 buffer red");
1849 auto shot = screenshot();
1850 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1851 }
1852
1853 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
1854
1855 {
1856 SCOPED_TRACE("set layer 2 buffer blue");
1857 auto shot = screenshot();
1858 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1859 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
1860 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
1861 }
1862
1863 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
1864 {
1865 SCOPED_TRACE("set layer 1 buffer green");
1866 auto shot = screenshot();
1867 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1868 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1869 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1870 }
1871
1872 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
1873
1874 {
1875 SCOPED_TRACE("set layer 2 buffer white");
1876 auto shot = screenshot();
1877 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
1878 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1879 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1880 }
1881}
1882
1883TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
1884 sp<SurfaceControl> layer;
1885 ASSERT_NO_FATAL_FAILURE(
1886 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1887
1888 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1889 Color::BLUE, Color::WHITE));
1890
1891 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90).apply();
1892
1893 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1894 Color::GREEN, true /* filtered */);
1895}
1896
1897TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
1898 sp<SurfaceControl> layer;
1899 ASSERT_NO_FATAL_FAILURE(
1900 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1901
1902 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1903 Color::BLUE, Color::WHITE));
1904
1905 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H).apply();
1906
1907 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1908 Color::BLUE, true /* filtered */);
1909}
1910
1911TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
1912 sp<SurfaceControl> layer;
1913 ASSERT_NO_FATAL_FAILURE(
1914 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1915
1916 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1917 Color::BLUE, Color::WHITE));
1918
1919 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V).apply();
1920
1921 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1922 Color::GREEN, true /* filtered */);
1923}
1924
1925TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
1926 sp<SurfaceControl> layer;
1927 ASSERT_NO_FATAL_FAILURE(
1928 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1929
1930 Transaction().setTransformToDisplayInverse(layer, false).apply();
1931
1932 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
1933
1934 Transaction().setTransformToDisplayInverse(layer, true).apply();
1935}
1936
1937TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
1938 sp<SurfaceControl> layer;
1939 ASSERT_NO_FATAL_FAILURE(
1940 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1941
1942 sp<GraphicBuffer> buffer =
1943 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1944 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1945 BufferUsage::COMPOSER_OVERLAY,
1946 "test");
1947 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1948
1949 sp<Fence> fence = new Fence(-1);
1950
1951 Transaction()
1952 .setBuffer(layer, buffer)
1953 .setAcquireFence(layer, fence)
1954 .setSize(layer, 32, 32)
1955 .apply();
1956
1957 auto shot = screenshot();
1958 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1959 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1960}
1961
1962TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
1963 sp<SurfaceControl> layer;
1964 ASSERT_NO_FATAL_FAILURE(
1965 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1966
1967 sp<GraphicBuffer> buffer =
1968 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1969 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1970 BufferUsage::COMPOSER_OVERLAY,
1971 "test");
1972 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1973
1974 Transaction()
1975 .setBuffer(layer, buffer)
1976 .setDataspace(layer, ui::Dataspace::UNKNOWN)
1977 .setSize(layer, 32, 32)
1978 .apply();
1979
1980 auto shot = screenshot();
1981 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1982 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1983}
1984
1985TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
1986 sp<SurfaceControl> layer;
1987 ASSERT_NO_FATAL_FAILURE(
1988 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1989
1990 sp<GraphicBuffer> buffer =
1991 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1992 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1993 BufferUsage::COMPOSER_OVERLAY,
1994 "test");
1995 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1996
1997 HdrMetadata hdrMetadata;
1998 hdrMetadata.validTypes = 0;
1999 Transaction()
2000 .setBuffer(layer, buffer)
2001 .setHdrMetadata(layer, hdrMetadata)
2002 .setSize(layer, 32, 32)
2003 .apply();
2004
2005 auto shot = screenshot();
2006 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2007 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2008}
2009
2010TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2011 sp<SurfaceControl> layer;
2012 ASSERT_NO_FATAL_FAILURE(
2013 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2014
2015 sp<GraphicBuffer> buffer =
2016 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2017 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2018 BufferUsage::COMPOSER_OVERLAY,
2019 "test");
2020 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2021
2022 Region region;
2023 region.set(32, 32);
2024 Transaction()
2025 .setBuffer(layer, buffer)
2026 .setSurfaceDamageRegion(layer, region)
2027 .setSize(layer, 32, 32)
2028 .apply();
2029
2030 auto shot = screenshot();
2031 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2032 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2033}
2034
2035TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2036 sp<SurfaceControl> layer;
2037 ASSERT_NO_FATAL_FAILURE(
2038 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2039
2040 sp<GraphicBuffer> buffer =
2041 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2042 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2043 BufferUsage::COMPOSER_OVERLAY,
2044 "test");
2045 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2046
2047 Transaction()
2048 .setBuffer(layer, buffer)
2049 .setApi(layer, NATIVE_WINDOW_API_CPU)
2050 .setSize(layer, 32, 32)
2051 .apply();
2052
2053 auto shot = screenshot();
2054 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2055 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2056}
2057
2058TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2059 sp<SurfaceControl> layer;
2060 ASSERT_NO_FATAL_FAILURE(
2061 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2062
2063 // verify this doesn't cause a crash
2064 Transaction().setSidebandStream(layer, nullptr).apply();
2065}
2066
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002067class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002068protected:
2069 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07002070 LayerTransactionTest::SetUp();
2071 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002072
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002073 sp<IBinder> display(
2074 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07002075 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002076 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07002077
2078 ssize_t displayWidth = info.w;
2079 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002080
2081 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07002082 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
2083 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002084 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002085 ASSERT_TRUE(mBGSurfaceControl->isValid());
2086 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
2087
2088 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07002089 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
2090
Peiyong Lin566a3b42018-01-09 18:22:43 -08002091 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002092 ASSERT_TRUE(mFGSurfaceControl->isValid());
2093
2094 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2095
2096 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07002097 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002098 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002099 ASSERT_TRUE(mSyncSurfaceControl->isValid());
2100
2101 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2102
Robert Carr4cdc58f2017-08-23 14:22:20 -07002103 asTransaction([&](Transaction& t) {
2104 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002105
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002106 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07002107
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002108 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
2109 .setPosition(mFGSurfaceControl, 64, 64)
2110 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002111
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002112 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
2113 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
2114 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002115 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002116 }
2117
2118 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07002119 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002120 mBGSurfaceControl = 0;
2121 mFGSurfaceControl = 0;
2122 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002123 }
2124
2125 void waitForPostedBuffers() {
2126 // Since the sync surface is in synchronous mode (i.e. double buffered)
2127 // posting three buffers to it should ensure that at least two
2128 // SurfaceFlinger::handlePageFlip calls have been made, which should
2129 // guaranteed that a buffer posted to another Surface has been retired.
2130 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2131 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2132 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2133 }
2134
Robert Carr4cdc58f2017-08-23 14:22:20 -07002135 void asTransaction(const std::function<void(Transaction&)>& exec) {
2136 Transaction t;
2137 exec(t);
2138 t.apply(true);
2139 }
2140
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002141 sp<SurfaceControl> mBGSurfaceControl;
2142 sp<SurfaceControl> mFGSurfaceControl;
2143
2144 // This surface is used to ensure that the buffers posted to
2145 // mFGSurfaceControl have been picked up by SurfaceFlinger.
2146 sp<SurfaceControl> mSyncSurfaceControl;
2147};
2148
Robert Carr7f619b22017-11-06 12:56:35 -08002149TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08002150
chaviw0e3479f2018-09-10 16:49:30 -07002151 std::unique_ptr<ScreenCapture> sc;
2152
2153 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08002154 fillSurfaceRGBA8(relative, 10, 10, 10);
2155 waitForPostedBuffers();
2156
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002157 Transaction{}
2158 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08002159 .setPosition(relative, 64, 64)
2160 .apply();
2161
2162 {
2163 // The relative should be on top of the FG control.
2164 ScreenCapture::captureScreen(&sc);
2165 sc->checkPixel(64, 64, 10, 10, 10);
2166 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002167 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002168
2169 {
2170 // Nothing should change at this point.
2171 ScreenCapture::captureScreen(&sc);
2172 sc->checkPixel(64, 64, 10, 10, 10);
2173 }
2174
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002175 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002176
2177 {
2178 // Ensure that the relative was actually hidden, rather than
2179 // being left in the detached but visible state.
2180 ScreenCapture::captureScreen(&sc);
2181 sc->expectFGColor(64, 64);
2182 }
2183}
2184
Robert Carr8d5227b2017-03-16 15:41:03 -07002185class GeometryLatchingTest : public LayerUpdateTest {
2186protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002187 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07002188 SCOPED_TRACE(trace);
2189 ScreenCapture::captureScreen(&sc);
2190 // We find the leading edge of the FG surface.
2191 sc->expectFGColor(127, 127);
2192 sc->expectBGColor(128, 128);
2193 }
Robert Carr7bf247e2017-05-18 14:02:49 -07002194
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002195 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07002196
2197 void unlockFGBuffer() {
2198 sp<Surface> s = mFGSurfaceControl->getSurface();
2199 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
2200 waitForPostedBuffers();
2201 }
2202
Robert Carr8d5227b2017-03-16 15:41:03 -07002203 void completeFGResize() {
2204 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2205 waitForPostedBuffers();
2206 }
2207 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002208 asTransaction([&](Transaction& t) {
2209 t.setSize(mFGSurfaceControl, 64, 64);
2210 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002211 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002212 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002213
2214 EXPECT_INITIAL_STATE("After restoring initial state");
2215 }
chaviw0e3479f2018-09-10 16:49:30 -07002216 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07002217};
2218
Robert Carr8d5227b2017-03-16 15:41:03 -07002219class CropLatchingTest : public GeometryLatchingTest {
2220protected:
2221 void EXPECT_CROPPED_STATE(const char* trace) {
2222 SCOPED_TRACE(trace);
2223 ScreenCapture::captureScreen(&sc);
2224 // The edge should be moved back one pixel by our crop.
2225 sc->expectFGColor(126, 126);
2226 sc->expectBGColor(127, 127);
2227 sc->expectBGColor(128, 128);
2228 }
chaviw59f5c562017-06-28 16:39:06 -07002229
2230 void EXPECT_RESIZE_STATE(const char* trace) {
2231 SCOPED_TRACE(trace);
2232 ScreenCapture::captureScreen(&sc);
2233 // The FG is now resized too 128,128 at 64,64
2234 sc->expectFGColor(64, 64);
2235 sc->expectFGColor(191, 191);
2236 sc->expectBGColor(192, 192);
2237 }
Robert Carr8d5227b2017-03-16 15:41:03 -07002238};
2239
Pablo Ceballos05289c22016-04-14 15:49:55 -07002240TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07002241 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07002242 {
2243 SCOPED_TRACE("before anything");
2244 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002245 sc->expectBGColor(32, 32);
2246 sc->expectFGColor(96, 96);
2247 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002248 }
2249
2250 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07002251 asTransaction([&](Transaction& t) {
2252 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002253 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2254 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002255 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002256
Robert Carr4cdc58f2017-08-23 14:22:20 -07002257 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002258 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002259 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2260 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002261 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002262
2263 {
2264 SCOPED_TRACE("before any trigger");
2265 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002266 sc->expectBGColor(32, 32);
2267 sc->expectFGColor(96, 96);
2268 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002269 }
2270
2271 // should trigger the first deferred transaction, but not the second one
2272 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2273 {
2274 SCOPED_TRACE("after first trigger");
2275 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002276 sc->expectBGColor(32, 32);
2277 sc->checkPixel(96, 96, 162, 63, 96);
2278 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002279 }
2280
2281 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002282 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002283
2284 // trigger the second deferred transaction
2285 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2286 {
2287 SCOPED_TRACE("after second trigger");
2288 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002289 sc->expectBGColor(32, 32);
2290 sc->expectBGColor(96, 96);
2291 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002292 }
2293}
2294
Robert Carre392b552017-09-19 12:16:05 -07002295TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07002296 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07002297
2298 sp<SurfaceControl> childNoBuffer =
chaviw0e3479f2018-09-10 16:49:30 -07002299 mClient->createSurface(String8("Bufferless child"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002300 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2301 sp<SurfaceControl> childBuffer =
chaviw0e3479f2018-09-10 16:49:30 -07002302 mClient->createSurface(String8("Buffered child"), 20, 20,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002303 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07002304 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
2305
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002306 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002307
2308 {
2309 ScreenCapture::captureScreen(&sc);
2310 sc->expectChildColor(73, 73);
2311 sc->expectFGColor(74, 74);
2312 }
2313
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002314 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002315
2316 {
2317 ScreenCapture::captureScreen(&sc);
2318 sc->expectChildColor(73, 73);
2319 sc->expectChildColor(74, 74);
2320 }
2321}
2322
Robert Carr2c5f6d22017-09-26 12:30:35 -07002323TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07002324 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07002325 {
2326 SCOPED_TRACE("before move");
2327 ScreenCapture::captureScreen(&sc);
2328 sc->expectBGColor(0, 12);
2329 sc->expectFGColor(75, 75);
2330 sc->expectBGColor(145, 145);
2331 }
2332
2333 Transaction t1, t2;
2334 t1.setPosition(mFGSurfaceControl, 128, 128);
2335 t2.setPosition(mFGSurfaceControl, 0, 0);
2336 // We expect that the position update from t2 now
2337 // overwrites the position update from t1.
2338 t1.merge(std::move(t2));
2339 t1.apply();
2340
2341 {
2342 ScreenCapture::captureScreen(&sc);
2343 sc->expectFGColor(1, 1);
2344 }
2345}
2346
Robert Carr1f0a16a2016-10-24 16:27:39 -07002347class ChildLayerTest : public LayerUpdateTest {
2348protected:
2349 void SetUp() override {
2350 LayerUpdateTest::SetUp();
chaviw0e3479f2018-09-10 16:49:30 -07002351 mChild = mClient->createSurface(String8("Child surface"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002352 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002353 fillSurfaceRGBA8(mChild, 200, 200, 200);
2354
2355 {
2356 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07002357 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002358 mCapture->expectChildColor(64, 64);
2359 }
2360 }
2361 void TearDown() override {
2362 LayerUpdateTest::TearDown();
2363 mChild = 0;
2364 }
2365
2366 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07002367 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002368};
2369
2370TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002371 asTransaction([&](Transaction& t) {
2372 t.show(mChild);
2373 t.setPosition(mChild, 10, 10);
2374 t.setPosition(mFGSurfaceControl, 64, 64);
2375 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002376
2377 {
chaviw0e3479f2018-09-10 16:49:30 -07002378 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002379 // Top left of foreground must now be visible
2380 mCapture->expectFGColor(64, 64);
2381 // But 10 pixels in we should see the child surface
2382 mCapture->expectChildColor(74, 74);
2383 // And 10 more pixels we should be back to the foreground surface
2384 mCapture->expectFGColor(84, 84);
2385 }
2386
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002387 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002388
2389 {
chaviw0e3479f2018-09-10 16:49:30 -07002390 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002391 // Top left of foreground should now be at 0, 0
2392 mCapture->expectFGColor(0, 0);
2393 // But 10 pixels in we should see the child surface
2394 mCapture->expectChildColor(10, 10);
2395 // And 10 more pixels we should be back to the foreground surface
2396 mCapture->expectFGColor(20, 20);
2397 }
2398}
2399
Robert Carr41b08b52017-06-01 16:11:34 -07002400TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002401 asTransaction([&](Transaction& t) {
2402 t.show(mChild);
2403 t.setPosition(mChild, 0, 0);
2404 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002405 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002406 });
Robert Carr41b08b52017-06-01 16:11:34 -07002407
2408 {
chaviw0e3479f2018-09-10 16:49:30 -07002409 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07002410 mCapture->expectChildColor(0, 0);
2411 mCapture->expectChildColor(4, 4);
2412 mCapture->expectBGColor(5, 5);
2413 }
2414}
2415
Robert Carr1f0a16a2016-10-24 16:27:39 -07002416TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002417 asTransaction([&](Transaction& t) {
2418 t.show(mChild);
2419 t.setPosition(mFGSurfaceControl, 0, 0);
2420 t.setPosition(mChild, 63, 63);
2421 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002422
2423 {
chaviw0e3479f2018-09-10 16:49:30 -07002424 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002425 mCapture->expectFGColor(0, 0);
2426 // Last pixel in foreground should now be the child.
2427 mCapture->expectChildColor(63, 63);
2428 // But the child should be constrained and the next pixel
2429 // must be the background
2430 mCapture->expectBGColor(64, 64);
2431 }
2432}
2433
2434TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002435 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002436
2437 // Find the boundary between the parent and child
2438 {
chaviw0e3479f2018-09-10 16:49:30 -07002439 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002440 mCapture->expectChildColor(9, 9);
2441 mCapture->expectFGColor(10, 10);
2442 }
2443
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002444 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002445
2446 // The boundary should be twice as far from the origin now.
2447 // The pixels from the last test should all be child now
2448 {
chaviw0e3479f2018-09-10 16:49:30 -07002449 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002450 mCapture->expectChildColor(9, 9);
2451 mCapture->expectChildColor(10, 10);
2452 mCapture->expectChildColor(19, 19);
2453 mCapture->expectFGColor(20, 20);
2454 }
2455}
Robert Carr9524cb32017-02-13 11:32:32 -08002456
Robert Carr6452f122017-03-21 10:41:29 -07002457TEST_F(ChildLayerTest, ChildLayerAlpha) {
2458 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2459 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2460 fillSurfaceRGBA8(mChild, 0, 254, 0);
2461 waitForPostedBuffers();
2462
Robert Carr4cdc58f2017-08-23 14:22:20 -07002463 asTransaction([&](Transaction& t) {
2464 t.show(mChild);
2465 t.setPosition(mChild, 0, 0);
2466 t.setPosition(mFGSurfaceControl, 0, 0);
2467 });
Robert Carr6452f122017-03-21 10:41:29 -07002468
2469 {
chaviw0e3479f2018-09-10 16:49:30 -07002470 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07002471 // Unblended child color
2472 mCapture->checkPixel(0, 0, 0, 254, 0);
2473 }
2474
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002475 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002476
2477 {
chaviw0e3479f2018-09-10 16:49:30 -07002478 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07002479 // Child and BG blended.
2480 mCapture->checkPixel(0, 0, 127, 127, 0);
2481 }
2482
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002483 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002484
2485 {
chaviw0e3479f2018-09-10 16:49:30 -07002486 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07002487 // Child and BG blended.
2488 mCapture->checkPixel(0, 0, 95, 64, 95);
2489 }
2490}
2491
Robert Carr9524cb32017-02-13 11:32:32 -08002492TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002493 asTransaction([&](Transaction& t) {
2494 t.show(mChild);
2495 t.setPosition(mChild, 10, 10);
2496 t.setPosition(mFGSurfaceControl, 64, 64);
2497 });
Robert Carr9524cb32017-02-13 11:32:32 -08002498
2499 {
chaviw0e3479f2018-09-10 16:49:30 -07002500 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002501 // Top left of foreground must now be visible
2502 mCapture->expectFGColor(64, 64);
2503 // But 10 pixels in we should see the child surface
2504 mCapture->expectChildColor(74, 74);
2505 // And 10 more pixels we should be back to the foreground surface
2506 mCapture->expectFGColor(84, 84);
2507 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002508
2509 asTransaction([&](Transaction& t) {
2510 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2511 });
2512
Robert Carr9524cb32017-02-13 11:32:32 -08002513 {
chaviw0e3479f2018-09-10 16:49:30 -07002514 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002515 mCapture->expectFGColor(64, 64);
2516 // In reparenting we should have exposed the entire foreground surface.
2517 mCapture->expectFGColor(74, 74);
2518 // And the child layer should now begin at 10, 10 (since the BG
2519 // layer is at (0, 0)).
2520 mCapture->expectBGColor(9, 9);
2521 mCapture->expectChildColor(10, 10);
2522 }
2523}
2524
chaviw161410b02017-07-27 10:46:08 -07002525TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002526 asTransaction([&](Transaction& t) {
2527 t.show(mChild);
2528 t.setPosition(mChild, 10, 10);
2529 t.setPosition(mFGSurfaceControl, 64, 64);
2530 });
Robert Carr9524cb32017-02-13 11:32:32 -08002531
2532 {
chaviw0e3479f2018-09-10 16:49:30 -07002533 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002534 // Top left of foreground must now be visible
2535 mCapture->expectFGColor(64, 64);
2536 // But 10 pixels in we should see the child surface
2537 mCapture->expectChildColor(74, 74);
2538 // And 10 more pixels we should be back to the foreground surface
2539 mCapture->expectFGColor(84, 84);
2540 }
2541
chaviw0e3479f2018-09-10 16:49:30 -07002542
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002543 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002544
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002545 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002546
chaviw161410b02017-07-27 10:46:08 -07002547 // Since the child has the same client as the parent, it will not get
2548 // detached and will be hidden.
2549 {
chaviw0e3479f2018-09-10 16:49:30 -07002550 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07002551 mCapture->expectFGColor(64, 64);
2552 mCapture->expectFGColor(74, 74);
2553 mCapture->expectFGColor(84, 84);
2554 }
2555}
2556
2557TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2558 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002559 sp<SurfaceControl> mChildNewClient =
2560 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2561 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002562
Peiyong Lin566a3b42018-01-09 18:22:43 -08002563 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07002564 ASSERT_TRUE(mChildNewClient->isValid());
2565
2566 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2567
Robert Carr4cdc58f2017-08-23 14:22:20 -07002568 asTransaction([&](Transaction& t) {
2569 t.hide(mChild);
2570 t.show(mChildNewClient);
2571 t.setPosition(mChildNewClient, 10, 10);
2572 t.setPosition(mFGSurfaceControl, 64, 64);
2573 });
chaviw161410b02017-07-27 10:46:08 -07002574
2575 {
chaviw0e3479f2018-09-10 16:49:30 -07002576 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07002577 // Top left of foreground must now be visible
2578 mCapture->expectFGColor(64, 64);
2579 // But 10 pixels in we should see the child surface
2580 mCapture->expectChildColor(74, 74);
2581 // And 10 more pixels we should be back to the foreground surface
2582 mCapture->expectFGColor(84, 84);
2583 }
2584
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002585 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002586
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002587 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002588
Robert Carr9524cb32017-02-13 11:32:32 -08002589 // Nothing should have changed.
2590 {
chaviw0e3479f2018-09-10 16:49:30 -07002591 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002592 mCapture->expectFGColor(64, 64);
2593 mCapture->expectChildColor(74, 74);
2594 mCapture->expectFGColor(84, 84);
2595 }
2596}
2597
Robert Carr9b429f42017-04-17 14:56:57 -07002598TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002599 asTransaction([&](Transaction& t) {
2600 t.show(mChild);
2601 t.setPosition(mChild, 0, 0);
2602 t.setPosition(mFGSurfaceControl, 0, 0);
2603 });
Robert Carr9b429f42017-04-17 14:56:57 -07002604
2605 {
chaviw0e3479f2018-09-10 16:49:30 -07002606 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07002607 // We've positioned the child in the top left.
2608 mCapture->expectChildColor(0, 0);
2609 // But it's only 10x10.
2610 mCapture->expectFGColor(10, 10);
2611 }
2612
Robert Carr4cdc58f2017-08-23 14:22:20 -07002613 asTransaction([&](Transaction& t) {
2614 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2615 // We cause scaling by 2.
2616 t.setSize(mFGSurfaceControl, 128, 128);
2617 });
Robert Carr9b429f42017-04-17 14:56:57 -07002618
2619 {
chaviw0e3479f2018-09-10 16:49:30 -07002620 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07002621 // We've positioned the child in the top left.
2622 mCapture->expectChildColor(0, 0);
2623 mCapture->expectChildColor(10, 10);
2624 mCapture->expectChildColor(19, 19);
2625 // And now it should be scaled all the way to 20x20
2626 mCapture->expectFGColor(20, 20);
2627 }
2628}
2629
Robert Carr1725eee2017-04-26 18:32:15 -07002630// Regression test for b/37673612
2631TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002632 asTransaction([&](Transaction& t) {
2633 t.show(mChild);
2634 t.setPosition(mChild, 0, 0);
2635 t.setPosition(mFGSurfaceControl, 0, 0);
2636 });
Robert Carr1725eee2017-04-26 18:32:15 -07002637
2638 {
chaviw0e3479f2018-09-10 16:49:30 -07002639 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07002640 // We've positioned the child in the top left.
2641 mCapture->expectChildColor(0, 0);
2642 // But it's only 10x10.
2643 mCapture->expectFGColor(10, 10);
2644 }
Robert Carr1725eee2017-04-26 18:32:15 -07002645 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2646 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002647 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002648 sp<Surface> s = mFGSurfaceControl->getSurface();
2649 auto anw = static_cast<ANativeWindow*>(s.get());
2650 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2651 native_window_set_buffers_dimensions(anw, 64, 128);
2652 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2653 waitForPostedBuffers();
2654
2655 {
2656 // The child should still be in the same place and not have any strange scaling as in
2657 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07002658 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07002659 mCapture->expectChildColor(0, 0);
2660 mCapture->expectFGColor(10, 10);
2661 }
2662}
2663
Dan Stoza412903f2017-04-27 13:42:17 -07002664TEST_F(ChildLayerTest, Bug36858924) {
2665 // Destroy the child layer
2666 mChild.clear();
2667
2668 // Now recreate it as hidden
chaviw0e3479f2018-09-10 16:49:30 -07002669 mChild = mClient->createSurface(String8("Child surface"), 10, 10,
Dan Stoza412903f2017-04-27 13:42:17 -07002670 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2671 mFGSurfaceControl.get());
2672
2673 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002674 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002675 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
2676 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002677 t.show(mChild);
2678 });
Dan Stoza412903f2017-04-27 13:42:17 -07002679
2680 // Render the foreground surface a few times
2681 //
2682 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2683 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2684 // never acquire/release the first buffer
2685 ALOGI("Filling 1");
2686 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2687 ALOGI("Filling 2");
2688 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2689 ALOGI("Filling 3");
2690 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2691 ALOGI("Filling 4");
2692 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2693}
2694
chaviwf1961f72017-09-18 16:41:07 -07002695TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002696 asTransaction([&](Transaction& t) {
2697 t.show(mChild);
2698 t.setPosition(mChild, 10, 10);
2699 t.setPosition(mFGSurfaceControl, 64, 64);
2700 });
chaviw06178942017-07-27 10:25:59 -07002701
2702 {
chaviw0e3479f2018-09-10 16:49:30 -07002703 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07002704 // Top left of foreground must now be visible
2705 mCapture->expectFGColor(64, 64);
2706 // But 10 pixels in we should see the child surface
2707 mCapture->expectChildColor(74, 74);
2708 // And 10 more pixels we should be back to the foreground surface
2709 mCapture->expectFGColor(84, 84);
2710 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002711
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002712 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002713
chaviw06178942017-07-27 10:25:59 -07002714 {
chaviw0e3479f2018-09-10 16:49:30 -07002715 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07002716 mCapture->expectFGColor(64, 64);
2717 // In reparenting we should have exposed the entire foreground surface.
2718 mCapture->expectFGColor(74, 74);
2719 // And the child layer should now begin at 10, 10 (since the BG
2720 // layer is at (0, 0)).
2721 mCapture->expectBGColor(9, 9);
2722 mCapture->expectChildColor(10, 10);
2723 }
2724}
2725
chaviwf1961f72017-09-18 16:41:07 -07002726TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002727 asTransaction([&](Transaction& t) {
2728 t.show(mChild);
2729 t.setPosition(mChild, 10, 10);
2730 t.setPosition(mFGSurfaceControl, 64, 64);
2731 });
chaviwf1961f72017-09-18 16:41:07 -07002732
2733 {
chaviw0e3479f2018-09-10 16:49:30 -07002734 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002735 // Top left of foreground must now be visible
2736 mCapture->expectFGColor(64, 64);
2737 // But 10 pixels in we should see the child surface
2738 mCapture->expectChildColor(74, 74);
2739 // And 10 more pixels we should be back to the foreground surface
2740 mCapture->expectFGColor(84, 84);
2741 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002742 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002743 {
chaviw0e3479f2018-09-10 16:49:30 -07002744 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002745 // Nothing should have changed.
2746 mCapture->expectFGColor(64, 64);
2747 mCapture->expectChildColor(74, 74);
2748 mCapture->expectFGColor(84, 84);
2749 }
2750}
2751
2752TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07002753 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002754 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07002755 ASSERT_TRUE(newSurface->isValid());
2756
2757 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002758 asTransaction([&](Transaction& t) {
2759 t.hide(mChild);
2760 t.show(newSurface);
2761 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002762 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002763 t.setPosition(mFGSurfaceControl, 64, 64);
2764 });
chaviwf1961f72017-09-18 16:41:07 -07002765
2766 {
chaviw0e3479f2018-09-10 16:49:30 -07002767 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002768 // Top left of foreground must now be visible
2769 mCapture->expectFGColor(64, 64);
2770 // At 10, 10 we should see the new surface
2771 mCapture->checkPixel(10, 10, 63, 195, 63);
2772 }
2773
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002774 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002775
2776 {
chaviw0e3479f2018-09-10 16:49:30 -07002777 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002778 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2779 // mFGSurface, putting it at 74, 74.
2780 mCapture->expectFGColor(64, 64);
2781 mCapture->checkPixel(74, 74, 63, 195, 63);
2782 mCapture->expectFGColor(84, 84);
2783 }
2784}
2785
chaviwc9674332017-08-28 12:32:18 -07002786TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002787 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07002788 mClient->createSurface(String8("Grandchild surface"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002789 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002790 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2791
2792 {
chaviw0e3479f2018-09-10 16:49:30 -07002793 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07002794 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2795 // which begins at 64, 64
2796 mCapture->checkPixel(64, 64, 50, 50, 50);
2797 }
2798}
2799
Robert Carr503c7042017-09-27 15:06:08 -07002800TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07002801 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002802 fillSurfaceRGBA8(relative, 255, 255, 255);
2803
2804 Transaction t;
2805 t.setLayer(relative, INT32_MAX)
2806 .setRelativeLayer(mChild, relative->getHandle(), 1)
2807 .setPosition(mFGSurfaceControl, 0, 0)
2808 .apply(true);
2809
2810 // We expect that the child should have been elevated above our
2811 // INT_MAX layer even though it's not a child of it.
2812 {
chaviw0e3479f2018-09-10 16:49:30 -07002813 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07002814 mCapture->expectChildColor(0, 0);
2815 mCapture->expectChildColor(9, 9);
2816 mCapture->checkPixel(10, 10, 255, 255, 255);
2817 }
2818}
2819
chaviwa76b2712017-09-20 12:02:26 -07002820class ScreenCaptureTest : public LayerUpdateTest {
2821protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002822 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07002823};
2824
2825TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2826 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002827 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002828 mCapture->expectBGColor(0, 0);
2829 // Doesn't capture FG layer which is at 64, 64
2830 mCapture->expectBGColor(64, 64);
2831}
2832
2833TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2834 auto fgHandle = mFGSurfaceControl->getHandle();
2835
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002836 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07002837 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002838 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002839 fillSurfaceRGBA8(child, 200, 200, 200);
2840
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002841 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002842
2843 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002844 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002845 mCapture->expectFGColor(10, 10);
2846 mCapture->expectChildColor(0, 0);
2847}
2848
Robert Carr578038f2018-03-09 12:25:24 -08002849TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
2850 auto fgHandle = mFGSurfaceControl->getHandle();
2851
2852 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07002853 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Robert Carr578038f2018-03-09 12:25:24 -08002854 0, mFGSurfaceControl.get());
2855 fillSurfaceRGBA8(child, 200, 200, 200);
2856
2857 SurfaceComposerClient::Transaction().show(child).apply(true);
2858
2859 // Captures mFGSurfaceControl's child
2860 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2861 mCapture->checkPixel(10, 10, 0, 0, 0);
2862 mCapture->expectChildColor(0, 0);
2863}
2864
chaviw50da5042018-04-09 13:49:37 -07002865TEST_F(ScreenCaptureTest, CaptureTransparent) {
2866 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07002867 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw50da5042018-04-09 13:49:37 -07002868 0, mFGSurfaceControl.get());
2869
2870 fillSurfaceRGBA8(child, 200, 200, 200);
2871
2872 SurfaceComposerClient::Transaction().show(child).apply(true);
2873
2874 auto childHandle = child->getHandle();
2875
2876 // Captures child
2877 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
2878 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
2879 // Area outside of child's bounds is transparent.
2880 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
2881}
2882
chaviw4b129c22018-04-09 16:19:43 -07002883TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
2884 auto fgHandle = mFGSurfaceControl->getHandle();
2885
2886 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07002887 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw4b129c22018-04-09 16:19:43 -07002888 0, mFGSurfaceControl.get());
chaviw0e3479f2018-09-10 16:49:30 -07002889 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07002890 fillSurfaceRGBA8(child, 200, 200, 200);
2891 fillSurfaceRGBA8(relative, 100, 100, 100);
2892
2893 SurfaceComposerClient::Transaction()
2894 .show(child)
2895 // Set relative layer above fg layer so should be shown above when computing all layers.
2896 .setRelativeLayer(relative, fgHandle, 1)
2897 .show(relative)
2898 .apply(true);
2899
2900 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
2901 ScreenCapture::captureLayers(&mCapture, fgHandle);
2902 mCapture->expectFGColor(10, 10);
2903 mCapture->expectChildColor(0, 0);
2904}
2905
2906TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
2907 auto fgHandle = mFGSurfaceControl->getHandle();
2908
2909 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07002910 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw4b129c22018-04-09 16:19:43 -07002911 0, mFGSurfaceControl.get());
2912 sp<SurfaceControl> relative =
chaviw0e3479f2018-09-10 16:49:30 -07002913 mClient->createSurface(String8("Relative surface"), 10, 10,
chaviw4b129c22018-04-09 16:19:43 -07002914 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2915 fillSurfaceRGBA8(child, 200, 200, 200);
2916 fillSurfaceRGBA8(relative, 100, 100, 100);
2917
2918 SurfaceComposerClient::Transaction()
2919 .show(child)
2920 // Set relative layer below fg layer but relative to child layer so it should be shown
2921 // above child layer.
2922 .setLayer(relative, -1)
2923 .setRelativeLayer(relative, child->getHandle(), 1)
2924 .show(relative)
2925 .apply(true);
2926
2927 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
2928 // relative value should be taken into account, placing it above child layer.
2929 ScreenCapture::captureLayers(&mCapture, fgHandle);
2930 mCapture->expectFGColor(10, 10);
2931 // Relative layer is showing on top of child layer
2932 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
2933}
Robert Carr578038f2018-03-09 12:25:24 -08002934
2935// In the following tests we verify successful skipping of a parent layer,
2936// so we use the same verification logic and only change how we mutate
2937// the parent layer to verify that various properties are ignored.
2938class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
2939public:
2940 void SetUp() override {
2941 LayerUpdateTest::SetUp();
2942
2943 mChild =
chaviw0e3479f2018-09-10 16:49:30 -07002944 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Robert Carr578038f2018-03-09 12:25:24 -08002945 0, mFGSurfaceControl.get());
2946 fillSurfaceRGBA8(mChild, 200, 200, 200);
2947
2948 SurfaceComposerClient::Transaction().show(mChild).apply(true);
2949 }
2950
2951 void verify() {
2952 auto fgHandle = mFGSurfaceControl->getHandle();
2953 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2954 mCapture->checkPixel(10, 10, 0, 0, 0);
2955 mCapture->expectChildColor(0, 0);
2956 }
2957
2958 std::unique_ptr<ScreenCapture> mCapture;
2959 sp<SurfaceControl> mChild;
2960};
2961
2962TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
2963
2964 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2965
2966 // Even though the parent is hidden we should still capture the child.
2967 verify();
2968}
2969
2970TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002971 SurfaceComposerClient::Transaction()
2972 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
2973 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08002974
2975 // Even though the parent is cropped out we should still capture the child.
2976 verify();
2977}
2978
2979TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
2980
2981 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
2982
2983 // We should not inherit the parent scaling.
2984 verify();
2985}
2986
Robert Carr15eae092018-03-23 13:43:53 -07002987TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
2988 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2989
2990 // Even though the parent is hidden we should still capture the child.
2991 verify();
2992
2993 // Verify everything was properly hidden when rendering the full-screen.
2994 screenshot()->expectBGColor(0,0);
2995}
2996
2997
chaviwa76b2712017-09-20 12:02:26 -07002998TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2999 auto fgHandle = mFGSurfaceControl->getHandle();
3000
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003001 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003002 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003003 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003004 fillSurfaceRGBA8(child, 200, 200, 200);
3005
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003006 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07003007 mClient->createSurface(String8("Grandchild surface"), 5, 5,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003008 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07003009
3010 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3011 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003012 .show(child)
3013 .setPosition(grandchild, 5, 5)
3014 .show(grandchild)
3015 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003016
3017 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003018 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003019 mCapture->expectFGColor(10, 10);
3020 mCapture->expectChildColor(0, 0);
3021 mCapture->checkPixel(5, 5, 50, 50, 50);
3022}
3023
3024TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003025 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003026 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003027 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003028 fillSurfaceRGBA8(child, 200, 200, 200);
3029 auto childHandle = child->getHandle();
3030
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003031 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003032
3033 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003034 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07003035 mCapture->expectChildColor(0, 0);
3036 mCapture->expectChildColor(9, 9);
3037}
3038
3039TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003040 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003041 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003042 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003043 fillSurfaceRGBA8(child, 200, 200, 200);
3044 auto childHandle = child->getHandle();
3045
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003046 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07003047 mClient->createSurface(String8("Grandchild surface"), 5, 5,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003048 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07003049 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3050
3051 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003052 .show(child)
3053 .setPosition(grandchild, 5, 5)
3054 .show(grandchild)
3055 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003056
3057 auto grandchildHandle = grandchild->getHandle();
3058
3059 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003060 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07003061 mCapture->checkPixel(0, 0, 50, 50, 50);
3062 mCapture->checkPixel(4, 4, 50, 50, 50);
3063}
3064
chaviw7206d492017-11-10 16:16:12 -08003065TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07003066 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003067 sp<SurfaceControl> blueLayer =
chaviw0e3479f2018-09-10 16:49:30 -07003068 mClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003069 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08003070
Marissa Wall61c58622018-07-18 10:12:20 -07003071 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
3072 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08003073
3074 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003075 .setLayer(redLayer, INT32_MAX - 1)
3076 .show(redLayer)
3077 .show(blueLayer)
3078 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08003079
3080 auto redLayerHandle = redLayer->getHandle();
3081
3082 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003083 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
3084 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
3085 // red area below the blue area
3086 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
3087 // red area to the right of the blue area
3088 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003089
3090 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003091 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08003092 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
3093 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003094 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08003095 mCapture->checkPixel(30, 30, 0, 0, 0);
3096}
3097
3098TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07003099 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003100 sp<SurfaceControl> blueLayer =
chaviw0e3479f2018-09-10 16:49:30 -07003101 mClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003102 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08003103
Marissa Wall61c58622018-07-18 10:12:20 -07003104 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
3105 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08003106
3107 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003108 .setLayer(redLayer, INT32_MAX - 1)
3109 .show(redLayer)
3110 .show(blueLayer)
3111 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08003112
3113 auto redLayerHandle = redLayer->getHandle();
3114
3115 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003116 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
3117 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
3118 // red area below the blue area
3119 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
3120 // red area to the right of the blue area
3121 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003122
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003123 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08003124 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003125 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
3126 // red area below the blue area
3127 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
3128 // red area to the right of the blue area
3129 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003130 mCapture->checkPixel(30, 30, 0, 0, 0);
3131}
3132
3133TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07003134 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08003135
Marissa Wall61c58622018-07-18 10:12:20 -07003136 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08003137
3138 auto redLayerHandle = redLayer->getHandle();
chaviw0e3479f2018-09-10 16:49:30 -07003139 mClient->destroySurface(redLayerHandle);
chaviw7206d492017-11-10 16:16:12 -08003140 SurfaceComposerClient::Transaction().apply(true);
3141
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003142 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08003143
3144 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003145 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
3146 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08003147}
3148
chaviw8e3fe5d2018-02-22 10:55:42 -08003149
3150class DereferenceSurfaceControlTest : public LayerTransactionTest {
3151protected:
3152 void SetUp() override {
3153 LayerTransactionTest::SetUp();
3154 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07003155 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08003156 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07003157 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08003158 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
3159 {
3160 SCOPED_TRACE("before anything");
3161 auto shot = screenshot();
3162 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
3163 }
3164 }
3165 void TearDown() override {
3166 LayerTransactionTest::TearDown();
3167 bgLayer = 0;
3168 fgLayer = 0;
3169 }
3170
3171 sp<SurfaceControl> bgLayer;
3172 sp<SurfaceControl> fgLayer;
3173};
3174
3175TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
3176 fgLayer = nullptr;
3177 {
3178 SCOPED_TRACE("after setting null");
3179 auto shot = screenshot();
3180 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
3181 }
3182}
3183
3184TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
3185 auto transaction = Transaction().show(fgLayer);
3186 fgLayer = nullptr;
3187 {
3188 SCOPED_TRACE("after setting null");
3189 auto shot = screenshot();
3190 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
3191 }
3192}
3193
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003194} // namespace android