blob: 57a2227593eaca29795eee8cc857a77676594003 [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu718daf82017-10-20 11:57:17 -070017#include <algorithm>
18#include <functional>
19#include <limits>
20#include <ostream>
21
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070022#include <gtest/gtest.h>
23
Michael Lentine5a16a622015-05-21 13:48:24 -070024#include <android/native_window.h>
25
Mathias Agopian90ac7992012-02-25 18:48:35 -080026#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070027#include <gui/LayerState.h>
28
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/Surface.h>
30#include <gui/SurfaceComposerClient.h>
31#include <private/gui/ComposerService.h>
32
Ady Abraham2a6ab2a2018-10-26 14:25:30 -070033#include <ui/ColorSpace.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070034#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070035#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070036#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070037
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070038#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070039#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070040
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070041namespace android {
42
Chia-I Wu718daf82017-10-20 11:57:17 -070043namespace {
44
45struct Color {
46 uint8_t r;
47 uint8_t g;
48 uint8_t b;
49 uint8_t a;
50
51 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070052 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070053 static const Color BLUE;
Chia-I Wu93853fe2017-11-02 08:30:27 -070054 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070055 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070056 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070057};
58
59const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070060const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070061const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070062const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070063const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070064const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070065
Marissa Wall61c58622018-07-18 10:12:20 -070066using android::hardware::graphics::common::V1_1::BufferUsage;
67
Chia-I Wu718daf82017-10-20 11:57:17 -070068std::ostream& operator<<(std::ostream& os, const Color& color) {
69 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
70 return os;
71}
72
73// Fill a region with the specified color.
Marissa Wall61c58622018-07-18 10:12:20 -070074void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
75 const Color& color) {
76 Rect r(0, 0, buffer.width, buffer.height);
77 if (!r.intersect(rect, &r)) {
78 return;
Chia-I Wu718daf82017-10-20 11:57:17 -070079 }
80
Marissa Wall61c58622018-07-18 10:12:20 -070081 int32_t width = r.right - r.left;
82 int32_t height = r.bottom - r.top;
83
84 for (int32_t row = 0; row < height; row++) {
85 uint8_t* dst =
86 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
87 for (int32_t column = 0; column < width; column++) {
Chia-I Wu718daf82017-10-20 11:57:17 -070088 dst[0] = color.r;
89 dst[1] = color.g;
90 dst[2] = color.b;
91 dst[3] = color.a;
92 dst += 4;
93 }
94 }
95}
96
Marissa Wall61c58622018-07-18 10:12:20 -070097// Fill a region with the specified color.
98void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
99 Rect r(0, 0, buffer->width, buffer->height);
100 if (!r.intersect(rect, &r)) {
101 return;
102 }
103
104 int32_t width = r.right - r.left;
105 int32_t height = r.bottom - r.top;
106
107 uint8_t* pixels;
108 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
109 reinterpret_cast<void**>(&pixels));
110
111 for (int32_t row = 0; row < height; row++) {
112 uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
113 for (int32_t column = 0; column < width; column++) {
114 dst[0] = color.r;
115 dst[1] = color.g;
116 dst[2] = color.b;
117 dst[3] = color.a;
118 dst += 4;
119 }
120 }
121 buffer->unlock();
122}
123
Chia-I Wu718daf82017-10-20 11:57:17 -0700124// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000125void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700126 const Color& color, uint8_t tolerance) {
127 int32_t x = rect.left;
128 int32_t y = rect.top;
129 int32_t width = rect.right - rect.left;
130 int32_t height = rect.bottom - rect.top;
131
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000132 int32_t bufferWidth = int32_t(outBuffer->getWidth());
133 int32_t bufferHeight = int32_t(outBuffer->getHeight());
134 if (x + width > bufferWidth) {
135 x = std::min(x, bufferWidth);
136 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700137 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000138 if (y + height > bufferHeight) {
139 y = std::min(y, bufferHeight);
140 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700141 }
142
143 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
144 uint8_t tmp = a >= b ? a - b : b - a;
145 return tmp <= tolerance;
146 };
147 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000148 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700149 for (int32_t i = 0; i < width; i++) {
150 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
151 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
152 << "pixel @ (" << x + i << ", " << y + j << "): "
153 << "expected (" << color << "), "
154 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
155 src += 4;
156 }
157 }
158}
159
160} // anonymous namespace
161
Robert Carr4cdc58f2017-08-23 14:22:20 -0700162using Transaction = SurfaceComposerClient::Transaction;
163
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700164// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700165static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
166 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800167 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700168 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800169 ASSERT_TRUE(s != nullptr);
170 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800171 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700172 for (int y = 0; y < outBuffer.height; y++) {
173 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700174 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700175 pixel[0] = r;
176 pixel[1] = g;
177 pixel[2] = b;
178 pixel[3] = 255;
179 }
180 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700181 if (unlock) {
182 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
183 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700184}
185
186// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
187// individual pixel values for testing purposes.
188class ScreenCapture : public RefBase {
189public:
chaviw0e3479f2018-09-10 16:49:30 -0700190 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700191 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700192 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700193 SurfaceComposerClient::Transaction().apply(true);
194
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000195 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700196 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -0700197 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, false));
198 *sc = std::make_unique<ScreenCapture>(outBuffer);
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000199 }
200
201 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
202 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
203 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
204 SurfaceComposerClient::Transaction().apply(true);
205
206 sp<GraphicBuffer> outBuffer;
207 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
208 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700209 }
210
Robert Carr578038f2018-03-09 12:25:24 -0800211 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
212 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
213 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
214 SurfaceComposerClient::Transaction().apply(true);
215
216 sp<GraphicBuffer> outBuffer;
217 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
218 *sc = std::make_unique<ScreenCapture>(outBuffer);
219 }
220
Chia-I Wu718daf82017-10-20 11:57:17 -0700221 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000222 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
223 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700224 }
225
226 void expectBorder(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());
Chia-I Wu718daf82017-10-20 11:57:17 -0700228 const bool leftBorder = rect.left > 0;
229 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000230 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
231 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700232
233 if (topBorder) {
234 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
235 if (leftBorder) {
236 top.left -= 1;
237 }
238 if (rightBorder) {
239 top.right += 1;
240 }
241 expectColor(top, color, tolerance);
242 }
243 if (leftBorder) {
244 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
245 expectColor(left, color, tolerance);
246 }
247 if (rightBorder) {
248 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
249 expectColor(right, color, tolerance);
250 }
251 if (bottomBorder) {
252 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
253 if (leftBorder) {
254 bottom.left -= 1;
255 }
256 if (rightBorder) {
257 bottom.right += 1;
258 }
259 expectColor(bottom, color, tolerance);
260 }
261 }
262
Chia-I Wu93853fe2017-11-02 08:30:27 -0700263 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
264 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
265 uint8_t tolerance = 0) {
266 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
267
268 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
269 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
270 // avoid checking borders due to unspecified filtering behavior
271 const int32_t offsetX = filtered ? 2 : 0;
272 const int32_t offsetY = filtered ? 2 : 0;
273 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
274 tolerance);
275 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
276 tolerance);
277 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
278 tolerance);
279 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
280 bottomRight, tolerance);
281 }
282
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700283 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000284 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
285 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700286 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
287 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700288 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
289 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700290 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700291 }
292 }
293
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700294 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700295
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700296 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700297
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700298 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700299
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000300 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
301 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700302 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700303
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000304 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700305
306private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000307 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800308 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700309};
310
Chia-I Wu718daf82017-10-20 11:57:17 -0700311class LayerTransactionTest : public ::testing::Test {
312protected:
313 void SetUp() override {
314 mClient = new SurfaceComposerClient;
315 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
316
317 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700318
319 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
320 sp<IBinder> binder = sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
321 mColorManagementUsed = sf->isColorManagementUsed();
Chia-I Wu718daf82017-10-20 11:57:17 -0700322 }
323
chaviw0e3479f2018-09-10 16:49:30 -0700324 virtual void TearDown() {
325 mBlackBgSurface = 0;
326 mClient->dispose();
327 mClient = 0;
328 }
329
Marissa Wall61c58622018-07-18 10:12:20 -0700330 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
331 uint32_t flags = 0) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700332 auto layer =
333 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
334 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
335
336 status_t error = Transaction()
337 .setLayerStack(layer, mDisplayLayerStack)
338 .setLayer(layer, mLayerZBase)
339 .apply();
340 if (error != NO_ERROR) {
341 ADD_FAILURE() << "failed to initialize SurfaceControl";
342 layer.clear();
343 }
344
345 return layer;
346 }
347
Marissa Wall61c58622018-07-18 10:12:20 -0700348 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700349 // wait for previous transactions (such as setSize) to complete
350 Transaction().apply(true);
351
352 ANativeWindow_Buffer buffer = {};
353 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
354
355 return buffer;
356 }
357
Marissa Wall61c58622018-07-18 10:12:20 -0700358 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700359 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
360
361 // wait for the newly posted buffer to be latched
362 waitForLayerBuffers();
363 }
364
Marissa Wall61c58622018-07-18 10:12:20 -0700365 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
366 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700367 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700368 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
369 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
370 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700371 }
372
Marissa Wall61c58622018-07-18 10:12:20 -0700373 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
374 int32_t bufferWidth, int32_t bufferHeight) {
375 sp<GraphicBuffer> buffer =
376 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
377 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
378 BufferUsage::COMPOSER_OVERLAY,
379 "test");
380 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
381 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
382 }
383
384 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
385 int32_t bufferWidth, int32_t bufferHeight) {
386 switch (mLayerType) {
387 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
388 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
389 break;
390 case ISurfaceComposerClient::eFXSurfaceBufferState:
391 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
392 break;
393 default:
394 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
395 }
396 }
397
398 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
399 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700400 const Color& topRight, const Color& bottomLeft,
401 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700402 switch (mLayerType) {
403 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
404 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
405 bottomLeft, bottomRight);
406 break;
407 case ISurfaceComposerClient::eFXSurfaceBufferState:
408 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
409 bottomLeft, bottomRight);
410 break;
411 default:
412 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
413 }
414 }
415
416 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
417 int32_t bufferHeight, const Color& topLeft,
418 const Color& topRight, const Color& bottomLeft,
419 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700420 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700421 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
422 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700423
Marissa Wall61c58622018-07-18 10:12:20 -0700424 const int32_t halfW = bufferWidth / 2;
425 const int32_t halfH = bufferHeight / 2;
426 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
427 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
428 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
429 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
430 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700431
Marissa Wall61c58622018-07-18 10:12:20 -0700432 postBufferQueueLayerBuffer(layer);
433 }
434
435 virtual void fillBufferStateLayerQuadrant(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) {
439 sp<GraphicBuffer> buffer =
440 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
441 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
442 BufferUsage::COMPOSER_OVERLAY,
443 "test");
444
445 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
446
447 const int32_t halfW = bufferWidth / 2;
448 const int32_t halfH = bufferHeight / 2;
449 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
450 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
451 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
452 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
453
454 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700455 }
456
chaviw0e3479f2018-09-10 16:49:30 -0700457 std::unique_ptr<ScreenCapture> screenshot() {
458 std::unique_ptr<ScreenCapture> screenshot;
459 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700460 return screenshot;
461 }
462
463 sp<SurfaceComposerClient> mClient;
464
465 sp<IBinder> mDisplay;
466 uint32_t mDisplayWidth;
467 uint32_t mDisplayHeight;
468 uint32_t mDisplayLayerStack;
469
470 // leave room for ~256 layers
471 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
472
Marissa Wall61c58622018-07-18 10:12:20 -0700473 void setPositionWithResizeHelper(uint32_t layerType);
474 void setSizeBasicHelper(uint32_t layerType);
475 void setMatrixWithResizeHelper(uint32_t layerType);
476
chaviw0e3479f2018-09-10 16:49:30 -0700477 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700478 bool mColorManagementUsed;
479
Chia-I Wu718daf82017-10-20 11:57:17 -0700480private:
481 void SetUpDisplay() {
482 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
483 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
484
485 // get display width/height
486 DisplayInfo info;
487 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
488 mDisplayWidth = info.w;
489 mDisplayHeight = info.h;
490
491 // After a new buffer is queued, SurfaceFlinger is notified and will
492 // latch the new buffer on next vsync. Let's heuristically wait for 3
493 // vsyncs.
494 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
495
496 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700497
498 mBlackBgSurface = mClient->createSurface(String8("BaseSurface"), mDisplayWidth,
499 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
500 ISurfaceComposerClient::eFXSurfaceColor);
501
Chia-I Wu718daf82017-10-20 11:57:17 -0700502 // set layer stack (b/68888219)
503 Transaction t;
504 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
chaviw0e3479f2018-09-10 16:49:30 -0700505 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
506 t.setColor(mBlackBgSurface, half3{0, 0, 0});
507 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700508 t.apply();
509 }
510
chaviw0e3479f2018-09-10 16:49:30 -0700511 void waitForLayerBuffers() {
512 // Request an empty transaction to get applied synchronously to ensure the buffer is
513 // latched.
514 Transaction().apply(true);
515 usleep(mBufferPostDelay);
516 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700517
518 int32_t mBufferPostDelay;
519};
520
Marissa Wall61c58622018-07-18 10:12:20 -0700521class LayerTypeTransactionTest : public LayerTransactionTest,
522 public ::testing::WithParamInterface<uint32_t> {
523public:
524 LayerTypeTransactionTest() { mLayerType = GetParam(); }
525
526 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
527 uint32_t flags = 0) override {
528 // if the flags already have a layer type specified, return an error
529 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
530 return nullptr;
531 }
532 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType);
533 }
534
535 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
536 int32_t bufferHeight) {
537 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
538 bufferWidth, bufferHeight));
539 }
540
541 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
542 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
543 const Color& bottomLeft, const Color& bottomRight) {
544 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
545 bufferWidth, bufferHeight,
546 topLeft, topRight,
547 bottomLeft, bottomRight));
548 }
549
550protected:
551 uint32_t mLayerType;
552};
553
554INSTANTIATE_TEST_CASE_P(
555 LayerTypeTransactionTests, LayerTypeTransactionTest,
556 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
557 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
558
559TEST_P(LayerTypeTransactionTest, SetPositionBasic) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700560 sp<SurfaceControl> layer;
561 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700562 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700563
564 {
565 SCOPED_TRACE("default position");
566 auto shot = screenshot();
567 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
568 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
569 }
570
571 Transaction().setPosition(layer, 5, 10).apply();
572 {
573 SCOPED_TRACE("new position");
574 auto shot = screenshot();
575 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
576 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
577 }
578}
579
Marissa Wall61c58622018-07-18 10:12:20 -0700580TEST_P(LayerTypeTransactionTest, SetPositionRounding) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700581 sp<SurfaceControl> layer;
582 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700583 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700584
585 // GLES requires only 4 bits of subpixel precision during rasterization
586 // XXX GLES composition does not match HWC composition due to precision
587 // loss (b/69315223)
588 const float epsilon = 1.0f / 16.0f;
589 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
590 {
591 SCOPED_TRACE("rounding down");
592 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
593 }
594
595 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
596 {
597 SCOPED_TRACE("rounding up");
598 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
599 }
600}
601
Marissa Wall61c58622018-07-18 10:12:20 -0700602TEST_P(LayerTypeTransactionTest, SetPositionOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700603 sp<SurfaceControl> layer;
604 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700605 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700606
607 Transaction().setPosition(layer, -32, -32).apply();
608 {
609 SCOPED_TRACE("negative coordinates");
610 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
611 }
612
613 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
614 {
615 SCOPED_TRACE("positive coordinates");
616 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
617 }
618}
619
Marissa Wall61c58622018-07-18 10:12:20 -0700620TEST_P(LayerTypeTransactionTest, SetPositionPartiallyOutOfBounds) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700621 sp<SurfaceControl> layer;
622 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700623 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700624
625 // partially out of bounds
626 Transaction().setPosition(layer, -30, -30).apply();
627 {
628 SCOPED_TRACE("negative coordinates");
629 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
630 }
631
632 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
633 {
634 SCOPED_TRACE("positive coordinates");
635 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
636 mDisplayHeight),
637 Color::RED);
638 }
639}
640
Marissa Wall61c58622018-07-18 10:12:20 -0700641void LayerTransactionTest::setPositionWithResizeHelper(uint32_t layerType) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700642 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700643 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
644 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700645
646 // setPosition is applied immediately by default, with or without resize
647 // pending
648 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
649 {
650 SCOPED_TRACE("resize pending");
651 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700652 Rect rect;
653 switch (layerType) {
654 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
655 rect = {5, 10, 37, 42};
656 break;
657 case ISurfaceComposerClient::eFXSurfaceBufferState:
658 rect = {5, 10, 69, 74};
659 break;
660 default:
661 ASSERT_FALSE(true) << "Unsupported layer type";
662 }
663
664 shot->expectColor(rect, Color::RED);
665 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700666 }
667
Marissa Wall61c58622018-07-18 10:12:20 -0700668 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700669 {
670 SCOPED_TRACE("resize applied");
671 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
672 }
673}
674
Marissa Wall61c58622018-07-18 10:12:20 -0700675TEST_F(LayerTransactionTest, SetPositionWithResize_BufferQueue) {
676 ASSERT_NO_FATAL_FAILURE(
677 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
678}
679
680TEST_F(LayerTransactionTest, SetPositionWithResize_BufferState) {
681 ASSERT_NO_FATAL_FAILURE(
682 setPositionWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
683}
684
685TEST_F(LayerTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700686 sp<SurfaceControl> layer;
687 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700688 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700689
690 // request setPosition to be applied with the next resize
691 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
692 {
693 SCOPED_TRACE("new position pending");
694 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
695 }
696
697 Transaction().setPosition(layer, 15, 20).apply();
698 {
699 SCOPED_TRACE("pending new position modified");
700 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
701 }
702
703 Transaction().setSize(layer, 64, 64).apply();
704 {
705 SCOPED_TRACE("resize pending");
706 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
707 }
708
709 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700710 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700711 {
712 SCOPED_TRACE("new position applied");
713 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
714 }
715}
716
Marissa Wall61c58622018-07-18 10:12:20 -0700717TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700718 sp<SurfaceControl> layer;
719 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700720 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700721
722 // setPosition is not immediate even with SCALE_TO_WINDOW override
723 Transaction()
724 .setPosition(layer, 5, 10)
725 .setSize(layer, 64, 64)
726 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
727 .setGeometryAppliesWithResize(layer)
728 .apply();
729 {
730 SCOPED_TRACE("new position pending");
731 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
732 }
733
Marissa Wall61c58622018-07-18 10:12:20 -0700734 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700735 {
736 SCOPED_TRACE("new position applied");
737 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
738 }
739}
740
Marissa Wall61c58622018-07-18 10:12:20 -0700741void LayerTransactionTest::setSizeBasicHelper(uint32_t layerType) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700742 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -0700743 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
744 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700745
746 Transaction().setSize(layer, 64, 64).apply();
747 {
748 SCOPED_TRACE("resize pending");
749 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -0700750 Rect rect;
751 switch (layerType) {
752 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
753 rect = {0, 0, 32, 32};
754 break;
755 case ISurfaceComposerClient::eFXSurfaceBufferState:
756 rect = {0, 0, 64, 64};
757 break;
758 default:
759 ASSERT_FALSE(true) << "Unsupported layer type";
760 }
761 shot->expectColor(rect, Color::RED);
762 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700763 }
764
Marissa Wall61c58622018-07-18 10:12:20 -0700765 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700766 {
767 SCOPED_TRACE("resize applied");
768 auto shot = screenshot();
769 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
770 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
771 }
772}
773
Marissa Wall61c58622018-07-18 10:12:20 -0700774TEST_F(LayerTransactionTest, SetSizeBasic_BufferQueue) {
775 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue);
776}
777
778TEST_F(LayerTransactionTest, SetSizeBasic_BufferState) {
779 setSizeBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState);
780}
781
782TEST_P(LayerTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700783 // cannot test robustness against invalid sizes (zero or really huge)
784}
785
Marissa Wall61c58622018-07-18 10:12:20 -0700786TEST_P(LayerTypeTransactionTest, SetSizeWithScaleToWindow) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700787 sp<SurfaceControl> layer;
788 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700789 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700790
791 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
792 Transaction()
793 .setSize(layer, 64, 64)
794 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
795 .apply();
796 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
797}
798
Marissa Wall61c58622018-07-18 10:12:20 -0700799TEST_P(LayerTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700800 sp<SurfaceControl> layerR;
801 sp<SurfaceControl> layerG;
802 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700803 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700804 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700805 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700806
807 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
808 {
809 SCOPED_TRACE("layerR");
810 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
811 }
812
813 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
814 {
815 SCOPED_TRACE("layerG");
816 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
817 }
818}
819
Marissa Wall61c58622018-07-18 10:12:20 -0700820TEST_P(LayerTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700821 sp<SurfaceControl> parent =
822 LayerTransactionTest::createLayer("Parent", mDisplayWidth, mDisplayHeight,
823 ISurfaceComposerClient::eFXSurfaceContainer);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700824 sp<SurfaceControl> layerR;
825 sp<SurfaceControl> layerG;
826 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700827 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700828 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700829 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700830
chaviw0e3479f2018-09-10 16:49:30 -0700831 Transaction()
832 .reparent(layerR, parent->getHandle())
833 .reparent(layerG, parent->getHandle())
834 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700835 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
836 {
837 SCOPED_TRACE("layerR");
chaviw0e3479f2018-09-10 16:49:30 -0700838 auto shot = screenshot();
839 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700840 }
841
842 Transaction().setLayer(layerR, -3).apply();
843 {
844 SCOPED_TRACE("layerG");
chaviw0e3479f2018-09-10 16:49:30 -0700845 auto shot = screenshot();
846 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700847 }
848}
849
Marissa Wall61c58622018-07-18 10:12:20 -0700850TEST_P(LayerTypeTransactionTest, SetRelativeZBasic) {
Chia-I Wu49313302017-10-31 10:14:40 -0700851 sp<SurfaceControl> layerR;
852 sp<SurfaceControl> layerG;
853 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700854 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700855 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700856 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700857
858 Transaction()
859 .setPosition(layerG, 16, 16)
860 .setRelativeLayer(layerG, layerR->getHandle(), 1)
861 .apply();
862 {
863 SCOPED_TRACE("layerG above");
864 auto shot = screenshot();
865 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
866 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
867 }
868
869 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
870 {
871 SCOPED_TRACE("layerG below");
872 auto shot = screenshot();
873 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
874 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
875 }
876}
877
Marissa Wall61c58622018-07-18 10:12:20 -0700878TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700879 sp<SurfaceControl> parent =
880 LayerTransactionTest::createLayer("Parent", mDisplayWidth, mDisplayHeight,
881 ISurfaceComposerClient::eFXSurfaceContainer);
Chia-I Wuec2d9852017-11-21 09:21:01 -0800882 sp<SurfaceControl> layerR;
883 sp<SurfaceControl> layerG;
884 sp<SurfaceControl> layerB;
885 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700886 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800887 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700888 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800889 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700890 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800891
chaviw0e3479f2018-09-10 16:49:30 -0700892 Transaction()
893 .reparent(layerB, parent->getHandle())
894 .apply();
895
Chia-I Wuec2d9852017-11-21 09:21:01 -0800896 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
897 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
898
chaviw0e3479f2018-09-10 16:49:30 -0700899 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800900 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700901 sp<IBinder> parentHandle = parent->getHandle();
902 ScreenCapture::captureLayers(&screenshot, parentHandle);
Chia-I Wuec2d9852017-11-21 09:21:01 -0800903 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
904}
905
Marissa Wall61c58622018-07-18 10:12:20 -0700906TEST_P(LayerTypeTransactionTest, SetRelativeZGroup) {
Chia-I Wu49313302017-10-31 10:14:40 -0700907 sp<SurfaceControl> layerR;
908 sp<SurfaceControl> layerG;
909 sp<SurfaceControl> layerB;
910 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700911 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700912 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700913 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700914 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700915 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700916
917 // layerR = 0, layerG = layerR + 3, layerB = 2
918 Transaction()
919 .setPosition(layerG, 8, 8)
920 .setRelativeLayer(layerG, layerR->getHandle(), 3)
921 .setPosition(layerB, 16, 16)
922 .setLayer(layerB, mLayerZBase + 2)
923 .apply();
924 {
925 SCOPED_TRACE("(layerR < layerG) < layerB");
926 auto shot = screenshot();
927 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
928 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
929 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
930 }
931
932 // layerR = 4, layerG = layerR + 3, layerB = 2
933 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
934 {
935 SCOPED_TRACE("layerB < (layerR < layerG)");
936 auto shot = screenshot();
937 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
938 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
939 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
940 }
941
942 // layerR = 4, layerG = layerR - 3, layerB = 2
943 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
944 {
945 SCOPED_TRACE("layerB < (layerG < layerR)");
946 auto shot = screenshot();
947 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
948 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
949 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
950 }
951
952 // restore to absolute z
953 // layerR = 4, layerG = 0, layerB = 2
954 Transaction().setLayer(layerG, mLayerZBase).apply();
955 {
956 SCOPED_TRACE("layerG < layerB < layerR");
957 auto shot = screenshot();
958 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
959 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
960 }
961
962 // layerR should not affect layerG anymore
963 // layerR = 1, layerG = 0, layerB = 2
964 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
965 {
966 SCOPED_TRACE("layerG < layerR < layerB");
967 auto shot = screenshot();
968 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
969 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
970 }
971}
972
Marissa Wall61c58622018-07-18 10:12:20 -0700973TEST_P(LayerTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -0700974 sp<SurfaceControl> layerR;
975 sp<SurfaceControl> layerG;
976
977 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700978 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700979 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700980 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700981
982 Transaction()
983 .setPosition(layerG, 16, 16)
984 .setRelativeLayer(layerG, layerR->getHandle(), 1)
985 .apply();
986
987 mClient->destroySurface(layerG->getHandle());
988 // layerG should have been removed
989 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
990}
991
Marissa Wall61c58622018-07-18 10:12:20 -0700992TEST_P(LayerTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -0700993 sp<SurfaceControl> layer;
994 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700995 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700996
997 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
998 {
999 SCOPED_TRACE("layer hidden");
1000 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1001 }
1002
1003 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1004 {
1005 SCOPED_TRACE("layer shown");
1006 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1007 }
1008}
1009
Marissa Wall61c58622018-07-18 10:12:20 -07001010TEST_P(LayerTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001011 const Color translucentRed = {100, 0, 0, 100};
1012 sp<SurfaceControl> layerR;
1013 sp<SurfaceControl> layerG;
1014 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001015 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001016 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001017 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001018
1019 Transaction()
1020 .setLayer(layerR, mLayerZBase + 1)
1021 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1022 .apply();
1023 {
1024 SCOPED_TRACE("layerR opaque");
1025 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
1026 }
1027
1028 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1029 {
1030 SCOPED_TRACE("layerR translucent");
1031 const uint8_t g = uint8_t(255 - translucentRed.a);
1032 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
1033 }
1034}
1035
Marissa Wall61c58622018-07-18 10:12:20 -07001036TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001037 sp<SurfaceControl> layer;
1038 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001039 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001040
1041 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001042 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001043 Transaction()
1044 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1045 .apply(true);
1046 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001047 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001048
1049 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1050 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001051 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001052}
1053
Marissa Wall61c58622018-07-18 10:12:20 -07001054TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001055 const Rect top(0, 0, 32, 16);
1056 const Rect bottom(0, 16, 32, 32);
1057 sp<SurfaceControl> layer;
1058 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1059
1060 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001061 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1062 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1063 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001064 // setTransparentRegionHint always applies to the following buffer
1065 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001066 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001067 {
1068 SCOPED_TRACE("top transparent");
1069 auto shot = screenshot();
1070 shot->expectColor(top, Color::BLACK);
1071 shot->expectColor(bottom, Color::RED);
1072 }
1073
1074 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1075 {
1076 SCOPED_TRACE("transparent region hint pending");
1077 auto shot = screenshot();
1078 shot->expectColor(top, Color::BLACK);
1079 shot->expectColor(bottom, Color::RED);
1080 }
1081
Marissa Wall61c58622018-07-18 10:12:20 -07001082 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1083 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1084 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1085 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001086 {
1087 SCOPED_TRACE("bottom transparent");
1088 auto shot = screenshot();
1089 shot->expectColor(top, Color::RED);
1090 shot->expectColor(bottom, Color::BLACK);
1091 }
1092}
1093
Marissa Wall61c58622018-07-18 10:12:20 -07001094TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1095 const Rect top(0, 0, 32, 16);
1096 const Rect bottom(0, 16, 32, 32);
1097 sp<SurfaceControl> layer;
1098 ASSERT_NO_FATAL_FAILURE(
1099 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1100
1101 sp<GraphicBuffer> buffer =
1102 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1103 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1104 BufferUsage::COMPOSER_OVERLAY,
1105 "test");
1106
1107 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1108 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1109 Transaction()
1110 .setTransparentRegionHint(layer, Region(top))
1111 .setBuffer(layer, buffer)
1112 .setSize(layer, 32, 32)
1113 .apply();
1114 {
1115 SCOPED_TRACE("top transparent");
1116 auto shot = screenshot();
1117 shot->expectColor(top, Color::BLACK);
1118 shot->expectColor(bottom, Color::RED);
1119 }
1120
1121 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1122 {
1123 SCOPED_TRACE("transparent region hint intermediate");
1124 auto shot = screenshot();
1125 shot->expectColor(top, Color::BLACK);
1126 shot->expectColor(bottom, Color::BLACK);
1127 }
1128
1129 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1130 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1131 BufferUsage::COMPOSER_OVERLAY,
1132 "test");
1133
1134 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1135 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
1136 Transaction().setBuffer(layer, buffer).setSize(layer, 32, 32).apply();
1137 {
1138 SCOPED_TRACE("bottom transparent");
1139 auto shot = screenshot();
1140 shot->expectColor(top, Color::RED);
1141 shot->expectColor(bottom, Color::BLACK);
1142 }
1143}
1144
1145TEST_P(LayerTypeTransactionTest, SetTransparentRegionHintOutOfBounds) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001146 sp<SurfaceControl> layerTransparent;
1147 sp<SurfaceControl> layerR;
1148 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1149 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1150
1151 // check that transparent region hint is bound by the layer size
1152 Transaction()
1153 .setTransparentRegionHint(layerTransparent,
1154 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
1155 .setPosition(layerR, 16, 16)
1156 .setLayer(layerR, mLayerZBase + 1)
1157 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001158 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1159 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001160 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1161}
1162
Marissa Wall61c58622018-07-18 10:12:20 -07001163TEST_P(LayerTypeTransactionTest, SetAlphaBasic) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001164 sp<SurfaceControl> layer1;
1165 sp<SurfaceControl> layer2;
1166 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
1167 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001168 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}, 32, 32));
1169 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001170
1171 Transaction()
1172 .setAlpha(layer1, 0.25f)
1173 .setAlpha(layer2, 0.75f)
1174 .setPosition(layer2, 16, 0)
1175 .setLayer(layer2, mLayerZBase + 1)
1176 .apply();
1177 {
1178 auto shot = screenshot();
1179 uint8_t r = 16; // 64 * 0.25f
1180 uint8_t g = 48; // 64 * 0.75f
1181 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1182 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1183
1184 r /= 4; // r * (1.0f - 0.75f)
1185 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1186 }
1187}
1188
Marissa Wall61c58622018-07-18 10:12:20 -07001189TEST_P(LayerTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001190 const Color color = {64, 0, 0, 255};
1191 sp<SurfaceControl> layer;
1192 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001193 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001194
1195 Transaction().setAlpha(layer, 2.0f).apply();
1196 {
1197 SCOPED_TRACE("clamped to 1.0f");
1198 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1199 }
1200
1201 Transaction().setAlpha(layer, -1.0f).apply();
1202 {
1203 SCOPED_TRACE("clamped to 0.0f");
1204 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1205 }
1206}
1207
Chia-I Wue4ef6102017-11-01 15:16:35 -07001208TEST_F(LayerTransactionTest, SetColorBasic) {
1209 sp<SurfaceControl> bufferLayer;
1210 sp<SurfaceControl> colorLayer;
1211 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001212 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001213 ASSERT_NO_FATAL_FAILURE(
1214 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1215
1216 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
1217 {
1218 SCOPED_TRACE("default color");
1219 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1220 }
1221
1222 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1223 const Color expected = {15, 51, 85, 255};
1224 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1225 // channel) should be less than one
1226 const uint8_t tolerance = 1;
1227 Transaction().setColor(colorLayer, color).apply();
1228 {
1229 SCOPED_TRACE("new color");
1230 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1231 }
1232}
1233
1234TEST_F(LayerTransactionTest, SetColorClamped) {
1235 sp<SurfaceControl> colorLayer;
1236 ASSERT_NO_FATAL_FAILURE(
1237 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1238
1239 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
1240 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1241}
1242
1243TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1244 sp<SurfaceControl> bufferLayer;
1245 sp<SurfaceControl> colorLayer;
1246 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001247 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001248 ASSERT_NO_FATAL_FAILURE(
1249 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1250
1251 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1252 const float alpha = 0.25f;
1253 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1254 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1255 // channel) should be less than one
1256 const uint8_t tolerance = 1;
1257 Transaction()
1258 .setColor(colorLayer, color)
1259 .setAlpha(colorLayer, alpha)
1260 .setLayer(colorLayer, mLayerZBase + 1)
1261 .apply();
1262 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1263 tolerance);
1264}
1265
Adrian Roosb7a96502018-04-08 11:38:55 -07001266TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1267 sp<SurfaceControl> bufferLayer;
1268 sp<SurfaceControl> parentLayer;
1269 sp<SurfaceControl> colorLayer;
1270 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1271 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001272 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Adrian Roosb7a96502018-04-08 11:38:55 -07001273 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer(
1274 "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1275
1276 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1277 const float alpha = 0.25f;
1278 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1279 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1280 // channel) should be less than one
1281 const uint8_t tolerance = 1;
1282 Transaction()
1283 .reparent(colorLayer, parentLayer->getHandle())
1284 .setColor(colorLayer, color)
1285 .setAlpha(parentLayer, alpha)
1286 .setLayer(parentLayer, mLayerZBase + 1)
1287 .apply();
1288 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1289 tolerance);
1290}
1291
Marissa Wall61c58622018-07-18 10:12:20 -07001292TEST_P(LayerTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001293 sp<SurfaceControl> bufferLayer;
1294 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001295 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001296
1297 // color is ignored
1298 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1299 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1300}
1301
Marissa Wall61c58622018-07-18 10:12:20 -07001302TEST_P(LayerTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001303 sp<SurfaceControl> layer;
1304 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001305 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001306
1307 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1308 {
1309 SCOPED_TRACE("non-existing layer stack");
1310 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1311 }
1312
1313 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1314 {
1315 SCOPED_TRACE("original layer stack");
1316 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1317 }
1318}
1319
Marissa Wall61c58622018-07-18 10:12:20 -07001320TEST_P(LayerTypeTransactionTest, SetMatrixBasic) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001321 sp<SurfaceControl> layer;
1322 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1323 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001324 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001325
1326 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1327 {
1328 SCOPED_TRACE("IDENTITY");
1329 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1330 Color::WHITE);
1331 }
1332
1333 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1334 {
1335 SCOPED_TRACE("FLIP_H");
1336 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1337 Color::BLUE);
1338 }
1339
1340 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1341 {
1342 SCOPED_TRACE("FLIP_V");
1343 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1344 Color::GREEN);
1345 }
1346
1347 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1348 {
1349 SCOPED_TRACE("ROT_90");
1350 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1351 Color::GREEN);
1352 }
1353
1354 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1355 {
1356 SCOPED_TRACE("SCALE");
1357 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1358 Color::WHITE, true /* filtered */);
1359 }
1360}
1361
Marissa Wall61c58622018-07-18 10:12:20 -07001362TEST_P(LayerTypeTransactionTest, SetMatrixRot45) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001363 sp<SurfaceControl> layer;
1364 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1365 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001366 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001367
1368 const float rot = M_SQRT1_2; // 45 degrees
1369 const float trans = M_SQRT2 * 16.0f;
1370 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1371
1372 auto shot = screenshot();
1373 // check a 8x8 region inside each color
1374 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1375 const int32_t halfL = 4;
1376 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1377 };
1378 const int32_t unit = int32_t(trans / 2);
1379 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1380 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1381 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1382 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1383}
1384
Marissa Wall61c58622018-07-18 10:12:20 -07001385void LayerTransactionTest::setMatrixWithResizeHelper(uint32_t layerType) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001386 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07001387 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32, layerType));
1388 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001389
1390 // setMatrix is applied after any pending resize, unlike setPosition
1391 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1392 {
1393 SCOPED_TRACE("resize pending");
1394 auto shot = screenshot();
Marissa Wall61c58622018-07-18 10:12:20 -07001395 Rect rect;
1396 switch (layerType) {
1397 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1398 rect = {0, 0, 32, 32};
1399 break;
1400 case ISurfaceComposerClient::eFXSurfaceBufferState:
1401 rect = {0, 0, 128, 128};
1402 break;
1403 default:
1404 ASSERT_FALSE(true) << "Unsupported layer type";
1405 }
1406 shot->expectColor(rect, Color::RED);
1407 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001408 }
1409
Marissa Wall61c58622018-07-18 10:12:20 -07001410 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001411 {
1412 SCOPED_TRACE("resize applied");
1413 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1414 }
1415}
1416
Marissa Wall61c58622018-07-18 10:12:20 -07001417TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferQueue) {
1418 ASSERT_NO_FATAL_FAILURE(
1419 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1420}
1421
1422TEST_F(LayerTransactionTest, SetMatrixWithResize_BufferState) {
1423 ASSERT_NO_FATAL_FAILURE(
1424 setMatrixWithResizeHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1425}
1426
1427TEST_P(LayerTypeTransactionTest, SetMatrixWithScaleToWindow) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001428 sp<SurfaceControl> layer;
1429 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001430 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001431
1432 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1433 Transaction()
1434 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1435 .setSize(layer, 64, 64)
1436 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1437 .apply();
1438 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1439}
1440
Marissa Wall61c58622018-07-18 10:12:20 -07001441TEST_P(LayerTypeTransactionTest, SetOverrideScalingModeBasic) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001442 sp<SurfaceControl> layer;
1443 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1444 ASSERT_NO_FATAL_FAILURE(
Marissa Wall61c58622018-07-18 10:12:20 -07001445 fillLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001446
1447 // XXX SCALE_CROP is not respected; calling setSize and
1448 // setOverrideScalingMode in separate transactions does not work
1449 // (b/69315456)
1450 Transaction()
1451 .setSize(layer, 64, 16)
1452 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1453 .apply();
1454 {
1455 SCOPED_TRACE("SCALE_TO_WINDOW");
1456 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1457 Color::WHITE, true /* filtered */);
1458 }
1459}
1460
Dan Stoza000dd012018-08-01 13:31:52 -07001461TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1462 sp<SurfaceControl> layer;
1463 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1464
1465 sp<IBinder> handle = layer->getHandle();
1466 ASSERT_TRUE(handle != nullptr);
1467
1468 FrameStats frameStats;
1469 mClient->getLayerFrameStats(handle, &frameStats);
1470
1471 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1472}
1473
Marissa Wall61c58622018-07-18 10:12:20 -07001474TEST_F(LayerTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001475 sp<SurfaceControl> layer;
1476 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001477 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001478 const Rect crop(8, 8, 24, 24);
1479
Marissa Wallf58c14b2018-07-24 10:50:43 -07001480 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001481 auto shot = screenshot();
1482 shot->expectColor(crop, Color::RED);
1483 shot->expectBorder(crop, Color::BLACK);
1484}
1485
Marissa Wall61c58622018-07-18 10:12:20 -07001486TEST_F(LayerTransactionTest, SetCropBasic_BufferState) {
1487 sp<SurfaceControl> layer;
1488 ASSERT_NO_FATAL_FAILURE(
1489 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1490 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1491 const Rect crop(8, 8, 24, 24);
1492
1493 Transaction().setCrop(layer, crop).apply();
1494 auto shot = screenshot();
1495 shot->expectColor(crop, Color::RED);
1496 shot->expectBorder(crop, Color::BLACK);
1497}
1498
1499TEST_F(LayerTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001500 sp<SurfaceControl> layer;
1501 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001502 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001503
1504 {
1505 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001506 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001507 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1508 }
1509
1510 {
1511 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001512 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001513 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1514 }
1515}
1516
Marissa Wall61c58622018-07-18 10:12:20 -07001517TEST_F(LayerTransactionTest, SetCropEmpty_BufferState) {
1518 sp<SurfaceControl> layer;
1519 ASSERT_NO_FATAL_FAILURE(
1520 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1521 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1522
1523 {
1524 SCOPED_TRACE("empty rect");
1525 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1526 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1527 }
1528
1529 {
1530 SCOPED_TRACE("negative rect");
1531 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1532 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1533 }
1534}
1535
1536TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001537 sp<SurfaceControl> layer;
1538 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001539 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001540
Marissa Wallf58c14b2018-07-24 10:50:43 -07001541 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001542 auto shot = screenshot();
1543 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1544 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1545}
1546
Marissa Wall61c58622018-07-18 10:12:20 -07001547TEST_F(LayerTransactionTest, SetCropOutOfBounds_BufferState) {
1548 sp<SurfaceControl> layer;
1549 ASSERT_NO_FATAL_FAILURE(
1550 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1551 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1552
1553 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1554 auto shot = screenshot();
1555 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1556 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1557}
1558
1559TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001560 sp<SurfaceControl> layer;
1561 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001562 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001563
1564 const Point position(32, 32);
1565 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001566 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001567 auto shot = screenshot();
1568 shot->expectColor(crop + position, Color::RED);
1569 shot->expectBorder(crop + position, Color::BLACK);
1570}
1571
Marissa Wall61c58622018-07-18 10:12:20 -07001572TEST_F(LayerTransactionTest, SetCropWithTranslation_BufferState) {
1573 sp<SurfaceControl> layer;
1574 ASSERT_NO_FATAL_FAILURE(
1575 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1576 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1577
1578 const Point position(32, 32);
1579 const Rect crop(8, 8, 24, 24);
1580 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1581 auto shot = screenshot();
1582 shot->expectColor(crop + position, Color::RED);
1583 shot->expectBorder(crop + position, Color::BLACK);
1584}
1585
1586TEST_F(LayerTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001587 sp<SurfaceControl> layer;
1588 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001589 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001590
1591 // crop is affected by matrix
1592 Transaction()
1593 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001594 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001595 .apply();
1596 auto shot = screenshot();
1597 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1598 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1599}
1600
Marissa Wall61c58622018-07-18 10:12:20 -07001601TEST_F(LayerTransactionTest, SetCropWithScale_BufferState) {
1602 sp<SurfaceControl> layer;
1603 ASSERT_NO_FATAL_FAILURE(
1604 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1605 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1606
1607 // crop is affected by matrix
1608 Transaction()
1609 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1610 .setCrop(layer, Rect(8, 8, 24, 24))
1611 .apply();
1612 auto shot = screenshot();
1613 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1614 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1615}
1616
1617TEST_F(LayerTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001618 sp<SurfaceControl> layer;
1619 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001620 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001621
Marissa Wallf58c14b2018-07-24 10:50:43 -07001622 // setCrop_legacy is applied immediately by default, with or without resize pending
1623 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001624 {
1625 SCOPED_TRACE("resize pending");
1626 auto shot = screenshot();
1627 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1628 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1629 }
1630
Marissa Wall61c58622018-07-18 10:12:20 -07001631 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001632 {
1633 SCOPED_TRACE("resize applied");
1634 auto shot = screenshot();
1635 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1636 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1637 }
1638}
1639
Marissa Wall61c58622018-07-18 10:12:20 -07001640TEST_F(LayerTransactionTest, SetCropWithResize_BufferState) {
1641 sp<SurfaceControl> layer;
1642 ASSERT_NO_FATAL_FAILURE(
1643 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1644 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1645
1646 // setCrop_legacy is applied immediately by default, with or without resize pending
1647 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1648 {
1649 SCOPED_TRACE("new buffer pending");
1650 auto shot = screenshot();
1651 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1652 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1653 }
1654
1655 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1656 {
1657 SCOPED_TRACE("new buffer");
1658 auto shot = screenshot();
1659 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1660 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1661 }
1662}
1663
1664TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001665 sp<SurfaceControl> layer;
1666 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001667 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001668
Marissa Wallf58c14b2018-07-24 10:50:43 -07001669 // request setCrop_legacy to be applied with the next resize
1670 Transaction()
1671 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1672 .setGeometryAppliesWithResize(layer)
1673 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001674 {
1675 SCOPED_TRACE("waiting for next resize");
1676 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1677 }
1678
Marissa Wallf58c14b2018-07-24 10:50:43 -07001679 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001680 {
1681 SCOPED_TRACE("pending crop modified");
1682 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1683 }
1684
1685 Transaction().setSize(layer, 16, 16).apply();
1686 {
1687 SCOPED_TRACE("resize pending");
1688 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1689 }
1690
1691 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07001692 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001693 {
1694 SCOPED_TRACE("new crop applied");
1695 auto shot = screenshot();
1696 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1697 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1698 }
1699}
1700
Marissa Wall61c58622018-07-18 10:12:20 -07001701TEST_F(LayerTransactionTest, SetCropWithNextResize_BufferState) {
1702 sp<SurfaceControl> layer;
1703 ASSERT_NO_FATAL_FAILURE(
1704 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1705 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1706
1707 // request setCrop_legacy to be applied with the next resize
1708 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1709 {
1710 SCOPED_TRACE("set crop 1");
1711 screenshot()->expectColor(Rect(8, 8, 24, 24), Color::RED);
1712 }
1713
1714 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1715 {
1716 SCOPED_TRACE("set crop 2");
1717 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1718 }
1719
1720 Transaction().setSize(layer, 16, 16).apply();
1721 {
1722 SCOPED_TRACE("resize");
1723 screenshot()->expectColor(Rect(4, 4, 12, 12), Color::RED);
1724 }
1725
1726 // finally resize
1727 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1728 {
1729 SCOPED_TRACE("new buffer");
1730 auto shot = screenshot();
1731 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1732 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1733 }
1734}
1735
1736TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001737 sp<SurfaceControl> layer;
1738 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001739 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001740
Marissa Wallf58c14b2018-07-24 10:50:43 -07001741 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001742 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001743 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001744 .setSize(layer, 16, 16)
1745 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1746 .setGeometryAppliesWithResize(layer)
1747 .apply();
1748 {
1749 SCOPED_TRACE("new crop pending");
1750 auto shot = screenshot();
1751 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1752 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1753 }
1754
1755 // XXX crop is never latched without other geometry change (b/69315677)
1756 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001757 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001758 Transaction().setPosition(layer, 0, 0).apply();
1759 {
1760 SCOPED_TRACE("new crop applied");
1761 auto shot = screenshot();
1762 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1763 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1764 }
1765}
1766
Marissa Wall61c58622018-07-18 10:12:20 -07001767TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow_BufferState) {
1768 sp<SurfaceControl> layer;
1769 ASSERT_NO_FATAL_FAILURE(
1770 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1771 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1772
1773 // all properties are applied immediate so setGeometryAppliesWithResize has no effect
1774 Transaction()
1775 .setCrop(layer, Rect(4, 4, 12, 12))
1776 .setSize(layer, 16, 16)
1777 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1778 .setGeometryAppliesWithResize(layer)
1779 .apply();
1780 {
1781 SCOPED_TRACE("new crop pending");
1782 auto shot = screenshot();
1783 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1784 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1785 }
1786
1787 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1788 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 16, 16));
1789 Transaction().setPosition(layer, 0, 0).apply();
1790 {
1791 SCOPED_TRACE("new crop applied");
1792 auto shot = screenshot();
1793 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1794 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1795 }
1796}
1797
Marissa Wall61c58622018-07-18 10:12:20 -07001798TEST_F(LayerTransactionTest, SetBufferBasic_BufferState) {
1799 sp<SurfaceControl> layer;
1800 ASSERT_NO_FATAL_FAILURE(
1801 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1802
1803 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1804
1805 auto shot = screenshot();
1806 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1807 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1808}
1809
1810TEST_F(LayerTransactionTest, SetBufferMultipleBuffers_BufferState) {
1811 sp<SurfaceControl> layer;
1812 ASSERT_NO_FATAL_FAILURE(
1813 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1814
1815 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1816
1817 {
1818 SCOPED_TRACE("set buffer 1");
1819 auto shot = screenshot();
1820 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1821 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1822 }
1823
1824 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
1825
1826 {
1827 SCOPED_TRACE("set buffer 2");
1828 auto shot = screenshot();
1829 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1830 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1831 }
1832
1833 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1834
1835 {
1836 SCOPED_TRACE("set buffer 3");
1837 auto shot = screenshot();
1838 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1839 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1840 }
1841}
1842
1843TEST_F(LayerTransactionTest, SetBufferMultipleLayers_BufferState) {
1844 sp<SurfaceControl> layer1;
1845 ASSERT_NO_FATAL_FAILURE(
1846 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
1847
1848 sp<SurfaceControl> layer2;
1849 ASSERT_NO_FATAL_FAILURE(
1850 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1851
1852 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
1853
1854 {
1855 SCOPED_TRACE("set layer 1 buffer red");
1856 auto shot = screenshot();
1857 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1858 }
1859
1860 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
1861
1862 {
1863 SCOPED_TRACE("set layer 2 buffer blue");
1864 auto shot = screenshot();
1865 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1866 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
1867 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
1868 }
1869
1870 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
1871 {
1872 SCOPED_TRACE("set layer 1 buffer green");
1873 auto shot = screenshot();
1874 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1875 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1876 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1877 }
1878
1879 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
1880
1881 {
1882 SCOPED_TRACE("set layer 2 buffer white");
1883 auto shot = screenshot();
1884 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
1885 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
1886 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
1887 }
1888}
1889
1890TEST_F(LayerTransactionTest, SetTransformRotate90_BufferState) {
1891 sp<SurfaceControl> layer;
1892 ASSERT_NO_FATAL_FAILURE(
1893 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1894
1895 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1896 Color::BLUE, Color::WHITE));
1897
1898 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90).apply();
1899
1900 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1901 Color::GREEN, true /* filtered */);
1902}
1903
1904TEST_F(LayerTransactionTest, SetTransformFlipH_BufferState) {
1905 sp<SurfaceControl> layer;
1906 ASSERT_NO_FATAL_FAILURE(
1907 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1908
1909 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1910 Color::BLUE, Color::WHITE));
1911
1912 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H).apply();
1913
1914 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1915 Color::BLUE, true /* filtered */);
1916}
1917
1918TEST_F(LayerTransactionTest, SetTransformFlipV_BufferState) {
1919 sp<SurfaceControl> layer;
1920 ASSERT_NO_FATAL_FAILURE(
1921 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1922
1923 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1924 Color::BLUE, Color::WHITE));
1925
1926 Transaction().setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V).apply();
1927
1928 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1929 Color::GREEN, true /* filtered */);
1930}
1931
1932TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
1933 sp<SurfaceControl> layer;
1934 ASSERT_NO_FATAL_FAILURE(
1935 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1936
1937 Transaction().setTransformToDisplayInverse(layer, false).apply();
1938
1939 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
1940
1941 Transaction().setTransformToDisplayInverse(layer, true).apply();
1942}
1943
1944TEST_F(LayerTransactionTest, SetFenceBasic_BufferState) {
1945 sp<SurfaceControl> layer;
1946 ASSERT_NO_FATAL_FAILURE(
1947 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1948
1949 sp<GraphicBuffer> buffer =
1950 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1951 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1952 BufferUsage::COMPOSER_OVERLAY,
1953 "test");
1954 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1955
1956 sp<Fence> fence = new Fence(-1);
1957
1958 Transaction()
1959 .setBuffer(layer, buffer)
1960 .setAcquireFence(layer, fence)
1961 .setSize(layer, 32, 32)
1962 .apply();
1963
1964 auto shot = screenshot();
1965 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1966 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1967}
1968
1969TEST_F(LayerTransactionTest, SetDataspaceBasic_BufferState) {
1970 sp<SurfaceControl> layer;
1971 ASSERT_NO_FATAL_FAILURE(
1972 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1973
1974 sp<GraphicBuffer> buffer =
1975 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1976 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1977 BufferUsage::COMPOSER_OVERLAY,
1978 "test");
1979 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
1980
1981 Transaction()
1982 .setBuffer(layer, buffer)
1983 .setDataspace(layer, ui::Dataspace::UNKNOWN)
1984 .setSize(layer, 32, 32)
1985 .apply();
1986
1987 auto shot = screenshot();
1988 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1989 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1990}
1991
1992TEST_F(LayerTransactionTest, SetHdrMetadataBasic_BufferState) {
1993 sp<SurfaceControl> layer;
1994 ASSERT_NO_FATAL_FAILURE(
1995 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1996
1997 sp<GraphicBuffer> buffer =
1998 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1999 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2000 BufferUsage::COMPOSER_OVERLAY,
2001 "test");
2002 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2003
2004 HdrMetadata hdrMetadata;
2005 hdrMetadata.validTypes = 0;
2006 Transaction()
2007 .setBuffer(layer, buffer)
2008 .setHdrMetadata(layer, hdrMetadata)
2009 .setSize(layer, 32, 32)
2010 .apply();
2011
2012 auto shot = screenshot();
2013 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2014 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2015}
2016
2017TEST_F(LayerTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2018 sp<SurfaceControl> layer;
2019 ASSERT_NO_FATAL_FAILURE(
2020 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2021
2022 sp<GraphicBuffer> buffer =
2023 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2024 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2025 BufferUsage::COMPOSER_OVERLAY,
2026 "test");
2027 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2028
2029 Region region;
2030 region.set(32, 32);
2031 Transaction()
2032 .setBuffer(layer, buffer)
2033 .setSurfaceDamageRegion(layer, region)
2034 .setSize(layer, 32, 32)
2035 .apply();
2036
2037 auto shot = screenshot();
2038 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2039 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2040}
2041
2042TEST_F(LayerTransactionTest, SetApiBasic_BufferState) {
2043 sp<SurfaceControl> layer;
2044 ASSERT_NO_FATAL_FAILURE(
2045 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2046
2047 sp<GraphicBuffer> buffer =
2048 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2049 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2050 BufferUsage::COMPOSER_OVERLAY,
2051 "test");
2052 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2053
2054 Transaction()
2055 .setBuffer(layer, buffer)
2056 .setApi(layer, NATIVE_WINDOW_API_CPU)
2057 .setSize(layer, 32, 32)
2058 .apply();
2059
2060 auto shot = screenshot();
2061 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2062 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2063}
2064
2065TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2066 sp<SurfaceControl> layer;
2067 ASSERT_NO_FATAL_FAILURE(
2068 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2069
2070 // verify this doesn't cause a crash
2071 Transaction().setSidebandStream(layer, nullptr).apply();
2072}
2073
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002074class ColorTransformHelper {
2075public:
2076 static void DegammaColorSingle(half& s) {
2077 if (s <= 0.03928f)
2078 s = s / 12.92f;
2079 else
2080 s = pow((s + 0.055f) / 1.055f, 2.4f);
2081 }
2082
2083 static void DegammaColor(half3& color) {
2084 DegammaColorSingle(color.r);
2085 DegammaColorSingle(color.g);
2086 DegammaColorSingle(color.b);
2087 }
2088
2089 static void GammaColorSingle(half& s) {
2090 if (s <= 0.0031308f) {
2091 s = s * 12.92f;
2092 } else {
2093 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2094 }
2095 }
2096
2097 static void GammaColor(half3& color) {
2098 GammaColorSingle(color.r);
2099 GammaColorSingle(color.g);
2100 GammaColorSingle(color.b);
2101 }
2102
2103 static void applyMatrix(half3& color, const mat3& mat) {
2104 half3 ret = half3(0);
2105
2106 for (int i = 0; i < 3; i++) {
2107 for (int j = 0; j < 3; j++) {
2108 ret[i] = ret[i] + color[j] * mat[j][i];
2109 }
2110 }
2111 color = ret;
2112 }
2113};
2114
Peiyong Lind3788632018-09-18 16:01:31 -07002115TEST_F(LayerTransactionTest, SetColorTransformBasic) {
2116 sp<SurfaceControl> colorLayer;
2117 ASSERT_NO_FATAL_FAILURE(
2118 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
2119
2120 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
2121 {
2122 SCOPED_TRACE("default color");
2123 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2124 }
2125
2126 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002127 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002128 mat3 matrix;
2129 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2130 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2131 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002132
2133 // degamma before applying the matrix
2134 if (mColorManagementUsed) {
2135 ColorTransformHelper::DegammaColor(expected);
2136 }
2137
2138 ColorTransformHelper::applyMatrix(expected, matrix);
2139
2140 if (mColorManagementUsed) {
2141 ColorTransformHelper::GammaColor(expected);
2142 }
2143
2144 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2145 uint8_t(expected.b * 255), 255};
2146
2147 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2148 // channel) should be less than one
2149 const uint8_t tolerance = 1;
2150
Peiyong Lind3788632018-09-18 16:01:31 -07002151 Transaction().setColor(colorLayer, color)
2152 .setColorTransform(colorLayer, matrix, vec3()).apply();
2153 {
2154 SCOPED_TRACE("new color");
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002155 screenshot()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002156 }
2157}
2158
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002159class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002160protected:
2161 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07002162 LayerTransactionTest::SetUp();
2163 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002164
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002165 sp<IBinder> display(
2166 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07002167 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002168 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07002169
2170 ssize_t displayWidth = info.w;
2171 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002172
2173 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07002174 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
2175 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002176 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002177 ASSERT_TRUE(mBGSurfaceControl->isValid());
2178 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
2179
2180 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07002181 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
2182
Peiyong Lin566a3b42018-01-09 18:22:43 -08002183 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002184 ASSERT_TRUE(mFGSurfaceControl->isValid());
2185
2186 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2187
2188 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07002189 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002190 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002191 ASSERT_TRUE(mSyncSurfaceControl->isValid());
2192
2193 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2194
Robert Carr4cdc58f2017-08-23 14:22:20 -07002195 asTransaction([&](Transaction& t) {
2196 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002197
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002198 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07002199
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002200 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
2201 .setPosition(mFGSurfaceControl, 64, 64)
2202 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002203
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002204 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
2205 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
2206 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002207 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002208 }
2209
2210 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07002211 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002212 mBGSurfaceControl = 0;
2213 mFGSurfaceControl = 0;
2214 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002215 }
2216
2217 void waitForPostedBuffers() {
2218 // Since the sync surface is in synchronous mode (i.e. double buffered)
2219 // posting three buffers to it should ensure that at least two
2220 // SurfaceFlinger::handlePageFlip calls have been made, which should
2221 // guaranteed that a buffer posted to another Surface has been retired.
2222 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2223 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2224 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2225 }
2226
Robert Carr4cdc58f2017-08-23 14:22:20 -07002227 void asTransaction(const std::function<void(Transaction&)>& exec) {
2228 Transaction t;
2229 exec(t);
2230 t.apply(true);
2231 }
2232
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07002233 sp<SurfaceControl> mBGSurfaceControl;
2234 sp<SurfaceControl> mFGSurfaceControl;
2235
2236 // This surface is used to ensure that the buffers posted to
2237 // mFGSurfaceControl have been picked up by SurfaceFlinger.
2238 sp<SurfaceControl> mSyncSurfaceControl;
2239};
2240
Robert Carr7f619b22017-11-06 12:56:35 -08002241TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08002242
chaviw0e3479f2018-09-10 16:49:30 -07002243 std::unique_ptr<ScreenCapture> sc;
2244
2245 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08002246 fillSurfaceRGBA8(relative, 10, 10, 10);
2247 waitForPostedBuffers();
2248
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002249 Transaction{}
2250 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08002251 .setPosition(relative, 64, 64)
2252 .apply();
2253
2254 {
2255 // The relative should be on top of the FG control.
2256 ScreenCapture::captureScreen(&sc);
2257 sc->checkPixel(64, 64, 10, 10, 10);
2258 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002259 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002260
2261 {
2262 // Nothing should change at this point.
2263 ScreenCapture::captureScreen(&sc);
2264 sc->checkPixel(64, 64, 10, 10, 10);
2265 }
2266
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002267 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08002268
2269 {
2270 // Ensure that the relative was actually hidden, rather than
2271 // being left in the detached but visible state.
2272 ScreenCapture::captureScreen(&sc);
2273 sc->expectFGColor(64, 64);
2274 }
2275}
2276
Robert Carr8d5227b2017-03-16 15:41:03 -07002277class GeometryLatchingTest : public LayerUpdateTest {
2278protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002279 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07002280 SCOPED_TRACE(trace);
2281 ScreenCapture::captureScreen(&sc);
2282 // We find the leading edge of the FG surface.
2283 sc->expectFGColor(127, 127);
2284 sc->expectBGColor(128, 128);
2285 }
Robert Carr7bf247e2017-05-18 14:02:49 -07002286
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002287 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07002288
2289 void unlockFGBuffer() {
2290 sp<Surface> s = mFGSurfaceControl->getSurface();
2291 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
2292 waitForPostedBuffers();
2293 }
2294
Robert Carr8d5227b2017-03-16 15:41:03 -07002295 void completeFGResize() {
2296 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2297 waitForPostedBuffers();
2298 }
2299 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002300 asTransaction([&](Transaction& t) {
2301 t.setSize(mFGSurfaceControl, 64, 64);
2302 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002303 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002304 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002305
2306 EXPECT_INITIAL_STATE("After restoring initial state");
2307 }
chaviw0e3479f2018-09-10 16:49:30 -07002308 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07002309};
2310
Robert Carr8d5227b2017-03-16 15:41:03 -07002311class CropLatchingTest : public GeometryLatchingTest {
2312protected:
2313 void EXPECT_CROPPED_STATE(const char* trace) {
2314 SCOPED_TRACE(trace);
2315 ScreenCapture::captureScreen(&sc);
2316 // The edge should be moved back one pixel by our crop.
2317 sc->expectFGColor(126, 126);
2318 sc->expectBGColor(127, 127);
2319 sc->expectBGColor(128, 128);
2320 }
chaviw59f5c562017-06-28 16:39:06 -07002321
2322 void EXPECT_RESIZE_STATE(const char* trace) {
2323 SCOPED_TRACE(trace);
2324 ScreenCapture::captureScreen(&sc);
2325 // The FG is now resized too 128,128 at 64,64
2326 sc->expectFGColor(64, 64);
2327 sc->expectFGColor(191, 191);
2328 sc->expectBGColor(192, 192);
2329 }
Robert Carr8d5227b2017-03-16 15:41:03 -07002330};
2331
Pablo Ceballos05289c22016-04-14 15:49:55 -07002332TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07002333 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07002334 {
2335 SCOPED_TRACE("before anything");
2336 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002337 sc->expectBGColor(32, 32);
2338 sc->expectFGColor(96, 96);
2339 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002340 }
2341
2342 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07002343 asTransaction([&](Transaction& t) {
2344 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002345 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2346 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002347 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002348
Robert Carr4cdc58f2017-08-23 14:22:20 -07002349 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002350 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002351 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
2352 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002353 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002354
2355 {
2356 SCOPED_TRACE("before any trigger");
2357 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002358 sc->expectBGColor(32, 32);
2359 sc->expectFGColor(96, 96);
2360 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002361 }
2362
2363 // should trigger the first deferred transaction, but not the second one
2364 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2365 {
2366 SCOPED_TRACE("after first trigger");
2367 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002368 sc->expectBGColor(32, 32);
2369 sc->checkPixel(96, 96, 162, 63, 96);
2370 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002371 }
2372
2373 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002374 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002375
2376 // trigger the second deferred transaction
2377 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2378 {
2379 SCOPED_TRACE("after second trigger");
2380 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002381 sc->expectBGColor(32, 32);
2382 sc->expectBGColor(96, 96);
2383 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002384 }
2385}
2386
Robert Carre392b552017-09-19 12:16:05 -07002387TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07002388 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07002389
2390 sp<SurfaceControl> childNoBuffer =
chaviw0e3479f2018-09-10 16:49:30 -07002391 mClient->createSurface(String8("Bufferless child"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002392 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2393 sp<SurfaceControl> childBuffer =
chaviw0e3479f2018-09-10 16:49:30 -07002394 mClient->createSurface(String8("Buffered child"), 20, 20,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002395 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07002396 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
2397
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002398 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002399
2400 {
2401 ScreenCapture::captureScreen(&sc);
2402 sc->expectChildColor(73, 73);
2403 sc->expectFGColor(74, 74);
2404 }
2405
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002406 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002407
2408 {
2409 ScreenCapture::captureScreen(&sc);
2410 sc->expectChildColor(73, 73);
2411 sc->expectChildColor(74, 74);
2412 }
2413}
2414
Robert Carr2c5f6d22017-09-26 12:30:35 -07002415TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07002416 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07002417 {
2418 SCOPED_TRACE("before move");
2419 ScreenCapture::captureScreen(&sc);
2420 sc->expectBGColor(0, 12);
2421 sc->expectFGColor(75, 75);
2422 sc->expectBGColor(145, 145);
2423 }
2424
2425 Transaction t1, t2;
2426 t1.setPosition(mFGSurfaceControl, 128, 128);
2427 t2.setPosition(mFGSurfaceControl, 0, 0);
2428 // We expect that the position update from t2 now
2429 // overwrites the position update from t1.
2430 t1.merge(std::move(t2));
2431 t1.apply();
2432
2433 {
2434 ScreenCapture::captureScreen(&sc);
2435 sc->expectFGColor(1, 1);
2436 }
2437}
2438
Robert Carr1f0a16a2016-10-24 16:27:39 -07002439class ChildLayerTest : public LayerUpdateTest {
2440protected:
2441 void SetUp() override {
2442 LayerUpdateTest::SetUp();
chaviw0e3479f2018-09-10 16:49:30 -07002443 mChild = mClient->createSurface(String8("Child surface"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002444 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002445 fillSurfaceRGBA8(mChild, 200, 200, 200);
2446
2447 {
2448 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07002449 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002450 mCapture->expectChildColor(64, 64);
2451 }
2452 }
2453 void TearDown() override {
2454 LayerUpdateTest::TearDown();
2455 mChild = 0;
2456 }
2457
2458 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07002459 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002460};
2461
2462TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002463 asTransaction([&](Transaction& t) {
2464 t.show(mChild);
2465 t.setPosition(mChild, 10, 10);
2466 t.setPosition(mFGSurfaceControl, 64, 64);
2467 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002468
2469 {
chaviw0e3479f2018-09-10 16:49:30 -07002470 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002471 // Top left of foreground must now be visible
2472 mCapture->expectFGColor(64, 64);
2473 // But 10 pixels in we should see the child surface
2474 mCapture->expectChildColor(74, 74);
2475 // And 10 more pixels we should be back to the foreground surface
2476 mCapture->expectFGColor(84, 84);
2477 }
2478
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002479 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002480
2481 {
chaviw0e3479f2018-09-10 16:49:30 -07002482 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002483 // Top left of foreground should now be at 0, 0
2484 mCapture->expectFGColor(0, 0);
2485 // But 10 pixels in we should see the child surface
2486 mCapture->expectChildColor(10, 10);
2487 // And 10 more pixels we should be back to the foreground surface
2488 mCapture->expectFGColor(20, 20);
2489 }
2490}
2491
Robert Carr41b08b52017-06-01 16:11:34 -07002492TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002493 asTransaction([&](Transaction& t) {
2494 t.show(mChild);
2495 t.setPosition(mChild, 0, 0);
2496 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002497 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07002498 });
Robert Carr41b08b52017-06-01 16:11:34 -07002499
2500 {
chaviw0e3479f2018-09-10 16:49:30 -07002501 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07002502 mCapture->expectChildColor(0, 0);
2503 mCapture->expectChildColor(4, 4);
2504 mCapture->expectBGColor(5, 5);
2505 }
2506}
2507
Robert Carr1f0a16a2016-10-24 16:27:39 -07002508TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002509 asTransaction([&](Transaction& t) {
2510 t.show(mChild);
2511 t.setPosition(mFGSurfaceControl, 0, 0);
2512 t.setPosition(mChild, 63, 63);
2513 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002514
2515 {
chaviw0e3479f2018-09-10 16:49:30 -07002516 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002517 mCapture->expectFGColor(0, 0);
2518 // Last pixel in foreground should now be the child.
2519 mCapture->expectChildColor(63, 63);
2520 // But the child should be constrained and the next pixel
2521 // must be the background
2522 mCapture->expectBGColor(64, 64);
2523 }
2524}
2525
2526TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002527 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002528
2529 // Find the boundary between the parent and child
2530 {
chaviw0e3479f2018-09-10 16:49:30 -07002531 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002532 mCapture->expectChildColor(9, 9);
2533 mCapture->expectFGColor(10, 10);
2534 }
2535
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002536 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002537
2538 // The boundary should be twice as far from the origin now.
2539 // The pixels from the last test should all be child now
2540 {
chaviw0e3479f2018-09-10 16:49:30 -07002541 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002542 mCapture->expectChildColor(9, 9);
2543 mCapture->expectChildColor(10, 10);
2544 mCapture->expectChildColor(19, 19);
2545 mCapture->expectFGColor(20, 20);
2546 }
2547}
Robert Carr9524cb32017-02-13 11:32:32 -08002548
Robert Carr6452f122017-03-21 10:41:29 -07002549TEST_F(ChildLayerTest, ChildLayerAlpha) {
2550 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2551 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2552 fillSurfaceRGBA8(mChild, 0, 254, 0);
2553 waitForPostedBuffers();
2554
Robert Carr4cdc58f2017-08-23 14:22:20 -07002555 asTransaction([&](Transaction& t) {
2556 t.show(mChild);
2557 t.setPosition(mChild, 0, 0);
2558 t.setPosition(mFGSurfaceControl, 0, 0);
2559 });
Robert Carr6452f122017-03-21 10:41:29 -07002560
2561 {
chaviw0e3479f2018-09-10 16:49:30 -07002562 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07002563 // Unblended child color
2564 mCapture->checkPixel(0, 0, 0, 254, 0);
2565 }
2566
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002567 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002568
2569 {
chaviw0e3479f2018-09-10 16:49:30 -07002570 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07002571 // Child and BG blended.
2572 mCapture->checkPixel(0, 0, 127, 127, 0);
2573 }
2574
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002575 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002576
2577 {
chaviw0e3479f2018-09-10 16:49:30 -07002578 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07002579 // Child and BG blended.
2580 mCapture->checkPixel(0, 0, 95, 64, 95);
2581 }
2582}
2583
Robert Carr9524cb32017-02-13 11:32:32 -08002584TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002585 asTransaction([&](Transaction& t) {
2586 t.show(mChild);
2587 t.setPosition(mChild, 10, 10);
2588 t.setPosition(mFGSurfaceControl, 64, 64);
2589 });
Robert Carr9524cb32017-02-13 11:32:32 -08002590
2591 {
chaviw0e3479f2018-09-10 16:49:30 -07002592 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002593 // Top left of foreground must now be visible
2594 mCapture->expectFGColor(64, 64);
2595 // But 10 pixels in we should see the child surface
2596 mCapture->expectChildColor(74, 74);
2597 // And 10 more pixels we should be back to the foreground surface
2598 mCapture->expectFGColor(84, 84);
2599 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002600
2601 asTransaction([&](Transaction& t) {
2602 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2603 });
2604
Robert Carr9524cb32017-02-13 11:32:32 -08002605 {
chaviw0e3479f2018-09-10 16:49:30 -07002606 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002607 mCapture->expectFGColor(64, 64);
2608 // In reparenting we should have exposed the entire foreground surface.
2609 mCapture->expectFGColor(74, 74);
2610 // And the child layer should now begin at 10, 10 (since the BG
2611 // layer is at (0, 0)).
2612 mCapture->expectBGColor(9, 9);
2613 mCapture->expectChildColor(10, 10);
2614 }
2615}
2616
Robert Carr2e102c92018-10-23 12:11:15 -07002617TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
2618 sp<SurfaceControl> mGrandChild =
2619 mClient->createSurface(String8("Grand Child"), 10, 10,
2620 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
2621 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
2622
2623 {
2624 SCOPED_TRACE("Grandchild visible");
2625 ScreenCapture::captureScreen(&mCapture);
2626 mCapture->checkPixel(64, 64, 111, 111, 111);
2627 }
2628
2629 mChild->clear();
2630
2631 {
2632 SCOPED_TRACE("After destroying child");
2633 ScreenCapture::captureScreen(&mCapture);
2634 mCapture->expectFGColor(64, 64);
2635 }
2636
2637 asTransaction([&](Transaction& t) {
2638 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
2639 });
2640
2641 {
2642 SCOPED_TRACE("After reparenting grandchild");
2643 ScreenCapture::captureScreen(&mCapture);
2644 mCapture->checkPixel(64, 64, 111, 111, 111);
2645 }
2646}
2647
chaviw161410b02017-07-27 10:46:08 -07002648TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002649 asTransaction([&](Transaction& t) {
2650 t.show(mChild);
2651 t.setPosition(mChild, 10, 10);
2652 t.setPosition(mFGSurfaceControl, 64, 64);
2653 });
Robert Carr9524cb32017-02-13 11:32:32 -08002654
2655 {
chaviw0e3479f2018-09-10 16:49:30 -07002656 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002657 // Top left of foreground must now be visible
2658 mCapture->expectFGColor(64, 64);
2659 // But 10 pixels in we should see the child surface
2660 mCapture->expectChildColor(74, 74);
2661 // And 10 more pixels we should be back to the foreground surface
2662 mCapture->expectFGColor(84, 84);
2663 }
2664
chaviw0e3479f2018-09-10 16:49:30 -07002665
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002666 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002667
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002668 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002669
chaviw161410b02017-07-27 10:46:08 -07002670 // Since the child has the same client as the parent, it will not get
2671 // detached and will be hidden.
2672 {
chaviw0e3479f2018-09-10 16:49:30 -07002673 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07002674 mCapture->expectFGColor(64, 64);
2675 mCapture->expectFGColor(74, 74);
2676 mCapture->expectFGColor(84, 84);
2677 }
2678}
2679
2680TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2681 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002682 sp<SurfaceControl> mChildNewClient =
2683 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2684 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002685
Peiyong Lin566a3b42018-01-09 18:22:43 -08002686 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07002687 ASSERT_TRUE(mChildNewClient->isValid());
2688
2689 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2690
Robert Carr4cdc58f2017-08-23 14:22:20 -07002691 asTransaction([&](Transaction& t) {
2692 t.hide(mChild);
2693 t.show(mChildNewClient);
2694 t.setPosition(mChildNewClient, 10, 10);
2695 t.setPosition(mFGSurfaceControl, 64, 64);
2696 });
chaviw161410b02017-07-27 10:46:08 -07002697
2698 {
chaviw0e3479f2018-09-10 16:49:30 -07002699 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07002700 // Top left of foreground must now be visible
2701 mCapture->expectFGColor(64, 64);
2702 // But 10 pixels in we should see the child surface
2703 mCapture->expectChildColor(74, 74);
2704 // And 10 more pixels we should be back to the foreground surface
2705 mCapture->expectFGColor(84, 84);
2706 }
2707
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002708 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002709
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002710 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002711
Robert Carr9524cb32017-02-13 11:32:32 -08002712 // Nothing should have changed.
2713 {
chaviw0e3479f2018-09-10 16:49:30 -07002714 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08002715 mCapture->expectFGColor(64, 64);
2716 mCapture->expectChildColor(74, 74);
2717 mCapture->expectFGColor(84, 84);
2718 }
2719}
2720
Robert Carr9b429f42017-04-17 14:56:57 -07002721TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002722 asTransaction([&](Transaction& t) {
2723 t.show(mChild);
2724 t.setPosition(mChild, 0, 0);
2725 t.setPosition(mFGSurfaceControl, 0, 0);
2726 });
Robert Carr9b429f42017-04-17 14:56:57 -07002727
2728 {
chaviw0e3479f2018-09-10 16:49:30 -07002729 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07002730 // We've positioned the child in the top left.
2731 mCapture->expectChildColor(0, 0);
2732 // But it's only 10x10.
2733 mCapture->expectFGColor(10, 10);
2734 }
2735
Robert Carr4cdc58f2017-08-23 14:22:20 -07002736 asTransaction([&](Transaction& t) {
2737 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2738 // We cause scaling by 2.
2739 t.setSize(mFGSurfaceControl, 128, 128);
2740 });
Robert Carr9b429f42017-04-17 14:56:57 -07002741
2742 {
chaviw0e3479f2018-09-10 16:49:30 -07002743 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07002744 // We've positioned the child in the top left.
2745 mCapture->expectChildColor(0, 0);
2746 mCapture->expectChildColor(10, 10);
2747 mCapture->expectChildColor(19, 19);
2748 // And now it should be scaled all the way to 20x20
2749 mCapture->expectFGColor(20, 20);
2750 }
2751}
2752
Robert Carr1725eee2017-04-26 18:32:15 -07002753// Regression test for b/37673612
2754TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002755 asTransaction([&](Transaction& t) {
2756 t.show(mChild);
2757 t.setPosition(mChild, 0, 0);
2758 t.setPosition(mFGSurfaceControl, 0, 0);
2759 });
Robert Carr1725eee2017-04-26 18:32:15 -07002760
2761 {
chaviw0e3479f2018-09-10 16:49:30 -07002762 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07002763 // We've positioned the child in the top left.
2764 mCapture->expectChildColor(0, 0);
2765 // But it's only 10x10.
2766 mCapture->expectFGColor(10, 10);
2767 }
Robert Carr1725eee2017-04-26 18:32:15 -07002768 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2769 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002770 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002771 sp<Surface> s = mFGSurfaceControl->getSurface();
2772 auto anw = static_cast<ANativeWindow*>(s.get());
2773 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2774 native_window_set_buffers_dimensions(anw, 64, 128);
2775 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2776 waitForPostedBuffers();
2777
2778 {
2779 // The child should still be in the same place and not have any strange scaling as in
2780 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07002781 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07002782 mCapture->expectChildColor(0, 0);
2783 mCapture->expectFGColor(10, 10);
2784 }
2785}
2786
Dan Stoza412903f2017-04-27 13:42:17 -07002787TEST_F(ChildLayerTest, Bug36858924) {
2788 // Destroy the child layer
2789 mChild.clear();
2790
2791 // Now recreate it as hidden
chaviw0e3479f2018-09-10 16:49:30 -07002792 mChild = mClient->createSurface(String8("Child surface"), 10, 10,
Dan Stoza412903f2017-04-27 13:42:17 -07002793 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2794 mFGSurfaceControl.get());
2795
2796 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002797 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002798 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
2799 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002800 t.show(mChild);
2801 });
Dan Stoza412903f2017-04-27 13:42:17 -07002802
2803 // Render the foreground surface a few times
2804 //
2805 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2806 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2807 // never acquire/release the first buffer
2808 ALOGI("Filling 1");
2809 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2810 ALOGI("Filling 2");
2811 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2812 ALOGI("Filling 3");
2813 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2814 ALOGI("Filling 4");
2815 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2816}
2817
chaviwf1961f72017-09-18 16:41:07 -07002818TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002819 asTransaction([&](Transaction& t) {
2820 t.show(mChild);
2821 t.setPosition(mChild, 10, 10);
2822 t.setPosition(mFGSurfaceControl, 64, 64);
2823 });
chaviw06178942017-07-27 10:25:59 -07002824
2825 {
chaviw0e3479f2018-09-10 16:49:30 -07002826 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07002827 // Top left of foreground must now be visible
2828 mCapture->expectFGColor(64, 64);
2829 // But 10 pixels in we should see the child surface
2830 mCapture->expectChildColor(74, 74);
2831 // And 10 more pixels we should be back to the foreground surface
2832 mCapture->expectFGColor(84, 84);
2833 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002834
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002835 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002836
chaviw06178942017-07-27 10:25:59 -07002837 {
chaviw0e3479f2018-09-10 16:49:30 -07002838 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07002839 mCapture->expectFGColor(64, 64);
2840 // In reparenting we should have exposed the entire foreground surface.
2841 mCapture->expectFGColor(74, 74);
2842 // And the child layer should now begin at 10, 10 (since the BG
2843 // layer is at (0, 0)).
2844 mCapture->expectBGColor(9, 9);
2845 mCapture->expectChildColor(10, 10);
2846 }
2847}
2848
chaviwf1961f72017-09-18 16:41:07 -07002849TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002850 asTransaction([&](Transaction& t) {
2851 t.show(mChild);
2852 t.setPosition(mChild, 10, 10);
2853 t.setPosition(mFGSurfaceControl, 64, 64);
2854 });
chaviwf1961f72017-09-18 16:41:07 -07002855
2856 {
chaviw0e3479f2018-09-10 16:49:30 -07002857 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002858 // Top left of foreground must now be visible
2859 mCapture->expectFGColor(64, 64);
2860 // But 10 pixels in we should see the child surface
2861 mCapture->expectChildColor(74, 74);
2862 // And 10 more pixels we should be back to the foreground surface
2863 mCapture->expectFGColor(84, 84);
2864 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002865 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002866 {
chaviw0e3479f2018-09-10 16:49:30 -07002867 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002868 // Nothing should have changed.
2869 mCapture->expectFGColor(64, 64);
2870 mCapture->expectChildColor(74, 74);
2871 mCapture->expectFGColor(84, 84);
2872 }
2873}
2874
2875TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07002876 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002877 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07002878 ASSERT_TRUE(newSurface->isValid());
2879
2880 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002881 asTransaction([&](Transaction& t) {
2882 t.hide(mChild);
2883 t.show(newSurface);
2884 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002885 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002886 t.setPosition(mFGSurfaceControl, 64, 64);
2887 });
chaviwf1961f72017-09-18 16:41:07 -07002888
2889 {
chaviw0e3479f2018-09-10 16:49:30 -07002890 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002891 // Top left of foreground must now be visible
2892 mCapture->expectFGColor(64, 64);
2893 // At 10, 10 we should see the new surface
2894 mCapture->checkPixel(10, 10, 63, 195, 63);
2895 }
2896
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002897 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002898
2899 {
chaviw0e3479f2018-09-10 16:49:30 -07002900 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07002901 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2902 // mFGSurface, putting it at 74, 74.
2903 mCapture->expectFGColor(64, 64);
2904 mCapture->checkPixel(74, 74, 63, 195, 63);
2905 mCapture->expectFGColor(84, 84);
2906 }
2907}
2908
chaviwc9674332017-08-28 12:32:18 -07002909TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002910 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07002911 mClient->createSurface(String8("Grandchild surface"), 10, 10,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002912 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002913 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2914
2915 {
chaviw0e3479f2018-09-10 16:49:30 -07002916 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07002917 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2918 // which begins at 64, 64
2919 mCapture->checkPixel(64, 64, 50, 50, 50);
2920 }
2921}
2922
Robert Carr503c7042017-09-27 15:06:08 -07002923TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07002924 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002925 fillSurfaceRGBA8(relative, 255, 255, 255);
2926
2927 Transaction t;
2928 t.setLayer(relative, INT32_MAX)
2929 .setRelativeLayer(mChild, relative->getHandle(), 1)
2930 .setPosition(mFGSurfaceControl, 0, 0)
2931 .apply(true);
2932
2933 // We expect that the child should have been elevated above our
2934 // INT_MAX layer even though it's not a child of it.
2935 {
chaviw0e3479f2018-09-10 16:49:30 -07002936 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07002937 mCapture->expectChildColor(0, 0);
2938 mCapture->expectChildColor(9, 9);
2939 mCapture->checkPixel(10, 10, 255, 255, 255);
2940 }
2941}
2942
chaviwa76b2712017-09-20 12:02:26 -07002943class ScreenCaptureTest : public LayerUpdateTest {
2944protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002945 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07002946};
2947
2948TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2949 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002950 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002951 mCapture->expectBGColor(0, 0);
2952 // Doesn't capture FG layer which is at 64, 64
2953 mCapture->expectBGColor(64, 64);
2954}
2955
2956TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2957 auto fgHandle = mFGSurfaceControl->getHandle();
2958
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002959 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07002960 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002961 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002962 fillSurfaceRGBA8(child, 200, 200, 200);
2963
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002964 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002965
2966 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002967 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002968 mCapture->expectFGColor(10, 10);
2969 mCapture->expectChildColor(0, 0);
2970}
2971
Robert Carr578038f2018-03-09 12:25:24 -08002972TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
2973 auto fgHandle = mFGSurfaceControl->getHandle();
2974
2975 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07002976 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Robert Carr578038f2018-03-09 12:25:24 -08002977 0, mFGSurfaceControl.get());
2978 fillSurfaceRGBA8(child, 200, 200, 200);
2979
2980 SurfaceComposerClient::Transaction().show(child).apply(true);
2981
2982 // Captures mFGSurfaceControl's child
2983 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2984 mCapture->checkPixel(10, 10, 0, 0, 0);
2985 mCapture->expectChildColor(0, 0);
2986}
2987
chaviw50da5042018-04-09 13:49:37 -07002988TEST_F(ScreenCaptureTest, CaptureTransparent) {
2989 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07002990 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw50da5042018-04-09 13:49:37 -07002991 0, mFGSurfaceControl.get());
2992
2993 fillSurfaceRGBA8(child, 200, 200, 200);
2994
2995 SurfaceComposerClient::Transaction().show(child).apply(true);
2996
2997 auto childHandle = child->getHandle();
2998
2999 // Captures child
3000 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
3001 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
3002 // Area outside of child's bounds is transparent.
3003 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
3004}
3005
chaviw4b129c22018-04-09 16:19:43 -07003006TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
3007 auto fgHandle = mFGSurfaceControl->getHandle();
3008
3009 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003010 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw4b129c22018-04-09 16:19:43 -07003011 0, mFGSurfaceControl.get());
chaviw0e3479f2018-09-10 16:49:30 -07003012 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07003013 fillSurfaceRGBA8(child, 200, 200, 200);
3014 fillSurfaceRGBA8(relative, 100, 100, 100);
3015
3016 SurfaceComposerClient::Transaction()
3017 .show(child)
3018 // Set relative layer above fg layer so should be shown above when computing all layers.
3019 .setRelativeLayer(relative, fgHandle, 1)
3020 .show(relative)
3021 .apply(true);
3022
3023 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
3024 ScreenCapture::captureLayers(&mCapture, fgHandle);
3025 mCapture->expectFGColor(10, 10);
3026 mCapture->expectChildColor(0, 0);
3027}
3028
3029TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
3030 auto fgHandle = mFGSurfaceControl->getHandle();
3031
3032 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003033 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
chaviw4b129c22018-04-09 16:19:43 -07003034 0, mFGSurfaceControl.get());
3035 sp<SurfaceControl> relative =
chaviw0e3479f2018-09-10 16:49:30 -07003036 mClient->createSurface(String8("Relative surface"), 10, 10,
chaviw4b129c22018-04-09 16:19:43 -07003037 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3038 fillSurfaceRGBA8(child, 200, 200, 200);
3039 fillSurfaceRGBA8(relative, 100, 100, 100);
3040
3041 SurfaceComposerClient::Transaction()
3042 .show(child)
3043 // Set relative layer below fg layer but relative to child layer so it should be shown
3044 // above child layer.
3045 .setLayer(relative, -1)
3046 .setRelativeLayer(relative, child->getHandle(), 1)
3047 .show(relative)
3048 .apply(true);
3049
3050 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
3051 // relative value should be taken into account, placing it above child layer.
3052 ScreenCapture::captureLayers(&mCapture, fgHandle);
3053 mCapture->expectFGColor(10, 10);
3054 // Relative layer is showing on top of child layer
3055 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
3056}
Robert Carr578038f2018-03-09 12:25:24 -08003057
3058// In the following tests we verify successful skipping of a parent layer,
3059// so we use the same verification logic and only change how we mutate
3060// the parent layer to verify that various properties are ignored.
3061class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
3062public:
3063 void SetUp() override {
3064 LayerUpdateTest::SetUp();
3065
3066 mChild =
chaviw0e3479f2018-09-10 16:49:30 -07003067 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Robert Carr578038f2018-03-09 12:25:24 -08003068 0, mFGSurfaceControl.get());
3069 fillSurfaceRGBA8(mChild, 200, 200, 200);
3070
3071 SurfaceComposerClient::Transaction().show(mChild).apply(true);
3072 }
3073
3074 void verify() {
3075 auto fgHandle = mFGSurfaceControl->getHandle();
3076 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
3077 mCapture->checkPixel(10, 10, 0, 0, 0);
3078 mCapture->expectChildColor(0, 0);
3079 }
3080
3081 std::unique_ptr<ScreenCapture> mCapture;
3082 sp<SurfaceControl> mChild;
3083};
3084
3085TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
3086
3087 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
3088
3089 // Even though the parent is hidden we should still capture the child.
3090 verify();
3091}
3092
3093TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07003094 SurfaceComposerClient::Transaction()
3095 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
3096 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08003097
3098 // Even though the parent is cropped out we should still capture the child.
3099 verify();
3100}
3101
3102TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
3103
3104 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
3105
3106 // We should not inherit the parent scaling.
3107 verify();
3108}
3109
Robert Carr15eae092018-03-23 13:43:53 -07003110TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
3111 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
3112
3113 // Even though the parent is hidden we should still capture the child.
3114 verify();
3115
3116 // Verify everything was properly hidden when rendering the full-screen.
3117 screenshot()->expectBGColor(0,0);
3118}
3119
3120
chaviwa76b2712017-09-20 12:02:26 -07003121TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
3122 auto fgHandle = mFGSurfaceControl->getHandle();
3123
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003124 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003125 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003126 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003127 fillSurfaceRGBA8(child, 200, 200, 200);
3128
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003129 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07003130 mClient->createSurface(String8("Grandchild surface"), 5, 5,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003131 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07003132
3133 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3134 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003135 .show(child)
3136 .setPosition(grandchild, 5, 5)
3137 .show(grandchild)
3138 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003139
3140 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003141 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07003142 mCapture->expectFGColor(10, 10);
3143 mCapture->expectChildColor(0, 0);
3144 mCapture->checkPixel(5, 5, 50, 50, 50);
3145}
3146
3147TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003148 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003149 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003150 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003151 fillSurfaceRGBA8(child, 200, 200, 200);
3152 auto childHandle = child->getHandle();
3153
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003154 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003155
3156 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003157 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07003158 mCapture->expectChildColor(0, 0);
3159 mCapture->expectChildColor(9, 9);
3160}
3161
3162TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003163 sp<SurfaceControl> child =
chaviw0e3479f2018-09-10 16:49:30 -07003164 mClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003165 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07003166 fillSurfaceRGBA8(child, 200, 200, 200);
3167 auto childHandle = child->getHandle();
3168
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003169 sp<SurfaceControl> grandchild =
chaviw0e3479f2018-09-10 16:49:30 -07003170 mClient->createSurface(String8("Grandchild surface"), 5, 5,
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003171 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07003172 fillSurfaceRGBA8(grandchild, 50, 50, 50);
3173
3174 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003175 .show(child)
3176 .setPosition(grandchild, 5, 5)
3177 .show(grandchild)
3178 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07003179
3180 auto grandchildHandle = grandchild->getHandle();
3181
3182 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003183 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07003184 mCapture->checkPixel(0, 0, 50, 50, 50);
3185 mCapture->checkPixel(4, 4, 50, 50, 50);
3186}
3187
chaviw7206d492017-11-10 16:16:12 -08003188TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07003189 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003190 sp<SurfaceControl> blueLayer =
chaviw0e3479f2018-09-10 16:49:30 -07003191 mClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003192 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08003193
Marissa Wall61c58622018-07-18 10:12:20 -07003194 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
3195 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08003196
3197 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003198 .setLayer(redLayer, INT32_MAX - 1)
3199 .show(redLayer)
3200 .show(blueLayer)
3201 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08003202
3203 auto redLayerHandle = redLayer->getHandle();
3204
3205 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003206 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
3207 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
3208 // red area below the blue area
3209 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
3210 // red area to the right of the blue area
3211 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003212
3213 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003214 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08003215 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
3216 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003217 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08003218 mCapture->checkPixel(30, 30, 0, 0, 0);
3219}
3220
3221TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07003222 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003223 sp<SurfaceControl> blueLayer =
chaviw0e3479f2018-09-10 16:49:30 -07003224 mClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003225 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08003226
Marissa Wall61c58622018-07-18 10:12:20 -07003227 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
3228 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08003229
3230 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003231 .setLayer(redLayer, INT32_MAX - 1)
3232 .show(redLayer)
3233 .show(blueLayer)
3234 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08003235
3236 auto redLayerHandle = redLayer->getHandle();
3237
3238 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003239 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
3240 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
3241 // red area below the blue area
3242 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
3243 // red area to the right of the blue area
3244 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003245
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003246 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08003247 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003248 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
3249 // red area below the blue area
3250 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
3251 // red area to the right of the blue area
3252 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08003253 mCapture->checkPixel(30, 30, 0, 0, 0);
3254}
3255
3256TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07003257 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08003258
Marissa Wall61c58622018-07-18 10:12:20 -07003259 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08003260
3261 auto redLayerHandle = redLayer->getHandle();
chaviw0e3479f2018-09-10 16:49:30 -07003262 mClient->destroySurface(redLayerHandle);
chaviw7206d492017-11-10 16:16:12 -08003263 SurfaceComposerClient::Transaction().apply(true);
3264
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003265 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08003266
3267 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003268 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
3269 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08003270}
3271
chaviw8e3fe5d2018-02-22 10:55:42 -08003272
3273class DereferenceSurfaceControlTest : public LayerTransactionTest {
3274protected:
3275 void SetUp() override {
3276 LayerTransactionTest::SetUp();
3277 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07003278 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08003279 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07003280 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08003281 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
3282 {
3283 SCOPED_TRACE("before anything");
3284 auto shot = screenshot();
3285 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
3286 }
3287 }
3288 void TearDown() override {
3289 LayerTransactionTest::TearDown();
3290 bgLayer = 0;
3291 fgLayer = 0;
3292 }
3293
3294 sp<SurfaceControl> bgLayer;
3295 sp<SurfaceControl> fgLayer;
3296};
3297
3298TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
3299 fgLayer = nullptr;
3300 {
3301 SCOPED_TRACE("after setting null");
3302 auto shot = screenshot();
3303 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
3304 }
3305}
3306
3307TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
3308 auto transaction = Transaction().show(fgLayer);
3309 fgLayer = nullptr;
3310 {
3311 SCOPED_TRACE("after setting null");
3312 auto shot = screenshot();
3313 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
3314 }
3315}
3316
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003317} // namespace android