blob: b95c0ec135f750e90371e9b2b3f44e371e4f1f44 [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu718daf82017-10-20 11:57:17 -070017#include <algorithm>
Marissa Wallfda30bb2018-10-12 11:34:28 -070018#include <chrono>
19#include <cinttypes>
Chia-I Wu718daf82017-10-20 11:57:17 -070020#include <functional>
21#include <limits>
22#include <ostream>
Marissa Wallfda30bb2018-10-12 11:34:28 -070023#include <thread>
Chia-I Wu718daf82017-10-20 11:57:17 -070024
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070025#include <gtest/gtest.h>
26
Michael Lentine5a16a622015-05-21 13:48:24 -070027#include <android/native_window.h>
28
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070030#include <gui/LayerState.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080031#include <gui/Surface.h>
32#include <gui/SurfaceComposerClient.h>
33#include <private/gui/ComposerService.h>
34
Ady Abraham2a6ab2a2018-10-26 14:25:30 -070035#include <ui/ColorSpace.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070037#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070038#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070039
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070040#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070041#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070042
Marissa Wall713b63f2018-10-17 15:42:43 -070043#include "BufferGenerator.h"
44
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070045namespace android {
46
Chia-I Wu718daf82017-10-20 11:57:17 -070047namespace {
48
49struct Color {
50 uint8_t r;
51 uint8_t g;
52 uint8_t b;
53 uint8_t a;
54
55 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070056 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070057 static const Color BLUE;
Chia-I Wu93853fe2017-11-02 08:30:27 -070058 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070059 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070060 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070061};
62
63const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070064const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070065const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070066const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070067const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070068const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070069
Marissa Wall61c58622018-07-18 10:12:20 -070070using android::hardware::graphics::common::V1_1::BufferUsage;
Marissa Wallfda30bb2018-10-12 11:34:28 -070071using namespace std::chrono_literals;
Marissa Wall61c58622018-07-18 10:12:20 -070072
Chia-I Wu718daf82017-10-20 11:57:17 -070073std::ostream& operator<<(std::ostream& os, const Color& color) {
74 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
75 return os;
76}
77
78// Fill a region with the specified color.
Marissa Wall61c58622018-07-18 10:12:20 -070079void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
80 const Color& color) {
81 Rect r(0, 0, buffer.width, buffer.height);
82 if (!r.intersect(rect, &r)) {
83 return;
Chia-I Wu718daf82017-10-20 11:57:17 -070084 }
85
Marissa Wall61c58622018-07-18 10:12:20 -070086 int32_t width = r.right - r.left;
87 int32_t height = r.bottom - r.top;
88
89 for (int32_t row = 0; row < height; row++) {
90 uint8_t* dst =
91 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
92 for (int32_t column = 0; column < width; column++) {
Chia-I Wu718daf82017-10-20 11:57:17 -070093 dst[0] = color.r;
94 dst[1] = color.g;
95 dst[2] = color.b;
96 dst[3] = color.a;
97 dst += 4;
98 }
99 }
100}
101
Marissa Wall61c58622018-07-18 10:12:20 -0700102// Fill a region with the specified color.
103void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
104 Rect r(0, 0, buffer->width, buffer->height);
105 if (!r.intersect(rect, &r)) {
106 return;
107 }
108
109 int32_t width = r.right - r.left;
110 int32_t height = r.bottom - r.top;
111
112 uint8_t* pixels;
113 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
114 reinterpret_cast<void**>(&pixels));
115
116 for (int32_t row = 0; row < height; row++) {
117 uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
118 for (int32_t column = 0; column < width; column++) {
119 dst[0] = color.r;
120 dst[1] = color.g;
121 dst[2] = color.b;
122 dst[3] = color.a;
123 dst += 4;
124 }
125 }
126 buffer->unlock();
127}
128
Chia-I Wu718daf82017-10-20 11:57:17 -0700129// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000130void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700131 const Color& color, uint8_t tolerance) {
132 int32_t x = rect.left;
133 int32_t y = rect.top;
134 int32_t width = rect.right - rect.left;
135 int32_t height = rect.bottom - rect.top;
136
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000137 int32_t bufferWidth = int32_t(outBuffer->getWidth());
138 int32_t bufferHeight = int32_t(outBuffer->getHeight());
139 if (x + width > bufferWidth) {
140 x = std::min(x, bufferWidth);
141 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700142 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000143 if (y + height > bufferHeight) {
144 y = std::min(y, bufferHeight);
145 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700146 }
147
148 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
149 uint8_t tmp = a >= b ? a - b : b - a;
150 return tmp <= tolerance;
151 };
152 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000153 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700154 for (int32_t i = 0; i < width; i++) {
155 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
156 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
157 << "pixel @ (" << x + i << ", " << y + j << "): "
158 << "expected (" << color << "), "
159 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
160 src += 4;
161 }
162 }
163}
164
165} // anonymous namespace
166
Robert Carr4cdc58f2017-08-23 14:22:20 -0700167using Transaction = SurfaceComposerClient::Transaction;
168
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700169// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700170static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
171 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800172 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700173 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800174 ASSERT_TRUE(s != nullptr);
175 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800176 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700177 for (int y = 0; y < outBuffer.height; y++) {
178 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700179 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700180 pixel[0] = r;
181 pixel[1] = g;
182 pixel[2] = b;
183 pixel[3] = 255;
184 }
185 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700186 if (unlock) {
187 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
188 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700189}
190
191// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
192// individual pixel values for testing purposes.
193class ScreenCapture : public RefBase {
194public:
chaviw0e3479f2018-09-10 16:49:30 -0700195 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700196 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700197 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700198 SurfaceComposerClient::Transaction().apply(true);
199
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000200 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700201 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -0700202 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, false));
203 *sc = std::make_unique<ScreenCapture>(outBuffer);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000204 }
205
206 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
207 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
208 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
209 SurfaceComposerClient::Transaction().apply(true);
210
211 sp<GraphicBuffer> outBuffer;
212 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
213 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700214 }
215
Robert Carr578038f2018-03-09 12:25:24 -0800216 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
217 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
218 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
219 SurfaceComposerClient::Transaction().apply(true);
220
221 sp<GraphicBuffer> outBuffer;
222 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
223 *sc = std::make_unique<ScreenCapture>(outBuffer);
224 }
225
Chia-I Wu718daf82017-10-20 11:57:17 -0700226 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000227 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
228 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700229 }
230
231 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000232 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700233 const bool leftBorder = rect.left > 0;
234 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000235 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
236 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700237
238 if (topBorder) {
239 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
240 if (leftBorder) {
241 top.left -= 1;
242 }
243 if (rightBorder) {
244 top.right += 1;
245 }
246 expectColor(top, color, tolerance);
247 }
248 if (leftBorder) {
249 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
250 expectColor(left, color, tolerance);
251 }
252 if (rightBorder) {
253 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
254 expectColor(right, color, tolerance);
255 }
256 if (bottomBorder) {
257 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
258 if (leftBorder) {
259 bottom.left -= 1;
260 }
261 if (rightBorder) {
262 bottom.right += 1;
263 }
264 expectColor(bottom, color, tolerance);
265 }
266 }
267
Chia-I Wu93853fe2017-11-02 08:30:27 -0700268 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
269 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
270 uint8_t tolerance = 0) {
271 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
272
273 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
274 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
275 // avoid checking borders due to unspecified filtering behavior
276 const int32_t offsetX = filtered ? 2 : 0;
277 const int32_t offsetY = filtered ? 2 : 0;
278 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
279 tolerance);
280 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
281 tolerance);
282 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
283 tolerance);
284 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
285 bottomRight, tolerance);
286 }
287
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700288 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000289 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
290 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700291 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
292 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700293 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
294 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700295 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700296 }
297 }
298
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700299 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700300
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700301 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700302
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700303 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700304
Chih-Hung Hsieh22749042018-12-20 15:50:39 -0800305 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000306 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700307 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700308
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000309 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700310
311private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000312 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800313 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700314};
315
Chia-I Wu718daf82017-10-20 11:57:17 -0700316class LayerTransactionTest : public ::testing::Test {
317protected:
318 void SetUp() override {
319 mClient = new SurfaceComposerClient;
320 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
321
322 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700323
324 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
325 sp<IBinder> binder = sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
Ady Abraham37965d42018-11-01 13:43:32 -0700326 ASSERT_NO_FATAL_FAILURE(sf->getColorManagement(&mColorManagementUsed));
Chia-I Wu718daf82017-10-20 11:57:17 -0700327 }
328
chaviw0e3479f2018-09-10 16:49:30 -0700329 virtual void TearDown() {
330 mBlackBgSurface = 0;
331 mClient->dispose();
332 mClient = 0;
333 }
334
Marissa Wallfda30bb2018-10-12 11:34:28 -0700335 virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
336 const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -0800337 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
338 auto layer =
339 createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags, parent);
Chia-I Wu718daf82017-10-20 11:57:17 -0700340
Vishnu Nair60356342018-11-13 13:00:45 -0800341 Transaction t;
342 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
Vishnu Nair60356342018-11-13 13:00:45 -0800343
344 status_t error = t.apply();
Chia-I Wu718daf82017-10-20 11:57:17 -0700345 if (error != NO_ERROR) {
346 ADD_FAILURE() << "failed to initialize SurfaceControl";
347 layer.clear();
348 }
349
350 return layer;
351 }
352
Vishnu Nair88a11f22018-11-28 18:30:57 -0800353 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
354 const char* name, uint32_t width, uint32_t height,
355 PixelFormat format, uint32_t flags,
356 SurfaceControl* parent = nullptr) {
357 auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
358 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
359 return layer;
360 }
361
Marissa Wallfda30bb2018-10-12 11:34:28 -0700362 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -0800363 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
364 return createLayer(mClient, name, width, height, flags, parent);
Marissa Wallfda30bb2018-10-12 11:34:28 -0700365 }
366
Marissa Wall61c58622018-07-18 10:12:20 -0700367 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700368 // wait for previous transactions (such as setSize) to complete
369 Transaction().apply(true);
370
371 ANativeWindow_Buffer buffer = {};
372 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
373
374 return buffer;
375 }
376
Marissa Wall61c58622018-07-18 10:12:20 -0700377 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700378 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
379
380 // wait for the newly posted buffer to be latched
381 waitForLayerBuffers();
382 }
383
Marissa Wall61c58622018-07-18 10:12:20 -0700384 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
385 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700386 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700387 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
388 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
389 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700390 }
391
Marissa Wall61c58622018-07-18 10:12:20 -0700392 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
393 int32_t bufferWidth, int32_t bufferHeight) {
394 sp<GraphicBuffer> buffer =
395 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
396 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
397 BufferUsage::COMPOSER_OVERLAY,
398 "test");
399 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
Marissa Wall861616d2018-10-22 12:52:23 -0700400 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -0700401 }
402
403 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
404 int32_t bufferWidth, int32_t bufferHeight) {
405 switch (mLayerType) {
406 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
407 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
408 break;
409 case ISurfaceComposerClient::eFXSurfaceBufferState:
410 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
411 break;
412 default:
413 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
414 }
415 }
416
417 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
418 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700419 const Color& topRight, const Color& bottomLeft,
420 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700421 switch (mLayerType) {
422 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
423 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
424 bottomLeft, bottomRight);
425 break;
426 case ISurfaceComposerClient::eFXSurfaceBufferState:
427 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
428 bottomLeft, bottomRight);
429 break;
430 default:
431 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
432 }
433 }
434
435 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
436 int32_t bufferHeight, const Color& topLeft,
437 const Color& topRight, const Color& bottomLeft,
438 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700439 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700440 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
441 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700442
Marissa Wall61c58622018-07-18 10:12:20 -0700443 const int32_t halfW = bufferWidth / 2;
444 const int32_t halfH = bufferHeight / 2;
445 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
446 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
447 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
448 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
449 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700450
Marissa Wall61c58622018-07-18 10:12:20 -0700451 postBufferQueueLayerBuffer(layer);
452 }
453
454 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
455 int32_t bufferHeight, const Color& topLeft,
456 const Color& topRight, const Color& bottomLeft,
457 const Color& bottomRight) {
458 sp<GraphicBuffer> buffer =
459 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
460 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
461 BufferUsage::COMPOSER_OVERLAY,
462 "test");
463
464 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
465
466 const int32_t halfW = bufferWidth / 2;
467 const int32_t halfH = bufferHeight / 2;
468 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
469 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
470 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
471 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
472
473 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700474 }
475
chaviw0e3479f2018-09-10 16:49:30 -0700476 std::unique_ptr<ScreenCapture> screenshot() {
477 std::unique_ptr<ScreenCapture> screenshot;
478 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700479 return screenshot;
480 }
481
Marissa Wall713b63f2018-10-17 15:42:43 -0700482 static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
483 static BufferGenerator bufferGenerator;
484 return bufferGenerator.get(outBuffer, outFence);
485 }
486
Chia-I Wu718daf82017-10-20 11:57:17 -0700487 sp<SurfaceComposerClient> mClient;
488
489 sp<IBinder> mDisplay;
490 uint32_t mDisplayWidth;
491 uint32_t mDisplayHeight;
492 uint32_t mDisplayLayerStack;
Marissa Wall861616d2018-10-22 12:52:23 -0700493 Rect mDisplayRect = Rect::INVALID_RECT;
Chia-I Wu718daf82017-10-20 11:57:17 -0700494
495 // leave room for ~256 layers
496 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
497
Marissa Wall861616d2018-10-22 12:52:23 -0700498 void setRelativeZBasicHelper(uint32_t layerType);
499 void setRelativeZGroupHelper(uint32_t layerType);
500 void setAlphaBasicHelper(uint32_t layerType);
Marissa Wall61c58622018-07-18 10:12:20 -0700501
chaviw0e3479f2018-09-10 16:49:30 -0700502 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700503 bool mColorManagementUsed;
504
Chia-I Wu718daf82017-10-20 11:57:17 -0700505private:
506 void SetUpDisplay() {
507 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
508 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
509
510 // get display width/height
511 DisplayInfo info;
512 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
513 mDisplayWidth = info.w;
514 mDisplayHeight = info.h;
Marissa Wall861616d2018-10-22 12:52:23 -0700515 mDisplayRect =
516 Rect(static_cast<int32_t>(mDisplayWidth), static_cast<int32_t>(mDisplayHeight));
Chia-I Wu718daf82017-10-20 11:57:17 -0700517
518 // After a new buffer is queued, SurfaceFlinger is notified and will
519 // latch the new buffer on next vsync. Let's heuristically wait for 3
520 // vsyncs.
521 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
522
523 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700524
Vishnu Nair88a11f22018-11-28 18:30:57 -0800525 mBlackBgSurface =
526 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
527 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
chaviw0e3479f2018-09-10 16:49:30 -0700528
Chia-I Wu718daf82017-10-20 11:57:17 -0700529 // set layer stack (b/68888219)
530 Transaction t;
531 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800532 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700533 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
534 t.setColor(mBlackBgSurface, half3{0, 0, 0});
535 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700536 t.apply();
537 }
538
chaviw0e3479f2018-09-10 16:49:30 -0700539 void waitForLayerBuffers() {
540 // Request an empty transaction to get applied synchronously to ensure the buffer is
541 // latched.
542 Transaction().apply(true);
543 usleep(mBufferPostDelay);
544 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700545
546 int32_t mBufferPostDelay;
547};
548
Marissa Wall61c58622018-07-18 10:12:20 -0700549class LayerTypeTransactionTest : public LayerTransactionTest,
550 public ::testing::WithParamInterface<uint32_t> {
551public:
552 LayerTypeTransactionTest() { mLayerType = GetParam(); }
553
554 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -0800555 uint32_t flags = 0, SurfaceControl* parent = nullptr) override {
Marissa Wall61c58622018-07-18 10:12:20 -0700556 // if the flags already have a layer type specified, return an error
557 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
558 return nullptr;
559 }
chaviwf66724d2018-11-28 16:35:21 -0800560 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent);
Marissa Wall61c58622018-07-18 10:12:20 -0700561 }
562
563 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
564 int32_t bufferHeight) {
565 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
566 bufferWidth, bufferHeight));
567 }
568
569 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
570 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
571 const Color& bottomLeft, const Color& bottomRight) {
572 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
573 bufferWidth, bufferHeight,
574 topLeft, topRight,
575 bottomLeft, bottomRight));
576 }
577
578protected:
579 uint32_t mLayerType;
580};
581
582INSTANTIATE_TEST_CASE_P(
583 LayerTypeTransactionTests, LayerTypeTransactionTest,
584 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
585 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
586
Marissa Wall861616d2018-10-22 12:52:23 -0700587TEST_F(LayerTransactionTest, SetPositionBasic_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700588 sp<SurfaceControl> layer;
589 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700590 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700591
592 {
593 SCOPED_TRACE("default position");
Marissa Wall861616d2018-10-22 12:52:23 -0700594 const Rect rect(0, 0, 32, 32);
Chia-I Wu718daf82017-10-20 11:57:17 -0700595 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700596 shot->expectColor(rect, Color::RED);
597 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700598 }
599
600 Transaction().setPosition(layer, 5, 10).apply();
601 {
602 SCOPED_TRACE("new position");
Marissa Wall861616d2018-10-22 12:52:23 -0700603 const Rect rect(5, 10, 37, 42);
Chia-I Wu718daf82017-10-20 11:57:17 -0700604 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700605 shot->expectColor(rect, Color::RED);
606 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700607 }
608}
609
Marissa Wall861616d2018-10-22 12:52:23 -0700610TEST_F(LayerTransactionTest, SetPositionRounding_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700611 sp<SurfaceControl> layer;
612 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700613 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700614
615 // GLES requires only 4 bits of subpixel precision during rasterization
616 // XXX GLES composition does not match HWC composition due to precision
617 // loss (b/69315223)
618 const float epsilon = 1.0f / 16.0f;
619 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
620 {
621 SCOPED_TRACE("rounding down");
622 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
623 }
624
625 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
626 {
627 SCOPED_TRACE("rounding up");
628 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
629 }
630}
631
Marissa Wall861616d2018-10-22 12:52:23 -0700632TEST_F(LayerTransactionTest, SetPositionOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700633 sp<SurfaceControl> layer;
634 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700635 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700636
637 Transaction().setPosition(layer, -32, -32).apply();
638 {
639 SCOPED_TRACE("negative coordinates");
Marissa Wall861616d2018-10-22 12:52:23 -0700640 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700641 }
642
643 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
644 {
645 SCOPED_TRACE("positive coordinates");
Marissa Wall861616d2018-10-22 12:52:23 -0700646 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700647 }
648}
649
Marissa Wall861616d2018-10-22 12:52:23 -0700650TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700651 sp<SurfaceControl> layer;
652 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700653 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700654
655 // partially out of bounds
656 Transaction().setPosition(layer, -30, -30).apply();
657 {
658 SCOPED_TRACE("negative coordinates");
659 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
660 }
661
662 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
663 {
664 SCOPED_TRACE("positive coordinates");
665 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
666 mDisplayHeight),
667 Color::RED);
668 }
669}
670
Marissa Wall861616d2018-10-22 12:52:23 -0700671TEST_F(LayerTransactionTest, SetPositionWithResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700672 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700673 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
674 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700675
676 // setPosition is applied immediately by default, with or without resize
677 // pending
678 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
679 {
680 SCOPED_TRACE("resize pending");
681 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700682 const Rect rect(5, 10, 37, 42);
Marissa Wall61c58622018-07-18 10:12:20 -0700683 shot->expectColor(rect, Color::RED);
684 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700685 }
686
Marissa Wall861616d2018-10-22 12:52:23 -0700687 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700688 {
689 SCOPED_TRACE("resize applied");
690 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
691 }
692}
693
Marissa Wall61c58622018-07-18 10:12:20 -0700694TEST_F(LayerTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700695 sp<SurfaceControl> layer;
696 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700697 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700698
699 // request setPosition to be applied with the next resize
700 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
701 {
702 SCOPED_TRACE("new position pending");
703 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
704 }
705
706 Transaction().setPosition(layer, 15, 20).apply();
707 {
708 SCOPED_TRACE("pending new position modified");
709 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
710 }
711
712 Transaction().setSize(layer, 64, 64).apply();
713 {
714 SCOPED_TRACE("resize pending");
715 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
716 }
717
718 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700719 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700720 {
721 SCOPED_TRACE("new position applied");
722 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
723 }
724}
725
Marissa Wall61c58622018-07-18 10:12:20 -0700726TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700727 sp<SurfaceControl> layer;
728 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700729 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700730
731 // setPosition is not immediate even with SCALE_TO_WINDOW override
732 Transaction()
733 .setPosition(layer, 5, 10)
734 .setSize(layer, 64, 64)
735 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
736 .setGeometryAppliesWithResize(layer)
737 .apply();
738 {
739 SCOPED_TRACE("new position pending");
740 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
741 }
742
Marissa Wall61c58622018-07-18 10:12:20 -0700743 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700744 {
745 SCOPED_TRACE("new position applied");
746 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
747 }
748}
749
Marissa Wall861616d2018-10-22 12:52:23 -0700750TEST_F(LayerTransactionTest, SetSizeBasic_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700751 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700752 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
753 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700754
755 Transaction().setSize(layer, 64, 64).apply();
756 {
757 SCOPED_TRACE("resize pending");
758 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700759 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -0700760 shot->expectColor(rect, Color::RED);
761 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700762 }
763
Marissa Wall861616d2018-10-22 12:52:23 -0700764 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700765 {
766 SCOPED_TRACE("resize applied");
767 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -0700768 const Rect rect(0, 0, 64, 64);
769 shot->expectColor(rect, Color::RED);
770 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700771 }
772}
773
Marissa Wall61c58622018-07-18 10:12:20 -0700774TEST_P(LayerTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700775 // cannot test robustness against invalid sizes (zero or really huge)
776}
777
Marissa Wall861616d2018-10-22 12:52:23 -0700778TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700779 sp<SurfaceControl> layer;
780 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700781 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700782
783 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
784 Transaction()
785 .setSize(layer, 64, 64)
786 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
787 .apply();
788 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
789}
790
Marissa Wall61c58622018-07-18 10:12:20 -0700791TEST_P(LayerTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700792 sp<SurfaceControl> layerR;
793 sp<SurfaceControl> layerG;
794 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700795 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700796 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700797 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700798
799 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
800 {
801 SCOPED_TRACE("layerR");
802 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
803 }
804
805 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
806 {
807 SCOPED_TRACE("layerG");
808 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
809 }
810}
811
Marissa Wall61c58622018-07-18 10:12:20 -0700812TEST_P(LayerTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700813 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800814 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700815 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800816 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700817 sp<SurfaceControl> layerR;
818 sp<SurfaceControl> layerG;
819 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700820 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700821 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700822 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700823
chaviw0e3479f2018-09-10 16:49:30 -0700824 Transaction()
825 .reparent(layerR, parent->getHandle())
826 .reparent(layerG, parent->getHandle())
827 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700828 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
829 {
830 SCOPED_TRACE("layerR");
chaviw0e3479f2018-09-10 16:49:30 -0700831 auto shot = screenshot();
832 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700833 }
834
835 Transaction().setLayer(layerR, -3).apply();
836 {
837 SCOPED_TRACE("layerG");
chaviw0e3479f2018-09-10 16:49:30 -0700838 auto shot = screenshot();
839 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700840 }
841}
842
Marissa Wall861616d2018-10-22 12:52:23 -0700843void LayerTransactionTest::setRelativeZBasicHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700844 sp<SurfaceControl> layerR;
845 sp<SurfaceControl> layerG;
Marissa Wall861616d2018-10-22 12:52:23 -0700846 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32, layerType));
847 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
848 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32, layerType));
849 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700850
Marissa Wall861616d2018-10-22 12:52:23 -0700851 switch (layerType) {
852 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
853 Transaction()
854 .setPosition(layerG, 16, 16)
855 .setRelativeLayer(layerG, layerR->getHandle(), 1)
856 .apply();
857 break;
858 case ISurfaceComposerClient::eFXSurfaceBufferState:
859 Transaction()
860 .setFrame(layerR, Rect(0, 0, 32, 32))
861 .setFrame(layerG, Rect(16, 16, 48, 48))
862 .setRelativeLayer(layerG, layerR->getHandle(), 1)
863 .apply();
864 break;
865 default:
866 ASSERT_FALSE(true) << "Unsupported layer type";
867 }
Chia-I Wu49313302017-10-31 10:14:40 -0700868 {
869 SCOPED_TRACE("layerG above");
870 auto shot = screenshot();
871 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
872 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
873 }
874
875 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
876 {
877 SCOPED_TRACE("layerG below");
878 auto shot = screenshot();
879 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
880 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
881 }
882}
883
Marissa Wall861616d2018-10-22 12:52:23 -0700884TEST_F(LayerTransactionTest, SetRelativeZBasic_BufferQueue) {
885 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
886}
887
888TEST_F(LayerTransactionTest, SetRelativeZBasic_BufferState) {
889 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
890}
891
Marissa Wall61c58622018-07-18 10:12:20 -0700892TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700893 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800894 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700895 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800896 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wuec2d9852017-11-21 09:21:01 -0800897 sp<SurfaceControl> layerR;
898 sp<SurfaceControl> layerG;
899 sp<SurfaceControl> layerB;
900 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700901 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800902 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700903 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800904 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700905 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800906
chaviw0e3479f2018-09-10 16:49:30 -0700907 Transaction()
908 .reparent(layerB, parent->getHandle())
909 .apply();
910
Chia-I Wuec2d9852017-11-21 09:21:01 -0800911 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
912 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
913
chaviw0e3479f2018-09-10 16:49:30 -0700914 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800915 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700916 sp<IBinder> parentHandle = parent->getHandle();
Marissa Wall861616d2018-10-22 12:52:23 -0700917 ScreenCapture::captureLayers(&screenshot, parentHandle, Rect(0, 0, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800918 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
919}
920
Marissa Wall861616d2018-10-22 12:52:23 -0700921void LayerTransactionTest::setRelativeZGroupHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700922 sp<SurfaceControl> layerR;
923 sp<SurfaceControl> layerG;
924 sp<SurfaceControl> layerB;
Marissa Wall861616d2018-10-22 12:52:23 -0700925 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test", 32, 32, layerType));
926 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
927 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test", 32, 32, layerType));
928 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
929 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test", 32, 32, layerType));
930 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700931
932 // layerR = 0, layerG = layerR + 3, layerB = 2
Marissa Wall861616d2018-10-22 12:52:23 -0700933 switch (layerType) {
934 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
935 Transaction()
936 .setPosition(layerG, 8, 8)
937 .setRelativeLayer(layerG, layerR->getHandle(), 3)
938 .setPosition(layerB, 16, 16)
939 .setLayer(layerB, mLayerZBase + 2)
940 .apply();
941 break;
942 case ISurfaceComposerClient::eFXSurfaceBufferState:
943 Transaction()
944 .setFrame(layerR, Rect(0, 0, 32, 32))
945 .setFrame(layerG, Rect(8, 8, 40, 40))
946 .setRelativeLayer(layerG, layerR->getHandle(), 3)
947 .setFrame(layerB, Rect(16, 16, 48, 48))
948 .setLayer(layerB, mLayerZBase + 2)
949 .apply();
950 break;
951 default:
952 ASSERT_FALSE(true) << "Unsupported layer type";
953 }
954
Chia-I Wu49313302017-10-31 10:14:40 -0700955 {
956 SCOPED_TRACE("(layerR < layerG) < layerB");
957 auto shot = screenshot();
958 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
959 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
960 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
961 }
962
963 // layerR = 4, layerG = layerR + 3, layerB = 2
964 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
965 {
966 SCOPED_TRACE("layerB < (layerR < layerG)");
967 auto shot = screenshot();
968 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
969 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
970 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
971 }
972
973 // layerR = 4, layerG = layerR - 3, layerB = 2
974 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
975 {
976 SCOPED_TRACE("layerB < (layerG < layerR)");
977 auto shot = screenshot();
978 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
979 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
980 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
981 }
982
983 // restore to absolute z
984 // layerR = 4, layerG = 0, layerB = 2
985 Transaction().setLayer(layerG, mLayerZBase).apply();
986 {
987 SCOPED_TRACE("layerG < layerB < layerR");
988 auto shot = screenshot();
989 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
990 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
991 }
992
993 // layerR should not affect layerG anymore
994 // layerR = 1, layerG = 0, layerB = 2
995 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
996 {
997 SCOPED_TRACE("layerG < layerR < layerB");
998 auto shot = screenshot();
999 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1000 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
1001 }
1002}
1003
Marissa Wall861616d2018-10-22 12:52:23 -07001004TEST_F(LayerTransactionTest, SetRelativeZGroup_BufferQueue) {
1005 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1006}
1007
1008TEST_F(LayerTransactionTest, SetRelativeZGroup_BufferState) {
1009 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1010}
1011
Marissa Wall61c58622018-07-18 10:12:20 -07001012TEST_P(LayerTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -07001013 sp<SurfaceControl> layerR;
1014 sp<SurfaceControl> layerG;
1015
1016 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001017 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001018 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001019 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -07001020
1021 Transaction()
1022 .setPosition(layerG, 16, 16)
1023 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1024 .apply();
1025
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001026 layerG->clear();
Chia-I Wu49313302017-10-31 10:14:40 -07001027 // layerG should have been removed
1028 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1029}
1030
Marissa Wall61c58622018-07-18 10:12:20 -07001031TEST_P(LayerTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001032 sp<SurfaceControl> layer;
1033 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001034 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001035
1036 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
1037 {
1038 SCOPED_TRACE("layer hidden");
Marissa Wall861616d2018-10-22 12:52:23 -07001039 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu57b27502017-10-31 10:14:40 -07001040 }
1041
1042 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1043 {
1044 SCOPED_TRACE("layer shown");
1045 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1046 }
1047}
1048
Marissa Wall61c58622018-07-18 10:12:20 -07001049TEST_P(LayerTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001050 const Color translucentRed = {100, 0, 0, 100};
1051 sp<SurfaceControl> layerR;
1052 sp<SurfaceControl> layerG;
1053 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001054 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001055 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001056 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001057
1058 Transaction()
1059 .setLayer(layerR, mLayerZBase + 1)
1060 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1061 .apply();
1062 {
1063 SCOPED_TRACE("layerR opaque");
1064 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
1065 }
1066
1067 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1068 {
1069 SCOPED_TRACE("layerR translucent");
1070 const uint8_t g = uint8_t(255 - translucentRed.a);
1071 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
1072 }
1073}
1074
Marissa Wall61c58622018-07-18 10:12:20 -07001075TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001076 sp<SurfaceControl> layer;
1077 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001078 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001079
1080 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001081 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001082 Transaction()
1083 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1084 .apply(true);
1085 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001086 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001087
1088 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1089 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001090 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001091}
1092
Marissa Wall61c58622018-07-18 10:12:20 -07001093TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001094 const Rect top(0, 0, 32, 16);
1095 const Rect bottom(0, 16, 32, 32);
1096 sp<SurfaceControl> layer;
1097 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1098
1099 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001100 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1101 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1102 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001103 // setTransparentRegionHint always applies to the following buffer
1104 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001105 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001106 {
1107 SCOPED_TRACE("top transparent");
1108 auto shot = screenshot();
1109 shot->expectColor(top, Color::BLACK);
1110 shot->expectColor(bottom, Color::RED);
1111 }
1112
1113 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1114 {
1115 SCOPED_TRACE("transparent region hint pending");
1116 auto shot = screenshot();
1117 shot->expectColor(top, Color::BLACK);
1118 shot->expectColor(bottom, Color::RED);
1119 }
1120
Marissa Wall61c58622018-07-18 10:12:20 -07001121 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1122 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1123 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1124 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001125 {
1126 SCOPED_TRACE("bottom transparent");
1127 auto shot = screenshot();
1128 shot->expectColor(top, Color::RED);
1129 shot->expectColor(bottom, Color::BLACK);
1130 }
1131}
1132
Marissa Wall61c58622018-07-18 10:12:20 -07001133TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1134 const Rect top(0, 0, 32, 16);
1135 const Rect bottom(0, 16, 32, 32);
1136 sp<SurfaceControl> layer;
1137 ASSERT_NO_FATAL_FAILURE(
1138 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1139
1140 sp<GraphicBuffer> buffer =
1141 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1142 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1143 BufferUsage::COMPOSER_OVERLAY,
1144 "test");
1145
1146 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1147 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1148 Transaction()
1149 .setTransparentRegionHint(layer, Region(top))
1150 .setBuffer(layer, buffer)
Marissa Wall861616d2018-10-22 12:52:23 -07001151 .setFrame(layer, Rect(0, 0, 32, 32))
Marissa Wall61c58622018-07-18 10:12:20 -07001152 .apply();
1153 {
1154 SCOPED_TRACE("top transparent");
1155 auto shot = screenshot();
1156 shot->expectColor(top, Color::BLACK);
1157 shot->expectColor(bottom, Color::RED);
1158 }
1159
1160 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1161 {
1162 SCOPED_TRACE("transparent region hint intermediate");
1163 auto shot = screenshot();
1164 shot->expectColor(top, Color::BLACK);
1165 shot->expectColor(bottom, Color::BLACK);
1166 }
1167
1168 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1169 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1170 BufferUsage::COMPOSER_OVERLAY,
1171 "test");
1172
1173 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1174 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
Marissa Wall861616d2018-10-22 12:52:23 -07001175 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001176 {
1177 SCOPED_TRACE("bottom transparent");
1178 auto shot = screenshot();
1179 shot->expectColor(top, Color::RED);
1180 shot->expectColor(bottom, Color::BLACK);
1181 }
1182}
1183
Marissa Wall861616d2018-10-22 12:52:23 -07001184TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001185 sp<SurfaceControl> layerTransparent;
1186 sp<SurfaceControl> layerR;
1187 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1188 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1189
1190 // check that transparent region hint is bound by the layer size
1191 Transaction()
Marissa Wall861616d2018-10-22 12:52:23 -07001192 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001193 .setPosition(layerR, 16, 16)
1194 .setLayer(layerR, mLayerZBase + 1)
1195 .apply();
Marissa Wall861616d2018-10-22 12:52:23 -07001196 ASSERT_NO_FATAL_FAILURE(
1197 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1198 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001199 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1200}
1201
Marissa Wall861616d2018-10-22 12:52:23 -07001202TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds_BufferState) {
1203 sp<SurfaceControl> layerTransparent;
1204 sp<SurfaceControl> layerR;
1205 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1206 ASSERT_NO_FATAL_FAILURE(
1207 layerR = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1208
1209 // check that transparent region hint is bound by the layer size
1210 Transaction()
1211 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
1212 .setFrame(layerR, Rect(16, 16, 48, 48))
1213 .setLayer(layerR, mLayerZBase + 1)
1214 .apply();
1215 ASSERT_NO_FATAL_FAILURE(
1216 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1217 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layerR, Color::RED, 32, 32));
1218 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1219}
1220
1221void LayerTransactionTest::setAlphaBasicHelper(uint32_t layerType) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001222 sp<SurfaceControl> layer1;
1223 sp<SurfaceControl> layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07001224 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32, layerType));
1225 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32, layerType));
1226 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer1, {64, 0, 0, 255}, 32, 32));
1227 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001228
Marissa Wall861616d2018-10-22 12:52:23 -07001229 switch (layerType) {
1230 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1231 Transaction()
1232 .setAlpha(layer1, 0.25f)
1233 .setAlpha(layer2, 0.75f)
1234 .setPosition(layer2, 16, 0)
1235 .setLayer(layer2, mLayerZBase + 1)
1236 .apply();
1237 break;
1238 case ISurfaceComposerClient::eFXSurfaceBufferState:
1239 Transaction()
1240 .setAlpha(layer1, 0.25f)
1241 .setAlpha(layer2, 0.75f)
1242 .setFrame(layer1, Rect(0, 0, 32, 32))
1243 .setFrame(layer2, Rect(16, 0, 48, 32))
1244 .setLayer(layer2, mLayerZBase + 1)
1245 .apply();
1246 break;
1247 default:
1248 ASSERT_FALSE(true) << "Unsupported layer type";
1249 }
Chia-I Wua8a515e2017-11-01 15:16:35 -07001250 {
1251 auto shot = screenshot();
1252 uint8_t r = 16; // 64 * 0.25f
1253 uint8_t g = 48; // 64 * 0.75f
1254 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1255 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1256
1257 r /= 4; // r * (1.0f - 0.75f)
1258 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1259 }
1260}
1261
Marissa Wall861616d2018-10-22 12:52:23 -07001262TEST_F(LayerTransactionTest, SetAlphaBasic_BufferQueue) {
1263 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1264}
1265
1266TEST_F(LayerTransactionTest, SetAlphaBasic_BufferState) {
1267 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1268}
1269
Marissa Wall61c58622018-07-18 10:12:20 -07001270TEST_P(LayerTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001271 const Color color = {64, 0, 0, 255};
1272 sp<SurfaceControl> layer;
1273 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001274 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001275
1276 Transaction().setAlpha(layer, 2.0f).apply();
1277 {
1278 SCOPED_TRACE("clamped to 1.0f");
1279 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1280 }
1281
1282 Transaction().setAlpha(layer, -1.0f).apply();
1283 {
1284 SCOPED_TRACE("clamped to 0.0f");
1285 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1286 }
1287}
1288
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001289TEST_P(LayerTypeTransactionTest, SetCornerRadius) {
1290 sp<SurfaceControl> layer;
1291 const uint8_t size = 64;
1292 const uint8_t testArea = 4;
Lucas Dupina1d0e312018-12-04 22:30:27 -08001293 const float cornerRadius = 20.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001294 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1295 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1296
1297 Transaction()
1298 .setCornerRadius(layer, cornerRadius)
1299 .apply();
1300 {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001301 const uint8_t bottom = size - 1;
1302 const uint8_t right = size - 1;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001303 auto shot = screenshot();
1304 // Transparent corners
1305 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
Lucas Dupina1d0e312018-12-04 22:30:27 -08001306 shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
1307 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1308 shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
1309 }
1310}
1311
1312TEST_P(LayerTypeTransactionTest, SetCornerRadiusChildCrop) {
1313 sp<SurfaceControl> parent;
1314 sp<SurfaceControl> child;
1315 const uint8_t size = 64;
1316 const uint8_t testArea = 4;
1317 const float cornerRadius = 20.0f;
1318 ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
1319 ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
1320 ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
1321 ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
1322
1323 Transaction()
1324 .setCornerRadius(parent, cornerRadius)
1325 .reparent(child, parent->getHandle())
1326 .setPosition(child, 0, size / 2)
1327 .apply();
1328 {
1329 const uint8_t bottom = size - 1;
1330 const uint8_t right = size - 1;
1331 auto shot = screenshot();
1332 // Top edge of child should not have rounded corners because it's translated in the parent
1333 shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
1334 Color::GREEN);
1335 // But bottom edges should have been clipped according to parent bounds
1336 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1337 shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001338 }
1339}
1340
Chia-I Wue4ef6102017-11-01 15:16:35 -07001341TEST_F(LayerTransactionTest, SetColorBasic) {
1342 sp<SurfaceControl> bufferLayer;
1343 sp<SurfaceControl> colorLayer;
1344 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001345 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001346 ASSERT_NO_FATAL_FAILURE(colorLayer =
1347 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1348 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001349
Vishnu Nair88a11f22018-11-28 18:30:57 -08001350 Transaction()
1351 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1352 .setLayer(colorLayer, mLayerZBase + 1)
1353 .apply();
1354
Chia-I Wue4ef6102017-11-01 15:16:35 -07001355 {
1356 SCOPED_TRACE("default color");
1357 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1358 }
1359
1360 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1361 const Color expected = {15, 51, 85, 255};
1362 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1363 // channel) should be less than one
1364 const uint8_t tolerance = 1;
1365 Transaction().setColor(colorLayer, color).apply();
1366 {
1367 SCOPED_TRACE("new color");
1368 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1369 }
1370}
1371
1372TEST_F(LayerTransactionTest, SetColorClamped) {
1373 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001374 ASSERT_NO_FATAL_FAILURE(colorLayer =
1375 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1376 ISurfaceComposerClient::eFXSurfaceColor));
1377 Transaction()
1378 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1379 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1380 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001381
Chia-I Wue4ef6102017-11-01 15:16:35 -07001382 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1383}
1384
1385TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1386 sp<SurfaceControl> bufferLayer;
1387 sp<SurfaceControl> colorLayer;
1388 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001389 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001390 ASSERT_NO_FATAL_FAILURE(colorLayer =
1391 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1392 ISurfaceComposerClient::eFXSurfaceColor));
1393 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001394
1395 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1396 const float alpha = 0.25f;
1397 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1398 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1399 // channel) should be less than one
1400 const uint8_t tolerance = 1;
1401 Transaction()
1402 .setColor(colorLayer, color)
1403 .setAlpha(colorLayer, alpha)
1404 .setLayer(colorLayer, mLayerZBase + 1)
1405 .apply();
1406 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1407 tolerance);
1408}
1409
Adrian Roosb7a96502018-04-08 11:38:55 -07001410TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1411 sp<SurfaceControl> bufferLayer;
1412 sp<SurfaceControl> parentLayer;
1413 sp<SurfaceControl> colorLayer;
1414 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1415 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001416 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001417 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1418 0 /* buffer height */,
1419 ISurfaceComposerClient::eFXSurfaceColor));
1420 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001421 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1422 const float alpha = 0.25f;
1423 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1424 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1425 // channel) should be less than one
1426 const uint8_t tolerance = 1;
1427 Transaction()
1428 .reparent(colorLayer, parentLayer->getHandle())
1429 .setColor(colorLayer, color)
1430 .setAlpha(parentLayer, alpha)
1431 .setLayer(parentLayer, mLayerZBase + 1)
1432 .apply();
1433 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1434 tolerance);
1435}
1436
Marissa Wall61c58622018-07-18 10:12:20 -07001437TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001438 sp<SurfaceControl> bufferLayer;
1439 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001440 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001441
1442 // color is ignored
1443 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1444 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1445}
1446
Marissa Wall61c58622018-07-18 10:12:20 -07001447TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001448 sp<SurfaceControl> layer;
1449 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001450 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001451
1452 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1453 {
1454 SCOPED_TRACE("non-existing layer stack");
Marissa Wall861616d2018-10-22 12:52:23 -07001455 screenshot()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001456 }
1457
1458 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1459 {
1460 SCOPED_TRACE("original layer stack");
1461 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1462 }
1463}
1464
Marissa Wall861616d2018-10-22 12:52:23 -07001465TEST_F(LayerTransactionTest, SetMatrixBasic_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001466 sp<SurfaceControl> layer;
1467 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001468 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1469 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001470
1471 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1472 {
1473 SCOPED_TRACE("IDENTITY");
1474 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1475 Color::WHITE);
1476 }
1477
1478 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1479 {
1480 SCOPED_TRACE("FLIP_H");
1481 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1482 Color::BLUE);
1483 }
1484
1485 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1486 {
1487 SCOPED_TRACE("FLIP_V");
1488 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1489 Color::GREEN);
1490 }
1491
1492 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1493 {
1494 SCOPED_TRACE("ROT_90");
1495 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1496 Color::GREEN);
1497 }
1498
1499 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1500 {
1501 SCOPED_TRACE("SCALE");
1502 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1503 Color::WHITE, true /* filtered */);
1504 }
1505}
1506
Marissa Wall861616d2018-10-22 12:52:23 -07001507TEST_F(LayerTransactionTest, SetMatrixBasic_BufferState) {
1508 sp<SurfaceControl> layer;
1509 ASSERT_NO_FATAL_FAILURE(
1510 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1511 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1512 Color::BLUE, Color::WHITE));
1513
1514 Transaction()
1515 .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
1516 .setFrame(layer, Rect(0, 0, 32, 32))
1517 .apply();
1518 {
1519 SCOPED_TRACE("IDENTITY");
1520 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1521 Color::WHITE);
1522 }
1523
1524 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
1525 {
1526 SCOPED_TRACE("FLIP_H");
1527 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1528 Color::WHITE);
1529 }
1530
1531 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
1532 {
1533 SCOPED_TRACE("FLIP_V");
1534 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1535 Color::WHITE);
1536 }
1537
1538 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
1539 {
1540 SCOPED_TRACE("ROT_90");
1541 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1542 Color::WHITE);
1543 }
1544
1545 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
1546 {
1547 SCOPED_TRACE("SCALE");
1548 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1549 Color::WHITE);
1550 }
1551}
1552
1553TEST_F(LayerTransactionTest, SetMatrixRot45_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001554 sp<SurfaceControl> layer;
1555 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001556 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1557 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001558
1559 const float rot = M_SQRT1_2; // 45 degrees
1560 const float trans = M_SQRT2 * 16.0f;
1561 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1562
1563 auto shot = screenshot();
1564 // check a 8x8 region inside each color
1565 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1566 const int32_t halfL = 4;
1567 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1568 };
1569 const int32_t unit = int32_t(trans / 2);
1570 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1571 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1572 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1573 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1574}
1575
Marissa Wall861616d2018-10-22 12:52:23 -07001576TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001577 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -07001578 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1579 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001580
1581 // setMatrix is applied after any pending resize, unlike setPosition
1582 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1583 {
1584 SCOPED_TRACE("resize pending");
1585 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -07001586 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -07001587 shot->expectColor(rect, Color::RED);
1588 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001589 }
1590
Marissa Wall861616d2018-10-22 12:52:23 -07001591 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001592 {
1593 SCOPED_TRACE("resize applied");
Marissa Wall861616d2018-10-22 12:52:23 -07001594 const Rect rect(0, 0, 128, 128);
1595 screenshot()->expectColor(rect, Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001596 }
1597}
1598
Marissa Wall861616d2018-10-22 12:52:23 -07001599TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001600 sp<SurfaceControl> layer;
1601 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001602 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001603
1604 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1605 Transaction()
1606 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1607 .setSize(layer, 64, 64)
1608 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1609 .apply();
1610 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1611}
1612
Marissa Wall861616d2018-10-22 12:52:23 -07001613TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001614 sp<SurfaceControl> layer;
1615 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001616 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1617 Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001618
1619 // XXX SCALE_CROP is not respected; calling setSize and
1620 // setOverrideScalingMode in separate transactions does not work
1621 // (b/69315456)
1622 Transaction()
1623 .setSize(layer, 64, 16)
1624 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1625 .apply();
1626 {
1627 SCOPED_TRACE("SCALE_TO_WINDOW");
1628 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1629 Color::WHITE, true /* filtered */);
1630 }
1631}
1632
Dan Stoza000dd012018-08-01 13:31:52 -07001633TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1634 sp<SurfaceControl> layer;
1635 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1636
1637 sp<IBinder> handle = layer->getHandle();
1638 ASSERT_TRUE(handle != nullptr);
1639
1640 FrameStats frameStats;
1641 mClient->getLayerFrameStats(handle, &frameStats);
1642
1643 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1644}
1645
Marissa Wall61c58622018-07-18 10:12:20 -07001646TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001647 sp<SurfaceControl> layer;
1648 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001649 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001650 const Rect crop(8, 8, 24, 24);
1651
Marissa Wallf58c14b2018-07-24 10:50:43 -07001652 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001653 auto shot = screenshot();
1654 shot->expectColor(crop, Color::RED);
1655 shot->expectBorder(crop, Color::BLACK);
1656}
1657
Marissa Wall61c58622018-07-18 10:12:20 -07001658TEST_F(LayerTransactionTest, SetCropBasic_BufferState) {
1659 sp<SurfaceControl> layer;
1660 ASSERT_NO_FATAL_FAILURE(
1661 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1662 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1663 const Rect crop(8, 8, 24, 24);
1664
1665 Transaction().setCrop(layer, crop).apply();
1666 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -07001667 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1668 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001669}
1670
1671TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001672 sp<SurfaceControl> layer;
1673 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001674 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001675
1676 {
1677 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001678 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001679 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1680 }
1681
1682 {
1683 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001684 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001685 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1686 }
1687}
1688
Marissa Wall61c58622018-07-18 10:12:20 -07001689TEST_F(LayerTransactionTest, SetCropEmpty_BufferState) {
1690 sp<SurfaceControl> layer;
1691 ASSERT_NO_FATAL_FAILURE(
1692 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1693 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1694
1695 {
1696 SCOPED_TRACE("empty rect");
1697 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1698 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1699 }
1700
1701 {
1702 SCOPED_TRACE("negative rect");
1703 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1704 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1705 }
1706}
1707
1708TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001709 sp<SurfaceControl> layer;
1710 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001711 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001712
Marissa Wallf58c14b2018-07-24 10:50:43 -07001713 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001714 auto shot = screenshot();
1715 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1716 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1717}
1718
Valerie Hau0bc09152018-12-20 07:42:47 -08001719TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1720 sp<SurfaceControl> layer;
1721 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", mDisplayWidth, mDisplayHeight / 2,
1722 ISurfaceComposerClient::eFXSurfaceBufferState));
1723 sp<GraphicBuffer> buffer =
1724 new GraphicBuffer(mDisplayWidth, mDisplayHeight / 2, PIXEL_FORMAT_RGBA_8888, 1,
1725 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1726 BufferUsage::COMPOSER_OVERLAY,
1727 "test");
1728 fillGraphicBufferColor(buffer, Rect(0, 0, mDisplayWidth, mDisplayHeight / 4), Color::BLUE);
1729 fillGraphicBufferColor(buffer, Rect(0, mDisplayHeight / 4, mDisplayWidth, mDisplayHeight / 2),
1730 Color::RED);
1731
1732 Transaction().setBuffer(layer, buffer).apply();
1733
1734 // Partially out of bounds in the negative (upper left) direction
1735 Transaction().setCrop(layer, Rect(-128, -128, mDisplayWidth, mDisplayHeight / 4)).apply();
1736 {
1737 SCOPED_TRACE("out of bounds, negative (upper left) direction");
1738 auto shot = screenshot();
1739 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::BLUE);
1740 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::BLACK);
1741 }
1742
1743 // Partially out of bounds in the positive (lower right) direction
1744 Transaction()
1745 .setCrop(layer, Rect(0, mDisplayHeight / 4, mDisplayWidth + 1, mDisplayHeight))
1746 .apply();
1747 {
1748 SCOPED_TRACE("out of bounds, positive (lower right) direction");
1749 auto shot = screenshot();
1750 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::RED);
1751 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight / 2), Color::BLACK);
1752 }
1753
1754 // Fully out of buffer space bounds
1755 Transaction().setCrop(layer, Rect(-128, -128, -1, -1)).apply();
1756 {
1757 SCOPED_TRACE("Fully out of bounds");
1758 auto shot = screenshot();
1759 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight / 4), Color::BLUE);
1760 shot->expectColor(Rect(0, mDisplayHeight / 4, mDisplayWidth, mDisplayHeight / 2),
1761 Color::RED);
1762 }
1763}
1764
Marissa Wall61c58622018-07-18 10:12:20 -07001765TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001766 sp<SurfaceControl> layer;
1767 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001768 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001769
1770 const Point position(32, 32);
1771 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001772 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001773 auto shot = screenshot();
1774 shot->expectColor(crop + position, Color::RED);
1775 shot->expectBorder(crop + position, Color::BLACK);
1776}
1777
Marissa Wall61c58622018-07-18 10:12:20 -07001778TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1779 sp<SurfaceControl> layer;
1780 ASSERT_NO_FATAL_FAILURE(
1781 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1782 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1783
Marissa Wall861616d2018-10-22 12:52:23 -07001784 const Rect frame(32, 32, 64, 64);
Marissa Wall61c58622018-07-18 10:12:20 -07001785 const Rect crop(8, 8, 24, 24);
Marissa Wall861616d2018-10-22 12:52:23 -07001786 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001787 auto shot = screenshot();
Marissa Wall861616d2018-10-22 12:52:23 -07001788 shot->expectColor(frame, Color::RED);
1789 shot->expectBorder(frame, Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001790}
1791
1792TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001793 sp<SurfaceControl> layer;
1794 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001795 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001796
Marissa Wall861616d2018-10-22 12:52:23 -07001797 // crop_legacy is affected by matrix
Chia-I Wu04dcca82017-11-02 08:30:27 -07001798 Transaction()
1799 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001800 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001801 .apply();
1802 auto shot = screenshot();
1803 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1804 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1805}
1806
Marissa Wall61c58622018-07-18 10:12:20 -07001807TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001808 sp<SurfaceControl> layer;
1809 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001810 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001811
Marissa Wallf58c14b2018-07-24 10:50:43 -07001812 // setCrop_legacy is applied immediately by default, with or without resize pending
1813 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001814 {
1815 SCOPED_TRACE("resize pending");
1816 auto shot = screenshot();
1817 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1818 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1819 }
1820
Marissa Wall61c58622018-07-18 10:12:20 -07001821 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001822 {
1823 SCOPED_TRACE("resize applied");
1824 auto shot = screenshot();
1825 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1826 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1827 }
1828}
1829
Marissa Wall61c58622018-07-18 10:12:20 -07001830TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001831 sp<SurfaceControl> layer;
1832 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001833 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001834
Marissa Wallf58c14b2018-07-24 10:50:43 -07001835 // request setCrop_legacy to be applied with the next resize
1836 Transaction()
1837 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1838 .setGeometryAppliesWithResize(layer)
1839 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001840 {
1841 SCOPED_TRACE("waiting for next resize");
1842 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1843 }
1844
Marissa Wallf58c14b2018-07-24 10:50:43 -07001845 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001846 {
1847 SCOPED_TRACE("pending crop modified");
1848 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1849 }
1850
1851 Transaction().setSize(layer, 16, 16).apply();
1852 {
1853 SCOPED_TRACE("resize pending");
1854 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1855 }
1856
1857 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001858 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001859 {
1860 SCOPED_TRACE("new crop applied");
1861 auto shot = screenshot();
1862 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1863 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1864 }
1865}
1866
Marissa Wall61c58622018-07-18 10:12:20 -07001867TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001868 sp<SurfaceControl> layer;
1869 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001870 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001871
Marissa Wallf58c14b2018-07-24 10:50:43 -07001872 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001873 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001874 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001875 .setSize(layer, 16, 16)
1876 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1877 .setGeometryAppliesWithResize(layer)
1878 .apply();
1879 {
1880 SCOPED_TRACE("new crop pending");
1881 auto shot = screenshot();
1882 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1883 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1884 }
1885
1886 // XXX crop is never latched without other geometry change (b/69315677)
1887 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001888 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001889 Transaction().setPosition(layer, 0, 0).apply();
1890 {
1891 SCOPED_TRACE("new crop applied");
1892 auto shot = screenshot();
1893 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1894 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1895 }
1896}
1897
Marissa Wall861616d2018-10-22 12:52:23 -07001898TEST_F(LayerTransactionTest, SetFrameBasic_BufferState) {
1899 sp<SurfaceControl> layer;
1900 ASSERT_NO_FATAL_FAILURE(
1901 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1902 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1903 const Rect frame(8, 8, 24, 24);
1904
1905 Transaction().setFrame(layer, frame).apply();
1906 auto shot = screenshot();
1907 shot->expectColor(frame, Color::RED);
1908 shot->expectBorder(frame, Color::BLACK);
1909}
1910
1911TEST_F(LayerTransactionTest, SetFrameEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001912 sp<SurfaceControl> layer;
1913 ASSERT_NO_FATAL_FAILURE(
1914 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1915 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1916
Marissa Wall61c58622018-07-18 10:12:20 -07001917 {
Marissa Wall861616d2018-10-22 12:52:23 -07001918 SCOPED_TRACE("empty rect");
1919 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
1920 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001921 }
1922
Marissa Wall61c58622018-07-18 10:12:20 -07001923 {
Marissa Wall861616d2018-10-22 12:52:23 -07001924 SCOPED_TRACE("negative rect");
1925 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
1926 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001927 }
1928}
1929
Marissa Wall861616d2018-10-22 12:52:23 -07001930TEST_F(LayerTransactionTest, SetFrameDefaultParentless_BufferState) {
1931 sp<SurfaceControl> layer;
1932 ASSERT_NO_FATAL_FAILURE(
1933 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1934 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
1935
1936 // A parentless layer will default to a frame with the same size as the buffer
1937 auto shot = screenshot();
1938 shot->expectColor(Rect(0, 0, 10, 10), Color::RED);
1939 shot->expectBorder(Rect(0, 0, 10, 10), Color::BLACK);
1940}
1941
1942TEST_F(LayerTransactionTest, SetFrameDefaultBSParent_BufferState) {
1943 sp<SurfaceControl> parent, child;
1944 ASSERT_NO_FATAL_FAILURE(
1945 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1946 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
1947 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
1948
1949 ASSERT_NO_FATAL_FAILURE(
1950 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1951 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
1952
1953 Transaction().reparent(child, parent->getHandle()).apply();
1954
1955 // A layer will default to the frame of its parent
1956 auto shot = screenshot();
1957 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1958 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1959}
1960
1961TEST_F(LayerTransactionTest, SetFrameDefaultBQParent_BufferState) {
1962 sp<SurfaceControl> parent, child;
1963 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
1964 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
1965
1966 ASSERT_NO_FATAL_FAILURE(
1967 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1968 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
1969
1970 Transaction().reparent(child, parent->getHandle()).apply();
1971
1972 // A layer will default to the frame of its parent
1973 auto shot = screenshot();
1974 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1975 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1976}
1977
1978TEST_F(LayerTransactionTest, SetFrameUpdate_BufferState) {
1979 sp<SurfaceControl> layer;
1980 ASSERT_NO_FATAL_FAILURE(
1981 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1982 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1983 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
1984
1985 std::this_thread::sleep_for(500ms);
1986
1987 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
1988
1989 auto shot = screenshot();
1990 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1991 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1992}
1993
1994TEST_F(LayerTransactionTest, SetFrameOutsideBounds_BufferState) {
1995 sp<SurfaceControl> parent, child;
1996 ASSERT_NO_FATAL_FAILURE(
1997 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1998 ASSERT_NO_FATAL_FAILURE(
1999 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2000 Transaction().reparent(child, parent->getHandle()).apply();
2001
2002 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2003 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2004
2005 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2006 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
2007
2008 auto shot = screenshot();
2009 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
2010 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
2011 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2012}
2013
Marissa Wall61c58622018-07-18 10:12:20 -07002014TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
2015 sp<SurfaceControl> layer;
2016 ASSERT_NO_FATAL_FAILURE(
2017 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2018
2019 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2020
2021 auto shot = screenshot();
2022 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2023 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2024}
2025
2026TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
2027 sp<SurfaceControl> layer;
2028 ASSERT_NO_FATAL_FAILURE(
2029 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2030
2031 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2032
2033 {
2034 SCOPED_TRACE("set buffer 1");
2035 auto shot = screenshot();
2036 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2037 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2038 }
2039
2040 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
2041
2042 {
2043 SCOPED_TRACE("set buffer 2");
2044 auto shot = screenshot();
2045 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2046 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2047 }
2048
2049 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2050
2051 {
2052 SCOPED_TRACE("set buffer 3");
2053 auto shot = screenshot();
2054 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2055 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2056 }
2057}
2058
2059TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
2060 sp<SurfaceControl> layer1;
2061 ASSERT_NO_FATAL_FAILURE(
2062 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2063
2064 sp<SurfaceControl> layer2;
2065 ASSERT_NO_FATAL_FAILURE(
2066 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2067
2068 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2069
Marissa Wall861616d2018-10-22 12:52:23 -07002070 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002071 {
2072 SCOPED_TRACE("set layer 1 buffer red");
2073 auto shot = screenshot();
2074 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2075 }
2076
2077 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2078
Marissa Wall861616d2018-10-22 12:52:23 -07002079 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002080 {
2081 SCOPED_TRACE("set layer 2 buffer blue");
2082 auto shot = screenshot();
2083 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2084 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2085 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2086 }
2087
2088 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2089 {
2090 SCOPED_TRACE("set layer 1 buffer green");
2091 auto shot = screenshot();
2092 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2093 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2094 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2095 }
2096
2097 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2098
2099 {
2100 SCOPED_TRACE("set layer 2 buffer white");
2101 auto shot = screenshot();
2102 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2103 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2104 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2105 }
2106}
2107
2108TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
2109 sp<SurfaceControl> layer;
2110 ASSERT_NO_FATAL_FAILURE(
2111 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2112
2113 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2114 Color::BLUE, Color::WHITE));
2115
Marissa Wall861616d2018-10-22 12:52:23 -07002116 Transaction()
2117 .setFrame(layer, Rect(0, 0, 32, 32))
2118 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2119 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002120
2121 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2122 Color::GREEN, true /* filtered */);
2123}
2124
2125TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
2126 sp<SurfaceControl> layer;
2127 ASSERT_NO_FATAL_FAILURE(
2128 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2129
2130 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2131 Color::BLUE, Color::WHITE));
2132
Marissa Wall861616d2018-10-22 12:52:23 -07002133 Transaction()
2134 .setFrame(layer, Rect(0, 0, 32, 32))
2135 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2136 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002137
2138 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2139 Color::BLUE, true /* filtered */);
2140}
2141
2142TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
2143 sp<SurfaceControl> layer;
2144 ASSERT_NO_FATAL_FAILURE(
2145 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2146
2147 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2148 Color::BLUE, Color::WHITE));
2149
Marissa Wall861616d2018-10-22 12:52:23 -07002150 Transaction()
2151 .setFrame(layer, Rect(0, 0, 32, 32))
2152 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2153 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002154
2155 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2156 Color::GREEN, true /* filtered */);
2157}
2158
2159TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2160 sp<SurfaceControl> layer;
2161 ASSERT_NO_FATAL_FAILURE(
2162 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2163
2164 Transaction().setTransformToDisplayInverse(layer, false).apply();
2165
2166 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2167
2168 Transaction().setTransformToDisplayInverse(layer, true).apply();
2169}
2170
2171TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
2172 sp<SurfaceControl> layer;
Marissa Wall713b63f2018-10-17 15:42:43 -07002173 Transaction transaction;
2174 ASSERT_NO_FATAL_FAILURE(
2175 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2176
2177 sp<GraphicBuffer> buffer =
2178 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2179 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2180 BufferUsage::COMPOSER_OVERLAY,
2181 "test");
2182 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2183
2184 sp<Fence> fence;
2185 if (getBuffer(nullptr, &fence) != NO_ERROR) {
2186 GTEST_SUCCEED() << "test not supported";
2187 return;
2188 }
2189
2190 Transaction().setBuffer(layer, buffer).setAcquireFence(layer, fence).apply();
2191
2192 status_t status = fence->wait(1000);
2193 ASSERT_NE(static_cast<status_t>(Fence::Status::Unsignaled), status);
2194 std::this_thread::sleep_for(200ms);
2195
2196 auto shot = screenshot();
2197 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2198 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2199}
2200
2201TEST_F(LayerTransactionTest, SetFenceNull_BufferState) {
2202 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07002203 ASSERT_NO_FATAL_FAILURE(
2204 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2205
2206 sp<GraphicBuffer> buffer =
2207 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2208 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2209 BufferUsage::COMPOSER_OVERLAY,
2210 "test");
2211 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2212
Marissa Wallfda30bb2018-10-12 11:34:28 -07002213 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002214
2215 Transaction()
2216 .setBuffer(layer, buffer)
2217 .setAcquireFence(layer, fence)
Marissa Wall61c58622018-07-18 10:12:20 -07002218 .apply();
2219
2220 auto shot = screenshot();
2221 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2222 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2223}
2224
2225TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
2226 sp<SurfaceControl> layer;
2227 ASSERT_NO_FATAL_FAILURE(
2228 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2229
2230 sp<GraphicBuffer> buffer =
2231 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2232 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2233 BufferUsage::COMPOSER_OVERLAY,
2234 "test");
2235 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2236
2237 Transaction()
2238 .setBuffer(layer, buffer)
2239 .setDataspace(layer, ui::Dataspace::UNKNOWN)
Marissa Wall61c58622018-07-18 10:12:20 -07002240 .apply();
2241
2242 auto shot = screenshot();
2243 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2244 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2245}
2246
2247TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
2248 sp<SurfaceControl> layer;
2249 ASSERT_NO_FATAL_FAILURE(
2250 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2251
2252 sp<GraphicBuffer> buffer =
2253 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2254 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2255 BufferUsage::COMPOSER_OVERLAY,
2256 "test");
2257 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2258
2259 HdrMetadata hdrMetadata;
2260 hdrMetadata.validTypes = 0;
2261 Transaction()
2262 .setBuffer(layer, buffer)
2263 .setHdrMetadata(layer, hdrMetadata)
Marissa Wall61c58622018-07-18 10:12:20 -07002264 .apply();
2265
2266 auto shot = screenshot();
2267 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2268 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2269}
2270
2271TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2272 sp<SurfaceControl> layer;
2273 ASSERT_NO_FATAL_FAILURE(
2274 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2275
2276 sp<GraphicBuffer> buffer =
2277 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2278 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2279 BufferUsage::COMPOSER_OVERLAY,
2280 "test");
2281 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2282
2283 Region region;
2284 region.set(32, 32);
2285 Transaction()
2286 .setBuffer(layer, buffer)
2287 .setSurfaceDamageRegion(layer, region)
Marissa Wall61c58622018-07-18 10:12:20 -07002288 .apply();
2289
2290 auto shot = screenshot();
2291 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2292 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2293}
2294
2295TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2296 sp<SurfaceControl> layer;
2297 ASSERT_NO_FATAL_FAILURE(
2298 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2299
2300 sp<GraphicBuffer> buffer =
2301 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2302 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2303 BufferUsage::COMPOSER_OVERLAY,
2304 "test");
2305 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2306
2307 Transaction()
2308 .setBuffer(layer, buffer)
2309 .setApi(layer, NATIVE_WINDOW_API_CPU)
Marissa Wall61c58622018-07-18 10:12:20 -07002310 .apply();
2311
2312 auto shot = screenshot();
2313 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2314 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2315}
2316
2317TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2318 sp<SurfaceControl> layer;
2319 ASSERT_NO_FATAL_FAILURE(
2320 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2321
2322 // verify this doesn't cause a crash
2323 Transaction().setSidebandStream(layer, nullptr).apply();
2324}
2325
Marissa Wallebc2c052019-01-16 19:16:55 -08002326TEST_F(LayerTransactionTest, CacheBuffer_BufferState) {
2327 sp<GraphicBuffer> buffer =
2328 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2329 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2330 BufferUsage::COMPOSER_OVERLAY,
2331 "test");
2332
2333 int32_t bufferId = -1;
2334 ASSERT_EQ(NO_ERROR, mClient->cacheBuffer(buffer, &bufferId));
2335 ASSERT_GE(bufferId, 0);
2336
2337 ASSERT_EQ(NO_ERROR, mClient->uncacheBuffer(bufferId));
2338}
2339
2340TEST_F(LayerTransactionTest, CacheBuffers_BufferState) {
2341 std::vector<int32_t> bufferIds;
2342 int32_t bufferCount = 20;
2343
2344 for (int i = 0; i < bufferCount; i++) {
2345 sp<GraphicBuffer> buffer =
2346 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2347 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2348 BufferUsage::COMPOSER_OVERLAY,
2349 "test");
2350 int32_t bufferId = -1;
2351 ASSERT_EQ(NO_ERROR, mClient->cacheBuffer(buffer, &bufferId));
2352 if (bufferId < 0) {
2353 EXPECT_GE(bufferId, 0);
2354 break;
2355 }
2356 bufferIds.push_back(bufferId);
2357 }
2358
2359 for (int32_t bufferId : bufferIds) {
2360 ASSERT_EQ(NO_ERROR, mClient->uncacheBuffer(bufferId));
2361 }
2362}
2363
2364TEST_F(LayerTransactionTest, CacheBufferInvalid_BufferState) {
2365 sp<GraphicBuffer> buffer = nullptr;
2366
2367 int32_t bufferId = -1;
2368 ASSERT_NE(NO_ERROR, mClient->cacheBuffer(buffer, &bufferId));
2369 ASSERT_LT(bufferId, 0);
2370}
2371
2372TEST_F(LayerTransactionTest, UncacheBufferTwice_BufferState) {
2373 sp<GraphicBuffer> buffer =
2374 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2375 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2376 BufferUsage::COMPOSER_OVERLAY,
2377 "test");
2378
2379 int32_t bufferId = -1;
2380 ASSERT_EQ(NO_ERROR, mClient->cacheBuffer(buffer, &bufferId));
2381 ASSERT_GE(bufferId, 0);
2382
2383 ASSERT_EQ(NO_ERROR, mClient->uncacheBuffer(bufferId));
2384 mClient->uncacheBuffer(bufferId);
2385}
2386
2387TEST_F(LayerTransactionTest, UncacheBufferInvalidId_BufferState) {
2388 mClient->uncacheBuffer(-1);
2389 mClient->uncacheBuffer(0);
2390 mClient->uncacheBuffer(1);
2391 mClient->uncacheBuffer(5);
2392 mClient->uncacheBuffer(1000);
2393}
2394
2395TEST_F(LayerTransactionTest, SetCachedBuffer_BufferState) {
2396 sp<SurfaceControl> layer;
2397 ASSERT_NO_FATAL_FAILURE(
2398 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2399
2400 sp<GraphicBuffer> buffer =
2401 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2402 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2403 BufferUsage::COMPOSER_OVERLAY,
2404 "test");
2405
2406 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2407
2408 int32_t bufferId = -1;
2409 ASSERT_EQ(NO_ERROR, mClient->cacheBuffer(buffer, &bufferId));
2410 ASSERT_GE(bufferId, 0);
2411
2412 Transaction().setCachedBuffer(layer, bufferId).apply();
2413
2414 auto shot = screenshot();
2415 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2416 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2417
2418 ASSERT_EQ(NO_ERROR, mClient->uncacheBuffer(bufferId));
2419}
2420
2421TEST_F(LayerTransactionTest, SetCachedBufferDelayed_BufferState) {
2422 sp<SurfaceControl> layer;
2423 ASSERT_NO_FATAL_FAILURE(
2424 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2425
2426 sp<GraphicBuffer> cachedBuffer =
2427 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2428 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2429 BufferUsage::COMPOSER_OVERLAY,
2430 "test");
2431 int32_t bufferId = -1;
2432 ASSERT_EQ(NO_ERROR, mClient->cacheBuffer(cachedBuffer, &bufferId));
2433 ASSERT_GE(bufferId, 0);
2434
2435 sp<GraphicBuffer> buffer =
2436 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2437 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2438 BufferUsage::COMPOSER_OVERLAY,
2439 "test");
2440 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::BLUE);
2441 Transaction().setBuffer(layer, buffer).apply();
2442 {
2443 SCOPED_TRACE("Uncached buffer");
2444
2445 auto shot = screenshot();
2446 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2447 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2448 }
2449
2450 fillGraphicBufferColor(cachedBuffer, Rect(0, 0, 32, 32), Color::RED);
2451 Transaction().setCachedBuffer(layer, bufferId).apply();
2452 {
2453 SCOPED_TRACE("Cached buffer");
2454
2455 auto shot = screenshot();
2456 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2457 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2458 }
2459
2460 ASSERT_EQ(NO_ERROR, mClient->uncacheBuffer(bufferId));
2461}
2462
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002463class ColorTransformHelper {
2464public:
2465 static void DegammaColorSingle(half& s) {
2466 if (s <= 0.03928f)
2467 s = s / 12.92f;
2468 else
2469 s = pow((s + 0.055f) / 1.055f, 2.4f);
2470 }
2471
2472 static void DegammaColor(half3& color) {
2473 DegammaColorSingle(color.r);
2474 DegammaColorSingle(color.g);
2475 DegammaColorSingle(color.b);
2476 }
2477
2478 static void GammaColorSingle(half& s) {
2479 if (s <= 0.0031308f) {
2480 s = s * 12.92f;
2481 } else {
2482 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2483 }
2484 }
2485
2486 static void GammaColor(half3& color) {
2487 GammaColorSingle(color.r);
2488 GammaColorSingle(color.g);
2489 GammaColorSingle(color.b);
2490 }
2491
2492 static void applyMatrix(half3& color, const mat3& mat) {
2493 half3 ret = half3(0);
2494
2495 for (int i = 0; i < 3; i++) {
2496 for (int j = 0; j < 3; j++) {
2497 ret[i] = ret[i] + color[j] * mat[j][i];
2498 }
2499 }
2500 color = ret;
2501 }
2502};
2503
Peiyong Lind3788632018-09-18 16:01:31 -07002504TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2505 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002506 ASSERT_NO_FATAL_FAILURE(colorLayer =
2507 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2508 ISurfaceComposerClient::eFXSurfaceColor));
2509 Transaction()
2510 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2511 .setLayer(colorLayer, mLayerZBase + 1)
2512 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002513 {
2514 SCOPED_TRACE("default color");
2515 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2516 }
2517
2518 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002519 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002520 mat3 matrix;
2521 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2522 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2523 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002524
2525 // degamma before applying the matrix
2526 if (mColorManagementUsed) {
2527 ColorTransformHelper::DegammaColor(expected);
2528 }
2529
2530 ColorTransformHelper::applyMatrix(expected, matrix);
2531
2532 if (mColorManagementUsed) {
2533 ColorTransformHelper::GammaColor(expected);
2534 }
2535
2536 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2537 uint8_t(expected.b * 255), 255};
2538
2539 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2540 // channel) should be less than one
2541 const uint8_t tolerance = 1;
2542
Peiyong Lind3788632018-09-18 16:01:31 -07002543 Transaction().setColor(colorLayer, color)
2544 .setColorTransform(colorLayer, matrix, vec3()).apply();
2545 {
2546 SCOPED_TRACE("new color");
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002547 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002548 }
2549}
2550
chaviwf66724d2018-11-28 16:35:21 -08002551TEST_F(LayerTransactionTest, SetColorTransformOnParent) {
2552 sp<SurfaceControl> parentLayer;
2553 sp<SurfaceControl> colorLayer;
2554 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2555 0 /* buffer height */,
2556 ISurfaceComposerClient::eFXSurfaceContainer));
2557 ASSERT_NO_FATAL_FAILURE(
2558 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2559 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2560
2561 Transaction()
2562 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2563 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2564 .setLayer(parentLayer, mLayerZBase + 1)
2565 .apply();
2566 {
2567 SCOPED_TRACE("default color");
2568 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2569 }
2570
2571 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2572 half3 expected = color;
2573 mat3 matrix;
2574 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2575 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2576 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2577
2578 // degamma before applying the matrix
2579 if (mColorManagementUsed) {
2580 ColorTransformHelper::DegammaColor(expected);
2581 }
2582
2583 ColorTransformHelper::applyMatrix(expected, matrix);
2584
2585 if (mColorManagementUsed) {
2586 ColorTransformHelper::GammaColor(expected);
2587 }
2588
2589 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2590 uint8_t(expected.b * 255), 255};
2591
2592 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2593 // channel) should be less than one
2594 const uint8_t tolerance = 1;
2595
2596 Transaction()
2597 .setColor(colorLayer, color)
2598 .setColorTransform(parentLayer, matrix, vec3())
2599 .apply();
2600 {
2601 SCOPED_TRACE("new color");
2602 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
2603 }
2604}
2605
2606TEST_F(LayerTransactionTest, SetColorTransformOnChildAndParent) {
2607 sp<SurfaceControl> parentLayer;
2608 sp<SurfaceControl> colorLayer;
2609 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2610 0 /* buffer height */,
2611 ISurfaceComposerClient::eFXSurfaceContainer));
2612 ASSERT_NO_FATAL_FAILURE(
2613 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2614 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2615
2616 Transaction()
2617 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2618 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2619 .setLayer(parentLayer, mLayerZBase + 1)
2620 .apply();
2621 {
2622 SCOPED_TRACE("default color");
2623 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2624 }
2625
2626 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2627 half3 expected = color;
2628 mat3 matrixChild;
2629 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
2630 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
2631 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
2632 mat3 matrixParent;
2633 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
2634 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
2635 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
2636
2637 // degamma before applying the matrix
2638 if (mColorManagementUsed) {
2639 ColorTransformHelper::DegammaColor(expected);
2640 }
2641
2642 ColorTransformHelper::applyMatrix(expected, matrixChild);
2643 ColorTransformHelper::applyMatrix(expected, matrixParent);
2644
2645 if (mColorManagementUsed) {
2646 ColorTransformHelper::GammaColor(expected);
2647 }
2648
2649 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2650 uint8_t(expected.b * 255), 255};
2651
2652 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2653 // channel) should be less than one
2654 const uint8_t tolerance = 1;
2655
2656 Transaction()
2657 .setColor(colorLayer, color)
2658 .setColorTransform(parentLayer, matrixParent, vec3())
2659 .setColorTransform(colorLayer, matrixChild, vec3())
2660 .apply();
2661 {
2662 SCOPED_TRACE("new color");
2663 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
2664 }
2665}
2666
Marissa Wall80d94ad2019-01-18 16:04:36 -08002667struct CallbackData {
2668 CallbackData() = default;
2669 CallbackData(nsecs_t time, const sp<Fence>& fence,
2670 const std::vector<SurfaceControlStats>& stats)
2671 : latchTime(time), presentFence(fence), surfaceControlStats(stats) {}
2672
2673 nsecs_t latchTime;
2674 sp<Fence> presentFence;
2675 std::vector<SurfaceControlStats> surfaceControlStats;
2676};
2677
Marissa Wallfda30bb2018-10-12 11:34:28 -07002678class ExpectedResult {
2679public:
2680 enum Transaction {
2681 NOT_PRESENTED = 0,
2682 PRESENTED,
2683 };
2684
2685 enum Buffer {
2686 NOT_ACQUIRED = 0,
2687 ACQUIRED,
2688 };
2689
2690 enum PreviousBuffer {
2691 NOT_RELEASED = 0,
2692 RELEASED,
Marissa Wall5a68a772018-12-22 17:43:42 -08002693 UNKNOWN,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002694 };
2695
2696 void reset() {
2697 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2698 mExpectedSurfaceResults.clear();
2699 }
2700
2701 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07002702 ExpectedResult::Buffer bufferResult = ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002703 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2704 mTransactionResult = transactionResult;
Marissa Wall80d94ad2019-01-18 16:04:36 -08002705 mExpectedSurfaceResults.emplace(std::piecewise_construct, std::forward_as_tuple(layer),
Marissa Wallfda30bb2018-10-12 11:34:28 -07002706 std::forward_as_tuple(bufferResult, previousBufferResult));
2707 }
2708
2709 void addSurfaces(ExpectedResult::Transaction transactionResult,
2710 const std::vector<sp<SurfaceControl>>& layers,
Marissa Wall713b63f2018-10-17 15:42:43 -07002711 ExpectedResult::Buffer bufferResult = ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002712 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2713 for (const auto& layer : layers) {
2714 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
2715 }
2716 }
2717
Marissa Wall17b4e452018-12-26 16:32:34 -08002718 void addExpectedPresentTime(nsecs_t expectedPresentTime) {
2719 mExpectedPresentTime = expectedPresentTime;
2720 }
2721
Marissa Wall80d94ad2019-01-18 16:04:36 -08002722 void verifyCallbackData(const CallbackData& callbackData) const {
2723 const auto& [latchTime, presentFence, surfaceControlStats] = callbackData;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002724 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
2725 ASSERT_GE(latchTime, 0) << "bad latch time";
Valerie Hau63258a12018-12-14 14:31:48 -08002726 ASSERT_NE(presentFence, nullptr);
Marissa Wall17b4e452018-12-26 16:32:34 -08002727 if (mExpectedPresentTime >= 0) {
2728 ASSERT_EQ(presentFence->wait(3000), NO_ERROR);
2729 ASSERT_GE(presentFence->getSignalTime(), mExpectedPresentTime - nsecs_t(5 * 1e6));
2730 // if the panel is running at 30 hz, at the worst case, our expected time just
2731 // misses vsync and we have to wait another 33.3ms
2732 ASSERT_LE(presentFence->getSignalTime(),
2733 mExpectedPresentTime + nsecs_t(66.666666 * 1e6));
2734 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002735 } else {
Valerie Hau63258a12018-12-14 14:31:48 -08002736 ASSERT_EQ(presentFence, nullptr) << "transaction shouldn't have been presented";
Marissa Wallfda30bb2018-10-12 11:34:28 -07002737 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
2738 }
2739
Marissa Wall80d94ad2019-01-18 16:04:36 -08002740 ASSERT_EQ(surfaceControlStats.size(), mExpectedSurfaceResults.size())
Marissa Wallfda30bb2018-10-12 11:34:28 -07002741 << "wrong number of surfaces";
2742
Marissa Wall80d94ad2019-01-18 16:04:36 -08002743 for (const auto& stats : surfaceControlStats) {
2744 ASSERT_NE(stats.surfaceControl, nullptr) << "returned null surface control";
2745
Marissa Wallfda30bb2018-10-12 11:34:28 -07002746 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
2747 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
2748 << "unexpected surface control";
Marissa Wall80d94ad2019-01-18 16:04:36 -08002749 expectedSurfaceResult->second.verifySurfaceControlStats(stats, latchTime);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002750 }
2751 }
2752
2753private:
2754 class ExpectedSurfaceResult {
2755 public:
2756 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
2757 ExpectedResult::PreviousBuffer previousBufferResult)
2758 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
2759
Marissa Wall80d94ad2019-01-18 16:04:36 -08002760 void verifySurfaceControlStats(const SurfaceControlStats& surfaceControlStats,
2761 nsecs_t latchTime) const {
2762 const auto& [surfaceControl, acquireTime, previousReleaseFence] = surfaceControlStats;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002763
2764 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
2765 << "bad acquire time";
2766 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
Marissa Wall5a68a772018-12-22 17:43:42 -08002767
2768 if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED) {
2769 ASSERT_NE(previousReleaseFence, nullptr)
2770 << "failed to set release prev buffer fence";
2771 } else if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::NOT_RELEASED) {
2772 ASSERT_EQ(previousReleaseFence, nullptr)
2773 << "should not have set released prev buffer fence";
2774 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002775 }
2776
2777 private:
2778 ExpectedResult::Buffer mBufferResult;
2779 ExpectedResult::PreviousBuffer mPreviousBufferResult;
2780 };
2781
Marissa Wall80d94ad2019-01-18 16:04:36 -08002782 struct SCHash {
2783 std::size_t operator()(const sp<SurfaceControl>& sc) const {
2784 return std::hash<IBinder*>{}(sc->getHandle().get());
Marissa Wallfda30bb2018-10-12 11:34:28 -07002785 }
2786 };
2787 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
Marissa Wall17b4e452018-12-26 16:32:34 -08002788 nsecs_t mExpectedPresentTime = -1;
Marissa Wall80d94ad2019-01-18 16:04:36 -08002789 std::unordered_map<sp<SurfaceControl>, ExpectedSurfaceResult, SCHash> mExpectedSurfaceResults;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002790};
2791
2792class CallbackHelper {
2793public:
Marissa Wall80d94ad2019-01-18 16:04:36 -08002794 static void function(void* callbackContext, nsecs_t latchTime, const sp<Fence>& presentFence,
2795 const std::vector<SurfaceControlStats>& stats) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002796 if (!callbackContext) {
2797 ALOGE("failed to get callback context");
2798 }
2799 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
2800 std::lock_guard lock(helper->mMutex);
Marissa Wall80d94ad2019-01-18 16:04:36 -08002801 helper->mCallbackDataQueue.emplace(latchTime, presentFence, stats);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002802 helper->mConditionVariable.notify_all();
2803 }
2804
Marissa Wall80d94ad2019-01-18 16:04:36 -08002805 void getCallbackData(CallbackData* outData) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002806 std::unique_lock lock(mMutex);
2807
Marissa Wall80d94ad2019-01-18 16:04:36 -08002808 if (mCallbackDataQueue.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002809 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
2810 std::cv_status::timeout)
2811 << "did not receive callback";
2812 }
2813
Marissa Wall80d94ad2019-01-18 16:04:36 -08002814 *outData = std::move(mCallbackDataQueue.front());
2815 mCallbackDataQueue.pop();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002816 }
2817
2818 void verifyFinalState() {
2819 // Wait to see if there are extra callbacks
2820 std::this_thread::sleep_for(500ms);
2821
2822 std::lock_guard lock(mMutex);
Marissa Wall80d94ad2019-01-18 16:04:36 -08002823 EXPECT_EQ(mCallbackDataQueue.size(), 0) << "extra callbacks received";
2824 mCallbackDataQueue = {};
Marissa Wallfda30bb2018-10-12 11:34:28 -07002825 }
2826
2827 void* getContext() { return static_cast<void*>(this); }
2828
2829 std::mutex mMutex;
2830 std::condition_variable mConditionVariable;
Marissa Wall80d94ad2019-01-18 16:04:36 -08002831 std::queue<CallbackData> mCallbackDataQueue;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002832};
2833
2834class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07002835public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07002836 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07002837 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002838 }
2839
Marissa Wall713b63f2018-10-17 15:42:43 -07002840 static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
2841 const sp<SurfaceControl>& layer = nullptr) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002842 if (layer) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002843 sp<GraphicBuffer> buffer;
2844 sp<Fence> fence;
2845 int err = getBuffer(&buffer, &fence);
2846 if (err != NO_ERROR) {
2847 return err;
2848 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002849
Marissa Wall861616d2018-10-22 12:52:23 -07002850 transaction.setBuffer(layer, buffer).setAcquireFence(layer, fence);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002851 }
2852
2853 transaction.addTransactionCompletedCallback(callbackHelper->function,
2854 callbackHelper->getContext());
Marissa Wall713b63f2018-10-17 15:42:43 -07002855 return NO_ERROR;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002856 }
2857
Marissa Wall861616d2018-10-22 12:52:23 -07002858 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2859 bool finalState = false) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08002860 CallbackData callbackData;
2861 ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
2862 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
Marissa Wallfda30bb2018-10-12 11:34:28 -07002863
2864 if (finalState) {
2865 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2866 }
2867 }
2868
Marissa Wall861616d2018-10-22 12:52:23 -07002869 static void waitForCallbacks(CallbackHelper& helper,
2870 const std::vector<ExpectedResult>& expectedResults,
2871 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002872 for (const auto& expectedResult : expectedResults) {
2873 waitForCallback(helper, expectedResult);
2874 }
2875 if (finalState) {
2876 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2877 }
2878 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002879};
2880
2881TEST_F(LayerCallbackTest, Basic) {
2882 sp<SurfaceControl> layer;
2883 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2884
2885 Transaction transaction;
2886 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002887 int err = fillTransaction(transaction, &callback, layer);
2888 if (err) {
2889 GTEST_SUCCEED() << "test not supported";
2890 return;
2891 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002892
2893 transaction.apply();
2894
2895 ExpectedResult expected;
2896 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2897 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2898}
2899
2900TEST_F(LayerCallbackTest, NoBuffer) {
2901 sp<SurfaceControl> layer;
2902 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2903
2904 Transaction transaction;
2905 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002906 int err = fillTransaction(transaction, &callback);
2907 if (err) {
2908 GTEST_SUCCEED() << "test not supported";
2909 return;
2910 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002911
Marissa Wall861616d2018-10-22 12:52:23 -07002912 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002913
2914 ExpectedResult expected;
Marissa Wall713b63f2018-10-17 15:42:43 -07002915 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
2916 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002917 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2918}
2919
2920TEST_F(LayerCallbackTest, NoStateChange) {
2921 Transaction transaction;
2922 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002923 int err = fillTransaction(transaction, &callback);
2924 if (err) {
2925 GTEST_SUCCEED() << "test not supported";
2926 return;
2927 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002928
2929 transaction.apply();
2930
2931 ExpectedResult expected;
2932 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2933}
2934
2935TEST_F(LayerCallbackTest, OffScreen) {
2936 sp<SurfaceControl> layer;
2937 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2938
2939 Transaction transaction;
2940 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002941 int err = fillTransaction(transaction, &callback, layer);
2942 if (err) {
2943 GTEST_SUCCEED() << "test not supported";
2944 return;
2945 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002946
Marissa Wall861616d2018-10-22 12:52:23 -07002947 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002948
2949 ExpectedResult expected;
2950 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2951 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2952}
2953
2954TEST_F(LayerCallbackTest, Merge) {
2955 sp<SurfaceControl> layer1, layer2;
2956 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2957 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2958
2959 Transaction transaction1, transaction2;
2960 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07002961 int err = fillTransaction(transaction1, &callback1, layer1);
2962 if (err) {
2963 GTEST_SUCCEED() << "test not supported";
2964 return;
2965 }
2966 err = fillTransaction(transaction2, &callback2, layer2);
2967 if (err) {
2968 GTEST_SUCCEED() << "test not supported";
2969 return;
2970 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002971
Marissa Wall861616d2018-10-22 12:52:23 -07002972 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2973 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002974
2975 ExpectedResult expected;
2976 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2977 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2978 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2979}
2980
2981TEST_F(LayerCallbackTest, Merge_SameCallback) {
2982 sp<SurfaceControl> layer1, layer2;
2983 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2984 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2985
2986 Transaction transaction1, transaction2;
2987 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002988 int err = fillTransaction(transaction1, &callback, layer1);
2989 if (err) {
2990 GTEST_SUCCEED() << "test not supported";
2991 return;
2992 }
2993 err = fillTransaction(transaction2, &callback, layer2);
2994 if (err) {
2995 GTEST_SUCCEED() << "test not supported";
2996 return;
2997 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002998
2999 transaction2.merge(std::move(transaction1)).apply();
3000
3001 ExpectedResult expected;
3002 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3003 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3004 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3005}
3006
3007TEST_F(LayerCallbackTest, Merge_SameLayer) {
3008 sp<SurfaceControl> layer;
3009 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3010
3011 Transaction transaction1, transaction2;
3012 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003013 int err = fillTransaction(transaction1, &callback1, layer);
3014 if (err) {
3015 GTEST_SUCCEED() << "test not supported";
3016 return;
3017 }
3018 err = fillTransaction(transaction2, &callback2, layer);
3019 if (err) {
3020 GTEST_SUCCEED() << "test not supported";
3021 return;
3022 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003023
3024 transaction2.merge(std::move(transaction1)).apply();
3025
3026 ExpectedResult expected;
3027 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3028 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3029 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3030}
3031
Marissa Wallfda30bb2018-10-12 11:34:28 -07003032TEST_F(LayerCallbackTest, Merge_DifferentClients) {
3033 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3034 client2(new SurfaceComposerClient);
3035
3036 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3037 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3038
3039 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003040 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003041 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003042 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003043 ISurfaceComposerClient::eFXSurfaceBufferState));
3044
3045 Transaction transaction1, transaction2;
3046 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003047 int err = fillTransaction(transaction1, &callback1, layer1);
3048 if (err) {
3049 GTEST_SUCCEED() << "test not supported";
3050 return;
3051 }
3052 err = fillTransaction(transaction2, &callback2, layer2);
3053 if (err) {
3054 GTEST_SUCCEED() << "test not supported";
3055 return;
3056 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003057
Marissa Wall861616d2018-10-22 12:52:23 -07003058 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3059 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003060
3061 ExpectedResult expected;
3062 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3063 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3064 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3065}
3066
3067TEST_F(LayerCallbackTest, MultipleTransactions) {
3068 sp<SurfaceControl> layer;
3069 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3070
3071 Transaction transaction;
3072 CallbackHelper callback;
3073 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003074 int err = fillTransaction(transaction, &callback, layer);
3075 if (err) {
3076 GTEST_SUCCEED() << "test not supported";
3077 return;
3078 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003079
3080 transaction.apply();
3081
3082 ExpectedResult expected;
3083 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07003084 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003085 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3086 : ExpectedResult::PreviousBuffer::RELEASED);
3087 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3088 }
3089 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3090}
3091
3092TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
3093 sp<SurfaceControl> layer;
3094 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3095
3096 Transaction transaction;
3097 CallbackHelper callback;
3098 for (size_t i = 0; i < 10; i++) {
3099 ExpectedResult expected;
3100
3101 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003102 int err = fillTransaction(transaction, &callback, layer);
3103 if (err) {
3104 GTEST_SUCCEED() << "test not supported";
3105 return;
3106 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003107 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3108 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003109 int err = fillTransaction(transaction, &callback);
3110 if (err) {
3111 GTEST_SUCCEED() << "test not supported";
3112 return;
3113 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003114 }
3115
3116 transaction.apply();
3117
3118 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3119 }
3120 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3121}
3122
3123TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
3124 sp<SurfaceControl> layer;
3125 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3126
3127 Transaction transaction;
3128 CallbackHelper callback;
3129 for (size_t i = 0; i < 10; i++) {
3130 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003131 int err = fillTransaction(transaction, &callback, layer);
3132 if (err) {
3133 GTEST_SUCCEED() << "test not supported";
3134 return;
3135 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003136 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003137 int err = fillTransaction(transaction, &callback);
3138 if (err) {
3139 GTEST_SUCCEED() << "test not supported";
3140 return;
3141 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003142 }
3143
Marissa Wall861616d2018-10-22 12:52:23 -07003144 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003145
3146 ExpectedResult expected;
3147 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
3148 : ExpectedResult::Transaction::NOT_PRESENTED,
Marissa Wall713b63f2018-10-17 15:42:43 -07003149 layer,
3150 (i == 0) ? ExpectedResult::Buffer::ACQUIRED
3151 : ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003152 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
3153 }
3154 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3155}
3156
3157TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
3158 sp<SurfaceControl> layer1, layer2;
3159 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3160 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3161
3162 Transaction transaction1, transaction2;
3163 CallbackHelper callback1, callback2;
3164 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003165 int err = fillTransaction(transaction1, &callback1, layer1);
3166 if (err) {
3167 GTEST_SUCCEED() << "test not supported";
3168 return;
3169 }
3170 err = fillTransaction(transaction2, &callback2, layer2);
3171 if (err) {
3172 GTEST_SUCCEED() << "test not supported";
3173 return;
3174 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003175
Marissa Wall861616d2018-10-22 12:52:23 -07003176 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3177 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003178
3179 ExpectedResult expected;
3180 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003181 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003182 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3183 : ExpectedResult::PreviousBuffer::RELEASED);
3184 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3185 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3186 }
3187 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3188 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3189}
3190
3191TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
3192 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3193 client2(new SurfaceComposerClient);
3194 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3195 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3196
3197 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003198 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003199 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003200 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003201 ISurfaceComposerClient::eFXSurfaceBufferState));
3202
3203 Transaction transaction1, transaction2;
3204 CallbackHelper callback1, callback2;
3205 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003206 int err = fillTransaction(transaction1, &callback1, layer1);
3207 if (err) {
3208 GTEST_SUCCEED() << "test not supported";
3209 return;
3210 }
3211 err = fillTransaction(transaction2, &callback2, layer2);
3212 if (err) {
3213 GTEST_SUCCEED() << "test not supported";
3214 return;
3215 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003216
Marissa Wall861616d2018-10-22 12:52:23 -07003217 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3218 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003219
3220 ExpectedResult expected;
3221 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003222 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003223 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3224 : ExpectedResult::PreviousBuffer::RELEASED);
3225 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3226 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3227 }
3228 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3229 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3230}
3231
3232TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
3233 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3234 client2(new SurfaceComposerClient);
3235 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3236 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3237
3238 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003239 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003240 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003241 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003242 ISurfaceComposerClient::eFXSurfaceBufferState));
3243
3244 Transaction transaction1, transaction2;
3245 CallbackHelper callback1, callback2;
3246
3247 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003248 int err = fillTransaction(transaction1, &callback1, layer1);
3249 if (err) {
3250 GTEST_SUCCEED() << "test not supported";
3251 return;
3252 }
3253 err = fillTransaction(transaction2, &callback2, layer2);
3254 if (err) {
3255 GTEST_SUCCEED() << "test not supported";
3256 return;
3257 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003258
Marissa Wall861616d2018-10-22 12:52:23 -07003259 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3260 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003261
3262 ExpectedResult expected;
3263 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3264 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3265 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3266 expected.reset();
3267
3268 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003269 err = fillTransaction(transaction1, &callback1);
3270 if (err) {
3271 GTEST_SUCCEED() << "test not supported";
3272 return;
3273 }
3274 err = fillTransaction(transaction2, &callback2);
3275 if (err) {
3276 GTEST_SUCCEED() << "test not supported";
3277 return;
3278 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003279
3280 transaction2.merge(std::move(transaction1)).apply();
3281
3282 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3283 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3284}
3285
3286TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
3287 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3288 client2(new SurfaceComposerClient);
3289
3290 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3291 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3292
3293 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003294 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003295 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003296 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003297 ISurfaceComposerClient::eFXSurfaceBufferState));
3298
3299 Transaction transaction1, transaction2;
3300 CallbackHelper callback1, callback2;
3301
3302 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003303 int err = fillTransaction(transaction1, &callback1, layer1);
3304 if (err) {
3305 GTEST_SUCCEED() << "test not supported";
3306 return;
3307 }
3308 err = fillTransaction(transaction2, &callback2, layer2);
3309 if (err) {
3310 GTEST_SUCCEED() << "test not supported";
3311 return;
3312 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003313
Marissa Wall861616d2018-10-22 12:52:23 -07003314 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3315 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003316
3317 ExpectedResult expected;
3318 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3319 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3320 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3321 expected.reset();
3322
3323 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003324 err = fillTransaction(transaction1, &callback1);
3325 if (err) {
3326 GTEST_SUCCEED() << "test not supported";
3327 return;
3328 }
3329 err = fillTransaction(transaction2, &callback2);
3330 if (err) {
3331 GTEST_SUCCEED() << "test not supported";
3332 return;
3333 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003334
Marissa Wall861616d2018-10-22 12:52:23 -07003335 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003336
Marissa Wall713b63f2018-10-17 15:42:43 -07003337 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
3338 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003339 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3340 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3341}
3342
3343TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3344 sp<SurfaceControl> layer;
3345 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3346
3347 Transaction transaction;
3348 CallbackHelper callback;
3349 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003350 for (auto& expected : expectedResults) {
3351 expected.reset();
3352 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall5a68a772018-12-22 17:43:42 -08003353 ExpectedResult::Buffer::ACQUIRED,
3354 ExpectedResult::PreviousBuffer::UNKNOWN);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003355
Marissa Wall713b63f2018-10-17 15:42:43 -07003356 int err = fillTransaction(transaction, &callback, layer);
3357 if (err) {
3358 GTEST_SUCCEED() << "test not supported";
3359 return;
3360 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003361
3362 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003363 }
3364 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3365}
3366
3367TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3368 sp<SurfaceControl> layer;
3369 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3370
Marissa Wall713b63f2018-10-17 15:42:43 -07003371 // Normal call to set up test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003372 Transaction transaction;
3373 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003374 int err = fillTransaction(transaction, &callback, layer);
3375 if (err) {
3376 GTEST_SUCCEED() << "test not supported";
3377 return;
3378 }
3379
3380 transaction.apply();
3381
3382 ExpectedResult expected;
3383 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3384 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3385
3386 // Test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003387 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003388 for (auto& expected : expectedResults) {
3389 expected.reset();
3390
Marissa Wall713b63f2018-10-17 15:42:43 -07003391 err = fillTransaction(transaction, &callback);
3392 if (err) {
3393 GTEST_SUCCEED() << "test not supported";
3394 return;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003395 }
3396
3397 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003398 }
3399 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3400}
3401
3402TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3403 sp<SurfaceControl> layer;
3404 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3405
3406 // Normal call to set up test
3407 Transaction transaction;
3408 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003409 int err = fillTransaction(transaction, &callback, layer);
3410 if (err) {
3411 GTEST_SUCCEED() << "test not supported";
3412 return;
3413 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003414
Marissa Wall861616d2018-10-22 12:52:23 -07003415 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003416
3417 ExpectedResult expectedResult;
3418 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3419 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3420
3421 // Test
3422 std::vector<ExpectedResult> expectedResults(50);
3423 for (auto& expected : expectedResults) {
3424 expected.reset();
Marissa Wall713b63f2018-10-17 15:42:43 -07003425 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
3426 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003427
Marissa Wall713b63f2018-10-17 15:42:43 -07003428 err = fillTransaction(transaction, &callback);
3429 if (err) {
3430 GTEST_SUCCEED() << "test not supported";
3431 return;
3432 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003433
Marissa Wall861616d2018-10-22 12:52:23 -07003434 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003435 }
3436 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3437}
3438
Marissa Wall17b4e452018-12-26 16:32:34 -08003439TEST_F(LayerCallbackTest, DesiredPresentTime) {
3440 sp<SurfaceControl> layer;
3441 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3442
3443 Transaction transaction;
3444 CallbackHelper callback;
3445 int err = fillTransaction(transaction, &callback, layer);
3446 if (err) {
3447 GTEST_SUCCEED() << "test not supported";
3448 return;
3449 }
3450
3451 // Try to present 100ms in the future
3452 nsecs_t time = systemTime() + (100 * 1e6);
3453
3454 transaction.setDesiredPresentTime(time);
3455 transaction.apply();
3456
3457 ExpectedResult expected;
3458 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3459 expected.addExpectedPresentTime(time);
3460 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3461}
3462
3463TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
3464 sp<SurfaceControl> layer;
3465 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3466
3467 Transaction transaction;
3468 CallbackHelper callback1;
3469 int err = fillTransaction(transaction, &callback1, layer);
3470 if (err) {
3471 GTEST_SUCCEED() << "test not supported";
3472 return;
3473 }
3474
3475 // Try to present 100ms in the future
3476 nsecs_t time = systemTime() + (100 * 1e6);
3477
3478 transaction.setDesiredPresentTime(time);
3479 transaction.apply();
3480
3481 ExpectedResult expected1;
3482 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3483 expected1.addExpectedPresentTime(time);
3484
3485 CallbackHelper callback2;
3486 err = fillTransaction(transaction, &callback2, layer);
3487 if (err) {
3488 GTEST_SUCCEED() << "test not supported";
3489 return;
3490 }
3491
3492 // Try to present 33ms after the first frame
3493 time += (33.3 * 1e6);
3494
3495 transaction.setDesiredPresentTime(time);
3496 transaction.apply();
3497
3498 ExpectedResult expected2;
3499 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3500 ExpectedResult::Buffer::ACQUIRED,
3501 ExpectedResult::PreviousBuffer::RELEASED);
3502 expected2.addExpectedPresentTime(time);
3503
3504 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3505 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3506}
3507
3508TEST_F(LayerCallbackTest, DesiredPresentTime_OutOfOrder) {
3509 sp<SurfaceControl> layer;
3510 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3511
3512 Transaction transaction;
3513 CallbackHelper callback1;
3514 int err = fillTransaction(transaction, &callback1, layer);
3515 if (err) {
3516 GTEST_SUCCEED() << "test not supported";
3517 return;
3518 }
3519
3520 // Try to present 100ms in the future
3521 nsecs_t time = systemTime() + (100 * 1e6);
3522
3523 transaction.setDesiredPresentTime(time);
3524 transaction.apply();
3525
3526 ExpectedResult expected1;
3527 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3528 expected1.addExpectedPresentTime(time);
3529
3530 CallbackHelper callback2;
3531 err = fillTransaction(transaction, &callback2, layer);
3532 if (err) {
3533 GTEST_SUCCEED() << "test not supported";
3534 return;
3535 }
3536
3537 // Try to present 33ms before the previous frame
3538 time -= (33.3 * 1e6);
3539
3540 transaction.setDesiredPresentTime(time);
3541 transaction.apply();
3542
3543 ExpectedResult expected2;
3544 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3545 ExpectedResult::Buffer::ACQUIRED,
3546 ExpectedResult::PreviousBuffer::RELEASED);
3547
3548 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3549 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3550}
3551
3552TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
3553 sp<SurfaceControl> layer;
3554 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3555
3556 Transaction transaction;
3557 CallbackHelper callback;
3558 int err = fillTransaction(transaction, &callback, layer);
3559 if (err) {
3560 GTEST_SUCCEED() << "test not supported";
3561 return;
3562 }
3563
3564 // Try to present 100ms in the past
3565 nsecs_t time = systemTime() - (100 * 1e6);
3566
3567 transaction.setDesiredPresentTime(time);
3568 transaction.apply();
3569
3570 ExpectedResult expected;
3571 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3572 expected.addExpectedPresentTime(systemTime());
3573 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3574}
3575
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003576class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003577protected:
3578 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07003579 LayerTransactionTest::SetUp();
3580 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003581
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003582 sp<IBinder> display(
3583 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07003584 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07003585 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07003586
3587 ssize_t displayWidth = info.w;
3588 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003589
3590 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07003591 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
3592 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003593 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003594 ASSERT_TRUE(mBGSurfaceControl->isValid());
3595 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
3596
3597 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07003598 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
3599
Peiyong Lin566a3b42018-01-09 18:22:43 -08003600 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003601 ASSERT_TRUE(mFGSurfaceControl->isValid());
3602
3603 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3604
3605 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07003606 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003607 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003608 ASSERT_TRUE(mSyncSurfaceControl->isValid());
3609
3610 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3611
Robert Carr4cdc58f2017-08-23 14:22:20 -07003612 asTransaction([&](Transaction& t) {
3613 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003614
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003615 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07003616
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003617 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
3618 .setPosition(mFGSurfaceControl, 64, 64)
3619 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003620
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003621 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
3622 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
3623 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003624 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003625 }
3626
3627 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07003628 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003629 mBGSurfaceControl = 0;
3630 mFGSurfaceControl = 0;
3631 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003632 }
3633
3634 void waitForPostedBuffers() {
3635 // Since the sync surface is in synchronous mode (i.e. double buffered)
3636 // posting three buffers to it should ensure that at least two
3637 // SurfaceFlinger::handlePageFlip calls have been made, which should
3638 // guaranteed that a buffer posted to another Surface has been retired.
3639 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3640 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3641 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3642 }
3643
Robert Carr4cdc58f2017-08-23 14:22:20 -07003644 void asTransaction(const std::function<void(Transaction&)>& exec) {
3645 Transaction t;
3646 exec(t);
3647 t.apply(true);
3648 }
3649
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003650 sp<SurfaceControl> mBGSurfaceControl;
3651 sp<SurfaceControl> mFGSurfaceControl;
3652
3653 // This surface is used to ensure that the buffers posted to
3654 // mFGSurfaceControl have been picked up by SurfaceFlinger.
3655 sp<SurfaceControl> mSyncSurfaceControl;
3656};
3657
Robert Carr7f619b22017-11-06 12:56:35 -08003658TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08003659
chaviw0e3479f2018-09-10 16:49:30 -07003660 std::unique_ptr<ScreenCapture> sc;
3661
3662 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08003663 fillSurfaceRGBA8(relative, 10, 10, 10);
3664 waitForPostedBuffers();
3665
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003666 Transaction{}
3667 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08003668 .setPosition(relative, 64, 64)
3669 .apply();
3670
3671 {
3672 // The relative should be on top of the FG control.
3673 ScreenCapture::captureScreen(&sc);
3674 sc->checkPixel(64, 64, 10, 10, 10);
3675 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003676 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003677
3678 {
3679 // Nothing should change at this point.
3680 ScreenCapture::captureScreen(&sc);
3681 sc->checkPixel(64, 64, 10, 10, 10);
3682 }
3683
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003684 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003685
3686 {
3687 // Ensure that the relative was actually hidden, rather than
3688 // being left in the detached but visible state.
3689 ScreenCapture::captureScreen(&sc);
3690 sc->expectFGColor(64, 64);
3691 }
3692}
3693
Robert Carr8d5227b2017-03-16 15:41:03 -07003694class GeometryLatchingTest : public LayerUpdateTest {
3695protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003696 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07003697 SCOPED_TRACE(trace);
3698 ScreenCapture::captureScreen(&sc);
3699 // We find the leading edge of the FG surface.
3700 sc->expectFGColor(127, 127);
3701 sc->expectBGColor(128, 128);
3702 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003703
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003704 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07003705
3706 void unlockFGBuffer() {
3707 sp<Surface> s = mFGSurfaceControl->getSurface();
3708 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
3709 waitForPostedBuffers();
3710 }
3711
Robert Carr8d5227b2017-03-16 15:41:03 -07003712 void completeFGResize() {
3713 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3714 waitForPostedBuffers();
3715 }
3716 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003717 asTransaction([&](Transaction& t) {
3718 t.setSize(mFGSurfaceControl, 64, 64);
3719 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003720 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003721 });
Robert Carr8d5227b2017-03-16 15:41:03 -07003722
3723 EXPECT_INITIAL_STATE("After restoring initial state");
3724 }
chaviw0e3479f2018-09-10 16:49:30 -07003725 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07003726};
3727
Robert Carr8d5227b2017-03-16 15:41:03 -07003728class CropLatchingTest : public GeometryLatchingTest {
3729protected:
3730 void EXPECT_CROPPED_STATE(const char* trace) {
3731 SCOPED_TRACE(trace);
3732 ScreenCapture::captureScreen(&sc);
3733 // The edge should be moved back one pixel by our crop.
3734 sc->expectFGColor(126, 126);
3735 sc->expectBGColor(127, 127);
3736 sc->expectBGColor(128, 128);
3737 }
chaviw59f5c562017-06-28 16:39:06 -07003738
3739 void EXPECT_RESIZE_STATE(const char* trace) {
3740 SCOPED_TRACE(trace);
3741 ScreenCapture::captureScreen(&sc);
3742 // The FG is now resized too 128,128 at 64,64
3743 sc->expectFGColor(64, 64);
3744 sc->expectFGColor(191, 191);
3745 sc->expectBGColor(192, 192);
3746 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003747};
3748
Pablo Ceballos05289c22016-04-14 15:49:55 -07003749TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003750 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003751 {
3752 SCOPED_TRACE("before anything");
3753 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003754 sc->expectBGColor(32, 32);
3755 sc->expectFGColor(96, 96);
3756 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003757 }
3758
3759 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003760 asTransaction([&](Transaction& t) {
3761 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003762 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3763 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003764 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003765
Robert Carr4cdc58f2017-08-23 14:22:20 -07003766 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003767 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003768 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3769 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003770 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003771
3772 {
3773 SCOPED_TRACE("before any trigger");
3774 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003775 sc->expectBGColor(32, 32);
3776 sc->expectFGColor(96, 96);
3777 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003778 }
3779
3780 // should trigger the first deferred transaction, but not the second one
3781 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3782 {
3783 SCOPED_TRACE("after first trigger");
3784 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003785 sc->expectBGColor(32, 32);
3786 sc->checkPixel(96, 96, 162, 63, 96);
3787 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003788 }
3789
3790 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003791 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003792
3793 // trigger the second deferred transaction
3794 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3795 {
3796 SCOPED_TRACE("after second trigger");
3797 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003798 sc->expectBGColor(32, 32);
3799 sc->expectBGColor(96, 96);
3800 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003801 }
3802}
3803
Robert Carre392b552017-09-19 12:16:05 -07003804TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003805 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003806
3807 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003808 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3809 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3810 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3811 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003812 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003813 SurfaceComposerClient::Transaction{}
3814 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3815 .show(childNoBuffer)
3816 .show(childBuffer)
3817 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003818 {
3819 ScreenCapture::captureScreen(&sc);
3820 sc->expectChildColor(73, 73);
3821 sc->expectFGColor(74, 74);
3822 }
Vishnu Nair60356342018-11-13 13:00:45 -08003823 SurfaceComposerClient::Transaction{}
3824 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3825 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003826 {
3827 ScreenCapture::captureScreen(&sc);
3828 sc->expectChildColor(73, 73);
3829 sc->expectChildColor(74, 74);
3830 }
3831}
3832
Robert Carr2c5f6d22017-09-26 12:30:35 -07003833TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003834 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003835 {
3836 SCOPED_TRACE("before move");
3837 ScreenCapture::captureScreen(&sc);
3838 sc->expectBGColor(0, 12);
3839 sc->expectFGColor(75, 75);
3840 sc->expectBGColor(145, 145);
3841 }
3842
3843 Transaction t1, t2;
3844 t1.setPosition(mFGSurfaceControl, 128, 128);
3845 t2.setPosition(mFGSurfaceControl, 0, 0);
3846 // We expect that the position update from t2 now
3847 // overwrites the position update from t1.
3848 t1.merge(std::move(t2));
3849 t1.apply();
3850
3851 {
3852 ScreenCapture::captureScreen(&sc);
3853 sc->expectFGColor(1, 1);
3854 }
3855}
3856
Robert Carr1f0a16a2016-10-24 16:27:39 -07003857class ChildLayerTest : public LayerUpdateTest {
3858protected:
3859 void SetUp() override {
3860 LayerUpdateTest::SetUp();
Vishnu Nair88a11f22018-11-28 18:30:57 -08003861 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
3862 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003863 fillSurfaceRGBA8(mChild, 200, 200, 200);
3864
3865 {
3866 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003867 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003868 mCapture->expectChildColor(64, 64);
3869 }
3870 }
3871 void TearDown() override {
3872 LayerUpdateTest::TearDown();
3873 mChild = 0;
3874 }
3875
3876 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003877 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003878};
3879
3880TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003881 asTransaction([&](Transaction& t) {
3882 t.show(mChild);
3883 t.setPosition(mChild, 10, 10);
3884 t.setPosition(mFGSurfaceControl, 64, 64);
3885 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003886
3887 {
chaviw0e3479f2018-09-10 16:49:30 -07003888 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003889 // Top left of foreground must now be visible
3890 mCapture->expectFGColor(64, 64);
3891 // But 10 pixels in we should see the child surface
3892 mCapture->expectChildColor(74, 74);
3893 // And 10 more pixels we should be back to the foreground surface
3894 mCapture->expectFGColor(84, 84);
3895 }
3896
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003897 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003898
3899 {
chaviw0e3479f2018-09-10 16:49:30 -07003900 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003901 // Top left of foreground should now be at 0, 0
3902 mCapture->expectFGColor(0, 0);
3903 // But 10 pixels in we should see the child surface
3904 mCapture->expectChildColor(10, 10);
3905 // And 10 more pixels we should be back to the foreground surface
3906 mCapture->expectFGColor(20, 20);
3907 }
3908}
3909
Robert Carr41b08b52017-06-01 16:11:34 -07003910TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003911 asTransaction([&](Transaction& t) {
3912 t.show(mChild);
3913 t.setPosition(mChild, 0, 0);
3914 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003915 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003916 });
Robert Carr41b08b52017-06-01 16:11:34 -07003917
3918 {
chaviw0e3479f2018-09-10 16:49:30 -07003919 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07003920 mCapture->expectChildColor(0, 0);
3921 mCapture->expectChildColor(4, 4);
3922 mCapture->expectBGColor(5, 5);
3923 }
3924}
3925
Robert Carr1f0a16a2016-10-24 16:27:39 -07003926TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003927 asTransaction([&](Transaction& t) {
3928 t.show(mChild);
3929 t.setPosition(mFGSurfaceControl, 0, 0);
3930 t.setPosition(mChild, 63, 63);
3931 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003932
3933 {
chaviw0e3479f2018-09-10 16:49:30 -07003934 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003935 mCapture->expectFGColor(0, 0);
3936 // Last pixel in foreground should now be the child.
3937 mCapture->expectChildColor(63, 63);
3938 // But the child should be constrained and the next pixel
3939 // must be the background
3940 mCapture->expectBGColor(64, 64);
3941 }
3942}
3943
3944TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003945 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003946
3947 // Find the boundary between the parent and child
3948 {
chaviw0e3479f2018-09-10 16:49:30 -07003949 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003950 mCapture->expectChildColor(9, 9);
3951 mCapture->expectFGColor(10, 10);
3952 }
3953
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003954 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003955
3956 // The boundary should be twice as far from the origin now.
3957 // The pixels from the last test should all be child now
3958 {
chaviw0e3479f2018-09-10 16:49:30 -07003959 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003960 mCapture->expectChildColor(9, 9);
3961 mCapture->expectChildColor(10, 10);
3962 mCapture->expectChildColor(19, 19);
3963 mCapture->expectFGColor(20, 20);
3964 }
3965}
Robert Carr9524cb32017-02-13 11:32:32 -08003966
Robert Carr6452f122017-03-21 10:41:29 -07003967TEST_F(ChildLayerTest, ChildLayerAlpha) {
3968 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
3969 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
3970 fillSurfaceRGBA8(mChild, 0, 254, 0);
3971 waitForPostedBuffers();
3972
Robert Carr4cdc58f2017-08-23 14:22:20 -07003973 asTransaction([&](Transaction& t) {
3974 t.show(mChild);
3975 t.setPosition(mChild, 0, 0);
3976 t.setPosition(mFGSurfaceControl, 0, 0);
3977 });
Robert Carr6452f122017-03-21 10:41:29 -07003978
3979 {
chaviw0e3479f2018-09-10 16:49:30 -07003980 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003981 // Unblended child color
3982 mCapture->checkPixel(0, 0, 0, 254, 0);
3983 }
3984
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003985 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003986
3987 {
chaviw0e3479f2018-09-10 16:49:30 -07003988 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003989 // Child and BG blended.
3990 mCapture->checkPixel(0, 0, 127, 127, 0);
3991 }
3992
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003993 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003994
3995 {
chaviw0e3479f2018-09-10 16:49:30 -07003996 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003997 // Child and BG blended.
3998 mCapture->checkPixel(0, 0, 95, 64, 95);
3999 }
4000}
4001
Robert Carr9524cb32017-02-13 11:32:32 -08004002TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004003 asTransaction([&](Transaction& t) {
4004 t.show(mChild);
4005 t.setPosition(mChild, 10, 10);
4006 t.setPosition(mFGSurfaceControl, 64, 64);
4007 });
Robert Carr9524cb32017-02-13 11:32:32 -08004008
4009 {
chaviw0e3479f2018-09-10 16:49:30 -07004010 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004011 // Top left of foreground must now be visible
4012 mCapture->expectFGColor(64, 64);
4013 // But 10 pixels in we should see the child surface
4014 mCapture->expectChildColor(74, 74);
4015 // And 10 more pixels we should be back to the foreground surface
4016 mCapture->expectFGColor(84, 84);
4017 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004018
4019 asTransaction([&](Transaction& t) {
4020 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
4021 });
4022
Robert Carr9524cb32017-02-13 11:32:32 -08004023 {
chaviw0e3479f2018-09-10 16:49:30 -07004024 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004025 mCapture->expectFGColor(64, 64);
4026 // In reparenting we should have exposed the entire foreground surface.
4027 mCapture->expectFGColor(74, 74);
4028 // And the child layer should now begin at 10, 10 (since the BG
4029 // layer is at (0, 0)).
4030 mCapture->expectBGColor(9, 9);
4031 mCapture->expectChildColor(10, 10);
4032 }
4033}
4034
Robert Carr2e102c92018-10-23 12:11:15 -07004035TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
4036 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004037 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07004038 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
4039
4040 {
4041 SCOPED_TRACE("Grandchild visible");
4042 ScreenCapture::captureScreen(&mCapture);
4043 mCapture->checkPixel(64, 64, 111, 111, 111);
4044 }
4045
4046 mChild->clear();
4047
4048 {
4049 SCOPED_TRACE("After destroying child");
4050 ScreenCapture::captureScreen(&mCapture);
4051 mCapture->expectFGColor(64, 64);
4052 }
4053
4054 asTransaction([&](Transaction& t) {
4055 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
4056 });
4057
4058 {
4059 SCOPED_TRACE("After reparenting grandchild");
4060 ScreenCapture::captureScreen(&mCapture);
4061 mCapture->checkPixel(64, 64, 111, 111, 111);
4062 }
4063}
4064
chaviw161410b02017-07-27 10:46:08 -07004065TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004066 asTransaction([&](Transaction& t) {
4067 t.show(mChild);
4068 t.setPosition(mChild, 10, 10);
4069 t.setPosition(mFGSurfaceControl, 64, 64);
4070 });
Robert Carr9524cb32017-02-13 11:32:32 -08004071
4072 {
chaviw0e3479f2018-09-10 16:49:30 -07004073 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004074 // Top left of foreground must now be visible
4075 mCapture->expectFGColor(64, 64);
4076 // But 10 pixels in we should see the child surface
4077 mCapture->expectChildColor(74, 74);
4078 // And 10 more pixels we should be back to the foreground surface
4079 mCapture->expectFGColor(84, 84);
4080 }
4081
chaviw0e3479f2018-09-10 16:49:30 -07004082
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004083 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08004084
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004085 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08004086
chaviw161410b02017-07-27 10:46:08 -07004087 // Since the child has the same client as the parent, it will not get
4088 // detached and will be hidden.
4089 {
chaviw0e3479f2018-09-10 16:49:30 -07004090 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004091 mCapture->expectFGColor(64, 64);
4092 mCapture->expectFGColor(74, 74);
4093 mCapture->expectFGColor(84, 84);
4094 }
4095}
4096
4097TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
4098 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004099 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004100 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
4101 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07004102
chaviw161410b02017-07-27 10:46:08 -07004103 ASSERT_TRUE(mChildNewClient->isValid());
4104
4105 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
4106
Robert Carr4cdc58f2017-08-23 14:22:20 -07004107 asTransaction([&](Transaction& t) {
4108 t.hide(mChild);
4109 t.show(mChildNewClient);
4110 t.setPosition(mChildNewClient, 10, 10);
4111 t.setPosition(mFGSurfaceControl, 64, 64);
4112 });
chaviw161410b02017-07-27 10:46:08 -07004113
4114 {
chaviw0e3479f2018-09-10 16:49:30 -07004115 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004116 // Top left of foreground must now be visible
4117 mCapture->expectFGColor(64, 64);
4118 // But 10 pixels in we should see the child surface
4119 mCapture->expectChildColor(74, 74);
4120 // And 10 more pixels we should be back to the foreground surface
4121 mCapture->expectFGColor(84, 84);
4122 }
4123
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004124 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07004125
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004126 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07004127
Robert Carr9524cb32017-02-13 11:32:32 -08004128 // Nothing should have changed.
4129 {
chaviw0e3479f2018-09-10 16:49:30 -07004130 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004131 mCapture->expectFGColor(64, 64);
4132 mCapture->expectChildColor(74, 74);
4133 mCapture->expectFGColor(84, 84);
4134 }
4135}
4136
chaviw5aedec92018-10-22 10:40:38 -07004137TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
4138 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4139 sp<SurfaceControl> childNewClient =
4140 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4141 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4142
4143 ASSERT_TRUE(childNewClient != nullptr);
4144 ASSERT_TRUE(childNewClient->isValid());
4145
4146 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4147
4148 Transaction()
4149 .hide(mChild)
4150 .show(childNewClient)
4151 .setPosition(childNewClient, 10, 10)
4152 .setPosition(mFGSurfaceControl, 64, 64)
4153 .apply();
4154
4155 {
4156 mCapture = screenshot();
4157 // Top left of foreground must now be visible
4158 mCapture->expectFGColor(64, 64);
4159 // But 10 pixels in we should see the child surface
4160 mCapture->expectChildColor(74, 74);
4161 // And 10 more pixels we should be back to the foreground surface
4162 mCapture->expectFGColor(84, 84);
4163 }
4164
4165 Transaction().detachChildren(mFGSurfaceControl).apply();
4166 Transaction().hide(childNewClient).apply();
4167
4168 // Nothing should have changed.
4169 {
4170 mCapture = screenshot();
4171 mCapture->expectFGColor(64, 64);
4172 mCapture->expectChildColor(74, 74);
4173 mCapture->expectFGColor(84, 84);
4174 }
4175
4176 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
4177 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
4178 32);
4179 Transaction()
4180 .setLayer(newParentSurface, INT32_MAX - 1)
4181 .show(newParentSurface)
4182 .setPosition(newParentSurface, 20, 20)
4183 .reparent(childNewClient, newParentSurface->getHandle())
4184 .apply();
4185 {
4186 mCapture = screenshot();
4187 // Child is now hidden.
4188 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
4189 }
4190}
4191
Robert Carr9b429f42017-04-17 14:56:57 -07004192TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004193 asTransaction([&](Transaction& t) {
4194 t.show(mChild);
4195 t.setPosition(mChild, 0, 0);
4196 t.setPosition(mFGSurfaceControl, 0, 0);
4197 });
Robert Carr9b429f42017-04-17 14:56:57 -07004198
4199 {
chaviw0e3479f2018-09-10 16:49:30 -07004200 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004201 // We've positioned the child in the top left.
4202 mCapture->expectChildColor(0, 0);
4203 // But it's only 10x10.
4204 mCapture->expectFGColor(10, 10);
4205 }
4206
Robert Carr4cdc58f2017-08-23 14:22:20 -07004207 asTransaction([&](Transaction& t) {
4208 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4209 // We cause scaling by 2.
4210 t.setSize(mFGSurfaceControl, 128, 128);
4211 });
Robert Carr9b429f42017-04-17 14:56:57 -07004212
4213 {
chaviw0e3479f2018-09-10 16:49:30 -07004214 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004215 // We've positioned the child in the top left.
4216 mCapture->expectChildColor(0, 0);
4217 mCapture->expectChildColor(10, 10);
4218 mCapture->expectChildColor(19, 19);
4219 // And now it should be scaled all the way to 20x20
4220 mCapture->expectFGColor(20, 20);
4221 }
4222}
4223
Robert Carr1725eee2017-04-26 18:32:15 -07004224// Regression test for b/37673612
4225TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004226 asTransaction([&](Transaction& t) {
4227 t.show(mChild);
4228 t.setPosition(mChild, 0, 0);
4229 t.setPosition(mFGSurfaceControl, 0, 0);
4230 });
Robert Carr1725eee2017-04-26 18:32:15 -07004231
4232 {
chaviw0e3479f2018-09-10 16:49:30 -07004233 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004234 // We've positioned the child in the top left.
4235 mCapture->expectChildColor(0, 0);
4236 // But it's only 10x10.
4237 mCapture->expectFGColor(10, 10);
4238 }
Robert Carr1725eee2017-04-26 18:32:15 -07004239 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
4240 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004241 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07004242 sp<Surface> s = mFGSurfaceControl->getSurface();
4243 auto anw = static_cast<ANativeWindow*>(s.get());
4244 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4245 native_window_set_buffers_dimensions(anw, 64, 128);
4246 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4247 waitForPostedBuffers();
4248
4249 {
4250 // The child should still be in the same place and not have any strange scaling as in
4251 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07004252 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004253 mCapture->expectChildColor(0, 0);
4254 mCapture->expectFGColor(10, 10);
4255 }
4256}
4257
Dan Stoza412903f2017-04-27 13:42:17 -07004258TEST_F(ChildLayerTest, Bug36858924) {
4259 // Destroy the child layer
4260 mChild.clear();
4261
4262 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08004263 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
4264 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07004265
4266 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07004267 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004268 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
4269 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07004270 t.show(mChild);
4271 });
Dan Stoza412903f2017-04-27 13:42:17 -07004272
4273 // Render the foreground surface a few times
4274 //
4275 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
4276 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
4277 // never acquire/release the first buffer
4278 ALOGI("Filling 1");
4279 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
4280 ALOGI("Filling 2");
4281 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
4282 ALOGI("Filling 3");
4283 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
4284 ALOGI("Filling 4");
4285 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
4286}
4287
chaviwf1961f72017-09-18 16:41:07 -07004288TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004289 asTransaction([&](Transaction& t) {
4290 t.show(mChild);
4291 t.setPosition(mChild, 10, 10);
4292 t.setPosition(mFGSurfaceControl, 64, 64);
4293 });
chaviw06178942017-07-27 10:25:59 -07004294
4295 {
chaviw0e3479f2018-09-10 16:49:30 -07004296 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004297 // Top left of foreground must now be visible
4298 mCapture->expectFGColor(64, 64);
4299 // But 10 pixels in we should see the child surface
4300 mCapture->expectChildColor(74, 74);
4301 // And 10 more pixels we should be back to the foreground surface
4302 mCapture->expectFGColor(84, 84);
4303 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004304
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004305 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07004306
chaviw06178942017-07-27 10:25:59 -07004307 {
chaviw0e3479f2018-09-10 16:49:30 -07004308 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004309 mCapture->expectFGColor(64, 64);
4310 // In reparenting we should have exposed the entire foreground surface.
4311 mCapture->expectFGColor(74, 74);
4312 // And the child layer should now begin at 10, 10 (since the BG
4313 // layer is at (0, 0)).
4314 mCapture->expectBGColor(9, 9);
4315 mCapture->expectChildColor(10, 10);
4316 }
4317}
4318
chaviwf1961f72017-09-18 16:41:07 -07004319TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004320 asTransaction([&](Transaction& t) {
4321 t.show(mChild);
4322 t.setPosition(mChild, 10, 10);
4323 t.setPosition(mFGSurfaceControl, 64, 64);
4324 });
chaviwf1961f72017-09-18 16:41:07 -07004325
4326 {
chaviw0e3479f2018-09-10 16:49:30 -07004327 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004328 // Top left of foreground must now be visible
4329 mCapture->expectFGColor(64, 64);
4330 // But 10 pixels in we should see the child surface
4331 mCapture->expectChildColor(74, 74);
4332 // And 10 more pixels we should be back to the foreground surface
4333 mCapture->expectFGColor(84, 84);
4334 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004335 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07004336 {
chaviw0e3479f2018-09-10 16:49:30 -07004337 mCapture = screenshot();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004338 // The surface should now be offscreen.
chaviwf1961f72017-09-18 16:41:07 -07004339 mCapture->expectFGColor(64, 64);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004340 mCapture->expectFGColor(74, 74);
chaviwf1961f72017-09-18 16:41:07 -07004341 mCapture->expectFGColor(84, 84);
4342 }
4343}
4344
4345TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07004346 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08004347 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07004348 ASSERT_TRUE(newSurface->isValid());
4349
4350 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004351 asTransaction([&](Transaction& t) {
4352 t.hide(mChild);
4353 t.show(newSurface);
4354 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004355 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004356 t.setPosition(mFGSurfaceControl, 64, 64);
4357 });
chaviwf1961f72017-09-18 16:41:07 -07004358
4359 {
chaviw0e3479f2018-09-10 16:49:30 -07004360 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004361 // Top left of foreground must now be visible
4362 mCapture->expectFGColor(64, 64);
4363 // At 10, 10 we should see the new surface
4364 mCapture->checkPixel(10, 10, 63, 195, 63);
4365 }
4366
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004367 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07004368
4369 {
chaviw0e3479f2018-09-10 16:49:30 -07004370 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004371 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
4372 // mFGSurface, putting it at 74, 74.
4373 mCapture->expectFGColor(64, 64);
4374 mCapture->checkPixel(74, 74, 63, 195, 63);
4375 mCapture->expectFGColor(84, 84);
4376 }
4377}
4378
chaviwc9674332017-08-28 12:32:18 -07004379TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004380 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
4381 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07004382 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4383
4384 {
chaviw0e3479f2018-09-10 16:49:30 -07004385 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07004386 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
4387 // which begins at 64, 64
4388 mCapture->checkPixel(64, 64, 50, 50, 50);
4389 }
4390}
4391
Robert Carr503c7042017-09-27 15:06:08 -07004392TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004393 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07004394 fillSurfaceRGBA8(relative, 255, 255, 255);
4395
4396 Transaction t;
4397 t.setLayer(relative, INT32_MAX)
4398 .setRelativeLayer(mChild, relative->getHandle(), 1)
4399 .setPosition(mFGSurfaceControl, 0, 0)
4400 .apply(true);
4401
4402 // We expect that the child should have been elevated above our
4403 // INT_MAX layer even though it's not a child of it.
4404 {
chaviw0e3479f2018-09-10 16:49:30 -07004405 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07004406 mCapture->expectChildColor(0, 0);
4407 mCapture->expectChildColor(9, 9);
4408 mCapture->checkPixel(10, 10, 255, 255, 255);
4409 }
4410}
Vishnu Nair60356342018-11-13 13:00:45 -08004411class BoundlessLayerTest : public LayerUpdateTest {
4412protected:
4413 std::unique_ptr<ScreenCapture> mCapture;
4414};
4415
4416// Verify setting a size on a buffer layer has no effect.
4417TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
4418 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004419 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
4420 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004421 ASSERT_TRUE(bufferLayer->isValid());
4422 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
4423 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
4424 {
4425 mCapture = screenshot();
4426 // Top left of background must now be visible
4427 mCapture->expectBGColor(0, 0);
4428 // Foreground Surface bounds must be color layer
4429 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
4430 // Buffer layer should not extend past buffer bounds
4431 mCapture->expectFGColor(95, 95);
4432 }
4433}
4434
4435// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
4436// which will crop the color layer.
4437TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
4438 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004439 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4440 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004441 ASSERT_TRUE(colorLayer->isValid());
4442 asTransaction([&](Transaction& t) {
4443 t.setColor(colorLayer, half3{0, 0, 0});
4444 t.show(colorLayer);
4445 });
4446 {
4447 mCapture = screenshot();
4448 // Top left of background must now be visible
4449 mCapture->expectBGColor(0, 0);
4450 // Foreground Surface bounds must be color layer
4451 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4452 // Color layer should not extend past foreground bounds
4453 mCapture->expectBGColor(129, 129);
4454 }
4455}
4456
4457// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
4458// a crop which will be used to crop the color layer.
4459TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004460 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4461 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004462 ASSERT_TRUE(cropLayer->isValid());
4463 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004464 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4465 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004466 ASSERT_TRUE(colorLayer->isValid());
4467 asTransaction([&](Transaction& t) {
4468 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
4469 t.setColor(colorLayer, half3{0, 0, 0});
4470 t.show(cropLayer);
4471 t.show(colorLayer);
4472 });
4473 {
4474 mCapture = screenshot();
4475 // Top left of background must now be visible
4476 mCapture->expectBGColor(0, 0);
4477 // Top left of foreground must now be visible
4478 mCapture->expectFGColor(64, 64);
4479 // 5 pixels from the foreground we should see the child surface
4480 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
4481 // 10 pixels from the foreground we should be back to the foreground surface
4482 mCapture->expectFGColor(74, 74);
4483 }
4484}
4485
4486// Verify for boundless layer with no children, their transforms have no effect.
4487TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
4488 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004489 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4490 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004491 ASSERT_TRUE(colorLayer->isValid());
4492 asTransaction([&](Transaction& t) {
4493 t.setPosition(colorLayer, 320, 320);
4494 t.setMatrix(colorLayer, 2, 0, 0, 2);
4495 t.setColor(colorLayer, half3{0, 0, 0});
4496 t.show(colorLayer);
4497 });
4498 {
4499 mCapture = screenshot();
4500 // Top left of background must now be visible
4501 mCapture->expectBGColor(0, 0);
4502 // Foreground Surface bounds must be color layer
4503 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4504 // Color layer should not extend past foreground bounds
4505 mCapture->expectBGColor(129, 129);
4506 }
4507}
4508
4509// Verify for boundless layer with children, their transforms have an effect.
4510TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
4511 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004512 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4513 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004514 ASSERT_TRUE(boundlessLayerRightShift->isValid());
4515 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004516 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4517 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004518 ASSERT_TRUE(boundlessLayerDownShift->isValid());
4519 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004520 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4521 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004522 ASSERT_TRUE(colorLayer->isValid());
4523 asTransaction([&](Transaction& t) {
4524 t.setPosition(boundlessLayerRightShift, 32, 0);
4525 t.show(boundlessLayerRightShift);
4526 t.setPosition(boundlessLayerDownShift, 0, 32);
4527 t.show(boundlessLayerDownShift);
4528 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4529 t.setColor(colorLayer, half3{0, 0, 0});
4530 t.show(colorLayer);
4531 });
4532 {
4533 mCapture = screenshot();
4534 // Top left of background must now be visible
4535 mCapture->expectBGColor(0, 0);
4536 // Top left of foreground must now be visible
4537 mCapture->expectFGColor(64, 64);
4538 // Foreground Surface bounds must be color layer
4539 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
4540 // Color layer should not extend past foreground bounds
4541 mCapture->expectBGColor(129, 129);
4542 }
4543}
4544
4545// Verify child layers do not get clipped if they temporarily move into the negative
4546// coordinate space as the result of an intermediate transformation.
4547TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
4548 sp<SurfaceControl> boundlessLayer =
4549 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4550 0 /* flags */, mFGSurfaceControl.get());
4551 ASSERT_TRUE(boundlessLayer != nullptr);
4552 ASSERT_TRUE(boundlessLayer->isValid());
4553 sp<SurfaceControl> colorLayer =
4554 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4555 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
4556 ASSERT_TRUE(colorLayer != nullptr);
4557 ASSERT_TRUE(colorLayer->isValid());
4558 asTransaction([&](Transaction& t) {
4559 // shift child layer off bounds. If this layer was not boundless, we will
4560 // expect the child layer to be cropped.
4561 t.setPosition(boundlessLayer, 32, 32);
4562 t.show(boundlessLayer);
4563 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4564 // undo shift by parent
4565 t.setPosition(colorLayer, -32, -32);
4566 t.setColor(colorLayer, half3{0, 0, 0});
4567 t.show(colorLayer);
4568 });
4569 {
4570 mCapture = screenshot();
4571 // Top left of background must now be visible
4572 mCapture->expectBGColor(0, 0);
4573 // Foreground Surface bounds must be color layer
4574 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4575 // Color layer should not extend past foreground bounds
4576 mCapture->expectBGColor(129, 129);
4577 }
4578}
4579
4580// Verify for boundless root layers with children, their transforms have an effect.
4581TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004582 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
4583 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08004584 ASSERT_TRUE(rootBoundlessLayer->isValid());
4585 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004586 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4587 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
4588
Vishnu Nair60356342018-11-13 13:00:45 -08004589 ASSERT_TRUE(colorLayer->isValid());
4590 asTransaction([&](Transaction& t) {
4591 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
4592 t.setPosition(rootBoundlessLayer, 32, 32);
4593 t.show(rootBoundlessLayer);
4594 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4595 t.setColor(colorLayer, half3{0, 0, 0});
4596 t.show(colorLayer);
4597 t.hide(mFGSurfaceControl);
4598 });
4599 {
4600 mCapture = screenshot();
4601 // Top left of background must now be visible
4602 mCapture->expectBGColor(0, 0);
4603 // Top left of foreground must now be visible
4604 mCapture->expectBGColor(31, 31);
4605 // Foreground Surface bounds must be color layer
4606 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
4607 // Color layer should not extend past foreground bounds
4608 mCapture->expectBGColor(97, 97);
4609 }
4610}
Robert Carr503c7042017-09-27 15:06:08 -07004611
chaviwa76b2712017-09-20 12:02:26 -07004612class ScreenCaptureTest : public LayerUpdateTest {
4613protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004614 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07004615};
4616
4617TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
4618 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004619 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004620 mCapture->expectBGColor(0, 0);
4621 // Doesn't capture FG layer which is at 64, 64
4622 mCapture->expectBGColor(64, 64);
4623}
4624
4625TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
4626 auto fgHandle = mFGSurfaceControl->getHandle();
4627
Vishnu Nair88a11f22018-11-28 18:30:57 -08004628 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4629 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004630 fillSurfaceRGBA8(child, 200, 200, 200);
4631
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004632 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004633
4634 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004635 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004636 mCapture->expectFGColor(10, 10);
4637 mCapture->expectChildColor(0, 0);
4638}
4639
Robert Carr578038f2018-03-09 12:25:24 -08004640TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
4641 auto fgHandle = mFGSurfaceControl->getHandle();
4642
Vishnu Nair88a11f22018-11-28 18:30:57 -08004643 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4644 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004645 fillSurfaceRGBA8(child, 200, 200, 200);
4646
4647 SurfaceComposerClient::Transaction().show(child).apply(true);
4648
4649 // Captures mFGSurfaceControl's child
4650 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4651 mCapture->checkPixel(10, 10, 0, 0, 0);
4652 mCapture->expectChildColor(0, 0);
4653}
4654
chaviw50da5042018-04-09 13:49:37 -07004655TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004656 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4657 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07004658
4659 fillSurfaceRGBA8(child, 200, 200, 200);
4660
4661 SurfaceComposerClient::Transaction().show(child).apply(true);
4662
4663 auto childHandle = child->getHandle();
4664
4665 // Captures child
4666 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
4667 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
4668 // Area outside of child's bounds is transparent.
4669 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
4670}
4671
chaviw4b129c22018-04-09 16:19:43 -07004672TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
4673 auto fgHandle = mFGSurfaceControl->getHandle();
4674
Vishnu Nair88a11f22018-11-28 18:30:57 -08004675 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4676 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4677 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07004678 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07004679 fillSurfaceRGBA8(child, 200, 200, 200);
4680 fillSurfaceRGBA8(relative, 100, 100, 100);
4681
4682 SurfaceComposerClient::Transaction()
4683 .show(child)
4684 // Set relative layer above fg layer so should be shown above when computing all layers.
4685 .setRelativeLayer(relative, fgHandle, 1)
4686 .show(relative)
4687 .apply(true);
4688
4689 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
4690 ScreenCapture::captureLayers(&mCapture, fgHandle);
4691 mCapture->expectFGColor(10, 10);
4692 mCapture->expectChildColor(0, 0);
4693}
4694
4695TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
4696 auto fgHandle = mFGSurfaceControl->getHandle();
4697
Vishnu Nair88a11f22018-11-28 18:30:57 -08004698 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4699 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4700 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
4701 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07004702 fillSurfaceRGBA8(child, 200, 200, 200);
4703 fillSurfaceRGBA8(relative, 100, 100, 100);
4704
4705 SurfaceComposerClient::Transaction()
4706 .show(child)
4707 // Set relative layer below fg layer but relative to child layer so it should be shown
4708 // above child layer.
4709 .setLayer(relative, -1)
4710 .setRelativeLayer(relative, child->getHandle(), 1)
4711 .show(relative)
4712 .apply(true);
4713
4714 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
4715 // relative value should be taken into account, placing it above child layer.
4716 ScreenCapture::captureLayers(&mCapture, fgHandle);
4717 mCapture->expectFGColor(10, 10);
4718 // Relative layer is showing on top of child layer
4719 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
4720}
Robert Carr578038f2018-03-09 12:25:24 -08004721
4722// In the following tests we verify successful skipping of a parent layer,
4723// so we use the same verification logic and only change how we mutate
4724// the parent layer to verify that various properties are ignored.
4725class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
4726public:
4727 void SetUp() override {
4728 LayerUpdateTest::SetUp();
4729
Vishnu Nair88a11f22018-11-28 18:30:57 -08004730 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
4731 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004732 fillSurfaceRGBA8(mChild, 200, 200, 200);
4733
4734 SurfaceComposerClient::Transaction().show(mChild).apply(true);
4735 }
4736
4737 void verify() {
4738 auto fgHandle = mFGSurfaceControl->getHandle();
4739 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4740 mCapture->checkPixel(10, 10, 0, 0, 0);
4741 mCapture->expectChildColor(0, 0);
4742 }
4743
4744 std::unique_ptr<ScreenCapture> mCapture;
4745 sp<SurfaceControl> mChild;
4746};
4747
4748TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
4749
4750 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4751
4752 // Even though the parent is hidden we should still capture the child.
4753 verify();
4754}
4755
4756TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004757 SurfaceComposerClient::Transaction()
4758 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
4759 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08004760
4761 // Even though the parent is cropped out we should still capture the child.
4762 verify();
4763}
4764
4765TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
4766
4767 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
4768
4769 // We should not inherit the parent scaling.
4770 verify();
4771}
4772
Robert Carr15eae092018-03-23 13:43:53 -07004773TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
4774 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4775
4776 // Even though the parent is hidden we should still capture the child.
4777 verify();
4778
4779 // Verify everything was properly hidden when rendering the full-screen.
4780 screenshot()->expectBGColor(0,0);
4781}
4782
4783
chaviwa76b2712017-09-20 12:02:26 -07004784TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
4785 auto fgHandle = mFGSurfaceControl->getHandle();
4786
Vishnu Nair88a11f22018-11-28 18:30:57 -08004787 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4788 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004789 fillSurfaceRGBA8(child, 200, 200, 200);
4790
Vishnu Nair88a11f22018-11-28 18:30:57 -08004791 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4792 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004793
4794 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4795 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004796 .show(child)
4797 .setPosition(grandchild, 5, 5)
4798 .show(grandchild)
4799 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004800
4801 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004802 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004803 mCapture->expectFGColor(10, 10);
4804 mCapture->expectChildColor(0, 0);
4805 mCapture->checkPixel(5, 5, 50, 50, 50);
4806}
4807
4808TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004809 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4810 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004811 fillSurfaceRGBA8(child, 200, 200, 200);
4812 auto childHandle = child->getHandle();
4813
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004814 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004815
4816 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004817 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07004818 mCapture->expectChildColor(0, 0);
4819 mCapture->expectChildColor(9, 9);
4820}
4821
4822TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004823 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4824 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004825 fillSurfaceRGBA8(child, 200, 200, 200);
4826 auto childHandle = child->getHandle();
4827
Vishnu Nair88a11f22018-11-28 18:30:57 -08004828 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4829 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004830 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4831
4832 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004833 .show(child)
4834 .setPosition(grandchild, 5, 5)
4835 .show(grandchild)
4836 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004837
4838 auto grandchildHandle = grandchild->getHandle();
4839
4840 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004841 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07004842 mCapture->checkPixel(0, 0, 50, 50, 50);
4843 mCapture->checkPixel(4, 4, 50, 50, 50);
4844}
4845
chaviw7206d492017-11-10 16:16:12 -08004846TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07004847 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004848 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4849 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004850
Marissa Wall61c58622018-07-18 10:12:20 -07004851 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4852 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004853
4854 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004855 .setLayer(redLayer, INT32_MAX - 1)
4856 .show(redLayer)
4857 .show(blueLayer)
4858 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004859
4860 auto redLayerHandle = redLayer->getHandle();
4861
4862 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004863 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4864 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4865 // red area below the blue area
4866 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4867 // red area to the right of the blue area
4868 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004869
Marissa Wall861616d2018-10-22 12:52:23 -07004870 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004871 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08004872 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
4873 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004874 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08004875 mCapture->checkPixel(30, 30, 0, 0, 0);
4876}
4877
4878TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07004879 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004880 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4881 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004882
Marissa Wall61c58622018-07-18 10:12:20 -07004883 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4884 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004885
4886 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004887 .setLayer(redLayer, INT32_MAX - 1)
4888 .show(redLayer)
4889 .show(blueLayer)
4890 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004891
4892 auto redLayerHandle = redLayer->getHandle();
4893
4894 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004895 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4896 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4897 // red area below the blue area
4898 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4899 // red area to the right of the blue area
4900 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004901
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004902 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08004903 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004904 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
4905 // red area below the blue area
4906 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
4907 // red area to the right of the blue area
4908 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004909 mCapture->checkPixel(30, 30, 0, 0, 0);
4910}
4911
4912TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004913 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08004914
Marissa Wall61c58622018-07-18 10:12:20 -07004915 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08004916
4917 auto redLayerHandle = redLayer->getHandle();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004918 redLayer->clear();
chaviw7206d492017-11-10 16:16:12 -08004919 SurfaceComposerClient::Transaction().apply(true);
4920
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004921 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08004922
4923 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004924 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
4925 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08004926}
4927
chaviw8e3fe5d2018-02-22 10:55:42 -08004928
4929class DereferenceSurfaceControlTest : public LayerTransactionTest {
4930protected:
4931 void SetUp() override {
4932 LayerTransactionTest::SetUp();
4933 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004934 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004935 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004936 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004937 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
4938 {
4939 SCOPED_TRACE("before anything");
4940 auto shot = screenshot();
4941 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4942 }
4943 }
4944 void TearDown() override {
4945 LayerTransactionTest::TearDown();
4946 bgLayer = 0;
4947 fgLayer = 0;
4948 }
4949
4950 sp<SurfaceControl> bgLayer;
4951 sp<SurfaceControl> fgLayer;
4952};
4953
4954TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
4955 fgLayer = nullptr;
4956 {
4957 SCOPED_TRACE("after setting null");
4958 auto shot = screenshot();
4959 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
4960 }
4961}
4962
4963TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
4964 auto transaction = Transaction().show(fgLayer);
4965 fgLayer = nullptr;
4966 {
4967 SCOPED_TRACE("after setting null");
4968 auto shot = screenshot();
4969 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4970 }
4971}
4972
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004973} // namespace android