blob: 70d9dab8a1b139dd1981a68086fcf01f0209453b [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu718daf82017-10-20 11:57:17 -070017#include <algorithm>
Marissa Wallfda30bb2018-10-12 11:34:28 -070018#include <chrono>
19#include <cinttypes>
Chia-I Wu718daf82017-10-20 11:57:17 -070020#include <functional>
21#include <limits>
22#include <ostream>
Marissa Wallfda30bb2018-10-12 11:34:28 -070023#include <thread>
Chia-I Wu718daf82017-10-20 11:57:17 -070024
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070025#include <gtest/gtest.h>
26
Michael Lentine5a16a622015-05-21 13:48:24 -070027#include <android/native_window.h>
28
Alec Mouri80863a62019-01-17 15:19:35 -080029#include <binder/ProcessState.h>
30#include <gui/BufferItemConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080031#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070032#include <gui/LayerState.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080033#include <gui/Surface.h>
34#include <gui/SurfaceComposerClient.h>
35#include <private/gui/ComposerService.h>
36
Ady Abraham2a6ab2a2018-10-26 14:25:30 -070037#include <ui/ColorSpace.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070038#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070039#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070040#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070041
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070042#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070043#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070044
Marissa Wall713b63f2018-10-17 15:42:43 -070045#include "BufferGenerator.h"
46
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070047namespace android {
48
Chia-I Wu718daf82017-10-20 11:57:17 -070049namespace {
50
51struct Color {
52 uint8_t r;
53 uint8_t g;
54 uint8_t b;
55 uint8_t a;
56
57 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070058 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070059 static const Color BLUE;
Chia-I Wu93853fe2017-11-02 08:30:27 -070060 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070061 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070062 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070063};
64
65const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070066const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070067const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070068const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070069const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070070const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070071
Marissa Wall61c58622018-07-18 10:12:20 -070072using android::hardware::graphics::common::V1_1::BufferUsage;
Marissa Wallfda30bb2018-10-12 11:34:28 -070073using namespace std::chrono_literals;
Marissa Wall61c58622018-07-18 10:12:20 -070074
Chia-I Wu718daf82017-10-20 11:57:17 -070075std::ostream& operator<<(std::ostream& os, const Color& color) {
76 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
77 return os;
78}
79
80// Fill a region with the specified color.
Marissa Wall61c58622018-07-18 10:12:20 -070081void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
82 const Color& color) {
83 Rect r(0, 0, buffer.width, buffer.height);
84 if (!r.intersect(rect, &r)) {
85 return;
Chia-I Wu718daf82017-10-20 11:57:17 -070086 }
87
Marissa Wall61c58622018-07-18 10:12:20 -070088 int32_t width = r.right - r.left;
89 int32_t height = r.bottom - r.top;
90
91 for (int32_t row = 0; row < height; row++) {
92 uint8_t* dst =
93 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
94 for (int32_t column = 0; column < width; column++) {
Chia-I Wu718daf82017-10-20 11:57:17 -070095 dst[0] = color.r;
96 dst[1] = color.g;
97 dst[2] = color.b;
98 dst[3] = color.a;
99 dst += 4;
100 }
101 }
102}
103
Marissa Wall61c58622018-07-18 10:12:20 -0700104// Fill a region with the specified color.
105void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
106 Rect r(0, 0, buffer->width, buffer->height);
107 if (!r.intersect(rect, &r)) {
108 return;
109 }
110
111 int32_t width = r.right - r.left;
112 int32_t height = r.bottom - r.top;
113
114 uint8_t* pixels;
115 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
116 reinterpret_cast<void**>(&pixels));
117
118 for (int32_t row = 0; row < height; row++) {
119 uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
120 for (int32_t column = 0; column < width; column++) {
121 dst[0] = color.r;
122 dst[1] = color.g;
123 dst[2] = color.b;
124 dst[3] = color.a;
125 dst += 4;
126 }
127 }
128 buffer->unlock();
129}
130
Chia-I Wu718daf82017-10-20 11:57:17 -0700131// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000132void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700133 const Color& color, uint8_t tolerance) {
134 int32_t x = rect.left;
135 int32_t y = rect.top;
136 int32_t width = rect.right - rect.left;
137 int32_t height = rect.bottom - rect.top;
138
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000139 int32_t bufferWidth = int32_t(outBuffer->getWidth());
140 int32_t bufferHeight = int32_t(outBuffer->getHeight());
141 if (x + width > bufferWidth) {
142 x = std::min(x, bufferWidth);
143 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700144 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000145 if (y + height > bufferHeight) {
146 y = std::min(y, bufferHeight);
147 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700148 }
149
150 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
151 uint8_t tmp = a >= b ? a - b : b - a;
152 return tmp <= tolerance;
153 };
154 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000155 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700156 for (int32_t i = 0; i < width; i++) {
157 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
158 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
159 << "pixel @ (" << x + i << ", " << y + j << "): "
160 << "expected (" << color << "), "
161 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
162 src += 4;
163 }
164 }
165}
166
167} // anonymous namespace
168
Robert Carr4cdc58f2017-08-23 14:22:20 -0700169using Transaction = SurfaceComposerClient::Transaction;
170
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700171// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700172static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
173 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800174 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700175 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800176 ASSERT_TRUE(s != nullptr);
177 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800178 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700179 for (int y = 0; y < outBuffer.height; y++) {
180 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700181 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700182 pixel[0] = r;
183 pixel[1] = g;
184 pixel[2] = b;
185 pixel[3] = 255;
186 }
187 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700188 if (unlock) {
189 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
190 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700191}
192
193// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
194// individual pixel values for testing purposes.
195class ScreenCapture : public RefBase {
196public:
chaviw0e3479f2018-09-10 16:49:30 -0700197 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700198 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700199 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700200 SurfaceComposerClient::Transaction().apply(true);
201
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000202 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700203 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -0700204 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, false));
205 *sc = std::make_unique<ScreenCapture>(outBuffer);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000206 }
207
208 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
209 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
210 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
211 SurfaceComposerClient::Transaction().apply(true);
212
213 sp<GraphicBuffer> outBuffer;
214 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
215 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700216 }
217
Robert Carr578038f2018-03-09 12:25:24 -0800218 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
219 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
220 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
221 SurfaceComposerClient::Transaction().apply(true);
222
223 sp<GraphicBuffer> outBuffer;
224 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
225 *sc = std::make_unique<ScreenCapture>(outBuffer);
226 }
227
Chia-I Wu718daf82017-10-20 11:57:17 -0700228 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000229 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
230 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700231 }
232
233 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000234 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700235 const bool leftBorder = rect.left > 0;
236 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000237 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
238 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700239
240 if (topBorder) {
241 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
242 if (leftBorder) {
243 top.left -= 1;
244 }
245 if (rightBorder) {
246 top.right += 1;
247 }
248 expectColor(top, color, tolerance);
249 }
250 if (leftBorder) {
251 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
252 expectColor(left, color, tolerance);
253 }
254 if (rightBorder) {
255 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
256 expectColor(right, color, tolerance);
257 }
258 if (bottomBorder) {
259 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
260 if (leftBorder) {
261 bottom.left -= 1;
262 }
263 if (rightBorder) {
264 bottom.right += 1;
265 }
266 expectColor(bottom, color, tolerance);
267 }
268 }
269
Chia-I Wu93853fe2017-11-02 08:30:27 -0700270 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
271 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
272 uint8_t tolerance = 0) {
273 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
274
275 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
276 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
277 // avoid checking borders due to unspecified filtering behavior
278 const int32_t offsetX = filtered ? 2 : 0;
279 const int32_t offsetY = filtered ? 2 : 0;
280 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
281 tolerance);
282 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
283 tolerance);
284 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
285 tolerance);
286 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
287 bottomRight, tolerance);
288 }
289
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700290 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000291 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
292 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700293 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
294 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700295 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
296 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700297 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700298 }
299 }
300
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700301 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700302
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700303 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700304
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700305 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700306
Chih-Hung Hsieh22749042018-12-20 15:50:39 -0800307 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000308 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700309 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700310
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000311 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700312
313private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000314 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800315 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700316};
317
Chia-I Wu718daf82017-10-20 11:57:17 -0700318class LayerTransactionTest : public ::testing::Test {
319protected:
320 void SetUp() override {
321 mClient = new SurfaceComposerClient;
322 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
323
324 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700325
326 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
327 sp<IBinder> binder = sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
Ady Abraham37965d42018-11-01 13:43:32 -0700328 ASSERT_NO_FATAL_FAILURE(sf->getColorManagement(&mColorManagementUsed));
Chia-I Wu718daf82017-10-20 11:57:17 -0700329 }
330
chaviw0e3479f2018-09-10 16:49:30 -0700331 virtual void TearDown() {
332 mBlackBgSurface = 0;
333 mClient->dispose();
334 mClient = 0;
335 }
336
Marissa Wallfda30bb2018-10-12 11:34:28 -0700337 virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
338 const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -0800339 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
340 auto layer =
341 createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags, parent);
Chia-I Wu718daf82017-10-20 11:57:17 -0700342
Vishnu Nair60356342018-11-13 13:00:45 -0800343 Transaction t;
344 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
Vishnu Nair60356342018-11-13 13:00:45 -0800345
346 status_t error = t.apply();
Chia-I Wu718daf82017-10-20 11:57:17 -0700347 if (error != NO_ERROR) {
348 ADD_FAILURE() << "failed to initialize SurfaceControl";
349 layer.clear();
350 }
351
352 return layer;
353 }
354
Vishnu Nair88a11f22018-11-28 18:30:57 -0800355 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
356 const char* name, uint32_t width, uint32_t height,
357 PixelFormat format, uint32_t flags,
358 SurfaceControl* parent = nullptr) {
359 auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
360 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
361 return layer;
362 }
363
Marissa Wallfda30bb2018-10-12 11:34:28 -0700364 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -0800365 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
366 return createLayer(mClient, name, width, height, flags, parent);
Marissa Wallfda30bb2018-10-12 11:34:28 -0700367 }
368
Marissa Wall61c58622018-07-18 10:12:20 -0700369 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700370 // wait for previous transactions (such as setSize) to complete
371 Transaction().apply(true);
372
373 ANativeWindow_Buffer buffer = {};
374 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
375
376 return buffer;
377 }
378
Marissa Wall61c58622018-07-18 10:12:20 -0700379 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700380 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
381
382 // wait for the newly posted buffer to be latched
383 waitForLayerBuffers();
384 }
385
Marissa Wall61c58622018-07-18 10:12:20 -0700386 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
387 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700388 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700389 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
390 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
391 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700392 }
393
Marissa Wall61c58622018-07-18 10:12:20 -0700394 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
395 int32_t bufferWidth, int32_t bufferHeight) {
396 sp<GraphicBuffer> buffer =
397 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
398 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
399 BufferUsage::COMPOSER_OVERLAY,
400 "test");
401 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
Marissa Wall861616d2018-10-22 12:52:23 -0700402 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -0700403 }
404
405 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
406 int32_t bufferWidth, int32_t bufferHeight) {
407 switch (mLayerType) {
408 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
409 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
410 break;
411 case ISurfaceComposerClient::eFXSurfaceBufferState:
412 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
413 break;
414 default:
415 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
416 }
417 }
418
419 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
420 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700421 const Color& topRight, const Color& bottomLeft,
422 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700423 switch (mLayerType) {
424 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
425 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
426 bottomLeft, bottomRight);
427 break;
428 case ISurfaceComposerClient::eFXSurfaceBufferState:
429 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
430 bottomLeft, bottomRight);
431 break;
432 default:
433 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
434 }
435 }
436
437 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
438 int32_t bufferHeight, const Color& topLeft,
439 const Color& topRight, const Color& bottomLeft,
440 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700441 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700442 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
443 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700444
Marissa Wall61c58622018-07-18 10:12:20 -0700445 const int32_t halfW = bufferWidth / 2;
446 const int32_t halfH = bufferHeight / 2;
447 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
448 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
449 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
450 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
451 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700452
Marissa Wall61c58622018-07-18 10:12:20 -0700453 postBufferQueueLayerBuffer(layer);
454 }
455
456 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
457 int32_t bufferHeight, const Color& topLeft,
458 const Color& topRight, const Color& bottomLeft,
459 const Color& bottomRight) {
460 sp<GraphicBuffer> buffer =
461 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
462 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
463 BufferUsage::COMPOSER_OVERLAY,
464 "test");
465
466 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
467
468 const int32_t halfW = bufferWidth / 2;
469 const int32_t halfH = bufferHeight / 2;
470 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
471 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
472 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
473 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
474
475 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700476 }
477
chaviw0e3479f2018-09-10 16:49:30 -0700478 std::unique_ptr<ScreenCapture> screenshot() {
479 std::unique_ptr<ScreenCapture> screenshot;
480 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700481 return screenshot;
482 }
483
Marissa Wall713b63f2018-10-17 15:42:43 -0700484 static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
485 static BufferGenerator bufferGenerator;
486 return bufferGenerator.get(outBuffer, outFence);
487 }
488
Chia-I Wu718daf82017-10-20 11:57:17 -0700489 sp<SurfaceComposerClient> mClient;
490
491 sp<IBinder> mDisplay;
492 uint32_t mDisplayWidth;
493 uint32_t mDisplayHeight;
494 uint32_t mDisplayLayerStack;
Marissa Wall861616d2018-10-22 12:52:23 -0700495 Rect mDisplayRect = Rect::INVALID_RECT;
Chia-I Wu718daf82017-10-20 11:57:17 -0700496
497 // leave room for ~256 layers
498 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
499
chaviw0e3479f2018-09-10 16:49:30 -0700500 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700501 bool mColorManagementUsed;
502
Chia-I Wu718daf82017-10-20 11:57:17 -0700503private:
504 void SetUpDisplay() {
505 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
506 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
507
508 // get display width/height
509 DisplayInfo info;
510 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
511 mDisplayWidth = info.w;
512 mDisplayHeight = info.h;
Marissa Wall861616d2018-10-22 12:52:23 -0700513 mDisplayRect =
514 Rect(static_cast<int32_t>(mDisplayWidth), static_cast<int32_t>(mDisplayHeight));
Chia-I Wu718daf82017-10-20 11:57:17 -0700515
516 // After a new buffer is queued, SurfaceFlinger is notified and will
517 // latch the new buffer on next vsync. Let's heuristically wait for 3
518 // vsyncs.
519 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
520
521 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700522
Vishnu Nair88a11f22018-11-28 18:30:57 -0800523 mBlackBgSurface =
524 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
525 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
chaviw0e3479f2018-09-10 16:49:30 -0700526
Chia-I Wu718daf82017-10-20 11:57:17 -0700527 // set layer stack (b/68888219)
528 Transaction t;
529 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800530 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700531 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
532 t.setColor(mBlackBgSurface, half3{0, 0, 0});
533 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700534 t.apply();
535 }
536
chaviw0e3479f2018-09-10 16:49:30 -0700537 void waitForLayerBuffers() {
538 // Request an empty transaction to get applied synchronously to ensure the buffer is
539 // latched.
540 Transaction().apply(true);
541 usleep(mBufferPostDelay);
542 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700543
544 int32_t mBufferPostDelay;
Alec Mouri80863a62019-01-17 15:19:35 -0800545
546 friend class LayerRenderPathTestHarness;
547};
548enum class RenderPath { SCREENSHOT, VIRTUAL_DISPLAY };
549
550class LayerRenderPathTestHarness {
551public:
552 LayerRenderPathTestHarness(LayerTransactionTest* delegate, RenderPath renderPath)
553 : mDelegate(delegate), mRenderPath(renderPath) {}
554
555 std::unique_ptr<ScreenCapture> getScreenCapture() {
556 switch (mRenderPath) {
557 case RenderPath::SCREENSHOT:
558 return mDelegate->screenshot();
559 case RenderPath::VIRTUAL_DISPLAY:
560
561 sp<IBinder> mainDisplay =
562 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
563 DisplayInfo mainDisplayInfo;
564 SurfaceComposerClient::getDisplayInfo(mainDisplay, &mainDisplayInfo);
565
566 sp<IBinder> vDisplay;
567 sp<IGraphicBufferProducer> producer;
568 sp<IGraphicBufferConsumer> consumer;
569 sp<BufferItemConsumer> itemConsumer;
570 BufferQueue::createBufferQueue(&producer, &consumer);
571
572 consumer->setConsumerName(String8("Virtual disp consumer"));
573 consumer->setDefaultBufferSize(mainDisplayInfo.w, mainDisplayInfo.h);
574
575 itemConsumer = new BufferItemConsumer(consumer,
576 // Sample usage bits from screenrecord
577 GRALLOC_USAGE_HW_VIDEO_ENCODER |
578 GRALLOC_USAGE_SW_READ_OFTEN);
579
580 vDisplay = SurfaceComposerClient::createDisplay(String8("VirtualDisplay"),
581 false /*secure*/);
582
583 SurfaceComposerClient::Transaction t;
584 t.setDisplaySurface(vDisplay, producer);
585 t.setDisplayLayerStack(vDisplay, 0);
586 t.setDisplayProjection(vDisplay, mainDisplayInfo.orientation,
587 Rect(mainDisplayInfo.viewportW, mainDisplayInfo.viewportH),
588 Rect(mainDisplayInfo.w, mainDisplayInfo.h));
589 t.apply();
590 SurfaceComposerClient::Transaction().apply(true);
591 BufferItem item;
592 itemConsumer->acquireBuffer(&item, 0, true);
593 auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer);
594 itemConsumer->releaseBuffer(item);
595 SurfaceComposerClient::destroyDisplay(vDisplay);
596 return sc;
597 }
598 }
599
600protected:
601 LayerTransactionTest* mDelegate;
602 RenderPath mRenderPath;
Chia-I Wu718daf82017-10-20 11:57:17 -0700603};
604
Alec Mouri80863a62019-01-17 15:19:35 -0800605class LayerTypeTransactionHarness : public LayerTransactionTest {
Marissa Wall61c58622018-07-18 10:12:20 -0700606public:
Alec Mouri80863a62019-01-17 15:19:35 -0800607 LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {}
Marissa Wall61c58622018-07-18 10:12:20 -0700608
609 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
Alec Mouri80863a62019-01-17 15:19:35 -0800610 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700611 // if the flags already have a layer type specified, return an error
612 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
613 return nullptr;
614 }
chaviwf66724d2018-11-28 16:35:21 -0800615 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent);
Marissa Wall61c58622018-07-18 10:12:20 -0700616 }
617
618 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
619 int32_t bufferHeight) {
620 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
621 bufferWidth, bufferHeight));
622 }
623
624 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
625 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
626 const Color& bottomLeft, const Color& bottomRight) {
627 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
628 bufferWidth, bufferHeight,
629 topLeft, topRight,
630 bottomLeft, bottomRight));
631 }
632
633protected:
634 uint32_t mLayerType;
635};
636
Alec Mouri80863a62019-01-17 15:19:35 -0800637class LayerTypeTransactionTest : public LayerTypeTransactionHarness,
638 public ::testing::WithParamInterface<uint32_t> {
639public:
640 LayerTypeTransactionTest() : LayerTypeTransactionHarness(GetParam()) {}
641};
642
643class LayerTypeAndRenderTypeTransactionTest
644 : public LayerTypeTransactionHarness,
645 public ::testing::WithParamInterface<std::tuple<uint32_t, RenderPath>> {
646public:
647 LayerTypeAndRenderTypeTransactionTest()
648 : LayerTypeTransactionHarness(std::get<0>(GetParam())),
649 mRenderPathHarness(LayerRenderPathTestHarness(this, std::get<1>(GetParam()))) {}
650
651 std::unique_ptr<ScreenCapture> getScreenCapture() {
652 return mRenderPathHarness.getScreenCapture();
653 }
654
655protected:
656 LayerRenderPathTestHarness mRenderPathHarness;
657};
658
659// Environment for starting up binder threads. This is required for testing
660// virtual displays, as BufferQueue parameters may be queried over binder.
661class BinderEnvironment : public ::testing::Environment {
662public:
663 void SetUp() override { ProcessState::self()->startThreadPool(); }
664};
665
666::testing::Environment* const binderEnv =
667 ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
668
669class LayerRenderTypeTransactionTest : public LayerTransactionTest,
670 public ::testing::WithParamInterface<RenderPath> {
671public:
672 LayerRenderTypeTransactionTest() : mHarness(LayerRenderPathTestHarness(this, GetParam())) {}
673
674 std::unique_ptr<ScreenCapture> getScreenCapture() { return mHarness.getScreenCapture(); }
675 void setRelativeZBasicHelper(uint32_t layerType);
676 void setRelativeZGroupHelper(uint32_t layerType);
677 void setAlphaBasicHelper(uint32_t layerType);
678
679protected:
680 LayerRenderPathTestHarness mHarness;
681};
682
683INSTANTIATE_TEST_CASE_P(
684 LayerTypeAndRenderTypeTransactionTests, LayerTypeAndRenderTypeTransactionTest,
685 ::testing::Combine(
686 ::testing::Values(
687 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
688 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)),
689 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT)));
690
691INSTANTIATE_TEST_CASE_P(LayerRenderTypeTransactionTests, LayerRenderTypeTransactionTest,
692 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT));
693
Marissa Wall61c58622018-07-18 10:12:20 -0700694INSTANTIATE_TEST_CASE_P(
695 LayerTypeTransactionTests, LayerTypeTransactionTest,
696 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
697 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
698
Alec Mouri80863a62019-01-17 15:19:35 -0800699TEST_P(LayerRenderTypeTransactionTest, SetPositionBasic_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700700 sp<SurfaceControl> layer;
701 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700702 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700703
704 {
705 SCOPED_TRACE("default position");
Marissa Wall861616d2018-10-22 12:52:23 -0700706 const Rect rect(0, 0, 32, 32);
Alec Mouri80863a62019-01-17 15:19:35 -0800707 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700708 shot->expectColor(rect, Color::RED);
709 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700710 }
711
712 Transaction().setPosition(layer, 5, 10).apply();
713 {
714 SCOPED_TRACE("new position");
Marissa Wall861616d2018-10-22 12:52:23 -0700715 const Rect rect(5, 10, 37, 42);
Alec Mouri80863a62019-01-17 15:19:35 -0800716 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700717 shot->expectColor(rect, Color::RED);
718 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700719 }
720}
721
Alec Mouri80863a62019-01-17 15:19:35 -0800722TEST_P(LayerRenderTypeTransactionTest, SetPositionRounding_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700723 sp<SurfaceControl> layer;
724 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700725 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700726
727 // GLES requires only 4 bits of subpixel precision during rasterization
728 // XXX GLES composition does not match HWC composition due to precision
729 // loss (b/69315223)
730 const float epsilon = 1.0f / 16.0f;
731 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
732 {
733 SCOPED_TRACE("rounding down");
Alec Mouri80863a62019-01-17 15:19:35 -0800734 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700735 }
736
737 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
738 {
739 SCOPED_TRACE("rounding up");
Alec Mouri80863a62019-01-17 15:19:35 -0800740 getScreenCapture()->expectColor(Rect(1, 1, 33, 33), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700741 }
742}
743
Alec Mouri80863a62019-01-17 15:19:35 -0800744TEST_P(LayerRenderTypeTransactionTest, SetPositionOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700745 sp<SurfaceControl> layer;
746 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700747 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700748
749 Transaction().setPosition(layer, -32, -32).apply();
750 {
751 SCOPED_TRACE("negative coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800752 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700753 }
754
755 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
756 {
757 SCOPED_TRACE("positive coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800758 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700759 }
760}
761
Alec Mouri80863a62019-01-17 15:19:35 -0800762TEST_P(LayerRenderTypeTransactionTest, SetPositionPartiallyOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700763 sp<SurfaceControl> layer;
764 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700765 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700766
767 // partially out of bounds
768 Transaction().setPosition(layer, -30, -30).apply();
769 {
770 SCOPED_TRACE("negative coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800771 getScreenCapture()->expectColor(Rect(0, 0, 2, 2), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700772 }
773
774 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
775 {
776 SCOPED_TRACE("positive coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800777 getScreenCapture()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
778 mDisplayHeight),
779 Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700780 }
781}
782
Alec Mouri80863a62019-01-17 15:19:35 -0800783TEST_P(LayerRenderTypeTransactionTest, SetPositionWithResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700784 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700785 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
786 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700787
788 // setPosition is applied immediately by default, with or without resize
789 // pending
790 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
791 {
792 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800793 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700794 const Rect rect(5, 10, 37, 42);
Marissa Wall61c58622018-07-18 10:12:20 -0700795 shot->expectColor(rect, Color::RED);
796 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700797 }
798
Marissa Wall861616d2018-10-22 12:52:23 -0700799 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700800 {
801 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800802 getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700803 }
804}
805
Alec Mouri80863a62019-01-17 15:19:35 -0800806TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700807 sp<SurfaceControl> layer;
808 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700809 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700810
811 // request setPosition to be applied with the next resize
812 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
813 {
814 SCOPED_TRACE("new position pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800815 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700816 }
817
818 Transaction().setPosition(layer, 15, 20).apply();
819 {
820 SCOPED_TRACE("pending new position modified");
Alec Mouri80863a62019-01-17 15:19:35 -0800821 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700822 }
823
824 Transaction().setSize(layer, 64, 64).apply();
825 {
826 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800827 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700828 }
829
830 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700831 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700832 {
833 SCOPED_TRACE("new position applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800834 getScreenCapture()->expectColor(Rect(15, 20, 79, 84), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700835 }
836}
837
Alec Mouri80863a62019-01-17 15:19:35 -0800838TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700839 sp<SurfaceControl> layer;
840 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700841 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700842
843 // setPosition is not immediate even with SCALE_TO_WINDOW override
844 Transaction()
845 .setPosition(layer, 5, 10)
846 .setSize(layer, 64, 64)
847 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
848 .setGeometryAppliesWithResize(layer)
849 .apply();
850 {
851 SCOPED_TRACE("new position pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800852 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700853 }
854
Marissa Wall61c58622018-07-18 10:12:20 -0700855 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700856 {
857 SCOPED_TRACE("new position applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800858 getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700859 }
860}
861
Alec Mouri80863a62019-01-17 15:19:35 -0800862TEST_P(LayerRenderTypeTransactionTest, SetSizeBasic_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700863 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700864 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
865 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700866
867 Transaction().setSize(layer, 64, 64).apply();
868 {
869 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800870 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700871 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -0700872 shot->expectColor(rect, Color::RED);
873 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700874 }
875
Marissa Wall861616d2018-10-22 12:52:23 -0700876 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700877 {
878 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800879 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700880 const Rect rect(0, 0, 64, 64);
881 shot->expectColor(rect, Color::RED);
882 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700883 }
884}
885
Alec Mouri80863a62019-01-17 15:19:35 -0800886TEST_P(LayerTypeAndRenderTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700887 // cannot test robustness against invalid sizes (zero or really huge)
888}
889
Alec Mouri80863a62019-01-17 15:19:35 -0800890TEST_P(LayerRenderTypeTransactionTest, SetSizeWithScaleToWindow_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700891 sp<SurfaceControl> layer;
892 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700893 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700894
895 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
896 Transaction()
897 .setSize(layer, 64, 64)
898 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
899 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -0800900 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700901}
902
Alec Mouri80863a62019-01-17 15:19:35 -0800903TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700904 sp<SurfaceControl> layerR;
905 sp<SurfaceControl> layerG;
906 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700907 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700908 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700909 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700910
911 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
912 {
913 SCOPED_TRACE("layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800914 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700915 }
916
917 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
918 {
919 SCOPED_TRACE("layerG");
Alec Mouri80863a62019-01-17 15:19:35 -0800920 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700921 }
922}
923
Alec Mouri80863a62019-01-17 15:19:35 -0800924TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700925 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800926 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700927 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800928 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700929 sp<SurfaceControl> layerR;
930 sp<SurfaceControl> layerG;
931 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700932 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700933 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700934 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700935
chaviw0e3479f2018-09-10 16:49:30 -0700936 Transaction()
937 .reparent(layerR, parent->getHandle())
938 .reparent(layerG, parent->getHandle())
939 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700940 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
941 {
942 SCOPED_TRACE("layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800943 auto shot = getScreenCapture();
chaviw0e3479f2018-09-10 16:49:30 -0700944 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700945 }
946
947 Transaction().setLayer(layerR, -3).apply();
948 {
949 SCOPED_TRACE("layerG");
Alec Mouri80863a62019-01-17 15:19:35 -0800950 auto shot = getScreenCapture();
chaviw0e3479f2018-09-10 16:49:30 -0700951 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700952 }
953}
954
Alec Mouri80863a62019-01-17 15:19:35 -0800955void LayerRenderTypeTransactionTest::setRelativeZBasicHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700956 sp<SurfaceControl> layerR;
957 sp<SurfaceControl> layerG;
Marissa Wall861616d2018-10-22 12:52:23 -0700958 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32, layerType));
959 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
960 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32, layerType));
961 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700962
Marissa Wall861616d2018-10-22 12:52:23 -0700963 switch (layerType) {
964 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
965 Transaction()
966 .setPosition(layerG, 16, 16)
967 .setRelativeLayer(layerG, layerR->getHandle(), 1)
968 .apply();
969 break;
970 case ISurfaceComposerClient::eFXSurfaceBufferState:
971 Transaction()
972 .setFrame(layerR, Rect(0, 0, 32, 32))
973 .setFrame(layerG, Rect(16, 16, 48, 48))
974 .setRelativeLayer(layerG, layerR->getHandle(), 1)
975 .apply();
976 break;
977 default:
978 ASSERT_FALSE(true) << "Unsupported layer type";
979 }
Chia-I Wu49313302017-10-31 10:14:40 -0700980 {
981 SCOPED_TRACE("layerG above");
Alec Mouri80863a62019-01-17 15:19:35 -0800982 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700983 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
984 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
985 }
986
987 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
988 {
989 SCOPED_TRACE("layerG below");
Alec Mouri80863a62019-01-17 15:19:35 -0800990 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700991 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
992 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
993 }
994}
995
Alec Mouri80863a62019-01-17 15:19:35 -0800996TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -0700997 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
998}
999
Alec Mouri80863a62019-01-17 15:19:35 -08001000TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001001 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1002}
1003
Marissa Wall61c58622018-07-18 10:12:20 -07001004TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -07001005 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -08001006 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -07001007 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -08001008 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wuec2d9852017-11-21 09:21:01 -08001009 sp<SurfaceControl> layerR;
1010 sp<SurfaceControl> layerG;
1011 sp<SurfaceControl> layerB;
1012 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001013 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -08001014 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001015 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -08001016 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001017 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -08001018
chaviw0e3479f2018-09-10 16:49:30 -07001019 Transaction()
1020 .reparent(layerB, parent->getHandle())
1021 .apply();
1022
Chia-I Wuec2d9852017-11-21 09:21:01 -08001023 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
1024 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
1025
chaviw0e3479f2018-09-10 16:49:30 -07001026 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -08001027 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -07001028 sp<IBinder> parentHandle = parent->getHandle();
Marissa Wall861616d2018-10-22 12:52:23 -07001029 ScreenCapture::captureLayers(&screenshot, parentHandle, Rect(0, 0, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -08001030 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1031}
1032
Alec Mouri80863a62019-01-17 15:19:35 -08001033void LayerRenderTypeTransactionTest::setRelativeZGroupHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -07001034 sp<SurfaceControl> layerR;
1035 sp<SurfaceControl> layerG;
1036 sp<SurfaceControl> layerB;
Marissa Wall861616d2018-10-22 12:52:23 -07001037 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test", 32, 32, layerType));
1038 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
1039 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test", 32, 32, layerType));
1040 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
1041 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test", 32, 32, layerType));
1042 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001043
1044 // layerR = 0, layerG = layerR + 3, layerB = 2
Marissa Wall861616d2018-10-22 12:52:23 -07001045 switch (layerType) {
1046 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1047 Transaction()
1048 .setPosition(layerG, 8, 8)
1049 .setRelativeLayer(layerG, layerR->getHandle(), 3)
1050 .setPosition(layerB, 16, 16)
1051 .setLayer(layerB, mLayerZBase + 2)
1052 .apply();
1053 break;
1054 case ISurfaceComposerClient::eFXSurfaceBufferState:
1055 Transaction()
1056 .setFrame(layerR, Rect(0, 0, 32, 32))
1057 .setFrame(layerG, Rect(8, 8, 40, 40))
1058 .setRelativeLayer(layerG, layerR->getHandle(), 3)
1059 .setFrame(layerB, Rect(16, 16, 48, 48))
1060 .setLayer(layerB, mLayerZBase + 2)
1061 .apply();
1062 break;
1063 default:
1064 ASSERT_FALSE(true) << "Unsupported layer type";
1065 }
1066
Chia-I Wu49313302017-10-31 10:14:40 -07001067 {
1068 SCOPED_TRACE("(layerR < layerG) < layerB");
Alec Mouri80863a62019-01-17 15:19:35 -08001069 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001070 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
1071 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
1072 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
1073 }
1074
1075 // layerR = 4, layerG = layerR + 3, layerB = 2
1076 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
1077 {
1078 SCOPED_TRACE("layerB < (layerR < layerG)");
Alec Mouri80863a62019-01-17 15:19:35 -08001079 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001080 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
1081 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
1082 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
1083 }
1084
1085 // layerR = 4, layerG = layerR - 3, layerB = 2
1086 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
1087 {
1088 SCOPED_TRACE("layerB < (layerG < layerR)");
Alec Mouri80863a62019-01-17 15:19:35 -08001089 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001090 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1091 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
1092 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
1093 }
1094
1095 // restore to absolute z
1096 // layerR = 4, layerG = 0, layerB = 2
1097 Transaction().setLayer(layerG, mLayerZBase).apply();
1098 {
1099 SCOPED_TRACE("layerG < layerB < layerR");
Alec Mouri80863a62019-01-17 15:19:35 -08001100 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001101 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1102 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
1103 }
1104
1105 // layerR should not affect layerG anymore
1106 // layerR = 1, layerG = 0, layerB = 2
1107 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
1108 {
1109 SCOPED_TRACE("layerG < layerR < layerB");
Alec Mouri80863a62019-01-17 15:19:35 -08001110 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001111 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1112 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
1113 }
1114}
1115
Alec Mouri80863a62019-01-17 15:19:35 -08001116TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -07001117 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1118}
1119
Alec Mouri80863a62019-01-17 15:19:35 -08001120TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001121 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1122}
1123
Alec Mouri80863a62019-01-17 15:19:35 -08001124TEST_P(LayerTypeAndRenderTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -07001125 sp<SurfaceControl> layerR;
1126 sp<SurfaceControl> layerG;
1127
1128 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001129 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001130 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001131 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001132
1133 Transaction()
1134 .setPosition(layerG, 16, 16)
1135 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1136 .apply();
1137
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001138 layerG->clear();
Chia-I Wu49313302017-10-31 10:14:40 -07001139 // layerG should have been removed
Alec Mouri80863a62019-01-17 15:19:35 -08001140 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu49313302017-10-31 10:14:40 -07001141}
1142
Alec Mouri80863a62019-01-17 15:19:35 -08001143TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001144 sp<SurfaceControl> layer;
1145 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001146 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001147
1148 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
1149 {
1150 SCOPED_TRACE("layer hidden");
Alec Mouri80863a62019-01-17 15:19:35 -08001151 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu57b27502017-10-31 10:14:40 -07001152 }
1153
1154 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1155 {
1156 SCOPED_TRACE("layer shown");
Alec Mouri80863a62019-01-17 15:19:35 -08001157 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu57b27502017-10-31 10:14:40 -07001158 }
1159}
1160
Alec Mouri80863a62019-01-17 15:19:35 -08001161TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001162 const Color translucentRed = {100, 0, 0, 100};
1163 sp<SurfaceControl> layerR;
1164 sp<SurfaceControl> layerG;
1165 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001166 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001167 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001168 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001169
1170 Transaction()
1171 .setLayer(layerR, mLayerZBase + 1)
1172 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1173 .apply();
1174 {
1175 SCOPED_TRACE("layerR opaque");
Alec Mouri80863a62019-01-17 15:19:35 -08001176 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
Chia-I Wu57b27502017-10-31 10:14:40 -07001177 }
1178
1179 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1180 {
1181 SCOPED_TRACE("layerR translucent");
1182 const uint8_t g = uint8_t(255 - translucentRed.a);
Alec Mouri80863a62019-01-17 15:19:35 -08001183 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
Chia-I Wu57b27502017-10-31 10:14:40 -07001184 }
1185}
1186
Marissa Wall61c58622018-07-18 10:12:20 -07001187TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001188 sp<SurfaceControl> layer;
1189 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001190 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001191
1192 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001193 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001194 Transaction()
1195 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1196 .apply(true);
1197 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001198 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001199
1200 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1201 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001202 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001203}
1204
Alec Mouri80863a62019-01-17 15:19:35 -08001205TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001206 const Rect top(0, 0, 32, 16);
1207 const Rect bottom(0, 16, 32, 32);
1208 sp<SurfaceControl> layer;
1209 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1210
1211 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001212 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1213 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1214 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001215 // setTransparentRegionHint always applies to the following buffer
1216 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001217 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001218 {
1219 SCOPED_TRACE("top transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001220 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001221 shot->expectColor(top, Color::BLACK);
1222 shot->expectColor(bottom, Color::RED);
1223 }
1224
1225 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1226 {
1227 SCOPED_TRACE("transparent region hint pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001228 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001229 shot->expectColor(top, Color::BLACK);
1230 shot->expectColor(bottom, Color::RED);
1231 }
1232
Marissa Wall61c58622018-07-18 10:12:20 -07001233 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1234 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1235 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1236 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001237 {
1238 SCOPED_TRACE("bottom transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001239 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001240 shot->expectColor(top, Color::RED);
1241 shot->expectColor(bottom, Color::BLACK);
1242 }
1243}
1244
Alec Mouri80863a62019-01-17 15:19:35 -08001245TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001246 const Rect top(0, 0, 32, 16);
1247 const Rect bottom(0, 16, 32, 32);
1248 sp<SurfaceControl> layer;
1249 ASSERT_NO_FATAL_FAILURE(
1250 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1251
1252 sp<GraphicBuffer> buffer =
1253 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1254 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1255 BufferUsage::COMPOSER_OVERLAY,
1256 "test");
1257
1258 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1259 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1260 Transaction()
1261 .setTransparentRegionHint(layer, Region(top))
1262 .setBuffer(layer, buffer)
Marissa Wall861616d2018-10-22 12:52:23 -07001263 .setFrame(layer, Rect(0, 0, 32, 32))
Marissa Wall61c58622018-07-18 10:12:20 -07001264 .apply();
1265 {
1266 SCOPED_TRACE("top transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001267 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001268 shot->expectColor(top, Color::BLACK);
1269 shot->expectColor(bottom, Color::RED);
1270 }
1271
1272 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1273 {
1274 SCOPED_TRACE("transparent region hint intermediate");
Alec Mouri80863a62019-01-17 15:19:35 -08001275 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001276 shot->expectColor(top, Color::BLACK);
1277 shot->expectColor(bottom, Color::BLACK);
1278 }
1279
1280 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1281 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1282 BufferUsage::COMPOSER_OVERLAY,
1283 "test");
1284
1285 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1286 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
Marissa Wall861616d2018-10-22 12:52:23 -07001287 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001288 {
1289 SCOPED_TRACE("bottom transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001290 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001291 shot->expectColor(top, Color::RED);
1292 shot->expectColor(bottom, Color::BLACK);
1293 }
1294}
1295
Alec Mouri80863a62019-01-17 15:19:35 -08001296TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001297 sp<SurfaceControl> layerTransparent;
1298 sp<SurfaceControl> layerR;
1299 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1300 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1301
1302 // check that transparent region hint is bound by the layer size
1303 Transaction()
Marissa Wall861616d2018-10-22 12:52:23 -07001304 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001305 .setPosition(layerR, 16, 16)
1306 .setLayer(layerR, mLayerZBase + 1)
1307 .apply();
Marissa Wall861616d2018-10-22 12:52:23 -07001308 ASSERT_NO_FATAL_FAILURE(
1309 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1310 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerR, Color::RED, 32, 32));
Alec Mouri80863a62019-01-17 15:19:35 -08001311 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001312}
1313
Alec Mouri80863a62019-01-17 15:19:35 -08001314TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001315 sp<SurfaceControl> layerTransparent;
1316 sp<SurfaceControl> layerR;
1317 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1318 ASSERT_NO_FATAL_FAILURE(
1319 layerR = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1320
1321 // check that transparent region hint is bound by the layer size
1322 Transaction()
1323 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
1324 .setFrame(layerR, Rect(16, 16, 48, 48))
1325 .setLayer(layerR, mLayerZBase + 1)
1326 .apply();
1327 ASSERT_NO_FATAL_FAILURE(
1328 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1329 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layerR, Color::RED, 32, 32));
Alec Mouri80863a62019-01-17 15:19:35 -08001330 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
Marissa Wall861616d2018-10-22 12:52:23 -07001331}
1332
Valerie Haua72e2812019-01-23 13:40:39 -08001333TEST_P(LayerRenderTypeTransactionTest, SetColorAlpha_Color_NoEffect) {
1334 sp<SurfaceControl> layer;
1335 ASSERT_NO_FATAL_FAILURE(
1336 layer = createLayer("test", 0, 0, ISurfaceComposerClient::eFXSurfaceColor));
1337
1338 half3 color;
1339 color.r = 1.0f;
1340 color.g = 0.0f;
1341 color.b = 0.0f;
1342 Transaction()
1343 .setCrop_legacy(layer, Rect(0, 0, 32, 32))
1344 .setAlpha(layer, 1.0f)
1345 .setColor(layer, color)
1346 .setColorAlpha(layer, 0.5f)
1347 .apply();
1348
1349 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1350}
1351
1352TEST_P(LayerRenderTypeTransactionTest, SetColorAlpha_BufferQueue_NoEffect) {
1353 sp<SurfaceControl> layer;
1354 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1355 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
1356
1357 Transaction().setAlpha(layer, 1.0f).setColorAlpha(layer, 0.5f).apply();
1358
1359 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1360}
1361
1362TEST_P(LayerRenderTypeTransactionTest, SetColorAlpha_BufferState_ColorLayer) {
1363 sp<SurfaceControl> bgLayer;
1364 sp<SurfaceControl> layer;
1365 ASSERT_NO_FATAL_FAILURE(bgLayer = createLayer("test bg", 32, 32));
1366 ASSERT_NO_FATAL_FAILURE(
1367 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1368
1369 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bgLayer, Color::RED, 32, 32));
1370
1371 // create color layer
1372 half3 color;
1373 color.r = 0.0f;
1374 color.g = 1.0f;
1375 color.b = 0.0f;
1376 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).setColor(layer, color).apply();
1377
1378 {
1379 SCOPED_TRACE("before alpha");
1380 auto shot = screenshot();
1381 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
1382 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1383 }
1384
1385 // apply alpha
1386 Transaction().setAlpha(layer, 0.0f).apply();
1387 {
1388 SCOPED_TRACE("set alpha");
1389 auto shot = screenshot();
1390 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1391 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1392 }
1393}
1394
1395TEST_P(LayerRenderTypeTransactionTest, SetColorAlpha_BufferState_NoColorLayer) {
1396 sp<SurfaceControl> bgLayer;
1397 sp<SurfaceControl> layer;
1398 ASSERT_NO_FATAL_FAILURE(bgLayer = createLayer("test bg", 32, 32));
1399 ASSERT_NO_FATAL_FAILURE(
1400 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1401
1402 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bgLayer, Color::RED, 32, 32));
1403
1404 {
1405 SCOPED_TRACE("before alpha");
1406 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1407 }
1408
1409 // setting alpha without creating color layer should have no effect
1410 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).setAlpha(layer, 0.5f).apply();
1411 {
1412 SCOPED_TRACE("alpha");
1413 auto shot = screenshot();
1414 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1415 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1416 }
1417}
1418
Alec Mouri80863a62019-01-17 15:19:35 -08001419void LayerRenderTypeTransactionTest::setAlphaBasicHelper(uint32_t layerType) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001420 sp<SurfaceControl> layer1;
1421 sp<SurfaceControl> layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07001422 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32, layerType));
1423 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32, layerType));
1424 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer1, {64, 0, 0, 255}, 32, 32));
1425 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001426
Marissa Wall861616d2018-10-22 12:52:23 -07001427 switch (layerType) {
1428 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1429 Transaction()
1430 .setAlpha(layer1, 0.25f)
1431 .setAlpha(layer2, 0.75f)
1432 .setPosition(layer2, 16, 0)
1433 .setLayer(layer2, mLayerZBase + 1)
1434 .apply();
1435 break;
1436 case ISurfaceComposerClient::eFXSurfaceBufferState:
1437 Transaction()
1438 .setAlpha(layer1, 0.25f)
1439 .setAlpha(layer2, 0.75f)
1440 .setFrame(layer1, Rect(0, 0, 32, 32))
1441 .setFrame(layer2, Rect(16, 0, 48, 32))
1442 .setLayer(layer2, mLayerZBase + 1)
1443 .apply();
1444 break;
1445 default:
1446 ASSERT_FALSE(true) << "Unsupported layer type";
1447 }
Chia-I Wua8a515e2017-11-01 15:16:35 -07001448 {
Alec Mouri80863a62019-01-17 15:19:35 -08001449 auto shot = getScreenCapture();
Chia-I Wua8a515e2017-11-01 15:16:35 -07001450 uint8_t r = 16; // 64 * 0.25f
1451 uint8_t g = 48; // 64 * 0.75f
1452 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1453 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1454
1455 r /= 4; // r * (1.0f - 0.75f)
1456 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1457 }
1458}
1459
Alec Mouri80863a62019-01-17 15:19:35 -08001460TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -07001461 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1462}
1463
Alec Mouri80863a62019-01-17 15:19:35 -08001464TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001465 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1466}
1467
Alec Mouri80863a62019-01-17 15:19:35 -08001468TEST_P(LayerTypeAndRenderTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001469 const Color color = {64, 0, 0, 255};
1470 sp<SurfaceControl> layer;
1471 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001472 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001473
1474 Transaction().setAlpha(layer, 2.0f).apply();
1475 {
1476 SCOPED_TRACE("clamped to 1.0f");
Alec Mouri80863a62019-01-17 15:19:35 -08001477 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), color);
Chia-I Wua8a515e2017-11-01 15:16:35 -07001478 }
1479
1480 Transaction().setAlpha(layer, -1.0f).apply();
1481 {
1482 SCOPED_TRACE("clamped to 0.0f");
Alec Mouri80863a62019-01-17 15:19:35 -08001483 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wua8a515e2017-11-01 15:16:35 -07001484 }
1485}
1486
Alec Mouri80863a62019-01-17 15:19:35 -08001487TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadius) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001488 sp<SurfaceControl> layer;
1489 const uint8_t size = 64;
1490 const uint8_t testArea = 4;
Lucas Dupina1d0e312018-12-04 22:30:27 -08001491 const float cornerRadius = 20.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001492 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1493 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1494
1495 Transaction()
1496 .setCornerRadius(layer, cornerRadius)
1497 .apply();
1498 {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001499 const uint8_t bottom = size - 1;
1500 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001501 auto shot = getScreenCapture();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001502 // Transparent corners
1503 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
Lucas Dupina1d0e312018-12-04 22:30:27 -08001504 shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
1505 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1506 shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
1507 }
1508}
1509
Alec Mouri80863a62019-01-17 15:19:35 -08001510TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildCrop) {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001511 sp<SurfaceControl> parent;
1512 sp<SurfaceControl> child;
1513 const uint8_t size = 64;
1514 const uint8_t testArea = 4;
1515 const float cornerRadius = 20.0f;
1516 ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
1517 ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
1518 ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
1519 ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
1520
1521 Transaction()
1522 .setCornerRadius(parent, cornerRadius)
1523 .reparent(child, parent->getHandle())
1524 .setPosition(child, 0, size / 2)
1525 .apply();
1526 {
1527 const uint8_t bottom = size - 1;
1528 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001529 auto shot = getScreenCapture();
Lucas Dupina1d0e312018-12-04 22:30:27 -08001530 // Top edge of child should not have rounded corners because it's translated in the parent
1531 shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
1532 Color::GREEN);
1533 // But bottom edges should have been clipped according to parent bounds
1534 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1535 shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001536 }
1537}
1538
Alec Mouri80863a62019-01-17 15:19:35 -08001539TEST_P(LayerRenderTypeTransactionTest, SetColorBasic) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001540 sp<SurfaceControl> bufferLayer;
1541 sp<SurfaceControl> colorLayer;
1542 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001543 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001544 ASSERT_NO_FATAL_FAILURE(colorLayer =
1545 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1546 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001547
Vishnu Nair88a11f22018-11-28 18:30:57 -08001548 Transaction()
1549 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1550 .setLayer(colorLayer, mLayerZBase + 1)
1551 .apply();
1552
Chia-I Wue4ef6102017-11-01 15:16:35 -07001553 {
1554 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08001555 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001556 }
1557
1558 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1559 const Color expected = {15, 51, 85, 255};
1560 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1561 // channel) should be less than one
1562 const uint8_t tolerance = 1;
1563 Transaction().setColor(colorLayer, color).apply();
1564 {
1565 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08001566 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001567 }
1568}
1569
Valerie Haua72e2812019-01-23 13:40:39 -08001570TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_BufferState_NoPriorColor) {
1571 sp<SurfaceControl> bufferQueueLayer;
1572 sp<SurfaceControl> bufferStateLayer;
1573 ASSERT_NO_FATAL_FAILURE(bufferQueueLayer = createLayer("test bg", 32, 32));
1574 ASSERT_NO_FATAL_FAILURE(
1575 bufferStateLayer =
1576 createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1577
1578 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferQueueLayer, Color::RED, 32, 32));
1579
1580 {
1581 SCOPED_TRACE("default color");
1582 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1583 }
1584
1585 half3 color;
1586 color.r = 0.0f;
1587 color.g = 1.0f;
1588 color.b = 0.0f;
1589 Transaction()
1590 .setFrame(bufferStateLayer, Rect(0, 0, 32, 32))
1591 .setLayer(bufferStateLayer, mLayerZBase + 1)
1592 .setColor(bufferStateLayer, color)
1593 .apply();
1594
1595 {
1596 SCOPED_TRACE("set color");
1597 auto shot = screenshot();
1598 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
1599 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1600 }
1601}
1602
1603TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_BufferState_PriorColor) {
1604 sp<SurfaceControl> bufferQueueLayer;
1605 sp<SurfaceControl> bufferStateLayer;
1606 ASSERT_NO_FATAL_FAILURE(bufferQueueLayer = createLayer("test bg", 32, 32));
1607 ASSERT_NO_FATAL_FAILURE(
1608 bufferStateLayer =
1609 createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1610
1611 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferQueueLayer, Color::RED, 32, 32));
1612
1613 half3 color;
1614 color.r = 0.0f;
1615 color.g = 1.0f;
1616 color.b = 0.0f;
1617 Transaction()
1618 .setFrame(bufferStateLayer, Rect(0, 0, 32, 32))
1619 .setLayer(bufferStateLayer, mLayerZBase + 1)
1620 .setColor(bufferStateLayer, color)
1621 .apply();
1622 {
1623 SCOPED_TRACE("default color");
1624 auto shot = screenshot();
1625 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
1626 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1627 }
1628
1629 color.r = 0.0f;
1630 color.g = 0.0f;
1631 color.b = 1.0f;
1632 Transaction().setColor(bufferStateLayer, color).apply();
1633 {
1634 SCOPED_TRACE("new color");
1635 auto shot = screenshot();
1636 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1637 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1638 }
1639}
1640
Alec Mouri80863a62019-01-17 15:19:35 -08001641TEST_P(LayerRenderTypeTransactionTest, SetColorClamped) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001642 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001643 ASSERT_NO_FATAL_FAILURE(colorLayer =
1644 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1645 ISurfaceComposerClient::eFXSurfaceColor));
1646 Transaction()
1647 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1648 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1649 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001650
Alec Mouri80863a62019-01-17 15:19:35 -08001651 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001652}
1653
Alec Mouri80863a62019-01-17 15:19:35 -08001654TEST_P(LayerRenderTypeTransactionTest, SetColorWithAlpha) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001655 sp<SurfaceControl> bufferLayer;
1656 sp<SurfaceControl> colorLayer;
1657 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001658 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001659 ASSERT_NO_FATAL_FAILURE(colorLayer =
1660 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1661 ISurfaceComposerClient::eFXSurfaceColor));
1662 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001663
1664 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1665 const float alpha = 0.25f;
1666 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1667 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1668 // channel) should be less than one
1669 const uint8_t tolerance = 1;
1670 Transaction()
1671 .setColor(colorLayer, color)
1672 .setAlpha(colorLayer, alpha)
1673 .setLayer(colorLayer, mLayerZBase + 1)
1674 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001675 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1676 tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001677}
1678
Alec Mouri80863a62019-01-17 15:19:35 -08001679TEST_P(LayerRenderTypeTransactionTest, SetColorWithParentAlpha_Bug74220420) {
Adrian Roosb7a96502018-04-08 11:38:55 -07001680 sp<SurfaceControl> bufferLayer;
1681 sp<SurfaceControl> parentLayer;
1682 sp<SurfaceControl> colorLayer;
1683 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1684 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001685 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001686 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1687 0 /* buffer height */,
1688 ISurfaceComposerClient::eFXSurfaceColor));
1689 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001690 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1691 const float alpha = 0.25f;
1692 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1693 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1694 // channel) should be less than one
1695 const uint8_t tolerance = 1;
1696 Transaction()
1697 .reparent(colorLayer, parentLayer->getHandle())
1698 .setColor(colorLayer, color)
1699 .setAlpha(parentLayer, alpha)
1700 .setLayer(parentLayer, mLayerZBase + 1)
1701 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001702 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1703 tolerance);
Adrian Roosb7a96502018-04-08 11:38:55 -07001704}
1705
Alec Mouri80863a62019-01-17 15:19:35 -08001706TEST_P(LayerTypeAndRenderTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001707 sp<SurfaceControl> bufferLayer;
1708 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001709 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001710
1711 // color is ignored
1712 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001713 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001714}
1715
Alec Mouri80863a62019-01-17 15:19:35 -08001716TEST_P(LayerTypeAndRenderTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001717 sp<SurfaceControl> layer;
1718 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001719 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001720
1721 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1722 {
1723 SCOPED_TRACE("non-existing layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001724 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001725 }
1726
1727 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1728 {
1729 SCOPED_TRACE("original layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001730 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001731 }
1732}
1733
Alec Mouri80863a62019-01-17 15:19:35 -08001734TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001735 sp<SurfaceControl> layer;
1736 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001737 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1738 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001739
1740 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1741 {
1742 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001743 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1744 Color::BLUE, Color::WHITE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001745 }
1746
1747 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1748 {
1749 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001750 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED,
1751 Color::WHITE, Color::BLUE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001752 }
1753
1754 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1755 {
1756 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001757 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE,
1758 Color::RED, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001759 }
1760
1761 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1762 {
1763 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001764 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED,
1765 Color::WHITE, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001766 }
1767
1768 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1769 {
1770 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001771 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN,
1772 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001773 }
1774}
1775
Alec Mouri80863a62019-01-17 15:19:35 -08001776TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001777 sp<SurfaceControl> layer;
1778 ASSERT_NO_FATAL_FAILURE(
1779 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1780 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1781 Color::BLUE, Color::WHITE));
1782
1783 Transaction()
1784 .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
1785 .setFrame(layer, Rect(0, 0, 32, 32))
1786 .apply();
1787 {
1788 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001789 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1790 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001791 }
1792
1793 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
1794 {
1795 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001796 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1797 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001798 }
1799
1800 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
1801 {
1802 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001803 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1804 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001805 }
1806
1807 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
1808 {
1809 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001810 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1811 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001812 }
1813
1814 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
1815 {
1816 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001817 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1818 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001819 }
1820}
1821
Alec Mouri80863a62019-01-17 15:19:35 -08001822TEST_P(LayerRenderTypeTransactionTest, SetMatrixRot45_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001823 sp<SurfaceControl> layer;
1824 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001825 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1826 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001827
1828 const float rot = M_SQRT1_2; // 45 degrees
1829 const float trans = M_SQRT2 * 16.0f;
1830 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1831
Alec Mouri80863a62019-01-17 15:19:35 -08001832 auto shot = getScreenCapture();
Chia-I Wu93853fe2017-11-02 08:30:27 -07001833 // check a 8x8 region inside each color
1834 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1835 const int32_t halfL = 4;
1836 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1837 };
1838 const int32_t unit = int32_t(trans / 2);
1839 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1840 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1841 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1842 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1843}
1844
Alec Mouri80863a62019-01-17 15:19:35 -08001845TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithResize_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001846 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -07001847 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1848 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001849
1850 // setMatrix is applied after any pending resize, unlike setPosition
1851 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1852 {
1853 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001854 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001855 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -07001856 shot->expectColor(rect, Color::RED);
1857 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001858 }
1859
Marissa Wall861616d2018-10-22 12:52:23 -07001860 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001861 {
1862 SCOPED_TRACE("resize applied");
Marissa Wall861616d2018-10-22 12:52:23 -07001863 const Rect rect(0, 0, 128, 128);
Alec Mouri80863a62019-01-17 15:19:35 -08001864 getScreenCapture()->expectColor(rect, Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001865 }
1866}
1867
Alec Mouri80863a62019-01-17 15:19:35 -08001868TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001869 sp<SurfaceControl> layer;
1870 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001871 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001872
1873 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1874 Transaction()
1875 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1876 .setSize(layer, 64, 64)
1877 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1878 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001879 getScreenCapture()->expectColor(Rect(0, 0, 128, 128), Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001880}
1881
Alec Mouri80863a62019-01-17 15:19:35 -08001882TEST_P(LayerRenderTypeTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001883 sp<SurfaceControl> layer;
1884 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001885 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1886 Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001887
1888 // XXX SCALE_CROP is not respected; calling setSize and
1889 // setOverrideScalingMode in separate transactions does not work
1890 // (b/69315456)
1891 Transaction()
1892 .setSize(layer, 64, 16)
1893 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1894 .apply();
1895 {
1896 SCOPED_TRACE("SCALE_TO_WINDOW");
Alec Mouri80863a62019-01-17 15:19:35 -08001897 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN,
1898 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wua56b2042017-11-01 15:16:35 -07001899 }
1900}
1901
Dan Stoza000dd012018-08-01 13:31:52 -07001902TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1903 sp<SurfaceControl> layer;
1904 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1905
1906 sp<IBinder> handle = layer->getHandle();
1907 ASSERT_TRUE(handle != nullptr);
1908
1909 FrameStats frameStats;
1910 mClient->getLayerFrameStats(handle, &frameStats);
1911
1912 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1913}
1914
Alec Mouri80863a62019-01-17 15:19:35 -08001915TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001916 sp<SurfaceControl> layer;
1917 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001918 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001919 const Rect crop(8, 8, 24, 24);
1920
Marissa Wallf58c14b2018-07-24 10:50:43 -07001921 Transaction().setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001922 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001923 shot->expectColor(crop, Color::RED);
1924 shot->expectBorder(crop, Color::BLACK);
1925}
1926
Alec Mouri80863a62019-01-17 15:19:35 -08001927TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001928 sp<SurfaceControl> layer;
1929 ASSERT_NO_FATAL_FAILURE(
1930 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1931 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1932 const Rect crop(8, 8, 24, 24);
1933
1934 Transaction().setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001935 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001936 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1937 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001938}
1939
Alec Mouri80863a62019-01-17 15:19:35 -08001940TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001941 sp<SurfaceControl> layer;
1942 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001943 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001944
1945 {
1946 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001947 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001948 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07001949 }
1950
1951 {
1952 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001953 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001954 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07001955 }
1956}
1957
Alec Mouri80863a62019-01-17 15:19:35 -08001958TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001959 sp<SurfaceControl> layer;
1960 ASSERT_NO_FATAL_FAILURE(
1961 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1962 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1963
1964 {
1965 SCOPED_TRACE("empty rect");
1966 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001967 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07001968 }
1969
1970 {
1971 SCOPED_TRACE("negative rect");
1972 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001973 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07001974 }
1975}
1976
Alec Mouri80863a62019-01-17 15:19:35 -08001977TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001978 sp<SurfaceControl> layer;
1979 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001980 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001981
Marissa Wallf58c14b2018-07-24 10:50:43 -07001982 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001983 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001984 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1985 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1986}
1987
Alec Mouri80863a62019-01-17 15:19:35 -08001988TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferState) {
Valerie Hau0bc09152018-12-20 07:42:47 -08001989 sp<SurfaceControl> layer;
1990 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", mDisplayWidth, mDisplayHeight / 2,
1991 ISurfaceComposerClient::eFXSurfaceBufferState));
1992 sp<GraphicBuffer> buffer =
1993 new GraphicBuffer(mDisplayWidth, mDisplayHeight / 2, PIXEL_FORMAT_RGBA_8888, 1,
1994 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1995 BufferUsage::COMPOSER_OVERLAY,
1996 "test");
1997 fillGraphicBufferColor(buffer, Rect(0, 0, mDisplayWidth, mDisplayHeight / 4), Color::BLUE);
1998 fillGraphicBufferColor(buffer, Rect(0, mDisplayHeight / 4, mDisplayWidth, mDisplayHeight / 2),
1999 Color::RED);
2000
2001 Transaction().setBuffer(layer, buffer).apply();
2002
2003 // Partially out of bounds in the negative (upper left) direction
2004 Transaction().setCrop(layer, Rect(-128, -128, mDisplayWidth, mDisplayHeight / 4)).apply();
2005 {
2006 SCOPED_TRACE("out of bounds, negative (upper left) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08002007 auto shot = getScreenCapture();
Valerie Hau0bc09152018-12-20 07:42:47 -08002008 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::BLUE);
2009 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::BLACK);
2010 }
2011
2012 // Partially out of bounds in the positive (lower right) direction
2013 Transaction()
2014 .setCrop(layer, Rect(0, mDisplayHeight / 4, mDisplayWidth + 1, mDisplayHeight))
2015 .apply();
2016 {
2017 SCOPED_TRACE("out of bounds, positive (lower right) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08002018 auto shot = getScreenCapture();
Valerie Hau0bc09152018-12-20 07:42:47 -08002019 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::RED);
2020 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::BLACK);
2021 }
2022
2023 // Fully out of buffer space bounds
2024 Transaction().setCrop(layer, Rect(-128, -128, -1, -1)).apply();
2025 {
2026 SCOPED_TRACE("Fully out of bounds");
Alec Mouri80863a62019-01-17 15:19:35 -08002027 auto shot = getScreenCapture();
Valerie Hau0bc09152018-12-20 07:42:47 -08002028 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight / 4), Color::BLUE);
2029 shot->expectColor(Rect(0, mDisplayHeight / 4, mDisplayWidth, mDisplayHeight / 2),
2030 Color::RED);
2031 }
2032}
2033
Alec Mouri80863a62019-01-17 15:19:35 -08002034TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002035 sp<SurfaceControl> layer;
2036 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002037 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002038
2039 const Point position(32, 32);
2040 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002041 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002042 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002043 shot->expectColor(crop + position, Color::RED);
2044 shot->expectBorder(crop + position, Color::BLACK);
2045}
2046
Alec Mouri80863a62019-01-17 15:19:35 -08002047TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002048 sp<SurfaceControl> layer;
2049 ASSERT_NO_FATAL_FAILURE(
2050 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2051 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2052
Marissa Wall861616d2018-10-22 12:52:23 -07002053 const Rect frame(32, 32, 64, 64);
Marissa Wall61c58622018-07-18 10:12:20 -07002054 const Rect crop(8, 8, 24, 24);
Marissa Wall861616d2018-10-22 12:52:23 -07002055 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002056 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002057 shot->expectColor(frame, Color::RED);
2058 shot->expectBorder(frame, Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002059}
2060
Alec Mouri80863a62019-01-17 15:19:35 -08002061TEST_P(LayerRenderTypeTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002062 sp<SurfaceControl> layer;
2063 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002064 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002065
Marissa Wall861616d2018-10-22 12:52:23 -07002066 // crop_legacy is affected by matrix
Chia-I Wu04dcca82017-11-02 08:30:27 -07002067 Transaction()
2068 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07002069 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07002070 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002071 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002072 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2073 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2074}
2075
Alec Mouri80863a62019-01-17 15:19:35 -08002076TEST_P(LayerRenderTypeTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002077 sp<SurfaceControl> layer;
2078 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002079 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002080
Marissa Wallf58c14b2018-07-24 10:50:43 -07002081 // setCrop_legacy is applied immediately by default, with or without resize pending
2082 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002083 {
2084 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002085 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002086 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
2087 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
2088 }
2089
Marissa Wall61c58622018-07-18 10:12:20 -07002090 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002091 {
2092 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002093 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002094 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
2095 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
2096 }
2097}
2098
Alec Mouri80863a62019-01-17 15:19:35 -08002099TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002100 sp<SurfaceControl> layer;
2101 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002102 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002103
Marissa Wallf58c14b2018-07-24 10:50:43 -07002104 // request setCrop_legacy to be applied with the next resize
2105 Transaction()
2106 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
2107 .setGeometryAppliesWithResize(layer)
2108 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002109 {
2110 SCOPED_TRACE("waiting for next resize");
Alec Mouri80863a62019-01-17 15:19:35 -08002111 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002112 }
2113
Marissa Wallf58c14b2018-07-24 10:50:43 -07002114 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002115 {
2116 SCOPED_TRACE("pending crop modified");
Alec Mouri80863a62019-01-17 15:19:35 -08002117 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002118 }
2119
2120 Transaction().setSize(layer, 16, 16).apply();
2121 {
2122 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002123 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002124 }
2125
2126 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07002127 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002128 {
2129 SCOPED_TRACE("new crop applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002130 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002131 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2132 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2133 }
2134}
2135
Alec Mouri80863a62019-01-17 15:19:35 -08002136TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002137 sp<SurfaceControl> layer;
2138 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002139 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002140
Marissa Wallf58c14b2018-07-24 10:50:43 -07002141 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07002142 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07002143 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07002144 .setSize(layer, 16, 16)
2145 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
2146 .setGeometryAppliesWithResize(layer)
2147 .apply();
2148 {
2149 SCOPED_TRACE("new crop pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002150 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002151 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
2152 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
2153 }
2154
2155 // XXX crop is never latched without other geometry change (b/69315677)
2156 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002157 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002158 Transaction().setPosition(layer, 0, 0).apply();
2159 {
2160 SCOPED_TRACE("new crop applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002161 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002162 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2163 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2164 }
2165}
2166
Alec Mouri80863a62019-01-17 15:19:35 -08002167TEST_P(LayerRenderTypeTransactionTest, SetFrameBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002168 sp<SurfaceControl> layer;
2169 ASSERT_NO_FATAL_FAILURE(
2170 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2171 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2172 const Rect frame(8, 8, 24, 24);
2173
2174 Transaction().setFrame(layer, frame).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002175 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002176 shot->expectColor(frame, Color::RED);
2177 shot->expectBorder(frame, Color::BLACK);
2178}
2179
Alec Mouri80863a62019-01-17 15:19:35 -08002180TEST_P(LayerRenderTypeTransactionTest, SetFrameEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002181 sp<SurfaceControl> layer;
2182 ASSERT_NO_FATAL_FAILURE(
2183 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2184 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2185
Marissa Wall61c58622018-07-18 10:12:20 -07002186 {
Marissa Wall861616d2018-10-22 12:52:23 -07002187 SCOPED_TRACE("empty rect");
2188 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002189 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002190 }
2191
Marissa Wall61c58622018-07-18 10:12:20 -07002192 {
Marissa Wall861616d2018-10-22 12:52:23 -07002193 SCOPED_TRACE("negative rect");
2194 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002195 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002196 }
2197}
2198
Alec Mouri80863a62019-01-17 15:19:35 -08002199TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultParentless_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002200 sp<SurfaceControl> layer;
2201 ASSERT_NO_FATAL_FAILURE(
2202 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2203 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
2204
2205 // A parentless layer will default to a frame with the same size as the buffer
Alec Mouri80863a62019-01-17 15:19:35 -08002206 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002207 shot->expectColor(Rect(0, 0, 10, 10), Color::RED);
2208 shot->expectBorder(Rect(0, 0, 10, 10), Color::BLACK);
2209}
2210
Alec Mouri80863a62019-01-17 15:19:35 -08002211TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBSParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002212 sp<SurfaceControl> parent, child;
2213 ASSERT_NO_FATAL_FAILURE(
2214 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2215 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2216 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2217
2218 ASSERT_NO_FATAL_FAILURE(
2219 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2220 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2221
2222 Transaction().reparent(child, parent->getHandle()).apply();
2223
2224 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002225 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002226 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2227 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2228}
2229
Alec Mouri80863a62019-01-17 15:19:35 -08002230TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBQParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002231 sp<SurfaceControl> parent, child;
2232 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
2233 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
2234
2235 ASSERT_NO_FATAL_FAILURE(
2236 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2237 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2238
2239 Transaction().reparent(child, parent->getHandle()).apply();
2240
2241 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002242 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002243 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2244 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2245}
2246
Alec Mouri80863a62019-01-17 15:19:35 -08002247TEST_P(LayerRenderTypeTransactionTest, SetFrameUpdate_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002248 sp<SurfaceControl> layer;
2249 ASSERT_NO_FATAL_FAILURE(
2250 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2251 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2252 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
2253
2254 std::this_thread::sleep_for(500ms);
2255
2256 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
2257
Alec Mouri80863a62019-01-17 15:19:35 -08002258 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002259 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2260 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2261}
2262
Alec Mouri80863a62019-01-17 15:19:35 -08002263TEST_P(LayerRenderTypeTransactionTest, SetFrameOutsideBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002264 sp<SurfaceControl> parent, child;
2265 ASSERT_NO_FATAL_FAILURE(
2266 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2267 ASSERT_NO_FATAL_FAILURE(
2268 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2269 Transaction().reparent(child, parent->getHandle()).apply();
2270
2271 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2272 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2273
2274 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2275 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
2276
Alec Mouri80863a62019-01-17 15:19:35 -08002277 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002278 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
2279 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
2280 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2281}
2282
Alec Mouri80863a62019-01-17 15:19:35 -08002283TEST_P(LayerRenderTypeTransactionTest, SetBufferBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002284 sp<SurfaceControl> layer;
2285 ASSERT_NO_FATAL_FAILURE(
2286 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2287
2288 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2289
Alec Mouri80863a62019-01-17 15:19:35 -08002290 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002291 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2292 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2293}
2294
Alec Mouri80863a62019-01-17 15:19:35 -08002295TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleBuffers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002296 sp<SurfaceControl> layer;
2297 ASSERT_NO_FATAL_FAILURE(
2298 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2299
2300 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2301
2302 {
2303 SCOPED_TRACE("set buffer 1");
Alec Mouri80863a62019-01-17 15:19:35 -08002304 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002305 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2306 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2307 }
2308
2309 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
2310
2311 {
2312 SCOPED_TRACE("set buffer 2");
Alec Mouri80863a62019-01-17 15:19:35 -08002313 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002314 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2315 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2316 }
2317
2318 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2319
2320 {
2321 SCOPED_TRACE("set buffer 3");
Alec Mouri80863a62019-01-17 15:19:35 -08002322 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002323 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2324 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2325 }
2326}
2327
Alec Mouri80863a62019-01-17 15:19:35 -08002328TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleLayers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002329 sp<SurfaceControl> layer1;
2330 ASSERT_NO_FATAL_FAILURE(
2331 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2332
2333 sp<SurfaceControl> layer2;
2334 ASSERT_NO_FATAL_FAILURE(
2335 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2336
2337 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2338
Marissa Wall861616d2018-10-22 12:52:23 -07002339 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002340 {
2341 SCOPED_TRACE("set layer 1 buffer red");
Alec Mouri80863a62019-01-17 15:19:35 -08002342 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002343 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2344 }
2345
2346 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2347
Marissa Wall861616d2018-10-22 12:52:23 -07002348 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002349 {
2350 SCOPED_TRACE("set layer 2 buffer blue");
Alec Mouri80863a62019-01-17 15:19:35 -08002351 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002352 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2353 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2354 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2355 }
2356
2357 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2358 {
2359 SCOPED_TRACE("set layer 1 buffer green");
Alec Mouri80863a62019-01-17 15:19:35 -08002360 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002361 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2362 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2363 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2364 }
2365
2366 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2367
2368 {
2369 SCOPED_TRACE("set layer 2 buffer white");
Alec Mouri80863a62019-01-17 15:19:35 -08002370 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002371 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2372 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2373 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2374 }
2375}
2376
Marissa Wall73411622019-01-25 10:45:41 -08002377TEST_F(LayerRenderTypeTransactionTest, SetBufferCaching_BufferState) {
2378 sp<SurfaceControl> layer;
2379 ASSERT_NO_FATAL_FAILURE(
2380 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2381
2382 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2383
2384 std::array<sp<GraphicBuffer>, 10> buffers;
2385
2386 size_t idx = 0;
2387 for (auto& buffer : buffers) {
2388 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2389 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2390 BufferUsage::COMPOSER_OVERLAY,
2391 "test");
2392 Color color = colors[idx % colors.size()];
2393 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2394 idx++;
2395 }
2396
2397 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2398 // cache is working.
2399 idx = 0;
2400 for (auto& buffer : buffers) {
2401 for (int i = 0; i < 2; i++) {
2402 Transaction().setBuffer(layer, buffer).apply();
2403
2404 Color color = colors[idx % colors.size()];
2405 auto shot = screenshot();
2406 shot->expectColor(Rect(0, 0, 32, 32), color);
2407 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2408 }
2409 idx++;
2410 }
2411}
2412
2413TEST_F(LayerRenderTypeTransactionTest, SetBufferCaching_LeastRecentlyUsed_BufferState) {
2414 sp<SurfaceControl> layer;
2415 ASSERT_NO_FATAL_FAILURE(
2416 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2417
2418 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2419
2420 std::array<sp<GraphicBuffer>, 70> buffers;
2421
2422 size_t idx = 0;
2423 for (auto& buffer : buffers) {
2424 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2425 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2426 BufferUsage::COMPOSER_OVERLAY,
2427 "test");
2428 Color color = colors[idx % colors.size()];
2429 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2430 idx++;
2431 }
2432
2433 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2434 // cache is working.
2435 idx = 0;
2436 for (auto& buffer : buffers) {
2437 for (int i = 0; i < 2; i++) {
2438 Transaction().setBuffer(layer, buffer).apply();
2439
2440 Color color = colors[idx % colors.size()];
2441 auto shot = screenshot();
2442 shot->expectColor(Rect(0, 0, 32, 32), color);
2443 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2444 }
2445 idx++;
2446 }
2447}
2448
2449TEST_F(LayerRenderTypeTransactionTest, SetBufferCaching_DestroyedBuffer_BufferState) {
2450 sp<SurfaceControl> layer;
2451 ASSERT_NO_FATAL_FAILURE(
2452 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2453
2454 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2455
2456 std::array<sp<GraphicBuffer>, 65> buffers;
2457
2458 size_t idx = 0;
2459 for (auto& buffer : buffers) {
2460 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2461 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2462 BufferUsage::COMPOSER_OVERLAY,
2463 "test");
2464 Color color = colors[idx % colors.size()];
2465 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2466 idx++;
2467 }
2468
2469 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2470 // cache is working.
2471 idx = 0;
2472 for (auto& buffer : buffers) {
2473 for (int i = 0; i < 2; i++) {
2474 Transaction().setBuffer(layer, buffer).apply();
2475
2476 Color color = colors[idx % colors.size()];
2477 auto shot = screenshot();
2478 shot->expectColor(Rect(0, 0, 32, 32), color);
2479 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2480 }
2481 if (idx == 0) {
2482 buffers[0].clear();
2483 }
2484 idx++;
2485 }
2486}
2487
Alec Mouri80863a62019-01-17 15:19:35 -08002488TEST_P(LayerRenderTypeTransactionTest, SetTransformRotate90_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002489 sp<SurfaceControl> layer;
2490 ASSERT_NO_FATAL_FAILURE(
2491 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2492
2493 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2494 Color::BLUE, Color::WHITE));
2495
Marissa Wall861616d2018-10-22 12:52:23 -07002496 Transaction()
2497 .setFrame(layer, Rect(0, 0, 32, 32))
2498 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2499 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002500
Alec Mouri80863a62019-01-17 15:19:35 -08002501 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2502 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002503}
2504
Alec Mouri80863a62019-01-17 15:19:35 -08002505TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipH_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002506 sp<SurfaceControl> layer;
2507 ASSERT_NO_FATAL_FAILURE(
2508 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2509
2510 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2511 Color::BLUE, Color::WHITE));
2512
Marissa Wall861616d2018-10-22 12:52:23 -07002513 Transaction()
2514 .setFrame(layer, Rect(0, 0, 32, 32))
2515 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2516 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002517
Alec Mouri80863a62019-01-17 15:19:35 -08002518 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2519 Color::BLUE, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002520}
2521
Alec Mouri80863a62019-01-17 15:19:35 -08002522TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipV_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002523 sp<SurfaceControl> layer;
2524 ASSERT_NO_FATAL_FAILURE(
2525 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2526
2527 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2528 Color::BLUE, Color::WHITE));
2529
Marissa Wall861616d2018-10-22 12:52:23 -07002530 Transaction()
2531 .setFrame(layer, Rect(0, 0, 32, 32))
2532 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2533 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002534
Alec Mouri80863a62019-01-17 15:19:35 -08002535 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2536 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002537}
2538
2539TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2540 sp<SurfaceControl> layer;
2541 ASSERT_NO_FATAL_FAILURE(
2542 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2543
2544 Transaction().setTransformToDisplayInverse(layer, false).apply();
2545
2546 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2547
2548 Transaction().setTransformToDisplayInverse(layer, true).apply();
2549}
2550
Alec Mouri80863a62019-01-17 15:19:35 -08002551TEST_P(LayerRenderTypeTransactionTest, SetFenceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002552 sp<SurfaceControl> layer;
Marissa Wall713b63f2018-10-17 15:42:43 -07002553 Transaction transaction;
2554 ASSERT_NO_FATAL_FAILURE(
2555 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2556
2557 sp<GraphicBuffer> buffer =
2558 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2559 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2560 BufferUsage::COMPOSER_OVERLAY,
2561 "test");
2562 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2563
2564 sp<Fence> fence;
2565 if (getBuffer(nullptr, &fence) != NO_ERROR) {
2566 GTEST_SUCCEED() << "test not supported";
2567 return;
2568 }
2569
2570 Transaction().setBuffer(layer, buffer).setAcquireFence(layer, fence).apply();
2571
2572 status_t status = fence->wait(1000);
2573 ASSERT_NE(static_cast<status_t>(Fence::Status::Unsignaled), status);
2574 std::this_thread::sleep_for(200ms);
2575
Alec Mouri80863a62019-01-17 15:19:35 -08002576 auto shot = getScreenCapture();
Marissa Wall713b63f2018-10-17 15:42:43 -07002577 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2578 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2579}
2580
Alec Mouri80863a62019-01-17 15:19:35 -08002581TEST_P(LayerRenderTypeTransactionTest, SetFenceNull_BufferState) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002582 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07002583 ASSERT_NO_FATAL_FAILURE(
2584 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2585
2586 sp<GraphicBuffer> buffer =
2587 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2588 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2589 BufferUsage::COMPOSER_OVERLAY,
2590 "test");
2591 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2592
Marissa Wallfda30bb2018-10-12 11:34:28 -07002593 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002594
2595 Transaction()
2596 .setBuffer(layer, buffer)
2597 .setAcquireFence(layer, fence)
Marissa Wall61c58622018-07-18 10:12:20 -07002598 .apply();
2599
Alec Mouri80863a62019-01-17 15:19:35 -08002600 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002601 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2602 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2603}
2604
Valerie Haua72e2812019-01-23 13:40:39 -08002605TEST_P(LayerRenderTypeTransactionTest, SetColorDataspace_ColorLayer_NoEffect) {
2606 sp<SurfaceControl> layer;
2607 ASSERT_NO_FATAL_FAILURE(
2608 layer = createLayer("test", 0, 0, ISurfaceComposerClient::eFXSurfaceColor));
2609 half3 color;
2610 color.r = 1.0f;
2611 color.g = 0.0f;
2612 color.b = 0.0f;
2613 Transaction()
2614 .setCrop_legacy(layer, Rect(0, 0, 32, 32))
2615 .setColor(layer, color)
2616 .setDataspace(layer, ui::Dataspace::UNKNOWN)
2617 .setColorDataspace(layer, ui::Dataspace::BT2020_ITU)
2618 .apply();
2619
2620 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
2621}
2622
2623TEST_P(LayerRenderTypeTransactionTest, SetColorDataspace_BufferQueue_NoEffect) {
2624 sp<SurfaceControl> layer;
2625 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2626 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2627
2628 Transaction()
2629 .setDataspace(layer, ui::Dataspace::UNKNOWN)
2630 .setColorDataspace(layer, ui::Dataspace::BT2020_ITU)
2631 .apply();
2632
2633 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
2634}
2635
2636TEST_P(LayerRenderTypeTransactionTest, SetColorDataspace_BufferState_ColorLayer) {
2637 sp<SurfaceControl> layer;
2638 ASSERT_NO_FATAL_FAILURE(
2639 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2640
2641 half3 color;
2642 color.r = 1.0f;
2643 color.g = 0.0f;
2644 color.b = 0.0f;
2645 Transaction()
2646 .setFrame(layer, Rect(0, 0, 32, 32))
2647 .setColor(layer, color)
2648 .setColorDataspace(layer, ui::Dataspace::BT2020_ITU)
2649 .apply();
2650 auto shot = screenshot();
2651 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2652 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2653}
2654
2655TEST_P(LayerRenderTypeTransactionTest, SetColorDataspace_BufferState_NoColorLayer) {
2656 sp<SurfaceControl> layer;
2657 ASSERT_NO_FATAL_FAILURE(
2658 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2659 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2660
2661 Transaction()
2662 .setFrame(layer, Rect(0, 0, 32, 32))
2663 .setDataspace(layer, ui::Dataspace::UNKNOWN)
2664 .setColorDataspace(layer, ui::Dataspace::DCI_P3)
2665 .apply();
2666
2667 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
2668}
2669
Alec Mouri80863a62019-01-17 15:19:35 -08002670TEST_P(LayerRenderTypeTransactionTest, SetDataspaceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002671 sp<SurfaceControl> layer;
2672 ASSERT_NO_FATAL_FAILURE(
2673 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2674
2675 sp<GraphicBuffer> buffer =
2676 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2677 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2678 BufferUsage::COMPOSER_OVERLAY,
2679 "test");
2680 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2681
2682 Transaction()
2683 .setBuffer(layer, buffer)
2684 .setDataspace(layer, ui::Dataspace::UNKNOWN)
Marissa Wall61c58622018-07-18 10:12:20 -07002685 .apply();
2686
Alec Mouri80863a62019-01-17 15:19:35 -08002687 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002688 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2689 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2690}
2691
Alec Mouri80863a62019-01-17 15:19:35 -08002692TEST_P(LayerRenderTypeTransactionTest, SetHdrMetadataBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002693 sp<SurfaceControl> layer;
2694 ASSERT_NO_FATAL_FAILURE(
2695 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2696
2697 sp<GraphicBuffer> buffer =
2698 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2699 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2700 BufferUsage::COMPOSER_OVERLAY,
2701 "test");
2702 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2703
2704 HdrMetadata hdrMetadata;
2705 hdrMetadata.validTypes = 0;
2706 Transaction()
2707 .setBuffer(layer, buffer)
2708 .setHdrMetadata(layer, hdrMetadata)
Marissa Wall61c58622018-07-18 10:12:20 -07002709 .apply();
2710
Alec Mouri80863a62019-01-17 15:19:35 -08002711 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002712 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2713 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2714}
2715
Alec Mouri80863a62019-01-17 15:19:35 -08002716TEST_P(LayerRenderTypeTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002717 sp<SurfaceControl> layer;
2718 ASSERT_NO_FATAL_FAILURE(
2719 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2720
2721 sp<GraphicBuffer> buffer =
2722 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2723 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2724 BufferUsage::COMPOSER_OVERLAY,
2725 "test");
2726 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2727
2728 Region region;
2729 region.set(32, 32);
2730 Transaction()
2731 .setBuffer(layer, buffer)
2732 .setSurfaceDamageRegion(layer, region)
Marissa Wall61c58622018-07-18 10:12:20 -07002733 .apply();
2734
Alec Mouri80863a62019-01-17 15:19:35 -08002735 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002736 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2737 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2738}
2739
Alec Mouri80863a62019-01-17 15:19:35 -08002740TEST_P(LayerRenderTypeTransactionTest, SetApiBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002741 sp<SurfaceControl> layer;
2742 ASSERT_NO_FATAL_FAILURE(
2743 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2744
2745 sp<GraphicBuffer> buffer =
2746 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2747 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2748 BufferUsage::COMPOSER_OVERLAY,
2749 "test");
2750 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2751
2752 Transaction()
2753 .setBuffer(layer, buffer)
2754 .setApi(layer, NATIVE_WINDOW_API_CPU)
Marissa Wall61c58622018-07-18 10:12:20 -07002755 .apply();
2756
Alec Mouri80863a62019-01-17 15:19:35 -08002757 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002758 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2759 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2760}
2761
2762TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2763 sp<SurfaceControl> layer;
2764 ASSERT_NO_FATAL_FAILURE(
2765 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2766
2767 // verify this doesn't cause a crash
2768 Transaction().setSidebandStream(layer, nullptr).apply();
2769}
2770
Robert Carr54cf5b12019-01-25 14:02:28 -08002771TEST_F(LayerTransactionTest, ReparentToSelf) {
2772 sp<SurfaceControl> layer;
2773 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2774 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2775 Transaction().reparent(layer, layer->getHandle()).apply();
2776
2777 {
2778 // We expect the transaction to be silently dropped, but for SurfaceFlinger
2779 // to still be functioning.
2780 SCOPED_TRACE("after reparent to self");
2781 const Rect rect(0, 0, 32, 32);
2782 auto shot = screenshot();
2783 shot->expectColor(rect, Color::RED);
2784 shot->expectBorder(rect, Color::BLACK);
2785 }
2786}
2787
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002788class ColorTransformHelper {
2789public:
2790 static void DegammaColorSingle(half& s) {
2791 if (s <= 0.03928f)
2792 s = s / 12.92f;
2793 else
2794 s = pow((s + 0.055f) / 1.055f, 2.4f);
2795 }
2796
2797 static void DegammaColor(half3& color) {
2798 DegammaColorSingle(color.r);
2799 DegammaColorSingle(color.g);
2800 DegammaColorSingle(color.b);
2801 }
2802
2803 static void GammaColorSingle(half& s) {
2804 if (s <= 0.0031308f) {
2805 s = s * 12.92f;
2806 } else {
2807 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2808 }
2809 }
2810
2811 static void GammaColor(half3& color) {
2812 GammaColorSingle(color.r);
2813 GammaColorSingle(color.g);
2814 GammaColorSingle(color.b);
2815 }
2816
2817 static void applyMatrix(half3& color, const mat3& mat) {
2818 half3 ret = half3(0);
2819
2820 for (int i = 0; i < 3; i++) {
2821 for (int j = 0; j < 3; j++) {
2822 ret[i] = ret[i] + color[j] * mat[j][i];
2823 }
2824 }
2825 color = ret;
2826 }
2827};
2828
Alec Mouri80863a62019-01-17 15:19:35 -08002829TEST_P(LayerRenderTypeTransactionTest, SetColorTransformBasic) {
Peiyong Lind3788632018-09-18 16:01:31 -07002830 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002831 ASSERT_NO_FATAL_FAILURE(colorLayer =
2832 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2833 ISurfaceComposerClient::eFXSurfaceColor));
2834 Transaction()
2835 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2836 .setLayer(colorLayer, mLayerZBase + 1)
2837 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002838 {
2839 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002840 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Peiyong Lind3788632018-09-18 16:01:31 -07002841 }
2842
2843 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002844 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002845 mat3 matrix;
2846 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2847 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2848 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002849
2850 // degamma before applying the matrix
2851 if (mColorManagementUsed) {
2852 ColorTransformHelper::DegammaColor(expected);
2853 }
2854
2855 ColorTransformHelper::applyMatrix(expected, matrix);
2856
2857 if (mColorManagementUsed) {
2858 ColorTransformHelper::GammaColor(expected);
2859 }
2860
2861 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2862 uint8_t(expected.b * 255), 255};
2863
2864 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2865 // channel) should be less than one
2866 const uint8_t tolerance = 1;
2867
Peiyong Lind3788632018-09-18 16:01:31 -07002868 Transaction().setColor(colorLayer, color)
2869 .setColorTransform(colorLayer, matrix, vec3()).apply();
2870 {
2871 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002872 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002873 }
2874}
2875
Alec Mouri80863a62019-01-17 15:19:35 -08002876TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnParent) {
chaviwf66724d2018-11-28 16:35:21 -08002877 sp<SurfaceControl> parentLayer;
2878 sp<SurfaceControl> colorLayer;
2879 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2880 0 /* buffer height */,
2881 ISurfaceComposerClient::eFXSurfaceContainer));
2882 ASSERT_NO_FATAL_FAILURE(
2883 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2884 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2885
2886 Transaction()
2887 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2888 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2889 .setLayer(parentLayer, mLayerZBase + 1)
2890 .apply();
2891 {
2892 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002893 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08002894 }
2895
2896 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2897 half3 expected = color;
2898 mat3 matrix;
2899 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2900 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2901 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2902
2903 // degamma before applying the matrix
2904 if (mColorManagementUsed) {
2905 ColorTransformHelper::DegammaColor(expected);
2906 }
2907
2908 ColorTransformHelper::applyMatrix(expected, matrix);
2909
2910 if (mColorManagementUsed) {
2911 ColorTransformHelper::GammaColor(expected);
2912 }
2913
2914 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2915 uint8_t(expected.b * 255), 255};
2916
2917 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2918 // channel) should be less than one
2919 const uint8_t tolerance = 1;
2920
2921 Transaction()
2922 .setColor(colorLayer, color)
2923 .setColorTransform(parentLayer, matrix, vec3())
2924 .apply();
2925 {
2926 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002927 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08002928 }
2929}
2930
Alec Mouri80863a62019-01-17 15:19:35 -08002931TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnChildAndParent) {
chaviwf66724d2018-11-28 16:35:21 -08002932 sp<SurfaceControl> parentLayer;
2933 sp<SurfaceControl> colorLayer;
2934 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2935 0 /* buffer height */,
2936 ISurfaceComposerClient::eFXSurfaceContainer));
2937 ASSERT_NO_FATAL_FAILURE(
2938 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2939 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2940
2941 Transaction()
2942 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2943 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2944 .setLayer(parentLayer, mLayerZBase + 1)
2945 .apply();
2946 {
2947 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002948 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08002949 }
2950
2951 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2952 half3 expected = color;
2953 mat3 matrixChild;
2954 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
2955 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
2956 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
2957 mat3 matrixParent;
2958 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
2959 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
2960 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
2961
2962 // degamma before applying the matrix
2963 if (mColorManagementUsed) {
2964 ColorTransformHelper::DegammaColor(expected);
2965 }
2966
2967 ColorTransformHelper::applyMatrix(expected, matrixChild);
2968 ColorTransformHelper::applyMatrix(expected, matrixParent);
2969
2970 if (mColorManagementUsed) {
2971 ColorTransformHelper::GammaColor(expected);
2972 }
2973
2974 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2975 uint8_t(expected.b * 255), 255};
2976
2977 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2978 // channel) should be less than one
2979 const uint8_t tolerance = 1;
2980
2981 Transaction()
2982 .setColor(colorLayer, color)
2983 .setColorTransform(parentLayer, matrixParent, vec3())
2984 .setColorTransform(colorLayer, matrixChild, vec3())
2985 .apply();
2986 {
2987 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002988 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08002989 }
2990}
2991
Marissa Wall80d94ad2019-01-18 16:04:36 -08002992struct CallbackData {
2993 CallbackData() = default;
2994 CallbackData(nsecs_t time, const sp<Fence>& fence,
2995 const std::vector<SurfaceControlStats>& stats)
2996 : latchTime(time), presentFence(fence), surfaceControlStats(stats) {}
2997
2998 nsecs_t latchTime;
2999 sp<Fence> presentFence;
3000 std::vector<SurfaceControlStats> surfaceControlStats;
3001};
3002
Marissa Wallfda30bb2018-10-12 11:34:28 -07003003class ExpectedResult {
3004public:
3005 enum Transaction {
3006 NOT_PRESENTED = 0,
3007 PRESENTED,
3008 };
3009
3010 enum Buffer {
3011 NOT_ACQUIRED = 0,
3012 ACQUIRED,
3013 };
3014
3015 enum PreviousBuffer {
3016 NOT_RELEASED = 0,
3017 RELEASED,
Marissa Wall5a68a772018-12-22 17:43:42 -08003018 UNKNOWN,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003019 };
3020
3021 void reset() {
3022 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
3023 mExpectedSurfaceResults.clear();
3024 }
3025
3026 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07003027 ExpectedResult::Buffer bufferResult = ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003028 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
3029 mTransactionResult = transactionResult;
Marissa Wall80d94ad2019-01-18 16:04:36 -08003030 mExpectedSurfaceResults.emplace(std::piecewise_construct, std::forward_as_tuple(layer),
Marissa Wallfda30bb2018-10-12 11:34:28 -07003031 std::forward_as_tuple(bufferResult, previousBufferResult));
3032 }
3033
3034 void addSurfaces(ExpectedResult::Transaction transactionResult,
3035 const std::vector<sp<SurfaceControl>>& layers,
Marissa Wall713b63f2018-10-17 15:42:43 -07003036 ExpectedResult::Buffer bufferResult = ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003037 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
3038 for (const auto& layer : layers) {
3039 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
3040 }
3041 }
3042
Marissa Wall17b4e452018-12-26 16:32:34 -08003043 void addExpectedPresentTime(nsecs_t expectedPresentTime) {
3044 mExpectedPresentTime = expectedPresentTime;
3045 }
3046
Marissa Wall80d94ad2019-01-18 16:04:36 -08003047 void verifyCallbackData(const CallbackData& callbackData) const {
3048 const auto& [latchTime, presentFence, surfaceControlStats] = callbackData;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003049 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
3050 ASSERT_GE(latchTime, 0) << "bad latch time";
Valerie Hau63258a12018-12-14 14:31:48 -08003051 ASSERT_NE(presentFence, nullptr);
Marissa Wall17b4e452018-12-26 16:32:34 -08003052 if (mExpectedPresentTime >= 0) {
3053 ASSERT_EQ(presentFence->wait(3000), NO_ERROR);
3054 ASSERT_GE(presentFence->getSignalTime(), mExpectedPresentTime - nsecs_t(5 * 1e6));
3055 // if the panel is running at 30 hz, at the worst case, our expected time just
3056 // misses vsync and we have to wait another 33.3ms
3057 ASSERT_LE(presentFence->getSignalTime(),
3058 mExpectedPresentTime + nsecs_t(66.666666 * 1e6));
3059 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003060 } else {
Valerie Hau63258a12018-12-14 14:31:48 -08003061 ASSERT_EQ(presentFence, nullptr) << "transaction shouldn't have been presented";
Marissa Wallfda30bb2018-10-12 11:34:28 -07003062 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
3063 }
3064
Marissa Wall80d94ad2019-01-18 16:04:36 -08003065 ASSERT_EQ(surfaceControlStats.size(), mExpectedSurfaceResults.size())
Marissa Wallfda30bb2018-10-12 11:34:28 -07003066 << "wrong number of surfaces";
3067
Marissa Wall80d94ad2019-01-18 16:04:36 -08003068 for (const auto& stats : surfaceControlStats) {
3069 ASSERT_NE(stats.surfaceControl, nullptr) << "returned null surface control";
3070
Marissa Wallfda30bb2018-10-12 11:34:28 -07003071 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
3072 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
3073 << "unexpected surface control";
Marissa Wall80d94ad2019-01-18 16:04:36 -08003074 expectedSurfaceResult->second.verifySurfaceControlStats(stats, latchTime);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003075 }
3076 }
3077
3078private:
3079 class ExpectedSurfaceResult {
3080 public:
3081 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
3082 ExpectedResult::PreviousBuffer previousBufferResult)
3083 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
3084
Marissa Wall80d94ad2019-01-18 16:04:36 -08003085 void verifySurfaceControlStats(const SurfaceControlStats& surfaceControlStats,
3086 nsecs_t latchTime) const {
3087 const auto& [surfaceControl, acquireTime, previousReleaseFence] = surfaceControlStats;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003088
3089 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
3090 << "bad acquire time";
3091 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
Marissa Wall5a68a772018-12-22 17:43:42 -08003092
3093 if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED) {
3094 ASSERT_NE(previousReleaseFence, nullptr)
3095 << "failed to set release prev buffer fence";
3096 } else if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::NOT_RELEASED) {
3097 ASSERT_EQ(previousReleaseFence, nullptr)
3098 << "should not have set released prev buffer fence";
3099 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003100 }
3101
3102 private:
3103 ExpectedResult::Buffer mBufferResult;
3104 ExpectedResult::PreviousBuffer mPreviousBufferResult;
3105 };
3106
Marissa Wall80d94ad2019-01-18 16:04:36 -08003107 struct SCHash {
3108 std::size_t operator()(const sp<SurfaceControl>& sc) const {
3109 return std::hash<IBinder*>{}(sc->getHandle().get());
Marissa Wallfda30bb2018-10-12 11:34:28 -07003110 }
3111 };
3112 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
Marissa Wall17b4e452018-12-26 16:32:34 -08003113 nsecs_t mExpectedPresentTime = -1;
Marissa Wall80d94ad2019-01-18 16:04:36 -08003114 std::unordered_map<sp<SurfaceControl>, ExpectedSurfaceResult, SCHash> mExpectedSurfaceResults;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003115};
3116
3117class CallbackHelper {
3118public:
Marissa Wall80d94ad2019-01-18 16:04:36 -08003119 static void function(void* callbackContext, nsecs_t latchTime, const sp<Fence>& presentFence,
3120 const std::vector<SurfaceControlStats>& stats) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003121 if (!callbackContext) {
3122 ALOGE("failed to get callback context");
3123 }
3124 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
3125 std::lock_guard lock(helper->mMutex);
Marissa Wall80d94ad2019-01-18 16:04:36 -08003126 helper->mCallbackDataQueue.emplace(latchTime, presentFence, stats);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003127 helper->mConditionVariable.notify_all();
3128 }
3129
Marissa Wall80d94ad2019-01-18 16:04:36 -08003130 void getCallbackData(CallbackData* outData) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003131 std::unique_lock lock(mMutex);
3132
Marissa Wall80d94ad2019-01-18 16:04:36 -08003133 if (mCallbackDataQueue.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003134 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
3135 std::cv_status::timeout)
3136 << "did not receive callback";
3137 }
3138
Marissa Wall80d94ad2019-01-18 16:04:36 -08003139 *outData = std::move(mCallbackDataQueue.front());
3140 mCallbackDataQueue.pop();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003141 }
3142
3143 void verifyFinalState() {
3144 // Wait to see if there are extra callbacks
3145 std::this_thread::sleep_for(500ms);
3146
3147 std::lock_guard lock(mMutex);
Marissa Wall80d94ad2019-01-18 16:04:36 -08003148 EXPECT_EQ(mCallbackDataQueue.size(), 0) << "extra callbacks received";
3149 mCallbackDataQueue = {};
Marissa Wallfda30bb2018-10-12 11:34:28 -07003150 }
3151
3152 void* getContext() { return static_cast<void*>(this); }
3153
3154 std::mutex mMutex;
3155 std::condition_variable mConditionVariable;
Marissa Wall80d94ad2019-01-18 16:04:36 -08003156 std::queue<CallbackData> mCallbackDataQueue;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003157};
3158
3159class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07003160public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07003161 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07003162 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003163 }
3164
Marissa Wall713b63f2018-10-17 15:42:43 -07003165 static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
3166 const sp<SurfaceControl>& layer = nullptr) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003167 if (layer) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003168 sp<GraphicBuffer> buffer;
3169 sp<Fence> fence;
3170 int err = getBuffer(&buffer, &fence);
3171 if (err != NO_ERROR) {
3172 return err;
3173 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003174
Marissa Wall861616d2018-10-22 12:52:23 -07003175 transaction.setBuffer(layer, buffer).setAcquireFence(layer, fence);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003176 }
3177
3178 transaction.addTransactionCompletedCallback(callbackHelper->function,
3179 callbackHelper->getContext());
Marissa Wall713b63f2018-10-17 15:42:43 -07003180 return NO_ERROR;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003181 }
3182
Marissa Wall861616d2018-10-22 12:52:23 -07003183 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
3184 bool finalState = false) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08003185 CallbackData callbackData;
3186 ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
3187 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
Marissa Wallfda30bb2018-10-12 11:34:28 -07003188
3189 if (finalState) {
3190 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
3191 }
3192 }
3193
Marissa Wall861616d2018-10-22 12:52:23 -07003194 static void waitForCallbacks(CallbackHelper& helper,
3195 const std::vector<ExpectedResult>& expectedResults,
3196 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003197 for (const auto& expectedResult : expectedResults) {
3198 waitForCallback(helper, expectedResult);
3199 }
3200 if (finalState) {
3201 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
3202 }
3203 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003204};
3205
3206TEST_F(LayerCallbackTest, Basic) {
3207 sp<SurfaceControl> layer;
3208 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3209
3210 Transaction transaction;
3211 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003212 int err = fillTransaction(transaction, &callback, layer);
3213 if (err) {
3214 GTEST_SUCCEED() << "test not supported";
3215 return;
3216 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003217
3218 transaction.apply();
3219
3220 ExpectedResult expected;
3221 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3222 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3223}
3224
3225TEST_F(LayerCallbackTest, NoBuffer) {
3226 sp<SurfaceControl> layer;
3227 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3228
3229 Transaction transaction;
3230 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003231 int err = fillTransaction(transaction, &callback);
3232 if (err) {
3233 GTEST_SUCCEED() << "test not supported";
3234 return;
3235 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003236
Marissa Wall861616d2018-10-22 12:52:23 -07003237 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003238
3239 ExpectedResult expected;
Marissa Wall713b63f2018-10-17 15:42:43 -07003240 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
3241 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003242 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3243}
3244
3245TEST_F(LayerCallbackTest, NoStateChange) {
3246 Transaction transaction;
3247 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003248 int err = fillTransaction(transaction, &callback);
3249 if (err) {
3250 GTEST_SUCCEED() << "test not supported";
3251 return;
3252 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003253
3254 transaction.apply();
3255
3256 ExpectedResult expected;
3257 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3258}
3259
3260TEST_F(LayerCallbackTest, OffScreen) {
3261 sp<SurfaceControl> layer;
3262 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3263
3264 Transaction transaction;
3265 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003266 int err = fillTransaction(transaction, &callback, layer);
3267 if (err) {
3268 GTEST_SUCCEED() << "test not supported";
3269 return;
3270 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003271
Marissa Wall861616d2018-10-22 12:52:23 -07003272 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003273
3274 ExpectedResult expected;
3275 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3276 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3277}
3278
3279TEST_F(LayerCallbackTest, Merge) {
3280 sp<SurfaceControl> layer1, layer2;
3281 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3282 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3283
3284 Transaction transaction1, transaction2;
3285 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003286 int err = fillTransaction(transaction1, &callback1, layer1);
3287 if (err) {
3288 GTEST_SUCCEED() << "test not supported";
3289 return;
3290 }
3291 err = fillTransaction(transaction2, &callback2, layer2);
3292 if (err) {
3293 GTEST_SUCCEED() << "test not supported";
3294 return;
3295 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003296
Marissa Wall861616d2018-10-22 12:52:23 -07003297 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3298 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003299
3300 ExpectedResult expected;
3301 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3302 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3303 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3304}
3305
3306TEST_F(LayerCallbackTest, Merge_SameCallback) {
3307 sp<SurfaceControl> layer1, layer2;
3308 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3309 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3310
3311 Transaction transaction1, transaction2;
3312 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003313 int err = fillTransaction(transaction1, &callback, layer1);
3314 if (err) {
3315 GTEST_SUCCEED() << "test not supported";
3316 return;
3317 }
3318 err = fillTransaction(transaction2, &callback, layer2);
3319 if (err) {
3320 GTEST_SUCCEED() << "test not supported";
3321 return;
3322 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003323
3324 transaction2.merge(std::move(transaction1)).apply();
3325
3326 ExpectedResult expected;
3327 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3328 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3329 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3330}
3331
3332TEST_F(LayerCallbackTest, Merge_SameLayer) {
3333 sp<SurfaceControl> layer;
3334 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3335
3336 Transaction transaction1, transaction2;
3337 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003338 int err = fillTransaction(transaction1, &callback1, layer);
3339 if (err) {
3340 GTEST_SUCCEED() << "test not supported";
3341 return;
3342 }
3343 err = fillTransaction(transaction2, &callback2, layer);
3344 if (err) {
3345 GTEST_SUCCEED() << "test not supported";
3346 return;
3347 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003348
3349 transaction2.merge(std::move(transaction1)).apply();
3350
3351 ExpectedResult expected;
3352 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3353 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3354 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3355}
3356
Marissa Wallfda30bb2018-10-12 11:34:28 -07003357TEST_F(LayerCallbackTest, Merge_DifferentClients) {
3358 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3359 client2(new SurfaceComposerClient);
3360
3361 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3362 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3363
3364 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003365 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003366 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003367 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003368 ISurfaceComposerClient::eFXSurfaceBufferState));
3369
3370 Transaction transaction1, transaction2;
3371 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003372 int err = fillTransaction(transaction1, &callback1, layer1);
3373 if (err) {
3374 GTEST_SUCCEED() << "test not supported";
3375 return;
3376 }
3377 err = fillTransaction(transaction2, &callback2, layer2);
3378 if (err) {
3379 GTEST_SUCCEED() << "test not supported";
3380 return;
3381 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003382
Marissa Wall861616d2018-10-22 12:52:23 -07003383 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3384 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003385
3386 ExpectedResult expected;
3387 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3388 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3389 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3390}
3391
3392TEST_F(LayerCallbackTest, MultipleTransactions) {
3393 sp<SurfaceControl> layer;
3394 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3395
3396 Transaction transaction;
3397 CallbackHelper callback;
3398 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003399 int err = fillTransaction(transaction, &callback, layer);
3400 if (err) {
3401 GTEST_SUCCEED() << "test not supported";
3402 return;
3403 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003404
3405 transaction.apply();
3406
3407 ExpectedResult expected;
3408 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07003409 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003410 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3411 : ExpectedResult::PreviousBuffer::RELEASED);
3412 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3413 }
3414 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3415}
3416
3417TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
3418 sp<SurfaceControl> layer;
3419 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3420
3421 Transaction transaction;
3422 CallbackHelper callback;
3423 for (size_t i = 0; i < 10; i++) {
3424 ExpectedResult expected;
3425
3426 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003427 int err = fillTransaction(transaction, &callback, layer);
3428 if (err) {
3429 GTEST_SUCCEED() << "test not supported";
3430 return;
3431 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003432 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3433 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003434 int err = fillTransaction(transaction, &callback);
3435 if (err) {
3436 GTEST_SUCCEED() << "test not supported";
3437 return;
3438 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003439 }
3440
3441 transaction.apply();
3442
3443 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3444 }
3445 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3446}
3447
3448TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
3449 sp<SurfaceControl> layer;
3450 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3451
3452 Transaction transaction;
3453 CallbackHelper callback;
3454 for (size_t i = 0; i < 10; i++) {
3455 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003456 int err = fillTransaction(transaction, &callback, layer);
3457 if (err) {
3458 GTEST_SUCCEED() << "test not supported";
3459 return;
3460 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003461 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003462 int err = fillTransaction(transaction, &callback);
3463 if (err) {
3464 GTEST_SUCCEED() << "test not supported";
3465 return;
3466 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003467 }
3468
Marissa Wall861616d2018-10-22 12:52:23 -07003469 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003470
3471 ExpectedResult expected;
3472 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
3473 : ExpectedResult::Transaction::NOT_PRESENTED,
Marissa Wall713b63f2018-10-17 15:42:43 -07003474 layer,
3475 (i == 0) ? ExpectedResult::Buffer::ACQUIRED
3476 : ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003477 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
3478 }
3479 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3480}
3481
3482TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
3483 sp<SurfaceControl> layer1, layer2;
3484 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3485 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3486
3487 Transaction transaction1, transaction2;
3488 CallbackHelper callback1, callback2;
3489 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003490 int err = fillTransaction(transaction1, &callback1, layer1);
3491 if (err) {
3492 GTEST_SUCCEED() << "test not supported";
3493 return;
3494 }
3495 err = fillTransaction(transaction2, &callback2, layer2);
3496 if (err) {
3497 GTEST_SUCCEED() << "test not supported";
3498 return;
3499 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003500
Marissa Wall861616d2018-10-22 12:52:23 -07003501 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3502 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003503
3504 ExpectedResult expected;
3505 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003506 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003507 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3508 : ExpectedResult::PreviousBuffer::RELEASED);
3509 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3510 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3511 }
3512 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3513 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3514}
3515
3516TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
3517 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3518 client2(new SurfaceComposerClient);
3519 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3520 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3521
3522 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003523 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003524 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003525 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003526 ISurfaceComposerClient::eFXSurfaceBufferState));
3527
3528 Transaction transaction1, transaction2;
3529 CallbackHelper callback1, callback2;
3530 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003531 int err = fillTransaction(transaction1, &callback1, layer1);
3532 if (err) {
3533 GTEST_SUCCEED() << "test not supported";
3534 return;
3535 }
3536 err = fillTransaction(transaction2, &callback2, layer2);
3537 if (err) {
3538 GTEST_SUCCEED() << "test not supported";
3539 return;
3540 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003541
Marissa Wall861616d2018-10-22 12:52:23 -07003542 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3543 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003544
3545 ExpectedResult expected;
3546 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003547 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003548 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3549 : ExpectedResult::PreviousBuffer::RELEASED);
3550 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3551 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3552 }
3553 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3554 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3555}
3556
3557TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
3558 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3559 client2(new SurfaceComposerClient);
3560 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3561 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3562
3563 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003564 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003565 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003566 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003567 ISurfaceComposerClient::eFXSurfaceBufferState));
3568
3569 Transaction transaction1, transaction2;
3570 CallbackHelper callback1, callback2;
3571
3572 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003573 int err = fillTransaction(transaction1, &callback1, layer1);
3574 if (err) {
3575 GTEST_SUCCEED() << "test not supported";
3576 return;
3577 }
3578 err = fillTransaction(transaction2, &callback2, layer2);
3579 if (err) {
3580 GTEST_SUCCEED() << "test not supported";
3581 return;
3582 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003583
Marissa Wall861616d2018-10-22 12:52:23 -07003584 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3585 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003586
3587 ExpectedResult expected;
3588 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3589 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3590 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3591 expected.reset();
3592
3593 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003594 err = fillTransaction(transaction1, &callback1);
3595 if (err) {
3596 GTEST_SUCCEED() << "test not supported";
3597 return;
3598 }
3599 err = fillTransaction(transaction2, &callback2);
3600 if (err) {
3601 GTEST_SUCCEED() << "test not supported";
3602 return;
3603 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003604
3605 transaction2.merge(std::move(transaction1)).apply();
3606
3607 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3608 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3609}
3610
3611TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
3612 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3613 client2(new SurfaceComposerClient);
3614
3615 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3616 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3617
3618 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003619 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003620 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003621 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003622 ISurfaceComposerClient::eFXSurfaceBufferState));
3623
3624 Transaction transaction1, transaction2;
3625 CallbackHelper callback1, callback2;
3626
3627 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003628 int err = fillTransaction(transaction1, &callback1, layer1);
3629 if (err) {
3630 GTEST_SUCCEED() << "test not supported";
3631 return;
3632 }
3633 err = fillTransaction(transaction2, &callback2, layer2);
3634 if (err) {
3635 GTEST_SUCCEED() << "test not supported";
3636 return;
3637 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003638
Marissa Wall861616d2018-10-22 12:52:23 -07003639 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3640 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003641
3642 ExpectedResult expected;
3643 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3644 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3645 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3646 expected.reset();
3647
3648 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003649 err = fillTransaction(transaction1, &callback1);
3650 if (err) {
3651 GTEST_SUCCEED() << "test not supported";
3652 return;
3653 }
3654 err = fillTransaction(transaction2, &callback2);
3655 if (err) {
3656 GTEST_SUCCEED() << "test not supported";
3657 return;
3658 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003659
Marissa Wall861616d2018-10-22 12:52:23 -07003660 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003661
Marissa Wall713b63f2018-10-17 15:42:43 -07003662 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
3663 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003664 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3665 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3666}
3667
3668TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3669 sp<SurfaceControl> layer;
3670 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3671
3672 Transaction transaction;
3673 CallbackHelper callback;
3674 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003675 for (auto& expected : expectedResults) {
3676 expected.reset();
3677 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall5a68a772018-12-22 17:43:42 -08003678 ExpectedResult::Buffer::ACQUIRED,
3679 ExpectedResult::PreviousBuffer::UNKNOWN);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003680
Marissa Wall713b63f2018-10-17 15:42:43 -07003681 int err = fillTransaction(transaction, &callback, layer);
3682 if (err) {
3683 GTEST_SUCCEED() << "test not supported";
3684 return;
3685 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003686
3687 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003688 }
3689 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3690}
3691
3692TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3693 sp<SurfaceControl> layer;
3694 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3695
Marissa Wall713b63f2018-10-17 15:42:43 -07003696 // Normal call to set up test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003697 Transaction transaction;
3698 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003699 int err = fillTransaction(transaction, &callback, layer);
3700 if (err) {
3701 GTEST_SUCCEED() << "test not supported";
3702 return;
3703 }
3704
3705 transaction.apply();
3706
3707 ExpectedResult expected;
3708 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3709 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3710
3711 // Test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003712 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003713 for (auto& expected : expectedResults) {
3714 expected.reset();
3715
Marissa Wall713b63f2018-10-17 15:42:43 -07003716 err = fillTransaction(transaction, &callback);
3717 if (err) {
3718 GTEST_SUCCEED() << "test not supported";
3719 return;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003720 }
3721
3722 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003723 }
3724 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3725}
3726
3727TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3728 sp<SurfaceControl> layer;
3729 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3730
3731 // Normal call to set up test
3732 Transaction transaction;
3733 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003734 int err = fillTransaction(transaction, &callback, layer);
3735 if (err) {
3736 GTEST_SUCCEED() << "test not supported";
3737 return;
3738 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003739
Marissa Wall861616d2018-10-22 12:52:23 -07003740 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003741
3742 ExpectedResult expectedResult;
3743 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3744 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3745
3746 // Test
3747 std::vector<ExpectedResult> expectedResults(50);
3748 for (auto& expected : expectedResults) {
3749 expected.reset();
Marissa Wall713b63f2018-10-17 15:42:43 -07003750 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
3751 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003752
Marissa Wall713b63f2018-10-17 15:42:43 -07003753 err = fillTransaction(transaction, &callback);
3754 if (err) {
3755 GTEST_SUCCEED() << "test not supported";
3756 return;
3757 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003758
Marissa Wall861616d2018-10-22 12:52:23 -07003759 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003760 }
3761 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3762}
3763
Marissa Wall17b4e452018-12-26 16:32:34 -08003764TEST_F(LayerCallbackTest, DesiredPresentTime) {
3765 sp<SurfaceControl> layer;
3766 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3767
3768 Transaction transaction;
3769 CallbackHelper callback;
3770 int err = fillTransaction(transaction, &callback, layer);
3771 if (err) {
3772 GTEST_SUCCEED() << "test not supported";
3773 return;
3774 }
3775
3776 // Try to present 100ms in the future
3777 nsecs_t time = systemTime() + (100 * 1e6);
3778
3779 transaction.setDesiredPresentTime(time);
3780 transaction.apply();
3781
3782 ExpectedResult expected;
3783 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3784 expected.addExpectedPresentTime(time);
3785 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3786}
3787
3788TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
3789 sp<SurfaceControl> layer;
3790 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3791
3792 Transaction transaction;
3793 CallbackHelper callback1;
3794 int err = fillTransaction(transaction, &callback1, layer);
3795 if (err) {
3796 GTEST_SUCCEED() << "test not supported";
3797 return;
3798 }
3799
3800 // Try to present 100ms in the future
3801 nsecs_t time = systemTime() + (100 * 1e6);
3802
3803 transaction.setDesiredPresentTime(time);
3804 transaction.apply();
3805
3806 ExpectedResult expected1;
3807 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3808 expected1.addExpectedPresentTime(time);
3809
3810 CallbackHelper callback2;
3811 err = fillTransaction(transaction, &callback2, layer);
3812 if (err) {
3813 GTEST_SUCCEED() << "test not supported";
3814 return;
3815 }
3816
3817 // Try to present 33ms after the first frame
3818 time += (33.3 * 1e6);
3819
3820 transaction.setDesiredPresentTime(time);
3821 transaction.apply();
3822
3823 ExpectedResult expected2;
3824 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3825 ExpectedResult::Buffer::ACQUIRED,
3826 ExpectedResult::PreviousBuffer::RELEASED);
3827 expected2.addExpectedPresentTime(time);
3828
3829 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3830 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3831}
3832
3833TEST_F(LayerCallbackTest, DesiredPresentTime_OutOfOrder) {
3834 sp<SurfaceControl> layer;
3835 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3836
3837 Transaction transaction;
3838 CallbackHelper callback1;
3839 int err = fillTransaction(transaction, &callback1, layer);
3840 if (err) {
3841 GTEST_SUCCEED() << "test not supported";
3842 return;
3843 }
3844
3845 // Try to present 100ms in the future
3846 nsecs_t time = systemTime() + (100 * 1e6);
3847
3848 transaction.setDesiredPresentTime(time);
3849 transaction.apply();
3850
3851 ExpectedResult expected1;
3852 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3853 expected1.addExpectedPresentTime(time);
3854
3855 CallbackHelper callback2;
3856 err = fillTransaction(transaction, &callback2, layer);
3857 if (err) {
3858 GTEST_SUCCEED() << "test not supported";
3859 return;
3860 }
3861
3862 // Try to present 33ms before the previous frame
3863 time -= (33.3 * 1e6);
3864
3865 transaction.setDesiredPresentTime(time);
3866 transaction.apply();
3867
3868 ExpectedResult expected2;
3869 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3870 ExpectedResult::Buffer::ACQUIRED,
3871 ExpectedResult::PreviousBuffer::RELEASED);
3872
3873 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3874 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3875}
3876
3877TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
3878 sp<SurfaceControl> layer;
3879 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3880
3881 Transaction transaction;
3882 CallbackHelper callback;
3883 int err = fillTransaction(transaction, &callback, layer);
3884 if (err) {
3885 GTEST_SUCCEED() << "test not supported";
3886 return;
3887 }
3888
3889 // Try to present 100ms in the past
3890 nsecs_t time = systemTime() - (100 * 1e6);
3891
3892 transaction.setDesiredPresentTime(time);
3893 transaction.apply();
3894
3895 ExpectedResult expected;
3896 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3897 expected.addExpectedPresentTime(systemTime());
3898 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3899}
3900
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003901class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003902protected:
3903 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07003904 LayerTransactionTest::SetUp();
3905 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003906
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003907 sp<IBinder> display(
3908 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07003909 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07003910 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07003911
3912 ssize_t displayWidth = info.w;
3913 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003914
3915 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07003916 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
3917 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003918 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003919 ASSERT_TRUE(mBGSurfaceControl->isValid());
3920 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
3921
3922 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07003923 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
3924
Peiyong Lin566a3b42018-01-09 18:22:43 -08003925 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003926 ASSERT_TRUE(mFGSurfaceControl->isValid());
3927
3928 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3929
3930 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07003931 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003932 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003933 ASSERT_TRUE(mSyncSurfaceControl->isValid());
3934
3935 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3936
Robert Carr4cdc58f2017-08-23 14:22:20 -07003937 asTransaction([&](Transaction& t) {
3938 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003939
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003940 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07003941
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003942 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
3943 .setPosition(mFGSurfaceControl, 64, 64)
3944 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003945
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003946 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
3947 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
3948 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003949 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003950 }
3951
3952 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07003953 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003954 mBGSurfaceControl = 0;
3955 mFGSurfaceControl = 0;
3956 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003957 }
3958
3959 void waitForPostedBuffers() {
3960 // Since the sync surface is in synchronous mode (i.e. double buffered)
3961 // posting three buffers to it should ensure that at least two
3962 // SurfaceFlinger::handlePageFlip calls have been made, which should
3963 // guaranteed that a buffer posted to another Surface has been retired.
3964 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3965 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3966 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3967 }
3968
Robert Carr4cdc58f2017-08-23 14:22:20 -07003969 void asTransaction(const std::function<void(Transaction&)>& exec) {
3970 Transaction t;
3971 exec(t);
3972 t.apply(true);
3973 }
3974
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003975 sp<SurfaceControl> mBGSurfaceControl;
3976 sp<SurfaceControl> mFGSurfaceControl;
3977
3978 // This surface is used to ensure that the buffers posted to
3979 // mFGSurfaceControl have been picked up by SurfaceFlinger.
3980 sp<SurfaceControl> mSyncSurfaceControl;
3981};
3982
Robert Carr7f619b22017-11-06 12:56:35 -08003983TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08003984
chaviw0e3479f2018-09-10 16:49:30 -07003985 std::unique_ptr<ScreenCapture> sc;
3986
3987 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08003988 fillSurfaceRGBA8(relative, 10, 10, 10);
3989 waitForPostedBuffers();
3990
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003991 Transaction{}
3992 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08003993 .setPosition(relative, 64, 64)
3994 .apply();
3995
3996 {
3997 // The relative should be on top of the FG control.
3998 ScreenCapture::captureScreen(&sc);
3999 sc->checkPixel(64, 64, 10, 10, 10);
4000 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004001 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08004002
4003 {
4004 // Nothing should change at this point.
4005 ScreenCapture::captureScreen(&sc);
4006 sc->checkPixel(64, 64, 10, 10, 10);
4007 }
4008
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004009 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08004010
4011 {
4012 // Ensure that the relative was actually hidden, rather than
4013 // being left in the detached but visible state.
4014 ScreenCapture::captureScreen(&sc);
4015 sc->expectFGColor(64, 64);
4016 }
4017}
4018
Robert Carr8d5227b2017-03-16 15:41:03 -07004019class GeometryLatchingTest : public LayerUpdateTest {
4020protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004021 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07004022 SCOPED_TRACE(trace);
4023 ScreenCapture::captureScreen(&sc);
4024 // We find the leading edge of the FG surface.
4025 sc->expectFGColor(127, 127);
4026 sc->expectBGColor(128, 128);
4027 }
Robert Carr7bf247e2017-05-18 14:02:49 -07004028
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004029 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07004030
4031 void unlockFGBuffer() {
4032 sp<Surface> s = mFGSurfaceControl->getSurface();
4033 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
4034 waitForPostedBuffers();
4035 }
4036
Robert Carr8d5227b2017-03-16 15:41:03 -07004037 void completeFGResize() {
4038 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4039 waitForPostedBuffers();
4040 }
4041 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004042 asTransaction([&](Transaction& t) {
4043 t.setSize(mFGSurfaceControl, 64, 64);
4044 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004045 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07004046 });
Robert Carr8d5227b2017-03-16 15:41:03 -07004047
4048 EXPECT_INITIAL_STATE("After restoring initial state");
4049 }
chaviw0e3479f2018-09-10 16:49:30 -07004050 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07004051};
4052
Robert Carr8d5227b2017-03-16 15:41:03 -07004053class CropLatchingTest : public GeometryLatchingTest {
4054protected:
4055 void EXPECT_CROPPED_STATE(const char* trace) {
4056 SCOPED_TRACE(trace);
4057 ScreenCapture::captureScreen(&sc);
4058 // The edge should be moved back one pixel by our crop.
4059 sc->expectFGColor(126, 126);
4060 sc->expectBGColor(127, 127);
4061 sc->expectBGColor(128, 128);
4062 }
chaviw59f5c562017-06-28 16:39:06 -07004063
4064 void EXPECT_RESIZE_STATE(const char* trace) {
4065 SCOPED_TRACE(trace);
4066 ScreenCapture::captureScreen(&sc);
4067 // The FG is now resized too 128,128 at 64,64
4068 sc->expectFGColor(64, 64);
4069 sc->expectFGColor(191, 191);
4070 sc->expectBGColor(192, 192);
4071 }
Robert Carr8d5227b2017-03-16 15:41:03 -07004072};
4073
Pablo Ceballos05289c22016-04-14 15:49:55 -07004074TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07004075 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07004076 {
4077 SCOPED_TRACE("before anything");
4078 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08004079 sc->expectBGColor(32, 32);
4080 sc->expectFGColor(96, 96);
4081 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07004082 }
4083
4084 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07004085 asTransaction([&](Transaction& t) {
4086 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004087 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
4088 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07004089 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07004090
Robert Carr4cdc58f2017-08-23 14:22:20 -07004091 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004092 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004093 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
4094 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004095 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07004096
4097 {
4098 SCOPED_TRACE("before any trigger");
4099 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08004100 sc->expectBGColor(32, 32);
4101 sc->expectFGColor(96, 96);
4102 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07004103 }
4104
4105 // should trigger the first deferred transaction, but not the second one
4106 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4107 {
4108 SCOPED_TRACE("after first trigger");
4109 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08004110 sc->expectBGColor(32, 32);
4111 sc->checkPixel(96, 96, 162, 63, 96);
4112 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07004113 }
4114
4115 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004116 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07004117
4118 // trigger the second deferred transaction
4119 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4120 {
4121 SCOPED_TRACE("after second trigger");
4122 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08004123 sc->expectBGColor(32, 32);
4124 sc->expectBGColor(96, 96);
4125 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07004126 }
4127}
4128
Robert Carre392b552017-09-19 12:16:05 -07004129TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07004130 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07004131
4132 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004133 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
4134 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4135 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
4136 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07004137 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08004138 SurfaceComposerClient::Transaction{}
4139 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
4140 .show(childNoBuffer)
4141 .show(childBuffer)
4142 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07004143 {
4144 ScreenCapture::captureScreen(&sc);
4145 sc->expectChildColor(73, 73);
4146 sc->expectFGColor(74, 74);
4147 }
Vishnu Nair60356342018-11-13 13:00:45 -08004148 SurfaceComposerClient::Transaction{}
4149 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
4150 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07004151 {
4152 ScreenCapture::captureScreen(&sc);
4153 sc->expectChildColor(73, 73);
4154 sc->expectChildColor(74, 74);
4155 }
4156}
4157
Robert Carr2c5f6d22017-09-26 12:30:35 -07004158TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07004159 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07004160 {
4161 SCOPED_TRACE("before move");
4162 ScreenCapture::captureScreen(&sc);
4163 sc->expectBGColor(0, 12);
4164 sc->expectFGColor(75, 75);
4165 sc->expectBGColor(145, 145);
4166 }
4167
4168 Transaction t1, t2;
4169 t1.setPosition(mFGSurfaceControl, 128, 128);
4170 t2.setPosition(mFGSurfaceControl, 0, 0);
4171 // We expect that the position update from t2 now
4172 // overwrites the position update from t1.
4173 t1.merge(std::move(t2));
4174 t1.apply();
4175
4176 {
4177 ScreenCapture::captureScreen(&sc);
4178 sc->expectFGColor(1, 1);
4179 }
4180}
4181
Robert Carr1f0a16a2016-10-24 16:27:39 -07004182class ChildLayerTest : public LayerUpdateTest {
4183protected:
4184 void SetUp() override {
4185 LayerUpdateTest::SetUp();
Vishnu Nair88a11f22018-11-28 18:30:57 -08004186 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
4187 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07004188 fillSurfaceRGBA8(mChild, 200, 200, 200);
4189
4190 {
4191 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07004192 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004193 mCapture->expectChildColor(64, 64);
4194 }
4195 }
4196 void TearDown() override {
4197 LayerUpdateTest::TearDown();
4198 mChild = 0;
4199 }
4200
4201 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07004202 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07004203};
4204
4205TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004206 asTransaction([&](Transaction& t) {
4207 t.show(mChild);
4208 t.setPosition(mChild, 10, 10);
4209 t.setPosition(mFGSurfaceControl, 64, 64);
4210 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004211
4212 {
chaviw0e3479f2018-09-10 16:49:30 -07004213 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004214 // Top left of foreground must now be visible
4215 mCapture->expectFGColor(64, 64);
4216 // But 10 pixels in we should see the child surface
4217 mCapture->expectChildColor(74, 74);
4218 // And 10 more pixels we should be back to the foreground surface
4219 mCapture->expectFGColor(84, 84);
4220 }
4221
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004222 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004223
4224 {
chaviw0e3479f2018-09-10 16:49:30 -07004225 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004226 // Top left of foreground should now be at 0, 0
4227 mCapture->expectFGColor(0, 0);
4228 // But 10 pixels in we should see the child surface
4229 mCapture->expectChildColor(10, 10);
4230 // And 10 more pixels we should be back to the foreground surface
4231 mCapture->expectFGColor(20, 20);
4232 }
4233}
4234
Robert Carr41b08b52017-06-01 16:11:34 -07004235TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004236 asTransaction([&](Transaction& t) {
4237 t.show(mChild);
4238 t.setPosition(mChild, 0, 0);
4239 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004240 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07004241 });
Robert Carr41b08b52017-06-01 16:11:34 -07004242
4243 {
chaviw0e3479f2018-09-10 16:49:30 -07004244 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07004245 mCapture->expectChildColor(0, 0);
4246 mCapture->expectChildColor(4, 4);
4247 mCapture->expectBGColor(5, 5);
4248 }
4249}
4250
Robert Carr1f0a16a2016-10-24 16:27:39 -07004251TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004252 asTransaction([&](Transaction& t) {
4253 t.show(mChild);
4254 t.setPosition(mFGSurfaceControl, 0, 0);
4255 t.setPosition(mChild, 63, 63);
4256 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004257
4258 {
chaviw0e3479f2018-09-10 16:49:30 -07004259 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004260 mCapture->expectFGColor(0, 0);
4261 // Last pixel in foreground should now be the child.
4262 mCapture->expectChildColor(63, 63);
4263 // But the child should be constrained and the next pixel
4264 // must be the background
4265 mCapture->expectBGColor(64, 64);
4266 }
4267}
4268
4269TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004270 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004271
4272 // Find the boundary between the parent and child
4273 {
chaviw0e3479f2018-09-10 16:49:30 -07004274 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004275 mCapture->expectChildColor(9, 9);
4276 mCapture->expectFGColor(10, 10);
4277 }
4278
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004279 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004280
4281 // The boundary should be twice as far from the origin now.
4282 // The pixels from the last test should all be child now
4283 {
chaviw0e3479f2018-09-10 16:49:30 -07004284 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004285 mCapture->expectChildColor(9, 9);
4286 mCapture->expectChildColor(10, 10);
4287 mCapture->expectChildColor(19, 19);
4288 mCapture->expectFGColor(20, 20);
4289 }
4290}
Robert Carr9524cb32017-02-13 11:32:32 -08004291
Robert Carr6452f122017-03-21 10:41:29 -07004292TEST_F(ChildLayerTest, ChildLayerAlpha) {
4293 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
4294 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
4295 fillSurfaceRGBA8(mChild, 0, 254, 0);
4296 waitForPostedBuffers();
4297
Robert Carr4cdc58f2017-08-23 14:22:20 -07004298 asTransaction([&](Transaction& t) {
4299 t.show(mChild);
4300 t.setPosition(mChild, 0, 0);
4301 t.setPosition(mFGSurfaceControl, 0, 0);
4302 });
Robert Carr6452f122017-03-21 10:41:29 -07004303
4304 {
chaviw0e3479f2018-09-10 16:49:30 -07004305 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004306 // Unblended child color
4307 mCapture->checkPixel(0, 0, 0, 254, 0);
4308 }
4309
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004310 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004311
4312 {
chaviw0e3479f2018-09-10 16:49:30 -07004313 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004314 // Child and BG blended.
4315 mCapture->checkPixel(0, 0, 127, 127, 0);
4316 }
4317
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004318 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004319
4320 {
chaviw0e3479f2018-09-10 16:49:30 -07004321 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004322 // Child and BG blended.
4323 mCapture->checkPixel(0, 0, 95, 64, 95);
4324 }
4325}
4326
Robert Carr9524cb32017-02-13 11:32:32 -08004327TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004328 asTransaction([&](Transaction& t) {
4329 t.show(mChild);
4330 t.setPosition(mChild, 10, 10);
4331 t.setPosition(mFGSurfaceControl, 64, 64);
4332 });
Robert Carr9524cb32017-02-13 11:32:32 -08004333
4334 {
chaviw0e3479f2018-09-10 16:49:30 -07004335 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004336 // Top left of foreground must now be visible
4337 mCapture->expectFGColor(64, 64);
4338 // But 10 pixels in we should see the child surface
4339 mCapture->expectChildColor(74, 74);
4340 // And 10 more pixels we should be back to the foreground surface
4341 mCapture->expectFGColor(84, 84);
4342 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004343
4344 asTransaction([&](Transaction& t) {
4345 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
4346 });
4347
Robert Carr9524cb32017-02-13 11:32:32 -08004348 {
chaviw0e3479f2018-09-10 16:49:30 -07004349 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004350 mCapture->expectFGColor(64, 64);
4351 // In reparenting we should have exposed the entire foreground surface.
4352 mCapture->expectFGColor(74, 74);
4353 // And the child layer should now begin at 10, 10 (since the BG
4354 // layer is at (0, 0)).
4355 mCapture->expectBGColor(9, 9);
4356 mCapture->expectChildColor(10, 10);
4357 }
4358}
4359
Robert Carr2e102c92018-10-23 12:11:15 -07004360TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
4361 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004362 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07004363 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
4364
4365 {
4366 SCOPED_TRACE("Grandchild visible");
4367 ScreenCapture::captureScreen(&mCapture);
4368 mCapture->checkPixel(64, 64, 111, 111, 111);
4369 }
4370
4371 mChild->clear();
4372
4373 {
4374 SCOPED_TRACE("After destroying child");
4375 ScreenCapture::captureScreen(&mCapture);
4376 mCapture->expectFGColor(64, 64);
4377 }
4378
4379 asTransaction([&](Transaction& t) {
4380 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
4381 });
4382
4383 {
4384 SCOPED_TRACE("After reparenting grandchild");
4385 ScreenCapture::captureScreen(&mCapture);
4386 mCapture->checkPixel(64, 64, 111, 111, 111);
4387 }
4388}
4389
chaviw161410b02017-07-27 10:46:08 -07004390TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004391 asTransaction([&](Transaction& t) {
4392 t.show(mChild);
4393 t.setPosition(mChild, 10, 10);
4394 t.setPosition(mFGSurfaceControl, 64, 64);
4395 });
Robert Carr9524cb32017-02-13 11:32:32 -08004396
4397 {
chaviw0e3479f2018-09-10 16:49:30 -07004398 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004399 // Top left of foreground must now be visible
4400 mCapture->expectFGColor(64, 64);
4401 // But 10 pixels in we should see the child surface
4402 mCapture->expectChildColor(74, 74);
4403 // And 10 more pixels we should be back to the foreground surface
4404 mCapture->expectFGColor(84, 84);
4405 }
4406
chaviw0e3479f2018-09-10 16:49:30 -07004407
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004408 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08004409
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004410 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08004411
chaviw161410b02017-07-27 10:46:08 -07004412 // Since the child has the same client as the parent, it will not get
4413 // detached and will be hidden.
4414 {
chaviw0e3479f2018-09-10 16:49:30 -07004415 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004416 mCapture->expectFGColor(64, 64);
4417 mCapture->expectFGColor(74, 74);
4418 mCapture->expectFGColor(84, 84);
4419 }
4420}
4421
4422TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
4423 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004424 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004425 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
4426 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07004427
chaviw161410b02017-07-27 10:46:08 -07004428 ASSERT_TRUE(mChildNewClient->isValid());
4429
4430 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
4431
Robert Carr4cdc58f2017-08-23 14:22:20 -07004432 asTransaction([&](Transaction& t) {
4433 t.hide(mChild);
4434 t.show(mChildNewClient);
4435 t.setPosition(mChildNewClient, 10, 10);
4436 t.setPosition(mFGSurfaceControl, 64, 64);
4437 });
chaviw161410b02017-07-27 10:46:08 -07004438
4439 {
chaviw0e3479f2018-09-10 16:49:30 -07004440 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004441 // Top left of foreground must now be visible
4442 mCapture->expectFGColor(64, 64);
4443 // But 10 pixels in we should see the child surface
4444 mCapture->expectChildColor(74, 74);
4445 // And 10 more pixels we should be back to the foreground surface
4446 mCapture->expectFGColor(84, 84);
4447 }
4448
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004449 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07004450
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004451 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07004452
Robert Carr9524cb32017-02-13 11:32:32 -08004453 // Nothing should have changed.
4454 {
chaviw0e3479f2018-09-10 16:49:30 -07004455 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004456 mCapture->expectFGColor(64, 64);
4457 mCapture->expectChildColor(74, 74);
4458 mCapture->expectFGColor(84, 84);
4459 }
4460}
4461
chaviw5aedec92018-10-22 10:40:38 -07004462TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
4463 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4464 sp<SurfaceControl> childNewClient =
4465 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4466 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4467
4468 ASSERT_TRUE(childNewClient != nullptr);
4469 ASSERT_TRUE(childNewClient->isValid());
4470
4471 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4472
4473 Transaction()
4474 .hide(mChild)
4475 .show(childNewClient)
4476 .setPosition(childNewClient, 10, 10)
4477 .setPosition(mFGSurfaceControl, 64, 64)
4478 .apply();
4479
4480 {
4481 mCapture = screenshot();
4482 // Top left of foreground must now be visible
4483 mCapture->expectFGColor(64, 64);
4484 // But 10 pixels in we should see the child surface
4485 mCapture->expectChildColor(74, 74);
4486 // And 10 more pixels we should be back to the foreground surface
4487 mCapture->expectFGColor(84, 84);
4488 }
4489
4490 Transaction().detachChildren(mFGSurfaceControl).apply();
4491 Transaction().hide(childNewClient).apply();
4492
4493 // Nothing should have changed.
4494 {
4495 mCapture = screenshot();
4496 mCapture->expectFGColor(64, 64);
4497 mCapture->expectChildColor(74, 74);
4498 mCapture->expectFGColor(84, 84);
4499 }
4500
4501 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
4502 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
4503 32);
4504 Transaction()
4505 .setLayer(newParentSurface, INT32_MAX - 1)
4506 .show(newParentSurface)
4507 .setPosition(newParentSurface, 20, 20)
4508 .reparent(childNewClient, newParentSurface->getHandle())
4509 .apply();
4510 {
4511 mCapture = screenshot();
4512 // Child is now hidden.
4513 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
4514 }
4515}
4516
Robert Carr9b429f42017-04-17 14:56:57 -07004517TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004518 asTransaction([&](Transaction& t) {
4519 t.show(mChild);
4520 t.setPosition(mChild, 0, 0);
4521 t.setPosition(mFGSurfaceControl, 0, 0);
4522 });
Robert Carr9b429f42017-04-17 14:56:57 -07004523
4524 {
chaviw0e3479f2018-09-10 16:49:30 -07004525 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004526 // We've positioned the child in the top left.
4527 mCapture->expectChildColor(0, 0);
4528 // But it's only 10x10.
4529 mCapture->expectFGColor(10, 10);
4530 }
4531
Robert Carr4cdc58f2017-08-23 14:22:20 -07004532 asTransaction([&](Transaction& t) {
4533 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4534 // We cause scaling by 2.
4535 t.setSize(mFGSurfaceControl, 128, 128);
4536 });
Robert Carr9b429f42017-04-17 14:56:57 -07004537
4538 {
chaviw0e3479f2018-09-10 16:49:30 -07004539 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004540 // We've positioned the child in the top left.
4541 mCapture->expectChildColor(0, 0);
4542 mCapture->expectChildColor(10, 10);
4543 mCapture->expectChildColor(19, 19);
4544 // And now it should be scaled all the way to 20x20
4545 mCapture->expectFGColor(20, 20);
4546 }
4547}
4548
Robert Carr1725eee2017-04-26 18:32:15 -07004549// Regression test for b/37673612
4550TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004551 asTransaction([&](Transaction& t) {
4552 t.show(mChild);
4553 t.setPosition(mChild, 0, 0);
4554 t.setPosition(mFGSurfaceControl, 0, 0);
4555 });
Robert Carr1725eee2017-04-26 18:32:15 -07004556
4557 {
chaviw0e3479f2018-09-10 16:49:30 -07004558 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004559 // We've positioned the child in the top left.
4560 mCapture->expectChildColor(0, 0);
4561 // But it's only 10x10.
4562 mCapture->expectFGColor(10, 10);
4563 }
Robert Carr1725eee2017-04-26 18:32:15 -07004564 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
4565 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004566 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07004567 sp<Surface> s = mFGSurfaceControl->getSurface();
4568 auto anw = static_cast<ANativeWindow*>(s.get());
4569 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4570 native_window_set_buffers_dimensions(anw, 64, 128);
4571 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4572 waitForPostedBuffers();
4573
4574 {
4575 // The child should still be in the same place and not have any strange scaling as in
4576 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07004577 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004578 mCapture->expectChildColor(0, 0);
4579 mCapture->expectFGColor(10, 10);
4580 }
4581}
4582
Dan Stoza412903f2017-04-27 13:42:17 -07004583TEST_F(ChildLayerTest, Bug36858924) {
4584 // Destroy the child layer
4585 mChild.clear();
4586
4587 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08004588 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
4589 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07004590
4591 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07004592 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004593 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
4594 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07004595 t.show(mChild);
4596 });
Dan Stoza412903f2017-04-27 13:42:17 -07004597
4598 // Render the foreground surface a few times
4599 //
4600 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
4601 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
4602 // never acquire/release the first buffer
4603 ALOGI("Filling 1");
4604 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
4605 ALOGI("Filling 2");
4606 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
4607 ALOGI("Filling 3");
4608 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
4609 ALOGI("Filling 4");
4610 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
4611}
4612
chaviwf1961f72017-09-18 16:41:07 -07004613TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004614 asTransaction([&](Transaction& t) {
4615 t.show(mChild);
4616 t.setPosition(mChild, 10, 10);
4617 t.setPosition(mFGSurfaceControl, 64, 64);
4618 });
chaviw06178942017-07-27 10:25:59 -07004619
4620 {
chaviw0e3479f2018-09-10 16:49:30 -07004621 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004622 // Top left of foreground must now be visible
4623 mCapture->expectFGColor(64, 64);
4624 // But 10 pixels in we should see the child surface
4625 mCapture->expectChildColor(74, 74);
4626 // And 10 more pixels we should be back to the foreground surface
4627 mCapture->expectFGColor(84, 84);
4628 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004629
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004630 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07004631
chaviw06178942017-07-27 10:25:59 -07004632 {
chaviw0e3479f2018-09-10 16:49:30 -07004633 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004634 mCapture->expectFGColor(64, 64);
4635 // In reparenting we should have exposed the entire foreground surface.
4636 mCapture->expectFGColor(74, 74);
4637 // And the child layer should now begin at 10, 10 (since the BG
4638 // layer is at (0, 0)).
4639 mCapture->expectBGColor(9, 9);
4640 mCapture->expectChildColor(10, 10);
4641 }
4642}
4643
chaviwf1961f72017-09-18 16:41:07 -07004644TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004645 asTransaction([&](Transaction& t) {
4646 t.show(mChild);
4647 t.setPosition(mChild, 10, 10);
4648 t.setPosition(mFGSurfaceControl, 64, 64);
4649 });
chaviwf1961f72017-09-18 16:41:07 -07004650
4651 {
chaviw0e3479f2018-09-10 16:49:30 -07004652 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004653 // Top left of foreground must now be visible
4654 mCapture->expectFGColor(64, 64);
4655 // But 10 pixels in we should see the child surface
4656 mCapture->expectChildColor(74, 74);
4657 // And 10 more pixels we should be back to the foreground surface
4658 mCapture->expectFGColor(84, 84);
4659 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004660 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07004661 {
chaviw0e3479f2018-09-10 16:49:30 -07004662 mCapture = screenshot();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004663 // The surface should now be offscreen.
chaviwf1961f72017-09-18 16:41:07 -07004664 mCapture->expectFGColor(64, 64);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004665 mCapture->expectFGColor(74, 74);
chaviwf1961f72017-09-18 16:41:07 -07004666 mCapture->expectFGColor(84, 84);
4667 }
4668}
4669
4670TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07004671 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08004672 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07004673 ASSERT_TRUE(newSurface->isValid());
4674
4675 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004676 asTransaction([&](Transaction& t) {
4677 t.hide(mChild);
4678 t.show(newSurface);
4679 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004680 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004681 t.setPosition(mFGSurfaceControl, 64, 64);
4682 });
chaviwf1961f72017-09-18 16:41:07 -07004683
4684 {
chaviw0e3479f2018-09-10 16:49:30 -07004685 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004686 // Top left of foreground must now be visible
4687 mCapture->expectFGColor(64, 64);
4688 // At 10, 10 we should see the new surface
4689 mCapture->checkPixel(10, 10, 63, 195, 63);
4690 }
4691
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004692 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07004693
4694 {
chaviw0e3479f2018-09-10 16:49:30 -07004695 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004696 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
4697 // mFGSurface, putting it at 74, 74.
4698 mCapture->expectFGColor(64, 64);
4699 mCapture->checkPixel(74, 74, 63, 195, 63);
4700 mCapture->expectFGColor(84, 84);
4701 }
4702}
4703
chaviwc9674332017-08-28 12:32:18 -07004704TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004705 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
4706 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07004707 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4708
4709 {
chaviw0e3479f2018-09-10 16:49:30 -07004710 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07004711 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
4712 // which begins at 64, 64
4713 mCapture->checkPixel(64, 64, 50, 50, 50);
4714 }
4715}
4716
Robert Carr503c7042017-09-27 15:06:08 -07004717TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004718 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07004719 fillSurfaceRGBA8(relative, 255, 255, 255);
4720
4721 Transaction t;
4722 t.setLayer(relative, INT32_MAX)
4723 .setRelativeLayer(mChild, relative->getHandle(), 1)
4724 .setPosition(mFGSurfaceControl, 0, 0)
4725 .apply(true);
4726
4727 // We expect that the child should have been elevated above our
4728 // INT_MAX layer even though it's not a child of it.
4729 {
chaviw0e3479f2018-09-10 16:49:30 -07004730 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07004731 mCapture->expectChildColor(0, 0);
4732 mCapture->expectChildColor(9, 9);
4733 mCapture->checkPixel(10, 10, 255, 255, 255);
4734 }
4735}
Vishnu Nair60356342018-11-13 13:00:45 -08004736class BoundlessLayerTest : public LayerUpdateTest {
4737protected:
4738 std::unique_ptr<ScreenCapture> mCapture;
4739};
4740
4741// Verify setting a size on a buffer layer has no effect.
4742TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
4743 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004744 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
4745 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004746 ASSERT_TRUE(bufferLayer->isValid());
4747 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
4748 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
4749 {
4750 mCapture = screenshot();
4751 // Top left of background must now be visible
4752 mCapture->expectBGColor(0, 0);
4753 // Foreground Surface bounds must be color layer
4754 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
4755 // Buffer layer should not extend past buffer bounds
4756 mCapture->expectFGColor(95, 95);
4757 }
4758}
4759
4760// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
4761// which will crop the color layer.
4762TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
4763 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004764 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4765 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004766 ASSERT_TRUE(colorLayer->isValid());
4767 asTransaction([&](Transaction& t) {
4768 t.setColor(colorLayer, half3{0, 0, 0});
4769 t.show(colorLayer);
4770 });
4771 {
4772 mCapture = screenshot();
4773 // Top left of background must now be visible
4774 mCapture->expectBGColor(0, 0);
4775 // Foreground Surface bounds must be color layer
4776 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4777 // Color layer should not extend past foreground bounds
4778 mCapture->expectBGColor(129, 129);
4779 }
4780}
4781
4782// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
4783// a crop which will be used to crop the color layer.
4784TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004785 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4786 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004787 ASSERT_TRUE(cropLayer->isValid());
4788 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004789 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4790 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004791 ASSERT_TRUE(colorLayer->isValid());
4792 asTransaction([&](Transaction& t) {
4793 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
4794 t.setColor(colorLayer, half3{0, 0, 0});
4795 t.show(cropLayer);
4796 t.show(colorLayer);
4797 });
4798 {
4799 mCapture = screenshot();
4800 // Top left of background must now be visible
4801 mCapture->expectBGColor(0, 0);
4802 // Top left of foreground must now be visible
4803 mCapture->expectFGColor(64, 64);
4804 // 5 pixels from the foreground we should see the child surface
4805 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
4806 // 10 pixels from the foreground we should be back to the foreground surface
4807 mCapture->expectFGColor(74, 74);
4808 }
4809}
4810
4811// Verify for boundless layer with no children, their transforms have no effect.
4812TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
4813 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004814 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4815 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004816 ASSERT_TRUE(colorLayer->isValid());
4817 asTransaction([&](Transaction& t) {
4818 t.setPosition(colorLayer, 320, 320);
4819 t.setMatrix(colorLayer, 2, 0, 0, 2);
4820 t.setColor(colorLayer, half3{0, 0, 0});
4821 t.show(colorLayer);
4822 });
4823 {
4824 mCapture = screenshot();
4825 // Top left of background must now be visible
4826 mCapture->expectBGColor(0, 0);
4827 // Foreground Surface bounds must be color layer
4828 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4829 // Color layer should not extend past foreground bounds
4830 mCapture->expectBGColor(129, 129);
4831 }
4832}
4833
4834// Verify for boundless layer with children, their transforms have an effect.
4835TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
4836 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004837 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4838 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004839 ASSERT_TRUE(boundlessLayerRightShift->isValid());
4840 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004841 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4842 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004843 ASSERT_TRUE(boundlessLayerDownShift->isValid());
4844 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004845 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4846 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004847 ASSERT_TRUE(colorLayer->isValid());
4848 asTransaction([&](Transaction& t) {
4849 t.setPosition(boundlessLayerRightShift, 32, 0);
4850 t.show(boundlessLayerRightShift);
4851 t.setPosition(boundlessLayerDownShift, 0, 32);
4852 t.show(boundlessLayerDownShift);
4853 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4854 t.setColor(colorLayer, half3{0, 0, 0});
4855 t.show(colorLayer);
4856 });
4857 {
4858 mCapture = screenshot();
4859 // Top left of background must now be visible
4860 mCapture->expectBGColor(0, 0);
4861 // Top left of foreground must now be visible
4862 mCapture->expectFGColor(64, 64);
4863 // Foreground Surface bounds must be color layer
4864 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
4865 // Color layer should not extend past foreground bounds
4866 mCapture->expectBGColor(129, 129);
4867 }
4868}
4869
4870// Verify child layers do not get clipped if they temporarily move into the negative
4871// coordinate space as the result of an intermediate transformation.
4872TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
4873 sp<SurfaceControl> boundlessLayer =
4874 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4875 0 /* flags */, mFGSurfaceControl.get());
4876 ASSERT_TRUE(boundlessLayer != nullptr);
4877 ASSERT_TRUE(boundlessLayer->isValid());
4878 sp<SurfaceControl> colorLayer =
4879 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4880 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
4881 ASSERT_TRUE(colorLayer != nullptr);
4882 ASSERT_TRUE(colorLayer->isValid());
4883 asTransaction([&](Transaction& t) {
4884 // shift child layer off bounds. If this layer was not boundless, we will
4885 // expect the child layer to be cropped.
4886 t.setPosition(boundlessLayer, 32, 32);
4887 t.show(boundlessLayer);
4888 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4889 // undo shift by parent
4890 t.setPosition(colorLayer, -32, -32);
4891 t.setColor(colorLayer, half3{0, 0, 0});
4892 t.show(colorLayer);
4893 });
4894 {
4895 mCapture = screenshot();
4896 // Top left of background must now be visible
4897 mCapture->expectBGColor(0, 0);
4898 // Foreground Surface bounds must be color layer
4899 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4900 // Color layer should not extend past foreground bounds
4901 mCapture->expectBGColor(129, 129);
4902 }
4903}
4904
4905// Verify for boundless root layers with children, their transforms have an effect.
4906TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004907 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
4908 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08004909 ASSERT_TRUE(rootBoundlessLayer->isValid());
4910 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004911 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4912 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
4913
Vishnu Nair60356342018-11-13 13:00:45 -08004914 ASSERT_TRUE(colorLayer->isValid());
4915 asTransaction([&](Transaction& t) {
4916 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
4917 t.setPosition(rootBoundlessLayer, 32, 32);
4918 t.show(rootBoundlessLayer);
4919 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4920 t.setColor(colorLayer, half3{0, 0, 0});
4921 t.show(colorLayer);
4922 t.hide(mFGSurfaceControl);
4923 });
4924 {
4925 mCapture = screenshot();
4926 // Top left of background must now be visible
4927 mCapture->expectBGColor(0, 0);
4928 // Top left of foreground must now be visible
4929 mCapture->expectBGColor(31, 31);
4930 // Foreground Surface bounds must be color layer
4931 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
4932 // Color layer should not extend past foreground bounds
4933 mCapture->expectBGColor(97, 97);
4934 }
4935}
Robert Carr503c7042017-09-27 15:06:08 -07004936
chaviwa76b2712017-09-20 12:02:26 -07004937class ScreenCaptureTest : public LayerUpdateTest {
4938protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004939 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07004940};
4941
4942TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
4943 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004944 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004945 mCapture->expectBGColor(0, 0);
4946 // Doesn't capture FG layer which is at 64, 64
4947 mCapture->expectBGColor(64, 64);
4948}
4949
4950TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
4951 auto fgHandle = mFGSurfaceControl->getHandle();
4952
Vishnu Nair88a11f22018-11-28 18:30:57 -08004953 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4954 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004955 fillSurfaceRGBA8(child, 200, 200, 200);
4956
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004957 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004958
4959 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004960 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004961 mCapture->expectFGColor(10, 10);
4962 mCapture->expectChildColor(0, 0);
4963}
4964
Robert Carr578038f2018-03-09 12:25:24 -08004965TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
4966 auto fgHandle = mFGSurfaceControl->getHandle();
4967
Vishnu Nair88a11f22018-11-28 18:30:57 -08004968 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4969 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004970 fillSurfaceRGBA8(child, 200, 200, 200);
4971
4972 SurfaceComposerClient::Transaction().show(child).apply(true);
4973
4974 // Captures mFGSurfaceControl's child
4975 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4976 mCapture->checkPixel(10, 10, 0, 0, 0);
4977 mCapture->expectChildColor(0, 0);
4978}
4979
chaviw50da5042018-04-09 13:49:37 -07004980TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004981 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4982 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07004983
4984 fillSurfaceRGBA8(child, 200, 200, 200);
4985
4986 SurfaceComposerClient::Transaction().show(child).apply(true);
4987
4988 auto childHandle = child->getHandle();
4989
4990 // Captures child
4991 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
4992 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
4993 // Area outside of child's bounds is transparent.
4994 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
4995}
4996
chaviw4b129c22018-04-09 16:19:43 -07004997TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
4998 auto fgHandle = mFGSurfaceControl->getHandle();
4999
Vishnu Nair88a11f22018-11-28 18:30:57 -08005000 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5001 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5002 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07005003 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07005004 fillSurfaceRGBA8(child, 200, 200, 200);
5005 fillSurfaceRGBA8(relative, 100, 100, 100);
5006
5007 SurfaceComposerClient::Transaction()
5008 .show(child)
5009 // Set relative layer above fg layer so should be shown above when computing all layers.
5010 .setRelativeLayer(relative, fgHandle, 1)
5011 .show(relative)
5012 .apply(true);
5013
5014 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
5015 ScreenCapture::captureLayers(&mCapture, fgHandle);
5016 mCapture->expectFGColor(10, 10);
5017 mCapture->expectChildColor(0, 0);
5018}
5019
5020TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
5021 auto fgHandle = mFGSurfaceControl->getHandle();
5022
Vishnu Nair88a11f22018-11-28 18:30:57 -08005023 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5024 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5025 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
5026 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07005027 fillSurfaceRGBA8(child, 200, 200, 200);
5028 fillSurfaceRGBA8(relative, 100, 100, 100);
5029
5030 SurfaceComposerClient::Transaction()
5031 .show(child)
5032 // Set relative layer below fg layer but relative to child layer so it should be shown
5033 // above child layer.
5034 .setLayer(relative, -1)
5035 .setRelativeLayer(relative, child->getHandle(), 1)
5036 .show(relative)
5037 .apply(true);
5038
5039 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
5040 // relative value should be taken into account, placing it above child layer.
5041 ScreenCapture::captureLayers(&mCapture, fgHandle);
5042 mCapture->expectFGColor(10, 10);
5043 // Relative layer is showing on top of child layer
5044 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
5045}
Robert Carr578038f2018-03-09 12:25:24 -08005046
5047// In the following tests we verify successful skipping of a parent layer,
5048// so we use the same verification logic and only change how we mutate
5049// the parent layer to verify that various properties are ignored.
5050class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
5051public:
5052 void SetUp() override {
5053 LayerUpdateTest::SetUp();
5054
Vishnu Nair88a11f22018-11-28 18:30:57 -08005055 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
5056 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08005057 fillSurfaceRGBA8(mChild, 200, 200, 200);
5058
5059 SurfaceComposerClient::Transaction().show(mChild).apply(true);
5060 }
5061
5062 void verify() {
5063 auto fgHandle = mFGSurfaceControl->getHandle();
5064 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
5065 mCapture->checkPixel(10, 10, 0, 0, 0);
5066 mCapture->expectChildColor(0, 0);
5067 }
5068
5069 std::unique_ptr<ScreenCapture> mCapture;
5070 sp<SurfaceControl> mChild;
5071};
5072
5073TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
5074
5075 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
5076
5077 // Even though the parent is hidden we should still capture the child.
5078 verify();
5079}
5080
5081TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07005082 SurfaceComposerClient::Transaction()
5083 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
5084 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005085
5086 // Even though the parent is cropped out we should still capture the child.
5087 verify();
5088}
5089
5090TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
5091
5092 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
5093
5094 // We should not inherit the parent scaling.
5095 verify();
5096}
5097
Robert Carr15eae092018-03-23 13:43:53 -07005098TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
5099 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
5100
5101 // Even though the parent is hidden we should still capture the child.
5102 verify();
5103
5104 // Verify everything was properly hidden when rendering the full-screen.
5105 screenshot()->expectBGColor(0,0);
5106}
5107
5108
chaviwa76b2712017-09-20 12:02:26 -07005109TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
5110 auto fgHandle = mFGSurfaceControl->getHandle();
5111
Vishnu Nair88a11f22018-11-28 18:30:57 -08005112 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5113 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005114 fillSurfaceRGBA8(child, 200, 200, 200);
5115
Vishnu Nair88a11f22018-11-28 18:30:57 -08005116 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5117 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07005118
5119 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5120 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005121 .show(child)
5122 .setPosition(grandchild, 5, 5)
5123 .show(grandchild)
5124 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005125
5126 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005127 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07005128 mCapture->expectFGColor(10, 10);
5129 mCapture->expectChildColor(0, 0);
5130 mCapture->checkPixel(5, 5, 50, 50, 50);
5131}
5132
5133TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005134 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5135 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005136 fillSurfaceRGBA8(child, 200, 200, 200);
5137 auto childHandle = child->getHandle();
5138
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005139 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005140
5141 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005142 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07005143 mCapture->expectChildColor(0, 0);
5144 mCapture->expectChildColor(9, 9);
5145}
5146
5147TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005148 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5149 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005150 fillSurfaceRGBA8(child, 200, 200, 200);
5151 auto childHandle = child->getHandle();
5152
Vishnu Nair88a11f22018-11-28 18:30:57 -08005153 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5154 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07005155 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5156
5157 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005158 .show(child)
5159 .setPosition(grandchild, 5, 5)
5160 .show(grandchild)
5161 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005162
5163 auto grandchildHandle = grandchild->getHandle();
5164
5165 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005166 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07005167 mCapture->checkPixel(0, 0, 50, 50, 50);
5168 mCapture->checkPixel(4, 4, 50, 50, 50);
5169}
5170
chaviw7206d492017-11-10 16:16:12 -08005171TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07005172 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005173 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5174 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005175
Marissa Wall61c58622018-07-18 10:12:20 -07005176 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5177 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005178
5179 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005180 .setLayer(redLayer, INT32_MAX - 1)
5181 .show(redLayer)
5182 .show(blueLayer)
5183 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005184
5185 auto redLayerHandle = redLayer->getHandle();
5186
5187 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005188 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5189 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5190 // red area below the blue area
5191 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5192 // red area to the right of the blue area
5193 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005194
Marissa Wall861616d2018-10-22 12:52:23 -07005195 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005196 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08005197 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
5198 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005199 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08005200 mCapture->checkPixel(30, 30, 0, 0, 0);
5201}
5202
5203TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07005204 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005205 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5206 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005207
Marissa Wall61c58622018-07-18 10:12:20 -07005208 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5209 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005210
5211 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005212 .setLayer(redLayer, INT32_MAX - 1)
5213 .show(redLayer)
5214 .show(blueLayer)
5215 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005216
5217 auto redLayerHandle = redLayer->getHandle();
5218
5219 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005220 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5221 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5222 // red area below the blue area
5223 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5224 // red area to the right of the blue area
5225 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005226
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005227 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08005228 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005229 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
5230 // red area below the blue area
5231 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
5232 // red area to the right of the blue area
5233 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005234 mCapture->checkPixel(30, 30, 0, 0, 0);
5235}
5236
5237TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07005238 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08005239
Marissa Wall61c58622018-07-18 10:12:20 -07005240 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08005241
5242 auto redLayerHandle = redLayer->getHandle();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08005243 redLayer->clear();
chaviw7206d492017-11-10 16:16:12 -08005244 SurfaceComposerClient::Transaction().apply(true);
5245
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005246 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08005247
5248 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005249 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
5250 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08005251}
5252
chaviw8e3fe5d2018-02-22 10:55:42 -08005253
5254class DereferenceSurfaceControlTest : public LayerTransactionTest {
5255protected:
5256 void SetUp() override {
5257 LayerTransactionTest::SetUp();
5258 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005259 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005260 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005261 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005262 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
5263 {
5264 SCOPED_TRACE("before anything");
5265 auto shot = screenshot();
5266 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5267 }
5268 }
5269 void TearDown() override {
5270 LayerTransactionTest::TearDown();
5271 bgLayer = 0;
5272 fgLayer = 0;
5273 }
5274
5275 sp<SurfaceControl> bgLayer;
5276 sp<SurfaceControl> fgLayer;
5277};
5278
5279TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
5280 fgLayer = nullptr;
5281 {
5282 SCOPED_TRACE("after setting null");
5283 auto shot = screenshot();
5284 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
5285 }
5286}
5287
5288TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
5289 auto transaction = Transaction().show(fgLayer);
5290 fgLayer = nullptr;
5291 {
5292 SCOPED_TRACE("after setting null");
5293 auto shot = screenshot();
5294 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5295 }
5296}
5297
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005298} // namespace android