blob: deca17799c4ebd0389a45f7f2a300b0fbc48e420 [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>
Robert Carr9085ced2019-02-19 10:05:00 -080032#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080033
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>
Robert Carr9085ced2019-02-19 10:05:00 -080040#include <sys/types.h>
41#include <unistd.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070042
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070043namespace android {
44
Chia-I Wu718daf82017-10-20 11:57:17 -070045namespace {
46
47struct Color {
48 uint8_t r;
49 uint8_t g;
50 uint8_t b;
51 uint8_t a;
52
53 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070054 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070055 static const Color BLUE;
Chia-I Wu93853fe2017-11-02 08:30:27 -070056 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070057 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070058 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070059};
60
61const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070062const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070063const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070064const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070065const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070066const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070067
68std::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.
Robert Carr9085ced2019-02-19 10:05:00 -080074void 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;
79 }
80
81 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++) {
88 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
97// Fill a region with the specified color.
Chia-I Wu718daf82017-10-20 11:57:17 -070098void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) {
99 int32_t x = rect.left;
100 int32_t y = rect.top;
101 int32_t width = rect.right - rect.left;
102 int32_t height = rect.bottom - rect.top;
103
104 if (x < 0) {
105 width += x;
106 x = 0;
107 }
108 if (y < 0) {
109 height += y;
110 y = 0;
111 }
112 if (x + width > buffer.width) {
113 x = std::min(x, buffer.width);
114 width = buffer.width - x;
115 }
116 if (y + height > buffer.height) {
117 y = std::min(y, buffer.height);
118 height = buffer.height - y;
119 }
120
121 for (int32_t j = 0; j < height; j++) {
122 uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4;
123 for (int32_t i = 0; i < width; i++) {
124 dst[0] = color.r;
125 dst[1] = color.g;
126 dst[2] = color.b;
127 dst[3] = color.a;
128 dst += 4;
129 }
130 }
131}
132
133// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000134void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700135 const Color& color, uint8_t tolerance) {
136 int32_t x = rect.left;
137 int32_t y = rect.top;
138 int32_t width = rect.right - rect.left;
139 int32_t height = rect.bottom - rect.top;
140
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000141 int32_t bufferWidth = int32_t(outBuffer->getWidth());
142 int32_t bufferHeight = int32_t(outBuffer->getHeight());
143 if (x + width > bufferWidth) {
144 x = std::min(x, bufferWidth);
145 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700146 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000147 if (y + height > bufferHeight) {
148 y = std::min(y, bufferHeight);
149 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700150 }
151
152 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
153 uint8_t tmp = a >= b ? a - b : b - a;
154 return tmp <= tolerance;
155 };
156 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000157 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700158 for (int32_t i = 0; i < width; i++) {
159 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
160 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
161 << "pixel @ (" << x + i << ", " << y + j << "): "
162 << "expected (" << color << "), "
163 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
164 src += 4;
165 }
166 }
167}
168
169} // anonymous namespace
170
Robert Carr4cdc58f2017-08-23 14:22:20 -0700171using Transaction = SurfaceComposerClient::Transaction;
172
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700173// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700174static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
175 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800176 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700177 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800178 ASSERT_TRUE(s != nullptr);
179 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800180 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700181 for (int y = 0; y < outBuffer.height; y++) {
182 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700183 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700184 pixel[0] = r;
185 pixel[1] = g;
186 pixel[2] = b;
187 pixel[3] = 255;
188 }
189 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700190 if (unlock) {
191 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
192 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700193}
194
195// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
196// individual pixel values for testing purposes.
197class ScreenCapture : public RefBase {
198public:
Chia-I Wu718daf82017-10-20 11:57:17 -0700199 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
200 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700201 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700202 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700203 SurfaceComposerClient::Transaction().apply(true);
204
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000205 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700206 ASSERT_EQ(NO_ERROR,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000207 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, minLayerZ, maxLayerZ,
208 false));
209 *sc = new ScreenCapture(outBuffer);
210 }
211
212 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
213 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
214 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
215 SurfaceComposerClient::Transaction().apply(true);
216
217 sp<GraphicBuffer> outBuffer;
218 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
219 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700220 }
221
Robert Carr578038f2018-03-09 12:25:24 -0800222 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
223 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
224 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
225 SurfaceComposerClient::Transaction().apply(true);
226
227 sp<GraphicBuffer> outBuffer;
228 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
229 *sc = std::make_unique<ScreenCapture>(outBuffer);
230 }
231
Chia-I Wu718daf82017-10-20 11:57:17 -0700232 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000233 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
234 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700235 }
236
237 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000238 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700239 const bool leftBorder = rect.left > 0;
240 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000241 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
242 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700243
244 if (topBorder) {
245 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
246 if (leftBorder) {
247 top.left -= 1;
248 }
249 if (rightBorder) {
250 top.right += 1;
251 }
252 expectColor(top, color, tolerance);
253 }
254 if (leftBorder) {
255 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
256 expectColor(left, color, tolerance);
257 }
258 if (rightBorder) {
259 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
260 expectColor(right, color, tolerance);
261 }
262 if (bottomBorder) {
263 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
264 if (leftBorder) {
265 bottom.left -= 1;
266 }
267 if (rightBorder) {
268 bottom.right += 1;
269 }
270 expectColor(bottom, color, tolerance);
271 }
272 }
273
Chia-I Wu93853fe2017-11-02 08:30:27 -0700274 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
275 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
276 uint8_t tolerance = 0) {
277 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
278
279 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
280 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
281 // avoid checking borders due to unspecified filtering behavior
282 const int32_t offsetX = filtered ? 2 : 0;
283 const int32_t offsetY = filtered ? 2 : 0;
284 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
285 tolerance);
286 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
287 tolerance);
288 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
289 tolerance);
290 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
291 bottomRight, tolerance);
292 }
293
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700294 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000295 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
296 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700297 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
298 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700299 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
300 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700301 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700302 }
303 }
304
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700305 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700306
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700307 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700308
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700309 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700310
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000311 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
312 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700313 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700314
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000315 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700316
317private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000318 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800319 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700320};
321
Chia-I Wu718daf82017-10-20 11:57:17 -0700322class LayerTransactionTest : public ::testing::Test {
323protected:
324 void SetUp() override {
325 mClient = new SurfaceComposerClient;
326 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
327
328 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
329 }
330
331 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
332 uint32_t flags = 0) {
333 auto layer =
334 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
335 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
336
337 status_t error = Transaction()
338 .setLayerStack(layer, mDisplayLayerStack)
339 .setLayer(layer, mLayerZBase)
340 .apply();
341 if (error != NO_ERROR) {
342 ADD_FAILURE() << "failed to initialize SurfaceControl";
343 layer.clear();
344 }
345
346 return layer;
347 }
348
Robert Carr9085ced2019-02-19 10:05:00 -0800349 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
350 // wait for previous transactions (such as setSize) to complete
351 Transaction().apply(true);
352
353 ANativeWindow_Buffer buffer = {};
354 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
355
356 return buffer;
357 }
358
Chia-I Wu718daf82017-10-20 11:57:17 -0700359 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
360 // wait for previous transactions (such as setSize) to complete
361 Transaction().apply(true);
362
363 ANativeWindow_Buffer buffer = {};
364 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
365
366 return buffer;
367 }
368
369 void postLayerBuffer(const sp<SurfaceControl>& layer) {
370 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
371
372 // wait for the newly posted buffer to be latched
373 waitForLayerBuffers();
374 }
375
Robert Carr9085ced2019-02-19 10:05:00 -0800376 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
377 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
378
379 // wait for the newly posted buffer to be latched
380 waitForLayerBuffers();
381 }
382
383 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
384 int32_t bufferWidth, int32_t bufferHeight) {
385 ANativeWindow_Buffer buffer;
386 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
387 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
388 postBufferQueueLayerBuffer(layer);
389 }
390
Chia-I Wu718daf82017-10-20 11:57:17 -0700391 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
392 ANativeWindow_Buffer buffer;
393 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
394 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
395 postLayerBuffer(layer);
396 }
397
Chia-I Wu93853fe2017-11-02 08:30:27 -0700398 void fillLayerQuadrant(const sp<SurfaceControl>& layer, const Color& topLeft,
399 const Color& topRight, const Color& bottomLeft,
400 const Color& bottomRight) {
401 ANativeWindow_Buffer buffer;
402 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
403 ASSERT_TRUE(buffer.width % 2 == 0 && buffer.height % 2 == 0);
404
405 const int32_t halfW = buffer.width / 2;
406 const int32_t halfH = buffer.height / 2;
407 fillBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
408 fillBufferColor(buffer, Rect(halfW, 0, buffer.width, halfH), topRight);
409 fillBufferColor(buffer, Rect(0, halfH, halfW, buffer.height), bottomLeft);
410 fillBufferColor(buffer, Rect(halfW, halfH, buffer.width, buffer.height), bottomRight);
411
412 postLayerBuffer(layer);
413 }
414
Chia-I Wu718daf82017-10-20 11:57:17 -0700415 sp<ScreenCapture> screenshot() {
416 sp<ScreenCapture> screenshot;
417 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
418 return screenshot;
419 }
420
421 sp<SurfaceComposerClient> mClient;
422
423 sp<IBinder> mDisplay;
424 uint32_t mDisplayWidth;
425 uint32_t mDisplayHeight;
426 uint32_t mDisplayLayerStack;
427
428 // leave room for ~256 layers
429 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
430
431private:
432 void SetUpDisplay() {
433 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
434 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
435
436 // get display width/height
437 DisplayInfo info;
438 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
439 mDisplayWidth = info.w;
440 mDisplayHeight = info.h;
441
442 // After a new buffer is queued, SurfaceFlinger is notified and will
443 // latch the new buffer on next vsync. Let's heuristically wait for 3
444 // vsyncs.
445 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
446
447 mDisplayLayerStack = 0;
448 // set layer stack (b/68888219)
449 Transaction t;
450 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
451 t.apply();
452 }
453
454 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
455
456 int32_t mBufferPostDelay;
457};
458
459TEST_F(LayerTransactionTest, SetPositionBasic) {
460 sp<SurfaceControl> layer;
461 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
462 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
463
464 {
465 SCOPED_TRACE("default position");
466 auto shot = screenshot();
467 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
468 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
469 }
470
471 Transaction().setPosition(layer, 5, 10).apply();
472 {
473 SCOPED_TRACE("new position");
474 auto shot = screenshot();
475 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
476 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
477 }
478}
479
480TEST_F(LayerTransactionTest, SetPositionRounding) {
481 sp<SurfaceControl> layer;
482 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
483 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
484
485 // GLES requires only 4 bits of subpixel precision during rasterization
486 // XXX GLES composition does not match HWC composition due to precision
487 // loss (b/69315223)
488 const float epsilon = 1.0f / 16.0f;
489 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
490 {
491 SCOPED_TRACE("rounding down");
492 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
493 }
494
495 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
496 {
497 SCOPED_TRACE("rounding up");
498 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
499 }
500}
501
502TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
503 sp<SurfaceControl> layer;
504 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
505 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
506
507 Transaction().setPosition(layer, -32, -32).apply();
508 {
509 SCOPED_TRACE("negative coordinates");
510 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
511 }
512
513 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
514 {
515 SCOPED_TRACE("positive coordinates");
516 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
517 }
518}
519
520TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
521 sp<SurfaceControl> layer;
522 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
523 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
524
525 // partially out of bounds
526 Transaction().setPosition(layer, -30, -30).apply();
527 {
528 SCOPED_TRACE("negative coordinates");
529 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
530 }
531
532 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
533 {
534 SCOPED_TRACE("positive coordinates");
535 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
536 mDisplayHeight),
537 Color::RED);
538 }
539}
540
541TEST_F(LayerTransactionTest, SetPositionWithResize) {
542 sp<SurfaceControl> layer;
543 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
544 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
545
546 // setPosition is applied immediately by default, with or without resize
547 // pending
548 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
549 {
550 SCOPED_TRACE("resize pending");
551 auto shot = screenshot();
552 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
553 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
554 }
555
556 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
557 {
558 SCOPED_TRACE("resize applied");
559 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
560 }
561}
562
563TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
564 sp<SurfaceControl> layer;
565 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
566 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
567
568 // request setPosition to be applied with the next resize
569 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
570 {
571 SCOPED_TRACE("new position pending");
572 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
573 }
574
575 Transaction().setPosition(layer, 15, 20).apply();
576 {
577 SCOPED_TRACE("pending new position modified");
578 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
579 }
580
581 Transaction().setSize(layer, 64, 64).apply();
582 {
583 SCOPED_TRACE("resize pending");
584 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
585 }
586
587 // finally resize and latch the buffer
588 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
589 {
590 SCOPED_TRACE("new position applied");
591 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
592 }
593}
594
595TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
596 sp<SurfaceControl> layer;
597 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
598 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
599
600 // setPosition is not immediate even with SCALE_TO_WINDOW override
601 Transaction()
602 .setPosition(layer, 5, 10)
603 .setSize(layer, 64, 64)
604 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
605 .setGeometryAppliesWithResize(layer)
606 .apply();
607 {
608 SCOPED_TRACE("new position pending");
609 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
610 }
611
612 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
613 {
614 SCOPED_TRACE("new position applied");
615 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
616 }
617}
618
Chia-I Wu0eaea312017-10-31 10:14:40 -0700619TEST_F(LayerTransactionTest, SetSizeBasic) {
620 sp<SurfaceControl> layer;
621 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
622 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
623
624 Transaction().setSize(layer, 64, 64).apply();
625 {
626 SCOPED_TRACE("resize pending");
627 auto shot = screenshot();
628 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
629 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
630 }
631
632 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
633 {
634 SCOPED_TRACE("resize applied");
635 auto shot = screenshot();
636 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
637 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
638 }
639}
640
641TEST_F(LayerTransactionTest, SetSizeInvalid) {
642 // cannot test robustness against invalid sizes (zero or really huge)
643}
644
645TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
646 sp<SurfaceControl> layer;
647 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
648 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
649
650 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
651 Transaction()
652 .setSize(layer, 64, 64)
653 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
654 .apply();
655 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
656}
657
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700658TEST_F(LayerTransactionTest, SetZBasic) {
659 sp<SurfaceControl> layerR;
660 sp<SurfaceControl> layerG;
661 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
662 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
663 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
664 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
665
666 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
667 {
668 SCOPED_TRACE("layerR");
669 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
670 }
671
672 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
673 {
674 SCOPED_TRACE("layerG");
675 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
676 }
677}
678
679TEST_F(LayerTransactionTest, SetZNegative) {
680 sp<SurfaceControl> layerR;
681 sp<SurfaceControl> layerG;
682 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
683 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
684 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
685 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
686
687 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
688 {
689 SCOPED_TRACE("layerR");
690 sp<ScreenCapture> screenshot;
691 ScreenCapture::captureScreen(&screenshot, -2, -1);
692 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
693 }
694
695 Transaction().setLayer(layerR, -3).apply();
696 {
697 SCOPED_TRACE("layerG");
698 sp<ScreenCapture> screenshot;
699 ScreenCapture::captureScreen(&screenshot, -3, -1);
700 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
701 }
702}
703
Chia-I Wu49313302017-10-31 10:14:40 -0700704TEST_F(LayerTransactionTest, SetRelativeZBasic) {
705 sp<SurfaceControl> layerR;
706 sp<SurfaceControl> layerG;
707 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
708 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
709 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
710 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
711
712 Transaction()
713 .setPosition(layerG, 16, 16)
714 .setRelativeLayer(layerG, layerR->getHandle(), 1)
715 .apply();
716 {
717 SCOPED_TRACE("layerG above");
718 auto shot = screenshot();
719 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
720 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
721 }
722
723 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
724 {
725 SCOPED_TRACE("layerG below");
726 auto shot = screenshot();
727 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
728 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
729 }
730}
731
Chia-I Wuec2d9852017-11-21 09:21:01 -0800732TEST_F(LayerTransactionTest, SetRelativeZNegative) {
733 sp<SurfaceControl> layerR;
734 sp<SurfaceControl> layerG;
735 sp<SurfaceControl> layerB;
736 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
737 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
738 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
739 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
740 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
741 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
742
743 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
744 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
745
746 sp<ScreenCapture> screenshot;
747 // only layerB is in this range
748 ScreenCapture::captureScreen(&screenshot, -2, -1);
749 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
750}
751
Chia-I Wu49313302017-10-31 10:14:40 -0700752TEST_F(LayerTransactionTest, SetRelativeZGroup) {
753 sp<SurfaceControl> layerR;
754 sp<SurfaceControl> layerG;
755 sp<SurfaceControl> layerB;
756 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
757 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
758 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
759 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
760 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
761 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
762
763 // layerR = 0, layerG = layerR + 3, layerB = 2
764 Transaction()
765 .setPosition(layerG, 8, 8)
766 .setRelativeLayer(layerG, layerR->getHandle(), 3)
767 .setPosition(layerB, 16, 16)
768 .setLayer(layerB, mLayerZBase + 2)
769 .apply();
770 {
771 SCOPED_TRACE("(layerR < layerG) < layerB");
772 auto shot = screenshot();
773 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
774 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
775 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
776 }
777
778 // layerR = 4, layerG = layerR + 3, layerB = 2
779 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
780 {
781 SCOPED_TRACE("layerB < (layerR < layerG)");
782 auto shot = screenshot();
783 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
784 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
785 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
786 }
787
788 // layerR = 4, layerG = layerR - 3, layerB = 2
789 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
790 {
791 SCOPED_TRACE("layerB < (layerG < layerR)");
792 auto shot = screenshot();
793 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
794 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
795 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
796 }
797
798 // restore to absolute z
799 // layerR = 4, layerG = 0, layerB = 2
800 Transaction().setLayer(layerG, mLayerZBase).apply();
801 {
802 SCOPED_TRACE("layerG < layerB < layerR");
803 auto shot = screenshot();
804 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
805 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
806 }
807
808 // layerR should not affect layerG anymore
809 // layerR = 1, layerG = 0, layerB = 2
810 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
811 {
812 SCOPED_TRACE("layerG < layerR < layerB");
813 auto shot = screenshot();
814 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
815 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
816 }
817}
818
819TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
820 sp<SurfaceControl> layerR;
821 sp<SurfaceControl> layerG;
822
823 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
824 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
825 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
826 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
827
828 Transaction()
829 .setPosition(layerG, 16, 16)
830 .setRelativeLayer(layerG, layerR->getHandle(), 1)
831 .apply();
832
833 mClient->destroySurface(layerG->getHandle());
834 // layerG should have been removed
835 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
836}
837
Chia-I Wu57b27502017-10-31 10:14:40 -0700838TEST_F(LayerTransactionTest, SetFlagsHidden) {
839 sp<SurfaceControl> layer;
840 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
841 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
842
843 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
844 {
845 SCOPED_TRACE("layer hidden");
846 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
847 }
848
849 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
850 {
851 SCOPED_TRACE("layer shown");
852 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
853 }
854}
855
856TEST_F(LayerTransactionTest, SetFlagsOpaque) {
857 const Color translucentRed = {100, 0, 0, 100};
858 sp<SurfaceControl> layerR;
859 sp<SurfaceControl> layerG;
860 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
861 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
862 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
863 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
864
865 Transaction()
866 .setLayer(layerR, mLayerZBase + 1)
867 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
868 .apply();
869 {
870 SCOPED_TRACE("layerR opaque");
871 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
872 }
873
874 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
875 {
876 SCOPED_TRACE("layerR translucent");
877 const uint8_t g = uint8_t(255 - translucentRed.a);
878 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
879 }
880}
881
882TEST_F(LayerTransactionTest, SetFlagsSecure) {
883 sp<SurfaceControl> layer;
884 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
885 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
886
887 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000888 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -0700889 Transaction()
890 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
891 .apply(true);
892 ASSERT_EQ(PERMISSION_DENIED,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000893 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
Chia-I Wu57b27502017-10-31 10:14:40 -0700894 false));
895
896 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
897 ASSERT_EQ(NO_ERROR,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000898 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
Chia-I Wu57b27502017-10-31 10:14:40 -0700899 false));
900}
901
Robert Carr9085ced2019-02-19 10:05:00 -0800902/** RAII Wrapper around get/seteuid */
903class UIDFaker {
904 uid_t oldId;
905public:
906 UIDFaker(uid_t uid) {
907 oldId = geteuid();
908 seteuid(uid);
909 }
910 ~UIDFaker() {
911 seteuid(oldId);
912 }
913};
914
915TEST_F(LayerTransactionTest, SetFlagsSecureEUidSystem) {
916 sp<SurfaceControl> layer;
917 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
918 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
919
920 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
921 sp<GraphicBuffer> outBuffer;
922 Transaction()
923 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
924 .apply(true);
925
926 ASSERT_EQ(PERMISSION_DENIED,
927 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, 0, INT_MAX, false));
928
929 UIDFaker f(AID_SYSTEM);
930
931 // By default the system can capture screenshots with secure layers but they
932 // will be blacked out
933 ASSERT_EQ(NO_ERROR,
934 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, 0, INT_MAX, false));
935
936 {
937 SCOPED_TRACE("as system");
938 auto shot = screenshot();
939 shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
940 }
941
942 // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
943 // to receive them...we are expected to take care with the results.
944 ASSERT_EQ(NO_ERROR,
945 composer->captureScreen(mDisplay, &outBuffer,
946 Rect(), 0, 0, 0, INT_MAX, false, ISurfaceComposer::eRotateNone, true));
947 ScreenCapture sc(outBuffer);
948 sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
949}
950
Chia-I Wu2113bdd2017-11-01 15:16:35 -0700951TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
952 const Rect top(0, 0, 32, 16);
953 const Rect bottom(0, 16, 32, 32);
954 sp<SurfaceControl> layer;
955 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
956
957 ANativeWindow_Buffer buffer;
958 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
959 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
960 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
961 // setTransparentRegionHint always applies to the following buffer
962 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
963 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
964 {
965 SCOPED_TRACE("top transparent");
966 auto shot = screenshot();
967 shot->expectColor(top, Color::BLACK);
968 shot->expectColor(bottom, Color::RED);
969 }
970
971 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
972 {
973 SCOPED_TRACE("transparent region hint pending");
974 auto shot = screenshot();
975 shot->expectColor(top, Color::BLACK);
976 shot->expectColor(bottom, Color::RED);
977 }
978
979 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
980 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
981 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
982 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
983 {
984 SCOPED_TRACE("bottom transparent");
985 auto shot = screenshot();
986 shot->expectColor(top, Color::RED);
987 shot->expectColor(bottom, Color::BLACK);
988 }
989}
990
991TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
992 sp<SurfaceControl> layerTransparent;
993 sp<SurfaceControl> layerR;
994 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
995 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
996
997 // check that transparent region hint is bound by the layer size
998 Transaction()
999 .setTransparentRegionHint(layerTransparent,
1000 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
1001 .setPosition(layerR, 16, 16)
1002 .setLayer(layerR, mLayerZBase + 1)
1003 .apply();
1004 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
1005 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
1006 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1007}
1008
Chia-I Wua8a515e2017-11-01 15:16:35 -07001009TEST_F(LayerTransactionTest, SetAlphaBasic) {
1010 sp<SurfaceControl> layer1;
1011 sp<SurfaceControl> layer2;
1012 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
1013 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
1014 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
1015 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
1016
1017 Transaction()
1018 .setAlpha(layer1, 0.25f)
1019 .setAlpha(layer2, 0.75f)
1020 .setPosition(layer2, 16, 0)
1021 .setLayer(layer2, mLayerZBase + 1)
1022 .apply();
1023 {
1024 auto shot = screenshot();
1025 uint8_t r = 16; // 64 * 0.25f
1026 uint8_t g = 48; // 64 * 0.75f
1027 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1028 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1029
1030 r /= 4; // r * (1.0f - 0.75f)
1031 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1032 }
1033}
1034
1035TEST_F(LayerTransactionTest, SetAlphaClamped) {
1036 const Color color = {64, 0, 0, 255};
1037 sp<SurfaceControl> layer;
1038 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1039 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
1040
1041 Transaction().setAlpha(layer, 2.0f).apply();
1042 {
1043 SCOPED_TRACE("clamped to 1.0f");
1044 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1045 }
1046
1047 Transaction().setAlpha(layer, -1.0f).apply();
1048 {
1049 SCOPED_TRACE("clamped to 0.0f");
1050 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1051 }
1052}
1053
Chia-I Wue4ef6102017-11-01 15:16:35 -07001054TEST_F(LayerTransactionTest, SetColorBasic) {
1055 sp<SurfaceControl> bufferLayer;
1056 sp<SurfaceControl> colorLayer;
1057 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1058 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1059 ASSERT_NO_FATAL_FAILURE(
1060 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1061
1062 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
1063 {
1064 SCOPED_TRACE("default color");
1065 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1066 }
1067
1068 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1069 const Color expected = {15, 51, 85, 255};
1070 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1071 // channel) should be less than one
1072 const uint8_t tolerance = 1;
1073 Transaction().setColor(colorLayer, color).apply();
1074 {
1075 SCOPED_TRACE("new color");
1076 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1077 }
1078}
1079
1080TEST_F(LayerTransactionTest, SetColorClamped) {
1081 sp<SurfaceControl> colorLayer;
1082 ASSERT_NO_FATAL_FAILURE(
1083 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1084
1085 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
1086 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1087}
1088
1089TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1090 sp<SurfaceControl> bufferLayer;
1091 sp<SurfaceControl> colorLayer;
1092 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1093 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1094 ASSERT_NO_FATAL_FAILURE(
1095 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1096
1097 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1098 const float alpha = 0.25f;
1099 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1100 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1101 // channel) should be less than one
1102 const uint8_t tolerance = 1;
1103 Transaction()
1104 .setColor(colorLayer, color)
1105 .setAlpha(colorLayer, alpha)
1106 .setLayer(colorLayer, mLayerZBase + 1)
1107 .apply();
1108 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1109 tolerance);
1110}
1111
Adrian Roosb7a96502018-04-08 11:38:55 -07001112TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1113 sp<SurfaceControl> bufferLayer;
1114 sp<SurfaceControl> parentLayer;
1115 sp<SurfaceControl> colorLayer;
1116 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1117 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
1118 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1119 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer(
1120 "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1121
1122 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1123 const float alpha = 0.25f;
1124 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1125 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1126 // channel) should be less than one
1127 const uint8_t tolerance = 1;
1128 Transaction()
1129 .reparent(colorLayer, parentLayer->getHandle())
1130 .setColor(colorLayer, color)
1131 .setAlpha(parentLayer, alpha)
1132 .setLayer(parentLayer, mLayerZBase + 1)
1133 .apply();
1134 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1135 tolerance);
1136}
1137
Chia-I Wue4ef6102017-11-01 15:16:35 -07001138TEST_F(LayerTransactionTest, SetColorWithBuffer) {
1139 sp<SurfaceControl> bufferLayer;
1140 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1141 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1142
1143 // color is ignored
1144 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1145 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1146}
1147
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001148TEST_F(LayerTransactionTest, SetLayerStackBasic) {
1149 sp<SurfaceControl> layer;
1150 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1151 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1152
1153 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1154 {
1155 SCOPED_TRACE("non-existing layer stack");
1156 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1157 }
1158
1159 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1160 {
1161 SCOPED_TRACE("original layer stack");
1162 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1163 }
1164}
1165
Chia-I Wu93853fe2017-11-02 08:30:27 -07001166TEST_F(LayerTransactionTest, SetMatrixBasic) {
1167 sp<SurfaceControl> layer;
1168 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1169 ASSERT_NO_FATAL_FAILURE(
1170 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1171
1172 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1173 {
1174 SCOPED_TRACE("IDENTITY");
1175 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1176 Color::WHITE);
1177 }
1178
1179 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1180 {
1181 SCOPED_TRACE("FLIP_H");
1182 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1183 Color::BLUE);
1184 }
1185
1186 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1187 {
1188 SCOPED_TRACE("FLIP_V");
1189 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1190 Color::GREEN);
1191 }
1192
1193 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1194 {
1195 SCOPED_TRACE("ROT_90");
1196 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1197 Color::GREEN);
1198 }
1199
1200 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1201 {
1202 SCOPED_TRACE("SCALE");
1203 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1204 Color::WHITE, true /* filtered */);
1205 }
1206}
1207
1208TEST_F(LayerTransactionTest, SetMatrixRot45) {
1209 sp<SurfaceControl> layer;
1210 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1211 ASSERT_NO_FATAL_FAILURE(
1212 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1213
1214 const float rot = M_SQRT1_2; // 45 degrees
1215 const float trans = M_SQRT2 * 16.0f;
1216 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1217
1218 auto shot = screenshot();
1219 // check a 8x8 region inside each color
1220 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1221 const int32_t halfL = 4;
1222 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1223 };
1224 const int32_t unit = int32_t(trans / 2);
1225 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1226 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1227 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1228 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1229}
1230
1231TEST_F(LayerTransactionTest, SetMatrixWithResize) {
1232 sp<SurfaceControl> layer;
1233 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1234 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1235
1236 // setMatrix is applied after any pending resize, unlike setPosition
1237 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1238 {
1239 SCOPED_TRACE("resize pending");
1240 auto shot = screenshot();
1241 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1242 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1243 }
1244
1245 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1246 {
1247 SCOPED_TRACE("resize applied");
1248 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1249 }
1250}
1251
1252TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) {
1253 sp<SurfaceControl> layer;
1254 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1255 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1256
1257 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1258 Transaction()
1259 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1260 .setSize(layer, 64, 64)
1261 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1262 .apply();
1263 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1264}
1265
Chia-I Wua56b2042017-11-01 15:16:35 -07001266TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) {
1267 sp<SurfaceControl> layer;
1268 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1269 ASSERT_NO_FATAL_FAILURE(
1270 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1271
1272 // XXX SCALE_CROP is not respected; calling setSize and
1273 // setOverrideScalingMode in separate transactions does not work
1274 // (b/69315456)
1275 Transaction()
1276 .setSize(layer, 64, 16)
1277 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1278 .apply();
1279 {
1280 SCOPED_TRACE("SCALE_TO_WINDOW");
1281 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1282 Color::WHITE, true /* filtered */);
1283 }
1284}
1285
Chia-I Wu04dcca82017-11-02 08:30:27 -07001286TEST_F(LayerTransactionTest, SetCropBasic) {
1287 sp<SurfaceControl> layer;
1288 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1289 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1290 const Rect crop(8, 8, 24, 24);
1291
1292 Transaction().setCrop(layer, crop).apply();
1293 auto shot = screenshot();
1294 shot->expectColor(crop, Color::RED);
1295 shot->expectBorder(crop, Color::BLACK);
1296}
1297
1298TEST_F(LayerTransactionTest, SetCropEmpty) {
1299 sp<SurfaceControl> layer;
1300 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1301 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1302
1303 {
1304 SCOPED_TRACE("empty rect");
1305 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1306 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1307 }
1308
1309 {
1310 SCOPED_TRACE("negative rect");
1311 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1312 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1313 }
1314}
1315
1316TEST_F(LayerTransactionTest, SetCropOutOfBounds) {
1317 sp<SurfaceControl> layer;
1318 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1319 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1320
1321 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1322 auto shot = screenshot();
1323 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1324 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1325}
1326
1327TEST_F(LayerTransactionTest, SetCropWithTranslation) {
1328 sp<SurfaceControl> layer;
1329 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1330 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1331
1332 const Point position(32, 32);
1333 const Rect crop(8, 8, 24, 24);
1334 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1335 auto shot = screenshot();
1336 shot->expectColor(crop + position, Color::RED);
1337 shot->expectBorder(crop + position, Color::BLACK);
1338}
1339
1340TEST_F(LayerTransactionTest, SetCropWithScale) {
1341 sp<SurfaceControl> layer;
1342 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1343 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1344
1345 // crop is affected by matrix
1346 Transaction()
1347 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1348 .setCrop(layer, Rect(8, 8, 24, 24))
1349 .apply();
1350 auto shot = screenshot();
1351 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1352 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1353}
1354
1355TEST_F(LayerTransactionTest, SetCropWithResize) {
1356 sp<SurfaceControl> layer;
1357 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1358 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1359
1360 // setCrop is applied immediately by default, with or without resize pending
1361 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1362 {
1363 SCOPED_TRACE("resize pending");
1364 auto shot = screenshot();
1365 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1366 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1367 }
1368
1369 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1370 {
1371 SCOPED_TRACE("resize applied");
1372 auto shot = screenshot();
1373 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1374 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1375 }
1376}
1377
1378TEST_F(LayerTransactionTest, SetCropWithNextResize) {
1379 sp<SurfaceControl> layer;
1380 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1381 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1382
1383 // request setCrop to be applied with the next resize
1384 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1385 {
1386 SCOPED_TRACE("waiting for next resize");
1387 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1388 }
1389
1390 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1391 {
1392 SCOPED_TRACE("pending crop modified");
1393 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1394 }
1395
1396 Transaction().setSize(layer, 16, 16).apply();
1397 {
1398 SCOPED_TRACE("resize pending");
1399 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1400 }
1401
1402 // finally resize
1403 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1404 {
1405 SCOPED_TRACE("new crop applied");
1406 auto shot = screenshot();
1407 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1408 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1409 }
1410}
1411
1412TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow) {
1413 sp<SurfaceControl> layer;
1414 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1415 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1416
1417 // setCrop is not immediate even with SCALE_TO_WINDOW override
1418 Transaction()
1419 .setCrop(layer, Rect(4, 4, 12, 12))
1420 .setSize(layer, 16, 16)
1421 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1422 .setGeometryAppliesWithResize(layer)
1423 .apply();
1424 {
1425 SCOPED_TRACE("new crop pending");
1426 auto shot = screenshot();
1427 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1428 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1429 }
1430
1431 // XXX crop is never latched without other geometry change (b/69315677)
1432 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1433 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1434 Transaction().setPosition(layer, 0, 0).apply();
1435 {
1436 SCOPED_TRACE("new crop applied");
1437 auto shot = screenshot();
1438 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1439 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1440 }
1441}
1442
Chia-I Wucdd71a52017-11-02 08:30:27 -07001443TEST_F(LayerTransactionTest, SetFinalCropBasic) {
1444 sp<SurfaceControl> layer;
1445 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1446 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1447 const Rect crop(8, 8, 24, 24);
1448
1449 // same as in SetCropBasic
1450 Transaction().setFinalCrop(layer, crop).apply();
1451 auto shot = screenshot();
1452 shot->expectColor(crop, Color::RED);
1453 shot->expectBorder(crop, Color::BLACK);
1454}
1455
1456TEST_F(LayerTransactionTest, SetFinalCropEmpty) {
1457 sp<SurfaceControl> layer;
1458 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1459 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1460
1461 // same as in SetCropEmpty
1462 {
1463 SCOPED_TRACE("empty rect");
1464 Transaction().setFinalCrop(layer, Rect(8, 8, 8, 8)).apply();
1465 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1466 }
1467
1468 {
1469 SCOPED_TRACE("negative rect");
1470 Transaction().setFinalCrop(layer, Rect(8, 8, 0, 0)).apply();
1471 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1472 }
1473}
1474
1475TEST_F(LayerTransactionTest, SetFinalCropOutOfBounds) {
1476 sp<SurfaceControl> layer;
1477 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1478 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1479
1480 // same as in SetCropOutOfBounds
1481 Transaction().setFinalCrop(layer, Rect(-128, -64, 128, 64)).apply();
1482 auto shot = screenshot();
1483 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1484 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1485}
1486
1487TEST_F(LayerTransactionTest, SetFinalCropWithTranslation) {
1488 sp<SurfaceControl> layer;
1489 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1490 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1491
1492 // final crop is applied post-translation
1493 Transaction().setPosition(layer, 16, 16).setFinalCrop(layer, Rect(8, 8, 24, 24)).apply();
1494 auto shot = screenshot();
1495 shot->expectColor(Rect(16, 16, 24, 24), Color::RED);
1496 shot->expectBorder(Rect(16, 16, 24, 24), Color::BLACK);
1497}
1498
1499TEST_F(LayerTransactionTest, SetFinalCropWithScale) {
1500 sp<SurfaceControl> layer;
1501 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1502 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1503
1504 // final crop is not affected by matrix
1505 Transaction()
1506 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1507 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1508 .apply();
1509 auto shot = screenshot();
1510 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1511 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1512}
1513
1514TEST_F(LayerTransactionTest, SetFinalCropWithResize) {
1515 sp<SurfaceControl> layer;
1516 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1517 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1518
1519 // same as in SetCropWithResize
1520 Transaction().setFinalCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1521 {
1522 SCOPED_TRACE("resize pending");
1523 auto shot = screenshot();
1524 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1525 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1526 }
1527
1528 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1529 {
1530 SCOPED_TRACE("resize applied");
1531 auto shot = screenshot();
1532 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1533 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1534 }
1535}
1536
1537TEST_F(LayerTransactionTest, SetFinalCropWithNextResize) {
1538 sp<SurfaceControl> layer;
1539 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1540 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1541
1542 // same as in SetCropWithNextResize
1543 Transaction()
1544 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1545 .setGeometryAppliesWithResize(layer)
1546 .apply();
1547 {
1548 SCOPED_TRACE("waiting for next resize");
1549 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1550 }
1551
1552 Transaction().setFinalCrop(layer, Rect(4, 4, 12, 12)).apply();
1553 {
1554 SCOPED_TRACE("pending final crop modified");
1555 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1556 }
1557
1558 Transaction().setSize(layer, 16, 16).apply();
1559 {
1560 SCOPED_TRACE("resize pending");
1561 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1562 }
1563
1564 // finally resize
1565 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1566 {
1567 SCOPED_TRACE("new final crop applied");
1568 auto shot = screenshot();
1569 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1570 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1571 }
1572}
1573
1574TEST_F(LayerTransactionTest, SetFinalCropWithNextResizeScaleToWindow) {
1575 sp<SurfaceControl> layer;
1576 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1577 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1578
1579 // same as in SetCropWithNextResizeScaleToWindow
1580 Transaction()
1581 .setFinalCrop(layer, Rect(4, 4, 12, 12))
1582 .setSize(layer, 16, 16)
1583 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1584 .setGeometryAppliesWithResize(layer)
1585 .apply();
1586 {
1587 SCOPED_TRACE("new final crop pending");
1588 auto shot = screenshot();
1589 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1590 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1591 }
1592
1593 // XXX final crop is never latched without other geometry change (b/69315677)
1594 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1595 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1596 Transaction().setPosition(layer, 0, 0).apply();
1597 {
1598 SCOPED_TRACE("new final crop applied");
1599 auto shot = screenshot();
1600 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1601 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1602 }
1603}
1604
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001605class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001606protected:
1607 virtual void SetUp() {
1608 mComposerClient = new SurfaceComposerClient;
1609 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1610
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001611 sp<IBinder> display(
1612 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001613 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001614 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001615
1616 ssize_t displayWidth = info.w;
1617 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001618
1619 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001620 mBGSurfaceControl =
1621 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1622 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001623 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001624 ASSERT_TRUE(mBGSurfaceControl->isValid());
1625 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1626
1627 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001628 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1629 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001630 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001631 ASSERT_TRUE(mFGSurfaceControl->isValid());
1632
1633 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1634
1635 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001636 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1637 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001638 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001639 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1640
1641 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1642
Robert Carr4cdc58f2017-08-23 14:22:20 -07001643 asTransaction([&](Transaction& t) {
1644 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001645
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001646 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001647
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001648 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1649 .setPosition(mFGSurfaceControl, 64, 64)
1650 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001651
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001652 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1653 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1654 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001655 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001656 }
1657
1658 virtual void TearDown() {
1659 mComposerClient->dispose();
1660 mBGSurfaceControl = 0;
1661 mFGSurfaceControl = 0;
1662 mSyncSurfaceControl = 0;
1663 mComposerClient = 0;
1664 }
1665
1666 void waitForPostedBuffers() {
1667 // Since the sync surface is in synchronous mode (i.e. double buffered)
1668 // posting three buffers to it should ensure that at least two
1669 // SurfaceFlinger::handlePageFlip calls have been made, which should
1670 // guaranteed that a buffer posted to another Surface has been retired.
1671 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1672 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1673 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1674 }
1675
Robert Carr4cdc58f2017-08-23 14:22:20 -07001676 void asTransaction(const std::function<void(Transaction&)>& exec) {
1677 Transaction t;
1678 exec(t);
1679 t.apply(true);
1680 }
1681
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001682 sp<SurfaceComposerClient> mComposerClient;
1683 sp<SurfaceControl> mBGSurfaceControl;
1684 sp<SurfaceControl> mFGSurfaceControl;
1685
1686 // This surface is used to ensure that the buffers posted to
1687 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1688 sp<SurfaceControl> mSyncSurfaceControl;
1689};
1690
Robert Carr7f619b22017-11-06 12:56:35 -08001691TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1692 sp<ScreenCapture> sc;
1693
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001694 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1695 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001696 fillSurfaceRGBA8(relative, 10, 10, 10);
1697 waitForPostedBuffers();
1698
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001699 Transaction{}
1700 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001701 .setPosition(relative, 64, 64)
1702 .apply();
1703
1704 {
1705 // The relative should be on top of the FG control.
1706 ScreenCapture::captureScreen(&sc);
1707 sc->checkPixel(64, 64, 10, 10, 10);
1708 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001709 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001710
1711 {
1712 // Nothing should change at this point.
1713 ScreenCapture::captureScreen(&sc);
1714 sc->checkPixel(64, 64, 10, 10, 10);
1715 }
1716
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001717 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001718
1719 {
1720 // Ensure that the relative was actually hidden, rather than
1721 // being left in the detached but visible state.
1722 ScreenCapture::captureScreen(&sc);
1723 sc->expectFGColor(64, 64);
1724 }
1725}
1726
Robert Carr8d5227b2017-03-16 15:41:03 -07001727class GeometryLatchingTest : public LayerUpdateTest {
1728protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001729 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001730 SCOPED_TRACE(trace);
1731 ScreenCapture::captureScreen(&sc);
1732 // We find the leading edge of the FG surface.
1733 sc->expectFGColor(127, 127);
1734 sc->expectBGColor(128, 128);
1735 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001736
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001737 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001738
1739 void unlockFGBuffer() {
1740 sp<Surface> s = mFGSurfaceControl->getSurface();
1741 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1742 waitForPostedBuffers();
1743 }
1744
Robert Carr8d5227b2017-03-16 15:41:03 -07001745 void completeFGResize() {
1746 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1747 waitForPostedBuffers();
1748 }
1749 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001750 asTransaction([&](Transaction& t) {
1751 t.setSize(mFGSurfaceControl, 64, 64);
1752 t.setPosition(mFGSurfaceControl, 64, 64);
1753 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1754 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1755 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001756
1757 EXPECT_INITIAL_STATE("After restoring initial state");
1758 }
1759 sp<ScreenCapture> sc;
1760};
1761
Robert Carr8d5227b2017-03-16 15:41:03 -07001762class CropLatchingTest : public GeometryLatchingTest {
1763protected:
1764 void EXPECT_CROPPED_STATE(const char* trace) {
1765 SCOPED_TRACE(trace);
1766 ScreenCapture::captureScreen(&sc);
1767 // The edge should be moved back one pixel by our crop.
1768 sc->expectFGColor(126, 126);
1769 sc->expectBGColor(127, 127);
1770 sc->expectBGColor(128, 128);
1771 }
chaviw59f5c562017-06-28 16:39:06 -07001772
1773 void EXPECT_RESIZE_STATE(const char* trace) {
1774 SCOPED_TRACE(trace);
1775 ScreenCapture::captureScreen(&sc);
1776 // The FG is now resized too 128,128 at 64,64
1777 sc->expectFGColor(64, 64);
1778 sc->expectFGColor(191, 191);
1779 sc->expectBGColor(192, 192);
1780 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001781};
1782
Robert Carr7bf247e2017-05-18 14:02:49 -07001783// In this test we ensure that setGeometryAppliesWithResize actually demands
1784// a buffer of the new size, and not just any size.
1785TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1786 EXPECT_INITIAL_STATE("before anything");
1787 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001788 asTransaction([&](Transaction& t) {
1789 t.setSize(mFGSurfaceControl, 128, 128);
1790 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1791 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001792
1793 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1794
1795 restoreInitialState();
1796
1797 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1798 // initiating the resize.
1799 lockAndFillFGBuffer();
1800
Robert Carr4cdc58f2017-08-23 14:22:20 -07001801 asTransaction([&](Transaction& t) {
1802 t.setSize(mFGSurfaceControl, 128, 128);
1803 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1804 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1805 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001806
1807 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1808
1809 // We now submit our old buffer, at the old size, and ensure it doesn't
1810 // trigger geometry latching.
1811 unlockFGBuffer();
1812
1813 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1814
1815 completeFGResize();
1816
1817 EXPECT_CROPPED_STATE("after the resize finishes");
1818}
1819
Pablo Ceballos05289c22016-04-14 15:49:55 -07001820TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1821 sp<ScreenCapture> sc;
1822 {
1823 SCOPED_TRACE("before anything");
1824 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001825 sc->expectBGColor(32, 32);
1826 sc->expectFGColor(96, 96);
1827 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001828 }
1829
1830 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001831 asTransaction([&](Transaction& t) {
1832 t.setAlpha(mFGSurfaceControl, 0.75);
1833 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001834 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001835 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001836
Robert Carr4cdc58f2017-08-23 14:22:20 -07001837 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001838 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001839 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001840 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001841 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001842
1843 {
1844 SCOPED_TRACE("before any trigger");
1845 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001846 sc->expectBGColor(32, 32);
1847 sc->expectFGColor(96, 96);
1848 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001849 }
1850
1851 // should trigger the first deferred transaction, but not the second one
1852 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1853 {
1854 SCOPED_TRACE("after first trigger");
1855 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001856 sc->expectBGColor(32, 32);
1857 sc->checkPixel(96, 96, 162, 63, 96);
1858 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001859 }
1860
1861 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001862 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001863
1864 // trigger the second deferred transaction
1865 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1866 {
1867 SCOPED_TRACE("after second trigger");
1868 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001869 sc->expectBGColor(32, 32);
1870 sc->expectBGColor(96, 96);
1871 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001872 }
1873}
1874
Robert Carre392b552017-09-19 12:16:05 -07001875TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1876 sp<ScreenCapture> sc;
1877
1878 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001879 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1880 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1881 sp<SurfaceControl> childBuffer =
1882 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1883 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001884 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1885
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001886 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001887
1888 {
1889 ScreenCapture::captureScreen(&sc);
1890 sc->expectChildColor(73, 73);
1891 sc->expectFGColor(74, 74);
1892 }
1893
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001894 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001895
1896 {
1897 ScreenCapture::captureScreen(&sc);
1898 sc->expectChildColor(73, 73);
1899 sc->expectChildColor(74, 74);
1900 }
1901}
1902
Robert Carr2c5f6d22017-09-26 12:30:35 -07001903TEST_F(LayerUpdateTest, MergingTransactions) {
1904 sp<ScreenCapture> sc;
1905 {
1906 SCOPED_TRACE("before move");
1907 ScreenCapture::captureScreen(&sc);
1908 sc->expectBGColor(0, 12);
1909 sc->expectFGColor(75, 75);
1910 sc->expectBGColor(145, 145);
1911 }
1912
1913 Transaction t1, t2;
1914 t1.setPosition(mFGSurfaceControl, 128, 128);
1915 t2.setPosition(mFGSurfaceControl, 0, 0);
1916 // We expect that the position update from t2 now
1917 // overwrites the position update from t1.
1918 t1.merge(std::move(t2));
1919 t1.apply();
1920
1921 {
1922 ScreenCapture::captureScreen(&sc);
1923 sc->expectFGColor(1, 1);
1924 }
1925}
1926
Robert Carr1f0a16a2016-10-24 16:27:39 -07001927class ChildLayerTest : public LayerUpdateTest {
1928protected:
1929 void SetUp() override {
1930 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001931 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1932 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001933 fillSurfaceRGBA8(mChild, 200, 200, 200);
1934
1935 {
1936 SCOPED_TRACE("before anything");
1937 ScreenCapture::captureScreen(&mCapture);
1938 mCapture->expectChildColor(64, 64);
1939 }
1940 }
1941 void TearDown() override {
1942 LayerUpdateTest::TearDown();
1943 mChild = 0;
1944 }
1945
1946 sp<SurfaceControl> mChild;
1947 sp<ScreenCapture> mCapture;
1948};
1949
1950TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001951 asTransaction([&](Transaction& t) {
1952 t.show(mChild);
1953 t.setPosition(mChild, 10, 10);
1954 t.setPosition(mFGSurfaceControl, 64, 64);
1955 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001956
1957 {
1958 ScreenCapture::captureScreen(&mCapture);
1959 // Top left of foreground must now be visible
1960 mCapture->expectFGColor(64, 64);
1961 // But 10 pixels in we should see the child surface
1962 mCapture->expectChildColor(74, 74);
1963 // And 10 more pixels we should be back to the foreground surface
1964 mCapture->expectFGColor(84, 84);
1965 }
1966
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001967 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001968
1969 {
1970 ScreenCapture::captureScreen(&mCapture);
1971 // Top left of foreground should now be at 0, 0
1972 mCapture->expectFGColor(0, 0);
1973 // But 10 pixels in we should see the child surface
1974 mCapture->expectChildColor(10, 10);
1975 // And 10 more pixels we should be back to the foreground surface
1976 mCapture->expectFGColor(20, 20);
1977 }
1978}
1979
Robert Carr41b08b52017-06-01 16:11:34 -07001980TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001981 asTransaction([&](Transaction& t) {
1982 t.show(mChild);
1983 t.setPosition(mChild, 0, 0);
1984 t.setPosition(mFGSurfaceControl, 0, 0);
1985 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1986 });
Robert Carr41b08b52017-06-01 16:11:34 -07001987
1988 {
1989 ScreenCapture::captureScreen(&mCapture);
1990 mCapture->expectChildColor(0, 0);
1991 mCapture->expectChildColor(4, 4);
1992 mCapture->expectBGColor(5, 5);
1993 }
1994}
1995
1996TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001997 asTransaction([&](Transaction& t) {
1998 t.show(mChild);
1999 t.setPosition(mChild, 0, 0);
2000 t.setPosition(mFGSurfaceControl, 0, 0);
2001 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
2002 });
Robert Carr41b08b52017-06-01 16:11:34 -07002003
2004 {
2005 ScreenCapture::captureScreen(&mCapture);
2006 mCapture->expectChildColor(0, 0);
2007 mCapture->expectChildColor(4, 4);
2008 mCapture->expectBGColor(5, 5);
2009 }
2010}
2011
Robert Carr1f0a16a2016-10-24 16:27:39 -07002012TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002013 asTransaction([&](Transaction& t) {
2014 t.show(mChild);
2015 t.setPosition(mFGSurfaceControl, 0, 0);
2016 t.setPosition(mChild, 63, 63);
2017 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002018
2019 {
2020 ScreenCapture::captureScreen(&mCapture);
2021 mCapture->expectFGColor(0, 0);
2022 // Last pixel in foreground should now be the child.
2023 mCapture->expectChildColor(63, 63);
2024 // But the child should be constrained and the next pixel
2025 // must be the background
2026 mCapture->expectBGColor(64, 64);
2027 }
2028}
2029
2030TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002031 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002032
2033 // Find the boundary between the parent and child
2034 {
2035 ScreenCapture::captureScreen(&mCapture);
2036 mCapture->expectChildColor(9, 9);
2037 mCapture->expectFGColor(10, 10);
2038 }
2039
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002040 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002041
2042 // The boundary should be twice as far from the origin now.
2043 // The pixels from the last test should all be child now
2044 {
2045 ScreenCapture::captureScreen(&mCapture);
2046 mCapture->expectChildColor(9, 9);
2047 mCapture->expectChildColor(10, 10);
2048 mCapture->expectChildColor(19, 19);
2049 mCapture->expectFGColor(20, 20);
2050 }
2051}
Robert Carr9524cb32017-02-13 11:32:32 -08002052
Robert Carr6452f122017-03-21 10:41:29 -07002053TEST_F(ChildLayerTest, ChildLayerAlpha) {
2054 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2055 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2056 fillSurfaceRGBA8(mChild, 0, 254, 0);
2057 waitForPostedBuffers();
2058
Robert Carr4cdc58f2017-08-23 14:22:20 -07002059 asTransaction([&](Transaction& t) {
2060 t.show(mChild);
2061 t.setPosition(mChild, 0, 0);
2062 t.setPosition(mFGSurfaceControl, 0, 0);
2063 });
Robert Carr6452f122017-03-21 10:41:29 -07002064
2065 {
2066 ScreenCapture::captureScreen(&mCapture);
2067 // Unblended child color
2068 mCapture->checkPixel(0, 0, 0, 254, 0);
2069 }
2070
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002071 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002072
2073 {
2074 ScreenCapture::captureScreen(&mCapture);
2075 // Child and BG blended.
2076 mCapture->checkPixel(0, 0, 127, 127, 0);
2077 }
2078
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002079 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002080
2081 {
2082 ScreenCapture::captureScreen(&mCapture);
2083 // Child and BG blended.
2084 mCapture->checkPixel(0, 0, 95, 64, 95);
2085 }
2086}
2087
Robert Carr9524cb32017-02-13 11:32:32 -08002088TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002089 asTransaction([&](Transaction& t) {
2090 t.show(mChild);
2091 t.setPosition(mChild, 10, 10);
2092 t.setPosition(mFGSurfaceControl, 64, 64);
2093 });
Robert Carr9524cb32017-02-13 11:32:32 -08002094
2095 {
2096 ScreenCapture::captureScreen(&mCapture);
2097 // Top left of foreground must now be visible
2098 mCapture->expectFGColor(64, 64);
2099 // But 10 pixels in we should see the child surface
2100 mCapture->expectChildColor(74, 74);
2101 // And 10 more pixels we should be back to the foreground surface
2102 mCapture->expectFGColor(84, 84);
2103 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002104
2105 asTransaction([&](Transaction& t) {
2106 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2107 });
2108
Robert Carr9524cb32017-02-13 11:32:32 -08002109 {
2110 ScreenCapture::captureScreen(&mCapture);
2111 mCapture->expectFGColor(64, 64);
2112 // In reparenting we should have exposed the entire foreground surface.
2113 mCapture->expectFGColor(74, 74);
2114 // And the child layer should now begin at 10, 10 (since the BG
2115 // layer is at (0, 0)).
2116 mCapture->expectBGColor(9, 9);
2117 mCapture->expectChildColor(10, 10);
2118 }
2119}
2120
chaviw161410b02017-07-27 10:46:08 -07002121TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002122 asTransaction([&](Transaction& t) {
2123 t.show(mChild);
2124 t.setPosition(mChild, 10, 10);
2125 t.setPosition(mFGSurfaceControl, 64, 64);
2126 });
Robert Carr9524cb32017-02-13 11:32:32 -08002127
2128 {
2129 ScreenCapture::captureScreen(&mCapture);
2130 // Top left of foreground must now be visible
2131 mCapture->expectFGColor(64, 64);
2132 // But 10 pixels in we should see the child surface
2133 mCapture->expectChildColor(74, 74);
2134 // And 10 more pixels we should be back to the foreground surface
2135 mCapture->expectFGColor(84, 84);
2136 }
2137
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002138 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002139
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002140 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002141
chaviw161410b02017-07-27 10:46:08 -07002142 // Since the child has the same client as the parent, it will not get
2143 // detached and will be hidden.
2144 {
2145 ScreenCapture::captureScreen(&mCapture);
2146 mCapture->expectFGColor(64, 64);
2147 mCapture->expectFGColor(74, 74);
2148 mCapture->expectFGColor(84, 84);
2149 }
2150}
2151
2152TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2153 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002154 sp<SurfaceControl> mChildNewClient =
2155 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2156 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002157
Peiyong Lin566a3b42018-01-09 18:22:43 -08002158 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07002159 ASSERT_TRUE(mChildNewClient->isValid());
2160
2161 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2162
Robert Carr4cdc58f2017-08-23 14:22:20 -07002163 asTransaction([&](Transaction& t) {
2164 t.hide(mChild);
2165 t.show(mChildNewClient);
2166 t.setPosition(mChildNewClient, 10, 10);
2167 t.setPosition(mFGSurfaceControl, 64, 64);
2168 });
chaviw161410b02017-07-27 10:46:08 -07002169
2170 {
2171 ScreenCapture::captureScreen(&mCapture);
2172 // Top left of foreground must now be visible
2173 mCapture->expectFGColor(64, 64);
2174 // But 10 pixels in we should see the child surface
2175 mCapture->expectChildColor(74, 74);
2176 // And 10 more pixels we should be back to the foreground surface
2177 mCapture->expectFGColor(84, 84);
2178 }
2179
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002180 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002181
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002182 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002183
Robert Carr9524cb32017-02-13 11:32:32 -08002184 // Nothing should have changed.
2185 {
2186 ScreenCapture::captureScreen(&mCapture);
2187 mCapture->expectFGColor(64, 64);
2188 mCapture->expectChildColor(74, 74);
2189 mCapture->expectFGColor(84, 84);
2190 }
2191}
2192
Robert Carr9b429f42017-04-17 14:56:57 -07002193TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002194 asTransaction([&](Transaction& t) {
2195 t.show(mChild);
2196 t.setPosition(mChild, 0, 0);
2197 t.setPosition(mFGSurfaceControl, 0, 0);
2198 });
Robert Carr9b429f42017-04-17 14:56:57 -07002199
2200 {
2201 ScreenCapture::captureScreen(&mCapture);
2202 // We've positioned the child in the top left.
2203 mCapture->expectChildColor(0, 0);
2204 // But it's only 10x10.
2205 mCapture->expectFGColor(10, 10);
2206 }
2207
Robert Carr4cdc58f2017-08-23 14:22:20 -07002208 asTransaction([&](Transaction& t) {
2209 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2210 // We cause scaling by 2.
2211 t.setSize(mFGSurfaceControl, 128, 128);
2212 });
Robert Carr9b429f42017-04-17 14:56:57 -07002213
2214 {
2215 ScreenCapture::captureScreen(&mCapture);
2216 // We've positioned the child in the top left.
2217 mCapture->expectChildColor(0, 0);
2218 mCapture->expectChildColor(10, 10);
2219 mCapture->expectChildColor(19, 19);
2220 // And now it should be scaled all the way to 20x20
2221 mCapture->expectFGColor(20, 20);
2222 }
2223}
2224
Robert Carr1725eee2017-04-26 18:32:15 -07002225// Regression test for b/37673612
2226TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002227 asTransaction([&](Transaction& t) {
2228 t.show(mChild);
2229 t.setPosition(mChild, 0, 0);
2230 t.setPosition(mFGSurfaceControl, 0, 0);
2231 });
Robert Carr1725eee2017-04-26 18:32:15 -07002232
2233 {
2234 ScreenCapture::captureScreen(&mCapture);
2235 // We've positioned the child in the top left.
2236 mCapture->expectChildColor(0, 0);
2237 // But it's only 10x10.
2238 mCapture->expectFGColor(10, 10);
2239 }
Robert Carr1725eee2017-04-26 18:32:15 -07002240 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2241 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002242 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002243 sp<Surface> s = mFGSurfaceControl->getSurface();
2244 auto anw = static_cast<ANativeWindow*>(s.get());
2245 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2246 native_window_set_buffers_dimensions(anw, 64, 128);
2247 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2248 waitForPostedBuffers();
2249
2250 {
2251 // The child should still be in the same place and not have any strange scaling as in
2252 // b/37673612.
2253 ScreenCapture::captureScreen(&mCapture);
2254 mCapture->expectChildColor(0, 0);
2255 mCapture->expectFGColor(10, 10);
2256 }
2257}
2258
Dan Stoza412903f2017-04-27 13:42:17 -07002259TEST_F(ChildLayerTest, Bug36858924) {
2260 // Destroy the child layer
2261 mChild.clear();
2262
2263 // Now recreate it as hidden
2264 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2265 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2266 mFGSurfaceControl.get());
2267
2268 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002269 asTransaction([&](Transaction& t) {
2270 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002271 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002272 t.show(mChild);
2273 });
Dan Stoza412903f2017-04-27 13:42:17 -07002274
2275 // Render the foreground surface a few times
2276 //
2277 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2278 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2279 // never acquire/release the first buffer
2280 ALOGI("Filling 1");
2281 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2282 ALOGI("Filling 2");
2283 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2284 ALOGI("Filling 3");
2285 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2286 ALOGI("Filling 4");
2287 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2288}
2289
chaviwf1961f72017-09-18 16:41:07 -07002290TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002291 asTransaction([&](Transaction& t) {
2292 t.show(mChild);
2293 t.setPosition(mChild, 10, 10);
2294 t.setPosition(mFGSurfaceControl, 64, 64);
2295 });
chaviw06178942017-07-27 10:25:59 -07002296
2297 {
2298 ScreenCapture::captureScreen(&mCapture);
2299 // Top left of foreground must now be visible
2300 mCapture->expectFGColor(64, 64);
2301 // But 10 pixels in we should see the child surface
2302 mCapture->expectChildColor(74, 74);
2303 // And 10 more pixels we should be back to the foreground surface
2304 mCapture->expectFGColor(84, 84);
2305 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002306
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002307 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002308
chaviw06178942017-07-27 10:25:59 -07002309 {
2310 ScreenCapture::captureScreen(&mCapture);
2311 mCapture->expectFGColor(64, 64);
2312 // In reparenting we should have exposed the entire foreground surface.
2313 mCapture->expectFGColor(74, 74);
2314 // And the child layer should now begin at 10, 10 (since the BG
2315 // layer is at (0, 0)).
2316 mCapture->expectBGColor(9, 9);
2317 mCapture->expectChildColor(10, 10);
2318 }
2319}
2320
chaviwf1961f72017-09-18 16:41:07 -07002321TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002322 asTransaction([&](Transaction& t) {
2323 t.show(mChild);
2324 t.setPosition(mChild, 10, 10);
2325 t.setPosition(mFGSurfaceControl, 64, 64);
2326 });
chaviwf1961f72017-09-18 16:41:07 -07002327
2328 {
2329 ScreenCapture::captureScreen(&mCapture);
2330 // Top left of foreground must now be visible
2331 mCapture->expectFGColor(64, 64);
2332 // But 10 pixels in we should see the child surface
2333 mCapture->expectChildColor(74, 74);
2334 // And 10 more pixels we should be back to the foreground surface
2335 mCapture->expectFGColor(84, 84);
2336 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002337 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002338 {
2339 ScreenCapture::captureScreen(&mCapture);
2340 // Nothing should have changed.
2341 mCapture->expectFGColor(64, 64);
2342 mCapture->expectChildColor(74, 74);
2343 mCapture->expectFGColor(84, 84);
2344 }
2345}
2346
2347TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002348 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2349 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002350 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07002351 ASSERT_TRUE(newSurface->isValid());
2352
2353 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002354 asTransaction([&](Transaction& t) {
2355 t.hide(mChild);
2356 t.show(newSurface);
2357 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002358 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002359 t.setPosition(mFGSurfaceControl, 64, 64);
2360 });
chaviwf1961f72017-09-18 16:41:07 -07002361
2362 {
2363 ScreenCapture::captureScreen(&mCapture);
2364 // Top left of foreground must now be visible
2365 mCapture->expectFGColor(64, 64);
2366 // At 10, 10 we should see the new surface
2367 mCapture->checkPixel(10, 10, 63, 195, 63);
2368 }
2369
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002370 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002371
2372 {
2373 ScreenCapture::captureScreen(&mCapture);
2374 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2375 // mFGSurface, putting it at 74, 74.
2376 mCapture->expectFGColor(64, 64);
2377 mCapture->checkPixel(74, 74, 63, 195, 63);
2378 mCapture->expectFGColor(84, 84);
2379 }
2380}
2381
chaviwc9674332017-08-28 12:32:18 -07002382TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002383 sp<SurfaceControl> grandchild =
2384 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2385 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002386 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2387
2388 {
2389 ScreenCapture::captureScreen(&mCapture);
2390 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2391 // which begins at 64, 64
2392 mCapture->checkPixel(64, 64, 50, 50, 50);
2393 }
2394}
2395
Robert Carr503c7042017-09-27 15:06:08 -07002396TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002397 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2398 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002399 fillSurfaceRGBA8(relative, 255, 255, 255);
2400
2401 Transaction t;
2402 t.setLayer(relative, INT32_MAX)
2403 .setRelativeLayer(mChild, relative->getHandle(), 1)
2404 .setPosition(mFGSurfaceControl, 0, 0)
2405 .apply(true);
2406
2407 // We expect that the child should have been elevated above our
2408 // INT_MAX layer even though it's not a child of it.
2409 {
2410 ScreenCapture::captureScreen(&mCapture);
2411 mCapture->expectChildColor(0, 0);
2412 mCapture->expectChildColor(9, 9);
2413 mCapture->checkPixel(10, 10, 255, 255, 255);
2414 }
2415}
2416
chaviwa76b2712017-09-20 12:02:26 -07002417class ScreenCaptureTest : public LayerUpdateTest {
2418protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002419 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07002420};
2421
2422TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2423 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002424 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002425 mCapture->expectBGColor(0, 0);
2426 // Doesn't capture FG layer which is at 64, 64
2427 mCapture->expectBGColor(64, 64);
2428}
2429
2430TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2431 auto fgHandle = mFGSurfaceControl->getHandle();
2432
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002433 sp<SurfaceControl> child =
2434 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2435 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002436 fillSurfaceRGBA8(child, 200, 200, 200);
2437
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002438 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002439
2440 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002441 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002442 mCapture->expectFGColor(10, 10);
2443 mCapture->expectChildColor(0, 0);
2444}
2445
Robert Carr578038f2018-03-09 12:25:24 -08002446TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
2447 auto fgHandle = mFGSurfaceControl->getHandle();
2448
2449 sp<SurfaceControl> child =
2450 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2451 0, mFGSurfaceControl.get());
2452 fillSurfaceRGBA8(child, 200, 200, 200);
2453
2454 SurfaceComposerClient::Transaction().show(child).apply(true);
2455
2456 // Captures mFGSurfaceControl's child
2457 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2458 mCapture->checkPixel(10, 10, 0, 0, 0);
2459 mCapture->expectChildColor(0, 0);
2460}
2461
chaviw50da5042018-04-09 13:49:37 -07002462TEST_F(ScreenCaptureTest, CaptureTransparent) {
2463 sp<SurfaceControl> child =
2464 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2465 0, mFGSurfaceControl.get());
2466
2467 fillSurfaceRGBA8(child, 200, 200, 200);
2468
2469 SurfaceComposerClient::Transaction().show(child).apply(true);
2470
2471 auto childHandle = child->getHandle();
2472
2473 // Captures child
2474 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
2475 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
2476 // Area outside of child's bounds is transparent.
2477 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
2478}
2479
chaviw4b129c22018-04-09 16:19:43 -07002480TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
2481 auto fgHandle = mFGSurfaceControl->getHandle();
2482
2483 sp<SurfaceControl> child =
2484 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2485 0, mFGSurfaceControl.get());
2486 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 10,
2487 10, PIXEL_FORMAT_RGBA_8888, 0);
2488 fillSurfaceRGBA8(child, 200, 200, 200);
2489 fillSurfaceRGBA8(relative, 100, 100, 100);
2490
2491 SurfaceComposerClient::Transaction()
2492 .show(child)
2493 // Set relative layer above fg layer so should be shown above when computing all layers.
2494 .setRelativeLayer(relative, fgHandle, 1)
2495 .show(relative)
2496 .apply(true);
2497
2498 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
2499 ScreenCapture::captureLayers(&mCapture, fgHandle);
2500 mCapture->expectFGColor(10, 10);
2501 mCapture->expectChildColor(0, 0);
2502}
2503
2504TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
2505 auto fgHandle = mFGSurfaceControl->getHandle();
2506
2507 sp<SurfaceControl> child =
2508 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2509 0, mFGSurfaceControl.get());
2510 sp<SurfaceControl> relative =
2511 mComposerClient->createSurface(String8("Relative surface"), 10, 10,
2512 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2513 fillSurfaceRGBA8(child, 200, 200, 200);
2514 fillSurfaceRGBA8(relative, 100, 100, 100);
2515
2516 SurfaceComposerClient::Transaction()
2517 .show(child)
2518 // Set relative layer below fg layer but relative to child layer so it should be shown
2519 // above child layer.
2520 .setLayer(relative, -1)
2521 .setRelativeLayer(relative, child->getHandle(), 1)
2522 .show(relative)
2523 .apply(true);
2524
2525 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
2526 // relative value should be taken into account, placing it above child layer.
2527 ScreenCapture::captureLayers(&mCapture, fgHandle);
2528 mCapture->expectFGColor(10, 10);
2529 // Relative layer is showing on top of child layer
2530 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
2531}
Robert Carr578038f2018-03-09 12:25:24 -08002532
2533// In the following tests we verify successful skipping of a parent layer,
2534// so we use the same verification logic and only change how we mutate
2535// the parent layer to verify that various properties are ignored.
2536class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
2537public:
2538 void SetUp() override {
2539 LayerUpdateTest::SetUp();
2540
2541 mChild =
2542 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2543 0, mFGSurfaceControl.get());
2544 fillSurfaceRGBA8(mChild, 200, 200, 200);
2545
2546 SurfaceComposerClient::Transaction().show(mChild).apply(true);
2547 }
2548
2549 void verify() {
2550 auto fgHandle = mFGSurfaceControl->getHandle();
2551 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2552 mCapture->checkPixel(10, 10, 0, 0, 0);
2553 mCapture->expectChildColor(0, 0);
2554 }
2555
2556 std::unique_ptr<ScreenCapture> mCapture;
2557 sp<SurfaceControl> mChild;
2558};
2559
2560TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
2561
2562 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2563
2564 // Even though the parent is hidden we should still capture the child.
2565 verify();
2566}
2567
2568TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
2569
2570 SurfaceComposerClient::Transaction().setCrop(mFGSurfaceControl, Rect(0, 0, 1, 1)).apply(true);
2571
2572 // Even though the parent is cropped out we should still capture the child.
2573 verify();
2574}
2575
2576TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
2577
2578 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
2579
2580 // We should not inherit the parent scaling.
2581 verify();
2582}
2583
Robert Carr15eae092018-03-23 13:43:53 -07002584TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
2585 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2586
2587 // Even though the parent is hidden we should still capture the child.
2588 verify();
2589
2590 // Verify everything was properly hidden when rendering the full-screen.
2591 screenshot()->expectBGColor(0,0);
2592}
2593
2594
chaviwa76b2712017-09-20 12:02:26 -07002595TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2596 auto fgHandle = mFGSurfaceControl->getHandle();
2597
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002598 sp<SurfaceControl> child =
2599 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2600 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002601 fillSurfaceRGBA8(child, 200, 200, 200);
2602
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002603 sp<SurfaceControl> grandchild =
2604 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2605 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002606
2607 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2608 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002609 .show(child)
2610 .setPosition(grandchild, 5, 5)
2611 .show(grandchild)
2612 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002613
2614 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002615 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002616 mCapture->expectFGColor(10, 10);
2617 mCapture->expectChildColor(0, 0);
2618 mCapture->checkPixel(5, 5, 50, 50, 50);
2619}
2620
2621TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002622 sp<SurfaceControl> child =
2623 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2624 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002625 fillSurfaceRGBA8(child, 200, 200, 200);
2626 auto childHandle = child->getHandle();
2627
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002628 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002629
2630 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002631 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07002632 mCapture->expectChildColor(0, 0);
2633 mCapture->expectChildColor(9, 9);
2634}
2635
2636TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002637 sp<SurfaceControl> child =
2638 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2639 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002640 fillSurfaceRGBA8(child, 200, 200, 200);
2641 auto childHandle = child->getHandle();
2642
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002643 sp<SurfaceControl> grandchild =
2644 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2645 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002646 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2647
2648 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002649 .show(child)
2650 .setPosition(grandchild, 5, 5)
2651 .show(grandchild)
2652 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002653
2654 auto grandchildHandle = grandchild->getHandle();
2655
2656 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002657 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07002658 mCapture->checkPixel(0, 0, 50, 50, 50);
2659 mCapture->checkPixel(4, 4, 50, 50, 50);
2660}
2661
chaviw7206d492017-11-10 16:16:12 -08002662TEST_F(ScreenCaptureTest, CaptureCrop) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002663 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2664 PIXEL_FORMAT_RGBA_8888, 0);
2665 sp<SurfaceControl> blueLayer =
2666 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2667 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002668
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002669 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2670 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002671
2672 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002673 .setLayer(redLayer, INT32_MAX - 1)
2674 .show(redLayer)
2675 .show(blueLayer)
2676 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002677
2678 auto redLayerHandle = redLayer->getHandle();
2679
2680 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002681 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2682 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2683 // red area below the blue area
2684 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2685 // red area to the right of the blue area
2686 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002687
2688 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002689 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08002690 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
2691 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002692 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08002693 mCapture->checkPixel(30, 30, 0, 0, 0);
2694}
2695
2696TEST_F(ScreenCaptureTest, CaptureSize) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002697 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2698 PIXEL_FORMAT_RGBA_8888, 0);
2699 sp<SurfaceControl> blueLayer =
2700 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2701 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002702
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002703 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2704 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002705
2706 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002707 .setLayer(redLayer, INT32_MAX - 1)
2708 .show(redLayer)
2709 .show(blueLayer)
2710 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002711
2712 auto redLayerHandle = redLayer->getHandle();
2713
2714 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002715 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2716 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2717 // red area below the blue area
2718 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2719 // red area to the right of the blue area
2720 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002721
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002722 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08002723 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002724 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
2725 // red area below the blue area
2726 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
2727 // red area to the right of the blue area
2728 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002729 mCapture->checkPixel(30, 30, 0, 0, 0);
2730}
2731
2732TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002733 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2734 PIXEL_FORMAT_RGBA_8888, 0);
chaviw7206d492017-11-10 16:16:12 -08002735
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002736 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
chaviw7206d492017-11-10 16:16:12 -08002737
2738 auto redLayerHandle = redLayer->getHandle();
2739 mComposerClient->destroySurface(redLayerHandle);
2740 SurfaceComposerClient::Transaction().apply(true);
2741
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002742 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08002743
2744 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002745 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2746 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08002747}
2748
chaviw8e3fe5d2018-02-22 10:55:42 -08002749
2750class DereferenceSurfaceControlTest : public LayerTransactionTest {
2751protected:
2752 void SetUp() override {
2753 LayerTransactionTest::SetUp();
2754 bgLayer = createLayer("BG layer", 20, 20);
2755 fillLayerColor(bgLayer, Color::RED);
2756 fgLayer = createLayer("FG layer", 20, 20);
2757 fillLayerColor(fgLayer, Color::BLUE);
2758 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
2759 {
2760 SCOPED_TRACE("before anything");
2761 auto shot = screenshot();
2762 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2763 }
2764 }
2765 void TearDown() override {
2766 LayerTransactionTest::TearDown();
2767 bgLayer = 0;
2768 fgLayer = 0;
2769 }
2770
2771 sp<SurfaceControl> bgLayer;
2772 sp<SurfaceControl> fgLayer;
2773};
2774
2775TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
2776 fgLayer = nullptr;
2777 {
2778 SCOPED_TRACE("after setting null");
2779 auto shot = screenshot();
2780 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
2781 }
2782}
2783
2784TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
2785 auto transaction = Transaction().show(fgLayer);
2786 fgLayer = nullptr;
2787 {
2788 SCOPED_TRACE("after setting null");
2789 auto shot = screenshot();
2790 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2791 }
2792}
2793
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002794} // namespace android