blob: e04e1e9201976fb05bf6710af1971ed6d5ca8b97 [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
Chih-Hung Hsieh22749042018-12-20 15:50:39 -0800311 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000312 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.
Robert Carr0283c732019-04-02 16:32:58 -0700944 bool outCapturedSecureLayers = false;
Robert Carr9085ced2019-02-19 10:05:00 -0800945 ASSERT_EQ(NO_ERROR,
Robert Carr0283c732019-04-02 16:32:58 -0700946 composer->captureScreen(mDisplay, &outBuffer, outCapturedSecureLayers,
Robert Carr9085ced2019-02-19 10:05:00 -0800947 Rect(), 0, 0, 0, INT_MAX, false, ISurfaceComposer::eRotateNone, true));
Robert Carr0283c732019-04-02 16:32:58 -0700948 ASSERT_EQ(true, outCapturedSecureLayers);
Robert Carr9085ced2019-02-19 10:05:00 -0800949 ScreenCapture sc(outBuffer);
950 sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
951}
952
Chia-I Wu2113bdd2017-11-01 15:16:35 -0700953TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
954 const Rect top(0, 0, 32, 16);
955 const Rect bottom(0, 16, 32, 32);
956 sp<SurfaceControl> layer;
957 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
958
959 ANativeWindow_Buffer buffer;
960 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
961 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
962 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
963 // setTransparentRegionHint always applies to the following buffer
964 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
965 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
966 {
967 SCOPED_TRACE("top transparent");
968 auto shot = screenshot();
969 shot->expectColor(top, Color::BLACK);
970 shot->expectColor(bottom, Color::RED);
971 }
972
973 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
974 {
975 SCOPED_TRACE("transparent region hint pending");
976 auto shot = screenshot();
977 shot->expectColor(top, Color::BLACK);
978 shot->expectColor(bottom, Color::RED);
979 }
980
981 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
982 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
983 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
984 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
985 {
986 SCOPED_TRACE("bottom transparent");
987 auto shot = screenshot();
988 shot->expectColor(top, Color::RED);
989 shot->expectColor(bottom, Color::BLACK);
990 }
991}
992
993TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
994 sp<SurfaceControl> layerTransparent;
995 sp<SurfaceControl> layerR;
996 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
997 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
998
999 // check that transparent region hint is bound by the layer size
1000 Transaction()
1001 .setTransparentRegionHint(layerTransparent,
1002 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
1003 .setPosition(layerR, 16, 16)
1004 .setLayer(layerR, mLayerZBase + 1)
1005 .apply();
1006 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
1007 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
1008 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1009}
1010
Chia-I Wua8a515e2017-11-01 15:16:35 -07001011TEST_F(LayerTransactionTest, SetAlphaBasic) {
1012 sp<SurfaceControl> layer1;
1013 sp<SurfaceControl> layer2;
1014 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
1015 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
1016 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
1017 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
1018
1019 Transaction()
1020 .setAlpha(layer1, 0.25f)
1021 .setAlpha(layer2, 0.75f)
1022 .setPosition(layer2, 16, 0)
1023 .setLayer(layer2, mLayerZBase + 1)
1024 .apply();
1025 {
1026 auto shot = screenshot();
1027 uint8_t r = 16; // 64 * 0.25f
1028 uint8_t g = 48; // 64 * 0.75f
1029 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1030 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1031
1032 r /= 4; // r * (1.0f - 0.75f)
1033 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1034 }
1035}
1036
1037TEST_F(LayerTransactionTest, SetAlphaClamped) {
1038 const Color color = {64, 0, 0, 255};
1039 sp<SurfaceControl> layer;
1040 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1041 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
1042
1043 Transaction().setAlpha(layer, 2.0f).apply();
1044 {
1045 SCOPED_TRACE("clamped to 1.0f");
1046 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1047 }
1048
1049 Transaction().setAlpha(layer, -1.0f).apply();
1050 {
1051 SCOPED_TRACE("clamped to 0.0f");
1052 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1053 }
1054}
1055
Chia-I Wue4ef6102017-11-01 15:16:35 -07001056TEST_F(LayerTransactionTest, SetColorBasic) {
1057 sp<SurfaceControl> bufferLayer;
1058 sp<SurfaceControl> colorLayer;
1059 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1060 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1061 ASSERT_NO_FATAL_FAILURE(
1062 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1063
1064 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
1065 {
1066 SCOPED_TRACE("default color");
1067 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1068 }
1069
1070 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1071 const Color expected = {15, 51, 85, 255};
1072 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1073 // channel) should be less than one
1074 const uint8_t tolerance = 1;
1075 Transaction().setColor(colorLayer, color).apply();
1076 {
1077 SCOPED_TRACE("new color");
1078 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1079 }
1080}
1081
1082TEST_F(LayerTransactionTest, SetColorClamped) {
1083 sp<SurfaceControl> colorLayer;
1084 ASSERT_NO_FATAL_FAILURE(
1085 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1086
1087 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
1088 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1089}
1090
1091TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1092 sp<SurfaceControl> bufferLayer;
1093 sp<SurfaceControl> colorLayer;
1094 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1095 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1096 ASSERT_NO_FATAL_FAILURE(
1097 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1098
1099 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1100 const float alpha = 0.25f;
1101 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1102 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1103 // channel) should be less than one
1104 const uint8_t tolerance = 1;
1105 Transaction()
1106 .setColor(colorLayer, color)
1107 .setAlpha(colorLayer, alpha)
1108 .setLayer(colorLayer, mLayerZBase + 1)
1109 .apply();
1110 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1111 tolerance);
1112}
1113
Adrian Roosb7a96502018-04-08 11:38:55 -07001114TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1115 sp<SurfaceControl> bufferLayer;
1116 sp<SurfaceControl> parentLayer;
1117 sp<SurfaceControl> colorLayer;
1118 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1119 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
1120 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1121 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer(
1122 "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1123
1124 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1125 const float alpha = 0.25f;
1126 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1127 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1128 // channel) should be less than one
1129 const uint8_t tolerance = 1;
1130 Transaction()
1131 .reparent(colorLayer, parentLayer->getHandle())
1132 .setColor(colorLayer, color)
1133 .setAlpha(parentLayer, alpha)
1134 .setLayer(parentLayer, mLayerZBase + 1)
1135 .apply();
1136 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1137 tolerance);
1138}
1139
Chia-I Wue4ef6102017-11-01 15:16:35 -07001140TEST_F(LayerTransactionTest, SetColorWithBuffer) {
1141 sp<SurfaceControl> bufferLayer;
1142 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1143 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1144
1145 // color is ignored
1146 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1147 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1148}
1149
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001150TEST_F(LayerTransactionTest, SetLayerStackBasic) {
1151 sp<SurfaceControl> layer;
1152 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1153 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1154
1155 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1156 {
1157 SCOPED_TRACE("non-existing layer stack");
1158 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1159 }
1160
1161 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1162 {
1163 SCOPED_TRACE("original layer stack");
1164 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1165 }
1166}
1167
Chia-I Wu93853fe2017-11-02 08:30:27 -07001168TEST_F(LayerTransactionTest, SetMatrixBasic) {
1169 sp<SurfaceControl> layer;
1170 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1171 ASSERT_NO_FATAL_FAILURE(
1172 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1173
1174 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1175 {
1176 SCOPED_TRACE("IDENTITY");
1177 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1178 Color::WHITE);
1179 }
1180
1181 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1182 {
1183 SCOPED_TRACE("FLIP_H");
1184 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1185 Color::BLUE);
1186 }
1187
1188 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1189 {
1190 SCOPED_TRACE("FLIP_V");
1191 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1192 Color::GREEN);
1193 }
1194
1195 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1196 {
1197 SCOPED_TRACE("ROT_90");
1198 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1199 Color::GREEN);
1200 }
1201
1202 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1203 {
1204 SCOPED_TRACE("SCALE");
1205 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1206 Color::WHITE, true /* filtered */);
1207 }
1208}
1209
1210TEST_F(LayerTransactionTest, SetMatrixRot45) {
1211 sp<SurfaceControl> layer;
1212 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1213 ASSERT_NO_FATAL_FAILURE(
1214 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1215
1216 const float rot = M_SQRT1_2; // 45 degrees
1217 const float trans = M_SQRT2 * 16.0f;
1218 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1219
1220 auto shot = screenshot();
1221 // check a 8x8 region inside each color
1222 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1223 const int32_t halfL = 4;
1224 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1225 };
1226 const int32_t unit = int32_t(trans / 2);
1227 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1228 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1229 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1230 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1231}
1232
1233TEST_F(LayerTransactionTest, SetMatrixWithResize) {
1234 sp<SurfaceControl> layer;
1235 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1236 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1237
1238 // setMatrix is applied after any pending resize, unlike setPosition
1239 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1240 {
1241 SCOPED_TRACE("resize pending");
1242 auto shot = screenshot();
1243 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1244 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1245 }
1246
1247 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1248 {
1249 SCOPED_TRACE("resize applied");
1250 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1251 }
1252}
1253
1254TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) {
1255 sp<SurfaceControl> layer;
1256 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1257 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1258
1259 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1260 Transaction()
1261 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1262 .setSize(layer, 64, 64)
1263 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1264 .apply();
1265 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1266}
1267
Chia-I Wua56b2042017-11-01 15:16:35 -07001268TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) {
1269 sp<SurfaceControl> layer;
1270 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1271 ASSERT_NO_FATAL_FAILURE(
1272 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1273
1274 // XXX SCALE_CROP is not respected; calling setSize and
1275 // setOverrideScalingMode in separate transactions does not work
1276 // (b/69315456)
1277 Transaction()
1278 .setSize(layer, 64, 16)
1279 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1280 .apply();
1281 {
1282 SCOPED_TRACE("SCALE_TO_WINDOW");
1283 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1284 Color::WHITE, true /* filtered */);
1285 }
1286}
1287
Chia-I Wu04dcca82017-11-02 08:30:27 -07001288TEST_F(LayerTransactionTest, SetCropBasic) {
1289 sp<SurfaceControl> layer;
1290 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1291 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1292 const Rect crop(8, 8, 24, 24);
1293
1294 Transaction().setCrop(layer, crop).apply();
1295 auto shot = screenshot();
1296 shot->expectColor(crop, Color::RED);
1297 shot->expectBorder(crop, Color::BLACK);
1298}
1299
1300TEST_F(LayerTransactionTest, SetCropEmpty) {
1301 sp<SurfaceControl> layer;
1302 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1303 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1304
1305 {
1306 SCOPED_TRACE("empty rect");
1307 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1308 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1309 }
1310
1311 {
1312 SCOPED_TRACE("negative rect");
1313 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1314 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1315 }
1316}
1317
1318TEST_F(LayerTransactionTest, SetCropOutOfBounds) {
1319 sp<SurfaceControl> layer;
1320 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1321 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1322
1323 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1324 auto shot = screenshot();
1325 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1326 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1327}
1328
1329TEST_F(LayerTransactionTest, SetCropWithTranslation) {
1330 sp<SurfaceControl> layer;
1331 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1332 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1333
1334 const Point position(32, 32);
1335 const Rect crop(8, 8, 24, 24);
1336 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1337 auto shot = screenshot();
1338 shot->expectColor(crop + position, Color::RED);
1339 shot->expectBorder(crop + position, Color::BLACK);
1340}
1341
1342TEST_F(LayerTransactionTest, SetCropWithScale) {
1343 sp<SurfaceControl> layer;
1344 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1345 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1346
1347 // crop is affected by matrix
1348 Transaction()
1349 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1350 .setCrop(layer, Rect(8, 8, 24, 24))
1351 .apply();
1352 auto shot = screenshot();
1353 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1354 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1355}
1356
1357TEST_F(LayerTransactionTest, SetCropWithResize) {
1358 sp<SurfaceControl> layer;
1359 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1360 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1361
1362 // setCrop is applied immediately by default, with or without resize pending
1363 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1364 {
1365 SCOPED_TRACE("resize pending");
1366 auto shot = screenshot();
1367 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1368 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1369 }
1370
1371 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1372 {
1373 SCOPED_TRACE("resize applied");
1374 auto shot = screenshot();
1375 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1376 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1377 }
1378}
1379
1380TEST_F(LayerTransactionTest, SetCropWithNextResize) {
1381 sp<SurfaceControl> layer;
1382 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1383 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1384
1385 // request setCrop to be applied with the next resize
1386 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1387 {
1388 SCOPED_TRACE("waiting for next resize");
1389 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1390 }
1391
1392 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1393 {
1394 SCOPED_TRACE("pending crop modified");
1395 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1396 }
1397
1398 Transaction().setSize(layer, 16, 16).apply();
1399 {
1400 SCOPED_TRACE("resize pending");
1401 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1402 }
1403
1404 // finally resize
1405 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1406 {
1407 SCOPED_TRACE("new crop applied");
1408 auto shot = screenshot();
1409 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1410 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1411 }
1412}
1413
1414TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow) {
1415 sp<SurfaceControl> layer;
1416 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1417 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1418
1419 // setCrop is not immediate even with SCALE_TO_WINDOW override
1420 Transaction()
1421 .setCrop(layer, Rect(4, 4, 12, 12))
1422 .setSize(layer, 16, 16)
1423 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1424 .setGeometryAppliesWithResize(layer)
1425 .apply();
1426 {
1427 SCOPED_TRACE("new crop pending");
1428 auto shot = screenshot();
1429 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1430 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1431 }
1432
1433 // XXX crop is never latched without other geometry change (b/69315677)
1434 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1435 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1436 Transaction().setPosition(layer, 0, 0).apply();
1437 {
1438 SCOPED_TRACE("new crop applied");
1439 auto shot = screenshot();
1440 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1441 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1442 }
1443}
1444
Chia-I Wucdd71a52017-11-02 08:30:27 -07001445TEST_F(LayerTransactionTest, SetFinalCropBasic) {
1446 sp<SurfaceControl> layer;
1447 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1448 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1449 const Rect crop(8, 8, 24, 24);
1450
1451 // same as in SetCropBasic
1452 Transaction().setFinalCrop(layer, crop).apply();
1453 auto shot = screenshot();
1454 shot->expectColor(crop, Color::RED);
1455 shot->expectBorder(crop, Color::BLACK);
1456}
1457
1458TEST_F(LayerTransactionTest, SetFinalCropEmpty) {
1459 sp<SurfaceControl> layer;
1460 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1461 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1462
1463 // same as in SetCropEmpty
1464 {
1465 SCOPED_TRACE("empty rect");
1466 Transaction().setFinalCrop(layer, Rect(8, 8, 8, 8)).apply();
1467 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1468 }
1469
1470 {
1471 SCOPED_TRACE("negative rect");
1472 Transaction().setFinalCrop(layer, Rect(8, 8, 0, 0)).apply();
1473 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1474 }
1475}
1476
1477TEST_F(LayerTransactionTest, SetFinalCropOutOfBounds) {
1478 sp<SurfaceControl> layer;
1479 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1480 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1481
1482 // same as in SetCropOutOfBounds
1483 Transaction().setFinalCrop(layer, Rect(-128, -64, 128, 64)).apply();
1484 auto shot = screenshot();
1485 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1486 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1487}
1488
1489TEST_F(LayerTransactionTest, SetFinalCropWithTranslation) {
1490 sp<SurfaceControl> layer;
1491 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1492 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1493
1494 // final crop is applied post-translation
1495 Transaction().setPosition(layer, 16, 16).setFinalCrop(layer, Rect(8, 8, 24, 24)).apply();
1496 auto shot = screenshot();
1497 shot->expectColor(Rect(16, 16, 24, 24), Color::RED);
1498 shot->expectBorder(Rect(16, 16, 24, 24), Color::BLACK);
1499}
1500
1501TEST_F(LayerTransactionTest, SetFinalCropWithScale) {
1502 sp<SurfaceControl> layer;
1503 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1504 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1505
1506 // final crop is not affected by matrix
1507 Transaction()
1508 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1509 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1510 .apply();
1511 auto shot = screenshot();
1512 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1513 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1514}
1515
1516TEST_F(LayerTransactionTest, SetFinalCropWithResize) {
1517 sp<SurfaceControl> layer;
1518 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1519 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1520
1521 // same as in SetCropWithResize
1522 Transaction().setFinalCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1523 {
1524 SCOPED_TRACE("resize pending");
1525 auto shot = screenshot();
1526 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1527 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1528 }
1529
1530 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1531 {
1532 SCOPED_TRACE("resize applied");
1533 auto shot = screenshot();
1534 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1535 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1536 }
1537}
1538
1539TEST_F(LayerTransactionTest, SetFinalCropWithNextResize) {
1540 sp<SurfaceControl> layer;
1541 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1542 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1543
1544 // same as in SetCropWithNextResize
1545 Transaction()
1546 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1547 .setGeometryAppliesWithResize(layer)
1548 .apply();
1549 {
1550 SCOPED_TRACE("waiting for next resize");
1551 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1552 }
1553
1554 Transaction().setFinalCrop(layer, Rect(4, 4, 12, 12)).apply();
1555 {
1556 SCOPED_TRACE("pending final crop modified");
1557 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1558 }
1559
1560 Transaction().setSize(layer, 16, 16).apply();
1561 {
1562 SCOPED_TRACE("resize pending");
1563 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1564 }
1565
1566 // finally resize
1567 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1568 {
1569 SCOPED_TRACE("new final crop applied");
1570 auto shot = screenshot();
1571 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1572 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1573 }
1574}
1575
1576TEST_F(LayerTransactionTest, SetFinalCropWithNextResizeScaleToWindow) {
1577 sp<SurfaceControl> layer;
1578 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1579 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1580
1581 // same as in SetCropWithNextResizeScaleToWindow
1582 Transaction()
1583 .setFinalCrop(layer, Rect(4, 4, 12, 12))
1584 .setSize(layer, 16, 16)
1585 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1586 .setGeometryAppliesWithResize(layer)
1587 .apply();
1588 {
1589 SCOPED_TRACE("new final crop pending");
1590 auto shot = screenshot();
1591 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1592 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1593 }
1594
1595 // XXX final crop is never latched without other geometry change (b/69315677)
1596 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1597 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1598 Transaction().setPosition(layer, 0, 0).apply();
1599 {
1600 SCOPED_TRACE("new final crop applied");
1601 auto shot = screenshot();
1602 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1603 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1604 }
1605}
1606
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001607class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001608protected:
1609 virtual void SetUp() {
1610 mComposerClient = new SurfaceComposerClient;
1611 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1612
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001613 sp<IBinder> display(
1614 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001615 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001616 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001617
1618 ssize_t displayWidth = info.w;
1619 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001620
1621 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001622 mBGSurfaceControl =
1623 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1624 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001625 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001626 ASSERT_TRUE(mBGSurfaceControl->isValid());
1627 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1628
1629 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001630 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1631 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001632 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001633 ASSERT_TRUE(mFGSurfaceControl->isValid());
1634
1635 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1636
1637 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001638 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1639 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001640 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001641 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1642
1643 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1644
Robert Carr4cdc58f2017-08-23 14:22:20 -07001645 asTransaction([&](Transaction& t) {
1646 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001647
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001648 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001649
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001650 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1651 .setPosition(mFGSurfaceControl, 64, 64)
1652 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001653
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001654 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1655 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1656 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001657 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001658 }
1659
1660 virtual void TearDown() {
1661 mComposerClient->dispose();
1662 mBGSurfaceControl = 0;
1663 mFGSurfaceControl = 0;
1664 mSyncSurfaceControl = 0;
1665 mComposerClient = 0;
1666 }
1667
1668 void waitForPostedBuffers() {
1669 // Since the sync surface is in synchronous mode (i.e. double buffered)
1670 // posting three buffers to it should ensure that at least two
1671 // SurfaceFlinger::handlePageFlip calls have been made, which should
1672 // guaranteed that a buffer posted to another Surface has been retired.
1673 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1674 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1675 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1676 }
1677
Robert Carr4cdc58f2017-08-23 14:22:20 -07001678 void asTransaction(const std::function<void(Transaction&)>& exec) {
1679 Transaction t;
1680 exec(t);
1681 t.apply(true);
1682 }
1683
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001684 sp<SurfaceComposerClient> mComposerClient;
1685 sp<SurfaceControl> mBGSurfaceControl;
1686 sp<SurfaceControl> mFGSurfaceControl;
1687
1688 // This surface is used to ensure that the buffers posted to
1689 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1690 sp<SurfaceControl> mSyncSurfaceControl;
1691};
1692
Robert Carr7f619b22017-11-06 12:56:35 -08001693TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1694 sp<ScreenCapture> sc;
1695
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001696 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1697 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001698 fillSurfaceRGBA8(relative, 10, 10, 10);
1699 waitForPostedBuffers();
1700
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001701 Transaction{}
1702 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001703 .setPosition(relative, 64, 64)
1704 .apply();
1705
1706 {
1707 // The relative should be on top of the FG control.
1708 ScreenCapture::captureScreen(&sc);
1709 sc->checkPixel(64, 64, 10, 10, 10);
1710 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001711 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001712
1713 {
1714 // Nothing should change at this point.
1715 ScreenCapture::captureScreen(&sc);
1716 sc->checkPixel(64, 64, 10, 10, 10);
1717 }
1718
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001719 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001720
1721 {
1722 // Ensure that the relative was actually hidden, rather than
1723 // being left in the detached but visible state.
1724 ScreenCapture::captureScreen(&sc);
1725 sc->expectFGColor(64, 64);
1726 }
1727}
1728
Robert Carr8d5227b2017-03-16 15:41:03 -07001729class GeometryLatchingTest : public LayerUpdateTest {
1730protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001731 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001732 SCOPED_TRACE(trace);
1733 ScreenCapture::captureScreen(&sc);
1734 // We find the leading edge of the FG surface.
1735 sc->expectFGColor(127, 127);
1736 sc->expectBGColor(128, 128);
1737 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001738
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001739 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001740
1741 void unlockFGBuffer() {
1742 sp<Surface> s = mFGSurfaceControl->getSurface();
1743 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1744 waitForPostedBuffers();
1745 }
1746
Robert Carr8d5227b2017-03-16 15:41:03 -07001747 void completeFGResize() {
1748 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1749 waitForPostedBuffers();
1750 }
1751 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001752 asTransaction([&](Transaction& t) {
1753 t.setSize(mFGSurfaceControl, 64, 64);
1754 t.setPosition(mFGSurfaceControl, 64, 64);
1755 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1756 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1757 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001758
1759 EXPECT_INITIAL_STATE("After restoring initial state");
1760 }
1761 sp<ScreenCapture> sc;
1762};
1763
Robert Carr8d5227b2017-03-16 15:41:03 -07001764class CropLatchingTest : public GeometryLatchingTest {
1765protected:
1766 void EXPECT_CROPPED_STATE(const char* trace) {
1767 SCOPED_TRACE(trace);
1768 ScreenCapture::captureScreen(&sc);
1769 // The edge should be moved back one pixel by our crop.
1770 sc->expectFGColor(126, 126);
1771 sc->expectBGColor(127, 127);
1772 sc->expectBGColor(128, 128);
1773 }
chaviw59f5c562017-06-28 16:39:06 -07001774
1775 void EXPECT_RESIZE_STATE(const char* trace) {
1776 SCOPED_TRACE(trace);
1777 ScreenCapture::captureScreen(&sc);
1778 // The FG is now resized too 128,128 at 64,64
1779 sc->expectFGColor(64, 64);
1780 sc->expectFGColor(191, 191);
1781 sc->expectBGColor(192, 192);
1782 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001783};
1784
Robert Carr7bf247e2017-05-18 14:02:49 -07001785// In this test we ensure that setGeometryAppliesWithResize actually demands
1786// a buffer of the new size, and not just any size.
1787TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1788 EXPECT_INITIAL_STATE("before anything");
1789 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001790 asTransaction([&](Transaction& t) {
1791 t.setSize(mFGSurfaceControl, 128, 128);
1792 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1793 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001794
1795 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1796
1797 restoreInitialState();
1798
1799 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1800 // initiating the resize.
1801 lockAndFillFGBuffer();
1802
Robert Carr4cdc58f2017-08-23 14:22:20 -07001803 asTransaction([&](Transaction& t) {
1804 t.setSize(mFGSurfaceControl, 128, 128);
1805 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1806 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1807 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001808
1809 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1810
1811 // We now submit our old buffer, at the old size, and ensure it doesn't
1812 // trigger geometry latching.
1813 unlockFGBuffer();
1814
1815 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1816
1817 completeFGResize();
1818
1819 EXPECT_CROPPED_STATE("after the resize finishes");
1820}
1821
Pablo Ceballos05289c22016-04-14 15:49:55 -07001822TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1823 sp<ScreenCapture> sc;
1824 {
1825 SCOPED_TRACE("before anything");
1826 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001827 sc->expectBGColor(32, 32);
1828 sc->expectFGColor(96, 96);
1829 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001830 }
1831
1832 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001833 asTransaction([&](Transaction& t) {
1834 t.setAlpha(mFGSurfaceControl, 0.75);
1835 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001836 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001837 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001838
Robert Carr4cdc58f2017-08-23 14:22:20 -07001839 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001840 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001841 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001842 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001843 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001844
1845 {
1846 SCOPED_TRACE("before any trigger");
1847 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001848 sc->expectBGColor(32, 32);
1849 sc->expectFGColor(96, 96);
1850 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001851 }
1852
1853 // should trigger the first deferred transaction, but not the second one
1854 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1855 {
1856 SCOPED_TRACE("after first trigger");
1857 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001858 sc->expectBGColor(32, 32);
1859 sc->checkPixel(96, 96, 162, 63, 96);
1860 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001861 }
1862
1863 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001864 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001865
1866 // trigger the second deferred transaction
1867 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1868 {
1869 SCOPED_TRACE("after second trigger");
1870 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001871 sc->expectBGColor(32, 32);
1872 sc->expectBGColor(96, 96);
1873 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001874 }
1875}
1876
Robert Carre392b552017-09-19 12:16:05 -07001877TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1878 sp<ScreenCapture> sc;
1879
1880 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001881 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1882 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1883 sp<SurfaceControl> childBuffer =
1884 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1885 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001886 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1887
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001888 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001889
1890 {
1891 ScreenCapture::captureScreen(&sc);
1892 sc->expectChildColor(73, 73);
1893 sc->expectFGColor(74, 74);
1894 }
1895
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001896 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001897
1898 {
1899 ScreenCapture::captureScreen(&sc);
1900 sc->expectChildColor(73, 73);
1901 sc->expectChildColor(74, 74);
1902 }
1903}
1904
Robert Carr2c5f6d22017-09-26 12:30:35 -07001905TEST_F(LayerUpdateTest, MergingTransactions) {
1906 sp<ScreenCapture> sc;
1907 {
1908 SCOPED_TRACE("before move");
1909 ScreenCapture::captureScreen(&sc);
1910 sc->expectBGColor(0, 12);
1911 sc->expectFGColor(75, 75);
1912 sc->expectBGColor(145, 145);
1913 }
1914
1915 Transaction t1, t2;
1916 t1.setPosition(mFGSurfaceControl, 128, 128);
1917 t2.setPosition(mFGSurfaceControl, 0, 0);
1918 // We expect that the position update from t2 now
1919 // overwrites the position update from t1.
1920 t1.merge(std::move(t2));
1921 t1.apply();
1922
1923 {
1924 ScreenCapture::captureScreen(&sc);
1925 sc->expectFGColor(1, 1);
1926 }
1927}
1928
Robert Carr1f0a16a2016-10-24 16:27:39 -07001929class ChildLayerTest : public LayerUpdateTest {
1930protected:
1931 void SetUp() override {
1932 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001933 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1934 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001935 fillSurfaceRGBA8(mChild, 200, 200, 200);
1936
1937 {
1938 SCOPED_TRACE("before anything");
1939 ScreenCapture::captureScreen(&mCapture);
1940 mCapture->expectChildColor(64, 64);
1941 }
1942 }
1943 void TearDown() override {
1944 LayerUpdateTest::TearDown();
1945 mChild = 0;
1946 }
1947
1948 sp<SurfaceControl> mChild;
1949 sp<ScreenCapture> mCapture;
1950};
1951
1952TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001953 asTransaction([&](Transaction& t) {
1954 t.show(mChild);
1955 t.setPosition(mChild, 10, 10);
1956 t.setPosition(mFGSurfaceControl, 64, 64);
1957 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001958
1959 {
1960 ScreenCapture::captureScreen(&mCapture);
1961 // Top left of foreground must now be visible
1962 mCapture->expectFGColor(64, 64);
1963 // But 10 pixels in we should see the child surface
1964 mCapture->expectChildColor(74, 74);
1965 // And 10 more pixels we should be back to the foreground surface
1966 mCapture->expectFGColor(84, 84);
1967 }
1968
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001969 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001970
1971 {
1972 ScreenCapture::captureScreen(&mCapture);
1973 // Top left of foreground should now be at 0, 0
1974 mCapture->expectFGColor(0, 0);
1975 // But 10 pixels in we should see the child surface
1976 mCapture->expectChildColor(10, 10);
1977 // And 10 more pixels we should be back to the foreground surface
1978 mCapture->expectFGColor(20, 20);
1979 }
1980}
1981
Robert Carr41b08b52017-06-01 16:11:34 -07001982TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001983 asTransaction([&](Transaction& t) {
1984 t.show(mChild);
1985 t.setPosition(mChild, 0, 0);
1986 t.setPosition(mFGSurfaceControl, 0, 0);
1987 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1988 });
Robert Carr41b08b52017-06-01 16:11:34 -07001989
1990 {
1991 ScreenCapture::captureScreen(&mCapture);
1992 mCapture->expectChildColor(0, 0);
1993 mCapture->expectChildColor(4, 4);
1994 mCapture->expectBGColor(5, 5);
1995 }
1996}
1997
1998TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001999 asTransaction([&](Transaction& t) {
2000 t.show(mChild);
2001 t.setPosition(mChild, 0, 0);
2002 t.setPosition(mFGSurfaceControl, 0, 0);
2003 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
2004 });
Robert Carr41b08b52017-06-01 16:11:34 -07002005
2006 {
2007 ScreenCapture::captureScreen(&mCapture);
2008 mCapture->expectChildColor(0, 0);
2009 mCapture->expectChildColor(4, 4);
2010 mCapture->expectBGColor(5, 5);
2011 }
2012}
2013
Robert Carr1f0a16a2016-10-24 16:27:39 -07002014TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002015 asTransaction([&](Transaction& t) {
2016 t.show(mChild);
2017 t.setPosition(mFGSurfaceControl, 0, 0);
2018 t.setPosition(mChild, 63, 63);
2019 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002020
2021 {
2022 ScreenCapture::captureScreen(&mCapture);
2023 mCapture->expectFGColor(0, 0);
2024 // Last pixel in foreground should now be the child.
2025 mCapture->expectChildColor(63, 63);
2026 // But the child should be constrained and the next pixel
2027 // must be the background
2028 mCapture->expectBGColor(64, 64);
2029 }
2030}
2031
2032TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002033 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002034
2035 // Find the boundary between the parent and child
2036 {
2037 ScreenCapture::captureScreen(&mCapture);
2038 mCapture->expectChildColor(9, 9);
2039 mCapture->expectFGColor(10, 10);
2040 }
2041
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002042 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002043
2044 // The boundary should be twice as far from the origin now.
2045 // The pixels from the last test should all be child now
2046 {
2047 ScreenCapture::captureScreen(&mCapture);
2048 mCapture->expectChildColor(9, 9);
2049 mCapture->expectChildColor(10, 10);
2050 mCapture->expectChildColor(19, 19);
2051 mCapture->expectFGColor(20, 20);
2052 }
2053}
Robert Carr9524cb32017-02-13 11:32:32 -08002054
Robert Carr6452f122017-03-21 10:41:29 -07002055TEST_F(ChildLayerTest, ChildLayerAlpha) {
2056 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2057 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2058 fillSurfaceRGBA8(mChild, 0, 254, 0);
2059 waitForPostedBuffers();
2060
Robert Carr4cdc58f2017-08-23 14:22:20 -07002061 asTransaction([&](Transaction& t) {
2062 t.show(mChild);
2063 t.setPosition(mChild, 0, 0);
2064 t.setPosition(mFGSurfaceControl, 0, 0);
2065 });
Robert Carr6452f122017-03-21 10:41:29 -07002066
2067 {
2068 ScreenCapture::captureScreen(&mCapture);
2069 // Unblended child color
2070 mCapture->checkPixel(0, 0, 0, 254, 0);
2071 }
2072
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002073 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002074
2075 {
2076 ScreenCapture::captureScreen(&mCapture);
2077 // Child and BG blended.
2078 mCapture->checkPixel(0, 0, 127, 127, 0);
2079 }
2080
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002081 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002082
2083 {
2084 ScreenCapture::captureScreen(&mCapture);
2085 // Child and BG blended.
2086 mCapture->checkPixel(0, 0, 95, 64, 95);
2087 }
2088}
2089
Robert Carr9524cb32017-02-13 11:32:32 -08002090TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002091 asTransaction([&](Transaction& t) {
2092 t.show(mChild);
2093 t.setPosition(mChild, 10, 10);
2094 t.setPosition(mFGSurfaceControl, 64, 64);
2095 });
Robert Carr9524cb32017-02-13 11:32:32 -08002096
2097 {
2098 ScreenCapture::captureScreen(&mCapture);
2099 // Top left of foreground must now be visible
2100 mCapture->expectFGColor(64, 64);
2101 // But 10 pixels in we should see the child surface
2102 mCapture->expectChildColor(74, 74);
2103 // And 10 more pixels we should be back to the foreground surface
2104 mCapture->expectFGColor(84, 84);
2105 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002106
2107 asTransaction([&](Transaction& t) {
2108 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2109 });
2110
Robert Carr9524cb32017-02-13 11:32:32 -08002111 {
2112 ScreenCapture::captureScreen(&mCapture);
2113 mCapture->expectFGColor(64, 64);
2114 // In reparenting we should have exposed the entire foreground surface.
2115 mCapture->expectFGColor(74, 74);
2116 // And the child layer should now begin at 10, 10 (since the BG
2117 // layer is at (0, 0)).
2118 mCapture->expectBGColor(9, 9);
2119 mCapture->expectChildColor(10, 10);
2120 }
2121}
2122
chaviw161410b02017-07-27 10:46:08 -07002123TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002124 asTransaction([&](Transaction& t) {
2125 t.show(mChild);
2126 t.setPosition(mChild, 10, 10);
2127 t.setPosition(mFGSurfaceControl, 64, 64);
2128 });
Robert Carr9524cb32017-02-13 11:32:32 -08002129
2130 {
2131 ScreenCapture::captureScreen(&mCapture);
2132 // Top left of foreground must now be visible
2133 mCapture->expectFGColor(64, 64);
2134 // But 10 pixels in we should see the child surface
2135 mCapture->expectChildColor(74, 74);
2136 // And 10 more pixels we should be back to the foreground surface
2137 mCapture->expectFGColor(84, 84);
2138 }
2139
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002140 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002141
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002142 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002143
chaviw161410b02017-07-27 10:46:08 -07002144 // Since the child has the same client as the parent, it will not get
2145 // detached and will be hidden.
2146 {
2147 ScreenCapture::captureScreen(&mCapture);
2148 mCapture->expectFGColor(64, 64);
2149 mCapture->expectFGColor(74, 74);
2150 mCapture->expectFGColor(84, 84);
2151 }
2152}
2153
2154TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2155 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002156 sp<SurfaceControl> mChildNewClient =
2157 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2158 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002159
Peiyong Lin566a3b42018-01-09 18:22:43 -08002160 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07002161 ASSERT_TRUE(mChildNewClient->isValid());
2162
2163 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2164
Robert Carr4cdc58f2017-08-23 14:22:20 -07002165 asTransaction([&](Transaction& t) {
2166 t.hide(mChild);
2167 t.show(mChildNewClient);
2168 t.setPosition(mChildNewClient, 10, 10);
2169 t.setPosition(mFGSurfaceControl, 64, 64);
2170 });
chaviw161410b02017-07-27 10:46:08 -07002171
2172 {
2173 ScreenCapture::captureScreen(&mCapture);
2174 // Top left of foreground must now be visible
2175 mCapture->expectFGColor(64, 64);
2176 // But 10 pixels in we should see the child surface
2177 mCapture->expectChildColor(74, 74);
2178 // And 10 more pixels we should be back to the foreground surface
2179 mCapture->expectFGColor(84, 84);
2180 }
2181
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002182 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002183
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002184 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002185
Robert Carr9524cb32017-02-13 11:32:32 -08002186 // Nothing should have changed.
2187 {
2188 ScreenCapture::captureScreen(&mCapture);
2189 mCapture->expectFGColor(64, 64);
2190 mCapture->expectChildColor(74, 74);
2191 mCapture->expectFGColor(84, 84);
2192 }
2193}
2194
Robert Carr9b429f42017-04-17 14:56:57 -07002195TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002196 asTransaction([&](Transaction& t) {
2197 t.show(mChild);
2198 t.setPosition(mChild, 0, 0);
2199 t.setPosition(mFGSurfaceControl, 0, 0);
2200 });
Robert Carr9b429f42017-04-17 14:56:57 -07002201
2202 {
2203 ScreenCapture::captureScreen(&mCapture);
2204 // We've positioned the child in the top left.
2205 mCapture->expectChildColor(0, 0);
2206 // But it's only 10x10.
2207 mCapture->expectFGColor(10, 10);
2208 }
2209
Robert Carr4cdc58f2017-08-23 14:22:20 -07002210 asTransaction([&](Transaction& t) {
2211 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2212 // We cause scaling by 2.
2213 t.setSize(mFGSurfaceControl, 128, 128);
2214 });
Robert Carr9b429f42017-04-17 14:56:57 -07002215
2216 {
2217 ScreenCapture::captureScreen(&mCapture);
2218 // We've positioned the child in the top left.
2219 mCapture->expectChildColor(0, 0);
2220 mCapture->expectChildColor(10, 10);
2221 mCapture->expectChildColor(19, 19);
2222 // And now it should be scaled all the way to 20x20
2223 mCapture->expectFGColor(20, 20);
2224 }
2225}
2226
Robert Carr1725eee2017-04-26 18:32:15 -07002227// Regression test for b/37673612
2228TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002229 asTransaction([&](Transaction& t) {
2230 t.show(mChild);
2231 t.setPosition(mChild, 0, 0);
2232 t.setPosition(mFGSurfaceControl, 0, 0);
2233 });
Robert Carr1725eee2017-04-26 18:32:15 -07002234
2235 {
2236 ScreenCapture::captureScreen(&mCapture);
2237 // We've positioned the child in the top left.
2238 mCapture->expectChildColor(0, 0);
2239 // But it's only 10x10.
2240 mCapture->expectFGColor(10, 10);
2241 }
Robert Carr1725eee2017-04-26 18:32:15 -07002242 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2243 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002244 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002245 sp<Surface> s = mFGSurfaceControl->getSurface();
2246 auto anw = static_cast<ANativeWindow*>(s.get());
2247 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2248 native_window_set_buffers_dimensions(anw, 64, 128);
2249 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2250 waitForPostedBuffers();
2251
2252 {
2253 // The child should still be in the same place and not have any strange scaling as in
2254 // b/37673612.
2255 ScreenCapture::captureScreen(&mCapture);
2256 mCapture->expectChildColor(0, 0);
2257 mCapture->expectFGColor(10, 10);
2258 }
2259}
2260
Dan Stoza412903f2017-04-27 13:42:17 -07002261TEST_F(ChildLayerTest, Bug36858924) {
2262 // Destroy the child layer
2263 mChild.clear();
2264
2265 // Now recreate it as hidden
2266 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2267 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2268 mFGSurfaceControl.get());
2269
2270 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002271 asTransaction([&](Transaction& t) {
2272 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002273 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002274 t.show(mChild);
2275 });
Dan Stoza412903f2017-04-27 13:42:17 -07002276
2277 // Render the foreground surface a few times
2278 //
2279 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2280 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2281 // never acquire/release the first buffer
2282 ALOGI("Filling 1");
2283 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2284 ALOGI("Filling 2");
2285 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2286 ALOGI("Filling 3");
2287 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2288 ALOGI("Filling 4");
2289 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2290}
2291
chaviwf1961f72017-09-18 16:41:07 -07002292TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002293 asTransaction([&](Transaction& t) {
2294 t.show(mChild);
2295 t.setPosition(mChild, 10, 10);
2296 t.setPosition(mFGSurfaceControl, 64, 64);
2297 });
chaviw06178942017-07-27 10:25:59 -07002298
2299 {
2300 ScreenCapture::captureScreen(&mCapture);
2301 // Top left of foreground must now be visible
2302 mCapture->expectFGColor(64, 64);
2303 // But 10 pixels in we should see the child surface
2304 mCapture->expectChildColor(74, 74);
2305 // And 10 more pixels we should be back to the foreground surface
2306 mCapture->expectFGColor(84, 84);
2307 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002308
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002309 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002310
chaviw06178942017-07-27 10:25:59 -07002311 {
2312 ScreenCapture::captureScreen(&mCapture);
2313 mCapture->expectFGColor(64, 64);
2314 // In reparenting we should have exposed the entire foreground surface.
2315 mCapture->expectFGColor(74, 74);
2316 // And the child layer should now begin at 10, 10 (since the BG
2317 // layer is at (0, 0)).
2318 mCapture->expectBGColor(9, 9);
2319 mCapture->expectChildColor(10, 10);
2320 }
2321}
2322
chaviwf1961f72017-09-18 16:41:07 -07002323TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002324 asTransaction([&](Transaction& t) {
2325 t.show(mChild);
2326 t.setPosition(mChild, 10, 10);
2327 t.setPosition(mFGSurfaceControl, 64, 64);
2328 });
chaviwf1961f72017-09-18 16:41:07 -07002329
2330 {
2331 ScreenCapture::captureScreen(&mCapture);
2332 // Top left of foreground must now be visible
2333 mCapture->expectFGColor(64, 64);
2334 // But 10 pixels in we should see the child surface
2335 mCapture->expectChildColor(74, 74);
2336 // And 10 more pixels we should be back to the foreground surface
2337 mCapture->expectFGColor(84, 84);
2338 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002339 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002340 {
2341 ScreenCapture::captureScreen(&mCapture);
2342 // Nothing should have changed.
2343 mCapture->expectFGColor(64, 64);
2344 mCapture->expectChildColor(74, 74);
2345 mCapture->expectFGColor(84, 84);
2346 }
2347}
2348
2349TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002350 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2351 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002352 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07002353 ASSERT_TRUE(newSurface->isValid());
2354
2355 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002356 asTransaction([&](Transaction& t) {
2357 t.hide(mChild);
2358 t.show(newSurface);
2359 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002360 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002361 t.setPosition(mFGSurfaceControl, 64, 64);
2362 });
chaviwf1961f72017-09-18 16:41:07 -07002363
2364 {
2365 ScreenCapture::captureScreen(&mCapture);
2366 // Top left of foreground must now be visible
2367 mCapture->expectFGColor(64, 64);
2368 // At 10, 10 we should see the new surface
2369 mCapture->checkPixel(10, 10, 63, 195, 63);
2370 }
2371
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002372 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002373
2374 {
2375 ScreenCapture::captureScreen(&mCapture);
2376 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2377 // mFGSurface, putting it at 74, 74.
2378 mCapture->expectFGColor(64, 64);
2379 mCapture->checkPixel(74, 74, 63, 195, 63);
2380 mCapture->expectFGColor(84, 84);
2381 }
2382}
2383
chaviwc9674332017-08-28 12:32:18 -07002384TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002385 sp<SurfaceControl> grandchild =
2386 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2387 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002388 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2389
2390 {
2391 ScreenCapture::captureScreen(&mCapture);
2392 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2393 // which begins at 64, 64
2394 mCapture->checkPixel(64, 64, 50, 50, 50);
2395 }
2396}
2397
Robert Carr503c7042017-09-27 15:06:08 -07002398TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002399 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2400 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002401 fillSurfaceRGBA8(relative, 255, 255, 255);
2402
2403 Transaction t;
2404 t.setLayer(relative, INT32_MAX)
2405 .setRelativeLayer(mChild, relative->getHandle(), 1)
2406 .setPosition(mFGSurfaceControl, 0, 0)
2407 .apply(true);
2408
2409 // We expect that the child should have been elevated above our
2410 // INT_MAX layer even though it's not a child of it.
2411 {
2412 ScreenCapture::captureScreen(&mCapture);
2413 mCapture->expectChildColor(0, 0);
2414 mCapture->expectChildColor(9, 9);
2415 mCapture->checkPixel(10, 10, 255, 255, 255);
2416 }
2417}
2418
chaviwa76b2712017-09-20 12:02:26 -07002419class ScreenCaptureTest : public LayerUpdateTest {
2420protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002421 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07002422};
2423
2424TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2425 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002426 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002427 mCapture->expectBGColor(0, 0);
2428 // Doesn't capture FG layer which is at 64, 64
2429 mCapture->expectBGColor(64, 64);
2430}
2431
2432TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2433 auto fgHandle = mFGSurfaceControl->getHandle();
2434
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002435 sp<SurfaceControl> child =
2436 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2437 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002438 fillSurfaceRGBA8(child, 200, 200, 200);
2439
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002440 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002441
2442 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002443 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002444 mCapture->expectFGColor(10, 10);
2445 mCapture->expectChildColor(0, 0);
2446}
2447
Robert Carr578038f2018-03-09 12:25:24 -08002448TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
2449 auto fgHandle = mFGSurfaceControl->getHandle();
2450
2451 sp<SurfaceControl> child =
2452 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2453 0, mFGSurfaceControl.get());
2454 fillSurfaceRGBA8(child, 200, 200, 200);
2455
2456 SurfaceComposerClient::Transaction().show(child).apply(true);
2457
2458 // Captures mFGSurfaceControl's child
2459 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2460 mCapture->checkPixel(10, 10, 0, 0, 0);
2461 mCapture->expectChildColor(0, 0);
2462}
2463
chaviw50da5042018-04-09 13:49:37 -07002464TEST_F(ScreenCaptureTest, CaptureTransparent) {
2465 sp<SurfaceControl> child =
2466 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2467 0, mFGSurfaceControl.get());
2468
2469 fillSurfaceRGBA8(child, 200, 200, 200);
2470
2471 SurfaceComposerClient::Transaction().show(child).apply(true);
2472
2473 auto childHandle = child->getHandle();
2474
2475 // Captures child
2476 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
2477 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
2478 // Area outside of child's bounds is transparent.
2479 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
2480}
2481
chaviw4b129c22018-04-09 16:19:43 -07002482TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
2483 auto fgHandle = mFGSurfaceControl->getHandle();
2484
2485 sp<SurfaceControl> child =
2486 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2487 0, mFGSurfaceControl.get());
2488 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 10,
2489 10, PIXEL_FORMAT_RGBA_8888, 0);
2490 fillSurfaceRGBA8(child, 200, 200, 200);
2491 fillSurfaceRGBA8(relative, 100, 100, 100);
2492
2493 SurfaceComposerClient::Transaction()
2494 .show(child)
2495 // Set relative layer above fg layer so should be shown above when computing all layers.
2496 .setRelativeLayer(relative, fgHandle, 1)
2497 .show(relative)
2498 .apply(true);
2499
2500 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
2501 ScreenCapture::captureLayers(&mCapture, fgHandle);
2502 mCapture->expectFGColor(10, 10);
2503 mCapture->expectChildColor(0, 0);
2504}
2505
2506TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
2507 auto fgHandle = mFGSurfaceControl->getHandle();
2508
2509 sp<SurfaceControl> child =
2510 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2511 0, mFGSurfaceControl.get());
2512 sp<SurfaceControl> relative =
2513 mComposerClient->createSurface(String8("Relative surface"), 10, 10,
2514 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2515 fillSurfaceRGBA8(child, 200, 200, 200);
2516 fillSurfaceRGBA8(relative, 100, 100, 100);
2517
2518 SurfaceComposerClient::Transaction()
2519 .show(child)
2520 // Set relative layer below fg layer but relative to child layer so it should be shown
2521 // above child layer.
2522 .setLayer(relative, -1)
2523 .setRelativeLayer(relative, child->getHandle(), 1)
2524 .show(relative)
2525 .apply(true);
2526
2527 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
2528 // relative value should be taken into account, placing it above child layer.
2529 ScreenCapture::captureLayers(&mCapture, fgHandle);
2530 mCapture->expectFGColor(10, 10);
2531 // Relative layer is showing on top of child layer
2532 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
2533}
Robert Carr578038f2018-03-09 12:25:24 -08002534
2535// In the following tests we verify successful skipping of a parent layer,
2536// so we use the same verification logic and only change how we mutate
2537// the parent layer to verify that various properties are ignored.
2538class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
2539public:
2540 void SetUp() override {
2541 LayerUpdateTest::SetUp();
2542
2543 mChild =
2544 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2545 0, mFGSurfaceControl.get());
2546 fillSurfaceRGBA8(mChild, 200, 200, 200);
2547
2548 SurfaceComposerClient::Transaction().show(mChild).apply(true);
2549 }
2550
2551 void verify() {
2552 auto fgHandle = mFGSurfaceControl->getHandle();
2553 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2554 mCapture->checkPixel(10, 10, 0, 0, 0);
2555 mCapture->expectChildColor(0, 0);
2556 }
2557
2558 std::unique_ptr<ScreenCapture> mCapture;
2559 sp<SurfaceControl> mChild;
2560};
2561
2562TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
2563
2564 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2565
2566 // Even though the parent is hidden we should still capture the child.
2567 verify();
2568}
2569
2570TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
2571
2572 SurfaceComposerClient::Transaction().setCrop(mFGSurfaceControl, Rect(0, 0, 1, 1)).apply(true);
2573
2574 // Even though the parent is cropped out we should still capture the child.
2575 verify();
2576}
2577
2578TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
2579
2580 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
2581
2582 // We should not inherit the parent scaling.
2583 verify();
2584}
2585
Robert Carr15eae092018-03-23 13:43:53 -07002586TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
2587 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2588
2589 // Even though the parent is hidden we should still capture the child.
2590 verify();
2591
2592 // Verify everything was properly hidden when rendering the full-screen.
2593 screenshot()->expectBGColor(0,0);
2594}
2595
2596
chaviwa76b2712017-09-20 12:02:26 -07002597TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2598 auto fgHandle = mFGSurfaceControl->getHandle();
2599
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002600 sp<SurfaceControl> child =
2601 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2602 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002603 fillSurfaceRGBA8(child, 200, 200, 200);
2604
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002605 sp<SurfaceControl> grandchild =
2606 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2607 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002608
2609 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2610 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002611 .show(child)
2612 .setPosition(grandchild, 5, 5)
2613 .show(grandchild)
2614 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002615
2616 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002617 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002618 mCapture->expectFGColor(10, 10);
2619 mCapture->expectChildColor(0, 0);
2620 mCapture->checkPixel(5, 5, 50, 50, 50);
2621}
2622
2623TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002624 sp<SurfaceControl> child =
2625 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2626 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002627 fillSurfaceRGBA8(child, 200, 200, 200);
2628 auto childHandle = child->getHandle();
2629
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002630 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002631
2632 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002633 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07002634 mCapture->expectChildColor(0, 0);
2635 mCapture->expectChildColor(9, 9);
2636}
2637
2638TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002639 sp<SurfaceControl> child =
2640 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2641 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002642 fillSurfaceRGBA8(child, 200, 200, 200);
2643 auto childHandle = child->getHandle();
2644
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002645 sp<SurfaceControl> grandchild =
2646 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2647 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002648 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2649
2650 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002651 .show(child)
2652 .setPosition(grandchild, 5, 5)
2653 .show(grandchild)
2654 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002655
2656 auto grandchildHandle = grandchild->getHandle();
2657
2658 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002659 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07002660 mCapture->checkPixel(0, 0, 50, 50, 50);
2661 mCapture->checkPixel(4, 4, 50, 50, 50);
2662}
2663
chaviw7206d492017-11-10 16:16:12 -08002664TEST_F(ScreenCaptureTest, CaptureCrop) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002665 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2666 PIXEL_FORMAT_RGBA_8888, 0);
2667 sp<SurfaceControl> blueLayer =
2668 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2669 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002670
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002671 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2672 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002673
2674 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002675 .setLayer(redLayer, INT32_MAX - 1)
2676 .show(redLayer)
2677 .show(blueLayer)
2678 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002679
2680 auto redLayerHandle = redLayer->getHandle();
2681
2682 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002683 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2684 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2685 // red area below the blue area
2686 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2687 // red area to the right of the blue area
2688 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002689
2690 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002691 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08002692 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
2693 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002694 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08002695 mCapture->checkPixel(30, 30, 0, 0, 0);
2696}
2697
2698TEST_F(ScreenCaptureTest, CaptureSize) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002699 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2700 PIXEL_FORMAT_RGBA_8888, 0);
2701 sp<SurfaceControl> blueLayer =
2702 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2703 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002704
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002705 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2706 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002707
2708 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002709 .setLayer(redLayer, INT32_MAX - 1)
2710 .show(redLayer)
2711 .show(blueLayer)
2712 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002713
2714 auto redLayerHandle = redLayer->getHandle();
2715
2716 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002717 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2718 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2719 // red area below the blue area
2720 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2721 // red area to the right of the blue area
2722 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002723
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002724 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08002725 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002726 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
2727 // red area below the blue area
2728 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
2729 // red area to the right of the blue area
2730 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002731 mCapture->checkPixel(30, 30, 0, 0, 0);
2732}
2733
2734TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002735 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2736 PIXEL_FORMAT_RGBA_8888, 0);
chaviw7206d492017-11-10 16:16:12 -08002737
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002738 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
chaviw7206d492017-11-10 16:16:12 -08002739
2740 auto redLayerHandle = redLayer->getHandle();
2741 mComposerClient->destroySurface(redLayerHandle);
2742 SurfaceComposerClient::Transaction().apply(true);
2743
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002744 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08002745
2746 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002747 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2748 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08002749}
2750
chaviw8e3fe5d2018-02-22 10:55:42 -08002751
2752class DereferenceSurfaceControlTest : public LayerTransactionTest {
2753protected:
2754 void SetUp() override {
2755 LayerTransactionTest::SetUp();
2756 bgLayer = createLayer("BG layer", 20, 20);
2757 fillLayerColor(bgLayer, Color::RED);
2758 fgLayer = createLayer("FG layer", 20, 20);
2759 fillLayerColor(fgLayer, Color::BLUE);
2760 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
2761 {
2762 SCOPED_TRACE("before anything");
2763 auto shot = screenshot();
2764 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2765 }
2766 }
2767 void TearDown() override {
2768 LayerTransactionTest::TearDown();
2769 bgLayer = 0;
2770 fgLayer = 0;
2771 }
2772
2773 sp<SurfaceControl> bgLayer;
2774 sp<SurfaceControl> fgLayer;
2775};
2776
2777TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
2778 fgLayer = nullptr;
2779 {
2780 SCOPED_TRACE("after setting null");
2781 auto shot = screenshot();
2782 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
2783 }
2784}
2785
2786TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
2787 auto transaction = Transaction().show(fgLayer);
2788 fgLayer = nullptr;
2789 {
2790 SCOPED_TRACE("after setting null");
2791 auto shot = screenshot();
2792 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2793 }
2794}
2795
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002796} // namespace android