blob: d5f65348d82d72fdab19a4f82813fc67fd890f47 [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>
Ady Abrahamdf9df4a2019-03-12 17:32:05 -070035#include <hardware/hwcomposer_defs.h>
Robert Carrfa8855f2019-02-19 10:05:00 -080036#include <private/android_filesystem_config.h>
Ady Abrahamdf9df4a2019-03-12 17:32:05 -070037#include <private/gui/ComposerService.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080038
Ady Abraham2a6ab2a2018-10-26 14:25:30 -070039#include <ui/ColorSpace.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070040#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070041#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070042#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070043
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070044#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070045#include <math/vec3.h>
Robert Carrfa8855f2019-02-19 10:05:00 -080046#include <sys/types.h>
47#include <unistd.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070048
Marissa Wall713b63f2018-10-17 15:42:43 -070049#include "BufferGenerator.h"
50
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070051namespace android {
52
Chia-I Wu718daf82017-10-20 11:57:17 -070053namespace {
54
55struct Color {
56 uint8_t r;
57 uint8_t g;
58 uint8_t b;
59 uint8_t a;
60
61 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070062 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070063 static const Color BLUE;
Chia-I Wu93853fe2017-11-02 08:30:27 -070064 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070065 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070066 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070067};
68
69const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070070const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070071const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070072const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070073const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070074const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070075
Marissa Wall61c58622018-07-18 10:12:20 -070076using android::hardware::graphics::common::V1_1::BufferUsage;
Marissa Wallfda30bb2018-10-12 11:34:28 -070077using namespace std::chrono_literals;
Marissa Wall61c58622018-07-18 10:12:20 -070078
Chia-I Wu718daf82017-10-20 11:57:17 -070079std::ostream& operator<<(std::ostream& os, const Color& color) {
80 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
81 return os;
82}
83
84// Fill a region with the specified color.
Marissa Wall61c58622018-07-18 10:12:20 -070085void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
86 const Color& color) {
87 Rect r(0, 0, buffer.width, buffer.height);
88 if (!r.intersect(rect, &r)) {
89 return;
Chia-I Wu718daf82017-10-20 11:57:17 -070090 }
91
Marissa Wall61c58622018-07-18 10:12:20 -070092 int32_t width = r.right - r.left;
93 int32_t height = r.bottom - r.top;
94
95 for (int32_t row = 0; row < height; row++) {
96 uint8_t* dst =
97 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
98 for (int32_t column = 0; column < width; column++) {
Chia-I Wu718daf82017-10-20 11:57:17 -070099 dst[0] = color.r;
100 dst[1] = color.g;
101 dst[2] = color.b;
102 dst[3] = color.a;
103 dst += 4;
104 }
105 }
106}
107
Marissa Wall61c58622018-07-18 10:12:20 -0700108// Fill a region with the specified color.
109void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
110 Rect r(0, 0, buffer->width, buffer->height);
111 if (!r.intersect(rect, &r)) {
112 return;
113 }
114
115 int32_t width = r.right - r.left;
116 int32_t height = r.bottom - r.top;
117
118 uint8_t* pixels;
119 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
120 reinterpret_cast<void**>(&pixels));
121
122 for (int32_t row = 0; row < height; row++) {
123 uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
124 for (int32_t column = 0; column < width; column++) {
125 dst[0] = color.r;
126 dst[1] = color.g;
127 dst[2] = color.b;
128 dst[3] = color.a;
129 dst += 4;
130 }
131 }
132 buffer->unlock();
133}
134
Chia-I Wu718daf82017-10-20 11:57:17 -0700135// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000136void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700137 const Color& color, uint8_t tolerance) {
138 int32_t x = rect.left;
139 int32_t y = rect.top;
140 int32_t width = rect.right - rect.left;
141 int32_t height = rect.bottom - rect.top;
142
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000143 int32_t bufferWidth = int32_t(outBuffer->getWidth());
144 int32_t bufferHeight = int32_t(outBuffer->getHeight());
145 if (x + width > bufferWidth) {
146 x = std::min(x, bufferWidth);
147 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700148 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000149 if (y + height > bufferHeight) {
150 y = std::min(y, bufferHeight);
151 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700152 }
153
154 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
155 uint8_t tmp = a >= b ? a - b : b - a;
156 return tmp <= tolerance;
157 };
158 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000159 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700160 for (int32_t i = 0; i < width; i++) {
161 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
162 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
163 << "pixel @ (" << x + i << ", " << y + j << "): "
164 << "expected (" << color << "), "
165 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
166 src += 4;
167 }
168 }
169}
170
171} // anonymous namespace
172
Robert Carr4cdc58f2017-08-23 14:22:20 -0700173using Transaction = SurfaceComposerClient::Transaction;
174
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700175// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700176static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
177 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800178 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700179 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800180 ASSERT_TRUE(s != nullptr);
181 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800182 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700183 for (int y = 0; y < outBuffer.height; y++) {
184 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700185 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700186 pixel[0] = r;
187 pixel[1] = g;
188 pixel[2] = b;
189 pixel[3] = 255;
190 }
191 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700192 if (unlock) {
193 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
194 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700195}
196
197// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
198// individual pixel values for testing purposes.
199class ScreenCapture : public RefBase {
200public:
chaviw0e3479f2018-09-10 16:49:30 -0700201 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
Vishnu Nairb927e1f2019-02-19 13:36:15 -0800202 captureScreen(sc, SurfaceComposerClient::getInternalDisplayToken());
203 }
204
205 static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800206 const auto sf = ComposerService::getComposerService();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700207 SurfaceComposerClient::Transaction().apply(true);
208
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000209 sp<GraphicBuffer> outBuffer;
Vishnu Nairb927e1f2019-02-19 13:36:15 -0800210 ASSERT_EQ(NO_ERROR, sf->captureScreen(displayToken, &outBuffer, Rect(), 0, 0, false));
chaviw0e3479f2018-09-10 16:49:30 -0700211 *sc = std::make_unique<ScreenCapture>(outBuffer);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000212 }
213
214 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
215 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
216 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
217 SurfaceComposerClient::Transaction().apply(true);
218
219 sp<GraphicBuffer> outBuffer;
220 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
221 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700222 }
223
Robert Carr578038f2018-03-09 12:25:24 -0800224 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
225 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
226 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
227 SurfaceComposerClient::Transaction().apply(true);
228
229 sp<GraphicBuffer> outBuffer;
230 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
231 *sc = std::make_unique<ScreenCapture>(outBuffer);
232 }
233
Robert Carr866455f2019-04-02 16:28:26 -0700234 static void captureChildLayersExcluding(
235 std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
236 std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>> excludeLayers) {
237 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
238 SurfaceComposerClient::Transaction().apply(true);
239
240 sp<GraphicBuffer> outBuffer;
241 ASSERT_EQ(NO_ERROR,
242 sf->captureLayers(parentHandle, &outBuffer, ui::Dataspace::V0_SRGB,
243 ui::PixelFormat::RGBA_8888, Rect::EMPTY_RECT, excludeLayers,
244 1.0f, true));
245 *sc = std::make_unique<ScreenCapture>(outBuffer);
246 }
247
Chia-I Wu718daf82017-10-20 11:57:17 -0700248 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000249 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
250 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700251 }
252
253 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000254 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700255 const bool leftBorder = rect.left > 0;
256 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000257 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
258 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700259
260 if (topBorder) {
261 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
262 if (leftBorder) {
263 top.left -= 1;
264 }
265 if (rightBorder) {
266 top.right += 1;
267 }
268 expectColor(top, color, tolerance);
269 }
270 if (leftBorder) {
271 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
272 expectColor(left, color, tolerance);
273 }
274 if (rightBorder) {
275 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
276 expectColor(right, color, tolerance);
277 }
278 if (bottomBorder) {
279 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
280 if (leftBorder) {
281 bottom.left -= 1;
282 }
283 if (rightBorder) {
284 bottom.right += 1;
285 }
286 expectColor(bottom, color, tolerance);
287 }
288 }
289
Chia-I Wu93853fe2017-11-02 08:30:27 -0700290 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
291 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
292 uint8_t tolerance = 0) {
293 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
294
295 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
296 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
297 // avoid checking borders due to unspecified filtering behavior
298 const int32_t offsetX = filtered ? 2 : 0;
299 const int32_t offsetY = filtered ? 2 : 0;
300 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
301 tolerance);
302 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
303 tolerance);
304 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
305 tolerance);
306 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
307 bottomRight, tolerance);
308 }
309
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700310 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000311 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
312 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700313 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
314 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700315 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
316 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700317 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700318 }
319 }
320
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700321 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700322
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700323 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700324
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700325 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700326
Chih-Hung Hsieh22749042018-12-20 15:50:39 -0800327 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000328 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700329 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700330
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000331 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700332
333private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000334 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800335 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700336};
337
Chia-I Wu718daf82017-10-20 11:57:17 -0700338class LayerTransactionTest : public ::testing::Test {
339protected:
340 void SetUp() override {
341 mClient = new SurfaceComposerClient;
342 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
343
344 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700345
346 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Ady Abraham37965d42018-11-01 13:43:32 -0700347 ASSERT_NO_FATAL_FAILURE(sf->getColorManagement(&mColorManagementUsed));
Chia-I Wu718daf82017-10-20 11:57:17 -0700348 }
349
chaviw0e3479f2018-09-10 16:49:30 -0700350 virtual void TearDown() {
351 mBlackBgSurface = 0;
352 mClient->dispose();
353 mClient = 0;
354 }
355
Marissa Wallfda30bb2018-10-12 11:34:28 -0700356 virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
357 const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -0800358 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
359 auto layer =
360 createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags, parent);
Chia-I Wu718daf82017-10-20 11:57:17 -0700361
Vishnu Nair60356342018-11-13 13:00:45 -0800362 Transaction t;
363 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
Vishnu Nair60356342018-11-13 13:00:45 -0800364
365 status_t error = t.apply();
Chia-I Wu718daf82017-10-20 11:57:17 -0700366 if (error != NO_ERROR) {
367 ADD_FAILURE() << "failed to initialize SurfaceControl";
368 layer.clear();
369 }
370
371 return layer;
372 }
373
Vishnu Nair88a11f22018-11-28 18:30:57 -0800374 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
375 const char* name, uint32_t width, uint32_t height,
376 PixelFormat format, uint32_t flags,
377 SurfaceControl* parent = nullptr) {
378 auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
379 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
380 return layer;
381 }
382
Marissa Wallfda30bb2018-10-12 11:34:28 -0700383 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -0800384 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
385 return createLayer(mClient, name, width, height, flags, parent);
Marissa Wallfda30bb2018-10-12 11:34:28 -0700386 }
387
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700388 sp<SurfaceControl> createColorLayer(const char* name, const Color& color,
389 SurfaceControl* parent = nullptr) {
390 auto colorLayer = createSurface(mClient, name, 0 /* buffer width */, 0 /* buffer height */,
391 PIXEL_FORMAT_RGBA_8888,
392 ISurfaceComposerClient::eFXSurfaceColor, parent);
393 asTransaction([&](Transaction& t) {
394 t.setColor(colorLayer, half3{color.r / 255.0f, color.g / 255.0f, color.b / 255.0f});
395 t.setAlpha(colorLayer, color.a / 255.0f);
396 });
397 return colorLayer;
398 }
399
Marissa Wall61c58622018-07-18 10:12:20 -0700400 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700401 // wait for previous transactions (such as setSize) to complete
402 Transaction().apply(true);
403
404 ANativeWindow_Buffer buffer = {};
405 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
406
407 return buffer;
408 }
409
Marissa Wall61c58622018-07-18 10:12:20 -0700410 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700411 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
412
413 // wait for the newly posted buffer to be latched
414 waitForLayerBuffers();
415 }
416
Marissa Wall61c58622018-07-18 10:12:20 -0700417 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
418 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700419 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700420 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
421 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
422 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700423 }
424
Marissa Wall61c58622018-07-18 10:12:20 -0700425 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
426 int32_t bufferWidth, int32_t bufferHeight) {
427 sp<GraphicBuffer> buffer =
428 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
429 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
430 BufferUsage::COMPOSER_OVERLAY,
431 "test");
432 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
Marissa Wall861616d2018-10-22 12:52:23 -0700433 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -0700434 }
435
436 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
437 int32_t bufferWidth, int32_t bufferHeight) {
438 switch (mLayerType) {
439 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
440 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
441 break;
442 case ISurfaceComposerClient::eFXSurfaceBufferState:
443 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
444 break;
445 default:
446 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
447 }
448 }
449
450 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
451 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700452 const Color& topRight, const Color& bottomLeft,
453 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700454 switch (mLayerType) {
455 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
456 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
457 bottomLeft, bottomRight);
458 break;
459 case ISurfaceComposerClient::eFXSurfaceBufferState:
460 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
461 bottomLeft, bottomRight);
462 break;
463 default:
464 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
465 }
466 }
467
468 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
469 int32_t bufferHeight, const Color& topLeft,
470 const Color& topRight, const Color& bottomLeft,
471 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700472 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700473 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
474 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700475
Marissa Wall61c58622018-07-18 10:12:20 -0700476 const int32_t halfW = bufferWidth / 2;
477 const int32_t halfH = bufferHeight / 2;
478 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
479 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
480 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
481 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
482 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700483
Marissa Wall61c58622018-07-18 10:12:20 -0700484 postBufferQueueLayerBuffer(layer);
485 }
486
487 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
488 int32_t bufferHeight, const Color& topLeft,
489 const Color& topRight, const Color& bottomLeft,
490 const Color& bottomRight) {
491 sp<GraphicBuffer> buffer =
492 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
493 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
494 BufferUsage::COMPOSER_OVERLAY,
495 "test");
496
497 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
498
499 const int32_t halfW = bufferWidth / 2;
500 const int32_t halfH = bufferHeight / 2;
501 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
502 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
503 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
504 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
505
506 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700507 }
508
chaviw0e3479f2018-09-10 16:49:30 -0700509 std::unique_ptr<ScreenCapture> screenshot() {
510 std::unique_ptr<ScreenCapture> screenshot;
511 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700512 return screenshot;
513 }
514
Vishnu Nairb927e1f2019-02-19 13:36:15 -0800515 void asTransaction(const std::function<void(Transaction&)>& exec) {
516 Transaction t;
517 exec(t);
518 t.apply(true);
519 }
520
Marissa Wall713b63f2018-10-17 15:42:43 -0700521 static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
522 static BufferGenerator bufferGenerator;
523 return bufferGenerator.get(outBuffer, outFence);
524 }
525
Chia-I Wu718daf82017-10-20 11:57:17 -0700526 sp<SurfaceComposerClient> mClient;
527
528 sp<IBinder> mDisplay;
529 uint32_t mDisplayWidth;
530 uint32_t mDisplayHeight;
531 uint32_t mDisplayLayerStack;
Marissa Wall861616d2018-10-22 12:52:23 -0700532 Rect mDisplayRect = Rect::INVALID_RECT;
Chia-I Wu718daf82017-10-20 11:57:17 -0700533
534 // leave room for ~256 layers
535 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
536
chaviw0e3479f2018-09-10 16:49:30 -0700537 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700538 bool mColorManagementUsed;
539
Chia-I Wu718daf82017-10-20 11:57:17 -0700540private:
541 void SetUpDisplay() {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800542 mDisplay = mClient->getInternalDisplayToken();
543 ASSERT_FALSE(mDisplay == nullptr) << "failed to get display";
Chia-I Wu718daf82017-10-20 11:57:17 -0700544
545 // get display width/height
546 DisplayInfo info;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800547 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplay, &info));
Chia-I Wu718daf82017-10-20 11:57:17 -0700548 mDisplayWidth = info.w;
549 mDisplayHeight = info.h;
Marissa Wall861616d2018-10-22 12:52:23 -0700550 mDisplayRect =
551 Rect(static_cast<int32_t>(mDisplayWidth), static_cast<int32_t>(mDisplayHeight));
Chia-I Wu718daf82017-10-20 11:57:17 -0700552
553 // After a new buffer is queued, SurfaceFlinger is notified and will
554 // latch the new buffer on next vsync. Let's heuristically wait for 3
555 // vsyncs.
556 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
557
558 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700559
Vishnu Nair88a11f22018-11-28 18:30:57 -0800560 mBlackBgSurface =
561 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
562 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
chaviw0e3479f2018-09-10 16:49:30 -0700563
Chia-I Wu718daf82017-10-20 11:57:17 -0700564 // set layer stack (b/68888219)
565 Transaction t;
566 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800567 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700568 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
569 t.setColor(mBlackBgSurface, half3{0, 0, 0});
570 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700571 t.apply();
572 }
573
chaviw0e3479f2018-09-10 16:49:30 -0700574 void waitForLayerBuffers() {
575 // Request an empty transaction to get applied synchronously to ensure the buffer is
576 // latched.
577 Transaction().apply(true);
578 usleep(mBufferPostDelay);
579 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700580
581 int32_t mBufferPostDelay;
Alec Mouri80863a62019-01-17 15:19:35 -0800582
583 friend class LayerRenderPathTestHarness;
584};
585enum class RenderPath { SCREENSHOT, VIRTUAL_DISPLAY };
586
587class LayerRenderPathTestHarness {
588public:
589 LayerRenderPathTestHarness(LayerTransactionTest* delegate, RenderPath renderPath)
590 : mDelegate(delegate), mRenderPath(renderPath) {}
591
592 std::unique_ptr<ScreenCapture> getScreenCapture() {
593 switch (mRenderPath) {
594 case RenderPath::SCREENSHOT:
595 return mDelegate->screenshot();
596 case RenderPath::VIRTUAL_DISPLAY:
597
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800598 const auto mainDisplay = SurfaceComposerClient::getInternalDisplayToken();
Alec Mouri80863a62019-01-17 15:19:35 -0800599 DisplayInfo mainDisplayInfo;
600 SurfaceComposerClient::getDisplayInfo(mainDisplay, &mainDisplayInfo);
601
602 sp<IBinder> vDisplay;
603 sp<IGraphicBufferProducer> producer;
604 sp<IGraphicBufferConsumer> consumer;
605 sp<BufferItemConsumer> itemConsumer;
606 BufferQueue::createBufferQueue(&producer, &consumer);
607
608 consumer->setConsumerName(String8("Virtual disp consumer"));
609 consumer->setDefaultBufferSize(mainDisplayInfo.w, mainDisplayInfo.h);
610
611 itemConsumer = new BufferItemConsumer(consumer,
612 // Sample usage bits from screenrecord
613 GRALLOC_USAGE_HW_VIDEO_ENCODER |
614 GRALLOC_USAGE_SW_READ_OFTEN);
615
616 vDisplay = SurfaceComposerClient::createDisplay(String8("VirtualDisplay"),
617 false /*secure*/);
618
619 SurfaceComposerClient::Transaction t;
620 t.setDisplaySurface(vDisplay, producer);
621 t.setDisplayLayerStack(vDisplay, 0);
622 t.setDisplayProjection(vDisplay, mainDisplayInfo.orientation,
623 Rect(mainDisplayInfo.viewportW, mainDisplayInfo.viewportH),
624 Rect(mainDisplayInfo.w, mainDisplayInfo.h));
625 t.apply();
626 SurfaceComposerClient::Transaction().apply(true);
627 BufferItem item;
628 itemConsumer->acquireBuffer(&item, 0, true);
629 auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer);
630 itemConsumer->releaseBuffer(item);
631 SurfaceComposerClient::destroyDisplay(vDisplay);
632 return sc;
633 }
634 }
635
636protected:
637 LayerTransactionTest* mDelegate;
638 RenderPath mRenderPath;
Chia-I Wu718daf82017-10-20 11:57:17 -0700639};
640
Alec Mouri80863a62019-01-17 15:19:35 -0800641class LayerTypeTransactionHarness : public LayerTransactionTest {
Marissa Wall61c58622018-07-18 10:12:20 -0700642public:
Alec Mouri80863a62019-01-17 15:19:35 -0800643 LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {}
Marissa Wall61c58622018-07-18 10:12:20 -0700644
645 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
Alec Mouri80863a62019-01-17 15:19:35 -0800646 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700647 // if the flags already have a layer type specified, return an error
648 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
649 return nullptr;
650 }
chaviwf66724d2018-11-28 16:35:21 -0800651 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent);
Marissa Wall61c58622018-07-18 10:12:20 -0700652 }
653
654 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
655 int32_t bufferHeight) {
656 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
657 bufferWidth, bufferHeight));
658 }
659
660 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
661 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
662 const Color& bottomLeft, const Color& bottomRight) {
663 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
664 bufferWidth, bufferHeight,
665 topLeft, topRight,
666 bottomLeft, bottomRight));
667 }
668
669protected:
670 uint32_t mLayerType;
671};
672
Alec Mouri80863a62019-01-17 15:19:35 -0800673class LayerTypeTransactionTest : public LayerTypeTransactionHarness,
674 public ::testing::WithParamInterface<uint32_t> {
675public:
676 LayerTypeTransactionTest() : LayerTypeTransactionHarness(GetParam()) {}
677};
678
679class LayerTypeAndRenderTypeTransactionTest
680 : public LayerTypeTransactionHarness,
681 public ::testing::WithParamInterface<std::tuple<uint32_t, RenderPath>> {
682public:
683 LayerTypeAndRenderTypeTransactionTest()
684 : LayerTypeTransactionHarness(std::get<0>(GetParam())),
685 mRenderPathHarness(LayerRenderPathTestHarness(this, std::get<1>(GetParam()))) {}
686
687 std::unique_ptr<ScreenCapture> getScreenCapture() {
688 return mRenderPathHarness.getScreenCapture();
689 }
690
691protected:
692 LayerRenderPathTestHarness mRenderPathHarness;
693};
694
695// Environment for starting up binder threads. This is required for testing
696// virtual displays, as BufferQueue parameters may be queried over binder.
697class BinderEnvironment : public ::testing::Environment {
698public:
699 void SetUp() override { ProcessState::self()->startThreadPool(); }
700};
701
702::testing::Environment* const binderEnv =
703 ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
704
705class LayerRenderTypeTransactionTest : public LayerTransactionTest,
706 public ::testing::WithParamInterface<RenderPath> {
707public:
708 LayerRenderTypeTransactionTest() : mHarness(LayerRenderPathTestHarness(this, GetParam())) {}
709
710 std::unique_ptr<ScreenCapture> getScreenCapture() { return mHarness.getScreenCapture(); }
711 void setRelativeZBasicHelper(uint32_t layerType);
712 void setRelativeZGroupHelper(uint32_t layerType);
713 void setAlphaBasicHelper(uint32_t layerType);
Valerie Haudd0b7572019-01-29 14:59:27 -0800714 void setBackgroundColorHelper(uint32_t layerType, bool priorColor, bool bufferFill, float alpha,
715 Color finalColor);
Alec Mouri80863a62019-01-17 15:19:35 -0800716
717protected:
718 LayerRenderPathTestHarness mHarness;
719};
720
721INSTANTIATE_TEST_CASE_P(
722 LayerTypeAndRenderTypeTransactionTests, LayerTypeAndRenderTypeTransactionTest,
723 ::testing::Combine(
724 ::testing::Values(
725 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
726 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)),
727 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT)));
728
729INSTANTIATE_TEST_CASE_P(LayerRenderTypeTransactionTests, LayerRenderTypeTransactionTest,
730 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT));
731
Marissa Wall61c58622018-07-18 10:12:20 -0700732INSTANTIATE_TEST_CASE_P(
733 LayerTypeTransactionTests, LayerTypeTransactionTest,
734 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
735 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
736
Alec Mouri80863a62019-01-17 15:19:35 -0800737TEST_P(LayerRenderTypeTransactionTest, SetPositionBasic_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700738 sp<SurfaceControl> layer;
739 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700740 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700741
742 {
743 SCOPED_TRACE("default position");
Marissa Wall861616d2018-10-22 12:52:23 -0700744 const Rect rect(0, 0, 32, 32);
Alec Mouri80863a62019-01-17 15:19:35 -0800745 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700746 shot->expectColor(rect, Color::RED);
747 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700748 }
749
750 Transaction().setPosition(layer, 5, 10).apply();
751 {
752 SCOPED_TRACE("new position");
Marissa Wall861616d2018-10-22 12:52:23 -0700753 const Rect rect(5, 10, 37, 42);
Alec Mouri80863a62019-01-17 15:19:35 -0800754 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700755 shot->expectColor(rect, Color::RED);
756 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700757 }
758}
759
Alec Mouri80863a62019-01-17 15:19:35 -0800760TEST_P(LayerRenderTypeTransactionTest, SetPositionRounding_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700761 sp<SurfaceControl> layer;
762 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700763 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700764
765 // GLES requires only 4 bits of subpixel precision during rasterization
766 // XXX GLES composition does not match HWC composition due to precision
767 // loss (b/69315223)
768 const float epsilon = 1.0f / 16.0f;
769 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
770 {
771 SCOPED_TRACE("rounding down");
Alec Mouri80863a62019-01-17 15:19:35 -0800772 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700773 }
774
775 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
776 {
777 SCOPED_TRACE("rounding up");
Alec Mouri80863a62019-01-17 15:19:35 -0800778 getScreenCapture()->expectColor(Rect(1, 1, 33, 33), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700779 }
780}
781
Alec Mouri80863a62019-01-17 15:19:35 -0800782TEST_P(LayerRenderTypeTransactionTest, SetPositionOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700783 sp<SurfaceControl> layer;
784 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700785 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700786
787 Transaction().setPosition(layer, -32, -32).apply();
788 {
789 SCOPED_TRACE("negative coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800790 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700791 }
792
793 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
794 {
795 SCOPED_TRACE("positive coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800796 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700797 }
798}
799
Alec Mouri80863a62019-01-17 15:19:35 -0800800TEST_P(LayerRenderTypeTransactionTest, SetPositionPartiallyOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700801 sp<SurfaceControl> layer;
802 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700803 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700804
805 // partially out of bounds
806 Transaction().setPosition(layer, -30, -30).apply();
807 {
808 SCOPED_TRACE("negative coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800809 getScreenCapture()->expectColor(Rect(0, 0, 2, 2), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700810 }
811
812 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
813 {
814 SCOPED_TRACE("positive coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800815 getScreenCapture()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
816 mDisplayHeight),
817 Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700818 }
819}
820
Alec Mouri80863a62019-01-17 15:19:35 -0800821TEST_P(LayerRenderTypeTransactionTest, SetPositionWithResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700822 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700823 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
824 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700825
826 // setPosition is applied immediately by default, with or without resize
827 // pending
828 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
829 {
830 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800831 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700832 const Rect rect(5, 10, 37, 42);
Marissa Wall61c58622018-07-18 10:12:20 -0700833 shot->expectColor(rect, Color::RED);
834 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700835 }
836
Marissa Wall861616d2018-10-22 12:52:23 -0700837 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700838 {
839 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800840 getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700841 }
842}
843
Alec Mouri80863a62019-01-17 15:19:35 -0800844TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700845 sp<SurfaceControl> layer;
846 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700847 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700848
849 // request setPosition to be applied with the next resize
850 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
851 {
852 SCOPED_TRACE("new position pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800853 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700854 }
855
856 Transaction().setPosition(layer, 15, 20).apply();
857 {
858 SCOPED_TRACE("pending new position modified");
Alec Mouri80863a62019-01-17 15:19:35 -0800859 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700860 }
861
862 Transaction().setSize(layer, 64, 64).apply();
863 {
864 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800865 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700866 }
867
868 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700869 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700870 {
871 SCOPED_TRACE("new position applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800872 getScreenCapture()->expectColor(Rect(15, 20, 79, 84), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700873 }
874}
875
Alec Mouri80863a62019-01-17 15:19:35 -0800876TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700877 sp<SurfaceControl> layer;
878 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700879 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700880
881 // setPosition is not immediate even with SCALE_TO_WINDOW override
882 Transaction()
883 .setPosition(layer, 5, 10)
884 .setSize(layer, 64, 64)
885 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
886 .setGeometryAppliesWithResize(layer)
887 .apply();
888 {
889 SCOPED_TRACE("new position pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800890 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700891 }
892
Marissa Wall61c58622018-07-18 10:12:20 -0700893 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700894 {
895 SCOPED_TRACE("new position applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800896 getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700897 }
898}
899
Alec Mouri80863a62019-01-17 15:19:35 -0800900TEST_P(LayerRenderTypeTransactionTest, SetSizeBasic_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700901 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700902 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
903 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700904
905 Transaction().setSize(layer, 64, 64).apply();
906 {
907 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800908 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700909 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -0700910 shot->expectColor(rect, Color::RED);
911 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700912 }
913
Marissa Wall861616d2018-10-22 12:52:23 -0700914 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700915 {
916 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800917 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700918 const Rect rect(0, 0, 64, 64);
919 shot->expectColor(rect, Color::RED);
920 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700921 }
922}
923
Alec Mouri80863a62019-01-17 15:19:35 -0800924TEST_P(LayerTypeAndRenderTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700925 // cannot test robustness against invalid sizes (zero or really huge)
926}
927
Alec Mouri80863a62019-01-17 15:19:35 -0800928TEST_P(LayerRenderTypeTransactionTest, SetSizeWithScaleToWindow_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700929 sp<SurfaceControl> layer;
930 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700931 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700932
933 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
934 Transaction()
935 .setSize(layer, 64, 64)
936 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
937 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -0800938 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700939}
940
Alec Mouri80863a62019-01-17 15:19:35 -0800941TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700942 sp<SurfaceControl> layerR;
943 sp<SurfaceControl> layerG;
944 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700945 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700946 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700947 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700948
949 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
950 {
951 SCOPED_TRACE("layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800952 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700953 }
954
955 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
956 {
957 SCOPED_TRACE("layerG");
Alec Mouri80863a62019-01-17 15:19:35 -0800958 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700959 }
960}
961
Alec Mouri80863a62019-01-17 15:19:35 -0800962TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700963 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800964 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700965 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800966 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700967 sp<SurfaceControl> layerR;
968 sp<SurfaceControl> layerG;
969 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700970 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700971 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700972 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700973
chaviw0e3479f2018-09-10 16:49:30 -0700974 Transaction()
975 .reparent(layerR, parent->getHandle())
976 .reparent(layerG, parent->getHandle())
977 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700978 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
979 {
980 SCOPED_TRACE("layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800981 auto shot = getScreenCapture();
chaviw0e3479f2018-09-10 16:49:30 -0700982 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700983 }
984
985 Transaction().setLayer(layerR, -3).apply();
986 {
987 SCOPED_TRACE("layerG");
Alec Mouri80863a62019-01-17 15:19:35 -0800988 auto shot = getScreenCapture();
chaviw0e3479f2018-09-10 16:49:30 -0700989 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700990 }
991}
992
Alec Mouri80863a62019-01-17 15:19:35 -0800993void LayerRenderTypeTransactionTest::setRelativeZBasicHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700994 sp<SurfaceControl> layerR;
995 sp<SurfaceControl> layerG;
Marissa Wall861616d2018-10-22 12:52:23 -0700996 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32, layerType));
997 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
998 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32, layerType));
999 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001000
Marissa Wall861616d2018-10-22 12:52:23 -07001001 switch (layerType) {
1002 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1003 Transaction()
1004 .setPosition(layerG, 16, 16)
1005 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1006 .apply();
1007 break;
1008 case ISurfaceComposerClient::eFXSurfaceBufferState:
1009 Transaction()
1010 .setFrame(layerR, Rect(0, 0, 32, 32))
1011 .setFrame(layerG, Rect(16, 16, 48, 48))
1012 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1013 .apply();
1014 break;
1015 default:
1016 ASSERT_FALSE(true) << "Unsupported layer type";
1017 }
Chia-I Wu49313302017-10-31 10:14:40 -07001018 {
1019 SCOPED_TRACE("layerG above");
Alec Mouri80863a62019-01-17 15:19:35 -08001020 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001021 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1022 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
1023 }
1024
1025 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
1026 {
1027 SCOPED_TRACE("layerG below");
Alec Mouri80863a62019-01-17 15:19:35 -08001028 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001029 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1030 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
1031 }
1032}
1033
Alec Mouri80863a62019-01-17 15:19:35 -08001034TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -07001035 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1036}
1037
Alec Mouri80863a62019-01-17 15:19:35 -08001038TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001039 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1040}
1041
Marissa Wall61c58622018-07-18 10:12:20 -07001042TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -07001043 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -08001044 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -07001045 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -08001046 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wuec2d9852017-11-21 09:21:01 -08001047 sp<SurfaceControl> layerR;
1048 sp<SurfaceControl> layerG;
1049 sp<SurfaceControl> layerB;
1050 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001051 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -08001052 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001053 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -08001054 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001055 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -08001056
chaviw0e3479f2018-09-10 16:49:30 -07001057 Transaction()
1058 .reparent(layerB, parent->getHandle())
1059 .apply();
1060
Chia-I Wuec2d9852017-11-21 09:21:01 -08001061 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
1062 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
1063
chaviw0e3479f2018-09-10 16:49:30 -07001064 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -08001065 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -07001066 sp<IBinder> parentHandle = parent->getHandle();
Marissa Wall861616d2018-10-22 12:52:23 -07001067 ScreenCapture::captureLayers(&screenshot, parentHandle, Rect(0, 0, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -08001068 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1069}
1070
chaviw32377582019-05-13 11:15:19 -07001071TEST_P(LayerTypeTransactionTest, SetLayerAndRelative) {
1072 sp<SurfaceControl> parent =
1073 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
1074 ISurfaceComposerClient::eFXSurfaceColor);
1075
1076 sp<SurfaceControl> childLayer;
1077 ASSERT_NO_FATAL_FAILURE(
1078 childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
1079 0 /* buffer height */,
1080 ISurfaceComposerClient::eFXSurfaceColor,
1081 parent.get()));
1082 Transaction()
1083 .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
1084 .setColor(parent, half3{0.0f, 0.0f, 0.0f})
1085 .show(childLayer)
1086 .show(parent)
1087 .setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight))
1088 .setCrop_legacy(childLayer, Rect(0, 0, 20, 30))
1089 .apply();
1090
1091 Transaction()
1092 .setRelativeLayer(childLayer, parent->getHandle(), -1)
1093 .setLayer(childLayer, 1)
1094 .apply();
1095
1096 {
1097 SCOPED_TRACE("setLayer above");
1098 // Set layer should get applied and place the child above.
1099 std::unique_ptr<ScreenCapture> screenshot;
1100 ScreenCapture::captureScreen(&screenshot);
1101 screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
1102 }
1103
1104 Transaction()
1105 .setLayer(childLayer, 1)
1106 .setRelativeLayer(childLayer, parent->getHandle(), -1)
1107 .apply();
1108
1109 {
1110 SCOPED_TRACE("setRelative below");
1111 // Set relative layer should get applied and place the child below.
1112 std::unique_ptr<ScreenCapture> screenshot;
1113 ScreenCapture::captureScreen(&screenshot);
1114 screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
1115 }
1116}
1117
Robert Carr1c5481e2019-07-01 14:42:27 -07001118TEST_P(LayerTypeTransactionTest, HideRelativeParentHidesLayer) {
1119 sp<SurfaceControl> parent =
1120 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
1121 ISurfaceComposerClient::eFXSurfaceColor);
1122 sp<SurfaceControl> relativeParent =
1123 LayerTransactionTest::createLayer("RelativeParent", 0 /* buffer width */,
1124 0 /* buffer height */, ISurfaceComposerClient::eFXSurfaceColor);
1125
1126 sp<SurfaceControl> childLayer;
1127 ASSERT_NO_FATAL_FAILURE(
1128 childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
1129 0 /* buffer height */,
1130 ISurfaceComposerClient::eFXSurfaceColor,
1131 parent.get()));
1132 Transaction()
1133 .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
1134 .setColor(parent, half3{0.0f, 0.0f, 0.0f})
1135 .setColor(relativeParent, half3{0.0f, 1.0f, 0.0f})
1136 .show(childLayer)
1137 .show(parent)
1138 .show(relativeParent)
1139 .setLayer(parent, mLayerZBase - 1)
1140 .setLayer(relativeParent, mLayerZBase)
1141 .apply();
1142
1143 Transaction()
1144 .setRelativeLayer(childLayer, relativeParent->getHandle(), 1)
1145 .apply();
1146
1147 {
1148 SCOPED_TRACE("setLayer above");
1149 // Set layer should get applied and place the child above.
1150 std::unique_ptr<ScreenCapture> screenshot;
1151 ScreenCapture::captureScreen(&screenshot);
1152 screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
1153 }
1154
1155 Transaction()
1156 .hide(relativeParent)
1157 .apply();
1158
1159 {
1160 SCOPED_TRACE("hide relative parent");
1161 // The relative should no longer be visible.
1162 std::unique_ptr<ScreenCapture> screenshot;
1163 ScreenCapture::captureScreen(&screenshot);
1164 screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
1165 }
1166}
1167
Alec Mouri80863a62019-01-17 15:19:35 -08001168void LayerRenderTypeTransactionTest::setRelativeZGroupHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -07001169 sp<SurfaceControl> layerR;
1170 sp<SurfaceControl> layerG;
1171 sp<SurfaceControl> layerB;
Marissa Wall861616d2018-10-22 12:52:23 -07001172 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test", 32, 32, layerType));
1173 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
1174 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test", 32, 32, layerType));
1175 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
1176 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test", 32, 32, layerType));
1177 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001178
1179 // layerR = 0, layerG = layerR + 3, layerB = 2
Marissa Wall861616d2018-10-22 12:52:23 -07001180 switch (layerType) {
1181 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1182 Transaction()
1183 .setPosition(layerG, 8, 8)
1184 .setRelativeLayer(layerG, layerR->getHandle(), 3)
1185 .setPosition(layerB, 16, 16)
1186 .setLayer(layerB, mLayerZBase + 2)
1187 .apply();
1188 break;
1189 case ISurfaceComposerClient::eFXSurfaceBufferState:
1190 Transaction()
1191 .setFrame(layerR, Rect(0, 0, 32, 32))
1192 .setFrame(layerG, Rect(8, 8, 40, 40))
1193 .setRelativeLayer(layerG, layerR->getHandle(), 3)
1194 .setFrame(layerB, Rect(16, 16, 48, 48))
1195 .setLayer(layerB, mLayerZBase + 2)
1196 .apply();
1197 break;
1198 default:
1199 ASSERT_FALSE(true) << "Unsupported layer type";
1200 }
1201
Chia-I Wu49313302017-10-31 10:14:40 -07001202 {
1203 SCOPED_TRACE("(layerR < layerG) < layerB");
Alec Mouri80863a62019-01-17 15:19:35 -08001204 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001205 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
1206 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
1207 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
1208 }
1209
1210 // layerR = 4, layerG = layerR + 3, layerB = 2
1211 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
1212 {
1213 SCOPED_TRACE("layerB < (layerR < layerG)");
Alec Mouri80863a62019-01-17 15:19:35 -08001214 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001215 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
1216 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
1217 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
1218 }
1219
1220 // layerR = 4, layerG = layerR - 3, layerB = 2
1221 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
1222 {
1223 SCOPED_TRACE("layerB < (layerG < layerR)");
Alec Mouri80863a62019-01-17 15:19:35 -08001224 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001225 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1226 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
1227 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
1228 }
1229
1230 // restore to absolute z
1231 // layerR = 4, layerG = 0, layerB = 2
1232 Transaction().setLayer(layerG, mLayerZBase).apply();
1233 {
1234 SCOPED_TRACE("layerG < layerB < layerR");
Alec Mouri80863a62019-01-17 15:19:35 -08001235 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001236 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1237 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
1238 }
1239
1240 // layerR should not affect layerG anymore
1241 // layerR = 1, layerG = 0, layerB = 2
1242 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
1243 {
1244 SCOPED_TRACE("layerG < layerR < layerB");
Alec Mouri80863a62019-01-17 15:19:35 -08001245 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -07001246 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1247 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
1248 }
1249}
1250
Alec Mouri80863a62019-01-17 15:19:35 -08001251TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -07001252 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1253}
1254
Alec Mouri80863a62019-01-17 15:19:35 -08001255TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001256 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1257}
1258
Alec Mouri80863a62019-01-17 15:19:35 -08001259TEST_P(LayerTypeAndRenderTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -07001260 sp<SurfaceControl> layerR;
1261 sp<SurfaceControl> layerG;
1262
1263 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001264 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001265 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001266 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001267
1268 Transaction()
1269 .setPosition(layerG, 16, 16)
1270 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1271 .apply();
1272
Robert Carr87246532019-02-04 15:20:26 -08001273 layerG.clear();
Chia-I Wu49313302017-10-31 10:14:40 -07001274 // layerG should have been removed
Alec Mouri80863a62019-01-17 15:19:35 -08001275 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu49313302017-10-31 10:14:40 -07001276}
1277
Alec Mouri80863a62019-01-17 15:19:35 -08001278TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001279 sp<SurfaceControl> layer;
1280 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001281 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001282
1283 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
1284 {
1285 SCOPED_TRACE("layer hidden");
Alec Mouri80863a62019-01-17 15:19:35 -08001286 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu57b27502017-10-31 10:14:40 -07001287 }
1288
1289 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1290 {
1291 SCOPED_TRACE("layer shown");
Alec Mouri80863a62019-01-17 15:19:35 -08001292 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu57b27502017-10-31 10:14:40 -07001293 }
1294}
1295
Alec Mouri80863a62019-01-17 15:19:35 -08001296TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001297 const Color translucentRed = {100, 0, 0, 100};
1298 sp<SurfaceControl> layerR;
1299 sp<SurfaceControl> layerG;
1300 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001301 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001302 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001303 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001304
1305 Transaction()
1306 .setLayer(layerR, mLayerZBase + 1)
1307 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1308 .apply();
1309 {
1310 SCOPED_TRACE("layerR opaque");
Alec Mouri80863a62019-01-17 15:19:35 -08001311 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
Chia-I Wu57b27502017-10-31 10:14:40 -07001312 }
1313
1314 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1315 {
1316 SCOPED_TRACE("layerR translucent");
1317 const uint8_t g = uint8_t(255 - translucentRed.a);
Alec Mouri80863a62019-01-17 15:19:35 -08001318 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
Chia-I Wu57b27502017-10-31 10:14:40 -07001319 }
1320}
1321
Marissa Wall61c58622018-07-18 10:12:20 -07001322TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001323 sp<SurfaceControl> layer;
1324 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001325 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001326
1327 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001328 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001329 Transaction()
1330 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1331 .apply(true);
1332 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001333 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001334
1335 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1336 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001337 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001338}
1339
Robert Carrfa8855f2019-02-19 10:05:00 -08001340/** RAII Wrapper around get/seteuid */
1341class UIDFaker {
1342 uid_t oldId;
1343public:
1344 UIDFaker(uid_t uid) {
1345 oldId = geteuid();
1346 seteuid(uid);
1347 }
1348 ~UIDFaker() {
1349 seteuid(oldId);
1350 }
1351};
1352
1353TEST_F(LayerTransactionTest, SetFlagsSecureEUidSystem) {
1354 sp<SurfaceControl> layer;
1355 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1356 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
1357
1358 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
1359 sp<GraphicBuffer> outBuffer;
1360 Transaction()
1361 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1362 .apply(true);
1363 ASSERT_EQ(PERMISSION_DENIED,
1364 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1365
1366 UIDFaker f(AID_SYSTEM);
1367
1368 // By default the system can capture screenshots with secure layers but they
1369 // will be blacked out
1370 ASSERT_EQ(NO_ERROR,
1371 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1372
1373 {
1374 SCOPED_TRACE("as system");
1375 auto shot = screenshot();
1376 shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1377 }
1378
1379 // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
1380 // to receive them...we are expected to take care with the results.
Robert Carr108b2c72019-04-02 16:32:58 -07001381 bool outCapturedSecureLayers;
Robert Carrfa8855f2019-02-19 10:05:00 -08001382 ASSERT_EQ(NO_ERROR,
Robert Carr108b2c72019-04-02 16:32:58 -07001383 composer->captureScreen(mDisplay, &outBuffer, outCapturedSecureLayers,
1384 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(), 0,
1385 0, false, ISurfaceComposer::eRotateNone, true));
1386 ASSERT_EQ(true, outCapturedSecureLayers);
Robert Carrfa8855f2019-02-19 10:05:00 -08001387 ScreenCapture sc(outBuffer);
1388 sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
1389}
1390
Alec Mouri80863a62019-01-17 15:19:35 -08001391TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001392 const Rect top(0, 0, 32, 16);
1393 const Rect bottom(0, 16, 32, 32);
1394 sp<SurfaceControl> layer;
1395 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1396
1397 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001398 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1399 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1400 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001401 // setTransparentRegionHint always applies to the following buffer
1402 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001403 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001404 {
1405 SCOPED_TRACE("top transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001406 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001407 shot->expectColor(top, Color::BLACK);
1408 shot->expectColor(bottom, Color::RED);
1409 }
1410
1411 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1412 {
1413 SCOPED_TRACE("transparent region hint pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001414 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001415 shot->expectColor(top, Color::BLACK);
1416 shot->expectColor(bottom, Color::RED);
1417 }
1418
Marissa Wall61c58622018-07-18 10:12:20 -07001419 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1420 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1421 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1422 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001423 {
1424 SCOPED_TRACE("bottom transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001425 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001426 shot->expectColor(top, Color::RED);
1427 shot->expectColor(bottom, Color::BLACK);
1428 }
1429}
1430
Alec Mouri80863a62019-01-17 15:19:35 -08001431TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001432 const Rect top(0, 0, 32, 16);
1433 const Rect bottom(0, 16, 32, 32);
1434 sp<SurfaceControl> layer;
1435 ASSERT_NO_FATAL_FAILURE(
1436 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1437
1438 sp<GraphicBuffer> buffer =
1439 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1440 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1441 BufferUsage::COMPOSER_OVERLAY,
1442 "test");
1443
1444 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1445 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1446 Transaction()
1447 .setTransparentRegionHint(layer, Region(top))
1448 .setBuffer(layer, buffer)
Marissa Wall861616d2018-10-22 12:52:23 -07001449 .setFrame(layer, Rect(0, 0, 32, 32))
Marissa Wall61c58622018-07-18 10:12:20 -07001450 .apply();
1451 {
1452 SCOPED_TRACE("top transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001453 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001454 shot->expectColor(top, Color::BLACK);
1455 shot->expectColor(bottom, Color::RED);
1456 }
1457
1458 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1459 {
1460 SCOPED_TRACE("transparent region hint intermediate");
Alec Mouri80863a62019-01-17 15:19:35 -08001461 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001462 shot->expectColor(top, Color::BLACK);
1463 shot->expectColor(bottom, Color::BLACK);
1464 }
1465
1466 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1467 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1468 BufferUsage::COMPOSER_OVERLAY,
1469 "test");
1470
1471 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1472 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
Marissa Wall861616d2018-10-22 12:52:23 -07001473 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001474 {
1475 SCOPED_TRACE("bottom transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001476 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001477 shot->expectColor(top, Color::RED);
1478 shot->expectColor(bottom, Color::BLACK);
1479 }
1480}
1481
Alec Mouri80863a62019-01-17 15:19:35 -08001482TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001483 sp<SurfaceControl> layerTransparent;
1484 sp<SurfaceControl> layerR;
1485 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1486 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1487
1488 // check that transparent region hint is bound by the layer size
1489 Transaction()
Marissa Wall861616d2018-10-22 12:52:23 -07001490 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001491 .setPosition(layerR, 16, 16)
1492 .setLayer(layerR, mLayerZBase + 1)
1493 .apply();
Marissa Wall861616d2018-10-22 12:52:23 -07001494 ASSERT_NO_FATAL_FAILURE(
1495 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1496 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerR, Color::RED, 32, 32));
Alec Mouri80863a62019-01-17 15:19:35 -08001497 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001498}
1499
Alec Mouri80863a62019-01-17 15:19:35 -08001500TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001501 sp<SurfaceControl> layerTransparent;
1502 sp<SurfaceControl> layerR;
1503 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1504 ASSERT_NO_FATAL_FAILURE(
1505 layerR = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1506
1507 // check that transparent region hint is bound by the layer size
1508 Transaction()
1509 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
1510 .setFrame(layerR, Rect(16, 16, 48, 48))
1511 .setLayer(layerR, mLayerZBase + 1)
1512 .apply();
1513 ASSERT_NO_FATAL_FAILURE(
1514 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1515 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layerR, Color::RED, 32, 32));
Alec Mouri80863a62019-01-17 15:19:35 -08001516 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
Marissa Wall861616d2018-10-22 12:52:23 -07001517}
1518
Alec Mouri80863a62019-01-17 15:19:35 -08001519void LayerRenderTypeTransactionTest::setAlphaBasicHelper(uint32_t layerType) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001520 sp<SurfaceControl> layer1;
1521 sp<SurfaceControl> layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07001522 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32, layerType));
1523 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32, layerType));
1524 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer1, {64, 0, 0, 255}, 32, 32));
1525 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001526
Marissa Wall861616d2018-10-22 12:52:23 -07001527 switch (layerType) {
1528 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1529 Transaction()
1530 .setAlpha(layer1, 0.25f)
1531 .setAlpha(layer2, 0.75f)
1532 .setPosition(layer2, 16, 0)
1533 .setLayer(layer2, mLayerZBase + 1)
1534 .apply();
1535 break;
1536 case ISurfaceComposerClient::eFXSurfaceBufferState:
1537 Transaction()
1538 .setAlpha(layer1, 0.25f)
1539 .setAlpha(layer2, 0.75f)
1540 .setFrame(layer1, Rect(0, 0, 32, 32))
1541 .setFrame(layer2, Rect(16, 0, 48, 32))
1542 .setLayer(layer2, mLayerZBase + 1)
1543 .apply();
1544 break;
1545 default:
1546 ASSERT_FALSE(true) << "Unsupported layer type";
1547 }
Chia-I Wua8a515e2017-11-01 15:16:35 -07001548 {
Alec Mouri80863a62019-01-17 15:19:35 -08001549 auto shot = getScreenCapture();
Chia-I Wua8a515e2017-11-01 15:16:35 -07001550 uint8_t r = 16; // 64 * 0.25f
1551 uint8_t g = 48; // 64 * 0.75f
1552 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1553 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1554
1555 r /= 4; // r * (1.0f - 0.75f)
1556 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1557 }
1558}
1559
Alec Mouri80863a62019-01-17 15:19:35 -08001560TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -07001561 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1562}
1563
Alec Mouri80863a62019-01-17 15:19:35 -08001564TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001565 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1566}
1567
Alec Mouri80863a62019-01-17 15:19:35 -08001568TEST_P(LayerTypeAndRenderTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001569 const Color color = {64, 0, 0, 255};
1570 sp<SurfaceControl> layer;
1571 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001572 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001573
1574 Transaction().setAlpha(layer, 2.0f).apply();
1575 {
1576 SCOPED_TRACE("clamped to 1.0f");
Alec Mouri80863a62019-01-17 15:19:35 -08001577 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), color);
Chia-I Wua8a515e2017-11-01 15:16:35 -07001578 }
1579
1580 Transaction().setAlpha(layer, -1.0f).apply();
1581 {
1582 SCOPED_TRACE("clamped to 0.0f");
Alec Mouri80863a62019-01-17 15:19:35 -08001583 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wua8a515e2017-11-01 15:16:35 -07001584 }
1585}
1586
Alec Mouri80863a62019-01-17 15:19:35 -08001587TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadius) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001588 sp<SurfaceControl> layer;
1589 const uint8_t size = 64;
1590 const uint8_t testArea = 4;
Lucas Dupina1d0e312018-12-04 22:30:27 -08001591 const float cornerRadius = 20.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001592 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1593 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1594
1595 Transaction()
1596 .setCornerRadius(layer, cornerRadius)
1597 .apply();
1598 {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001599 const uint8_t bottom = size - 1;
1600 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001601 auto shot = getScreenCapture();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001602 // Transparent corners
1603 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
Lucas Dupina1d0e312018-12-04 22:30:27 -08001604 shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
1605 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1606 shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
1607 }
1608}
1609
Alec Mouri80863a62019-01-17 15:19:35 -08001610TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildCrop) {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001611 sp<SurfaceControl> parent;
1612 sp<SurfaceControl> child;
1613 const uint8_t size = 64;
1614 const uint8_t testArea = 4;
1615 const float cornerRadius = 20.0f;
1616 ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
1617 ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
1618 ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
1619 ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
1620
1621 Transaction()
1622 .setCornerRadius(parent, cornerRadius)
1623 .reparent(child, parent->getHandle())
1624 .setPosition(child, 0, size / 2)
1625 .apply();
1626 {
1627 const uint8_t bottom = size - 1;
1628 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001629 auto shot = getScreenCapture();
Lucas Dupina1d0e312018-12-04 22:30:27 -08001630 // Top edge of child should not have rounded corners because it's translated in the parent
1631 shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
1632 Color::GREEN);
1633 // But bottom edges should have been clipped according to parent bounds
1634 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1635 shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001636 }
1637}
1638
Alec Mouri80863a62019-01-17 15:19:35 -08001639TEST_P(LayerRenderTypeTransactionTest, SetColorBasic) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001640 sp<SurfaceControl> bufferLayer;
1641 sp<SurfaceControl> colorLayer;
1642 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001643 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001644 ASSERT_NO_FATAL_FAILURE(colorLayer =
1645 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1646 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001647
Vishnu Nair88a11f22018-11-28 18:30:57 -08001648 Transaction()
1649 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1650 .setLayer(colorLayer, mLayerZBase + 1)
1651 .apply();
1652
Chia-I Wue4ef6102017-11-01 15:16:35 -07001653 {
1654 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08001655 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001656 }
1657
1658 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1659 const Color expected = {15, 51, 85, 255};
1660 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1661 // channel) should be less than one
1662 const uint8_t tolerance = 1;
1663 Transaction().setColor(colorLayer, color).apply();
1664 {
1665 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08001666 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001667 }
1668}
1669
Valerie Haudd0b7572019-01-29 14:59:27 -08001670// RED: Color layer base color and BufferQueueLayer/BufferStateLayer fill
1671// BLUE: prior background color
1672// GREEN: final background color
1673// BLACK: no color or fill
1674void LayerRenderTypeTransactionTest::setBackgroundColorHelper(uint32_t layerType, bool priorColor,
1675 bool bufferFill, float alpha,
1676 Color finalColor) {
1677 sp<SurfaceControl> layer;
1678 int32_t width = 500;
1679 int32_t height = 500;
Valerie Haua72e2812019-01-23 13:40:39 -08001680
Valerie Haudd0b7572019-01-29 14:59:27 -08001681 Color fillColor = Color::RED;
1682 Color priorBgColor = Color::BLUE;
1683 Color expectedColor = Color::BLACK;
1684 switch (layerType) {
1685 case ISurfaceComposerClient::eFXSurfaceColor:
1686 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 0, 0, layerType));
1687 Transaction()
1688 .setCrop_legacy(layer, Rect(0, 0, width, height))
1689 .setColor(layer, half3(1.0f, 0, 0))
1690 .apply();
1691 expectedColor = fillColor;
1692 break;
1693 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1694 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
1695 if (bufferFill) {
1696 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, fillColor, width, height));
1697 expectedColor = fillColor;
1698 }
1699 Transaction().setCrop_legacy(layer, Rect(0, 0, width, height)).apply();
1700 break;
1701 case ISurfaceComposerClient::eFXSurfaceBufferState:
1702 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height, layerType));
1703 if (bufferFill) {
1704 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, fillColor, width, height));
1705 expectedColor = fillColor;
1706 }
1707 Transaction().setFrame(layer, Rect(0, 0, width, height)).apply();
1708 break;
1709 default:
1710 GTEST_FAIL() << "Unknown layer type in setBackgroundColorHelper";
1711 return;
Valerie Haua72e2812019-01-23 13:40:39 -08001712 }
1713
Valerie Haudd0b7572019-01-29 14:59:27 -08001714 if (priorColor && layerType != ISurfaceComposerClient::eFXSurfaceColor) {
1715 Transaction()
1716 .setBackgroundColor(layer, half3(0, 0, 1.0f), 1.0f, ui::Dataspace::UNKNOWN)
1717 .apply();
1718 if (!bufferFill) {
1719 expectedColor = priorBgColor;
1720 }
1721 }
1722
1723 {
1724 SCOPED_TRACE("default before setting background color layer");
1725 screenshot()->expectColor(Rect(0, 0, width, height), expectedColor);
1726 }
Valerie Haua72e2812019-01-23 13:40:39 -08001727 Transaction()
Valerie Haudd0b7572019-01-29 14:59:27 -08001728 .setBackgroundColor(layer, half3(0, 1.0f, 0), alpha, ui::Dataspace::UNKNOWN)
Valerie Haua72e2812019-01-23 13:40:39 -08001729 .apply();
1730
1731 {
Valerie Haua72e2812019-01-23 13:40:39 -08001732 auto shot = screenshot();
Valerie Haudd0b7572019-01-29 14:59:27 -08001733 shot->expectColor(Rect(0, 0, width, height), finalColor);
1734 shot->expectBorder(Rect(0, 0, width, height), Color::BLACK);
Valerie Haua72e2812019-01-23 13:40:39 -08001735 }
1736}
1737
Valerie Haudd0b7572019-01-29 14:59:27 -08001738TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_Color_NoEffect) {
1739 bool priorColor = false;
1740 bool bufferFill = false;
1741 float alpha = 1.0f;
1742 Color finalColor = Color::RED;
1743 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceColor,
1744 priorColor, bufferFill, alpha, finalColor));
1745}
Valerie Haua72e2812019-01-23 13:40:39 -08001746
Valerie Haudd0b7572019-01-29 14:59:27 -08001747TEST_P(LayerRenderTypeTransactionTest,
1748 SetBackgroundColor_BufferQueue_BufferFill_NoPriorColor_Basic) {
1749 bool priorColor = false;
1750 bool bufferFill = true;
1751 float alpha = 1.0f;
1752 Color finalColor = Color::RED;
1753 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1754 priorColor, bufferFill, alpha, finalColor));
1755}
Valerie Haua72e2812019-01-23 13:40:39 -08001756
Valerie Haudd0b7572019-01-29 14:59:27 -08001757TEST_P(LayerRenderTypeTransactionTest,
1758 SetBackgroundColor_BufferQueue_NoBufferFill_NoPriorColor_Basic) {
1759 bool priorColor = false;
1760 bool bufferFill = false;
1761 float alpha = 1.0f;
1762 Color finalColor = Color::GREEN;
1763 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1764 priorColor, bufferFill, alpha, finalColor));
1765}
Valerie Haua72e2812019-01-23 13:40:39 -08001766
Valerie Haudd0b7572019-01-29 14:59:27 -08001767TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_BufferQueue_BufferFill_PriorColor_Basic) {
1768 bool priorColor = true;
1769 bool bufferFill = true;
1770 float alpha = 1.0f;
1771 Color finalColor = Color::RED;
1772 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1773 priorColor, bufferFill, alpha, finalColor));
1774}
1775
1776TEST_P(LayerRenderTypeTransactionTest,
1777 SetBackgroundColor_BufferQueue_NoBufferFill_PriorColor_Basic) {
1778 bool priorColor = true;
1779 bool bufferFill = false;
1780 float alpha = 1.0f;
1781 Color finalColor = Color::GREEN;
1782 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1783 priorColor, bufferFill, alpha, finalColor));
1784}
1785TEST_P(LayerRenderTypeTransactionTest,
1786 SetBackgroundColor_BufferQueue_NoPriorColor_ZeroAlpha_NoEffect) {
1787 bool priorColor = false;
1788 bool bufferFill = false;
1789 float alpha = 0;
1790 Color finalColor = Color::BLACK;
1791 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1792 priorColor, bufferFill, alpha, finalColor));
1793}
1794
1795TEST_P(LayerRenderTypeTransactionTest,
1796 SetBackgroundColor_BufferQueue_PriorColor_ZeroAlpha_DeleteBackground) {
1797 bool priorColor = true;
1798 bool bufferFill = false;
1799 float alpha = 0;
1800 Color finalColor = Color::BLACK;
1801 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1802 priorColor, bufferFill, alpha, finalColor));
1803}
1804
1805TEST_P(LayerRenderTypeTransactionTest,
1806 SetBackgroundColor_BufferState_BufferFill_NoPriorColor_Basic) {
1807 bool priorColor = false;
1808 bool bufferFill = true;
1809 float alpha = 1.0f;
1810 Color finalColor = Color::RED;
Valerie Haua6b15a12019-02-05 14:16:30 -08001811 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001812 priorColor, bufferFill, alpha, finalColor));
1813}
1814
1815TEST_P(LayerRenderTypeTransactionTest,
1816 SetBackgroundColor_BufferState_NoBufferFill_NoPriorColor_Basic) {
1817 bool priorColor = false;
1818 bool bufferFill = false;
1819 float alpha = 1.0f;
1820 Color finalColor = Color::GREEN;
Valerie Haua6b15a12019-02-05 14:16:30 -08001821 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001822 priorColor, bufferFill, alpha, finalColor));
1823}
1824
1825TEST_P(LayerRenderTypeTransactionTest,
1826 SetBackgroundColor_BufferState_NoBufferFill_PriorColor_Basic) {
1827 bool priorColor = true;
1828 bool bufferFill = false;
1829 float alpha = 1.0f;
1830 Color finalColor = Color::GREEN;
Valerie Haua6b15a12019-02-05 14:16:30 -08001831 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001832 priorColor, bufferFill, alpha, finalColor));
1833}
1834
1835TEST_P(LayerRenderTypeTransactionTest,
1836 SetBackgroundColor_BufferState_NoPriorColor_ZeroAlpha_NoEffect) {
1837 bool priorColor = false;
1838 bool bufferFill = false;
1839 float alpha = 0;
1840 Color finalColor = Color::BLACK;
Valerie Haua6b15a12019-02-05 14:16:30 -08001841 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001842 priorColor, bufferFill, alpha, finalColor));
1843}
1844
1845TEST_P(LayerRenderTypeTransactionTest,
1846 SetBackgroundColor_BufferState_PriorColor_ZeroAlpha_DeleteBackground) {
1847 bool priorColor = true;
1848 bool bufferFill = false;
1849 float alpha = 0;
1850 Color finalColor = Color::BLACK;
Valerie Haua6b15a12019-02-05 14:16:30 -08001851 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001852 priorColor, bufferFill, alpha, finalColor));
Valerie Haua72e2812019-01-23 13:40:39 -08001853}
1854
Alec Mouri80863a62019-01-17 15:19:35 -08001855TEST_P(LayerRenderTypeTransactionTest, SetColorClamped) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001856 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001857 ASSERT_NO_FATAL_FAILURE(colorLayer =
1858 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1859 ISurfaceComposerClient::eFXSurfaceColor));
1860 Transaction()
1861 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1862 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1863 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001864
Alec Mouri80863a62019-01-17 15:19:35 -08001865 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001866}
1867
Alec Mouri80863a62019-01-17 15:19:35 -08001868TEST_P(LayerRenderTypeTransactionTest, SetColorWithAlpha) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001869 sp<SurfaceControl> bufferLayer;
1870 sp<SurfaceControl> colorLayer;
1871 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001872 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001873 ASSERT_NO_FATAL_FAILURE(colorLayer =
1874 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1875 ISurfaceComposerClient::eFXSurfaceColor));
1876 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001877
1878 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1879 const float alpha = 0.25f;
1880 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1881 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1882 // channel) should be less than one
1883 const uint8_t tolerance = 1;
1884 Transaction()
1885 .setColor(colorLayer, color)
1886 .setAlpha(colorLayer, alpha)
1887 .setLayer(colorLayer, mLayerZBase + 1)
1888 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001889 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1890 tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001891}
1892
Alec Mouri80863a62019-01-17 15:19:35 -08001893TEST_P(LayerRenderTypeTransactionTest, SetColorWithParentAlpha_Bug74220420) {
Adrian Roosb7a96502018-04-08 11:38:55 -07001894 sp<SurfaceControl> bufferLayer;
1895 sp<SurfaceControl> parentLayer;
1896 sp<SurfaceControl> colorLayer;
1897 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1898 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001899 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001900 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1901 0 /* buffer height */,
1902 ISurfaceComposerClient::eFXSurfaceColor));
1903 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001904 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1905 const float alpha = 0.25f;
1906 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1907 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1908 // channel) should be less than one
1909 const uint8_t tolerance = 1;
1910 Transaction()
1911 .reparent(colorLayer, parentLayer->getHandle())
1912 .setColor(colorLayer, color)
1913 .setAlpha(parentLayer, alpha)
1914 .setLayer(parentLayer, mLayerZBase + 1)
1915 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001916 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1917 tolerance);
Adrian Roosb7a96502018-04-08 11:38:55 -07001918}
1919
Alec Mouri80863a62019-01-17 15:19:35 -08001920TEST_P(LayerTypeAndRenderTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001921 sp<SurfaceControl> bufferLayer;
1922 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001923 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001924
1925 // color is ignored
1926 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001927 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001928}
1929
Alec Mouri80863a62019-01-17 15:19:35 -08001930TEST_P(LayerTypeAndRenderTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001931 sp<SurfaceControl> layer;
1932 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001933 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001934
1935 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1936 {
1937 SCOPED_TRACE("non-existing layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001938 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001939 }
1940
1941 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1942 {
1943 SCOPED_TRACE("original layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001944 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001945 }
1946}
1947
Alec Mouri80863a62019-01-17 15:19:35 -08001948TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001949 sp<SurfaceControl> layer;
1950 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001951 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1952 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001953
1954 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1955 {
1956 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001957 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1958 Color::BLUE, Color::WHITE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001959 }
1960
1961 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1962 {
1963 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001964 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED,
1965 Color::WHITE, Color::BLUE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001966 }
1967
1968 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1969 {
1970 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001971 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE,
1972 Color::RED, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001973 }
1974
1975 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1976 {
1977 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001978 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED,
1979 Color::WHITE, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001980 }
1981
1982 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1983 {
1984 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001985 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN,
1986 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001987 }
1988}
1989
Alec Mouri80863a62019-01-17 15:19:35 -08001990TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001991 sp<SurfaceControl> layer;
1992 ASSERT_NO_FATAL_FAILURE(
1993 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1994 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1995 Color::BLUE, Color::WHITE));
1996
1997 Transaction()
1998 .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
1999 .setFrame(layer, Rect(0, 0, 32, 32))
2000 .apply();
2001 {
2002 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08002003 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
2004 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07002005 }
2006
2007 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
2008 {
2009 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08002010 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
2011 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07002012 }
2013
2014 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
2015 {
2016 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08002017 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
2018 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07002019 }
2020
2021 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
2022 {
2023 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08002024 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
2025 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07002026 }
2027
2028 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
2029 {
2030 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08002031 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
2032 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07002033 }
2034}
2035
Alec Mouri80863a62019-01-17 15:19:35 -08002036TEST_P(LayerRenderTypeTransactionTest, SetMatrixRot45_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07002037 sp<SurfaceControl> layer;
2038 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07002039 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2040 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07002041
2042 const float rot = M_SQRT1_2; // 45 degrees
2043 const float trans = M_SQRT2 * 16.0f;
2044 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
2045
Alec Mouri80863a62019-01-17 15:19:35 -08002046 auto shot = getScreenCapture();
Chia-I Wu93853fe2017-11-02 08:30:27 -07002047 // check a 8x8 region inside each color
2048 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
2049 const int32_t halfL = 4;
2050 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
2051 };
2052 const int32_t unit = int32_t(trans / 2);
2053 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
2054 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
2055 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
2056 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
2057}
2058
Alec Mouri80863a62019-01-17 15:19:35 -08002059TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithResize_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07002060 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -07002061 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2062 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07002063
2064 // setMatrix is applied after any pending resize, unlike setPosition
2065 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
2066 {
2067 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002068 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002069 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -07002070 shot->expectColor(rect, Color::RED);
2071 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07002072 }
2073
Marissa Wall861616d2018-10-22 12:52:23 -07002074 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07002075 {
2076 SCOPED_TRACE("resize applied");
Marissa Wall861616d2018-10-22 12:52:23 -07002077 const Rect rect(0, 0, 128, 128);
Alec Mouri80863a62019-01-17 15:19:35 -08002078 getScreenCapture()->expectColor(rect, Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07002079 }
2080}
2081
Alec Mouri80863a62019-01-17 15:19:35 -08002082TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07002083 sp<SurfaceControl> layer;
2084 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07002085 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07002086
2087 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
2088 Transaction()
2089 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
2090 .setSize(layer, 64, 64)
2091 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
2092 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002093 getScreenCapture()->expectColor(Rect(0, 0, 128, 128), Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07002094}
2095
Alec Mouri80863a62019-01-17 15:19:35 -08002096TEST_P(LayerRenderTypeTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
Chia-I Wua56b2042017-11-01 15:16:35 -07002097 sp<SurfaceControl> layer;
2098 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07002099 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2100 Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07002101
2102 // XXX SCALE_CROP is not respected; calling setSize and
2103 // setOverrideScalingMode in separate transactions does not work
2104 // (b/69315456)
2105 Transaction()
2106 .setSize(layer, 64, 16)
2107 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
2108 .apply();
2109 {
2110 SCOPED_TRACE("SCALE_TO_WINDOW");
Alec Mouri80863a62019-01-17 15:19:35 -08002111 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN,
2112 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wua56b2042017-11-01 15:16:35 -07002113 }
2114}
2115
Dan Stoza000dd012018-08-01 13:31:52 -07002116TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
2117 sp<SurfaceControl> layer;
2118 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2119
2120 sp<IBinder> handle = layer->getHandle();
2121 ASSERT_TRUE(handle != nullptr);
2122
2123 FrameStats frameStats;
2124 mClient->getLayerFrameStats(handle, &frameStats);
2125
2126 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
2127}
2128
Alec Mouri80863a62019-01-17 15:19:35 -08002129TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002130 sp<SurfaceControl> layer;
2131 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002132 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002133 const Rect crop(8, 8, 24, 24);
2134
Marissa Wallf58c14b2018-07-24 10:50:43 -07002135 Transaction().setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002136 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002137 shot->expectColor(crop, Color::RED);
2138 shot->expectBorder(crop, Color::BLACK);
2139}
2140
Alec Mouri80863a62019-01-17 15:19:35 -08002141TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002142 sp<SurfaceControl> layer;
2143 ASSERT_NO_FATAL_FAILURE(
2144 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2145 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2146 const Rect crop(8, 8, 24, 24);
2147
2148 Transaction().setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002149 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002150 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2151 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002152}
2153
Alec Mouri80863a62019-01-17 15:19:35 -08002154TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002155 sp<SurfaceControl> layer;
2156 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002157 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002158
2159 {
2160 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07002161 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002162 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002163 }
2164
2165 {
2166 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07002167 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002168 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002169 }
2170}
2171
Alec Mouri80863a62019-01-17 15:19:35 -08002172TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002173 sp<SurfaceControl> layer;
2174 ASSERT_NO_FATAL_FAILURE(
2175 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2176 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2177
2178 {
2179 SCOPED_TRACE("empty rect");
2180 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
Marissa Wall290ad082019-03-06 13:23:47 -08002181 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002182 }
2183
2184 {
2185 SCOPED_TRACE("negative rect");
2186 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
Marissa Wall290ad082019-03-06 13:23:47 -08002187 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002188 }
2189}
2190
Alec Mouri80863a62019-01-17 15:19:35 -08002191TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002192 sp<SurfaceControl> layer;
2193 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002194 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002195
Marissa Wallf58c14b2018-07-24 10:50:43 -07002196 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002197 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002198 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2199 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2200}
2201
Alec Mouri80863a62019-01-17 15:19:35 -08002202TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferState) {
Valerie Hau0bc09152018-12-20 07:42:47 -08002203 sp<SurfaceControl> layer;
Marissa Wall290ad082019-03-06 13:23:47 -08002204 ASSERT_NO_FATAL_FAILURE(
2205 layer = createLayer("test", 32, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
Valerie Hau0bc09152018-12-20 07:42:47 -08002206 sp<GraphicBuffer> buffer =
Marissa Wall290ad082019-03-06 13:23:47 -08002207 new GraphicBuffer(32, 64, PIXEL_FORMAT_RGBA_8888, 1,
Valerie Hau0bc09152018-12-20 07:42:47 -08002208 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2209 BufferUsage::COMPOSER_OVERLAY,
2210 "test");
Marissa Wall290ad082019-03-06 13:23:47 -08002211 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 16), Color::BLUE);
2212 fillGraphicBufferColor(buffer, Rect(0, 16, 32, 64), Color::RED);
2213
2214 Transaction().setFrame(layer, Rect(0, 0, 64, 64)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08002215
2216 Transaction().setBuffer(layer, buffer).apply();
2217
2218 // Partially out of bounds in the negative (upper left) direction
Marissa Wall290ad082019-03-06 13:23:47 -08002219 Transaction().setCrop(layer, Rect(-128, -128, 32, 16)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08002220 {
2221 SCOPED_TRACE("out of bounds, negative (upper left) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08002222 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002223 shot->expectColor(Rect(0, 0, 64, 64), Color::BLUE);
2224 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08002225 }
2226
2227 // Partially out of bounds in the positive (lower right) direction
Marissa Wall290ad082019-03-06 13:23:47 -08002228 Transaction().setCrop(layer, Rect(0, 16, 128, 128)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08002229 {
2230 SCOPED_TRACE("out of bounds, positive (lower right) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08002231 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002232 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2233 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08002234 }
2235
2236 // Fully out of buffer space bounds
2237 Transaction().setCrop(layer, Rect(-128, -128, -1, -1)).apply();
2238 {
2239 SCOPED_TRACE("Fully out of bounds");
Alec Mouri80863a62019-01-17 15:19:35 -08002240 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002241 shot->expectColor(Rect(0, 0, 64, 16), Color::BLUE);
2242 shot->expectColor(Rect(0, 16, 64, 64), Color::RED);
2243 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08002244 }
2245}
2246
Alec Mouri80863a62019-01-17 15:19:35 -08002247TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002248 sp<SurfaceControl> layer;
2249 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002250 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002251
2252 const Point position(32, 32);
2253 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002254 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002255 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002256 shot->expectColor(crop + position, Color::RED);
2257 shot->expectBorder(crop + position, Color::BLACK);
2258}
2259
Alec Mouri80863a62019-01-17 15:19:35 -08002260TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002261 sp<SurfaceControl> layer;
2262 ASSERT_NO_FATAL_FAILURE(
2263 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2264 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2265
Marissa Wall861616d2018-10-22 12:52:23 -07002266 const Rect frame(32, 32, 64, 64);
Marissa Wall61c58622018-07-18 10:12:20 -07002267 const Rect crop(8, 8, 24, 24);
Marissa Wall861616d2018-10-22 12:52:23 -07002268 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002269 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002270 shot->expectColor(frame, Color::RED);
2271 shot->expectBorder(frame, Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002272}
2273
Alec Mouri80863a62019-01-17 15:19:35 -08002274TEST_P(LayerRenderTypeTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002275 sp<SurfaceControl> layer;
2276 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002277 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002278
Marissa Wall861616d2018-10-22 12:52:23 -07002279 // crop_legacy is affected by matrix
Chia-I Wu04dcca82017-11-02 08:30:27 -07002280 Transaction()
2281 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07002282 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07002283 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002284 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002285 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2286 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2287}
2288
Alec Mouri80863a62019-01-17 15:19:35 -08002289TEST_P(LayerRenderTypeTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002290 sp<SurfaceControl> layer;
2291 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002292 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002293
Marissa Wallf58c14b2018-07-24 10:50:43 -07002294 // setCrop_legacy is applied immediately by default, with or without resize pending
2295 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002296 {
2297 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002298 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002299 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
2300 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
2301 }
2302
Marissa Wall61c58622018-07-18 10:12:20 -07002303 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002304 {
2305 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002306 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002307 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
2308 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
2309 }
2310}
2311
Alec Mouri80863a62019-01-17 15:19:35 -08002312TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002313 sp<SurfaceControl> layer;
2314 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002315 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002316
Marissa Wallf58c14b2018-07-24 10:50:43 -07002317 // request setCrop_legacy to be applied with the next resize
2318 Transaction()
2319 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
2320 .setGeometryAppliesWithResize(layer)
2321 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002322 {
2323 SCOPED_TRACE("waiting for next resize");
Alec Mouri80863a62019-01-17 15:19:35 -08002324 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002325 }
2326
Marissa Wallf58c14b2018-07-24 10:50:43 -07002327 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002328 {
2329 SCOPED_TRACE("pending crop modified");
Alec Mouri80863a62019-01-17 15:19:35 -08002330 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002331 }
2332
2333 Transaction().setSize(layer, 16, 16).apply();
2334 {
2335 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002336 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002337 }
2338
2339 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07002340 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002341 {
2342 SCOPED_TRACE("new crop applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002343 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002344 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2345 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2346 }
2347}
2348
Alec Mouri80863a62019-01-17 15:19:35 -08002349TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002350 sp<SurfaceControl> layer;
2351 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002352 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002353
Marissa Wallf58c14b2018-07-24 10:50:43 -07002354 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07002355 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07002356 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07002357 .setSize(layer, 16, 16)
2358 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
2359 .setGeometryAppliesWithResize(layer)
2360 .apply();
2361 {
2362 SCOPED_TRACE("new crop pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002363 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002364 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
2365 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
2366 }
2367
2368 // XXX crop is never latched without other geometry change (b/69315677)
2369 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002370 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002371 Transaction().setPosition(layer, 0, 0).apply();
2372 {
2373 SCOPED_TRACE("new crop applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002374 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002375 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2376 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2377 }
2378}
2379
Alec Mouri80863a62019-01-17 15:19:35 -08002380TEST_P(LayerRenderTypeTransactionTest, SetFrameBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002381 sp<SurfaceControl> layer;
2382 ASSERT_NO_FATAL_FAILURE(
2383 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2384 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2385 const Rect frame(8, 8, 24, 24);
2386
2387 Transaction().setFrame(layer, frame).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002388 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002389 shot->expectColor(frame, Color::RED);
2390 shot->expectBorder(frame, Color::BLACK);
2391}
2392
Alec Mouri80863a62019-01-17 15:19:35 -08002393TEST_P(LayerRenderTypeTransactionTest, SetFrameEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002394 sp<SurfaceControl> layer;
2395 ASSERT_NO_FATAL_FAILURE(
2396 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2397 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2398
Marissa Wall61c58622018-07-18 10:12:20 -07002399 {
Marissa Wall861616d2018-10-22 12:52:23 -07002400 SCOPED_TRACE("empty rect");
2401 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002402 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002403 }
2404
Marissa Wall61c58622018-07-18 10:12:20 -07002405 {
Marissa Wall861616d2018-10-22 12:52:23 -07002406 SCOPED_TRACE("negative rect");
2407 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002408 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002409 }
2410}
2411
Alec Mouri80863a62019-01-17 15:19:35 -08002412TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultParentless_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002413 sp<SurfaceControl> layer;
2414 ASSERT_NO_FATAL_FAILURE(
2415 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2416 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
2417
2418 // A parentless layer will default to a frame with the same size as the buffer
Alec Mouri80863a62019-01-17 15:19:35 -08002419 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002420 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2421 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall861616d2018-10-22 12:52:23 -07002422}
2423
Alec Mouri80863a62019-01-17 15:19:35 -08002424TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBSParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002425 sp<SurfaceControl> parent, child;
2426 ASSERT_NO_FATAL_FAILURE(
2427 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2428 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2429 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2430
2431 ASSERT_NO_FATAL_FAILURE(
2432 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2433 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2434
2435 Transaction().reparent(child, parent->getHandle()).apply();
2436
2437 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002438 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002439 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2440 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2441}
2442
Alec Mouri80863a62019-01-17 15:19:35 -08002443TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBQParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002444 sp<SurfaceControl> parent, child;
2445 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
2446 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
2447
2448 ASSERT_NO_FATAL_FAILURE(
2449 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2450 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2451
2452 Transaction().reparent(child, parent->getHandle()).apply();
2453
2454 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002455 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002456 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2457 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2458}
2459
Alec Mouri80863a62019-01-17 15:19:35 -08002460TEST_P(LayerRenderTypeTransactionTest, SetFrameUpdate_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002461 sp<SurfaceControl> layer;
2462 ASSERT_NO_FATAL_FAILURE(
2463 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2464 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2465 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
2466
2467 std::this_thread::sleep_for(500ms);
2468
2469 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
2470
Alec Mouri80863a62019-01-17 15:19:35 -08002471 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002472 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2473 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2474}
2475
Alec Mouri80863a62019-01-17 15:19:35 -08002476TEST_P(LayerRenderTypeTransactionTest, SetFrameOutsideBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002477 sp<SurfaceControl> parent, child;
2478 ASSERT_NO_FATAL_FAILURE(
2479 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2480 ASSERT_NO_FATAL_FAILURE(
2481 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2482 Transaction().reparent(child, parent->getHandle()).apply();
2483
2484 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2485 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2486
2487 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2488 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
2489
Alec Mouri80863a62019-01-17 15:19:35 -08002490 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002491 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
2492 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
2493 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2494}
2495
Alec Mouri80863a62019-01-17 15:19:35 -08002496TEST_P(LayerRenderTypeTransactionTest, SetBufferBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002497 sp<SurfaceControl> layer;
2498 ASSERT_NO_FATAL_FAILURE(
2499 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2500
2501 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2502
Alec Mouri80863a62019-01-17 15:19:35 -08002503 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002504 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2505 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002506}
2507
Alec Mouri80863a62019-01-17 15:19:35 -08002508TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleBuffers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002509 sp<SurfaceControl> layer;
2510 ASSERT_NO_FATAL_FAILURE(
2511 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2512
2513 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2514
2515 {
2516 SCOPED_TRACE("set buffer 1");
Alec Mouri80863a62019-01-17 15:19:35 -08002517 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002518 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2519 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002520 }
2521
2522 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
2523
2524 {
2525 SCOPED_TRACE("set buffer 2");
Alec Mouri80863a62019-01-17 15:19:35 -08002526 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002527 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLUE);
2528 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002529 }
2530
2531 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2532
2533 {
2534 SCOPED_TRACE("set buffer 3");
Alec Mouri80863a62019-01-17 15:19:35 -08002535 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002536 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2537 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002538 }
2539}
2540
Alec Mouri80863a62019-01-17 15:19:35 -08002541TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleLayers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002542 sp<SurfaceControl> layer1;
2543 ASSERT_NO_FATAL_FAILURE(
2544 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2545
2546 sp<SurfaceControl> layer2;
2547 ASSERT_NO_FATAL_FAILURE(
2548 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2549
2550 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2551
Marissa Wall861616d2018-10-22 12:52:23 -07002552 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002553 {
2554 SCOPED_TRACE("set layer 1 buffer red");
Alec Mouri80863a62019-01-17 15:19:35 -08002555 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002556 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2557 }
2558
2559 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2560
Marissa Wall861616d2018-10-22 12:52:23 -07002561 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002562 {
2563 SCOPED_TRACE("set layer 2 buffer blue");
Alec Mouri80863a62019-01-17 15:19:35 -08002564 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002565 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2566 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2567 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2568 }
2569
2570 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2571 {
2572 SCOPED_TRACE("set layer 1 buffer green");
Alec Mouri80863a62019-01-17 15:19:35 -08002573 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002574 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2575 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2576 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2577 }
2578
2579 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2580
2581 {
2582 SCOPED_TRACE("set layer 2 buffer white");
Alec Mouri80863a62019-01-17 15:19:35 -08002583 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002584 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2585 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2586 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2587 }
2588}
2589
Valerie Haua6b15a12019-02-05 14:16:30 -08002590TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002591 sp<SurfaceControl> layer;
2592 ASSERT_NO_FATAL_FAILURE(
2593 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2594
2595 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2596
2597 std::array<sp<GraphicBuffer>, 10> buffers;
2598
2599 size_t idx = 0;
2600 for (auto& buffer : buffers) {
2601 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2602 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2603 BufferUsage::COMPOSER_OVERLAY,
2604 "test");
2605 Color color = colors[idx % colors.size()];
2606 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2607 idx++;
2608 }
2609
2610 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2611 // cache is working.
2612 idx = 0;
2613 for (auto& buffer : buffers) {
2614 for (int i = 0; i < 2; i++) {
2615 Transaction().setBuffer(layer, buffer).apply();
2616
2617 Color color = colors[idx % colors.size()];
2618 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002619 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2620 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002621 }
2622 idx++;
2623 }
2624}
2625
Valerie Haua6b15a12019-02-05 14:16:30 -08002626TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_LeastRecentlyUsed_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002627 sp<SurfaceControl> layer;
2628 ASSERT_NO_FATAL_FAILURE(
2629 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2630
2631 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2632
2633 std::array<sp<GraphicBuffer>, 70> buffers;
2634
2635 size_t idx = 0;
2636 for (auto& buffer : buffers) {
2637 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2638 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2639 BufferUsage::COMPOSER_OVERLAY,
2640 "test");
2641 Color color = colors[idx % colors.size()];
2642 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2643 idx++;
2644 }
2645
2646 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2647 // cache is working.
2648 idx = 0;
2649 for (auto& buffer : buffers) {
2650 for (int i = 0; i < 2; i++) {
2651 Transaction().setBuffer(layer, buffer).apply();
2652
2653 Color color = colors[idx % colors.size()];
2654 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002655 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2656 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002657 }
2658 idx++;
2659 }
2660}
2661
Valerie Haua6b15a12019-02-05 14:16:30 -08002662TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_DestroyedBuffer_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002663 sp<SurfaceControl> layer;
2664 ASSERT_NO_FATAL_FAILURE(
2665 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2666
2667 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2668
2669 std::array<sp<GraphicBuffer>, 65> buffers;
2670
2671 size_t idx = 0;
2672 for (auto& buffer : buffers) {
2673 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2674 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2675 BufferUsage::COMPOSER_OVERLAY,
2676 "test");
2677 Color color = colors[idx % colors.size()];
2678 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2679 idx++;
2680 }
2681
2682 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2683 // cache is working.
2684 idx = 0;
2685 for (auto& buffer : buffers) {
2686 for (int i = 0; i < 2; i++) {
2687 Transaction().setBuffer(layer, buffer).apply();
2688
2689 Color color = colors[idx % colors.size()];
2690 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002691 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2692 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002693 }
2694 if (idx == 0) {
2695 buffers[0].clear();
2696 }
2697 idx++;
2698 }
2699}
2700
Alec Mouri80863a62019-01-17 15:19:35 -08002701TEST_P(LayerRenderTypeTransactionTest, SetTransformRotate90_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002702 sp<SurfaceControl> layer;
2703 ASSERT_NO_FATAL_FAILURE(
2704 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2705
2706 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2707 Color::BLUE, Color::WHITE));
2708
Marissa Wall861616d2018-10-22 12:52:23 -07002709 Transaction()
2710 .setFrame(layer, Rect(0, 0, 32, 32))
2711 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2712 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002713
Alec Mouri80863a62019-01-17 15:19:35 -08002714 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2715 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002716}
2717
Alec Mouri80863a62019-01-17 15:19:35 -08002718TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipH_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002719 sp<SurfaceControl> layer;
2720 ASSERT_NO_FATAL_FAILURE(
2721 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2722
2723 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2724 Color::BLUE, Color::WHITE));
2725
Marissa Wall861616d2018-10-22 12:52:23 -07002726 Transaction()
2727 .setFrame(layer, Rect(0, 0, 32, 32))
2728 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2729 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002730
Alec Mouri80863a62019-01-17 15:19:35 -08002731 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2732 Color::BLUE, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002733}
2734
Alec Mouri80863a62019-01-17 15:19:35 -08002735TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipV_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002736 sp<SurfaceControl> layer;
2737 ASSERT_NO_FATAL_FAILURE(
2738 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2739
2740 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2741 Color::BLUE, Color::WHITE));
2742
Marissa Wall861616d2018-10-22 12:52:23 -07002743 Transaction()
2744 .setFrame(layer, Rect(0, 0, 32, 32))
2745 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2746 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002747
Alec Mouri80863a62019-01-17 15:19:35 -08002748 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2749 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002750}
2751
2752TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2753 sp<SurfaceControl> layer;
2754 ASSERT_NO_FATAL_FAILURE(
2755 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2756
2757 Transaction().setTransformToDisplayInverse(layer, false).apply();
2758
2759 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2760
2761 Transaction().setTransformToDisplayInverse(layer, true).apply();
2762}
2763
Alec Mouri80863a62019-01-17 15:19:35 -08002764TEST_P(LayerRenderTypeTransactionTest, SetFenceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002765 sp<SurfaceControl> layer;
Marissa Wall713b63f2018-10-17 15:42:43 -07002766 Transaction transaction;
2767 ASSERT_NO_FATAL_FAILURE(
2768 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2769
2770 sp<GraphicBuffer> buffer =
2771 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2772 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2773 BufferUsage::COMPOSER_OVERLAY,
2774 "test");
2775 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2776
2777 sp<Fence> fence;
2778 if (getBuffer(nullptr, &fence) != NO_ERROR) {
2779 GTEST_SUCCEED() << "test not supported";
2780 return;
2781 }
2782
2783 Transaction().setBuffer(layer, buffer).setAcquireFence(layer, fence).apply();
2784
2785 status_t status = fence->wait(1000);
2786 ASSERT_NE(static_cast<status_t>(Fence::Status::Unsignaled), status);
2787 std::this_thread::sleep_for(200ms);
2788
Alec Mouri80863a62019-01-17 15:19:35 -08002789 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002790 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2791 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall713b63f2018-10-17 15:42:43 -07002792}
2793
Alec Mouri80863a62019-01-17 15:19:35 -08002794TEST_P(LayerRenderTypeTransactionTest, SetFenceNull_BufferState) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002795 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07002796 ASSERT_NO_FATAL_FAILURE(
2797 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2798
2799 sp<GraphicBuffer> buffer =
2800 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2801 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2802 BufferUsage::COMPOSER_OVERLAY,
2803 "test");
2804 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2805
Marissa Wallfda30bb2018-10-12 11:34:28 -07002806 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002807
2808 Transaction()
2809 .setBuffer(layer, buffer)
2810 .setAcquireFence(layer, fence)
Marissa Wall61c58622018-07-18 10:12:20 -07002811 .apply();
2812
Alec Mouri80863a62019-01-17 15:19:35 -08002813 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002814 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2815 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002816}
2817
Alec Mouri80863a62019-01-17 15:19:35 -08002818TEST_P(LayerRenderTypeTransactionTest, SetDataspaceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002819 sp<SurfaceControl> layer;
2820 ASSERT_NO_FATAL_FAILURE(
2821 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2822
2823 sp<GraphicBuffer> buffer =
2824 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2825 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2826 BufferUsage::COMPOSER_OVERLAY,
2827 "test");
2828 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2829
2830 Transaction()
2831 .setBuffer(layer, buffer)
2832 .setDataspace(layer, ui::Dataspace::UNKNOWN)
Marissa Wall61c58622018-07-18 10:12:20 -07002833 .apply();
2834
Alec Mouri80863a62019-01-17 15:19:35 -08002835 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002836 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2837 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002838}
2839
Alec Mouri80863a62019-01-17 15:19:35 -08002840TEST_P(LayerRenderTypeTransactionTest, SetHdrMetadataBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002841 sp<SurfaceControl> layer;
2842 ASSERT_NO_FATAL_FAILURE(
2843 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2844
2845 sp<GraphicBuffer> buffer =
2846 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2847 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2848 BufferUsage::COMPOSER_OVERLAY,
2849 "test");
2850 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2851
2852 HdrMetadata hdrMetadata;
2853 hdrMetadata.validTypes = 0;
2854 Transaction()
2855 .setBuffer(layer, buffer)
2856 .setHdrMetadata(layer, hdrMetadata)
Marissa Wall61c58622018-07-18 10:12:20 -07002857 .apply();
2858
Alec Mouri80863a62019-01-17 15:19:35 -08002859 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002860 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2861 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002862}
2863
Alec Mouri80863a62019-01-17 15:19:35 -08002864TEST_P(LayerRenderTypeTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002865 sp<SurfaceControl> layer;
2866 ASSERT_NO_FATAL_FAILURE(
2867 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2868
2869 sp<GraphicBuffer> buffer =
2870 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2871 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2872 BufferUsage::COMPOSER_OVERLAY,
2873 "test");
2874 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2875
2876 Region region;
2877 region.set(32, 32);
2878 Transaction()
2879 .setBuffer(layer, buffer)
2880 .setSurfaceDamageRegion(layer, region)
Marissa Wall61c58622018-07-18 10:12:20 -07002881 .apply();
2882
Alec Mouri80863a62019-01-17 15:19:35 -08002883 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002884 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2885 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002886}
2887
Alec Mouri80863a62019-01-17 15:19:35 -08002888TEST_P(LayerRenderTypeTransactionTest, SetApiBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002889 sp<SurfaceControl> layer;
2890 ASSERT_NO_FATAL_FAILURE(
2891 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2892
2893 sp<GraphicBuffer> buffer =
2894 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2895 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2896 BufferUsage::COMPOSER_OVERLAY,
2897 "test");
2898 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2899
2900 Transaction()
2901 .setBuffer(layer, buffer)
2902 .setApi(layer, NATIVE_WINDOW_API_CPU)
Marissa Wall61c58622018-07-18 10:12:20 -07002903 .apply();
2904
Alec Mouri80863a62019-01-17 15:19:35 -08002905 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002906 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2907 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002908}
2909
2910TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2911 sp<SurfaceControl> layer;
2912 ASSERT_NO_FATAL_FAILURE(
2913 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2914
2915 // verify this doesn't cause a crash
2916 Transaction().setSidebandStream(layer, nullptr).apply();
2917}
2918
Robert Carr54cf5b12019-01-25 14:02:28 -08002919TEST_F(LayerTransactionTest, ReparentToSelf) {
2920 sp<SurfaceControl> layer;
2921 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2922 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2923 Transaction().reparent(layer, layer->getHandle()).apply();
2924
2925 {
2926 // We expect the transaction to be silently dropped, but for SurfaceFlinger
2927 // to still be functioning.
2928 SCOPED_TRACE("after reparent to self");
2929 const Rect rect(0, 0, 32, 32);
2930 auto shot = screenshot();
2931 shot->expectColor(rect, Color::RED);
2932 shot->expectBorder(rect, Color::BLACK);
2933 }
2934}
2935
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002936class ColorTransformHelper {
2937public:
2938 static void DegammaColorSingle(half& s) {
2939 if (s <= 0.03928f)
2940 s = s / 12.92f;
2941 else
2942 s = pow((s + 0.055f) / 1.055f, 2.4f);
2943 }
2944
2945 static void DegammaColor(half3& color) {
2946 DegammaColorSingle(color.r);
2947 DegammaColorSingle(color.g);
2948 DegammaColorSingle(color.b);
2949 }
2950
2951 static void GammaColorSingle(half& s) {
2952 if (s <= 0.0031308f) {
2953 s = s * 12.92f;
2954 } else {
2955 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2956 }
2957 }
2958
2959 static void GammaColor(half3& color) {
2960 GammaColorSingle(color.r);
2961 GammaColorSingle(color.g);
2962 GammaColorSingle(color.b);
2963 }
2964
2965 static void applyMatrix(half3& color, const mat3& mat) {
2966 half3 ret = half3(0);
2967
2968 for (int i = 0; i < 3; i++) {
2969 for (int j = 0; j < 3; j++) {
2970 ret[i] = ret[i] + color[j] * mat[j][i];
2971 }
2972 }
2973 color = ret;
2974 }
2975};
2976
Alec Mouri80863a62019-01-17 15:19:35 -08002977TEST_P(LayerRenderTypeTransactionTest, SetColorTransformBasic) {
Peiyong Lind3788632018-09-18 16:01:31 -07002978 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002979 ASSERT_NO_FATAL_FAILURE(colorLayer =
2980 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2981 ISurfaceComposerClient::eFXSurfaceColor));
2982 Transaction()
2983 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2984 .setLayer(colorLayer, mLayerZBase + 1)
2985 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002986 {
2987 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002988 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Peiyong Lind3788632018-09-18 16:01:31 -07002989 }
2990
2991 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002992 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002993 mat3 matrix;
2994 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2995 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2996 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002997
2998 // degamma before applying the matrix
2999 if (mColorManagementUsed) {
3000 ColorTransformHelper::DegammaColor(expected);
3001 }
3002
3003 ColorTransformHelper::applyMatrix(expected, matrix);
3004
3005 if (mColorManagementUsed) {
3006 ColorTransformHelper::GammaColor(expected);
3007 }
3008
3009 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
3010 uint8_t(expected.b * 255), 255};
3011
3012 // this is handwavy, but the precison loss scaled by 255 (8-bit per
3013 // channel) should be less than one
3014 const uint8_t tolerance = 1;
3015
Peiyong Lind3788632018-09-18 16:01:31 -07003016 Transaction().setColor(colorLayer, color)
3017 .setColorTransform(colorLayer, matrix, vec3()).apply();
3018 {
3019 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08003020 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07003021 }
3022}
3023
Alec Mouri80863a62019-01-17 15:19:35 -08003024TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnParent) {
chaviwf66724d2018-11-28 16:35:21 -08003025 sp<SurfaceControl> parentLayer;
3026 sp<SurfaceControl> colorLayer;
3027 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
3028 0 /* buffer height */,
3029 ISurfaceComposerClient::eFXSurfaceContainer));
3030 ASSERT_NO_FATAL_FAILURE(
3031 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
3032 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
3033
3034 Transaction()
3035 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
3036 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
3037 .setLayer(parentLayer, mLayerZBase + 1)
3038 .apply();
3039 {
3040 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08003041 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08003042 }
3043
3044 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
3045 half3 expected = color;
3046 mat3 matrix;
3047 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
3048 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
3049 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
3050
3051 // degamma before applying the matrix
3052 if (mColorManagementUsed) {
3053 ColorTransformHelper::DegammaColor(expected);
3054 }
3055
3056 ColorTransformHelper::applyMatrix(expected, matrix);
3057
3058 if (mColorManagementUsed) {
3059 ColorTransformHelper::GammaColor(expected);
3060 }
3061
3062 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
3063 uint8_t(expected.b * 255), 255};
3064
3065 // this is handwavy, but the precison loss scaled by 255 (8-bit per
3066 // channel) should be less than one
3067 const uint8_t tolerance = 1;
3068
3069 Transaction()
3070 .setColor(colorLayer, color)
3071 .setColorTransform(parentLayer, matrix, vec3())
3072 .apply();
3073 {
3074 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08003075 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08003076 }
3077}
3078
Alec Mouri80863a62019-01-17 15:19:35 -08003079TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnChildAndParent) {
chaviwf66724d2018-11-28 16:35:21 -08003080 sp<SurfaceControl> parentLayer;
3081 sp<SurfaceControl> colorLayer;
3082 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
3083 0 /* buffer height */,
3084 ISurfaceComposerClient::eFXSurfaceContainer));
3085 ASSERT_NO_FATAL_FAILURE(
3086 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
3087 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
3088
3089 Transaction()
3090 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
3091 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
3092 .setLayer(parentLayer, mLayerZBase + 1)
3093 .apply();
3094 {
3095 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08003096 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08003097 }
3098
3099 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
3100 half3 expected = color;
3101 mat3 matrixChild;
3102 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
3103 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
3104 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
3105 mat3 matrixParent;
3106 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
3107 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
3108 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
3109
3110 // degamma before applying the matrix
3111 if (mColorManagementUsed) {
3112 ColorTransformHelper::DegammaColor(expected);
3113 }
3114
3115 ColorTransformHelper::applyMatrix(expected, matrixChild);
3116 ColorTransformHelper::applyMatrix(expected, matrixParent);
3117
3118 if (mColorManagementUsed) {
3119 ColorTransformHelper::GammaColor(expected);
3120 }
3121
3122 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
3123 uint8_t(expected.b * 255), 255};
3124
3125 // this is handwavy, but the precison loss scaled by 255 (8-bit per
3126 // channel) should be less than one
3127 const uint8_t tolerance = 1;
3128
3129 Transaction()
3130 .setColor(colorLayer, color)
3131 .setColorTransform(parentLayer, matrixParent, vec3())
3132 .setColorTransform(colorLayer, matrixChild, vec3())
3133 .apply();
3134 {
3135 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08003136 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08003137 }
3138}
3139
Marissa Wall80d94ad2019-01-18 16:04:36 -08003140struct CallbackData {
3141 CallbackData() = default;
3142 CallbackData(nsecs_t time, const sp<Fence>& fence,
3143 const std::vector<SurfaceControlStats>& stats)
3144 : latchTime(time), presentFence(fence), surfaceControlStats(stats) {}
3145
3146 nsecs_t latchTime;
3147 sp<Fence> presentFence;
3148 std::vector<SurfaceControlStats> surfaceControlStats;
3149};
3150
Marissa Wallfda30bb2018-10-12 11:34:28 -07003151class ExpectedResult {
3152public:
3153 enum Transaction {
3154 NOT_PRESENTED = 0,
3155 PRESENTED,
3156 };
3157
3158 enum Buffer {
3159 NOT_ACQUIRED = 0,
3160 ACQUIRED,
3161 };
3162
3163 enum PreviousBuffer {
3164 NOT_RELEASED = 0,
3165 RELEASED,
Marissa Wall5a68a772018-12-22 17:43:42 -08003166 UNKNOWN,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003167 };
3168
3169 void reset() {
3170 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
3171 mExpectedSurfaceResults.clear();
3172 }
3173
3174 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07003175 ExpectedResult::Buffer bufferResult = ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003176 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
3177 mTransactionResult = transactionResult;
Marissa Wall80d94ad2019-01-18 16:04:36 -08003178 mExpectedSurfaceResults.emplace(std::piecewise_construct, std::forward_as_tuple(layer),
Marissa Wallfda30bb2018-10-12 11:34:28 -07003179 std::forward_as_tuple(bufferResult, previousBufferResult));
3180 }
3181
3182 void addSurfaces(ExpectedResult::Transaction transactionResult,
3183 const std::vector<sp<SurfaceControl>>& layers,
Marissa Wall713b63f2018-10-17 15:42:43 -07003184 ExpectedResult::Buffer bufferResult = ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003185 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
3186 for (const auto& layer : layers) {
3187 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
3188 }
3189 }
3190
Marissa Wall17b4e452018-12-26 16:32:34 -08003191 void addExpectedPresentTime(nsecs_t expectedPresentTime) {
3192 mExpectedPresentTime = expectedPresentTime;
3193 }
3194
Marissa Wall80d94ad2019-01-18 16:04:36 -08003195 void verifyCallbackData(const CallbackData& callbackData) const {
3196 const auto& [latchTime, presentFence, surfaceControlStats] = callbackData;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003197 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
3198 ASSERT_GE(latchTime, 0) << "bad latch time";
Valerie Hau63258a12018-12-14 14:31:48 -08003199 ASSERT_NE(presentFence, nullptr);
Marissa Wall17b4e452018-12-26 16:32:34 -08003200 if (mExpectedPresentTime >= 0) {
3201 ASSERT_EQ(presentFence->wait(3000), NO_ERROR);
3202 ASSERT_GE(presentFence->getSignalTime(), mExpectedPresentTime - nsecs_t(5 * 1e6));
3203 // if the panel is running at 30 hz, at the worst case, our expected time just
3204 // misses vsync and we have to wait another 33.3ms
3205 ASSERT_LE(presentFence->getSignalTime(),
3206 mExpectedPresentTime + nsecs_t(66.666666 * 1e6));
3207 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003208 } else {
Valerie Hau63258a12018-12-14 14:31:48 -08003209 ASSERT_EQ(presentFence, nullptr) << "transaction shouldn't have been presented";
Marissa Wallfda30bb2018-10-12 11:34:28 -07003210 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
3211 }
3212
Marissa Wall80d94ad2019-01-18 16:04:36 -08003213 ASSERT_EQ(surfaceControlStats.size(), mExpectedSurfaceResults.size())
Marissa Wallfda30bb2018-10-12 11:34:28 -07003214 << "wrong number of surfaces";
3215
Marissa Wall80d94ad2019-01-18 16:04:36 -08003216 for (const auto& stats : surfaceControlStats) {
3217 ASSERT_NE(stats.surfaceControl, nullptr) << "returned null surface control";
3218
Marissa Wallfda30bb2018-10-12 11:34:28 -07003219 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
3220 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
3221 << "unexpected surface control";
Marissa Wall80d94ad2019-01-18 16:04:36 -08003222 expectedSurfaceResult->second.verifySurfaceControlStats(stats, latchTime);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003223 }
3224 }
3225
3226private:
3227 class ExpectedSurfaceResult {
3228 public:
3229 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
3230 ExpectedResult::PreviousBuffer previousBufferResult)
3231 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
3232
Marissa Wall80d94ad2019-01-18 16:04:36 -08003233 void verifySurfaceControlStats(const SurfaceControlStats& surfaceControlStats,
3234 nsecs_t latchTime) const {
3235 const auto& [surfaceControl, acquireTime, previousReleaseFence] = surfaceControlStats;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003236
3237 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
3238 << "bad acquire time";
3239 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
Marissa Wall5a68a772018-12-22 17:43:42 -08003240
3241 if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED) {
3242 ASSERT_NE(previousReleaseFence, nullptr)
3243 << "failed to set release prev buffer fence";
3244 } else if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::NOT_RELEASED) {
3245 ASSERT_EQ(previousReleaseFence, nullptr)
3246 << "should not have set released prev buffer fence";
3247 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003248 }
3249
3250 private:
3251 ExpectedResult::Buffer mBufferResult;
3252 ExpectedResult::PreviousBuffer mPreviousBufferResult;
3253 };
3254
Marissa Wall80d94ad2019-01-18 16:04:36 -08003255 struct SCHash {
3256 std::size_t operator()(const sp<SurfaceControl>& sc) const {
3257 return std::hash<IBinder*>{}(sc->getHandle().get());
Marissa Wallfda30bb2018-10-12 11:34:28 -07003258 }
3259 };
3260 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
Marissa Wall17b4e452018-12-26 16:32:34 -08003261 nsecs_t mExpectedPresentTime = -1;
Marissa Wall80d94ad2019-01-18 16:04:36 -08003262 std::unordered_map<sp<SurfaceControl>, ExpectedSurfaceResult, SCHash> mExpectedSurfaceResults;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003263};
3264
3265class CallbackHelper {
3266public:
Marissa Wall80d94ad2019-01-18 16:04:36 -08003267 static void function(void* callbackContext, nsecs_t latchTime, const sp<Fence>& presentFence,
3268 const std::vector<SurfaceControlStats>& stats) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003269 if (!callbackContext) {
3270 ALOGE("failed to get callback context");
3271 }
3272 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
3273 std::lock_guard lock(helper->mMutex);
Marissa Wall80d94ad2019-01-18 16:04:36 -08003274 helper->mCallbackDataQueue.emplace(latchTime, presentFence, stats);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003275 helper->mConditionVariable.notify_all();
3276 }
3277
Marissa Wall80d94ad2019-01-18 16:04:36 -08003278 void getCallbackData(CallbackData* outData) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003279 std::unique_lock lock(mMutex);
3280
Marissa Wall80d94ad2019-01-18 16:04:36 -08003281 if (mCallbackDataQueue.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003282 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
3283 std::cv_status::timeout)
3284 << "did not receive callback";
3285 }
3286
Marissa Wall80d94ad2019-01-18 16:04:36 -08003287 *outData = std::move(mCallbackDataQueue.front());
3288 mCallbackDataQueue.pop();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003289 }
3290
3291 void verifyFinalState() {
3292 // Wait to see if there are extra callbacks
3293 std::this_thread::sleep_for(500ms);
3294
3295 std::lock_guard lock(mMutex);
Marissa Wall80d94ad2019-01-18 16:04:36 -08003296 EXPECT_EQ(mCallbackDataQueue.size(), 0) << "extra callbacks received";
3297 mCallbackDataQueue = {};
Marissa Wallfda30bb2018-10-12 11:34:28 -07003298 }
3299
3300 void* getContext() { return static_cast<void*>(this); }
3301
3302 std::mutex mMutex;
3303 std::condition_variable mConditionVariable;
Marissa Wall80d94ad2019-01-18 16:04:36 -08003304 std::queue<CallbackData> mCallbackDataQueue;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003305};
3306
3307class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07003308public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07003309 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07003310 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003311 }
3312
Marissa Wall713b63f2018-10-17 15:42:43 -07003313 static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
Valerie Hauaa194562019-02-05 16:21:38 -08003314 const sp<SurfaceControl>& layer = nullptr, bool setBuffer = true,
3315 bool setBackgroundColor = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003316 if (layer) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003317 sp<GraphicBuffer> buffer;
3318 sp<Fence> fence;
Valerie Hauaa194562019-02-05 16:21:38 -08003319 if (setBuffer) {
3320 int err = getBuffer(&buffer, &fence);
3321 if (err != NO_ERROR) {
3322 return err;
3323 }
3324
3325 transaction.setBuffer(layer, buffer);
3326 transaction.setAcquireFence(layer, fence);
Marissa Wall713b63f2018-10-17 15:42:43 -07003327 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003328
Valerie Hauaa194562019-02-05 16:21:38 -08003329 if (setBackgroundColor) {
3330 transaction.setBackgroundColor(layer, /*color*/ half3(1.0f, 0, 0), /*alpha*/ 1.0f,
3331 ui::Dataspace::UNKNOWN);
3332 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003333 }
3334
3335 transaction.addTransactionCompletedCallback(callbackHelper->function,
3336 callbackHelper->getContext());
Marissa Wall713b63f2018-10-17 15:42:43 -07003337 return NO_ERROR;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003338 }
3339
Marissa Wall861616d2018-10-22 12:52:23 -07003340 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
3341 bool finalState = false) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08003342 CallbackData callbackData;
3343 ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
3344 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
Marissa Wallfda30bb2018-10-12 11:34:28 -07003345
3346 if (finalState) {
3347 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
3348 }
3349 }
3350
Marissa Wall861616d2018-10-22 12:52:23 -07003351 static void waitForCallbacks(CallbackHelper& helper,
3352 const std::vector<ExpectedResult>& expectedResults,
3353 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003354 for (const auto& expectedResult : expectedResults) {
3355 waitForCallback(helper, expectedResult);
3356 }
3357 if (finalState) {
3358 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
3359 }
3360 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003361};
3362
Valerie Hauaa194562019-02-05 16:21:38 -08003363TEST_F(LayerCallbackTest, BufferColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003364 sp<SurfaceControl> layer;
3365 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3366
3367 Transaction transaction;
3368 CallbackHelper callback;
Valerie Hauaa194562019-02-05 16:21:38 -08003369 int err = fillTransaction(transaction, &callback, layer, true, true);
Marissa Wall713b63f2018-10-17 15:42:43 -07003370 if (err) {
3371 GTEST_SUCCEED() << "test not supported";
3372 return;
3373 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003374
3375 transaction.apply();
3376
3377 ExpectedResult expected;
3378 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3379 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3380}
3381
Valerie Hauaa194562019-02-05 16:21:38 -08003382TEST_F(LayerCallbackTest, NoBufferNoColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003383 sp<SurfaceControl> layer;
3384 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3385
3386 Transaction transaction;
3387 CallbackHelper callback;
Valerie Hauaa194562019-02-05 16:21:38 -08003388 int err = fillTransaction(transaction, &callback, layer, false, false);
Marissa Wall713b63f2018-10-17 15:42:43 -07003389 if (err) {
3390 GTEST_SUCCEED() << "test not supported";
3391 return;
3392 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003393
Marissa Wall861616d2018-10-22 12:52:23 -07003394 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003395
3396 ExpectedResult expected;
Marissa Wall713b63f2018-10-17 15:42:43 -07003397 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
3398 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003399 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3400}
3401
Valerie Hauaa194562019-02-05 16:21:38 -08003402TEST_F(LayerCallbackTest, BufferNoColor) {
3403 sp<SurfaceControl> layer;
3404 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3405
3406 Transaction transaction;
3407 CallbackHelper callback;
3408 int err = fillTransaction(transaction, &callback, layer, true, false);
3409 if (err) {
3410 GTEST_SUCCEED() << "test not supported";
3411 return;
3412 }
3413
3414 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
3415
3416 ExpectedResult expected;
3417 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3418 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3419}
3420
3421TEST_F(LayerCallbackTest, NoBufferColor) {
3422 sp<SurfaceControl> layer;
3423 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3424
3425 Transaction transaction;
3426 CallbackHelper callback;
3427 int err = fillTransaction(transaction, &callback, layer, false, true);
3428 if (err) {
3429 GTEST_SUCCEED() << "test not supported";
3430 return;
3431 }
3432
3433 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
3434
3435 ExpectedResult expected;
3436 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3437 ExpectedResult::Buffer::NOT_ACQUIRED);
3438 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3439}
3440
Marissa Wallfda30bb2018-10-12 11:34:28 -07003441TEST_F(LayerCallbackTest, NoStateChange) {
3442 Transaction transaction;
3443 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003444 int err = fillTransaction(transaction, &callback);
3445 if (err) {
3446 GTEST_SUCCEED() << "test not supported";
3447 return;
3448 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003449
3450 transaction.apply();
3451
3452 ExpectedResult expected;
3453 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3454}
3455
3456TEST_F(LayerCallbackTest, OffScreen) {
3457 sp<SurfaceControl> layer;
3458 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3459
3460 Transaction transaction;
3461 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003462 int err = fillTransaction(transaction, &callback, layer);
3463 if (err) {
3464 GTEST_SUCCEED() << "test not supported";
3465 return;
3466 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003467
Marissa Wall861616d2018-10-22 12:52:23 -07003468 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003469
3470 ExpectedResult expected;
3471 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3472 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3473}
3474
Valerie Hauaa194562019-02-05 16:21:38 -08003475TEST_F(LayerCallbackTest, MergeBufferNoColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07003476 sp<SurfaceControl> layer1, layer2;
3477 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3478 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3479
3480 Transaction transaction1, transaction2;
3481 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003482 int err = fillTransaction(transaction1, &callback1, layer1);
3483 if (err) {
3484 GTEST_SUCCEED() << "test not supported";
3485 return;
3486 }
3487 err = fillTransaction(transaction2, &callback2, layer2);
3488 if (err) {
3489 GTEST_SUCCEED() << "test not supported";
3490 return;
3491 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003492
Marissa Wall861616d2018-10-22 12:52:23 -07003493 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3494 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003495
3496 ExpectedResult expected;
3497 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3498 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3499 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3500}
3501
Valerie Hauaa194562019-02-05 16:21:38 -08003502TEST_F(LayerCallbackTest, MergeNoBufferColor) {
3503 sp<SurfaceControl> layer1, layer2;
3504 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3505 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3506
3507 Transaction transaction1, transaction2;
3508 CallbackHelper callback1, callback2;
3509 int err = fillTransaction(transaction1, &callback1, layer1, false, true);
3510 if (err) {
3511 GTEST_SUCCEED() << "test not supported";
3512 return;
3513 }
3514 err = fillTransaction(transaction2, &callback2, layer2, false, true);
3515 if (err) {
3516 GTEST_SUCCEED() << "test not supported";
3517 return;
3518 }
3519
3520 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3521 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3522
3523 ExpectedResult expected;
3524 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
3525 ExpectedResult::Buffer::NOT_ACQUIRED);
3526 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3527 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3528}
3529
3530TEST_F(LayerCallbackTest, MergeOneBufferOneColor) {
3531 sp<SurfaceControl> layer1, layer2;
3532 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3533 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3534
3535 Transaction transaction1, transaction2;
3536 CallbackHelper callback1, callback2;
3537 int err = fillTransaction(transaction1, &callback1, layer1);
3538 if (err) {
3539 GTEST_SUCCEED() << "test not supported";
3540 return;
3541 }
3542 err = fillTransaction(transaction2, &callback2, layer2, false, true);
3543 if (err) {
3544 GTEST_SUCCEED() << "test not supported";
3545 return;
3546 }
3547
3548 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3549 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3550
3551 ExpectedResult expected;
3552 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer1);
3553 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer2,
3554 ExpectedResult::Buffer::NOT_ACQUIRED);
3555 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3556 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3557}
Marissa Wallfda30bb2018-10-12 11:34:28 -07003558TEST_F(LayerCallbackTest, Merge_SameCallback) {
3559 sp<SurfaceControl> layer1, layer2;
3560 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3561 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3562
3563 Transaction transaction1, transaction2;
3564 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003565 int err = fillTransaction(transaction1, &callback, layer1);
3566 if (err) {
3567 GTEST_SUCCEED() << "test not supported";
3568 return;
3569 }
3570 err = fillTransaction(transaction2, &callback, layer2);
3571 if (err) {
3572 GTEST_SUCCEED() << "test not supported";
3573 return;
3574 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003575
3576 transaction2.merge(std::move(transaction1)).apply();
3577
3578 ExpectedResult expected;
3579 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3580 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3581 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3582}
3583
3584TEST_F(LayerCallbackTest, Merge_SameLayer) {
3585 sp<SurfaceControl> layer;
3586 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3587
3588 Transaction transaction1, transaction2;
3589 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003590 int err = fillTransaction(transaction1, &callback1, layer);
3591 if (err) {
3592 GTEST_SUCCEED() << "test not supported";
3593 return;
3594 }
3595 err = fillTransaction(transaction2, &callback2, layer);
3596 if (err) {
3597 GTEST_SUCCEED() << "test not supported";
3598 return;
3599 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003600
3601 transaction2.merge(std::move(transaction1)).apply();
3602
3603 ExpectedResult expected;
3604 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3605 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3606 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3607}
3608
Marissa Wallfda30bb2018-10-12 11:34:28 -07003609TEST_F(LayerCallbackTest, Merge_DifferentClients) {
3610 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3611 client2(new SurfaceComposerClient);
3612
3613 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3614 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3615
3616 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003617 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003618 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003619 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003620 ISurfaceComposerClient::eFXSurfaceBufferState));
3621
3622 Transaction transaction1, transaction2;
3623 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003624 int err = fillTransaction(transaction1, &callback1, layer1);
3625 if (err) {
3626 GTEST_SUCCEED() << "test not supported";
3627 return;
3628 }
3629 err = fillTransaction(transaction2, &callback2, layer2);
3630 if (err) {
3631 GTEST_SUCCEED() << "test not supported";
3632 return;
3633 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003634
Marissa Wall861616d2018-10-22 12:52:23 -07003635 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3636 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003637
3638 ExpectedResult expected;
3639 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3640 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3641 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3642}
3643
3644TEST_F(LayerCallbackTest, MultipleTransactions) {
3645 sp<SurfaceControl> layer;
3646 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3647
3648 Transaction transaction;
3649 CallbackHelper callback;
3650 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003651 int err = fillTransaction(transaction, &callback, layer);
3652 if (err) {
3653 GTEST_SUCCEED() << "test not supported";
3654 return;
3655 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003656
3657 transaction.apply();
3658
3659 ExpectedResult expected;
3660 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07003661 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003662 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3663 : ExpectedResult::PreviousBuffer::RELEASED);
3664 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3665 }
3666 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3667}
3668
3669TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
3670 sp<SurfaceControl> layer;
3671 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3672
3673 Transaction transaction;
3674 CallbackHelper callback;
3675 for (size_t i = 0; i < 10; i++) {
3676 ExpectedResult expected;
3677
3678 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003679 int err = fillTransaction(transaction, &callback, layer);
3680 if (err) {
3681 GTEST_SUCCEED() << "test not supported";
3682 return;
3683 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003684 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3685 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003686 int err = fillTransaction(transaction, &callback);
3687 if (err) {
3688 GTEST_SUCCEED() << "test not supported";
3689 return;
3690 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003691 }
3692
3693 transaction.apply();
3694
3695 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3696 }
3697 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3698}
3699
3700TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
3701 sp<SurfaceControl> layer;
3702 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3703
3704 Transaction transaction;
3705 CallbackHelper callback;
3706 for (size_t i = 0; i < 10; i++) {
3707 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003708 int err = fillTransaction(transaction, &callback, layer);
3709 if (err) {
3710 GTEST_SUCCEED() << "test not supported";
3711 return;
3712 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003713 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003714 int err = fillTransaction(transaction, &callback);
3715 if (err) {
3716 GTEST_SUCCEED() << "test not supported";
3717 return;
3718 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003719 }
3720
Marissa Wall861616d2018-10-22 12:52:23 -07003721 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003722
3723 ExpectedResult expected;
3724 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
3725 : ExpectedResult::Transaction::NOT_PRESENTED,
Marissa Wall713b63f2018-10-17 15:42:43 -07003726 layer,
3727 (i == 0) ? ExpectedResult::Buffer::ACQUIRED
3728 : ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003729 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
3730 }
3731 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3732}
3733
3734TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
3735 sp<SurfaceControl> layer1, layer2;
3736 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3737 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3738
3739 Transaction transaction1, transaction2;
3740 CallbackHelper callback1, callback2;
3741 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003742 int err = fillTransaction(transaction1, &callback1, layer1);
3743 if (err) {
3744 GTEST_SUCCEED() << "test not supported";
3745 return;
3746 }
3747 err = fillTransaction(transaction2, &callback2, layer2);
3748 if (err) {
3749 GTEST_SUCCEED() << "test not supported";
3750 return;
3751 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003752
Marissa Wall861616d2018-10-22 12:52:23 -07003753 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3754 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003755
3756 ExpectedResult expected;
3757 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003758 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003759 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3760 : ExpectedResult::PreviousBuffer::RELEASED);
3761 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3762 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3763 }
3764 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3765 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3766}
3767
3768TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
3769 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3770 client2(new SurfaceComposerClient);
3771 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3772 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3773
3774 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003775 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003776 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003777 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003778 ISurfaceComposerClient::eFXSurfaceBufferState));
3779
3780 Transaction transaction1, transaction2;
3781 CallbackHelper callback1, callback2;
3782 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003783 int err = fillTransaction(transaction1, &callback1, layer1);
3784 if (err) {
3785 GTEST_SUCCEED() << "test not supported";
3786 return;
3787 }
3788 err = fillTransaction(transaction2, &callback2, layer2);
3789 if (err) {
3790 GTEST_SUCCEED() << "test not supported";
3791 return;
3792 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003793
Marissa Wall861616d2018-10-22 12:52:23 -07003794 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3795 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003796
3797 ExpectedResult expected;
3798 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003799 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003800 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3801 : ExpectedResult::PreviousBuffer::RELEASED);
3802 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3803 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3804 }
3805 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3806 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3807}
3808
3809TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
3810 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3811 client2(new SurfaceComposerClient);
3812 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3813 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3814
3815 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003816 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003817 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003818 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003819 ISurfaceComposerClient::eFXSurfaceBufferState));
3820
3821 Transaction transaction1, transaction2;
3822 CallbackHelper callback1, callback2;
3823
3824 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003825 int err = fillTransaction(transaction1, &callback1, layer1);
3826 if (err) {
3827 GTEST_SUCCEED() << "test not supported";
3828 return;
3829 }
3830 err = fillTransaction(transaction2, &callback2, layer2);
3831 if (err) {
3832 GTEST_SUCCEED() << "test not supported";
3833 return;
3834 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003835
Marissa Wall861616d2018-10-22 12:52:23 -07003836 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3837 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003838
3839 ExpectedResult expected;
3840 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3841 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3842 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3843 expected.reset();
3844
3845 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003846 err = fillTransaction(transaction1, &callback1);
3847 if (err) {
3848 GTEST_SUCCEED() << "test not supported";
3849 return;
3850 }
3851 err = fillTransaction(transaction2, &callback2);
3852 if (err) {
3853 GTEST_SUCCEED() << "test not supported";
3854 return;
3855 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003856
3857 transaction2.merge(std::move(transaction1)).apply();
3858
3859 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3860 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3861}
3862
3863TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
3864 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3865 client2(new SurfaceComposerClient);
3866
3867 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3868 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3869
3870 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003871 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003872 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003873 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003874 ISurfaceComposerClient::eFXSurfaceBufferState));
3875
3876 Transaction transaction1, transaction2;
3877 CallbackHelper callback1, callback2;
3878
3879 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003880 int err = fillTransaction(transaction1, &callback1, layer1);
3881 if (err) {
3882 GTEST_SUCCEED() << "test not supported";
3883 return;
3884 }
3885 err = fillTransaction(transaction2, &callback2, layer2);
3886 if (err) {
3887 GTEST_SUCCEED() << "test not supported";
3888 return;
3889 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003890
Marissa Wall861616d2018-10-22 12:52:23 -07003891 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3892 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003893
3894 ExpectedResult expected;
3895 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3896 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3897 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3898 expected.reset();
3899
3900 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003901 err = fillTransaction(transaction1, &callback1);
3902 if (err) {
3903 GTEST_SUCCEED() << "test not supported";
3904 return;
3905 }
3906 err = fillTransaction(transaction2, &callback2);
3907 if (err) {
3908 GTEST_SUCCEED() << "test not supported";
3909 return;
3910 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003911
Marissa Wall861616d2018-10-22 12:52:23 -07003912 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003913
Marissa Wall713b63f2018-10-17 15:42:43 -07003914 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
3915 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003916 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3917 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3918}
3919
3920TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3921 sp<SurfaceControl> layer;
3922 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3923
3924 Transaction transaction;
3925 CallbackHelper callback;
3926 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003927 for (auto& expected : expectedResults) {
3928 expected.reset();
3929 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall5a68a772018-12-22 17:43:42 -08003930 ExpectedResult::Buffer::ACQUIRED,
3931 ExpectedResult::PreviousBuffer::UNKNOWN);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003932
Marissa Wall713b63f2018-10-17 15:42:43 -07003933 int err = fillTransaction(transaction, &callback, layer);
3934 if (err) {
3935 GTEST_SUCCEED() << "test not supported";
3936 return;
3937 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003938
3939 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003940 }
3941 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3942}
3943
3944TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3945 sp<SurfaceControl> layer;
3946 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3947
Marissa Wall713b63f2018-10-17 15:42:43 -07003948 // Normal call to set up test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003949 Transaction transaction;
3950 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003951 int err = fillTransaction(transaction, &callback, layer);
3952 if (err) {
3953 GTEST_SUCCEED() << "test not supported";
3954 return;
3955 }
3956
3957 transaction.apply();
3958
3959 ExpectedResult expected;
3960 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3961 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3962
3963 // Test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003964 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003965 for (auto& expected : expectedResults) {
3966 expected.reset();
3967
Marissa Wall713b63f2018-10-17 15:42:43 -07003968 err = fillTransaction(transaction, &callback);
3969 if (err) {
3970 GTEST_SUCCEED() << "test not supported";
3971 return;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003972 }
3973
3974 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003975 }
3976 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3977}
3978
3979TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3980 sp<SurfaceControl> layer;
3981 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3982
3983 // Normal call to set up test
3984 Transaction transaction;
3985 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003986 int err = fillTransaction(transaction, &callback, layer);
3987 if (err) {
3988 GTEST_SUCCEED() << "test not supported";
3989 return;
3990 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003991
Marissa Wall861616d2018-10-22 12:52:23 -07003992 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003993
3994 ExpectedResult expectedResult;
3995 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3996 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3997
3998 // Test
3999 std::vector<ExpectedResult> expectedResults(50);
4000 for (auto& expected : expectedResults) {
4001 expected.reset();
Marissa Wall713b63f2018-10-17 15:42:43 -07004002 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
4003 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07004004
Marissa Wall713b63f2018-10-17 15:42:43 -07004005 err = fillTransaction(transaction, &callback);
4006 if (err) {
4007 GTEST_SUCCEED() << "test not supported";
4008 return;
4009 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07004010
Marissa Wall861616d2018-10-22 12:52:23 -07004011 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07004012 }
4013 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
4014}
4015
Marissa Wall17b4e452018-12-26 16:32:34 -08004016TEST_F(LayerCallbackTest, DesiredPresentTime) {
4017 sp<SurfaceControl> layer;
4018 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
4019
4020 Transaction transaction;
4021 CallbackHelper callback;
4022 int err = fillTransaction(transaction, &callback, layer);
4023 if (err) {
4024 GTEST_SUCCEED() << "test not supported";
4025 return;
4026 }
4027
4028 // Try to present 100ms in the future
4029 nsecs_t time = systemTime() + (100 * 1e6);
4030
4031 transaction.setDesiredPresentTime(time);
4032 transaction.apply();
4033
4034 ExpectedResult expected;
4035 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
4036 expected.addExpectedPresentTime(time);
4037 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
4038}
4039
4040TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
4041 sp<SurfaceControl> layer;
4042 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
4043
4044 Transaction transaction;
4045 CallbackHelper callback1;
4046 int err = fillTransaction(transaction, &callback1, layer);
4047 if (err) {
4048 GTEST_SUCCEED() << "test not supported";
4049 return;
4050 }
4051
4052 // Try to present 100ms in the future
4053 nsecs_t time = systemTime() + (100 * 1e6);
4054
4055 transaction.setDesiredPresentTime(time);
4056 transaction.apply();
4057
4058 ExpectedResult expected1;
4059 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
4060 expected1.addExpectedPresentTime(time);
4061
4062 CallbackHelper callback2;
4063 err = fillTransaction(transaction, &callback2, layer);
4064 if (err) {
4065 GTEST_SUCCEED() << "test not supported";
4066 return;
4067 }
4068
4069 // Try to present 33ms after the first frame
4070 time += (33.3 * 1e6);
4071
4072 transaction.setDesiredPresentTime(time);
4073 transaction.apply();
4074
4075 ExpectedResult expected2;
4076 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
4077 ExpectedResult::Buffer::ACQUIRED,
4078 ExpectedResult::PreviousBuffer::RELEASED);
4079 expected2.addExpectedPresentTime(time);
4080
4081 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
4082 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
4083}
4084
4085TEST_F(LayerCallbackTest, DesiredPresentTime_OutOfOrder) {
4086 sp<SurfaceControl> layer;
4087 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
4088
4089 Transaction transaction;
4090 CallbackHelper callback1;
4091 int err = fillTransaction(transaction, &callback1, layer);
4092 if (err) {
4093 GTEST_SUCCEED() << "test not supported";
4094 return;
4095 }
4096
4097 // Try to present 100ms in the future
4098 nsecs_t time = systemTime() + (100 * 1e6);
4099
4100 transaction.setDesiredPresentTime(time);
4101 transaction.apply();
4102
4103 ExpectedResult expected1;
4104 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
4105 expected1.addExpectedPresentTime(time);
4106
4107 CallbackHelper callback2;
4108 err = fillTransaction(transaction, &callback2, layer);
4109 if (err) {
4110 GTEST_SUCCEED() << "test not supported";
4111 return;
4112 }
4113
4114 // Try to present 33ms before the previous frame
4115 time -= (33.3 * 1e6);
4116
4117 transaction.setDesiredPresentTime(time);
4118 transaction.apply();
4119
4120 ExpectedResult expected2;
4121 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
4122 ExpectedResult::Buffer::ACQUIRED,
4123 ExpectedResult::PreviousBuffer::RELEASED);
4124
4125 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
4126 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
4127}
4128
4129TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
4130 sp<SurfaceControl> layer;
4131 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
4132
4133 Transaction transaction;
4134 CallbackHelper callback;
4135 int err = fillTransaction(transaction, &callback, layer);
4136 if (err) {
4137 GTEST_SUCCEED() << "test not supported";
4138 return;
4139 }
4140
4141 // Try to present 100ms in the past
4142 nsecs_t time = systemTime() - (100 * 1e6);
4143
4144 transaction.setDesiredPresentTime(time);
4145 transaction.apply();
4146
4147 ExpectedResult expected;
4148 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
4149 expected.addExpectedPresentTime(systemTime());
4150 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
4151}
4152
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004153class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004154protected:
4155 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07004156 LayerTransactionTest::SetUp();
4157 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004158
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08004159 const auto display = SurfaceComposerClient::getInternalDisplayToken();
4160 ASSERT_FALSE(display == nullptr);
4161
Mathias Agopianc666cae2012-07-25 18:56:13 -07004162 DisplayInfo info;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08004163 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
Mathias Agopianc666cae2012-07-25 18:56:13 -07004164
4165 ssize_t displayWidth = info.w;
4166 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004167
4168 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07004169 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
4170 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08004171 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004172 ASSERT_TRUE(mBGSurfaceControl->isValid());
4173 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
4174
4175 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07004176 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
4177
Peiyong Lin566a3b42018-01-09 18:22:43 -08004178 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004179 ASSERT_TRUE(mFGSurfaceControl->isValid());
4180
4181 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4182
4183 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07004184 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08004185 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004186 ASSERT_TRUE(mSyncSurfaceControl->isValid());
4187
4188 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4189
Robert Carr4cdc58f2017-08-23 14:22:20 -07004190 asTransaction([&](Transaction& t) {
4191 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004192
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004193 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07004194
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004195 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
4196 .setPosition(mFGSurfaceControl, 64, 64)
4197 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004198
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004199 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
4200 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
4201 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004202 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004203 }
4204
4205 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07004206 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004207 mBGSurfaceControl = 0;
4208 mFGSurfaceControl = 0;
4209 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004210 }
4211
4212 void waitForPostedBuffers() {
4213 // Since the sync surface is in synchronous mode (i.e. double buffered)
4214 // posting three buffers to it should ensure that at least two
4215 // SurfaceFlinger::handlePageFlip calls have been made, which should
4216 // guaranteed that a buffer posted to another Surface has been retired.
4217 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4218 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4219 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4220 }
4221
Robert Carr4cdc58f2017-08-23 14:22:20 -07004222
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07004223 sp<SurfaceControl> mBGSurfaceControl;
4224 sp<SurfaceControl> mFGSurfaceControl;
4225
4226 // This surface is used to ensure that the buffers posted to
4227 // mFGSurfaceControl have been picked up by SurfaceFlinger.
4228 sp<SurfaceControl> mSyncSurfaceControl;
4229};
4230
Robert Carr7f619b22017-11-06 12:56:35 -08004231TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08004232
chaviw0e3479f2018-09-10 16:49:30 -07004233 std::unique_ptr<ScreenCapture> sc;
4234
4235 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08004236 fillSurfaceRGBA8(relative, 10, 10, 10);
4237 waitForPostedBuffers();
4238
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004239 Transaction{}
4240 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08004241 .setPosition(relative, 64, 64)
4242 .apply();
4243
4244 {
4245 // The relative should be on top of the FG control.
4246 ScreenCapture::captureScreen(&sc);
4247 sc->checkPixel(64, 64, 10, 10, 10);
4248 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004249 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08004250
4251 {
4252 // Nothing should change at this point.
4253 ScreenCapture::captureScreen(&sc);
4254 sc->checkPixel(64, 64, 10, 10, 10);
4255 }
4256
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004257 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08004258
4259 {
4260 // Ensure that the relative was actually hidden, rather than
4261 // being left in the detached but visible state.
4262 ScreenCapture::captureScreen(&sc);
4263 sc->expectFGColor(64, 64);
4264 }
4265}
4266
Robert Carr8d5227b2017-03-16 15:41:03 -07004267class GeometryLatchingTest : public LayerUpdateTest {
4268protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004269 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07004270 SCOPED_TRACE(trace);
4271 ScreenCapture::captureScreen(&sc);
4272 // We find the leading edge of the FG surface.
4273 sc->expectFGColor(127, 127);
4274 sc->expectBGColor(128, 128);
4275 }
Robert Carr7bf247e2017-05-18 14:02:49 -07004276
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004277 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07004278
4279 void unlockFGBuffer() {
4280 sp<Surface> s = mFGSurfaceControl->getSurface();
4281 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
4282 waitForPostedBuffers();
4283 }
4284
Robert Carr8d5227b2017-03-16 15:41:03 -07004285 void completeFGResize() {
4286 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4287 waitForPostedBuffers();
4288 }
4289 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004290 asTransaction([&](Transaction& t) {
4291 t.setSize(mFGSurfaceControl, 64, 64);
4292 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004293 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07004294 });
Robert Carr8d5227b2017-03-16 15:41:03 -07004295
4296 EXPECT_INITIAL_STATE("After restoring initial state");
4297 }
chaviw0e3479f2018-09-10 16:49:30 -07004298 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07004299};
4300
Robert Carr8d5227b2017-03-16 15:41:03 -07004301class CropLatchingTest : public GeometryLatchingTest {
4302protected:
4303 void EXPECT_CROPPED_STATE(const char* trace) {
4304 SCOPED_TRACE(trace);
4305 ScreenCapture::captureScreen(&sc);
4306 // The edge should be moved back one pixel by our crop.
4307 sc->expectFGColor(126, 126);
4308 sc->expectBGColor(127, 127);
4309 sc->expectBGColor(128, 128);
4310 }
chaviw59f5c562017-06-28 16:39:06 -07004311
4312 void EXPECT_RESIZE_STATE(const char* trace) {
4313 SCOPED_TRACE(trace);
4314 ScreenCapture::captureScreen(&sc);
4315 // The FG is now resized too 128,128 at 64,64
4316 sc->expectFGColor(64, 64);
4317 sc->expectFGColor(191, 191);
4318 sc->expectBGColor(192, 192);
4319 }
Robert Carr8d5227b2017-03-16 15:41:03 -07004320};
4321
Pablo Ceballos05289c22016-04-14 15:49:55 -07004322TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07004323 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07004324 {
4325 SCOPED_TRACE("before anything");
4326 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08004327 sc->expectBGColor(32, 32);
4328 sc->expectFGColor(96, 96);
4329 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07004330 }
4331
4332 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07004333 asTransaction([&](Transaction& t) {
4334 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004335 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
4336 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07004337 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07004338
Robert Carr4cdc58f2017-08-23 14:22:20 -07004339 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004340 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004341 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
4342 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004343 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07004344
4345 {
4346 SCOPED_TRACE("before any trigger");
4347 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08004348 sc->expectBGColor(32, 32);
4349 sc->expectFGColor(96, 96);
4350 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07004351 }
4352
4353 // should trigger the first deferred transaction, but not the second one
4354 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4355 {
4356 SCOPED_TRACE("after first trigger");
4357 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08004358 sc->expectBGColor(32, 32);
4359 sc->checkPixel(96, 96, 162, 63, 96);
4360 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07004361 }
4362
4363 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004364 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07004365
4366 // trigger the second deferred transaction
4367 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4368 {
4369 SCOPED_TRACE("after second trigger");
4370 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08004371 sc->expectBGColor(32, 32);
4372 sc->expectBGColor(96, 96);
4373 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07004374 }
4375}
4376
Robert Carre392b552017-09-19 12:16:05 -07004377TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07004378 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07004379
4380 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004381 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
4382 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4383 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
4384 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07004385 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08004386 SurfaceComposerClient::Transaction{}
4387 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
4388 .show(childNoBuffer)
4389 .show(childBuffer)
4390 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07004391 {
4392 ScreenCapture::captureScreen(&sc);
4393 sc->expectChildColor(73, 73);
4394 sc->expectFGColor(74, 74);
4395 }
Vishnu Nair60356342018-11-13 13:00:45 -08004396 SurfaceComposerClient::Transaction{}
4397 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
4398 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07004399 {
4400 ScreenCapture::captureScreen(&sc);
4401 sc->expectChildColor(73, 73);
4402 sc->expectChildColor(74, 74);
4403 }
4404}
4405
Robert Carr2c5f6d22017-09-26 12:30:35 -07004406TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07004407 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07004408 {
4409 SCOPED_TRACE("before move");
4410 ScreenCapture::captureScreen(&sc);
4411 sc->expectBGColor(0, 12);
4412 sc->expectFGColor(75, 75);
4413 sc->expectBGColor(145, 145);
4414 }
4415
4416 Transaction t1, t2;
4417 t1.setPosition(mFGSurfaceControl, 128, 128);
4418 t2.setPosition(mFGSurfaceControl, 0, 0);
4419 // We expect that the position update from t2 now
4420 // overwrites the position update from t1.
4421 t1.merge(std::move(t2));
4422 t1.apply();
4423
4424 {
4425 ScreenCapture::captureScreen(&sc);
4426 sc->expectFGColor(1, 1);
4427 }
4428}
4429
Robert Carr1f0a16a2016-10-24 16:27:39 -07004430class ChildLayerTest : public LayerUpdateTest {
4431protected:
4432 void SetUp() override {
4433 LayerUpdateTest::SetUp();
Vishnu Nairc652ff82019-03-15 12:48:54 -07004434 mChild = createSurface(mClient, "Child surface", 10, 15, PIXEL_FORMAT_RGBA_8888, 0,
Vishnu Nair88a11f22018-11-28 18:30:57 -08004435 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07004436 fillSurfaceRGBA8(mChild, 200, 200, 200);
4437
4438 {
4439 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07004440 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004441 mCapture->expectChildColor(64, 64);
4442 }
4443 }
4444 void TearDown() override {
4445 LayerUpdateTest::TearDown();
4446 mChild = 0;
4447 }
4448
4449 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07004450 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07004451};
4452
4453TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004454 asTransaction([&](Transaction& t) {
4455 t.show(mChild);
4456 t.setPosition(mChild, 10, 10);
4457 t.setPosition(mFGSurfaceControl, 64, 64);
4458 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004459
4460 {
chaviw0e3479f2018-09-10 16:49:30 -07004461 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004462 // Top left of foreground must now be visible
4463 mCapture->expectFGColor(64, 64);
4464 // But 10 pixels in we should see the child surface
4465 mCapture->expectChildColor(74, 74);
4466 // And 10 more pixels we should be back to the foreground surface
4467 mCapture->expectFGColor(84, 84);
4468 }
4469
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004470 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004471
4472 {
chaviw0e3479f2018-09-10 16:49:30 -07004473 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004474 // Top left of foreground should now be at 0, 0
4475 mCapture->expectFGColor(0, 0);
4476 // But 10 pixels in we should see the child surface
4477 mCapture->expectChildColor(10, 10);
4478 // And 10 more pixels we should be back to the foreground surface
4479 mCapture->expectFGColor(20, 20);
4480 }
4481}
4482
Robert Carr41b08b52017-06-01 16:11:34 -07004483TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004484 asTransaction([&](Transaction& t) {
4485 t.show(mChild);
4486 t.setPosition(mChild, 0, 0);
4487 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004488 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07004489 });
Robert Carr41b08b52017-06-01 16:11:34 -07004490
4491 {
chaviw0e3479f2018-09-10 16:49:30 -07004492 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07004493 mCapture->expectChildColor(0, 0);
4494 mCapture->expectChildColor(4, 4);
4495 mCapture->expectBGColor(5, 5);
4496 }
4497}
4498
Robert Carr1f0a16a2016-10-24 16:27:39 -07004499TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004500 asTransaction([&](Transaction& t) {
4501 t.show(mChild);
4502 t.setPosition(mFGSurfaceControl, 0, 0);
4503 t.setPosition(mChild, 63, 63);
4504 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004505
4506 {
chaviw0e3479f2018-09-10 16:49:30 -07004507 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004508 mCapture->expectFGColor(0, 0);
4509 // Last pixel in foreground should now be the child.
4510 mCapture->expectChildColor(63, 63);
4511 // But the child should be constrained and the next pixel
4512 // must be the background
4513 mCapture->expectBGColor(64, 64);
4514 }
4515}
4516
4517TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004518 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004519
4520 // Find the boundary between the parent and child
4521 {
chaviw0e3479f2018-09-10 16:49:30 -07004522 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004523 mCapture->expectChildColor(9, 9);
4524 mCapture->expectFGColor(10, 10);
4525 }
4526
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004527 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004528
4529 // The boundary should be twice as far from the origin now.
4530 // The pixels from the last test should all be child now
4531 {
chaviw0e3479f2018-09-10 16:49:30 -07004532 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004533 mCapture->expectChildColor(9, 9);
4534 mCapture->expectChildColor(10, 10);
4535 mCapture->expectChildColor(19, 19);
4536 mCapture->expectFGColor(20, 20);
4537 }
4538}
Robert Carr9524cb32017-02-13 11:32:32 -08004539
Vishnu Nairc652ff82019-03-15 12:48:54 -07004540// A child with a scale transform should be cropped by its parent bounds.
4541TEST_F(ChildLayerTest, ChildLayerScalingCroppedByParent) {
4542 asTransaction([&](Transaction& t) {
4543 t.setPosition(mFGSurfaceControl, 0, 0);
4544 t.setPosition(mChild, 0, 0);
4545 });
4546
4547 // Find the boundary between the parent and child.
4548 {
4549 mCapture = screenshot();
4550 mCapture->expectChildColor(0, 0);
4551 mCapture->expectChildColor(9, 9);
4552 mCapture->expectFGColor(10, 10);
4553 }
4554
4555 asTransaction([&](Transaction& t) { t.setMatrix(mChild, 10.0, 0, 0, 10.0); });
4556
4557 // The child should fill its parent bounds and be cropped by it.
4558 {
4559 mCapture = screenshot();
4560 mCapture->expectChildColor(0, 0);
4561 mCapture->expectChildColor(63, 63);
4562 mCapture->expectBGColor(64, 64);
4563 }
4564}
4565
Robert Carr6452f122017-03-21 10:41:29 -07004566TEST_F(ChildLayerTest, ChildLayerAlpha) {
4567 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
4568 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
4569 fillSurfaceRGBA8(mChild, 0, 254, 0);
4570 waitForPostedBuffers();
4571
Robert Carr4cdc58f2017-08-23 14:22:20 -07004572 asTransaction([&](Transaction& t) {
4573 t.show(mChild);
4574 t.setPosition(mChild, 0, 0);
4575 t.setPosition(mFGSurfaceControl, 0, 0);
4576 });
Robert Carr6452f122017-03-21 10:41:29 -07004577
4578 {
chaviw0e3479f2018-09-10 16:49:30 -07004579 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004580 // Unblended child color
4581 mCapture->checkPixel(0, 0, 0, 254, 0);
4582 }
4583
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004584 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004585
4586 {
chaviw0e3479f2018-09-10 16:49:30 -07004587 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004588 // Child and BG blended.
4589 mCapture->checkPixel(0, 0, 127, 127, 0);
4590 }
4591
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004592 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004593
4594 {
chaviw0e3479f2018-09-10 16:49:30 -07004595 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004596 // Child and BG blended.
4597 mCapture->checkPixel(0, 0, 95, 64, 95);
4598 }
4599}
4600
Robert Carr9524cb32017-02-13 11:32:32 -08004601TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004602 asTransaction([&](Transaction& t) {
4603 t.show(mChild);
4604 t.setPosition(mChild, 10, 10);
4605 t.setPosition(mFGSurfaceControl, 64, 64);
4606 });
Robert Carr9524cb32017-02-13 11:32:32 -08004607
4608 {
chaviw0e3479f2018-09-10 16:49:30 -07004609 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004610 // Top left of foreground must now be visible
4611 mCapture->expectFGColor(64, 64);
4612 // But 10 pixels in we should see the child surface
4613 mCapture->expectChildColor(74, 74);
4614 // And 10 more pixels we should be back to the foreground surface
4615 mCapture->expectFGColor(84, 84);
4616 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004617
4618 asTransaction([&](Transaction& t) {
4619 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
4620 });
4621
Robert Carr9524cb32017-02-13 11:32:32 -08004622 {
chaviw0e3479f2018-09-10 16:49:30 -07004623 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004624 mCapture->expectFGColor(64, 64);
4625 // In reparenting we should have exposed the entire foreground surface.
4626 mCapture->expectFGColor(74, 74);
4627 // And the child layer should now begin at 10, 10 (since the BG
4628 // layer is at (0, 0)).
4629 mCapture->expectBGColor(9, 9);
4630 mCapture->expectChildColor(10, 10);
4631 }
4632}
4633
Robert Carr2e102c92018-10-23 12:11:15 -07004634TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
4635 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004636 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07004637 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
4638
4639 {
4640 SCOPED_TRACE("Grandchild visible");
4641 ScreenCapture::captureScreen(&mCapture);
4642 mCapture->checkPixel(64, 64, 111, 111, 111);
4643 }
4644
Robert Carr87246532019-02-04 15:20:26 -08004645 mChild.clear();
Robert Carr2e102c92018-10-23 12:11:15 -07004646
4647 {
4648 SCOPED_TRACE("After destroying child");
4649 ScreenCapture::captureScreen(&mCapture);
4650 mCapture->expectFGColor(64, 64);
4651 }
4652
4653 asTransaction([&](Transaction& t) {
4654 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
4655 });
4656
4657 {
4658 SCOPED_TRACE("After reparenting grandchild");
4659 ScreenCapture::captureScreen(&mCapture);
4660 mCapture->checkPixel(64, 64, 111, 111, 111);
4661 }
4662}
4663
chaviw161410b02017-07-27 10:46:08 -07004664TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004665 asTransaction([&](Transaction& t) {
4666 t.show(mChild);
4667 t.setPosition(mChild, 10, 10);
4668 t.setPosition(mFGSurfaceControl, 64, 64);
4669 });
Robert Carr9524cb32017-02-13 11:32:32 -08004670
4671 {
chaviw0e3479f2018-09-10 16:49:30 -07004672 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004673 // Top left of foreground must now be visible
4674 mCapture->expectFGColor(64, 64);
4675 // But 10 pixels in we should see the child surface
4676 mCapture->expectChildColor(74, 74);
4677 // And 10 more pixels we should be back to the foreground surface
4678 mCapture->expectFGColor(84, 84);
4679 }
4680
chaviw0e3479f2018-09-10 16:49:30 -07004681
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004682 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08004683
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004684 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08004685
chaviw161410b02017-07-27 10:46:08 -07004686 // Since the child has the same client as the parent, it will not get
4687 // detached and will be hidden.
4688 {
chaviw0e3479f2018-09-10 16:49:30 -07004689 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004690 mCapture->expectFGColor(64, 64);
4691 mCapture->expectFGColor(74, 74);
4692 mCapture->expectFGColor(84, 84);
4693 }
4694}
4695
4696TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
4697 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004698 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004699 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
4700 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07004701
chaviw161410b02017-07-27 10:46:08 -07004702 ASSERT_TRUE(mChildNewClient->isValid());
4703
4704 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
4705
Robert Carr4cdc58f2017-08-23 14:22:20 -07004706 asTransaction([&](Transaction& t) {
4707 t.hide(mChild);
4708 t.show(mChildNewClient);
4709 t.setPosition(mChildNewClient, 10, 10);
4710 t.setPosition(mFGSurfaceControl, 64, 64);
4711 });
chaviw161410b02017-07-27 10:46:08 -07004712
4713 {
chaviw0e3479f2018-09-10 16:49:30 -07004714 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004715 // Top left of foreground must now be visible
4716 mCapture->expectFGColor(64, 64);
4717 // But 10 pixels in we should see the child surface
4718 mCapture->expectChildColor(74, 74);
4719 // And 10 more pixels we should be back to the foreground surface
4720 mCapture->expectFGColor(84, 84);
4721 }
4722
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004723 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07004724
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004725 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07004726
Robert Carr9524cb32017-02-13 11:32:32 -08004727 // Nothing should have changed.
4728 {
chaviw0e3479f2018-09-10 16:49:30 -07004729 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004730 mCapture->expectFGColor(64, 64);
4731 mCapture->expectChildColor(74, 74);
4732 mCapture->expectFGColor(84, 84);
4733 }
4734}
4735
chaviw5aedec92018-10-22 10:40:38 -07004736TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
4737 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4738 sp<SurfaceControl> childNewClient =
4739 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4740 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4741
4742 ASSERT_TRUE(childNewClient != nullptr);
4743 ASSERT_TRUE(childNewClient->isValid());
4744
4745 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4746
4747 Transaction()
4748 .hide(mChild)
4749 .show(childNewClient)
4750 .setPosition(childNewClient, 10, 10)
4751 .setPosition(mFGSurfaceControl, 64, 64)
4752 .apply();
4753
4754 {
4755 mCapture = screenshot();
4756 // Top left of foreground must now be visible
4757 mCapture->expectFGColor(64, 64);
4758 // But 10 pixels in we should see the child surface
4759 mCapture->expectChildColor(74, 74);
4760 // And 10 more pixels we should be back to the foreground surface
4761 mCapture->expectFGColor(84, 84);
4762 }
4763
4764 Transaction().detachChildren(mFGSurfaceControl).apply();
4765 Transaction().hide(childNewClient).apply();
4766
4767 // Nothing should have changed.
4768 {
4769 mCapture = screenshot();
4770 mCapture->expectFGColor(64, 64);
4771 mCapture->expectChildColor(74, 74);
4772 mCapture->expectFGColor(84, 84);
4773 }
4774
4775 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
4776 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
4777 32);
4778 Transaction()
4779 .setLayer(newParentSurface, INT32_MAX - 1)
4780 .show(newParentSurface)
4781 .setPosition(newParentSurface, 20, 20)
4782 .reparent(childNewClient, newParentSurface->getHandle())
4783 .apply();
4784 {
4785 mCapture = screenshot();
4786 // Child is now hidden.
4787 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
4788 }
4789}
chaviw43cb3cb2019-05-31 15:23:41 -07004790TEST_F(ChildLayerTest, DetachChildrenWithDeferredTransaction) {
4791 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4792 sp<SurfaceControl> childNewClient =
4793 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4794 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4795
4796 ASSERT_TRUE(childNewClient != nullptr);
4797 ASSERT_TRUE(childNewClient->isValid());
4798
4799 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4800
4801 Transaction()
4802 .hide(mChild)
4803 .show(childNewClient)
4804 .setPosition(childNewClient, 10, 10)
4805 .setPosition(mFGSurfaceControl, 64, 64)
4806 .apply();
4807
4808 {
4809 mCapture = screenshot();
4810 Rect rect = Rect(74, 74, 84, 84);
4811 mCapture->expectBorder(rect, Color{195, 63, 63, 255});
4812 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4813 }
4814
4815 Transaction()
4816 .deferTransactionUntil_legacy(childNewClient, mFGSurfaceControl->getHandle(),
4817 mFGSurfaceControl->getSurface()->getNextFrameNumber())
4818 .apply();
4819 Transaction().detachChildren(mFGSurfaceControl).apply();
4820 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(mFGSurfaceControl, Color::RED, 32, 32));
4821
4822 // BufferLayer can still dequeue buffers even though there's a detached layer with a
4823 // deferred transaction.
4824 {
4825 SCOPED_TRACE("new buffer");
4826 mCapture = screenshot();
4827 Rect rect = Rect(74, 74, 84, 84);
4828 mCapture->expectBorder(rect, Color::RED);
4829 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4830 }
4831}
chaviw5aedec92018-10-22 10:40:38 -07004832
Robert Carr9b429f42017-04-17 14:56:57 -07004833TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004834 asTransaction([&](Transaction& t) {
4835 t.show(mChild);
4836 t.setPosition(mChild, 0, 0);
4837 t.setPosition(mFGSurfaceControl, 0, 0);
4838 });
Robert Carr9b429f42017-04-17 14:56:57 -07004839
4840 {
chaviw0e3479f2018-09-10 16:49:30 -07004841 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004842 // We've positioned the child in the top left.
4843 mCapture->expectChildColor(0, 0);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004844 // But it's only 10x15.
4845 mCapture->expectFGColor(10, 15);
Robert Carr9b429f42017-04-17 14:56:57 -07004846 }
4847
Robert Carr4cdc58f2017-08-23 14:22:20 -07004848 asTransaction([&](Transaction& t) {
4849 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4850 // We cause scaling by 2.
4851 t.setSize(mFGSurfaceControl, 128, 128);
4852 });
Robert Carr9b429f42017-04-17 14:56:57 -07004853
4854 {
chaviw0e3479f2018-09-10 16:49:30 -07004855 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004856 // We've positioned the child in the top left.
4857 mCapture->expectChildColor(0, 0);
4858 mCapture->expectChildColor(10, 10);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004859 mCapture->expectChildColor(19, 29);
4860 // And now it should be scaled all the way to 20x30
4861 mCapture->expectFGColor(20, 30);
Robert Carr9b429f42017-04-17 14:56:57 -07004862 }
4863}
4864
Robert Carr1725eee2017-04-26 18:32:15 -07004865// Regression test for b/37673612
4866TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004867 asTransaction([&](Transaction& t) {
4868 t.show(mChild);
4869 t.setPosition(mChild, 0, 0);
4870 t.setPosition(mFGSurfaceControl, 0, 0);
4871 });
Robert Carr1725eee2017-04-26 18:32:15 -07004872
4873 {
chaviw0e3479f2018-09-10 16:49:30 -07004874 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004875 // We've positioned the child in the top left.
4876 mCapture->expectChildColor(0, 0);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004877 mCapture->expectChildColor(9, 14);
4878 // But it's only 10x15.
4879 mCapture->expectFGColor(10, 15);
Robert Carr1725eee2017-04-26 18:32:15 -07004880 }
Robert Carr1725eee2017-04-26 18:32:15 -07004881 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
4882 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004883 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07004884 sp<Surface> s = mFGSurfaceControl->getSurface();
4885 auto anw = static_cast<ANativeWindow*>(s.get());
4886 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4887 native_window_set_buffers_dimensions(anw, 64, 128);
4888 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4889 waitForPostedBuffers();
4890
4891 {
4892 // The child should still be in the same place and not have any strange scaling as in
4893 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07004894 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004895 mCapture->expectChildColor(0, 0);
4896 mCapture->expectFGColor(10, 10);
4897 }
4898}
4899
Vishnu Nairc652ff82019-03-15 12:48:54 -07004900// A child with a buffer transform from its parents should be cropped by its parent bounds.
4901TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferTransform) {
4902 asTransaction([&](Transaction& t) {
4903 t.show(mChild);
4904 t.setPosition(mChild, 0, 0);
4905 t.setPosition(mFGSurfaceControl, 0, 0);
4906 t.setSize(mChild, 100, 100);
4907 });
4908 fillSurfaceRGBA8(mChild, 200, 200, 200);
4909
4910 {
4911 mCapture = screenshot();
4912
4913 mCapture->expectChildColor(0, 0);
4914 mCapture->expectChildColor(63, 63);
4915 mCapture->expectBGColor(64, 64);
4916 }
4917
4918 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
4919 sp<Surface> s = mFGSurfaceControl->getSurface();
4920 auto anw = static_cast<ANativeWindow*>(s.get());
4921 // Apply a 90 transform on the buffer.
4922 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4923 native_window_set_buffers_dimensions(anw, 64, 128);
4924 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4925 waitForPostedBuffers();
4926
4927 // The child should be cropped by the new parent bounds.
4928 {
4929 mCapture = screenshot();
4930 mCapture->expectChildColor(0, 0);
4931 mCapture->expectChildColor(99, 63);
4932 mCapture->expectFGColor(100, 63);
4933 mCapture->expectBGColor(128, 64);
4934 }
4935}
4936
4937// A child with a scale transform from its parents should be cropped by its parent bounds.
4938TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferScale) {
4939 asTransaction([&](Transaction& t) {
4940 t.show(mChild);
4941 t.setPosition(mChild, 0, 0);
4942 t.setPosition(mFGSurfaceControl, 0, 0);
4943 t.setSize(mChild, 200, 200);
4944 });
4945 fillSurfaceRGBA8(mChild, 200, 200, 200);
4946
4947 {
4948 mCapture = screenshot();
4949
4950 mCapture->expectChildColor(0, 0);
4951 mCapture->expectChildColor(63, 63);
4952 mCapture->expectBGColor(64, 64);
4953 }
4954
4955 asTransaction([&](Transaction& t) {
4956 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4957 // Set a scaling by 2.
4958 t.setSize(mFGSurfaceControl, 128, 128);
4959 });
4960
4961 // Child should inherit its parents scale but should be cropped by its parent bounds.
4962 {
4963 mCapture = screenshot();
4964 mCapture->expectChildColor(0, 0);
4965 mCapture->expectChildColor(127, 127);
4966 mCapture->expectBGColor(128, 128);
4967 }
4968}
4969
4970// Regression test for b/127368943
4971// Child should ignore the buffer transform but apply parent scale transform.
4972TEST_F(ChildLayerTest, ChildrenWithParentBufferTransformAndScale) {
4973 asTransaction([&](Transaction& t) {
4974 t.show(mChild);
4975 t.setPosition(mChild, 0, 0);
4976 t.setPosition(mFGSurfaceControl, 0, 0);
4977 });
4978
4979 {
4980 mCapture = screenshot();
4981 mCapture->expectChildColor(0, 0);
4982 mCapture->expectChildColor(9, 14);
4983 mCapture->expectFGColor(10, 15);
4984 }
4985
4986 // Change the size of the foreground to 128 * 64 so we can test rotation as well.
4987 asTransaction([&](Transaction& t) {
4988 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4989 t.setSize(mFGSurfaceControl, 128, 64);
4990 });
4991 sp<Surface> s = mFGSurfaceControl->getSurface();
4992 auto anw = static_cast<ANativeWindow*>(s.get());
4993 // Apply a 90 transform on the buffer and submit a buffer half the expected size so that we
4994 // have an effective scale of 2.0 applied to the buffer along with a rotation transform.
4995 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4996 native_window_set_buffers_dimensions(anw, 32, 64);
4997 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4998 waitForPostedBuffers();
4999
5000 // The child should ignore the buffer transform but apply the 2.0 scale from parent.
5001 {
5002 mCapture = screenshot();
5003 mCapture->expectChildColor(0, 0);
5004 mCapture->expectChildColor(19, 29);
5005 mCapture->expectFGColor(20, 30);
5006 }
5007}
5008
Dan Stoza412903f2017-04-27 13:42:17 -07005009TEST_F(ChildLayerTest, Bug36858924) {
5010 // Destroy the child layer
5011 mChild.clear();
5012
5013 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08005014 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
5015 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07005016
5017 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07005018 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07005019 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
5020 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07005021 t.show(mChild);
5022 });
Dan Stoza412903f2017-04-27 13:42:17 -07005023
5024 // Render the foreground surface a few times
5025 //
5026 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
5027 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
5028 // never acquire/release the first buffer
5029 ALOGI("Filling 1");
5030 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
5031 ALOGI("Filling 2");
5032 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
5033 ALOGI("Filling 3");
5034 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
5035 ALOGI("Filling 4");
5036 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
5037}
5038
chaviwf1961f72017-09-18 16:41:07 -07005039TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07005040 asTransaction([&](Transaction& t) {
5041 t.show(mChild);
5042 t.setPosition(mChild, 10, 10);
5043 t.setPosition(mFGSurfaceControl, 64, 64);
5044 });
chaviw06178942017-07-27 10:25:59 -07005045
5046 {
chaviw0e3479f2018-09-10 16:49:30 -07005047 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07005048 // Top left of foreground must now be visible
5049 mCapture->expectFGColor(64, 64);
5050 // But 10 pixels in we should see the child surface
5051 mCapture->expectChildColor(74, 74);
5052 // And 10 more pixels we should be back to the foreground surface
5053 mCapture->expectFGColor(84, 84);
5054 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07005055
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005056 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07005057
chaviw06178942017-07-27 10:25:59 -07005058 {
chaviw0e3479f2018-09-10 16:49:30 -07005059 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07005060 mCapture->expectFGColor(64, 64);
5061 // In reparenting we should have exposed the entire foreground surface.
5062 mCapture->expectFGColor(74, 74);
5063 // And the child layer should now begin at 10, 10 (since the BG
5064 // layer is at (0, 0)).
5065 mCapture->expectBGColor(9, 9);
5066 mCapture->expectChildColor(10, 10);
5067 }
5068}
5069
chaviwf1961f72017-09-18 16:41:07 -07005070TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07005071 asTransaction([&](Transaction& t) {
5072 t.show(mChild);
5073 t.setPosition(mChild, 10, 10);
5074 t.setPosition(mFGSurfaceControl, 64, 64);
5075 });
chaviwf1961f72017-09-18 16:41:07 -07005076
5077 {
chaviw0e3479f2018-09-10 16:49:30 -07005078 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07005079 // Top left of foreground must now be visible
5080 mCapture->expectFGColor(64, 64);
5081 // But 10 pixels in we should see the child surface
5082 mCapture->expectChildColor(74, 74);
5083 // And 10 more pixels we should be back to the foreground surface
5084 mCapture->expectFGColor(84, 84);
5085 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005086 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07005087 {
chaviw0e3479f2018-09-10 16:49:30 -07005088 mCapture = screenshot();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08005089 // The surface should now be offscreen.
chaviwf1961f72017-09-18 16:41:07 -07005090 mCapture->expectFGColor(64, 64);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08005091 mCapture->expectFGColor(74, 74);
chaviwf1961f72017-09-18 16:41:07 -07005092 mCapture->expectFGColor(84, 84);
5093 }
5094}
5095
5096TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07005097 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08005098 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07005099 ASSERT_TRUE(newSurface->isValid());
5100
5101 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07005102 asTransaction([&](Transaction& t) {
5103 t.hide(mChild);
5104 t.show(newSurface);
5105 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005106 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07005107 t.setPosition(mFGSurfaceControl, 64, 64);
5108 });
chaviwf1961f72017-09-18 16:41:07 -07005109
5110 {
chaviw0e3479f2018-09-10 16:49:30 -07005111 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07005112 // Top left of foreground must now be visible
5113 mCapture->expectFGColor(64, 64);
5114 // At 10, 10 we should see the new surface
5115 mCapture->checkPixel(10, 10, 63, 195, 63);
5116 }
5117
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005118 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07005119
5120 {
chaviw0e3479f2018-09-10 16:49:30 -07005121 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07005122 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
5123 // mFGSurface, putting it at 74, 74.
5124 mCapture->expectFGColor(64, 64);
5125 mCapture->checkPixel(74, 74, 63, 195, 63);
5126 mCapture->expectFGColor(84, 84);
5127 }
5128}
5129
chaviwc9674332017-08-28 12:32:18 -07005130TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005131 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
5132 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07005133 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5134
5135 {
chaviw0e3479f2018-09-10 16:49:30 -07005136 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07005137 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
5138 // which begins at 64, 64
5139 mCapture->checkPixel(64, 64, 50, 50, 50);
5140 }
5141}
5142
Robert Carr503c7042017-09-27 15:06:08 -07005143TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07005144 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07005145 fillSurfaceRGBA8(relative, 255, 255, 255);
5146
5147 Transaction t;
5148 t.setLayer(relative, INT32_MAX)
5149 .setRelativeLayer(mChild, relative->getHandle(), 1)
5150 .setPosition(mFGSurfaceControl, 0, 0)
5151 .apply(true);
5152
5153 // We expect that the child should have been elevated above our
5154 // INT_MAX layer even though it's not a child of it.
5155 {
chaviw0e3479f2018-09-10 16:49:30 -07005156 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07005157 mCapture->expectChildColor(0, 0);
5158 mCapture->expectChildColor(9, 9);
5159 mCapture->checkPixel(10, 10, 255, 255, 255);
5160 }
5161}
Vishnu Nairc652ff82019-03-15 12:48:54 -07005162
Vishnu Nair60356342018-11-13 13:00:45 -08005163class BoundlessLayerTest : public LayerUpdateTest {
5164protected:
5165 std::unique_ptr<ScreenCapture> mCapture;
5166};
5167
5168// Verify setting a size on a buffer layer has no effect.
5169TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
5170 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08005171 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
5172 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08005173 ASSERT_TRUE(bufferLayer->isValid());
5174 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
5175 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
5176 {
5177 mCapture = screenshot();
5178 // Top left of background must now be visible
5179 mCapture->expectBGColor(0, 0);
5180 // Foreground Surface bounds must be color layer
5181 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
5182 // Buffer layer should not extend past buffer bounds
5183 mCapture->expectFGColor(95, 95);
5184 }
5185}
5186
5187// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
5188// which will crop the color layer.
5189TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
5190 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08005191 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5192 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08005193 ASSERT_TRUE(colorLayer->isValid());
5194 asTransaction([&](Transaction& t) {
5195 t.setColor(colorLayer, half3{0, 0, 0});
5196 t.show(colorLayer);
5197 });
5198 {
5199 mCapture = screenshot();
5200 // Top left of background must now be visible
5201 mCapture->expectBGColor(0, 0);
5202 // Foreground Surface bounds must be color layer
5203 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
5204 // Color layer should not extend past foreground bounds
5205 mCapture->expectBGColor(129, 129);
5206 }
5207}
5208
5209// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
5210// a crop which will be used to crop the color layer.
5211TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005212 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5213 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08005214 ASSERT_TRUE(cropLayer->isValid());
5215 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08005216 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5217 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08005218 ASSERT_TRUE(colorLayer->isValid());
5219 asTransaction([&](Transaction& t) {
5220 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
5221 t.setColor(colorLayer, half3{0, 0, 0});
5222 t.show(cropLayer);
5223 t.show(colorLayer);
5224 });
5225 {
5226 mCapture = screenshot();
5227 // Top left of background must now be visible
5228 mCapture->expectBGColor(0, 0);
5229 // Top left of foreground must now be visible
5230 mCapture->expectFGColor(64, 64);
5231 // 5 pixels from the foreground we should see the child surface
5232 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
5233 // 10 pixels from the foreground we should be back to the foreground surface
5234 mCapture->expectFGColor(74, 74);
5235 }
5236}
5237
5238// Verify for boundless layer with no children, their transforms have no effect.
5239TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
5240 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08005241 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5242 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08005243 ASSERT_TRUE(colorLayer->isValid());
5244 asTransaction([&](Transaction& t) {
5245 t.setPosition(colorLayer, 320, 320);
5246 t.setMatrix(colorLayer, 2, 0, 0, 2);
5247 t.setColor(colorLayer, half3{0, 0, 0});
5248 t.show(colorLayer);
5249 });
5250 {
5251 mCapture = screenshot();
5252 // Top left of background must now be visible
5253 mCapture->expectBGColor(0, 0);
5254 // Foreground Surface bounds must be color layer
5255 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
5256 // Color layer should not extend past foreground bounds
5257 mCapture->expectBGColor(129, 129);
5258 }
5259}
5260
5261// Verify for boundless layer with children, their transforms have an effect.
5262TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
5263 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08005264 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
5265 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08005266 ASSERT_TRUE(boundlessLayerRightShift->isValid());
5267 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08005268 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
5269 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08005270 ASSERT_TRUE(boundlessLayerDownShift->isValid());
5271 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08005272 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5273 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08005274 ASSERT_TRUE(colorLayer->isValid());
5275 asTransaction([&](Transaction& t) {
5276 t.setPosition(boundlessLayerRightShift, 32, 0);
5277 t.show(boundlessLayerRightShift);
5278 t.setPosition(boundlessLayerDownShift, 0, 32);
5279 t.show(boundlessLayerDownShift);
5280 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
5281 t.setColor(colorLayer, half3{0, 0, 0});
5282 t.show(colorLayer);
5283 });
5284 {
5285 mCapture = screenshot();
5286 // Top left of background must now be visible
5287 mCapture->expectBGColor(0, 0);
5288 // Top left of foreground must now be visible
5289 mCapture->expectFGColor(64, 64);
5290 // Foreground Surface bounds must be color layer
5291 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
5292 // Color layer should not extend past foreground bounds
5293 mCapture->expectBGColor(129, 129);
5294 }
5295}
5296
5297// Verify child layers do not get clipped if they temporarily move into the negative
5298// coordinate space as the result of an intermediate transformation.
5299TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
5300 sp<SurfaceControl> boundlessLayer =
5301 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
5302 0 /* flags */, mFGSurfaceControl.get());
5303 ASSERT_TRUE(boundlessLayer != nullptr);
5304 ASSERT_TRUE(boundlessLayer->isValid());
5305 sp<SurfaceControl> colorLayer =
5306 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
5307 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
5308 ASSERT_TRUE(colorLayer != nullptr);
5309 ASSERT_TRUE(colorLayer->isValid());
5310 asTransaction([&](Transaction& t) {
5311 // shift child layer off bounds. If this layer was not boundless, we will
5312 // expect the child layer to be cropped.
5313 t.setPosition(boundlessLayer, 32, 32);
5314 t.show(boundlessLayer);
5315 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
5316 // undo shift by parent
5317 t.setPosition(colorLayer, -32, -32);
5318 t.setColor(colorLayer, half3{0, 0, 0});
5319 t.show(colorLayer);
5320 });
5321 {
5322 mCapture = screenshot();
5323 // Top left of background must now be visible
5324 mCapture->expectBGColor(0, 0);
5325 // Foreground Surface bounds must be color layer
5326 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
5327 // Color layer should not extend past foreground bounds
5328 mCapture->expectBGColor(129, 129);
5329 }
5330}
5331
5332// Verify for boundless root layers with children, their transforms have an effect.
5333TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005334 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
5335 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08005336 ASSERT_TRUE(rootBoundlessLayer->isValid());
5337 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08005338 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5339 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
5340
Vishnu Nair60356342018-11-13 13:00:45 -08005341 ASSERT_TRUE(colorLayer->isValid());
5342 asTransaction([&](Transaction& t) {
5343 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
5344 t.setPosition(rootBoundlessLayer, 32, 32);
5345 t.show(rootBoundlessLayer);
5346 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
5347 t.setColor(colorLayer, half3{0, 0, 0});
5348 t.show(colorLayer);
5349 t.hide(mFGSurfaceControl);
5350 });
5351 {
5352 mCapture = screenshot();
5353 // Top left of background must now be visible
5354 mCapture->expectBGColor(0, 0);
5355 // Top left of foreground must now be visible
5356 mCapture->expectBGColor(31, 31);
5357 // Foreground Surface bounds must be color layer
5358 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
5359 // Color layer should not extend past foreground bounds
5360 mCapture->expectBGColor(97, 97);
5361 }
5362}
Robert Carr503c7042017-09-27 15:06:08 -07005363
chaviwa76b2712017-09-20 12:02:26 -07005364class ScreenCaptureTest : public LayerUpdateTest {
5365protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005366 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07005367};
5368
5369TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
5370 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005371 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07005372 mCapture->expectBGColor(0, 0);
5373 // Doesn't capture FG layer which is at 64, 64
5374 mCapture->expectBGColor(64, 64);
5375}
5376
5377TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
5378 auto fgHandle = mFGSurfaceControl->getHandle();
5379
Vishnu Nair88a11f22018-11-28 18:30:57 -08005380 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5381 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005382 fillSurfaceRGBA8(child, 200, 200, 200);
5383
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005384 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005385
5386 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005387 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07005388 mCapture->expectFGColor(10, 10);
5389 mCapture->expectChildColor(0, 0);
5390}
5391
Robert Carr578038f2018-03-09 12:25:24 -08005392TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
5393 auto fgHandle = mFGSurfaceControl->getHandle();
5394
Vishnu Nair88a11f22018-11-28 18:30:57 -08005395 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5396 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08005397 fillSurfaceRGBA8(child, 200, 200, 200);
5398
5399 SurfaceComposerClient::Transaction().show(child).apply(true);
5400
5401 // Captures mFGSurfaceControl's child
5402 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
5403 mCapture->checkPixel(10, 10, 0, 0, 0);
5404 mCapture->expectChildColor(0, 0);
5405}
5406
Robert Carr866455f2019-04-02 16:28:26 -07005407TEST_F(ScreenCaptureTest, CaptureLayerExclude) {
5408 auto fgHandle = mFGSurfaceControl->getHandle();
5409
5410 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5411 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5412 fillSurfaceRGBA8(child, 200, 200, 200);
5413 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
5414 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5415 fillSurfaceRGBA8(child2, 200, 0, 200);
5416
5417 SurfaceComposerClient::Transaction()
5418 .show(child)
5419 .show(child2)
5420 .setLayer(child, 1)
5421 .setLayer(child2, 2)
5422 .apply(true);
5423
5424 // Child2 would be visible but its excluded, so we should see child1 color instead.
5425 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
5426 mCapture->checkPixel(10, 10, 0, 0, 0);
5427 mCapture->checkPixel(0, 0, 200, 200, 200);
5428}
5429
5430// Like the last test but verifies that children are also exclude.
5431TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) {
5432 auto fgHandle = mFGSurfaceControl->getHandle();
5433
5434 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5435 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5436 fillSurfaceRGBA8(child, 200, 200, 200);
5437 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
5438 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5439 fillSurfaceRGBA8(child2, 200, 0, 200);
5440 sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10,
5441 PIXEL_FORMAT_RGBA_8888, 0, child2.get());
5442 fillSurfaceRGBA8(child2, 200, 0, 200);
5443
5444 SurfaceComposerClient::Transaction()
5445 .show(child)
5446 .show(child2)
5447 .show(child3)
5448 .setLayer(child, 1)
5449 .setLayer(child2, 2)
5450 .apply(true);
5451
5452 // Child2 would be visible but its excluded, so we should see child1 color instead.
5453 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
5454 mCapture->checkPixel(10, 10, 0, 0, 0);
5455 mCapture->checkPixel(0, 0, 200, 200, 200);
5456}
5457
chaviw50da5042018-04-09 13:49:37 -07005458TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005459 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5460 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07005461
5462 fillSurfaceRGBA8(child, 200, 200, 200);
5463
5464 SurfaceComposerClient::Transaction().show(child).apply(true);
5465
5466 auto childHandle = child->getHandle();
5467
5468 // Captures child
5469 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
5470 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
5471 // Area outside of child's bounds is transparent.
5472 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
5473}
5474
chaviw4b129c22018-04-09 16:19:43 -07005475TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
5476 auto fgHandle = mFGSurfaceControl->getHandle();
5477
Vishnu Nair88a11f22018-11-28 18:30:57 -08005478 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5479 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5480 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07005481 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07005482 fillSurfaceRGBA8(child, 200, 200, 200);
5483 fillSurfaceRGBA8(relative, 100, 100, 100);
5484
5485 SurfaceComposerClient::Transaction()
5486 .show(child)
5487 // Set relative layer above fg layer so should be shown above when computing all layers.
5488 .setRelativeLayer(relative, fgHandle, 1)
5489 .show(relative)
5490 .apply(true);
5491
5492 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
5493 ScreenCapture::captureLayers(&mCapture, fgHandle);
5494 mCapture->expectFGColor(10, 10);
5495 mCapture->expectChildColor(0, 0);
5496}
5497
5498TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
5499 auto fgHandle = mFGSurfaceControl->getHandle();
5500
Vishnu Nair88a11f22018-11-28 18:30:57 -08005501 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5502 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5503 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
5504 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07005505 fillSurfaceRGBA8(child, 200, 200, 200);
5506 fillSurfaceRGBA8(relative, 100, 100, 100);
5507
5508 SurfaceComposerClient::Transaction()
5509 .show(child)
5510 // Set relative layer below fg layer but relative to child layer so it should be shown
5511 // above child layer.
5512 .setLayer(relative, -1)
5513 .setRelativeLayer(relative, child->getHandle(), 1)
5514 .show(relative)
5515 .apply(true);
5516
5517 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
5518 // relative value should be taken into account, placing it above child layer.
5519 ScreenCapture::captureLayers(&mCapture, fgHandle);
5520 mCapture->expectFGColor(10, 10);
5521 // Relative layer is showing on top of child layer
5522 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
5523}
Robert Carr578038f2018-03-09 12:25:24 -08005524
5525// In the following tests we verify successful skipping of a parent layer,
5526// so we use the same verification logic and only change how we mutate
5527// the parent layer to verify that various properties are ignored.
5528class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
5529public:
5530 void SetUp() override {
5531 LayerUpdateTest::SetUp();
5532
Vishnu Nair88a11f22018-11-28 18:30:57 -08005533 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
5534 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08005535 fillSurfaceRGBA8(mChild, 200, 200, 200);
5536
5537 SurfaceComposerClient::Transaction().show(mChild).apply(true);
5538 }
5539
Vishnu Nair333a9572019-02-15 16:05:56 -08005540 void verify(std::function<void()> verifyStartingState) {
5541 // Verify starting state before a screenshot is taken.
5542 verifyStartingState();
5543
5544 // Verify child layer does not inherit any of the properties of its
5545 // parent when its screenshot is captured.
Robert Carr578038f2018-03-09 12:25:24 -08005546 auto fgHandle = mFGSurfaceControl->getHandle();
5547 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
5548 mCapture->checkPixel(10, 10, 0, 0, 0);
5549 mCapture->expectChildColor(0, 0);
Vishnu Nair333a9572019-02-15 16:05:56 -08005550
5551 // Verify all assumptions are still true after the screenshot is taken.
5552 verifyStartingState();
Robert Carr578038f2018-03-09 12:25:24 -08005553 }
5554
5555 std::unique_ptr<ScreenCapture> mCapture;
5556 sp<SurfaceControl> mChild;
5557};
5558
Vishnu Nair333a9572019-02-15 16:05:56 -08005559// Regression test b/76099859
Robert Carr578038f2018-03-09 12:25:24 -08005560TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
5561
5562 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
5563
5564 // Even though the parent is hidden we should still capture the child.
Vishnu Nair333a9572019-02-15 16:05:56 -08005565
5566 // Before and after reparenting, verify child is properly hidden
5567 // when rendering full-screen.
5568 verify([&] { screenshot()->expectBGColor(64, 64); });
Robert Carr578038f2018-03-09 12:25:24 -08005569}
5570
5571TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07005572 SurfaceComposerClient::Transaction()
5573 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
5574 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005575
5576 // Even though the parent is cropped out we should still capture the child.
Vishnu Nair333a9572019-02-15 16:05:56 -08005577
5578 // Before and after reparenting, verify child is cropped by parent.
5579 verify([&] { screenshot()->expectBGColor(65, 65); });
Robert Carr578038f2018-03-09 12:25:24 -08005580}
5581
Vishnu Nair333a9572019-02-15 16:05:56 -08005582// Regression test b/124372894
Robert Carr578038f2018-03-09 12:25:24 -08005583TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
Vishnu Nair333a9572019-02-15 16:05:56 -08005584 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005585
5586 // We should not inherit the parent scaling.
Robert Carr578038f2018-03-09 12:25:24 -08005587
Vishnu Nair333a9572019-02-15 16:05:56 -08005588 // Before and after reparenting, verify child is properly scaled.
5589 verify([&] { screenshot()->expectChildColor(80, 80); });
Robert Carr15eae092018-03-23 13:43:53 -07005590}
5591
5592
chaviwa76b2712017-09-20 12:02:26 -07005593TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
5594 auto fgHandle = mFGSurfaceControl->getHandle();
5595
Vishnu Nair88a11f22018-11-28 18:30:57 -08005596 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5597 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005598 fillSurfaceRGBA8(child, 200, 200, 200);
5599
Vishnu Nair88a11f22018-11-28 18:30:57 -08005600 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5601 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07005602
5603 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5604 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005605 .show(child)
5606 .setPosition(grandchild, 5, 5)
5607 .show(grandchild)
5608 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005609
5610 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005611 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07005612 mCapture->expectFGColor(10, 10);
5613 mCapture->expectChildColor(0, 0);
5614 mCapture->checkPixel(5, 5, 50, 50, 50);
5615}
5616
5617TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005618 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5619 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005620 fillSurfaceRGBA8(child, 200, 200, 200);
5621 auto childHandle = child->getHandle();
5622
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005623 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005624
5625 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005626 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07005627 mCapture->expectChildColor(0, 0);
5628 mCapture->expectChildColor(9, 9);
5629}
5630
5631TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005632 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5633 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005634 fillSurfaceRGBA8(child, 200, 200, 200);
5635 auto childHandle = child->getHandle();
5636
Vishnu Nair88a11f22018-11-28 18:30:57 -08005637 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5638 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07005639 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5640
5641 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005642 .show(child)
5643 .setPosition(grandchild, 5, 5)
5644 .show(grandchild)
5645 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005646
5647 auto grandchildHandle = grandchild->getHandle();
5648
5649 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005650 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07005651 mCapture->checkPixel(0, 0, 50, 50, 50);
5652 mCapture->checkPixel(4, 4, 50, 50, 50);
5653}
5654
chaviw7206d492017-11-10 16:16:12 -08005655TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07005656 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005657 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5658 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005659
Marissa Wall61c58622018-07-18 10:12:20 -07005660 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5661 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005662
5663 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005664 .setLayer(redLayer, INT32_MAX - 1)
5665 .show(redLayer)
5666 .show(blueLayer)
5667 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005668
5669 auto redLayerHandle = redLayer->getHandle();
5670
5671 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005672 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5673 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5674 // red area below the blue area
5675 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5676 // red area to the right of the blue area
5677 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005678
Marissa Wall861616d2018-10-22 12:52:23 -07005679 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005680 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08005681 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
5682 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005683 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08005684 mCapture->checkPixel(30, 30, 0, 0, 0);
5685}
5686
5687TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07005688 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005689 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5690 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005691
Marissa Wall61c58622018-07-18 10:12:20 -07005692 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5693 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005694
5695 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005696 .setLayer(redLayer, INT32_MAX - 1)
5697 .show(redLayer)
5698 .show(blueLayer)
5699 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005700
5701 auto redLayerHandle = redLayer->getHandle();
5702
5703 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005704 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5705 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5706 // red area below the blue area
5707 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5708 // red area to the right of the blue area
5709 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005710
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005711 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08005712 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005713 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
5714 // red area below the blue area
5715 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
5716 // red area to the right of the blue area
5717 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005718 mCapture->checkPixel(30, 30, 0, 0, 0);
5719}
5720
5721TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07005722 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08005723
Marissa Wall61c58622018-07-18 10:12:20 -07005724 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08005725
5726 auto redLayerHandle = redLayer->getHandle();
Robert Carr87246532019-02-04 15:20:26 -08005727 redLayer.clear();
chaviw7206d492017-11-10 16:16:12 -08005728 SurfaceComposerClient::Transaction().apply(true);
5729
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005730 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08005731
5732 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005733 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
5734 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08005735}
5736
chaviw8e3fe5d2018-02-22 10:55:42 -08005737
5738class DereferenceSurfaceControlTest : public LayerTransactionTest {
5739protected:
5740 void SetUp() override {
5741 LayerTransactionTest::SetUp();
5742 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005743 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005744 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005745 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005746 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
5747 {
5748 SCOPED_TRACE("before anything");
5749 auto shot = screenshot();
5750 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5751 }
5752 }
5753 void TearDown() override {
5754 LayerTransactionTest::TearDown();
5755 bgLayer = 0;
5756 fgLayer = 0;
5757 }
5758
5759 sp<SurfaceControl> bgLayer;
5760 sp<SurfaceControl> fgLayer;
5761};
5762
5763TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
5764 fgLayer = nullptr;
5765 {
5766 SCOPED_TRACE("after setting null");
5767 auto shot = screenshot();
5768 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
5769 }
5770}
5771
5772TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
5773 auto transaction = Transaction().show(fgLayer);
5774 fgLayer = nullptr;
5775 {
5776 SCOPED_TRACE("after setting null");
5777 auto shot = screenshot();
5778 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5779 }
5780}
5781
Vishnu Nairb927e1f2019-02-19 13:36:15 -08005782class MultiDisplayLayerBoundsTest : public LayerTransactionTest {
5783protected:
5784 virtual void SetUp() {
5785 LayerTransactionTest::SetUp();
5786 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5787
5788 mMainDisplay = SurfaceComposerClient::getInternalDisplayToken();
5789 SurfaceComposerClient::getDisplayInfo(mMainDisplay, &mMainDisplayInfo);
5790
5791 sp<IGraphicBufferConsumer> consumer;
5792 BufferQueue::createBufferQueue(&mProducer, &consumer);
5793 consumer->setConsumerName(String8("Virtual disp consumer"));
5794 consumer->setDefaultBufferSize(mMainDisplayInfo.w, mMainDisplayInfo.h);
5795 }
5796
5797 virtual void TearDown() {
5798 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
5799 LayerTransactionTest::TearDown();
5800 mColorLayer = 0;
5801 }
5802
5803 void createDisplay(const Rect& layerStackRect, uint32_t layerStack) {
5804 mVirtualDisplay =
5805 SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), false /*secure*/);
5806 asTransaction([&](Transaction& t) {
5807 t.setDisplaySurface(mVirtualDisplay, mProducer);
5808 t.setDisplayLayerStack(mVirtualDisplay, layerStack);
5809 t.setDisplayProjection(mVirtualDisplay, mMainDisplayInfo.orientation, layerStackRect,
5810 Rect(mMainDisplayInfo.w, mMainDisplayInfo.h));
5811 });
5812 }
5813
5814 void createColorLayer(uint32_t layerStack) {
5815 mColorLayer =
5816 createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */,
5817 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
5818 ASSERT_TRUE(mColorLayer != nullptr);
5819 ASSERT_TRUE(mColorLayer->isValid());
5820 asTransaction([&](Transaction& t) {
5821 t.setLayerStack(mColorLayer, layerStack);
5822 t.setCrop_legacy(mColorLayer, Rect(0, 0, 30, 40));
5823 t.setLayer(mColorLayer, INT32_MAX - 2);
5824 t.setColor(mColorLayer,
5825 half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f,
5826 mExpectedColor.b / 255.0f});
5827 t.show(mColorLayer);
5828 });
5829 }
5830
5831 DisplayInfo mMainDisplayInfo;
5832 sp<IBinder> mMainDisplay;
5833 sp<IBinder> mVirtualDisplay;
5834 sp<IGraphicBufferProducer> mProducer;
5835 sp<SurfaceControl> mColorLayer;
5836 Color mExpectedColor = {63, 63, 195, 255};
5837};
5838
5839TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) {
5840 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 1 /* layerStack */);
5841 createColorLayer(1 /* layerStack */);
5842
5843 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5844
5845 // Verify color layer does not render on main display.
5846 std::unique_ptr<ScreenCapture> sc;
5847 ScreenCapture::captureScreen(&sc, mMainDisplay);
5848 sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255});
5849 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5850
5851 // Verify color layer renders correctly on virtual display.
5852 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5853 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5854 sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 0});
5855}
5856
5857TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) {
5858 // Create a display and set its layer stack to the main display's layer stack so
5859 // the contents of the main display are mirrored on to the virtual display.
5860
5861 // Assumption here is that the new mirrored display has the same viewport as the
5862 // primary display that it is mirroring.
5863 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 0 /* layerStack */);
5864 createColorLayer(0 /* layerStack */);
5865
5866 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5867
5868 // Verify color layer renders correctly on main display and it is mirrored on the
5869 // virtual display.
5870 std::unique_ptr<ScreenCapture> sc;
5871 ScreenCapture::captureScreen(&sc, mMainDisplay);
5872 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5873 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5874
5875 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5876 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5877 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5878}
5879
Ady Abrahamdf9df4a2019-03-12 17:32:05 -07005880class DisplayActiveConfigTest : public ::testing::Test {
5881protected:
5882 void SetUp() override {
5883 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
5884 SurfaceComposerClient::getDisplayConfigs(mDisplayToken, &mDisplayconfigs);
5885 EXPECT_GT(mDisplayconfigs.size(), 0);
5886
5887 // set display power to on to make sure config can be changed
5888 SurfaceComposerClient::setDisplayPowerMode(mDisplayToken, HWC_POWER_MODE_NORMAL);
5889 }
5890
5891 sp<IBinder> mDisplayToken;
5892 Vector<DisplayInfo> mDisplayconfigs;
5893};
5894
5895TEST_F(DisplayActiveConfigTest, allConfigsAllowed) {
5896 std::vector<int32_t> allowedConfigs;
5897
5898 // Add all configs to the allowed configs
5899 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5900 allowedConfigs.push_back(i);
5901 }
5902
5903 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5904 EXPECT_EQ(res, NO_ERROR);
5905
5906 std::vector<int32_t> outConfigs;
5907 res = SurfaceComposerClient::getAllowedDisplayConfigs(mDisplayToken, &outConfigs);
5908 EXPECT_EQ(res, NO_ERROR);
5909 EXPECT_EQ(allowedConfigs, outConfigs);
5910}
5911
5912TEST_F(DisplayActiveConfigTest, changeAllowedConfig) {
5913 // we need at least 2 configs available for this test
5914 if (mDisplayconfigs.size() <= 1) return;
5915
5916 int activeConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5917
5918 // We want to set the allowed config to everything but the active config
5919 std::vector<int32_t> allowedConfigs;
5920 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5921 if (i != activeConfig) {
5922 allowedConfigs.push_back(i);
5923 }
5924 }
5925
5926 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5927 EXPECT_EQ(res, NO_ERROR);
5928
5929 // Allow some time for the config change
5930 std::this_thread::sleep_for(200ms);
5931
5932 int newActiveConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5933 EXPECT_NE(activeConfig, newActiveConfig);
5934
5935 // Make sure the new config is part of allowed config
5936 EXPECT_TRUE(std::find(allowedConfigs.begin(), allowedConfigs.end(), newActiveConfig) !=
5937 allowedConfigs.end());
5938}
5939
Vishnu Nairda9c85a2019-06-03 17:26:48 -07005940class RelativeZTest : public LayerTransactionTest {
5941protected:
5942 virtual void SetUp() {
5943 LayerTransactionTest::SetUp();
5944 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5945
5946 const auto display = SurfaceComposerClient::getInternalDisplayToken();
5947 ASSERT_FALSE(display == nullptr);
5948
5949 // Back layer
5950 mBackgroundLayer = createColorLayer("Background layer", Color::RED);
5951
5952 // Front layer
5953 mForegroundLayer = createColorLayer("Foreground layer", Color::GREEN);
5954
5955 asTransaction([&](Transaction& t) {
5956 t.setDisplayLayerStack(display, 0);
5957 t.setLayer(mBackgroundLayer, INT32_MAX - 2).show(mBackgroundLayer);
5958 t.setLayer(mForegroundLayer, INT32_MAX - 1).show(mForegroundLayer);
5959 });
5960 }
5961
5962 virtual void TearDown() {
5963 LayerTransactionTest::TearDown();
5964 mBackgroundLayer = 0;
5965 mForegroundLayer = 0;
5966 }
5967
5968 sp<SurfaceControl> mBackgroundLayer;
5969 sp<SurfaceControl> mForegroundLayer;
5970};
5971
5972// When a layer is reparented offscreen, remove relative z order if the relative parent
5973// is still onscreen so that the layer is not drawn.
5974TEST_F(RelativeZTest, LayerRemoved) {
5975 std::unique_ptr<ScreenCapture> sc;
5976
5977 // Background layer (RED)
5978 // Child layer (WHITE) (relative to foregroud layer)
5979 // Foregroud layer (GREEN)
5980 sp<SurfaceControl> childLayer =
5981 createColorLayer("Child layer", Color::BLUE, mBackgroundLayer.get());
5982
5983 Transaction{}
5984 .setRelativeLayer(childLayer, mForegroundLayer->getHandle(), 1)
5985 .show(childLayer)
5986 .apply();
5987
5988 {
5989 // The childLayer should be in front of the FG control.
5990 ScreenCapture::captureScreen(&sc);
5991 sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b);
5992 }
5993
5994 // Background layer (RED)
5995 // Foregroud layer (GREEN)
5996 Transaction{}.reparent(childLayer, nullptr).apply();
5997
5998 // Background layer (RED)
5999 // Child layer (WHITE)
6000 // Foregroud layer (GREEN)
6001 Transaction{}.reparent(childLayer, mBackgroundLayer->getHandle()).apply();
6002
6003 {
6004 // The relative z info for child layer should be reset, leaving FG control on top.
6005 ScreenCapture::captureScreen(&sc);
6006 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
6007 }
6008}
6009
6010// When a layer is reparented offscreen, preseve relative z order if the relative parent
6011// is also offscreen. Regression test b/132613412
6012TEST_F(RelativeZTest, LayerRemovedOffscreenRelativeParent) {
6013 std::unique_ptr<ScreenCapture> sc;
6014
6015 // Background layer (RED)
6016 // Foregroud layer (GREEN)
6017 // child level 1 (WHITE)
6018 // child level 2a (BLUE)
6019 // child level 3 (GREEN) (relative to child level 2b)
6020 // child level 2b (BLACK)
6021 sp<SurfaceControl> childLevel1 =
6022 createColorLayer("child level 1", Color::WHITE, mForegroundLayer.get());
6023 sp<SurfaceControl> childLevel2a =
6024 createColorLayer("child level 2a", Color::BLUE, childLevel1.get());
6025 sp<SurfaceControl> childLevel2b =
6026 createColorLayer("child level 2b", Color::BLACK, childLevel1.get());
6027 sp<SurfaceControl> childLevel3 =
6028 createColorLayer("child level 3", Color::GREEN, childLevel2a.get());
6029
6030 Transaction{}
6031 .setRelativeLayer(childLevel3, childLevel2b->getHandle(), 1)
6032 .show(childLevel2a)
6033 .show(childLevel2b)
6034 .show(childLevel3)
6035 .apply();
6036
6037 {
6038 // The childLevel3 should be in front of childLevel2b.
6039 ScreenCapture::captureScreen(&sc);
6040 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
6041 }
6042
6043 // Background layer (RED)
6044 // Foregroud layer (GREEN)
6045 Transaction{}.reparent(childLevel1, nullptr).apply();
6046
6047 // Background layer (RED)
6048 // Foregroud layer (GREEN)
6049 // child level 1 (WHITE)
6050 // child level 2 back (BLUE)
6051 // child level 3 (GREEN) (relative to child level 2b)
6052 // child level 2 front (BLACK)
6053 Transaction{}.reparent(childLevel1, mForegroundLayer->getHandle()).apply();
6054
6055 {
6056 // Nothing should change at this point since relative z info was preserved.
6057 ScreenCapture::captureScreen(&sc);
6058 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
6059 }
6060}
6061
Chavi Weingarten40482ff2017-11-30 01:51:40 +00006062} // namespace android