blob: 396fc5557c5c44b14893e86e75ea84db392d623f [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
Robert Carr54cf5b12019-01-25 14:02:28 -08002463TEST_F(LayerTransactionTest, ReparentToSelf) {
2464 sp<SurfaceControl> layer;
2465 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2466 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2467 Transaction().reparent(layer, layer->getHandle()).apply();
2468
2469 {
2470 // We expect the transaction to be silently dropped, but for SurfaceFlinger
2471 // to still be functioning.
2472 SCOPED_TRACE("after reparent to self");
2473 const Rect rect(0, 0, 32, 32);
2474 auto shot = screenshot();
2475 shot->expectColor(rect, Color::RED);
2476 shot->expectBorder(rect, Color::BLACK);
2477 }
2478}
2479
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002480class ColorTransformHelper {
2481public:
2482 static void DegammaColorSingle(half& s) {
2483 if (s <= 0.03928f)
2484 s = s / 12.92f;
2485 else
2486 s = pow((s + 0.055f) / 1.055f, 2.4f);
2487 }
2488
2489 static void DegammaColor(half3& color) {
2490 DegammaColorSingle(color.r);
2491 DegammaColorSingle(color.g);
2492 DegammaColorSingle(color.b);
2493 }
2494
2495 static void GammaColorSingle(half& s) {
2496 if (s <= 0.0031308f) {
2497 s = s * 12.92f;
2498 } else {
2499 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2500 }
2501 }
2502
2503 static void GammaColor(half3& color) {
2504 GammaColorSingle(color.r);
2505 GammaColorSingle(color.g);
2506 GammaColorSingle(color.b);
2507 }
2508
2509 static void applyMatrix(half3& color, const mat3& mat) {
2510 half3 ret = half3(0);
2511
2512 for (int i = 0; i < 3; i++) {
2513 for (int j = 0; j < 3; j++) {
2514 ret[i] = ret[i] + color[j] * mat[j][i];
2515 }
2516 }
2517 color = ret;
2518 }
2519};
2520
Peiyong Lind3788632018-09-18 16:01:31 -07002521TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2522 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002523 ASSERT_NO_FATAL_FAILURE(colorLayer =
2524 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2525 ISurfaceComposerClient::eFXSurfaceColor));
2526 Transaction()
2527 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2528 .setLayer(colorLayer, mLayerZBase + 1)
2529 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002530 {
2531 SCOPED_TRACE("default color");
2532 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2533 }
2534
2535 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002536 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002537 mat3 matrix;
2538 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2539 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2540 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002541
2542 // degamma before applying the matrix
2543 if (mColorManagementUsed) {
2544 ColorTransformHelper::DegammaColor(expected);
2545 }
2546
2547 ColorTransformHelper::applyMatrix(expected, matrix);
2548
2549 if (mColorManagementUsed) {
2550 ColorTransformHelper::GammaColor(expected);
2551 }
2552
2553 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2554 uint8_t(expected.b * 255), 255};
2555
2556 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2557 // channel) should be less than one
2558 const uint8_t tolerance = 1;
2559
Peiyong Lind3788632018-09-18 16:01:31 -07002560 Transaction().setColor(colorLayer, color)
2561 .setColorTransform(colorLayer, matrix, vec3()).apply();
2562 {
2563 SCOPED_TRACE("new color");
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002564 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002565 }
2566}
2567
chaviwf66724d2018-11-28 16:35:21 -08002568TEST_F(LayerTransactionTest, SetColorTransformOnParent) {
2569 sp<SurfaceControl> parentLayer;
2570 sp<SurfaceControl> colorLayer;
2571 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2572 0 /* buffer height */,
2573 ISurfaceComposerClient::eFXSurfaceContainer));
2574 ASSERT_NO_FATAL_FAILURE(
2575 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2576 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2577
2578 Transaction()
2579 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2580 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2581 .setLayer(parentLayer, mLayerZBase + 1)
2582 .apply();
2583 {
2584 SCOPED_TRACE("default color");
2585 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2586 }
2587
2588 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2589 half3 expected = color;
2590 mat3 matrix;
2591 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2592 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2593 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2594
2595 // degamma before applying the matrix
2596 if (mColorManagementUsed) {
2597 ColorTransformHelper::DegammaColor(expected);
2598 }
2599
2600 ColorTransformHelper::applyMatrix(expected, matrix);
2601
2602 if (mColorManagementUsed) {
2603 ColorTransformHelper::GammaColor(expected);
2604 }
2605
2606 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2607 uint8_t(expected.b * 255), 255};
2608
2609 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2610 // channel) should be less than one
2611 const uint8_t tolerance = 1;
2612
2613 Transaction()
2614 .setColor(colorLayer, color)
2615 .setColorTransform(parentLayer, matrix, vec3())
2616 .apply();
2617 {
2618 SCOPED_TRACE("new color");
2619 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
2620 }
2621}
2622
2623TEST_F(LayerTransactionTest, SetColorTransformOnChildAndParent) {
2624 sp<SurfaceControl> parentLayer;
2625 sp<SurfaceControl> colorLayer;
2626 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2627 0 /* buffer height */,
2628 ISurfaceComposerClient::eFXSurfaceContainer));
2629 ASSERT_NO_FATAL_FAILURE(
2630 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2631 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2632
2633 Transaction()
2634 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2635 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2636 .setLayer(parentLayer, mLayerZBase + 1)
2637 .apply();
2638 {
2639 SCOPED_TRACE("default color");
2640 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2641 }
2642
2643 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2644 half3 expected = color;
2645 mat3 matrixChild;
2646 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
2647 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
2648 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
2649 mat3 matrixParent;
2650 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
2651 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
2652 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
2653
2654 // degamma before applying the matrix
2655 if (mColorManagementUsed) {
2656 ColorTransformHelper::DegammaColor(expected);
2657 }
2658
2659 ColorTransformHelper::applyMatrix(expected, matrixChild);
2660 ColorTransformHelper::applyMatrix(expected, matrixParent);
2661
2662 if (mColorManagementUsed) {
2663 ColorTransformHelper::GammaColor(expected);
2664 }
2665
2666 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2667 uint8_t(expected.b * 255), 255};
2668
2669 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2670 // channel) should be less than one
2671 const uint8_t tolerance = 1;
2672
2673 Transaction()
2674 .setColor(colorLayer, color)
2675 .setColorTransform(parentLayer, matrixParent, vec3())
2676 .setColorTransform(colorLayer, matrixChild, vec3())
2677 .apply();
2678 {
2679 SCOPED_TRACE("new color");
2680 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
2681 }
2682}
2683
Marissa Wall80d94ad2019-01-18 16:04:36 -08002684struct CallbackData {
2685 CallbackData() = default;
2686 CallbackData(nsecs_t time, const sp<Fence>& fence,
2687 const std::vector<SurfaceControlStats>& stats)
2688 : latchTime(time), presentFence(fence), surfaceControlStats(stats) {}
2689
2690 nsecs_t latchTime;
2691 sp<Fence> presentFence;
2692 std::vector<SurfaceControlStats> surfaceControlStats;
2693};
2694
Marissa Wallfda30bb2018-10-12 11:34:28 -07002695class ExpectedResult {
2696public:
2697 enum Transaction {
2698 NOT_PRESENTED = 0,
2699 PRESENTED,
2700 };
2701
2702 enum Buffer {
2703 NOT_ACQUIRED = 0,
2704 ACQUIRED,
2705 };
2706
2707 enum PreviousBuffer {
2708 NOT_RELEASED = 0,
2709 RELEASED,
Marissa Wall5a68a772018-12-22 17:43:42 -08002710 UNKNOWN,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002711 };
2712
2713 void reset() {
2714 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
2715 mExpectedSurfaceResults.clear();
2716 }
2717
2718 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07002719 ExpectedResult::Buffer bufferResult = ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002720 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2721 mTransactionResult = transactionResult;
Marissa Wall80d94ad2019-01-18 16:04:36 -08002722 mExpectedSurfaceResults.emplace(std::piecewise_construct, std::forward_as_tuple(layer),
Marissa Wallfda30bb2018-10-12 11:34:28 -07002723 std::forward_as_tuple(bufferResult, previousBufferResult));
2724 }
2725
2726 void addSurfaces(ExpectedResult::Transaction transactionResult,
2727 const std::vector<sp<SurfaceControl>>& layers,
Marissa Wall713b63f2018-10-17 15:42:43 -07002728 ExpectedResult::Buffer bufferResult = ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07002729 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
2730 for (const auto& layer : layers) {
2731 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
2732 }
2733 }
2734
Marissa Wall17b4e452018-12-26 16:32:34 -08002735 void addExpectedPresentTime(nsecs_t expectedPresentTime) {
2736 mExpectedPresentTime = expectedPresentTime;
2737 }
2738
Marissa Wall80d94ad2019-01-18 16:04:36 -08002739 void verifyCallbackData(const CallbackData& callbackData) const {
2740 const auto& [latchTime, presentFence, surfaceControlStats] = callbackData;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002741 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
2742 ASSERT_GE(latchTime, 0) << "bad latch time";
Valerie Hau63258a12018-12-14 14:31:48 -08002743 ASSERT_NE(presentFence, nullptr);
Marissa Wall17b4e452018-12-26 16:32:34 -08002744 if (mExpectedPresentTime >= 0) {
2745 ASSERT_EQ(presentFence->wait(3000), NO_ERROR);
2746 ASSERT_GE(presentFence->getSignalTime(), mExpectedPresentTime - nsecs_t(5 * 1e6));
2747 // if the panel is running at 30 hz, at the worst case, our expected time just
2748 // misses vsync and we have to wait another 33.3ms
2749 ASSERT_LE(presentFence->getSignalTime(),
2750 mExpectedPresentTime + nsecs_t(66.666666 * 1e6));
2751 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002752 } else {
Valerie Hau63258a12018-12-14 14:31:48 -08002753 ASSERT_EQ(presentFence, nullptr) << "transaction shouldn't have been presented";
Marissa Wallfda30bb2018-10-12 11:34:28 -07002754 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
2755 }
2756
Marissa Wall80d94ad2019-01-18 16:04:36 -08002757 ASSERT_EQ(surfaceControlStats.size(), mExpectedSurfaceResults.size())
Marissa Wallfda30bb2018-10-12 11:34:28 -07002758 << "wrong number of surfaces";
2759
Marissa Wall80d94ad2019-01-18 16:04:36 -08002760 for (const auto& stats : surfaceControlStats) {
2761 ASSERT_NE(stats.surfaceControl, nullptr) << "returned null surface control";
2762
Marissa Wallfda30bb2018-10-12 11:34:28 -07002763 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
2764 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
2765 << "unexpected surface control";
Marissa Wall80d94ad2019-01-18 16:04:36 -08002766 expectedSurfaceResult->second.verifySurfaceControlStats(stats, latchTime);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002767 }
2768 }
2769
2770private:
2771 class ExpectedSurfaceResult {
2772 public:
2773 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
2774 ExpectedResult::PreviousBuffer previousBufferResult)
2775 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
2776
Marissa Wall80d94ad2019-01-18 16:04:36 -08002777 void verifySurfaceControlStats(const SurfaceControlStats& surfaceControlStats,
2778 nsecs_t latchTime) const {
2779 const auto& [surfaceControl, acquireTime, previousReleaseFence] = surfaceControlStats;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002780
2781 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
2782 << "bad acquire time";
2783 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
Marissa Wall5a68a772018-12-22 17:43:42 -08002784
2785 if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED) {
2786 ASSERT_NE(previousReleaseFence, nullptr)
2787 << "failed to set release prev buffer fence";
2788 } else if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::NOT_RELEASED) {
2789 ASSERT_EQ(previousReleaseFence, nullptr)
2790 << "should not have set released prev buffer fence";
2791 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002792 }
2793
2794 private:
2795 ExpectedResult::Buffer mBufferResult;
2796 ExpectedResult::PreviousBuffer mPreviousBufferResult;
2797 };
2798
Marissa Wall80d94ad2019-01-18 16:04:36 -08002799 struct SCHash {
2800 std::size_t operator()(const sp<SurfaceControl>& sc) const {
2801 return std::hash<IBinder*>{}(sc->getHandle().get());
Marissa Wallfda30bb2018-10-12 11:34:28 -07002802 }
2803 };
2804 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
Marissa Wall17b4e452018-12-26 16:32:34 -08002805 nsecs_t mExpectedPresentTime = -1;
Marissa Wall80d94ad2019-01-18 16:04:36 -08002806 std::unordered_map<sp<SurfaceControl>, ExpectedSurfaceResult, SCHash> mExpectedSurfaceResults;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002807};
2808
2809class CallbackHelper {
2810public:
Marissa Wall80d94ad2019-01-18 16:04:36 -08002811 static void function(void* callbackContext, nsecs_t latchTime, const sp<Fence>& presentFence,
2812 const std::vector<SurfaceControlStats>& stats) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002813 if (!callbackContext) {
2814 ALOGE("failed to get callback context");
2815 }
2816 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
2817 std::lock_guard lock(helper->mMutex);
Marissa Wall80d94ad2019-01-18 16:04:36 -08002818 helper->mCallbackDataQueue.emplace(latchTime, presentFence, stats);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002819 helper->mConditionVariable.notify_all();
2820 }
2821
Marissa Wall80d94ad2019-01-18 16:04:36 -08002822 void getCallbackData(CallbackData* outData) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002823 std::unique_lock lock(mMutex);
2824
Marissa Wall80d94ad2019-01-18 16:04:36 -08002825 if (mCallbackDataQueue.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002826 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
2827 std::cv_status::timeout)
2828 << "did not receive callback";
2829 }
2830
Marissa Wall80d94ad2019-01-18 16:04:36 -08002831 *outData = std::move(mCallbackDataQueue.front());
2832 mCallbackDataQueue.pop();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002833 }
2834
2835 void verifyFinalState() {
2836 // Wait to see if there are extra callbacks
2837 std::this_thread::sleep_for(500ms);
2838
2839 std::lock_guard lock(mMutex);
Marissa Wall80d94ad2019-01-18 16:04:36 -08002840 EXPECT_EQ(mCallbackDataQueue.size(), 0) << "extra callbacks received";
2841 mCallbackDataQueue = {};
Marissa Wallfda30bb2018-10-12 11:34:28 -07002842 }
2843
2844 void* getContext() { return static_cast<void*>(this); }
2845
2846 std::mutex mMutex;
2847 std::condition_variable mConditionVariable;
Marissa Wall80d94ad2019-01-18 16:04:36 -08002848 std::queue<CallbackData> mCallbackDataQueue;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002849};
2850
2851class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07002852public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07002853 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07002854 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002855 }
2856
Marissa Wall713b63f2018-10-17 15:42:43 -07002857 static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
2858 const sp<SurfaceControl>& layer = nullptr) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002859 if (layer) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002860 sp<GraphicBuffer> buffer;
2861 sp<Fence> fence;
2862 int err = getBuffer(&buffer, &fence);
2863 if (err != NO_ERROR) {
2864 return err;
2865 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002866
Marissa Wall861616d2018-10-22 12:52:23 -07002867 transaction.setBuffer(layer, buffer).setAcquireFence(layer, fence);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002868 }
2869
2870 transaction.addTransactionCompletedCallback(callbackHelper->function,
2871 callbackHelper->getContext());
Marissa Wall713b63f2018-10-17 15:42:43 -07002872 return NO_ERROR;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002873 }
2874
Marissa Wall861616d2018-10-22 12:52:23 -07002875 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2876 bool finalState = false) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08002877 CallbackData callbackData;
2878 ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
2879 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
Marissa Wallfda30bb2018-10-12 11:34:28 -07002880
2881 if (finalState) {
2882 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2883 }
2884 }
2885
Marissa Wall861616d2018-10-22 12:52:23 -07002886 static void waitForCallbacks(CallbackHelper& helper,
2887 const std::vector<ExpectedResult>& expectedResults,
2888 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002889 for (const auto& expectedResult : expectedResults) {
2890 waitForCallback(helper, expectedResult);
2891 }
2892 if (finalState) {
2893 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2894 }
2895 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002896};
2897
2898TEST_F(LayerCallbackTest, Basic) {
2899 sp<SurfaceControl> layer;
2900 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2901
2902 Transaction transaction;
2903 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002904 int err = fillTransaction(transaction, &callback, layer);
2905 if (err) {
2906 GTEST_SUCCEED() << "test not supported";
2907 return;
2908 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002909
2910 transaction.apply();
2911
2912 ExpectedResult expected;
2913 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2914 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2915}
2916
2917TEST_F(LayerCallbackTest, NoBuffer) {
2918 sp<SurfaceControl> layer;
2919 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2920
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
Marissa Wall861616d2018-10-22 12:52:23 -07002929 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002930
2931 ExpectedResult expected;
Marissa Wall713b63f2018-10-17 15:42:43 -07002932 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
2933 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002934 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2935}
2936
2937TEST_F(LayerCallbackTest, NoStateChange) {
2938 Transaction transaction;
2939 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002940 int err = fillTransaction(transaction, &callback);
2941 if (err) {
2942 GTEST_SUCCEED() << "test not supported";
2943 return;
2944 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002945
2946 transaction.apply();
2947
2948 ExpectedResult expected;
2949 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2950}
2951
2952TEST_F(LayerCallbackTest, OffScreen) {
2953 sp<SurfaceControl> layer;
2954 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2955
2956 Transaction transaction;
2957 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002958 int err = fillTransaction(transaction, &callback, layer);
2959 if (err) {
2960 GTEST_SUCCEED() << "test not supported";
2961 return;
2962 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002963
Marissa Wall861616d2018-10-22 12:52:23 -07002964 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002965
2966 ExpectedResult expected;
2967 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2968 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2969}
2970
2971TEST_F(LayerCallbackTest, Merge) {
2972 sp<SurfaceControl> layer1, layer2;
2973 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2974 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2975
2976 Transaction transaction1, transaction2;
2977 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07002978 int err = fillTransaction(transaction1, &callback1, layer1);
2979 if (err) {
2980 GTEST_SUCCEED() << "test not supported";
2981 return;
2982 }
2983 err = fillTransaction(transaction2, &callback2, layer2);
2984 if (err) {
2985 GTEST_SUCCEED() << "test not supported";
2986 return;
2987 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002988
Marissa Wall861616d2018-10-22 12:52:23 -07002989 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2990 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002991
2992 ExpectedResult expected;
2993 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2994 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2995 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2996}
2997
2998TEST_F(LayerCallbackTest, Merge_SameCallback) {
2999 sp<SurfaceControl> layer1, layer2;
3000 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3001 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3002
3003 Transaction transaction1, transaction2;
3004 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003005 int err = fillTransaction(transaction1, &callback, layer1);
3006 if (err) {
3007 GTEST_SUCCEED() << "test not supported";
3008 return;
3009 }
3010 err = fillTransaction(transaction2, &callback, layer2);
3011 if (err) {
3012 GTEST_SUCCEED() << "test not supported";
3013 return;
3014 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003015
3016 transaction2.merge(std::move(transaction1)).apply();
3017
3018 ExpectedResult expected;
3019 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3020 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3021 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3022}
3023
3024TEST_F(LayerCallbackTest, Merge_SameLayer) {
3025 sp<SurfaceControl> layer;
3026 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3027
3028 Transaction transaction1, transaction2;
3029 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003030 int err = fillTransaction(transaction1, &callback1, layer);
3031 if (err) {
3032 GTEST_SUCCEED() << "test not supported";
3033 return;
3034 }
3035 err = fillTransaction(transaction2, &callback2, layer);
3036 if (err) {
3037 GTEST_SUCCEED() << "test not supported";
3038 return;
3039 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003040
3041 transaction2.merge(std::move(transaction1)).apply();
3042
3043 ExpectedResult expected;
3044 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3045 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3046 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3047}
3048
Marissa Wallfda30bb2018-10-12 11:34:28 -07003049TEST_F(LayerCallbackTest, Merge_DifferentClients) {
3050 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3051 client2(new SurfaceComposerClient);
3052
3053 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3054 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3055
3056 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003057 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003058 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003059 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003060 ISurfaceComposerClient::eFXSurfaceBufferState));
3061
3062 Transaction transaction1, transaction2;
3063 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003064 int err = fillTransaction(transaction1, &callback1, layer1);
3065 if (err) {
3066 GTEST_SUCCEED() << "test not supported";
3067 return;
3068 }
3069 err = fillTransaction(transaction2, &callback2, layer2);
3070 if (err) {
3071 GTEST_SUCCEED() << "test not supported";
3072 return;
3073 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003074
Marissa Wall861616d2018-10-22 12:52:23 -07003075 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3076 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003077
3078 ExpectedResult expected;
3079 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3080 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3081 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3082}
3083
3084TEST_F(LayerCallbackTest, MultipleTransactions) {
3085 sp<SurfaceControl> layer;
3086 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3087
3088 Transaction transaction;
3089 CallbackHelper callback;
3090 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003091 int err = fillTransaction(transaction, &callback, layer);
3092 if (err) {
3093 GTEST_SUCCEED() << "test not supported";
3094 return;
3095 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003096
3097 transaction.apply();
3098
3099 ExpectedResult expected;
3100 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07003101 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003102 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3103 : ExpectedResult::PreviousBuffer::RELEASED);
3104 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3105 }
3106 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3107}
3108
3109TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
3110 sp<SurfaceControl> layer;
3111 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3112
3113 Transaction transaction;
3114 CallbackHelper callback;
3115 for (size_t i = 0; i < 10; i++) {
3116 ExpectedResult expected;
3117
3118 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003119 int err = fillTransaction(transaction, &callback, layer);
3120 if (err) {
3121 GTEST_SUCCEED() << "test not supported";
3122 return;
3123 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003124 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3125 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003126 int err = fillTransaction(transaction, &callback);
3127 if (err) {
3128 GTEST_SUCCEED() << "test not supported";
3129 return;
3130 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003131 }
3132
3133 transaction.apply();
3134
3135 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3136 }
3137 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3138}
3139
3140TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
3141 sp<SurfaceControl> layer;
3142 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3143
3144 Transaction transaction;
3145 CallbackHelper callback;
3146 for (size_t i = 0; i < 10; i++) {
3147 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003148 int err = fillTransaction(transaction, &callback, layer);
3149 if (err) {
3150 GTEST_SUCCEED() << "test not supported";
3151 return;
3152 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003153 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003154 int err = fillTransaction(transaction, &callback);
3155 if (err) {
3156 GTEST_SUCCEED() << "test not supported";
3157 return;
3158 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003159 }
3160
Marissa Wall861616d2018-10-22 12:52:23 -07003161 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003162
3163 ExpectedResult expected;
3164 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
3165 : ExpectedResult::Transaction::NOT_PRESENTED,
Marissa Wall713b63f2018-10-17 15:42:43 -07003166 layer,
3167 (i == 0) ? ExpectedResult::Buffer::ACQUIRED
3168 : ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003169 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
3170 }
3171 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3172}
3173
3174TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
3175 sp<SurfaceControl> layer1, layer2;
3176 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3177 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3178
3179 Transaction transaction1, transaction2;
3180 CallbackHelper callback1, callback2;
3181 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003182 int err = fillTransaction(transaction1, &callback1, layer1);
3183 if (err) {
3184 GTEST_SUCCEED() << "test not supported";
3185 return;
3186 }
3187 err = fillTransaction(transaction2, &callback2, layer2);
3188 if (err) {
3189 GTEST_SUCCEED() << "test not supported";
3190 return;
3191 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003192
Marissa Wall861616d2018-10-22 12:52:23 -07003193 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3194 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003195
3196 ExpectedResult expected;
3197 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003198 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003199 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3200 : ExpectedResult::PreviousBuffer::RELEASED);
3201 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3202 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3203 }
3204 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3205 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3206}
3207
3208TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
3209 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3210 client2(new SurfaceComposerClient);
3211 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3212 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3213
3214 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003215 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003216 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003217 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003218 ISurfaceComposerClient::eFXSurfaceBufferState));
3219
3220 Transaction transaction1, transaction2;
3221 CallbackHelper callback1, callback2;
3222 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003223 int err = fillTransaction(transaction1, &callback1, layer1);
3224 if (err) {
3225 GTEST_SUCCEED() << "test not supported";
3226 return;
3227 }
3228 err = fillTransaction(transaction2, &callback2, layer2);
3229 if (err) {
3230 GTEST_SUCCEED() << "test not supported";
3231 return;
3232 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003233
Marissa Wall861616d2018-10-22 12:52:23 -07003234 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3235 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003236
3237 ExpectedResult expected;
3238 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003239 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003240 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3241 : ExpectedResult::PreviousBuffer::RELEASED);
3242 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3243 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3244 }
3245 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3246 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3247}
3248
3249TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
3250 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3251 client2(new SurfaceComposerClient);
3252 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3253 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3254
3255 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003256 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003257 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003258 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003259 ISurfaceComposerClient::eFXSurfaceBufferState));
3260
3261 Transaction transaction1, transaction2;
3262 CallbackHelper callback1, callback2;
3263
3264 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003265 int err = fillTransaction(transaction1, &callback1, layer1);
3266 if (err) {
3267 GTEST_SUCCEED() << "test not supported";
3268 return;
3269 }
3270 err = fillTransaction(transaction2, &callback2, layer2);
3271 if (err) {
3272 GTEST_SUCCEED() << "test not supported";
3273 return;
3274 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003275
Marissa Wall861616d2018-10-22 12:52:23 -07003276 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3277 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003278
3279 ExpectedResult expected;
3280 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3281 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3282 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3283 expected.reset();
3284
3285 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003286 err = fillTransaction(transaction1, &callback1);
3287 if (err) {
3288 GTEST_SUCCEED() << "test not supported";
3289 return;
3290 }
3291 err = fillTransaction(transaction2, &callback2);
3292 if (err) {
3293 GTEST_SUCCEED() << "test not supported";
3294 return;
3295 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003296
3297 transaction2.merge(std::move(transaction1)).apply();
3298
3299 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3300 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3301}
3302
3303TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
3304 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3305 client2(new SurfaceComposerClient);
3306
3307 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3308 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3309
3310 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003311 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003312 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003313 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003314 ISurfaceComposerClient::eFXSurfaceBufferState));
3315
3316 Transaction transaction1, transaction2;
3317 CallbackHelper callback1, callback2;
3318
3319 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003320 int err = fillTransaction(transaction1, &callback1, layer1);
3321 if (err) {
3322 GTEST_SUCCEED() << "test not supported";
3323 return;
3324 }
3325 err = fillTransaction(transaction2, &callback2, layer2);
3326 if (err) {
3327 GTEST_SUCCEED() << "test not supported";
3328 return;
3329 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003330
Marissa Wall861616d2018-10-22 12:52:23 -07003331 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3332 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003333
3334 ExpectedResult expected;
3335 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3336 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3337 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3338 expected.reset();
3339
3340 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003341 err = fillTransaction(transaction1, &callback1);
3342 if (err) {
3343 GTEST_SUCCEED() << "test not supported";
3344 return;
3345 }
3346 err = fillTransaction(transaction2, &callback2);
3347 if (err) {
3348 GTEST_SUCCEED() << "test not supported";
3349 return;
3350 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003351
Marissa Wall861616d2018-10-22 12:52:23 -07003352 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003353
Marissa Wall713b63f2018-10-17 15:42:43 -07003354 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
3355 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003356 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3357 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3358}
3359
3360TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3361 sp<SurfaceControl> layer;
3362 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3363
3364 Transaction transaction;
3365 CallbackHelper callback;
3366 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003367 for (auto& expected : expectedResults) {
3368 expected.reset();
3369 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall5a68a772018-12-22 17:43:42 -08003370 ExpectedResult::Buffer::ACQUIRED,
3371 ExpectedResult::PreviousBuffer::UNKNOWN);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003372
Marissa Wall713b63f2018-10-17 15:42:43 -07003373 int err = fillTransaction(transaction, &callback, layer);
3374 if (err) {
3375 GTEST_SUCCEED() << "test not supported";
3376 return;
3377 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003378
3379 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003380 }
3381 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3382}
3383
3384TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3385 sp<SurfaceControl> layer;
3386 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3387
Marissa Wall713b63f2018-10-17 15:42:43 -07003388 // Normal call to set up test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003389 Transaction transaction;
3390 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003391 int err = fillTransaction(transaction, &callback, layer);
3392 if (err) {
3393 GTEST_SUCCEED() << "test not supported";
3394 return;
3395 }
3396
3397 transaction.apply();
3398
3399 ExpectedResult expected;
3400 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3401 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3402
3403 // Test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003404 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003405 for (auto& expected : expectedResults) {
3406 expected.reset();
3407
Marissa Wall713b63f2018-10-17 15:42:43 -07003408 err = fillTransaction(transaction, &callback);
3409 if (err) {
3410 GTEST_SUCCEED() << "test not supported";
3411 return;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003412 }
3413
3414 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003415 }
3416 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3417}
3418
3419TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3420 sp<SurfaceControl> layer;
3421 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3422
3423 // Normal call to set up test
3424 Transaction transaction;
3425 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003426 int err = fillTransaction(transaction, &callback, layer);
3427 if (err) {
3428 GTEST_SUCCEED() << "test not supported";
3429 return;
3430 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003431
Marissa Wall861616d2018-10-22 12:52:23 -07003432 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003433
3434 ExpectedResult expectedResult;
3435 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3436 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3437
3438 // Test
3439 std::vector<ExpectedResult> expectedResults(50);
3440 for (auto& expected : expectedResults) {
3441 expected.reset();
Marissa Wall713b63f2018-10-17 15:42:43 -07003442 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
3443 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003444
Marissa Wall713b63f2018-10-17 15:42:43 -07003445 err = fillTransaction(transaction, &callback);
3446 if (err) {
3447 GTEST_SUCCEED() << "test not supported";
3448 return;
3449 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003450
Marissa Wall861616d2018-10-22 12:52:23 -07003451 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003452 }
3453 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3454}
3455
Marissa Wall17b4e452018-12-26 16:32:34 -08003456TEST_F(LayerCallbackTest, DesiredPresentTime) {
3457 sp<SurfaceControl> layer;
3458 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3459
3460 Transaction transaction;
3461 CallbackHelper callback;
3462 int err = fillTransaction(transaction, &callback, layer);
3463 if (err) {
3464 GTEST_SUCCEED() << "test not supported";
3465 return;
3466 }
3467
3468 // Try to present 100ms in the future
3469 nsecs_t time = systemTime() + (100 * 1e6);
3470
3471 transaction.setDesiredPresentTime(time);
3472 transaction.apply();
3473
3474 ExpectedResult expected;
3475 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3476 expected.addExpectedPresentTime(time);
3477 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3478}
3479
3480TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
3481 sp<SurfaceControl> layer;
3482 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3483
3484 Transaction transaction;
3485 CallbackHelper callback1;
3486 int err = fillTransaction(transaction, &callback1, layer);
3487 if (err) {
3488 GTEST_SUCCEED() << "test not supported";
3489 return;
3490 }
3491
3492 // Try to present 100ms in the future
3493 nsecs_t time = systemTime() + (100 * 1e6);
3494
3495 transaction.setDesiredPresentTime(time);
3496 transaction.apply();
3497
3498 ExpectedResult expected1;
3499 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3500 expected1.addExpectedPresentTime(time);
3501
3502 CallbackHelper callback2;
3503 err = fillTransaction(transaction, &callback2, layer);
3504 if (err) {
3505 GTEST_SUCCEED() << "test not supported";
3506 return;
3507 }
3508
3509 // Try to present 33ms after the first frame
3510 time += (33.3 * 1e6);
3511
3512 transaction.setDesiredPresentTime(time);
3513 transaction.apply();
3514
3515 ExpectedResult expected2;
3516 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3517 ExpectedResult::Buffer::ACQUIRED,
3518 ExpectedResult::PreviousBuffer::RELEASED);
3519 expected2.addExpectedPresentTime(time);
3520
3521 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3522 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3523}
3524
3525TEST_F(LayerCallbackTest, DesiredPresentTime_OutOfOrder) {
3526 sp<SurfaceControl> layer;
3527 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3528
3529 Transaction transaction;
3530 CallbackHelper callback1;
3531 int err = fillTransaction(transaction, &callback1, layer);
3532 if (err) {
3533 GTEST_SUCCEED() << "test not supported";
3534 return;
3535 }
3536
3537 // Try to present 100ms in the future
3538 nsecs_t time = systemTime() + (100 * 1e6);
3539
3540 transaction.setDesiredPresentTime(time);
3541 transaction.apply();
3542
3543 ExpectedResult expected1;
3544 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3545 expected1.addExpectedPresentTime(time);
3546
3547 CallbackHelper callback2;
3548 err = fillTransaction(transaction, &callback2, layer);
3549 if (err) {
3550 GTEST_SUCCEED() << "test not supported";
3551 return;
3552 }
3553
3554 // Try to present 33ms before the previous frame
3555 time -= (33.3 * 1e6);
3556
3557 transaction.setDesiredPresentTime(time);
3558 transaction.apply();
3559
3560 ExpectedResult expected2;
3561 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3562 ExpectedResult::Buffer::ACQUIRED,
3563 ExpectedResult::PreviousBuffer::RELEASED);
3564
3565 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3566 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3567}
3568
3569TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
3570 sp<SurfaceControl> layer;
3571 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3572
3573 Transaction transaction;
3574 CallbackHelper callback;
3575 int err = fillTransaction(transaction, &callback, layer);
3576 if (err) {
3577 GTEST_SUCCEED() << "test not supported";
3578 return;
3579 }
3580
3581 // Try to present 100ms in the past
3582 nsecs_t time = systemTime() - (100 * 1e6);
3583
3584 transaction.setDesiredPresentTime(time);
3585 transaction.apply();
3586
3587 ExpectedResult expected;
3588 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3589 expected.addExpectedPresentTime(systemTime());
3590 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3591}
3592
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003593class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003594protected:
3595 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07003596 LayerTransactionTest::SetUp();
3597 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003598
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003599 sp<IBinder> display(
3600 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07003601 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07003602 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07003603
3604 ssize_t displayWidth = info.w;
3605 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003606
3607 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07003608 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
3609 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003610 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003611 ASSERT_TRUE(mBGSurfaceControl->isValid());
3612 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
3613
3614 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07003615 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
3616
Peiyong Lin566a3b42018-01-09 18:22:43 -08003617 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003618 ASSERT_TRUE(mFGSurfaceControl->isValid());
3619
3620 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3621
3622 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07003623 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003624 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003625 ASSERT_TRUE(mSyncSurfaceControl->isValid());
3626
3627 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3628
Robert Carr4cdc58f2017-08-23 14:22:20 -07003629 asTransaction([&](Transaction& t) {
3630 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003631
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003632 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07003633
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003634 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
3635 .setPosition(mFGSurfaceControl, 64, 64)
3636 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003637
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003638 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
3639 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
3640 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003641 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003642 }
3643
3644 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07003645 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003646 mBGSurfaceControl = 0;
3647 mFGSurfaceControl = 0;
3648 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003649 }
3650
3651 void waitForPostedBuffers() {
3652 // Since the sync surface is in synchronous mode (i.e. double buffered)
3653 // posting three buffers to it should ensure that at least two
3654 // SurfaceFlinger::handlePageFlip calls have been made, which should
3655 // guaranteed that a buffer posted to another Surface has been retired.
3656 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3657 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3658 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3659 }
3660
Robert Carr4cdc58f2017-08-23 14:22:20 -07003661 void asTransaction(const std::function<void(Transaction&)>& exec) {
3662 Transaction t;
3663 exec(t);
3664 t.apply(true);
3665 }
3666
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003667 sp<SurfaceControl> mBGSurfaceControl;
3668 sp<SurfaceControl> mFGSurfaceControl;
3669
3670 // This surface is used to ensure that the buffers posted to
3671 // mFGSurfaceControl have been picked up by SurfaceFlinger.
3672 sp<SurfaceControl> mSyncSurfaceControl;
3673};
3674
Robert Carr7f619b22017-11-06 12:56:35 -08003675TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08003676
chaviw0e3479f2018-09-10 16:49:30 -07003677 std::unique_ptr<ScreenCapture> sc;
3678
3679 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08003680 fillSurfaceRGBA8(relative, 10, 10, 10);
3681 waitForPostedBuffers();
3682
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003683 Transaction{}
3684 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08003685 .setPosition(relative, 64, 64)
3686 .apply();
3687
3688 {
3689 // The relative should be on top of the FG control.
3690 ScreenCapture::captureScreen(&sc);
3691 sc->checkPixel(64, 64, 10, 10, 10);
3692 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003693 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003694
3695 {
3696 // Nothing should change at this point.
3697 ScreenCapture::captureScreen(&sc);
3698 sc->checkPixel(64, 64, 10, 10, 10);
3699 }
3700
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003701 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003702
3703 {
3704 // Ensure that the relative was actually hidden, rather than
3705 // being left in the detached but visible state.
3706 ScreenCapture::captureScreen(&sc);
3707 sc->expectFGColor(64, 64);
3708 }
3709}
3710
Robert Carr8d5227b2017-03-16 15:41:03 -07003711class GeometryLatchingTest : public LayerUpdateTest {
3712protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003713 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07003714 SCOPED_TRACE(trace);
3715 ScreenCapture::captureScreen(&sc);
3716 // We find the leading edge of the FG surface.
3717 sc->expectFGColor(127, 127);
3718 sc->expectBGColor(128, 128);
3719 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003720
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003721 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07003722
3723 void unlockFGBuffer() {
3724 sp<Surface> s = mFGSurfaceControl->getSurface();
3725 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
3726 waitForPostedBuffers();
3727 }
3728
Robert Carr8d5227b2017-03-16 15:41:03 -07003729 void completeFGResize() {
3730 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3731 waitForPostedBuffers();
3732 }
3733 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003734 asTransaction([&](Transaction& t) {
3735 t.setSize(mFGSurfaceControl, 64, 64);
3736 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003737 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003738 });
Robert Carr8d5227b2017-03-16 15:41:03 -07003739
3740 EXPECT_INITIAL_STATE("After restoring initial state");
3741 }
chaviw0e3479f2018-09-10 16:49:30 -07003742 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07003743};
3744
Robert Carr8d5227b2017-03-16 15:41:03 -07003745class CropLatchingTest : public GeometryLatchingTest {
3746protected:
3747 void EXPECT_CROPPED_STATE(const char* trace) {
3748 SCOPED_TRACE(trace);
3749 ScreenCapture::captureScreen(&sc);
3750 // The edge should be moved back one pixel by our crop.
3751 sc->expectFGColor(126, 126);
3752 sc->expectBGColor(127, 127);
3753 sc->expectBGColor(128, 128);
3754 }
chaviw59f5c562017-06-28 16:39:06 -07003755
3756 void EXPECT_RESIZE_STATE(const char* trace) {
3757 SCOPED_TRACE(trace);
3758 ScreenCapture::captureScreen(&sc);
3759 // The FG is now resized too 128,128 at 64,64
3760 sc->expectFGColor(64, 64);
3761 sc->expectFGColor(191, 191);
3762 sc->expectBGColor(192, 192);
3763 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003764};
3765
Pablo Ceballos05289c22016-04-14 15:49:55 -07003766TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003767 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003768 {
3769 SCOPED_TRACE("before anything");
3770 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003771 sc->expectBGColor(32, 32);
3772 sc->expectFGColor(96, 96);
3773 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003774 }
3775
3776 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003777 asTransaction([&](Transaction& t) {
3778 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003779 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3780 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003781 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003782
Robert Carr4cdc58f2017-08-23 14:22:20 -07003783 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003784 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003785 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3786 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003787 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003788
3789 {
3790 SCOPED_TRACE("before any trigger");
3791 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003792 sc->expectBGColor(32, 32);
3793 sc->expectFGColor(96, 96);
3794 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003795 }
3796
3797 // should trigger the first deferred transaction, but not the second one
3798 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3799 {
3800 SCOPED_TRACE("after first trigger");
3801 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003802 sc->expectBGColor(32, 32);
3803 sc->checkPixel(96, 96, 162, 63, 96);
3804 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003805 }
3806
3807 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003808 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003809
3810 // trigger the second deferred transaction
3811 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3812 {
3813 SCOPED_TRACE("after second trigger");
3814 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003815 sc->expectBGColor(32, 32);
3816 sc->expectBGColor(96, 96);
3817 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003818 }
3819}
3820
Robert Carre392b552017-09-19 12:16:05 -07003821TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003822 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003823
3824 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003825 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3826 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3827 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3828 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003829 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003830 SurfaceComposerClient::Transaction{}
3831 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3832 .show(childNoBuffer)
3833 .show(childBuffer)
3834 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003835 {
3836 ScreenCapture::captureScreen(&sc);
3837 sc->expectChildColor(73, 73);
3838 sc->expectFGColor(74, 74);
3839 }
Vishnu Nair60356342018-11-13 13:00:45 -08003840 SurfaceComposerClient::Transaction{}
3841 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3842 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003843 {
3844 ScreenCapture::captureScreen(&sc);
3845 sc->expectChildColor(73, 73);
3846 sc->expectChildColor(74, 74);
3847 }
3848}
3849
Robert Carr2c5f6d22017-09-26 12:30:35 -07003850TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003851 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003852 {
3853 SCOPED_TRACE("before move");
3854 ScreenCapture::captureScreen(&sc);
3855 sc->expectBGColor(0, 12);
3856 sc->expectFGColor(75, 75);
3857 sc->expectBGColor(145, 145);
3858 }
3859
3860 Transaction t1, t2;
3861 t1.setPosition(mFGSurfaceControl, 128, 128);
3862 t2.setPosition(mFGSurfaceControl, 0, 0);
3863 // We expect that the position update from t2 now
3864 // overwrites the position update from t1.
3865 t1.merge(std::move(t2));
3866 t1.apply();
3867
3868 {
3869 ScreenCapture::captureScreen(&sc);
3870 sc->expectFGColor(1, 1);
3871 }
3872}
3873
Robert Carr1f0a16a2016-10-24 16:27:39 -07003874class ChildLayerTest : public LayerUpdateTest {
3875protected:
3876 void SetUp() override {
3877 LayerUpdateTest::SetUp();
Vishnu Nair88a11f22018-11-28 18:30:57 -08003878 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
3879 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003880 fillSurfaceRGBA8(mChild, 200, 200, 200);
3881
3882 {
3883 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003884 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003885 mCapture->expectChildColor(64, 64);
3886 }
3887 }
3888 void TearDown() override {
3889 LayerUpdateTest::TearDown();
3890 mChild = 0;
3891 }
3892
3893 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003894 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003895};
3896
3897TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003898 asTransaction([&](Transaction& t) {
3899 t.show(mChild);
3900 t.setPosition(mChild, 10, 10);
3901 t.setPosition(mFGSurfaceControl, 64, 64);
3902 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003903
3904 {
chaviw0e3479f2018-09-10 16:49:30 -07003905 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003906 // Top left of foreground must now be visible
3907 mCapture->expectFGColor(64, 64);
3908 // But 10 pixels in we should see the child surface
3909 mCapture->expectChildColor(74, 74);
3910 // And 10 more pixels we should be back to the foreground surface
3911 mCapture->expectFGColor(84, 84);
3912 }
3913
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003914 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003915
3916 {
chaviw0e3479f2018-09-10 16:49:30 -07003917 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003918 // Top left of foreground should now be at 0, 0
3919 mCapture->expectFGColor(0, 0);
3920 // But 10 pixels in we should see the child surface
3921 mCapture->expectChildColor(10, 10);
3922 // And 10 more pixels we should be back to the foreground surface
3923 mCapture->expectFGColor(20, 20);
3924 }
3925}
3926
Robert Carr41b08b52017-06-01 16:11:34 -07003927TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003928 asTransaction([&](Transaction& t) {
3929 t.show(mChild);
3930 t.setPosition(mChild, 0, 0);
3931 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003932 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003933 });
Robert Carr41b08b52017-06-01 16:11:34 -07003934
3935 {
chaviw0e3479f2018-09-10 16:49:30 -07003936 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07003937 mCapture->expectChildColor(0, 0);
3938 mCapture->expectChildColor(4, 4);
3939 mCapture->expectBGColor(5, 5);
3940 }
3941}
3942
Robert Carr1f0a16a2016-10-24 16:27:39 -07003943TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003944 asTransaction([&](Transaction& t) {
3945 t.show(mChild);
3946 t.setPosition(mFGSurfaceControl, 0, 0);
3947 t.setPosition(mChild, 63, 63);
3948 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003949
3950 {
chaviw0e3479f2018-09-10 16:49:30 -07003951 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003952 mCapture->expectFGColor(0, 0);
3953 // Last pixel in foreground should now be the child.
3954 mCapture->expectChildColor(63, 63);
3955 // But the child should be constrained and the next pixel
3956 // must be the background
3957 mCapture->expectBGColor(64, 64);
3958 }
3959}
3960
3961TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003962 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003963
3964 // Find the boundary between the parent and child
3965 {
chaviw0e3479f2018-09-10 16:49:30 -07003966 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003967 mCapture->expectChildColor(9, 9);
3968 mCapture->expectFGColor(10, 10);
3969 }
3970
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003971 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003972
3973 // The boundary should be twice as far from the origin now.
3974 // The pixels from the last test should all be child now
3975 {
chaviw0e3479f2018-09-10 16:49:30 -07003976 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003977 mCapture->expectChildColor(9, 9);
3978 mCapture->expectChildColor(10, 10);
3979 mCapture->expectChildColor(19, 19);
3980 mCapture->expectFGColor(20, 20);
3981 }
3982}
Robert Carr9524cb32017-02-13 11:32:32 -08003983
Robert Carr6452f122017-03-21 10:41:29 -07003984TEST_F(ChildLayerTest, ChildLayerAlpha) {
3985 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
3986 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
3987 fillSurfaceRGBA8(mChild, 0, 254, 0);
3988 waitForPostedBuffers();
3989
Robert Carr4cdc58f2017-08-23 14:22:20 -07003990 asTransaction([&](Transaction& t) {
3991 t.show(mChild);
3992 t.setPosition(mChild, 0, 0);
3993 t.setPosition(mFGSurfaceControl, 0, 0);
3994 });
Robert Carr6452f122017-03-21 10:41:29 -07003995
3996 {
chaviw0e3479f2018-09-10 16:49:30 -07003997 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003998 // Unblended child color
3999 mCapture->checkPixel(0, 0, 0, 254, 0);
4000 }
4001
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004002 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004003
4004 {
chaviw0e3479f2018-09-10 16:49:30 -07004005 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004006 // Child and BG blended.
4007 mCapture->checkPixel(0, 0, 127, 127, 0);
4008 }
4009
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004010 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004011
4012 {
chaviw0e3479f2018-09-10 16:49:30 -07004013 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004014 // Child and BG blended.
4015 mCapture->checkPixel(0, 0, 95, 64, 95);
4016 }
4017}
4018
Robert Carr9524cb32017-02-13 11:32:32 -08004019TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004020 asTransaction([&](Transaction& t) {
4021 t.show(mChild);
4022 t.setPosition(mChild, 10, 10);
4023 t.setPosition(mFGSurfaceControl, 64, 64);
4024 });
Robert Carr9524cb32017-02-13 11:32:32 -08004025
4026 {
chaviw0e3479f2018-09-10 16:49:30 -07004027 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004028 // Top left of foreground must now be visible
4029 mCapture->expectFGColor(64, 64);
4030 // But 10 pixels in we should see the child surface
4031 mCapture->expectChildColor(74, 74);
4032 // And 10 more pixels we should be back to the foreground surface
4033 mCapture->expectFGColor(84, 84);
4034 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004035
4036 asTransaction([&](Transaction& t) {
4037 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
4038 });
4039
Robert Carr9524cb32017-02-13 11:32:32 -08004040 {
chaviw0e3479f2018-09-10 16:49:30 -07004041 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004042 mCapture->expectFGColor(64, 64);
4043 // In reparenting we should have exposed the entire foreground surface.
4044 mCapture->expectFGColor(74, 74);
4045 // And the child layer should now begin at 10, 10 (since the BG
4046 // layer is at (0, 0)).
4047 mCapture->expectBGColor(9, 9);
4048 mCapture->expectChildColor(10, 10);
4049 }
4050}
4051
Robert Carr2e102c92018-10-23 12:11:15 -07004052TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
4053 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004054 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07004055 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
4056
4057 {
4058 SCOPED_TRACE("Grandchild visible");
4059 ScreenCapture::captureScreen(&mCapture);
4060 mCapture->checkPixel(64, 64, 111, 111, 111);
4061 }
4062
4063 mChild->clear();
4064
4065 {
4066 SCOPED_TRACE("After destroying child");
4067 ScreenCapture::captureScreen(&mCapture);
4068 mCapture->expectFGColor(64, 64);
4069 }
4070
4071 asTransaction([&](Transaction& t) {
4072 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
4073 });
4074
4075 {
4076 SCOPED_TRACE("After reparenting grandchild");
4077 ScreenCapture::captureScreen(&mCapture);
4078 mCapture->checkPixel(64, 64, 111, 111, 111);
4079 }
4080}
4081
chaviw161410b02017-07-27 10:46:08 -07004082TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004083 asTransaction([&](Transaction& t) {
4084 t.show(mChild);
4085 t.setPosition(mChild, 10, 10);
4086 t.setPosition(mFGSurfaceControl, 64, 64);
4087 });
Robert Carr9524cb32017-02-13 11:32:32 -08004088
4089 {
chaviw0e3479f2018-09-10 16:49:30 -07004090 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004091 // Top left of foreground must now be visible
4092 mCapture->expectFGColor(64, 64);
4093 // But 10 pixels in we should see the child surface
4094 mCapture->expectChildColor(74, 74);
4095 // And 10 more pixels we should be back to the foreground surface
4096 mCapture->expectFGColor(84, 84);
4097 }
4098
chaviw0e3479f2018-09-10 16:49:30 -07004099
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004100 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08004101
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004102 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08004103
chaviw161410b02017-07-27 10:46:08 -07004104 // Since the child has the same client as the parent, it will not get
4105 // detached and will be hidden.
4106 {
chaviw0e3479f2018-09-10 16:49:30 -07004107 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004108 mCapture->expectFGColor(64, 64);
4109 mCapture->expectFGColor(74, 74);
4110 mCapture->expectFGColor(84, 84);
4111 }
4112}
4113
4114TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
4115 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004116 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004117 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
4118 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07004119
chaviw161410b02017-07-27 10:46:08 -07004120 ASSERT_TRUE(mChildNewClient->isValid());
4121
4122 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
4123
Robert Carr4cdc58f2017-08-23 14:22:20 -07004124 asTransaction([&](Transaction& t) {
4125 t.hide(mChild);
4126 t.show(mChildNewClient);
4127 t.setPosition(mChildNewClient, 10, 10);
4128 t.setPosition(mFGSurfaceControl, 64, 64);
4129 });
chaviw161410b02017-07-27 10:46:08 -07004130
4131 {
chaviw0e3479f2018-09-10 16:49:30 -07004132 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004133 // Top left of foreground must now be visible
4134 mCapture->expectFGColor(64, 64);
4135 // But 10 pixels in we should see the child surface
4136 mCapture->expectChildColor(74, 74);
4137 // And 10 more pixels we should be back to the foreground surface
4138 mCapture->expectFGColor(84, 84);
4139 }
4140
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004141 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07004142
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004143 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07004144
Robert Carr9524cb32017-02-13 11:32:32 -08004145 // Nothing should have changed.
4146 {
chaviw0e3479f2018-09-10 16:49:30 -07004147 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004148 mCapture->expectFGColor(64, 64);
4149 mCapture->expectChildColor(74, 74);
4150 mCapture->expectFGColor(84, 84);
4151 }
4152}
4153
chaviw5aedec92018-10-22 10:40:38 -07004154TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
4155 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4156 sp<SurfaceControl> childNewClient =
4157 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4158 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4159
4160 ASSERT_TRUE(childNewClient != nullptr);
4161 ASSERT_TRUE(childNewClient->isValid());
4162
4163 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4164
4165 Transaction()
4166 .hide(mChild)
4167 .show(childNewClient)
4168 .setPosition(childNewClient, 10, 10)
4169 .setPosition(mFGSurfaceControl, 64, 64)
4170 .apply();
4171
4172 {
4173 mCapture = screenshot();
4174 // Top left of foreground must now be visible
4175 mCapture->expectFGColor(64, 64);
4176 // But 10 pixels in we should see the child surface
4177 mCapture->expectChildColor(74, 74);
4178 // And 10 more pixels we should be back to the foreground surface
4179 mCapture->expectFGColor(84, 84);
4180 }
4181
4182 Transaction().detachChildren(mFGSurfaceControl).apply();
4183 Transaction().hide(childNewClient).apply();
4184
4185 // Nothing should have changed.
4186 {
4187 mCapture = screenshot();
4188 mCapture->expectFGColor(64, 64);
4189 mCapture->expectChildColor(74, 74);
4190 mCapture->expectFGColor(84, 84);
4191 }
4192
4193 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
4194 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
4195 32);
4196 Transaction()
4197 .setLayer(newParentSurface, INT32_MAX - 1)
4198 .show(newParentSurface)
4199 .setPosition(newParentSurface, 20, 20)
4200 .reparent(childNewClient, newParentSurface->getHandle())
4201 .apply();
4202 {
4203 mCapture = screenshot();
4204 // Child is now hidden.
4205 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
4206 }
4207}
4208
Robert Carr9b429f42017-04-17 14:56:57 -07004209TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004210 asTransaction([&](Transaction& t) {
4211 t.show(mChild);
4212 t.setPosition(mChild, 0, 0);
4213 t.setPosition(mFGSurfaceControl, 0, 0);
4214 });
Robert Carr9b429f42017-04-17 14:56:57 -07004215
4216 {
chaviw0e3479f2018-09-10 16:49:30 -07004217 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004218 // We've positioned the child in the top left.
4219 mCapture->expectChildColor(0, 0);
4220 // But it's only 10x10.
4221 mCapture->expectFGColor(10, 10);
4222 }
4223
Robert Carr4cdc58f2017-08-23 14:22:20 -07004224 asTransaction([&](Transaction& t) {
4225 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4226 // We cause scaling by 2.
4227 t.setSize(mFGSurfaceControl, 128, 128);
4228 });
Robert Carr9b429f42017-04-17 14:56:57 -07004229
4230 {
chaviw0e3479f2018-09-10 16:49:30 -07004231 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004232 // We've positioned the child in the top left.
4233 mCapture->expectChildColor(0, 0);
4234 mCapture->expectChildColor(10, 10);
4235 mCapture->expectChildColor(19, 19);
4236 // And now it should be scaled all the way to 20x20
4237 mCapture->expectFGColor(20, 20);
4238 }
4239}
4240
Robert Carr1725eee2017-04-26 18:32:15 -07004241// Regression test for b/37673612
4242TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004243 asTransaction([&](Transaction& t) {
4244 t.show(mChild);
4245 t.setPosition(mChild, 0, 0);
4246 t.setPosition(mFGSurfaceControl, 0, 0);
4247 });
Robert Carr1725eee2017-04-26 18:32:15 -07004248
4249 {
chaviw0e3479f2018-09-10 16:49:30 -07004250 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004251 // We've positioned the child in the top left.
4252 mCapture->expectChildColor(0, 0);
4253 // But it's only 10x10.
4254 mCapture->expectFGColor(10, 10);
4255 }
Robert Carr1725eee2017-04-26 18:32:15 -07004256 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
4257 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004258 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07004259 sp<Surface> s = mFGSurfaceControl->getSurface();
4260 auto anw = static_cast<ANativeWindow*>(s.get());
4261 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4262 native_window_set_buffers_dimensions(anw, 64, 128);
4263 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4264 waitForPostedBuffers();
4265
4266 {
4267 // The child should still be in the same place and not have any strange scaling as in
4268 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07004269 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004270 mCapture->expectChildColor(0, 0);
4271 mCapture->expectFGColor(10, 10);
4272 }
4273}
4274
Dan Stoza412903f2017-04-27 13:42:17 -07004275TEST_F(ChildLayerTest, Bug36858924) {
4276 // Destroy the child layer
4277 mChild.clear();
4278
4279 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08004280 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
4281 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07004282
4283 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07004284 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004285 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
4286 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07004287 t.show(mChild);
4288 });
Dan Stoza412903f2017-04-27 13:42:17 -07004289
4290 // Render the foreground surface a few times
4291 //
4292 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
4293 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
4294 // never acquire/release the first buffer
4295 ALOGI("Filling 1");
4296 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
4297 ALOGI("Filling 2");
4298 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
4299 ALOGI("Filling 3");
4300 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
4301 ALOGI("Filling 4");
4302 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
4303}
4304
chaviwf1961f72017-09-18 16:41:07 -07004305TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004306 asTransaction([&](Transaction& t) {
4307 t.show(mChild);
4308 t.setPosition(mChild, 10, 10);
4309 t.setPosition(mFGSurfaceControl, 64, 64);
4310 });
chaviw06178942017-07-27 10:25:59 -07004311
4312 {
chaviw0e3479f2018-09-10 16:49:30 -07004313 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004314 // Top left of foreground must now be visible
4315 mCapture->expectFGColor(64, 64);
4316 // But 10 pixels in we should see the child surface
4317 mCapture->expectChildColor(74, 74);
4318 // And 10 more pixels we should be back to the foreground surface
4319 mCapture->expectFGColor(84, 84);
4320 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004321
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004322 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07004323
chaviw06178942017-07-27 10:25:59 -07004324 {
chaviw0e3479f2018-09-10 16:49:30 -07004325 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004326 mCapture->expectFGColor(64, 64);
4327 // In reparenting we should have exposed the entire foreground surface.
4328 mCapture->expectFGColor(74, 74);
4329 // And the child layer should now begin at 10, 10 (since the BG
4330 // layer is at (0, 0)).
4331 mCapture->expectBGColor(9, 9);
4332 mCapture->expectChildColor(10, 10);
4333 }
4334}
4335
chaviwf1961f72017-09-18 16:41:07 -07004336TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004337 asTransaction([&](Transaction& t) {
4338 t.show(mChild);
4339 t.setPosition(mChild, 10, 10);
4340 t.setPosition(mFGSurfaceControl, 64, 64);
4341 });
chaviwf1961f72017-09-18 16:41:07 -07004342
4343 {
chaviw0e3479f2018-09-10 16:49:30 -07004344 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004345 // Top left of foreground must now be visible
4346 mCapture->expectFGColor(64, 64);
4347 // But 10 pixels in we should see the child surface
4348 mCapture->expectChildColor(74, 74);
4349 // And 10 more pixels we should be back to the foreground surface
4350 mCapture->expectFGColor(84, 84);
4351 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004352 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07004353 {
chaviw0e3479f2018-09-10 16:49:30 -07004354 mCapture = screenshot();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004355 // The surface should now be offscreen.
chaviwf1961f72017-09-18 16:41:07 -07004356 mCapture->expectFGColor(64, 64);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004357 mCapture->expectFGColor(74, 74);
chaviwf1961f72017-09-18 16:41:07 -07004358 mCapture->expectFGColor(84, 84);
4359 }
4360}
4361
4362TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07004363 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08004364 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07004365 ASSERT_TRUE(newSurface->isValid());
4366
4367 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004368 asTransaction([&](Transaction& t) {
4369 t.hide(mChild);
4370 t.show(newSurface);
4371 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004372 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004373 t.setPosition(mFGSurfaceControl, 64, 64);
4374 });
chaviwf1961f72017-09-18 16:41:07 -07004375
4376 {
chaviw0e3479f2018-09-10 16:49:30 -07004377 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004378 // Top left of foreground must now be visible
4379 mCapture->expectFGColor(64, 64);
4380 // At 10, 10 we should see the new surface
4381 mCapture->checkPixel(10, 10, 63, 195, 63);
4382 }
4383
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004384 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07004385
4386 {
chaviw0e3479f2018-09-10 16:49:30 -07004387 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004388 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
4389 // mFGSurface, putting it at 74, 74.
4390 mCapture->expectFGColor(64, 64);
4391 mCapture->checkPixel(74, 74, 63, 195, 63);
4392 mCapture->expectFGColor(84, 84);
4393 }
4394}
4395
chaviwc9674332017-08-28 12:32:18 -07004396TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004397 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
4398 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07004399 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4400
4401 {
chaviw0e3479f2018-09-10 16:49:30 -07004402 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07004403 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
4404 // which begins at 64, 64
4405 mCapture->checkPixel(64, 64, 50, 50, 50);
4406 }
4407}
4408
Robert Carr503c7042017-09-27 15:06:08 -07004409TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004410 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07004411 fillSurfaceRGBA8(relative, 255, 255, 255);
4412
4413 Transaction t;
4414 t.setLayer(relative, INT32_MAX)
4415 .setRelativeLayer(mChild, relative->getHandle(), 1)
4416 .setPosition(mFGSurfaceControl, 0, 0)
4417 .apply(true);
4418
4419 // We expect that the child should have been elevated above our
4420 // INT_MAX layer even though it's not a child of it.
4421 {
chaviw0e3479f2018-09-10 16:49:30 -07004422 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07004423 mCapture->expectChildColor(0, 0);
4424 mCapture->expectChildColor(9, 9);
4425 mCapture->checkPixel(10, 10, 255, 255, 255);
4426 }
4427}
Vishnu Nair60356342018-11-13 13:00:45 -08004428class BoundlessLayerTest : public LayerUpdateTest {
4429protected:
4430 std::unique_ptr<ScreenCapture> mCapture;
4431};
4432
4433// Verify setting a size on a buffer layer has no effect.
4434TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
4435 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004436 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
4437 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004438 ASSERT_TRUE(bufferLayer->isValid());
4439 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
4440 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
4441 {
4442 mCapture = screenshot();
4443 // Top left of background must now be visible
4444 mCapture->expectBGColor(0, 0);
4445 // Foreground Surface bounds must be color layer
4446 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
4447 // Buffer layer should not extend past buffer bounds
4448 mCapture->expectFGColor(95, 95);
4449 }
4450}
4451
4452// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
4453// which will crop the color layer.
4454TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
4455 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004456 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4457 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004458 ASSERT_TRUE(colorLayer->isValid());
4459 asTransaction([&](Transaction& t) {
4460 t.setColor(colorLayer, half3{0, 0, 0});
4461 t.show(colorLayer);
4462 });
4463 {
4464 mCapture = screenshot();
4465 // Top left of background must now be visible
4466 mCapture->expectBGColor(0, 0);
4467 // Foreground Surface bounds must be color layer
4468 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4469 // Color layer should not extend past foreground bounds
4470 mCapture->expectBGColor(129, 129);
4471 }
4472}
4473
4474// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
4475// a crop which will be used to crop the color layer.
4476TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004477 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4478 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004479 ASSERT_TRUE(cropLayer->isValid());
4480 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004481 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4482 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004483 ASSERT_TRUE(colorLayer->isValid());
4484 asTransaction([&](Transaction& t) {
4485 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
4486 t.setColor(colorLayer, half3{0, 0, 0});
4487 t.show(cropLayer);
4488 t.show(colorLayer);
4489 });
4490 {
4491 mCapture = screenshot();
4492 // Top left of background must now be visible
4493 mCapture->expectBGColor(0, 0);
4494 // Top left of foreground must now be visible
4495 mCapture->expectFGColor(64, 64);
4496 // 5 pixels from the foreground we should see the child surface
4497 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
4498 // 10 pixels from the foreground we should be back to the foreground surface
4499 mCapture->expectFGColor(74, 74);
4500 }
4501}
4502
4503// Verify for boundless layer with no children, their transforms have no effect.
4504TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
4505 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004506 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4507 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004508 ASSERT_TRUE(colorLayer->isValid());
4509 asTransaction([&](Transaction& t) {
4510 t.setPosition(colorLayer, 320, 320);
4511 t.setMatrix(colorLayer, 2, 0, 0, 2);
4512 t.setColor(colorLayer, half3{0, 0, 0});
4513 t.show(colorLayer);
4514 });
4515 {
4516 mCapture = screenshot();
4517 // Top left of background must now be visible
4518 mCapture->expectBGColor(0, 0);
4519 // Foreground Surface bounds must be color layer
4520 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4521 // Color layer should not extend past foreground bounds
4522 mCapture->expectBGColor(129, 129);
4523 }
4524}
4525
4526// Verify for boundless layer with children, their transforms have an effect.
4527TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
4528 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004529 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4530 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004531 ASSERT_TRUE(boundlessLayerRightShift->isValid());
4532 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004533 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4534 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004535 ASSERT_TRUE(boundlessLayerDownShift->isValid());
4536 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004537 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4538 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004539 ASSERT_TRUE(colorLayer->isValid());
4540 asTransaction([&](Transaction& t) {
4541 t.setPosition(boundlessLayerRightShift, 32, 0);
4542 t.show(boundlessLayerRightShift);
4543 t.setPosition(boundlessLayerDownShift, 0, 32);
4544 t.show(boundlessLayerDownShift);
4545 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4546 t.setColor(colorLayer, half3{0, 0, 0});
4547 t.show(colorLayer);
4548 });
4549 {
4550 mCapture = screenshot();
4551 // Top left of background must now be visible
4552 mCapture->expectBGColor(0, 0);
4553 // Top left of foreground must now be visible
4554 mCapture->expectFGColor(64, 64);
4555 // Foreground Surface bounds must be color layer
4556 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
4557 // Color layer should not extend past foreground bounds
4558 mCapture->expectBGColor(129, 129);
4559 }
4560}
4561
4562// Verify child layers do not get clipped if they temporarily move into the negative
4563// coordinate space as the result of an intermediate transformation.
4564TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
4565 sp<SurfaceControl> boundlessLayer =
4566 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4567 0 /* flags */, mFGSurfaceControl.get());
4568 ASSERT_TRUE(boundlessLayer != nullptr);
4569 ASSERT_TRUE(boundlessLayer->isValid());
4570 sp<SurfaceControl> colorLayer =
4571 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4572 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
4573 ASSERT_TRUE(colorLayer != nullptr);
4574 ASSERT_TRUE(colorLayer->isValid());
4575 asTransaction([&](Transaction& t) {
4576 // shift child layer off bounds. If this layer was not boundless, we will
4577 // expect the child layer to be cropped.
4578 t.setPosition(boundlessLayer, 32, 32);
4579 t.show(boundlessLayer);
4580 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4581 // undo shift by parent
4582 t.setPosition(colorLayer, -32, -32);
4583 t.setColor(colorLayer, half3{0, 0, 0});
4584 t.show(colorLayer);
4585 });
4586 {
4587 mCapture = screenshot();
4588 // Top left of background must now be visible
4589 mCapture->expectBGColor(0, 0);
4590 // Foreground Surface bounds must be color layer
4591 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4592 // Color layer should not extend past foreground bounds
4593 mCapture->expectBGColor(129, 129);
4594 }
4595}
4596
4597// Verify for boundless root layers with children, their transforms have an effect.
4598TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004599 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
4600 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08004601 ASSERT_TRUE(rootBoundlessLayer->isValid());
4602 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004603 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4604 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
4605
Vishnu Nair60356342018-11-13 13:00:45 -08004606 ASSERT_TRUE(colorLayer->isValid());
4607 asTransaction([&](Transaction& t) {
4608 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
4609 t.setPosition(rootBoundlessLayer, 32, 32);
4610 t.show(rootBoundlessLayer);
4611 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4612 t.setColor(colorLayer, half3{0, 0, 0});
4613 t.show(colorLayer);
4614 t.hide(mFGSurfaceControl);
4615 });
4616 {
4617 mCapture = screenshot();
4618 // Top left of background must now be visible
4619 mCapture->expectBGColor(0, 0);
4620 // Top left of foreground must now be visible
4621 mCapture->expectBGColor(31, 31);
4622 // Foreground Surface bounds must be color layer
4623 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
4624 // Color layer should not extend past foreground bounds
4625 mCapture->expectBGColor(97, 97);
4626 }
4627}
Robert Carr503c7042017-09-27 15:06:08 -07004628
chaviwa76b2712017-09-20 12:02:26 -07004629class ScreenCaptureTest : public LayerUpdateTest {
4630protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004631 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07004632};
4633
4634TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
4635 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004636 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004637 mCapture->expectBGColor(0, 0);
4638 // Doesn't capture FG layer which is at 64, 64
4639 mCapture->expectBGColor(64, 64);
4640}
4641
4642TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
4643 auto fgHandle = mFGSurfaceControl->getHandle();
4644
Vishnu Nair88a11f22018-11-28 18:30:57 -08004645 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4646 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004647 fillSurfaceRGBA8(child, 200, 200, 200);
4648
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004649 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004650
4651 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004652 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004653 mCapture->expectFGColor(10, 10);
4654 mCapture->expectChildColor(0, 0);
4655}
4656
Robert Carr578038f2018-03-09 12:25:24 -08004657TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
4658 auto fgHandle = mFGSurfaceControl->getHandle();
4659
Vishnu Nair88a11f22018-11-28 18:30:57 -08004660 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4661 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004662 fillSurfaceRGBA8(child, 200, 200, 200);
4663
4664 SurfaceComposerClient::Transaction().show(child).apply(true);
4665
4666 // Captures mFGSurfaceControl's child
4667 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4668 mCapture->checkPixel(10, 10, 0, 0, 0);
4669 mCapture->expectChildColor(0, 0);
4670}
4671
chaviw50da5042018-04-09 13:49:37 -07004672TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004673 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4674 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07004675
4676 fillSurfaceRGBA8(child, 200, 200, 200);
4677
4678 SurfaceComposerClient::Transaction().show(child).apply(true);
4679
4680 auto childHandle = child->getHandle();
4681
4682 // Captures child
4683 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
4684 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
4685 // Area outside of child's bounds is transparent.
4686 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
4687}
4688
chaviw4b129c22018-04-09 16:19:43 -07004689TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
4690 auto fgHandle = mFGSurfaceControl->getHandle();
4691
Vishnu Nair88a11f22018-11-28 18:30:57 -08004692 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4693 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4694 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07004695 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07004696 fillSurfaceRGBA8(child, 200, 200, 200);
4697 fillSurfaceRGBA8(relative, 100, 100, 100);
4698
4699 SurfaceComposerClient::Transaction()
4700 .show(child)
4701 // Set relative layer above fg layer so should be shown above when computing all layers.
4702 .setRelativeLayer(relative, fgHandle, 1)
4703 .show(relative)
4704 .apply(true);
4705
4706 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
4707 ScreenCapture::captureLayers(&mCapture, fgHandle);
4708 mCapture->expectFGColor(10, 10);
4709 mCapture->expectChildColor(0, 0);
4710}
4711
4712TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
4713 auto fgHandle = mFGSurfaceControl->getHandle();
4714
Vishnu Nair88a11f22018-11-28 18:30:57 -08004715 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4716 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4717 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
4718 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07004719 fillSurfaceRGBA8(child, 200, 200, 200);
4720 fillSurfaceRGBA8(relative, 100, 100, 100);
4721
4722 SurfaceComposerClient::Transaction()
4723 .show(child)
4724 // Set relative layer below fg layer but relative to child layer so it should be shown
4725 // above child layer.
4726 .setLayer(relative, -1)
4727 .setRelativeLayer(relative, child->getHandle(), 1)
4728 .show(relative)
4729 .apply(true);
4730
4731 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
4732 // relative value should be taken into account, placing it above child layer.
4733 ScreenCapture::captureLayers(&mCapture, fgHandle);
4734 mCapture->expectFGColor(10, 10);
4735 // Relative layer is showing on top of child layer
4736 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
4737}
Robert Carr578038f2018-03-09 12:25:24 -08004738
4739// In the following tests we verify successful skipping of a parent layer,
4740// so we use the same verification logic and only change how we mutate
4741// the parent layer to verify that various properties are ignored.
4742class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
4743public:
4744 void SetUp() override {
4745 LayerUpdateTest::SetUp();
4746
Vishnu Nair88a11f22018-11-28 18:30:57 -08004747 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
4748 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004749 fillSurfaceRGBA8(mChild, 200, 200, 200);
4750
4751 SurfaceComposerClient::Transaction().show(mChild).apply(true);
4752 }
4753
4754 void verify() {
4755 auto fgHandle = mFGSurfaceControl->getHandle();
4756 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4757 mCapture->checkPixel(10, 10, 0, 0, 0);
4758 mCapture->expectChildColor(0, 0);
4759 }
4760
4761 std::unique_ptr<ScreenCapture> mCapture;
4762 sp<SurfaceControl> mChild;
4763};
4764
4765TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
4766
4767 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4768
4769 // Even though the parent is hidden we should still capture the child.
4770 verify();
4771}
4772
4773TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004774 SurfaceComposerClient::Transaction()
4775 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
4776 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08004777
4778 // Even though the parent is cropped out we should still capture the child.
4779 verify();
4780}
4781
4782TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
4783
4784 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
4785
4786 // We should not inherit the parent scaling.
4787 verify();
4788}
4789
Robert Carr15eae092018-03-23 13:43:53 -07004790TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
4791 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
4792
4793 // Even though the parent is hidden we should still capture the child.
4794 verify();
4795
4796 // Verify everything was properly hidden when rendering the full-screen.
4797 screenshot()->expectBGColor(0,0);
4798}
4799
4800
chaviwa76b2712017-09-20 12:02:26 -07004801TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
4802 auto fgHandle = mFGSurfaceControl->getHandle();
4803
Vishnu Nair88a11f22018-11-28 18:30:57 -08004804 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4805 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004806 fillSurfaceRGBA8(child, 200, 200, 200);
4807
Vishnu Nair88a11f22018-11-28 18:30:57 -08004808 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4809 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004810
4811 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4812 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004813 .show(child)
4814 .setPosition(grandchild, 5, 5)
4815 .show(grandchild)
4816 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004817
4818 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004819 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004820 mCapture->expectFGColor(10, 10);
4821 mCapture->expectChildColor(0, 0);
4822 mCapture->checkPixel(5, 5, 50, 50, 50);
4823}
4824
4825TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004826 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4827 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004828 fillSurfaceRGBA8(child, 200, 200, 200);
4829 auto childHandle = child->getHandle();
4830
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004831 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004832
4833 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004834 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07004835 mCapture->expectChildColor(0, 0);
4836 mCapture->expectChildColor(9, 9);
4837}
4838
4839TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004840 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4841 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004842 fillSurfaceRGBA8(child, 200, 200, 200);
4843 auto childHandle = child->getHandle();
4844
Vishnu Nair88a11f22018-11-28 18:30:57 -08004845 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
4846 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07004847 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4848
4849 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004850 .show(child)
4851 .setPosition(grandchild, 5, 5)
4852 .show(grandchild)
4853 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004854
4855 auto grandchildHandle = grandchild->getHandle();
4856
4857 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004858 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07004859 mCapture->checkPixel(0, 0, 50, 50, 50);
4860 mCapture->checkPixel(4, 4, 50, 50, 50);
4861}
4862
chaviw7206d492017-11-10 16:16:12 -08004863TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07004864 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004865 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4866 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004867
Marissa Wall61c58622018-07-18 10:12:20 -07004868 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4869 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004870
4871 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004872 .setLayer(redLayer, INT32_MAX - 1)
4873 .show(redLayer)
4874 .show(blueLayer)
4875 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004876
4877 auto redLayerHandle = redLayer->getHandle();
4878
4879 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004880 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4881 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4882 // red area below the blue area
4883 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4884 // red area to the right of the blue area
4885 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004886
Marissa Wall861616d2018-10-22 12:52:23 -07004887 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004888 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08004889 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
4890 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004891 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08004892 mCapture->checkPixel(30, 30, 0, 0, 0);
4893}
4894
4895TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07004896 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08004897 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
4898 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08004899
Marissa Wall61c58622018-07-18 10:12:20 -07004900 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
4901 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08004902
4903 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004904 .setLayer(redLayer, INT32_MAX - 1)
4905 .show(redLayer)
4906 .show(blueLayer)
4907 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08004908
4909 auto redLayerHandle = redLayer->getHandle();
4910
4911 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004912 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
4913 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
4914 // red area below the blue area
4915 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
4916 // red area to the right of the blue area
4917 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004918
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004919 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08004920 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004921 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
4922 // red area below the blue area
4923 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
4924 // red area to the right of the blue area
4925 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08004926 mCapture->checkPixel(30, 30, 0, 0, 0);
4927}
4928
4929TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004930 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08004931
Marissa Wall61c58622018-07-18 10:12:20 -07004932 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08004933
4934 auto redLayerHandle = redLayer->getHandle();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004935 redLayer->clear();
chaviw7206d492017-11-10 16:16:12 -08004936 SurfaceComposerClient::Transaction().apply(true);
4937
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004938 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08004939
4940 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004941 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
4942 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08004943}
4944
chaviw8e3fe5d2018-02-22 10:55:42 -08004945
4946class DereferenceSurfaceControlTest : public LayerTransactionTest {
4947protected:
4948 void SetUp() override {
4949 LayerTransactionTest::SetUp();
4950 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004951 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004952 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07004953 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08004954 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
4955 {
4956 SCOPED_TRACE("before anything");
4957 auto shot = screenshot();
4958 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4959 }
4960 }
4961 void TearDown() override {
4962 LayerTransactionTest::TearDown();
4963 bgLayer = 0;
4964 fgLayer = 0;
4965 }
4966
4967 sp<SurfaceControl> bgLayer;
4968 sp<SurfaceControl> fgLayer;
4969};
4970
4971TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
4972 fgLayer = nullptr;
4973 {
4974 SCOPED_TRACE("after setting null");
4975 auto shot = screenshot();
4976 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
4977 }
4978}
4979
4980TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
4981 auto transaction = Transaction().show(fgLayer);
4982 fgLayer = nullptr;
4983 {
4984 SCOPED_TRACE("after setting null");
4985 auto shot = screenshot();
4986 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
4987 }
4988}
4989
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004990} // namespace android