blob: 2ce32b59672a5960bacc7d165a5d13add8f1ad92 [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu718daf82017-10-20 11:57:17 -070017#include <algorithm>
18#include <functional>
19#include <limits>
20#include <ostream>
21
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070022#include <gtest/gtest.h>
23
Michael Lentine5a16a622015-05-21 13:48:24 -070024#include <android/native_window.h>
25
Mathias Agopian90ac7992012-02-25 18:48:35 -080026#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070027#include <gui/LayerState.h>
28
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/Surface.h>
30#include <gui/SurfaceComposerClient.h>
31#include <private/gui/ComposerService.h>
32
Mathias Agopianc666cae2012-07-25 18:56:13 -070033#include <ui/DisplayInfo.h>
Chia-I Wu718daf82017-10-20 11:57:17 -070034#include <ui/Rect.h>
Chia-I Wu1078bbb2017-10-20 11:29:02 -070035#include <utils/String8.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070036
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070037#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070038#include <math/vec3.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070039
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040namespace android {
41
Chia-I Wu718daf82017-10-20 11:57:17 -070042namespace {
43
44struct Color {
45 uint8_t r;
46 uint8_t g;
47 uint8_t b;
48 uint8_t a;
49
50 static const Color RED;
Chia-I Wu0ea0f822017-10-31 10:14:40 -070051 static const Color GREEN;
Chia-I Wu49313302017-10-31 10:14:40 -070052 static const Color BLUE;
Chia-I Wu93853fe2017-11-02 08:30:27 -070053 static const Color WHITE;
Chia-I Wu718daf82017-10-20 11:57:17 -070054 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070055 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070056};
57
58const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070059const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070060const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu93853fe2017-11-02 08:30:27 -070061const Color Color::WHITE{255, 255, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070062const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070063const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070064
65std::ostream& operator<<(std::ostream& os, const Color& color) {
66 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
67 return os;
68}
69
70// Fill a region with the specified color.
71void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) {
72 int32_t x = rect.left;
73 int32_t y = rect.top;
74 int32_t width = rect.right - rect.left;
75 int32_t height = rect.bottom - rect.top;
76
77 if (x < 0) {
78 width += x;
79 x = 0;
80 }
81 if (y < 0) {
82 height += y;
83 y = 0;
84 }
85 if (x + width > buffer.width) {
86 x = std::min(x, buffer.width);
87 width = buffer.width - x;
88 }
89 if (y + height > buffer.height) {
90 y = std::min(y, buffer.height);
91 height = buffer.height - y;
92 }
93
94 for (int32_t j = 0; j < height; j++) {
95 uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4;
96 for (int32_t i = 0; i < width; i++) {
97 dst[0] = color.r;
98 dst[1] = color.g;
99 dst[2] = color.b;
100 dst[3] = color.a;
101 dst += 4;
102 }
103 }
104}
105
106// Check if a region has the specified color.
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000107void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
Chia-I Wu718daf82017-10-20 11:57:17 -0700108 const Color& color, uint8_t tolerance) {
109 int32_t x = rect.left;
110 int32_t y = rect.top;
111 int32_t width = rect.right - rect.left;
112 int32_t height = rect.bottom - rect.top;
113
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000114 int32_t bufferWidth = int32_t(outBuffer->getWidth());
115 int32_t bufferHeight = int32_t(outBuffer->getHeight());
116 if (x + width > bufferWidth) {
117 x = std::min(x, bufferWidth);
118 width = bufferWidth - x;
Chia-I Wu718daf82017-10-20 11:57:17 -0700119 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000120 if (y + height > bufferHeight) {
121 y = std::min(y, bufferHeight);
122 height = bufferHeight - y;
Chia-I Wu718daf82017-10-20 11:57:17 -0700123 }
124
125 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
126 uint8_t tmp = a >= b ? a - b : b - a;
127 return tmp <= tolerance;
128 };
129 for (int32_t j = 0; j < height; j++) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000130 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
Chia-I Wu718daf82017-10-20 11:57:17 -0700131 for (int32_t i = 0; i < width; i++) {
132 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
133 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
134 << "pixel @ (" << x + i << ", " << y + j << "): "
135 << "expected (" << color << "), "
136 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
137 src += 4;
138 }
139 }
140}
141
142} // anonymous namespace
143
Robert Carr4cdc58f2017-08-23 14:22:20 -0700144using Transaction = SurfaceComposerClient::Transaction;
145
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700146// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700147static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
148 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800149 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700150 sp<Surface> s = sc->getSurface();
Peiyong Lin566a3b42018-01-09 18:22:43 -0800151 ASSERT_TRUE(s != nullptr);
152 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800153 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700154 for (int y = 0; y < outBuffer.height; y++) {
155 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700156 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700157 pixel[0] = r;
158 pixel[1] = g;
159 pixel[2] = b;
160 pixel[3] = 255;
161 }
162 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700163 if (unlock) {
164 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
165 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700166}
167
168// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
169// individual pixel values for testing purposes.
170class ScreenCapture : public RefBase {
171public:
Chia-I Wu718daf82017-10-20 11:57:17 -0700172 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
173 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700174 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700175 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700176 SurfaceComposerClient::Transaction().apply(true);
177
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000178 sp<GraphicBuffer> outBuffer;
Chia-I Wu718daf82017-10-20 11:57:17 -0700179 ASSERT_EQ(NO_ERROR,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000180 sf->captureScreen(display, &outBuffer, Rect(), 0, 0, minLayerZ, maxLayerZ,
181 false));
182 *sc = new ScreenCapture(outBuffer);
183 }
184
185 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
186 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
187 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
188 SurfaceComposerClient::Transaction().apply(true);
189
190 sp<GraphicBuffer> outBuffer;
191 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
192 *sc = std::make_unique<ScreenCapture>(outBuffer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700193 }
194
Robert Carr578038f2018-03-09 12:25:24 -0800195 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
196 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
197 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
198 SurfaceComposerClient::Transaction().apply(true);
199
200 sp<GraphicBuffer> outBuffer;
201 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
202 *sc = std::make_unique<ScreenCapture>(outBuffer);
203 }
204
Chia-I Wu718daf82017-10-20 11:57:17 -0700205 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000206 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
207 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
Chia-I Wu718daf82017-10-20 11:57:17 -0700208 }
209
210 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000211 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
Chia-I Wu718daf82017-10-20 11:57:17 -0700212 const bool leftBorder = rect.left > 0;
213 const bool topBorder = rect.top > 0;
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000214 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
215 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
Chia-I Wu718daf82017-10-20 11:57:17 -0700216
217 if (topBorder) {
218 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
219 if (leftBorder) {
220 top.left -= 1;
221 }
222 if (rightBorder) {
223 top.right += 1;
224 }
225 expectColor(top, color, tolerance);
226 }
227 if (leftBorder) {
228 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
229 expectColor(left, color, tolerance);
230 }
231 if (rightBorder) {
232 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
233 expectColor(right, color, tolerance);
234 }
235 if (bottomBorder) {
236 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
237 if (leftBorder) {
238 bottom.left -= 1;
239 }
240 if (rightBorder) {
241 bottom.right += 1;
242 }
243 expectColor(bottom, color, tolerance);
244 }
245 }
246
Chia-I Wu93853fe2017-11-02 08:30:27 -0700247 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
248 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
249 uint8_t tolerance = 0) {
250 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
251
252 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
253 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
254 // avoid checking borders due to unspecified filtering behavior
255 const int32_t offsetX = filtered ? 2 : 0;
256 const int32_t offsetY = filtered ? 2 : 0;
257 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
258 tolerance);
259 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
260 tolerance);
261 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
262 tolerance);
263 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
264 bottomRight, tolerance);
265 }
266
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700267 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000268 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
269 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700270 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
271 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700272 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
273 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700274 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700275 }
276 }
277
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700278 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700279
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700280 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700281
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700282 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700283
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000284 ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
285 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
Michael Lentine5a16a622015-05-21 13:48:24 -0700286 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700287
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000288 ~ScreenCapture() { mOutBuffer->unlock(); }
chaviwa76b2712017-09-20 12:02:26 -0700289
290private:
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000291 sp<GraphicBuffer> mOutBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800292 uint8_t* mPixels = nullptr;
chaviwa76b2712017-09-20 12:02:26 -0700293};
294
Chia-I Wu718daf82017-10-20 11:57:17 -0700295class LayerTransactionTest : public ::testing::Test {
296protected:
297 void SetUp() override {
298 mClient = new SurfaceComposerClient;
299 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
300
301 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
302 }
303
304 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
305 uint32_t flags = 0) {
306 auto layer =
307 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
308 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
309
310 status_t error = Transaction()
311 .setLayerStack(layer, mDisplayLayerStack)
312 .setLayer(layer, mLayerZBase)
313 .apply();
314 if (error != NO_ERROR) {
315 ADD_FAILURE() << "failed to initialize SurfaceControl";
316 layer.clear();
317 }
318
319 return layer;
320 }
321
322 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
323 // wait for previous transactions (such as setSize) to complete
324 Transaction().apply(true);
325
326 ANativeWindow_Buffer buffer = {};
327 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
328
329 return buffer;
330 }
331
332 void postLayerBuffer(const sp<SurfaceControl>& layer) {
333 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
334
335 // wait for the newly posted buffer to be latched
336 waitForLayerBuffers();
337 }
338
339 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
340 ANativeWindow_Buffer buffer;
341 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
342 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
343 postLayerBuffer(layer);
344 }
345
Chia-I Wu93853fe2017-11-02 08:30:27 -0700346 void fillLayerQuadrant(const sp<SurfaceControl>& layer, const Color& topLeft,
347 const Color& topRight, const Color& bottomLeft,
348 const Color& bottomRight) {
349 ANativeWindow_Buffer buffer;
350 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
351 ASSERT_TRUE(buffer.width % 2 == 0 && buffer.height % 2 == 0);
352
353 const int32_t halfW = buffer.width / 2;
354 const int32_t halfH = buffer.height / 2;
355 fillBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
356 fillBufferColor(buffer, Rect(halfW, 0, buffer.width, halfH), topRight);
357 fillBufferColor(buffer, Rect(0, halfH, halfW, buffer.height), bottomLeft);
358 fillBufferColor(buffer, Rect(halfW, halfH, buffer.width, buffer.height), bottomRight);
359
360 postLayerBuffer(layer);
361 }
362
Chia-I Wu718daf82017-10-20 11:57:17 -0700363 sp<ScreenCapture> screenshot() {
364 sp<ScreenCapture> screenshot;
365 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
366 return screenshot;
367 }
368
369 sp<SurfaceComposerClient> mClient;
370
371 sp<IBinder> mDisplay;
372 uint32_t mDisplayWidth;
373 uint32_t mDisplayHeight;
374 uint32_t mDisplayLayerStack;
375
376 // leave room for ~256 layers
377 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
378
379private:
380 void SetUpDisplay() {
381 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
382 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
383
384 // get display width/height
385 DisplayInfo info;
386 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
387 mDisplayWidth = info.w;
388 mDisplayHeight = info.h;
389
390 // After a new buffer is queued, SurfaceFlinger is notified and will
391 // latch the new buffer on next vsync. Let's heuristically wait for 3
392 // vsyncs.
393 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
394
395 mDisplayLayerStack = 0;
396 // set layer stack (b/68888219)
397 Transaction t;
398 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
399 t.apply();
400 }
401
402 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
403
404 int32_t mBufferPostDelay;
405};
406
407TEST_F(LayerTransactionTest, SetPositionBasic) {
408 sp<SurfaceControl> layer;
409 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
410 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
411
412 {
413 SCOPED_TRACE("default position");
414 auto shot = screenshot();
415 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
416 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
417 }
418
419 Transaction().setPosition(layer, 5, 10).apply();
420 {
421 SCOPED_TRACE("new position");
422 auto shot = screenshot();
423 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
424 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
425 }
426}
427
428TEST_F(LayerTransactionTest, SetPositionRounding) {
429 sp<SurfaceControl> layer;
430 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
431 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
432
433 // GLES requires only 4 bits of subpixel precision during rasterization
434 // XXX GLES composition does not match HWC composition due to precision
435 // loss (b/69315223)
436 const float epsilon = 1.0f / 16.0f;
437 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
438 {
439 SCOPED_TRACE("rounding down");
440 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
441 }
442
443 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
444 {
445 SCOPED_TRACE("rounding up");
446 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
447 }
448}
449
450TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
451 sp<SurfaceControl> layer;
452 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
453 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
454
455 Transaction().setPosition(layer, -32, -32).apply();
456 {
457 SCOPED_TRACE("negative coordinates");
458 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
459 }
460
461 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
462 {
463 SCOPED_TRACE("positive coordinates");
464 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
465 }
466}
467
468TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
469 sp<SurfaceControl> layer;
470 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
471 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
472
473 // partially out of bounds
474 Transaction().setPosition(layer, -30, -30).apply();
475 {
476 SCOPED_TRACE("negative coordinates");
477 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
478 }
479
480 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
481 {
482 SCOPED_TRACE("positive coordinates");
483 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
484 mDisplayHeight),
485 Color::RED);
486 }
487}
488
489TEST_F(LayerTransactionTest, SetPositionWithResize) {
490 sp<SurfaceControl> layer;
491 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
492 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
493
494 // setPosition is applied immediately by default, with or without resize
495 // pending
496 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
497 {
498 SCOPED_TRACE("resize pending");
499 auto shot = screenshot();
500 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
501 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
502 }
503
504 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
505 {
506 SCOPED_TRACE("resize applied");
507 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
508 }
509}
510
511TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
512 sp<SurfaceControl> layer;
513 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
514 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
515
516 // request setPosition to be applied with the next resize
517 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
518 {
519 SCOPED_TRACE("new position pending");
520 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
521 }
522
523 Transaction().setPosition(layer, 15, 20).apply();
524 {
525 SCOPED_TRACE("pending new position modified");
526 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
527 }
528
529 Transaction().setSize(layer, 64, 64).apply();
530 {
531 SCOPED_TRACE("resize pending");
532 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
533 }
534
535 // finally resize and latch the buffer
536 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
537 {
538 SCOPED_TRACE("new position applied");
539 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
540 }
541}
542
543TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
544 sp<SurfaceControl> layer;
545 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
546 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
547
548 // setPosition is not immediate even with SCALE_TO_WINDOW override
549 Transaction()
550 .setPosition(layer, 5, 10)
551 .setSize(layer, 64, 64)
552 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
553 .setGeometryAppliesWithResize(layer)
554 .apply();
555 {
556 SCOPED_TRACE("new position pending");
557 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
558 }
559
560 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
561 {
562 SCOPED_TRACE("new position applied");
563 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
564 }
565}
566
Chia-I Wu0eaea312017-10-31 10:14:40 -0700567TEST_F(LayerTransactionTest, SetSizeBasic) {
568 sp<SurfaceControl> layer;
569 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
570 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
571
572 Transaction().setSize(layer, 64, 64).apply();
573 {
574 SCOPED_TRACE("resize pending");
575 auto shot = screenshot();
576 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
577 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
578 }
579
580 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
581 {
582 SCOPED_TRACE("resize applied");
583 auto shot = screenshot();
584 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
585 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
586 }
587}
588
589TEST_F(LayerTransactionTest, SetSizeInvalid) {
590 // cannot test robustness against invalid sizes (zero or really huge)
591}
592
593TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
594 sp<SurfaceControl> layer;
595 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
596 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
597
598 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
599 Transaction()
600 .setSize(layer, 64, 64)
601 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
602 .apply();
603 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
604}
605
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700606TEST_F(LayerTransactionTest, SetZBasic) {
607 sp<SurfaceControl> layerR;
608 sp<SurfaceControl> layerG;
609 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
610 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
611 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
612 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
613
614 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
615 {
616 SCOPED_TRACE("layerR");
617 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
618 }
619
620 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
621 {
622 SCOPED_TRACE("layerG");
623 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
624 }
625}
626
627TEST_F(LayerTransactionTest, SetZNegative) {
628 sp<SurfaceControl> layerR;
629 sp<SurfaceControl> layerG;
630 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
631 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
632 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
633 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
634
635 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
636 {
637 SCOPED_TRACE("layerR");
638 sp<ScreenCapture> screenshot;
639 ScreenCapture::captureScreen(&screenshot, -2, -1);
640 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
641 }
642
643 Transaction().setLayer(layerR, -3).apply();
644 {
645 SCOPED_TRACE("layerG");
646 sp<ScreenCapture> screenshot;
647 ScreenCapture::captureScreen(&screenshot, -3, -1);
648 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
649 }
650}
651
Chia-I Wu49313302017-10-31 10:14:40 -0700652TEST_F(LayerTransactionTest, SetRelativeZBasic) {
653 sp<SurfaceControl> layerR;
654 sp<SurfaceControl> layerG;
655 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
656 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
657 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
658 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
659
660 Transaction()
661 .setPosition(layerG, 16, 16)
662 .setRelativeLayer(layerG, layerR->getHandle(), 1)
663 .apply();
664 {
665 SCOPED_TRACE("layerG above");
666 auto shot = screenshot();
667 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
668 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
669 }
670
671 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
672 {
673 SCOPED_TRACE("layerG below");
674 auto shot = screenshot();
675 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
676 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
677 }
678}
679
Chia-I Wuec2d9852017-11-21 09:21:01 -0800680TEST_F(LayerTransactionTest, SetRelativeZNegative) {
681 sp<SurfaceControl> layerR;
682 sp<SurfaceControl> layerG;
683 sp<SurfaceControl> layerB;
684 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
685 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
686 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
687 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
688 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
689 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
690
691 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
692 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
693
694 sp<ScreenCapture> screenshot;
695 // only layerB is in this range
696 ScreenCapture::captureScreen(&screenshot, -2, -1);
697 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
698}
699
Chia-I Wu49313302017-10-31 10:14:40 -0700700TEST_F(LayerTransactionTest, SetRelativeZGroup) {
701 sp<SurfaceControl> layerR;
702 sp<SurfaceControl> layerG;
703 sp<SurfaceControl> layerB;
704 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
705 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
706 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
707 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
708 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
709 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
710
711 // layerR = 0, layerG = layerR + 3, layerB = 2
712 Transaction()
713 .setPosition(layerG, 8, 8)
714 .setRelativeLayer(layerG, layerR->getHandle(), 3)
715 .setPosition(layerB, 16, 16)
716 .setLayer(layerB, mLayerZBase + 2)
717 .apply();
718 {
719 SCOPED_TRACE("(layerR < layerG) < layerB");
720 auto shot = screenshot();
721 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
722 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
723 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
724 }
725
726 // layerR = 4, layerG = layerR + 3, layerB = 2
727 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
728 {
729 SCOPED_TRACE("layerB < (layerR < layerG)");
730 auto shot = screenshot();
731 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
732 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
733 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
734 }
735
736 // layerR = 4, layerG = layerR - 3, layerB = 2
737 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
738 {
739 SCOPED_TRACE("layerB < (layerG < layerR)");
740 auto shot = screenshot();
741 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
742 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
743 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
744 }
745
746 // restore to absolute z
747 // layerR = 4, layerG = 0, layerB = 2
748 Transaction().setLayer(layerG, mLayerZBase).apply();
749 {
750 SCOPED_TRACE("layerG < layerB < layerR");
751 auto shot = screenshot();
752 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
753 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
754 }
755
756 // layerR should not affect layerG anymore
757 // layerR = 1, layerG = 0, layerB = 2
758 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
759 {
760 SCOPED_TRACE("layerG < layerR < layerB");
761 auto shot = screenshot();
762 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
763 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
764 }
765}
766
767TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
768 sp<SurfaceControl> layerR;
769 sp<SurfaceControl> layerG;
770
771 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
772 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
773 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
774 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
775
776 Transaction()
777 .setPosition(layerG, 16, 16)
778 .setRelativeLayer(layerG, layerR->getHandle(), 1)
779 .apply();
780
781 mClient->destroySurface(layerG->getHandle());
782 // layerG should have been removed
783 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
784}
785
Chia-I Wu57b27502017-10-31 10:14:40 -0700786TEST_F(LayerTransactionTest, SetFlagsHidden) {
787 sp<SurfaceControl> layer;
788 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
789 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
790
791 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
792 {
793 SCOPED_TRACE("layer hidden");
794 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
795 }
796
797 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
798 {
799 SCOPED_TRACE("layer shown");
800 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
801 }
802}
803
804TEST_F(LayerTransactionTest, SetFlagsOpaque) {
805 const Color translucentRed = {100, 0, 0, 100};
806 sp<SurfaceControl> layerR;
807 sp<SurfaceControl> layerG;
808 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
809 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
810 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
811 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
812
813 Transaction()
814 .setLayer(layerR, mLayerZBase + 1)
815 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
816 .apply();
817 {
818 SCOPED_TRACE("layerR opaque");
819 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
820 }
821
822 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
823 {
824 SCOPED_TRACE("layerR translucent");
825 const uint8_t g = uint8_t(255 - translucentRed.a);
826 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
827 }
828}
829
830TEST_F(LayerTransactionTest, SetFlagsSecure) {
831 sp<SurfaceControl> layer;
832 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
833 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
834
835 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000836 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -0700837 Transaction()
838 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
839 .apply(true);
840 ASSERT_EQ(PERMISSION_DENIED,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000841 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
Chia-I Wu57b27502017-10-31 10:14:40 -0700842 false));
843
844 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
845 ASSERT_EQ(NO_ERROR,
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000846 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
Chia-I Wu57b27502017-10-31 10:14:40 -0700847 false));
848}
849
Chia-I Wu2113bdd2017-11-01 15:16:35 -0700850TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
851 const Rect top(0, 0, 32, 16);
852 const Rect bottom(0, 16, 32, 32);
853 sp<SurfaceControl> layer;
854 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
855
856 ANativeWindow_Buffer buffer;
857 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
858 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
859 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
860 // setTransparentRegionHint always applies to the following buffer
861 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
862 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
863 {
864 SCOPED_TRACE("top transparent");
865 auto shot = screenshot();
866 shot->expectColor(top, Color::BLACK);
867 shot->expectColor(bottom, Color::RED);
868 }
869
870 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
871 {
872 SCOPED_TRACE("transparent region hint pending");
873 auto shot = screenshot();
874 shot->expectColor(top, Color::BLACK);
875 shot->expectColor(bottom, Color::RED);
876 }
877
878 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
879 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
880 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
881 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
882 {
883 SCOPED_TRACE("bottom transparent");
884 auto shot = screenshot();
885 shot->expectColor(top, Color::RED);
886 shot->expectColor(bottom, Color::BLACK);
887 }
888}
889
890TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
891 sp<SurfaceControl> layerTransparent;
892 sp<SurfaceControl> layerR;
893 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
894 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
895
896 // check that transparent region hint is bound by the layer size
897 Transaction()
898 .setTransparentRegionHint(layerTransparent,
899 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
900 .setPosition(layerR, 16, 16)
901 .setLayer(layerR, mLayerZBase + 1)
902 .apply();
903 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
904 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
905 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
906}
907
Chia-I Wua8a515e2017-11-01 15:16:35 -0700908TEST_F(LayerTransactionTest, SetAlphaBasic) {
909 sp<SurfaceControl> layer1;
910 sp<SurfaceControl> layer2;
911 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
912 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
913 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
914 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
915
916 Transaction()
917 .setAlpha(layer1, 0.25f)
918 .setAlpha(layer2, 0.75f)
919 .setPosition(layer2, 16, 0)
920 .setLayer(layer2, mLayerZBase + 1)
921 .apply();
922 {
923 auto shot = screenshot();
924 uint8_t r = 16; // 64 * 0.25f
925 uint8_t g = 48; // 64 * 0.75f
926 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
927 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
928
929 r /= 4; // r * (1.0f - 0.75f)
930 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
931 }
932}
933
934TEST_F(LayerTransactionTest, SetAlphaClamped) {
935 const Color color = {64, 0, 0, 255};
936 sp<SurfaceControl> layer;
937 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
938 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
939
940 Transaction().setAlpha(layer, 2.0f).apply();
941 {
942 SCOPED_TRACE("clamped to 1.0f");
943 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
944 }
945
946 Transaction().setAlpha(layer, -1.0f).apply();
947 {
948 SCOPED_TRACE("clamped to 0.0f");
949 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
950 }
951}
952
Chia-I Wue4ef6102017-11-01 15:16:35 -0700953TEST_F(LayerTransactionTest, SetColorBasic) {
954 sp<SurfaceControl> bufferLayer;
955 sp<SurfaceControl> colorLayer;
956 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
957 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
958 ASSERT_NO_FATAL_FAILURE(
959 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
960
961 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
962 {
963 SCOPED_TRACE("default color");
964 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
965 }
966
967 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
968 const Color expected = {15, 51, 85, 255};
969 // this is handwavy, but the precison loss scaled by 255 (8-bit per
970 // channel) should be less than one
971 const uint8_t tolerance = 1;
972 Transaction().setColor(colorLayer, color).apply();
973 {
974 SCOPED_TRACE("new color");
975 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
976 }
977}
978
979TEST_F(LayerTransactionTest, SetColorClamped) {
980 sp<SurfaceControl> colorLayer;
981 ASSERT_NO_FATAL_FAILURE(
982 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
983
984 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
985 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
986}
987
988TEST_F(LayerTransactionTest, SetColorWithAlpha) {
989 sp<SurfaceControl> bufferLayer;
990 sp<SurfaceControl> colorLayer;
991 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
992 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
993 ASSERT_NO_FATAL_FAILURE(
994 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
995
996 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
997 const float alpha = 0.25f;
998 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
999 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1000 // channel) should be less than one
1001 const uint8_t tolerance = 1;
1002 Transaction()
1003 .setColor(colorLayer, color)
1004 .setAlpha(colorLayer, alpha)
1005 .setLayer(colorLayer, mLayerZBase + 1)
1006 .apply();
1007 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1008 tolerance);
1009}
1010
Adrian Roosb7a96502018-04-08 11:38:55 -07001011TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1012 sp<SurfaceControl> bufferLayer;
1013 sp<SurfaceControl> parentLayer;
1014 sp<SurfaceControl> colorLayer;
1015 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1016 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
1017 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1018 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer(
1019 "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1020
1021 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1022 const float alpha = 0.25f;
1023 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1024 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1025 // channel) should be less than one
1026 const uint8_t tolerance = 1;
1027 Transaction()
1028 .reparent(colorLayer, parentLayer->getHandle())
1029 .setColor(colorLayer, color)
1030 .setAlpha(parentLayer, alpha)
1031 .setLayer(parentLayer, mLayerZBase + 1)
1032 .apply();
1033 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1034 tolerance);
1035}
1036
Chia-I Wue4ef6102017-11-01 15:16:35 -07001037TEST_F(LayerTransactionTest, SetColorWithBuffer) {
1038 sp<SurfaceControl> bufferLayer;
1039 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1040 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1041
1042 // color is ignored
1043 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1044 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1045}
1046
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001047TEST_F(LayerTransactionTest, SetLayerStackBasic) {
1048 sp<SurfaceControl> layer;
1049 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1050 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1051
1052 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1053 {
1054 SCOPED_TRACE("non-existing layer stack");
1055 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1056 }
1057
1058 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1059 {
1060 SCOPED_TRACE("original layer stack");
1061 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1062 }
1063}
1064
Chia-I Wu93853fe2017-11-02 08:30:27 -07001065TEST_F(LayerTransactionTest, SetMatrixBasic) {
1066 sp<SurfaceControl> layer;
1067 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1068 ASSERT_NO_FATAL_FAILURE(
1069 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1070
1071 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1072 {
1073 SCOPED_TRACE("IDENTITY");
1074 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1075 Color::WHITE);
1076 }
1077
1078 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1079 {
1080 SCOPED_TRACE("FLIP_H");
1081 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1082 Color::BLUE);
1083 }
1084
1085 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1086 {
1087 SCOPED_TRACE("FLIP_V");
1088 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1089 Color::GREEN);
1090 }
1091
1092 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1093 {
1094 SCOPED_TRACE("ROT_90");
1095 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1096 Color::GREEN);
1097 }
1098
1099 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1100 {
1101 SCOPED_TRACE("SCALE");
1102 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1103 Color::WHITE, true /* filtered */);
1104 }
1105}
1106
1107TEST_F(LayerTransactionTest, SetMatrixRot45) {
1108 sp<SurfaceControl> layer;
1109 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1110 ASSERT_NO_FATAL_FAILURE(
1111 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1112
1113 const float rot = M_SQRT1_2; // 45 degrees
1114 const float trans = M_SQRT2 * 16.0f;
1115 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1116
1117 auto shot = screenshot();
1118 // check a 8x8 region inside each color
1119 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1120 const int32_t halfL = 4;
1121 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1122 };
1123 const int32_t unit = int32_t(trans / 2);
1124 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1125 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1126 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1127 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1128}
1129
1130TEST_F(LayerTransactionTest, SetMatrixWithResize) {
1131 sp<SurfaceControl> layer;
1132 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1133 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1134
1135 // setMatrix is applied after any pending resize, unlike setPosition
1136 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1137 {
1138 SCOPED_TRACE("resize pending");
1139 auto shot = screenshot();
1140 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1141 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1142 }
1143
1144 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1145 {
1146 SCOPED_TRACE("resize applied");
1147 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1148 }
1149}
1150
1151TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) {
1152 sp<SurfaceControl> layer;
1153 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1154 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1155
1156 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1157 Transaction()
1158 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1159 .setSize(layer, 64, 64)
1160 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1161 .apply();
1162 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1163}
1164
Chia-I Wua56b2042017-11-01 15:16:35 -07001165TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) {
1166 sp<SurfaceControl> layer;
1167 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1168 ASSERT_NO_FATAL_FAILURE(
1169 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1170
1171 // XXX SCALE_CROP is not respected; calling setSize and
1172 // setOverrideScalingMode in separate transactions does not work
1173 // (b/69315456)
1174 Transaction()
1175 .setSize(layer, 64, 16)
1176 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1177 .apply();
1178 {
1179 SCOPED_TRACE("SCALE_TO_WINDOW");
1180 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1181 Color::WHITE, true /* filtered */);
1182 }
1183}
1184
Chia-I Wu04dcca82017-11-02 08:30:27 -07001185TEST_F(LayerTransactionTest, SetCropBasic) {
1186 sp<SurfaceControl> layer;
1187 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1188 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1189 const Rect crop(8, 8, 24, 24);
1190
Marissa Wallf58c14b2018-07-24 10:50:43 -07001191 Transaction().setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001192 auto shot = screenshot();
1193 shot->expectColor(crop, Color::RED);
1194 shot->expectBorder(crop, Color::BLACK);
1195}
1196
1197TEST_F(LayerTransactionTest, SetCropEmpty) {
1198 sp<SurfaceControl> layer;
1199 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1200 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1201
1202 {
1203 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001204 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001205 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1206 }
1207
1208 {
1209 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001210 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001211 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1212 }
1213}
1214
1215TEST_F(LayerTransactionTest, SetCropOutOfBounds) {
1216 sp<SurfaceControl> layer;
1217 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1218 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1219
Marissa Wallf58c14b2018-07-24 10:50:43 -07001220 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001221 auto shot = screenshot();
1222 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1223 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1224}
1225
1226TEST_F(LayerTransactionTest, SetCropWithTranslation) {
1227 sp<SurfaceControl> layer;
1228 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1229 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1230
1231 const Point position(32, 32);
1232 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001233 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001234 auto shot = screenshot();
1235 shot->expectColor(crop + position, Color::RED);
1236 shot->expectBorder(crop + position, Color::BLACK);
1237}
1238
1239TEST_F(LayerTransactionTest, SetCropWithScale) {
1240 sp<SurfaceControl> layer;
1241 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1242 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1243
1244 // crop is affected by matrix
1245 Transaction()
1246 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001247 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001248 .apply();
1249 auto shot = screenshot();
1250 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1251 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1252}
1253
1254TEST_F(LayerTransactionTest, SetCropWithResize) {
1255 sp<SurfaceControl> layer;
1256 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1257 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1258
Marissa Wallf58c14b2018-07-24 10:50:43 -07001259 // setCrop_legacy is applied immediately by default, with or without resize pending
1260 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001261 {
1262 SCOPED_TRACE("resize pending");
1263 auto shot = screenshot();
1264 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1265 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1266 }
1267
1268 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1269 {
1270 SCOPED_TRACE("resize applied");
1271 auto shot = screenshot();
1272 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1273 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1274 }
1275}
1276
1277TEST_F(LayerTransactionTest, SetCropWithNextResize) {
1278 sp<SurfaceControl> layer;
1279 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1280 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1281
Marissa Wallf58c14b2018-07-24 10:50:43 -07001282 // request setCrop_legacy to be applied with the next resize
1283 Transaction()
1284 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
1285 .setGeometryAppliesWithResize(layer)
1286 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001287 {
1288 SCOPED_TRACE("waiting for next resize");
1289 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1290 }
1291
Marissa Wallf58c14b2018-07-24 10:50:43 -07001292 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001293 {
1294 SCOPED_TRACE("pending crop modified");
1295 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1296 }
1297
1298 Transaction().setSize(layer, 16, 16).apply();
1299 {
1300 SCOPED_TRACE("resize pending");
1301 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1302 }
1303
1304 // finally resize
1305 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1306 {
1307 SCOPED_TRACE("new crop applied");
1308 auto shot = screenshot();
1309 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1310 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1311 }
1312}
1313
1314TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow) {
1315 sp<SurfaceControl> layer;
1316 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1317 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1318
Marissa Wallf58c14b2018-07-24 10:50:43 -07001319 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07001320 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001321 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001322 .setSize(layer, 16, 16)
1323 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1324 .setGeometryAppliesWithResize(layer)
1325 .apply();
1326 {
1327 SCOPED_TRACE("new crop pending");
1328 auto shot = screenshot();
1329 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1330 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1331 }
1332
1333 // XXX crop is never latched without other geometry change (b/69315677)
1334 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1335 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1336 Transaction().setPosition(layer, 0, 0).apply();
1337 {
1338 SCOPED_TRACE("new crop applied");
1339 auto shot = screenshot();
1340 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1341 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1342 }
1343}
1344
Chia-I Wucdd71a52017-11-02 08:30:27 -07001345TEST_F(LayerTransactionTest, SetFinalCropBasic) {
1346 sp<SurfaceControl> layer;
1347 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1348 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1349 const Rect crop(8, 8, 24, 24);
1350
1351 // same as in SetCropBasic
Marissa Wallf58c14b2018-07-24 10:50:43 -07001352 Transaction().setFinalCrop_legacy(layer, crop).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001353 auto shot = screenshot();
1354 shot->expectColor(crop, Color::RED);
1355 shot->expectBorder(crop, Color::BLACK);
1356}
1357
1358TEST_F(LayerTransactionTest, SetFinalCropEmpty) {
1359 sp<SurfaceControl> layer;
1360 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1361 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1362
1363 // same as in SetCropEmpty
1364 {
1365 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001366 Transaction().setFinalCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001367 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1368 }
1369
1370 {
1371 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001372 Transaction().setFinalCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001373 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1374 }
1375}
1376
1377TEST_F(LayerTransactionTest, SetFinalCropOutOfBounds) {
1378 sp<SurfaceControl> layer;
1379 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1380 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1381
1382 // same as in SetCropOutOfBounds
Marissa Wallf58c14b2018-07-24 10:50:43 -07001383 Transaction().setFinalCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001384 auto shot = screenshot();
1385 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1386 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1387}
1388
1389TEST_F(LayerTransactionTest, SetFinalCropWithTranslation) {
1390 sp<SurfaceControl> layer;
1391 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1392 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1393
1394 // final crop is applied post-translation
Marissa Wallf58c14b2018-07-24 10:50:43 -07001395 Transaction().setPosition(layer, 16, 16).setFinalCrop_legacy(layer, Rect(8, 8, 24, 24)).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001396 auto shot = screenshot();
1397 shot->expectColor(Rect(16, 16, 24, 24), Color::RED);
1398 shot->expectBorder(Rect(16, 16, 24, 24), Color::BLACK);
1399}
1400
1401TEST_F(LayerTransactionTest, SetFinalCropWithScale) {
1402 sp<SurfaceControl> layer;
1403 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1404 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1405
1406 // final crop is not affected by matrix
1407 Transaction()
1408 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001409 .setFinalCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wucdd71a52017-11-02 08:30:27 -07001410 .apply();
1411 auto shot = screenshot();
1412 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1413 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1414}
1415
1416TEST_F(LayerTransactionTest, SetFinalCropWithResize) {
1417 sp<SurfaceControl> layer;
1418 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1419 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1420
1421 // same as in SetCropWithResize
Marissa Wallf58c14b2018-07-24 10:50:43 -07001422 Transaction().setFinalCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001423 {
1424 SCOPED_TRACE("resize pending");
1425 auto shot = screenshot();
1426 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1427 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1428 }
1429
1430 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1431 {
1432 SCOPED_TRACE("resize applied");
1433 auto shot = screenshot();
1434 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1435 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1436 }
1437}
1438
1439TEST_F(LayerTransactionTest, SetFinalCropWithNextResize) {
1440 sp<SurfaceControl> layer;
1441 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1442 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1443
1444 // same as in SetCropWithNextResize
1445 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001446 .setFinalCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wucdd71a52017-11-02 08:30:27 -07001447 .setGeometryAppliesWithResize(layer)
1448 .apply();
1449 {
1450 SCOPED_TRACE("waiting for next resize");
1451 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1452 }
1453
Marissa Wallf58c14b2018-07-24 10:50:43 -07001454 Transaction().setFinalCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wucdd71a52017-11-02 08:30:27 -07001455 {
1456 SCOPED_TRACE("pending final crop modified");
1457 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1458 }
1459
1460 Transaction().setSize(layer, 16, 16).apply();
1461 {
1462 SCOPED_TRACE("resize pending");
1463 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1464 }
1465
1466 // finally resize
1467 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1468 {
1469 SCOPED_TRACE("new final crop applied");
1470 auto shot = screenshot();
1471 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1472 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1473 }
1474}
1475
1476TEST_F(LayerTransactionTest, SetFinalCropWithNextResizeScaleToWindow) {
1477 sp<SurfaceControl> layer;
1478 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1479 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1480
1481 // same as in SetCropWithNextResizeScaleToWindow
1482 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07001483 .setFinalCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wucdd71a52017-11-02 08:30:27 -07001484 .setSize(layer, 16, 16)
1485 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1486 .setGeometryAppliesWithResize(layer)
1487 .apply();
1488 {
1489 SCOPED_TRACE("new final crop pending");
1490 auto shot = screenshot();
1491 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1492 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1493 }
1494
1495 // XXX final crop is never latched without other geometry change (b/69315677)
1496 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1497 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1498 Transaction().setPosition(layer, 0, 0).apply();
1499 {
1500 SCOPED_TRACE("new final crop applied");
1501 auto shot = screenshot();
1502 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1503 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1504 }
1505}
1506
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001507class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001508protected:
1509 virtual void SetUp() {
1510 mComposerClient = new SurfaceComposerClient;
1511 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1512
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001513 sp<IBinder> display(
1514 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001515 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001516 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001517
1518 ssize_t displayWidth = info.w;
1519 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001520
1521 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001522 mBGSurfaceControl =
1523 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1524 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001525 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001526 ASSERT_TRUE(mBGSurfaceControl->isValid());
1527 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1528
1529 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001530 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1531 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001532 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001533 ASSERT_TRUE(mFGSurfaceControl->isValid());
1534
1535 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1536
1537 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001538 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1539 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08001540 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001541 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1542
1543 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1544
Robert Carr4cdc58f2017-08-23 14:22:20 -07001545 asTransaction([&](Transaction& t) {
1546 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001547
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001548 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001549
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001550 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1551 .setPosition(mFGSurfaceControl, 64, 64)
1552 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001553
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001554 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1555 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1556 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001557 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001558 }
1559
1560 virtual void TearDown() {
1561 mComposerClient->dispose();
1562 mBGSurfaceControl = 0;
1563 mFGSurfaceControl = 0;
1564 mSyncSurfaceControl = 0;
1565 mComposerClient = 0;
1566 }
1567
1568 void waitForPostedBuffers() {
1569 // Since the sync surface is in synchronous mode (i.e. double buffered)
1570 // posting three buffers to it should ensure that at least two
1571 // SurfaceFlinger::handlePageFlip calls have been made, which should
1572 // guaranteed that a buffer posted to another Surface has been retired.
1573 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1574 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1575 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1576 }
1577
Robert Carr4cdc58f2017-08-23 14:22:20 -07001578 void asTransaction(const std::function<void(Transaction&)>& exec) {
1579 Transaction t;
1580 exec(t);
1581 t.apply(true);
1582 }
1583
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001584 sp<SurfaceComposerClient> mComposerClient;
1585 sp<SurfaceControl> mBGSurfaceControl;
1586 sp<SurfaceControl> mFGSurfaceControl;
1587
1588 // This surface is used to ensure that the buffers posted to
1589 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1590 sp<SurfaceControl> mSyncSurfaceControl;
1591};
1592
Robert Carr7f619b22017-11-06 12:56:35 -08001593TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1594 sp<ScreenCapture> sc;
1595
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001596 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1597 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001598 fillSurfaceRGBA8(relative, 10, 10, 10);
1599 waitForPostedBuffers();
1600
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001601 Transaction{}
1602 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001603 .setPosition(relative, 64, 64)
1604 .apply();
1605
1606 {
1607 // The relative should be on top of the FG control.
1608 ScreenCapture::captureScreen(&sc);
1609 sc->checkPixel(64, 64, 10, 10, 10);
1610 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001611 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001612
1613 {
1614 // Nothing should change at this point.
1615 ScreenCapture::captureScreen(&sc);
1616 sc->checkPixel(64, 64, 10, 10, 10);
1617 }
1618
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001619 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001620
1621 {
1622 // Ensure that the relative was actually hidden, rather than
1623 // being left in the detached but visible state.
1624 ScreenCapture::captureScreen(&sc);
1625 sc->expectFGColor(64, 64);
1626 }
1627}
1628
Robert Carr8d5227b2017-03-16 15:41:03 -07001629class GeometryLatchingTest : public LayerUpdateTest {
1630protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001631 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001632 SCOPED_TRACE(trace);
1633 ScreenCapture::captureScreen(&sc);
1634 // We find the leading edge of the FG surface.
1635 sc->expectFGColor(127, 127);
1636 sc->expectBGColor(128, 128);
1637 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001638
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001639 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001640
1641 void unlockFGBuffer() {
1642 sp<Surface> s = mFGSurfaceControl->getSurface();
1643 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1644 waitForPostedBuffers();
1645 }
1646
Robert Carr8d5227b2017-03-16 15:41:03 -07001647 void completeFGResize() {
1648 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1649 waitForPostedBuffers();
1650 }
1651 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001652 asTransaction([&](Transaction& t) {
1653 t.setSize(mFGSurfaceControl, 64, 64);
1654 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001655 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
1656 t.setFinalCrop_legacy(mFGSurfaceControl, Rect(0, 0, -1, -1));
Robert Carr4cdc58f2017-08-23 14:22:20 -07001657 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001658
1659 EXPECT_INITIAL_STATE("After restoring initial state");
1660 }
1661 sp<ScreenCapture> sc;
1662};
1663
Robert Carr8d5227b2017-03-16 15:41:03 -07001664class CropLatchingTest : public GeometryLatchingTest {
1665protected:
1666 void EXPECT_CROPPED_STATE(const char* trace) {
1667 SCOPED_TRACE(trace);
1668 ScreenCapture::captureScreen(&sc);
1669 // The edge should be moved back one pixel by our crop.
1670 sc->expectFGColor(126, 126);
1671 sc->expectBGColor(127, 127);
1672 sc->expectBGColor(128, 128);
1673 }
chaviw59f5c562017-06-28 16:39:06 -07001674
1675 void EXPECT_RESIZE_STATE(const char* trace) {
1676 SCOPED_TRACE(trace);
1677 ScreenCapture::captureScreen(&sc);
1678 // The FG is now resized too 128,128 at 64,64
1679 sc->expectFGColor(64, 64);
1680 sc->expectFGColor(191, 191);
1681 sc->expectBGColor(192, 192);
1682 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001683};
1684
Robert Carr7bf247e2017-05-18 14:02:49 -07001685// In this test we ensure that setGeometryAppliesWithResize actually demands
1686// a buffer of the new size, and not just any size.
1687TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1688 EXPECT_INITIAL_STATE("before anything");
1689 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001690 asTransaction([&](Transaction& t) {
1691 t.setSize(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001692 t.setFinalCrop_legacy(mFGSurfaceControl, Rect(64, 64, 127, 127));
Robert Carr4cdc58f2017-08-23 14:22:20 -07001693 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001694
1695 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1696
1697 restoreInitialState();
1698
1699 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1700 // initiating the resize.
1701 lockAndFillFGBuffer();
1702
Robert Carr4cdc58f2017-08-23 14:22:20 -07001703 asTransaction([&](Transaction& t) {
1704 t.setSize(mFGSurfaceControl, 128, 128);
1705 t.setGeometryAppliesWithResize(mFGSurfaceControl);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001706 t.setFinalCrop_legacy(mFGSurfaceControl, Rect(64, 64, 127, 127));
Robert Carr4cdc58f2017-08-23 14:22:20 -07001707 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001708
1709 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1710
1711 // We now submit our old buffer, at the old size, and ensure it doesn't
1712 // trigger geometry latching.
1713 unlockFGBuffer();
1714
1715 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1716
1717 completeFGResize();
1718
1719 EXPECT_CROPPED_STATE("after the resize finishes");
1720}
1721
Pablo Ceballos05289c22016-04-14 15:49:55 -07001722TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1723 sp<ScreenCapture> sc;
1724 {
1725 SCOPED_TRACE("before anything");
1726 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001727 sc->expectBGColor(32, 32);
1728 sc->expectFGColor(96, 96);
1729 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001730 }
1731
1732 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001733 asTransaction([&](Transaction& t) {
1734 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001735 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
1736 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001737 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001738
Robert Carr4cdc58f2017-08-23 14:22:20 -07001739 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001740 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001741 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
1742 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001743 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001744
1745 {
1746 SCOPED_TRACE("before any trigger");
1747 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001748 sc->expectBGColor(32, 32);
1749 sc->expectFGColor(96, 96);
1750 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001751 }
1752
1753 // should trigger the first deferred transaction, but not the second one
1754 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1755 {
1756 SCOPED_TRACE("after first trigger");
1757 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001758 sc->expectBGColor(32, 32);
1759 sc->checkPixel(96, 96, 162, 63, 96);
1760 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001761 }
1762
1763 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001764 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001765
1766 // trigger the second deferred transaction
1767 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1768 {
1769 SCOPED_TRACE("after second trigger");
1770 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001771 sc->expectBGColor(32, 32);
1772 sc->expectBGColor(96, 96);
1773 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001774 }
1775}
1776
Robert Carre392b552017-09-19 12:16:05 -07001777TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1778 sp<ScreenCapture> sc;
1779
1780 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001781 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1782 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1783 sp<SurfaceControl> childBuffer =
1784 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1785 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001786 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1787
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001788 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001789
1790 {
1791 ScreenCapture::captureScreen(&sc);
1792 sc->expectChildColor(73, 73);
1793 sc->expectFGColor(74, 74);
1794 }
1795
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001796 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001797
1798 {
1799 ScreenCapture::captureScreen(&sc);
1800 sc->expectChildColor(73, 73);
1801 sc->expectChildColor(74, 74);
1802 }
1803}
1804
Robert Carr2c5f6d22017-09-26 12:30:35 -07001805TEST_F(LayerUpdateTest, MergingTransactions) {
1806 sp<ScreenCapture> sc;
1807 {
1808 SCOPED_TRACE("before move");
1809 ScreenCapture::captureScreen(&sc);
1810 sc->expectBGColor(0, 12);
1811 sc->expectFGColor(75, 75);
1812 sc->expectBGColor(145, 145);
1813 }
1814
1815 Transaction t1, t2;
1816 t1.setPosition(mFGSurfaceControl, 128, 128);
1817 t2.setPosition(mFGSurfaceControl, 0, 0);
1818 // We expect that the position update from t2 now
1819 // overwrites the position update from t1.
1820 t1.merge(std::move(t2));
1821 t1.apply();
1822
1823 {
1824 ScreenCapture::captureScreen(&sc);
1825 sc->expectFGColor(1, 1);
1826 }
1827}
1828
Robert Carr1f0a16a2016-10-24 16:27:39 -07001829class ChildLayerTest : public LayerUpdateTest {
1830protected:
1831 void SetUp() override {
1832 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001833 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1834 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001835 fillSurfaceRGBA8(mChild, 200, 200, 200);
1836
1837 {
1838 SCOPED_TRACE("before anything");
1839 ScreenCapture::captureScreen(&mCapture);
1840 mCapture->expectChildColor(64, 64);
1841 }
1842 }
1843 void TearDown() override {
1844 LayerUpdateTest::TearDown();
1845 mChild = 0;
1846 }
1847
1848 sp<SurfaceControl> mChild;
1849 sp<ScreenCapture> mCapture;
1850};
1851
1852TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001853 asTransaction([&](Transaction& t) {
1854 t.show(mChild);
1855 t.setPosition(mChild, 10, 10);
1856 t.setPosition(mFGSurfaceControl, 64, 64);
1857 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001858
1859 {
1860 ScreenCapture::captureScreen(&mCapture);
1861 // Top left of foreground must now be visible
1862 mCapture->expectFGColor(64, 64);
1863 // But 10 pixels in we should see the child surface
1864 mCapture->expectChildColor(74, 74);
1865 // And 10 more pixels we should be back to the foreground surface
1866 mCapture->expectFGColor(84, 84);
1867 }
1868
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001869 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001870
1871 {
1872 ScreenCapture::captureScreen(&mCapture);
1873 // Top left of foreground should now be at 0, 0
1874 mCapture->expectFGColor(0, 0);
1875 // But 10 pixels in we should see the child surface
1876 mCapture->expectChildColor(10, 10);
1877 // And 10 more pixels we should be back to the foreground surface
1878 mCapture->expectFGColor(20, 20);
1879 }
1880}
1881
Robert Carr41b08b52017-06-01 16:11:34 -07001882TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001883 asTransaction([&](Transaction& t) {
1884 t.show(mChild);
1885 t.setPosition(mChild, 0, 0);
1886 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001887 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07001888 });
Robert Carr41b08b52017-06-01 16:11:34 -07001889
1890 {
1891 ScreenCapture::captureScreen(&mCapture);
1892 mCapture->expectChildColor(0, 0);
1893 mCapture->expectChildColor(4, 4);
1894 mCapture->expectBGColor(5, 5);
1895 }
1896}
1897
1898TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001899 asTransaction([&](Transaction& t) {
1900 t.show(mChild);
1901 t.setPosition(mChild, 0, 0);
1902 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001903 t.setFinalCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07001904 });
Robert Carr41b08b52017-06-01 16:11:34 -07001905
1906 {
1907 ScreenCapture::captureScreen(&mCapture);
1908 mCapture->expectChildColor(0, 0);
1909 mCapture->expectChildColor(4, 4);
1910 mCapture->expectBGColor(5, 5);
1911 }
1912}
1913
Robert Carr1f0a16a2016-10-24 16:27:39 -07001914TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001915 asTransaction([&](Transaction& t) {
1916 t.show(mChild);
1917 t.setPosition(mFGSurfaceControl, 0, 0);
1918 t.setPosition(mChild, 63, 63);
1919 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001920
1921 {
1922 ScreenCapture::captureScreen(&mCapture);
1923 mCapture->expectFGColor(0, 0);
1924 // Last pixel in foreground should now be the child.
1925 mCapture->expectChildColor(63, 63);
1926 // But the child should be constrained and the next pixel
1927 // must be the background
1928 mCapture->expectBGColor(64, 64);
1929 }
1930}
1931
1932TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001933 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001934
1935 // Find the boundary between the parent and child
1936 {
1937 ScreenCapture::captureScreen(&mCapture);
1938 mCapture->expectChildColor(9, 9);
1939 mCapture->expectFGColor(10, 10);
1940 }
1941
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001942 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001943
1944 // The boundary should be twice as far from the origin now.
1945 // The pixels from the last test should all be child now
1946 {
1947 ScreenCapture::captureScreen(&mCapture);
1948 mCapture->expectChildColor(9, 9);
1949 mCapture->expectChildColor(10, 10);
1950 mCapture->expectChildColor(19, 19);
1951 mCapture->expectFGColor(20, 20);
1952 }
1953}
Robert Carr9524cb32017-02-13 11:32:32 -08001954
Robert Carr6452f122017-03-21 10:41:29 -07001955TEST_F(ChildLayerTest, ChildLayerAlpha) {
1956 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1957 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1958 fillSurfaceRGBA8(mChild, 0, 254, 0);
1959 waitForPostedBuffers();
1960
Robert Carr4cdc58f2017-08-23 14:22:20 -07001961 asTransaction([&](Transaction& t) {
1962 t.show(mChild);
1963 t.setPosition(mChild, 0, 0);
1964 t.setPosition(mFGSurfaceControl, 0, 0);
1965 });
Robert Carr6452f122017-03-21 10:41:29 -07001966
1967 {
1968 ScreenCapture::captureScreen(&mCapture);
1969 // Unblended child color
1970 mCapture->checkPixel(0, 0, 0, 254, 0);
1971 }
1972
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001973 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001974
1975 {
1976 ScreenCapture::captureScreen(&mCapture);
1977 // Child and BG blended.
1978 mCapture->checkPixel(0, 0, 127, 127, 0);
1979 }
1980
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001981 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001982
1983 {
1984 ScreenCapture::captureScreen(&mCapture);
1985 // Child and BG blended.
1986 mCapture->checkPixel(0, 0, 95, 64, 95);
1987 }
1988}
1989
Robert Carr9524cb32017-02-13 11:32:32 -08001990TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001991 asTransaction([&](Transaction& t) {
1992 t.show(mChild);
1993 t.setPosition(mChild, 10, 10);
1994 t.setPosition(mFGSurfaceControl, 64, 64);
1995 });
Robert Carr9524cb32017-02-13 11:32:32 -08001996
1997 {
1998 ScreenCapture::captureScreen(&mCapture);
1999 // Top left of foreground must now be visible
2000 mCapture->expectFGColor(64, 64);
2001 // But 10 pixels in we should see the child surface
2002 mCapture->expectChildColor(74, 74);
2003 // And 10 more pixels we should be back to the foreground surface
2004 mCapture->expectFGColor(84, 84);
2005 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002006
2007 asTransaction([&](Transaction& t) {
2008 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2009 });
2010
Robert Carr9524cb32017-02-13 11:32:32 -08002011 {
2012 ScreenCapture::captureScreen(&mCapture);
2013 mCapture->expectFGColor(64, 64);
2014 // In reparenting we should have exposed the entire foreground surface.
2015 mCapture->expectFGColor(74, 74);
2016 // And the child layer should now begin at 10, 10 (since the BG
2017 // layer is at (0, 0)).
2018 mCapture->expectBGColor(9, 9);
2019 mCapture->expectChildColor(10, 10);
2020 }
2021}
2022
chaviw161410b02017-07-27 10:46:08 -07002023TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002024 asTransaction([&](Transaction& t) {
2025 t.show(mChild);
2026 t.setPosition(mChild, 10, 10);
2027 t.setPosition(mFGSurfaceControl, 64, 64);
2028 });
Robert Carr9524cb32017-02-13 11:32:32 -08002029
2030 {
2031 ScreenCapture::captureScreen(&mCapture);
2032 // Top left of foreground must now be visible
2033 mCapture->expectFGColor(64, 64);
2034 // But 10 pixels in we should see the child surface
2035 mCapture->expectChildColor(74, 74);
2036 // And 10 more pixels we should be back to the foreground surface
2037 mCapture->expectFGColor(84, 84);
2038 }
2039
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002040 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002041
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002042 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002043
chaviw161410b02017-07-27 10:46:08 -07002044 // Since the child has the same client as the parent, it will not get
2045 // detached and will be hidden.
2046 {
2047 ScreenCapture::captureScreen(&mCapture);
2048 mCapture->expectFGColor(64, 64);
2049 mCapture->expectFGColor(74, 74);
2050 mCapture->expectFGColor(84, 84);
2051 }
2052}
2053
2054TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2055 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002056 sp<SurfaceControl> mChildNewClient =
2057 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2058 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002059
Peiyong Lin566a3b42018-01-09 18:22:43 -08002060 ASSERT_TRUE(mChildNewClient != nullptr);
chaviw161410b02017-07-27 10:46:08 -07002061 ASSERT_TRUE(mChildNewClient->isValid());
2062
2063 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2064
Robert Carr4cdc58f2017-08-23 14:22:20 -07002065 asTransaction([&](Transaction& t) {
2066 t.hide(mChild);
2067 t.show(mChildNewClient);
2068 t.setPosition(mChildNewClient, 10, 10);
2069 t.setPosition(mFGSurfaceControl, 64, 64);
2070 });
chaviw161410b02017-07-27 10:46:08 -07002071
2072 {
2073 ScreenCapture::captureScreen(&mCapture);
2074 // Top left of foreground must now be visible
2075 mCapture->expectFGColor(64, 64);
2076 // But 10 pixels in we should see the child surface
2077 mCapture->expectChildColor(74, 74);
2078 // And 10 more pixels we should be back to the foreground surface
2079 mCapture->expectFGColor(84, 84);
2080 }
2081
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002082 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002083
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002084 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002085
Robert Carr9524cb32017-02-13 11:32:32 -08002086 // Nothing should have changed.
2087 {
2088 ScreenCapture::captureScreen(&mCapture);
2089 mCapture->expectFGColor(64, 64);
2090 mCapture->expectChildColor(74, 74);
2091 mCapture->expectFGColor(84, 84);
2092 }
2093}
2094
Robert Carr9b429f42017-04-17 14:56:57 -07002095TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002096 asTransaction([&](Transaction& t) {
2097 t.show(mChild);
2098 t.setPosition(mChild, 0, 0);
2099 t.setPosition(mFGSurfaceControl, 0, 0);
2100 });
Robert Carr9b429f42017-04-17 14:56:57 -07002101
2102 {
2103 ScreenCapture::captureScreen(&mCapture);
2104 // We've positioned the child in the top left.
2105 mCapture->expectChildColor(0, 0);
2106 // But it's only 10x10.
2107 mCapture->expectFGColor(10, 10);
2108 }
2109
Robert Carr4cdc58f2017-08-23 14:22:20 -07002110 asTransaction([&](Transaction& t) {
2111 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2112 // We cause scaling by 2.
2113 t.setSize(mFGSurfaceControl, 128, 128);
2114 });
Robert Carr9b429f42017-04-17 14:56:57 -07002115
2116 {
2117 ScreenCapture::captureScreen(&mCapture);
2118 // We've positioned the child in the top left.
2119 mCapture->expectChildColor(0, 0);
2120 mCapture->expectChildColor(10, 10);
2121 mCapture->expectChildColor(19, 19);
2122 // And now it should be scaled all the way to 20x20
2123 mCapture->expectFGColor(20, 20);
2124 }
2125}
2126
Robert Carr1725eee2017-04-26 18:32:15 -07002127// Regression test for b/37673612
2128TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002129 asTransaction([&](Transaction& t) {
2130 t.show(mChild);
2131 t.setPosition(mChild, 0, 0);
2132 t.setPosition(mFGSurfaceControl, 0, 0);
2133 });
Robert Carr1725eee2017-04-26 18:32:15 -07002134
2135 {
2136 ScreenCapture::captureScreen(&mCapture);
2137 // We've positioned the child in the top left.
2138 mCapture->expectChildColor(0, 0);
2139 // But it's only 10x10.
2140 mCapture->expectFGColor(10, 10);
2141 }
Robert Carr1725eee2017-04-26 18:32:15 -07002142 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2143 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002144 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002145 sp<Surface> s = mFGSurfaceControl->getSurface();
2146 auto anw = static_cast<ANativeWindow*>(s.get());
2147 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2148 native_window_set_buffers_dimensions(anw, 64, 128);
2149 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2150 waitForPostedBuffers();
2151
2152 {
2153 // The child should still be in the same place and not have any strange scaling as in
2154 // b/37673612.
2155 ScreenCapture::captureScreen(&mCapture);
2156 mCapture->expectChildColor(0, 0);
2157 mCapture->expectFGColor(10, 10);
2158 }
2159}
2160
Dan Stoza412903f2017-04-27 13:42:17 -07002161TEST_F(ChildLayerTest, Bug36858924) {
2162 // Destroy the child layer
2163 mChild.clear();
2164
2165 // Now recreate it as hidden
2166 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2167 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2168 mFGSurfaceControl.get());
2169
2170 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002171 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002172 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
2173 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002174 t.show(mChild);
2175 });
Dan Stoza412903f2017-04-27 13:42:17 -07002176
2177 // Render the foreground surface a few times
2178 //
2179 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2180 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2181 // never acquire/release the first buffer
2182 ALOGI("Filling 1");
2183 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2184 ALOGI("Filling 2");
2185 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2186 ALOGI("Filling 3");
2187 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2188 ALOGI("Filling 4");
2189 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2190}
2191
chaviwf1961f72017-09-18 16:41:07 -07002192TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002193 asTransaction([&](Transaction& t) {
2194 t.show(mChild);
2195 t.setPosition(mChild, 10, 10);
2196 t.setPosition(mFGSurfaceControl, 64, 64);
2197 });
chaviw06178942017-07-27 10:25:59 -07002198
2199 {
2200 ScreenCapture::captureScreen(&mCapture);
2201 // Top left of foreground must now be visible
2202 mCapture->expectFGColor(64, 64);
2203 // But 10 pixels in we should see the child surface
2204 mCapture->expectChildColor(74, 74);
2205 // And 10 more pixels we should be back to the foreground surface
2206 mCapture->expectFGColor(84, 84);
2207 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002208
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002209 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002210
chaviw06178942017-07-27 10:25:59 -07002211 {
2212 ScreenCapture::captureScreen(&mCapture);
2213 mCapture->expectFGColor(64, 64);
2214 // In reparenting we should have exposed the entire foreground surface.
2215 mCapture->expectFGColor(74, 74);
2216 // And the child layer should now begin at 10, 10 (since the BG
2217 // layer is at (0, 0)).
2218 mCapture->expectBGColor(9, 9);
2219 mCapture->expectChildColor(10, 10);
2220 }
2221}
2222
chaviwf1961f72017-09-18 16:41:07 -07002223TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002224 asTransaction([&](Transaction& t) {
2225 t.show(mChild);
2226 t.setPosition(mChild, 10, 10);
2227 t.setPosition(mFGSurfaceControl, 64, 64);
2228 });
chaviwf1961f72017-09-18 16:41:07 -07002229
2230 {
2231 ScreenCapture::captureScreen(&mCapture);
2232 // Top left of foreground must now be visible
2233 mCapture->expectFGColor(64, 64);
2234 // But 10 pixels in we should see the child surface
2235 mCapture->expectChildColor(74, 74);
2236 // And 10 more pixels we should be back to the foreground surface
2237 mCapture->expectFGColor(84, 84);
2238 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002239 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002240 {
2241 ScreenCapture::captureScreen(&mCapture);
2242 // Nothing should have changed.
2243 mCapture->expectFGColor(64, 64);
2244 mCapture->expectChildColor(74, 74);
2245 mCapture->expectFGColor(84, 84);
2246 }
2247}
2248
2249TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002250 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2251 PIXEL_FORMAT_RGBA_8888, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08002252 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07002253 ASSERT_TRUE(newSurface->isValid());
2254
2255 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002256 asTransaction([&](Transaction& t) {
2257 t.hide(mChild);
2258 t.show(newSurface);
2259 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002260 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002261 t.setPosition(mFGSurfaceControl, 64, 64);
2262 });
chaviwf1961f72017-09-18 16:41:07 -07002263
2264 {
2265 ScreenCapture::captureScreen(&mCapture);
2266 // Top left of foreground must now be visible
2267 mCapture->expectFGColor(64, 64);
2268 // At 10, 10 we should see the new surface
2269 mCapture->checkPixel(10, 10, 63, 195, 63);
2270 }
2271
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002272 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002273
2274 {
2275 ScreenCapture::captureScreen(&mCapture);
2276 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2277 // mFGSurface, putting it at 74, 74.
2278 mCapture->expectFGColor(64, 64);
2279 mCapture->checkPixel(74, 74, 63, 195, 63);
2280 mCapture->expectFGColor(84, 84);
2281 }
2282}
2283
chaviwc9674332017-08-28 12:32:18 -07002284TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002285 sp<SurfaceControl> grandchild =
2286 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2287 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002288 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2289
2290 {
2291 ScreenCapture::captureScreen(&mCapture);
2292 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2293 // which begins at 64, 64
2294 mCapture->checkPixel(64, 64, 50, 50, 50);
2295 }
2296}
2297
Robert Carr503c7042017-09-27 15:06:08 -07002298TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002299 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2300 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002301 fillSurfaceRGBA8(relative, 255, 255, 255);
2302
2303 Transaction t;
2304 t.setLayer(relative, INT32_MAX)
2305 .setRelativeLayer(mChild, relative->getHandle(), 1)
2306 .setPosition(mFGSurfaceControl, 0, 0)
2307 .apply(true);
2308
2309 // We expect that the child should have been elevated above our
2310 // INT_MAX layer even though it's not a child of it.
2311 {
2312 ScreenCapture::captureScreen(&mCapture);
2313 mCapture->expectChildColor(0, 0);
2314 mCapture->expectChildColor(9, 9);
2315 mCapture->checkPixel(10, 10, 255, 255, 255);
2316 }
2317}
2318
chaviwa76b2712017-09-20 12:02:26 -07002319class ScreenCaptureTest : public LayerUpdateTest {
2320protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002321 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07002322};
2323
2324TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2325 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002326 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002327 mCapture->expectBGColor(0, 0);
2328 // Doesn't capture FG layer which is at 64, 64
2329 mCapture->expectBGColor(64, 64);
2330}
2331
2332TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2333 auto fgHandle = mFGSurfaceControl->getHandle();
2334
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002335 sp<SurfaceControl> child =
2336 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2337 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002338 fillSurfaceRGBA8(child, 200, 200, 200);
2339
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002340 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002341
2342 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002343 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002344 mCapture->expectFGColor(10, 10);
2345 mCapture->expectChildColor(0, 0);
2346}
2347
Robert Carr578038f2018-03-09 12:25:24 -08002348TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
2349 auto fgHandle = mFGSurfaceControl->getHandle();
2350
2351 sp<SurfaceControl> child =
2352 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2353 0, mFGSurfaceControl.get());
2354 fillSurfaceRGBA8(child, 200, 200, 200);
2355
2356 SurfaceComposerClient::Transaction().show(child).apply(true);
2357
2358 // Captures mFGSurfaceControl's child
2359 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2360 mCapture->checkPixel(10, 10, 0, 0, 0);
2361 mCapture->expectChildColor(0, 0);
2362}
2363
chaviw50da5042018-04-09 13:49:37 -07002364TEST_F(ScreenCaptureTest, CaptureTransparent) {
2365 sp<SurfaceControl> child =
2366 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2367 0, mFGSurfaceControl.get());
2368
2369 fillSurfaceRGBA8(child, 200, 200, 200);
2370
2371 SurfaceComposerClient::Transaction().show(child).apply(true);
2372
2373 auto childHandle = child->getHandle();
2374
2375 // Captures child
2376 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
2377 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
2378 // Area outside of child's bounds is transparent.
2379 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
2380}
2381
chaviw4b129c22018-04-09 16:19:43 -07002382TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
2383 auto fgHandle = mFGSurfaceControl->getHandle();
2384
2385 sp<SurfaceControl> child =
2386 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2387 0, mFGSurfaceControl.get());
2388 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 10,
2389 10, PIXEL_FORMAT_RGBA_8888, 0);
2390 fillSurfaceRGBA8(child, 200, 200, 200);
2391 fillSurfaceRGBA8(relative, 100, 100, 100);
2392
2393 SurfaceComposerClient::Transaction()
2394 .show(child)
2395 // Set relative layer above fg layer so should be shown above when computing all layers.
2396 .setRelativeLayer(relative, fgHandle, 1)
2397 .show(relative)
2398 .apply(true);
2399
2400 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
2401 ScreenCapture::captureLayers(&mCapture, fgHandle);
2402 mCapture->expectFGColor(10, 10);
2403 mCapture->expectChildColor(0, 0);
2404}
2405
2406TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
2407 auto fgHandle = mFGSurfaceControl->getHandle();
2408
2409 sp<SurfaceControl> child =
2410 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2411 0, mFGSurfaceControl.get());
2412 sp<SurfaceControl> relative =
2413 mComposerClient->createSurface(String8("Relative surface"), 10, 10,
2414 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2415 fillSurfaceRGBA8(child, 200, 200, 200);
2416 fillSurfaceRGBA8(relative, 100, 100, 100);
2417
2418 SurfaceComposerClient::Transaction()
2419 .show(child)
2420 // Set relative layer below fg layer but relative to child layer so it should be shown
2421 // above child layer.
2422 .setLayer(relative, -1)
2423 .setRelativeLayer(relative, child->getHandle(), 1)
2424 .show(relative)
2425 .apply(true);
2426
2427 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
2428 // relative value should be taken into account, placing it above child layer.
2429 ScreenCapture::captureLayers(&mCapture, fgHandle);
2430 mCapture->expectFGColor(10, 10);
2431 // Relative layer is showing on top of child layer
2432 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
2433}
Robert Carr578038f2018-03-09 12:25:24 -08002434
2435// In the following tests we verify successful skipping of a parent layer,
2436// so we use the same verification logic and only change how we mutate
2437// the parent layer to verify that various properties are ignored.
2438class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
2439public:
2440 void SetUp() override {
2441 LayerUpdateTest::SetUp();
2442
2443 mChild =
2444 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2445 0, mFGSurfaceControl.get());
2446 fillSurfaceRGBA8(mChild, 200, 200, 200);
2447
2448 SurfaceComposerClient::Transaction().show(mChild).apply(true);
2449 }
2450
2451 void verify() {
2452 auto fgHandle = mFGSurfaceControl->getHandle();
2453 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2454 mCapture->checkPixel(10, 10, 0, 0, 0);
2455 mCapture->expectChildColor(0, 0);
2456 }
2457
2458 std::unique_ptr<ScreenCapture> mCapture;
2459 sp<SurfaceControl> mChild;
2460};
2461
2462TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
2463
2464 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2465
2466 // Even though the parent is hidden we should still capture the child.
2467 verify();
2468}
2469
2470TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002471 SurfaceComposerClient::Transaction()
2472 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
2473 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08002474
2475 // Even though the parent is cropped out we should still capture the child.
2476 verify();
2477}
2478
2479TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
2480
2481 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
2482
2483 // We should not inherit the parent scaling.
2484 verify();
2485}
2486
Robert Carr15eae092018-03-23 13:43:53 -07002487TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
2488 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2489
2490 // Even though the parent is hidden we should still capture the child.
2491 verify();
2492
2493 // Verify everything was properly hidden when rendering the full-screen.
2494 screenshot()->expectBGColor(0,0);
2495}
2496
2497
chaviwa76b2712017-09-20 12:02:26 -07002498TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2499 auto fgHandle = mFGSurfaceControl->getHandle();
2500
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002501 sp<SurfaceControl> child =
2502 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2503 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002504 fillSurfaceRGBA8(child, 200, 200, 200);
2505
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002506 sp<SurfaceControl> grandchild =
2507 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2508 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002509
2510 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2511 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002512 .show(child)
2513 .setPosition(grandchild, 5, 5)
2514 .show(grandchild)
2515 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002516
2517 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002518 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07002519 mCapture->expectFGColor(10, 10);
2520 mCapture->expectChildColor(0, 0);
2521 mCapture->checkPixel(5, 5, 50, 50, 50);
2522}
2523
2524TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002525 sp<SurfaceControl> child =
2526 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2527 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002528 fillSurfaceRGBA8(child, 200, 200, 200);
2529 auto childHandle = child->getHandle();
2530
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002531 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002532
2533 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002534 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07002535 mCapture->expectChildColor(0, 0);
2536 mCapture->expectChildColor(9, 9);
2537}
2538
2539TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002540 sp<SurfaceControl> child =
2541 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2542 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002543 fillSurfaceRGBA8(child, 200, 200, 200);
2544 auto childHandle = child->getHandle();
2545
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002546 sp<SurfaceControl> grandchild =
2547 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2548 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002549 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2550
2551 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002552 .show(child)
2553 .setPosition(grandchild, 5, 5)
2554 .show(grandchild)
2555 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002556
2557 auto grandchildHandle = grandchild->getHandle();
2558
2559 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002560 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07002561 mCapture->checkPixel(0, 0, 50, 50, 50);
2562 mCapture->checkPixel(4, 4, 50, 50, 50);
2563}
2564
chaviw7206d492017-11-10 16:16:12 -08002565TEST_F(ScreenCaptureTest, CaptureCrop) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002566 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2567 PIXEL_FORMAT_RGBA_8888, 0);
2568 sp<SurfaceControl> blueLayer =
2569 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2570 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002571
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002572 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2573 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002574
2575 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002576 .setLayer(redLayer, INT32_MAX - 1)
2577 .show(redLayer)
2578 .show(blueLayer)
2579 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002580
2581 auto redLayerHandle = redLayer->getHandle();
2582
2583 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002584 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2585 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2586 // red area below the blue area
2587 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2588 // red area to the right of the blue area
2589 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002590
2591 Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002592 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08002593 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
2594 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002595 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08002596 mCapture->checkPixel(30, 30, 0, 0, 0);
2597}
2598
2599TEST_F(ScreenCaptureTest, CaptureSize) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002600 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2601 PIXEL_FORMAT_RGBA_8888, 0);
2602 sp<SurfaceControl> blueLayer =
2603 mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2604 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08002605
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002606 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2607 ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
chaviw7206d492017-11-10 16:16:12 -08002608
2609 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002610 .setLayer(redLayer, INT32_MAX - 1)
2611 .show(redLayer)
2612 .show(blueLayer)
2613 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08002614
2615 auto redLayerHandle = redLayer->getHandle();
2616
2617 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002618 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2619 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2620 // red area below the blue area
2621 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2622 // red area to the right of the blue area
2623 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002624
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002625 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08002626 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002627 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
2628 // red area below the blue area
2629 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
2630 // red area to the right of the blue area
2631 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08002632 mCapture->checkPixel(30, 30, 0, 0, 0);
2633}
2634
2635TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002636 sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2637 PIXEL_FORMAT_RGBA_8888, 0);
chaviw7206d492017-11-10 16:16:12 -08002638
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002639 ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
chaviw7206d492017-11-10 16:16:12 -08002640
2641 auto redLayerHandle = redLayer->getHandle();
2642 mComposerClient->destroySurface(redLayerHandle);
2643 SurfaceComposerClient::Transaction().apply(true);
2644
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002645 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08002646
2647 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002648 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2649 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08002650}
2651
chaviw8e3fe5d2018-02-22 10:55:42 -08002652
2653class DereferenceSurfaceControlTest : public LayerTransactionTest {
2654protected:
2655 void SetUp() override {
2656 LayerTransactionTest::SetUp();
2657 bgLayer = createLayer("BG layer", 20, 20);
2658 fillLayerColor(bgLayer, Color::RED);
2659 fgLayer = createLayer("FG layer", 20, 20);
2660 fillLayerColor(fgLayer, Color::BLUE);
2661 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
2662 {
2663 SCOPED_TRACE("before anything");
2664 auto shot = screenshot();
2665 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2666 }
2667 }
2668 void TearDown() override {
2669 LayerTransactionTest::TearDown();
2670 bgLayer = 0;
2671 fgLayer = 0;
2672 }
2673
2674 sp<SurfaceControl> bgLayer;
2675 sp<SurfaceControl> fgLayer;
2676};
2677
2678TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
2679 fgLayer = nullptr;
2680 {
2681 SCOPED_TRACE("after setting null");
2682 auto shot = screenshot();
2683 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
2684 }
2685}
2686
2687TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
2688 auto transaction = Transaction().show(fgLayer);
2689 fgLayer = nullptr;
2690 {
2691 SCOPED_TRACE("after setting null");
2692 auto shot = screenshot();
2693 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2694 }
2695}
2696
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002697} // namespace android