blob: 39aef8164c5bf81b18ccfa363db30213ecf87820 [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.
107void expectBufferColor(const CpuConsumer::LockedBuffer& buffer, const Rect& rect,
108 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
114 if (x + width > int32_t(buffer.width)) {
115 x = std::min(x, int32_t(buffer.width));
116 width = buffer.width - x;
117 }
118 if (y + height > int32_t(buffer.height)) {
119 y = std::min(y, int32_t(buffer.height));
120 height = buffer.height - y;
121 }
122
123 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
124 uint8_t tmp = a >= b ? a - b : b - a;
125 return tmp <= tolerance;
126 };
127 for (int32_t j = 0; j < height; j++) {
128 const uint8_t* src =
129 static_cast<const uint8_t*>(buffer.data) + (buffer.stride * (y + j) + x) * 4;
130 for (int32_t i = 0; i < width; i++) {
131 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
132 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
133 << "pixel @ (" << x + i << ", " << y + j << "): "
134 << "expected (" << color << "), "
135 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
136 src += 4;
137 }
138 }
139}
140
141} // anonymous namespace
142
Robert Carr4cdc58f2017-08-23 14:22:20 -0700143using Transaction = SurfaceComposerClient::Transaction;
144
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700145// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700146static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
147 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800148 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700149 sp<Surface> s = sc->getSurface();
150 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800151 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
152 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700153 for (int y = 0; y < outBuffer.height; y++) {
154 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700155 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700156 pixel[0] = r;
157 pixel[1] = g;
158 pixel[2] = b;
159 pixel[3] = 255;
160 }
161 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700162 if (unlock) {
163 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
164 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700165}
166
167// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
168// individual pixel values for testing purposes.
169class ScreenCapture : public RefBase {
170public:
Chia-I Wu718daf82017-10-20 11:57:17 -0700171 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
172 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700173 sp<IGraphicBufferProducer> producer;
174 sp<IGraphicBufferConsumer> consumer;
175 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -0700176 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700177 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700178 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700179 SurfaceComposerClient::Transaction().apply(true);
180
Chia-I Wu718daf82017-10-20 11:57:17 -0700181 ASSERT_EQ(NO_ERROR,
182 sf->captureScreen(display, producer, Rect(), 0, 0, minLayerZ, maxLayerZ, false));
Michael Lentine5a16a622015-05-21 13:48:24 -0700183 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700184 }
185
Chia-I Wu718daf82017-10-20 11:57:17 -0700186 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
187 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
188 expectBufferColor(mBuf, rect, color, tolerance);
189 }
190
191 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
192 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
193 const bool leftBorder = rect.left > 0;
194 const bool topBorder = rect.top > 0;
195 const bool rightBorder = rect.right < int32_t(mBuf.width);
196 const bool bottomBorder = rect.bottom < int32_t(mBuf.height);
197
198 if (topBorder) {
199 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
200 if (leftBorder) {
201 top.left -= 1;
202 }
203 if (rightBorder) {
204 top.right += 1;
205 }
206 expectColor(top, color, tolerance);
207 }
208 if (leftBorder) {
209 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
210 expectColor(left, color, tolerance);
211 }
212 if (rightBorder) {
213 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
214 expectColor(right, color, tolerance);
215 }
216 if (bottomBorder) {
217 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
218 if (leftBorder) {
219 bottom.left -= 1;
220 }
221 if (rightBorder) {
222 bottom.right += 1;
223 }
224 expectColor(bottom, color, tolerance);
225 }
226 }
227
Chia-I Wu93853fe2017-11-02 08:30:27 -0700228 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
229 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
230 uint8_t tolerance = 0) {
231 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
232
233 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
234 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
235 // avoid checking borders due to unspecified filtering behavior
236 const int32_t offsetX = filtered ? 2 : 0;
237 const int32_t offsetY = filtered ? 2 : 0;
238 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
239 tolerance);
240 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
241 tolerance);
242 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
243 tolerance);
244 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
245 bottomRight, tolerance);
246 }
247
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700248 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700249 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
250 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
251 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700252 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
253 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700254 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
255 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700256 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700257 }
258 }
259
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700260 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700261
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700262 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700263
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700264 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700265
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700266private:
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700267 ScreenCapture(const sp<CpuConsumer>& cc) : mCC(cc) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700268 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
269 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700270
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700271 ~ScreenCapture() { mCC->unlockBuffer(mBuf); }
Michael Lentine5a16a622015-05-21 13:48:24 -0700272
273 sp<CpuConsumer> mCC;
274 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700275};
276
chaviwa76b2712017-09-20 12:02:26 -0700277class CaptureLayer {
278public:
chaviw7206d492017-11-10 16:16:12 -0800279 static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle,
280 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
chaviwa76b2712017-09-20 12:02:26 -0700281 sp<IGraphicBufferProducer> producer;
282 sp<IGraphicBufferConsumer> consumer;
283 BufferQueue::createBufferQueue(&producer, &consumer);
284 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
285 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
chaviwa76b2712017-09-20 12:02:26 -0700286 SurfaceComposerClient::Transaction().apply(true);
chaviw7206d492017-11-10 16:16:12 -0800287 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer, crop, frameScale));
chaviwa76b2712017-09-20 12:02:26 -0700288 *sc = std::make_unique<CaptureLayer>(cpuConsumer);
289 }
290
291 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
292 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuffer.format);
293 const uint8_t* img = static_cast<const uint8_t*>(mBuffer.data);
294 const uint8_t* pixel = img + (4 * (y * mBuffer.stride + x));
295 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
296 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700297 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
chaviwa76b2712017-09-20 12:02:26 -0700298 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
299 EXPECT_EQ(String8(), err) << err.string();
300 }
301 }
302
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700303 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
chaviwa76b2712017-09-20 12:02:26 -0700304
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700305 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
chaviwa76b2712017-09-20 12:02:26 -0700306
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700307 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
chaviwa76b2712017-09-20 12:02:26 -0700308
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700309 CaptureLayer(const sp<CpuConsumer>& cc) : mCC(cc) {
chaviwa76b2712017-09-20 12:02:26 -0700310 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuffer));
311 }
312
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700313 ~CaptureLayer() { mCC->unlockBuffer(mBuffer); }
chaviwa76b2712017-09-20 12:02:26 -0700314
315private:
316 sp<CpuConsumer> mCC;
317 CpuConsumer::LockedBuffer mBuffer;
318};
319
Chia-I Wu718daf82017-10-20 11:57:17 -0700320class LayerTransactionTest : public ::testing::Test {
321protected:
322 void SetUp() override {
323 mClient = new SurfaceComposerClient;
324 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
325
326 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
327 }
328
329 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
330 uint32_t flags = 0) {
331 auto layer =
332 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
333 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
334
335 status_t error = Transaction()
336 .setLayerStack(layer, mDisplayLayerStack)
337 .setLayer(layer, mLayerZBase)
338 .apply();
339 if (error != NO_ERROR) {
340 ADD_FAILURE() << "failed to initialize SurfaceControl";
341 layer.clear();
342 }
343
344 return layer;
345 }
346
347 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
348 // wait for previous transactions (such as setSize) to complete
349 Transaction().apply(true);
350
351 ANativeWindow_Buffer buffer = {};
352 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
353
354 return buffer;
355 }
356
357 void postLayerBuffer(const sp<SurfaceControl>& layer) {
358 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
359
360 // wait for the newly posted buffer to be latched
361 waitForLayerBuffers();
362 }
363
364 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
365 ANativeWindow_Buffer buffer;
366 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
367 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
368 postLayerBuffer(layer);
369 }
370
Chia-I Wu93853fe2017-11-02 08:30:27 -0700371 void fillLayerQuadrant(const sp<SurfaceControl>& layer, const Color& topLeft,
372 const Color& topRight, const Color& bottomLeft,
373 const Color& bottomRight) {
374 ANativeWindow_Buffer buffer;
375 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
376 ASSERT_TRUE(buffer.width % 2 == 0 && buffer.height % 2 == 0);
377
378 const int32_t halfW = buffer.width / 2;
379 const int32_t halfH = buffer.height / 2;
380 fillBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
381 fillBufferColor(buffer, Rect(halfW, 0, buffer.width, halfH), topRight);
382 fillBufferColor(buffer, Rect(0, halfH, halfW, buffer.height), bottomLeft);
383 fillBufferColor(buffer, Rect(halfW, halfH, buffer.width, buffer.height), bottomRight);
384
385 postLayerBuffer(layer);
386 }
387
Chia-I Wu718daf82017-10-20 11:57:17 -0700388 sp<ScreenCapture> screenshot() {
389 sp<ScreenCapture> screenshot;
390 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
391 return screenshot;
392 }
393
394 sp<SurfaceComposerClient> mClient;
395
396 sp<IBinder> mDisplay;
397 uint32_t mDisplayWidth;
398 uint32_t mDisplayHeight;
399 uint32_t mDisplayLayerStack;
400
401 // leave room for ~256 layers
402 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
403
404private:
405 void SetUpDisplay() {
406 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
407 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
408
409 // get display width/height
410 DisplayInfo info;
411 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
412 mDisplayWidth = info.w;
413 mDisplayHeight = info.h;
414
415 // After a new buffer is queued, SurfaceFlinger is notified and will
416 // latch the new buffer on next vsync. Let's heuristically wait for 3
417 // vsyncs.
418 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
419
420 mDisplayLayerStack = 0;
421 // set layer stack (b/68888219)
422 Transaction t;
423 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
424 t.apply();
425 }
426
427 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
428
429 int32_t mBufferPostDelay;
430};
431
432TEST_F(LayerTransactionTest, SetPositionBasic) {
433 sp<SurfaceControl> layer;
434 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
435 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
436
437 {
438 SCOPED_TRACE("default position");
439 auto shot = screenshot();
440 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
441 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
442 }
443
444 Transaction().setPosition(layer, 5, 10).apply();
445 {
446 SCOPED_TRACE("new position");
447 auto shot = screenshot();
448 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
449 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
450 }
451}
452
453TEST_F(LayerTransactionTest, SetPositionRounding) {
454 sp<SurfaceControl> layer;
455 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
456 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
457
458 // GLES requires only 4 bits of subpixel precision during rasterization
459 // XXX GLES composition does not match HWC composition due to precision
460 // loss (b/69315223)
461 const float epsilon = 1.0f / 16.0f;
462 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
463 {
464 SCOPED_TRACE("rounding down");
465 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
466 }
467
468 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
469 {
470 SCOPED_TRACE("rounding up");
471 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
472 }
473}
474
475TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
476 sp<SurfaceControl> layer;
477 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
478 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
479
480 Transaction().setPosition(layer, -32, -32).apply();
481 {
482 SCOPED_TRACE("negative coordinates");
483 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
484 }
485
486 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
487 {
488 SCOPED_TRACE("positive coordinates");
489 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
490 }
491}
492
493TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
494 sp<SurfaceControl> layer;
495 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
496 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
497
498 // partially out of bounds
499 Transaction().setPosition(layer, -30, -30).apply();
500 {
501 SCOPED_TRACE("negative coordinates");
502 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
503 }
504
505 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
506 {
507 SCOPED_TRACE("positive coordinates");
508 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
509 mDisplayHeight),
510 Color::RED);
511 }
512}
513
514TEST_F(LayerTransactionTest, SetPositionWithResize) {
515 sp<SurfaceControl> layer;
516 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
517 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
518
519 // setPosition is applied immediately by default, with or without resize
520 // pending
521 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
522 {
523 SCOPED_TRACE("resize pending");
524 auto shot = screenshot();
525 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
526 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
527 }
528
529 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
530 {
531 SCOPED_TRACE("resize applied");
532 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
533 }
534}
535
536TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
537 sp<SurfaceControl> layer;
538 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
539 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
540
541 // request setPosition to be applied with the next resize
542 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
543 {
544 SCOPED_TRACE("new position pending");
545 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
546 }
547
548 Transaction().setPosition(layer, 15, 20).apply();
549 {
550 SCOPED_TRACE("pending new position modified");
551 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
552 }
553
554 Transaction().setSize(layer, 64, 64).apply();
555 {
556 SCOPED_TRACE("resize pending");
557 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
558 }
559
560 // finally resize and latch the buffer
561 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
562 {
563 SCOPED_TRACE("new position applied");
564 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
565 }
566}
567
568TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
569 sp<SurfaceControl> layer;
570 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
571 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
572
573 // setPosition is not immediate even with SCALE_TO_WINDOW override
574 Transaction()
575 .setPosition(layer, 5, 10)
576 .setSize(layer, 64, 64)
577 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
578 .setGeometryAppliesWithResize(layer)
579 .apply();
580 {
581 SCOPED_TRACE("new position pending");
582 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
583 }
584
585 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
586 {
587 SCOPED_TRACE("new position applied");
588 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
589 }
590}
591
Chia-I Wu0eaea312017-10-31 10:14:40 -0700592TEST_F(LayerTransactionTest, SetSizeBasic) {
593 sp<SurfaceControl> layer;
594 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
595 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
596
597 Transaction().setSize(layer, 64, 64).apply();
598 {
599 SCOPED_TRACE("resize pending");
600 auto shot = screenshot();
601 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
602 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
603 }
604
605 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
606 {
607 SCOPED_TRACE("resize applied");
608 auto shot = screenshot();
609 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
610 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
611 }
612}
613
614TEST_F(LayerTransactionTest, SetSizeInvalid) {
615 // cannot test robustness against invalid sizes (zero or really huge)
616}
617
618TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
619 sp<SurfaceControl> layer;
620 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
621 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
622
623 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
624 Transaction()
625 .setSize(layer, 64, 64)
626 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
627 .apply();
628 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
629}
630
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700631TEST_F(LayerTransactionTest, SetZBasic) {
632 sp<SurfaceControl> layerR;
633 sp<SurfaceControl> layerG;
634 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
635 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
636 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
637 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
638
639 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
640 {
641 SCOPED_TRACE("layerR");
642 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
643 }
644
645 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
646 {
647 SCOPED_TRACE("layerG");
648 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
649 }
650}
651
652TEST_F(LayerTransactionTest, SetZNegative) {
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().setLayer(layerR, -1).setLayer(layerG, -2).apply();
661 {
662 SCOPED_TRACE("layerR");
663 sp<ScreenCapture> screenshot;
664 ScreenCapture::captureScreen(&screenshot, -2, -1);
665 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
666 }
667
668 Transaction().setLayer(layerR, -3).apply();
669 {
670 SCOPED_TRACE("layerG");
671 sp<ScreenCapture> screenshot;
672 ScreenCapture::captureScreen(&screenshot, -3, -1);
673 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
674 }
675}
676
Chia-I Wu49313302017-10-31 10:14:40 -0700677TEST_F(LayerTransactionTest, SetRelativeZBasic) {
678 sp<SurfaceControl> layerR;
679 sp<SurfaceControl> layerG;
680 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
681 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
682 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
683 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
684
685 Transaction()
686 .setPosition(layerG, 16, 16)
687 .setRelativeLayer(layerG, layerR->getHandle(), 1)
688 .apply();
689 {
690 SCOPED_TRACE("layerG above");
691 auto shot = screenshot();
692 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
693 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
694 }
695
696 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
697 {
698 SCOPED_TRACE("layerG below");
699 auto shot = screenshot();
700 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
701 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
702 }
703}
704
Chia-I Wuec2d9852017-11-21 09:21:01 -0800705TEST_F(LayerTransactionTest, SetRelativeZNegative) {
706 sp<SurfaceControl> layerR;
707 sp<SurfaceControl> layerG;
708 sp<SurfaceControl> layerB;
709 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
710 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
711 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
712 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
713 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
714 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
715
716 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
717 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
718
719 sp<ScreenCapture> screenshot;
720 // only layerB is in this range
721 ScreenCapture::captureScreen(&screenshot, -2, -1);
722 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
723}
724
Chia-I Wu49313302017-10-31 10:14:40 -0700725TEST_F(LayerTransactionTest, SetRelativeZGroup) {
726 sp<SurfaceControl> layerR;
727 sp<SurfaceControl> layerG;
728 sp<SurfaceControl> layerB;
729 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
730 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
731 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
732 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
733 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
734 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
735
736 // layerR = 0, layerG = layerR + 3, layerB = 2
737 Transaction()
738 .setPosition(layerG, 8, 8)
739 .setRelativeLayer(layerG, layerR->getHandle(), 3)
740 .setPosition(layerB, 16, 16)
741 .setLayer(layerB, mLayerZBase + 2)
742 .apply();
743 {
744 SCOPED_TRACE("(layerR < layerG) < layerB");
745 auto shot = screenshot();
746 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
747 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
748 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
749 }
750
751 // layerR = 4, layerG = layerR + 3, layerB = 2
752 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
753 {
754 SCOPED_TRACE("layerB < (layerR < layerG)");
755 auto shot = screenshot();
756 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
757 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
758 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
759 }
760
761 // layerR = 4, layerG = layerR - 3, layerB = 2
762 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
763 {
764 SCOPED_TRACE("layerB < (layerG < layerR)");
765 auto shot = screenshot();
766 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
767 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
768 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
769 }
770
771 // restore to absolute z
772 // layerR = 4, layerG = 0, layerB = 2
773 Transaction().setLayer(layerG, mLayerZBase).apply();
774 {
775 SCOPED_TRACE("layerG < layerB < layerR");
776 auto shot = screenshot();
777 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
778 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
779 }
780
781 // layerR should not affect layerG anymore
782 // layerR = 1, layerG = 0, layerB = 2
783 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
784 {
785 SCOPED_TRACE("layerG < layerR < layerB");
786 auto shot = screenshot();
787 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
788 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
789 }
790}
791
792TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
793 sp<SurfaceControl> layerR;
794 sp<SurfaceControl> layerG;
795
796 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
797 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
798 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
799 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
800
801 Transaction()
802 .setPosition(layerG, 16, 16)
803 .setRelativeLayer(layerG, layerR->getHandle(), 1)
804 .apply();
805
806 mClient->destroySurface(layerG->getHandle());
807 // layerG should have been removed
808 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
809}
810
Chia-I Wu57b27502017-10-31 10:14:40 -0700811TEST_F(LayerTransactionTest, SetFlagsHidden) {
812 sp<SurfaceControl> layer;
813 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
814 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
815
816 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
817 {
818 SCOPED_TRACE("layer hidden");
819 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
820 }
821
822 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
823 {
824 SCOPED_TRACE("layer shown");
825 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
826 }
827}
828
829TEST_F(LayerTransactionTest, SetFlagsOpaque) {
830 const Color translucentRed = {100, 0, 0, 100};
831 sp<SurfaceControl> layerR;
832 sp<SurfaceControl> layerG;
833 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
834 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
835 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
836 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
837
838 Transaction()
839 .setLayer(layerR, mLayerZBase + 1)
840 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
841 .apply();
842 {
843 SCOPED_TRACE("layerR opaque");
844 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
845 }
846
847 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
848 {
849 SCOPED_TRACE("layerR translucent");
850 const uint8_t g = uint8_t(255 - translucentRed.a);
851 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
852 }
853}
854
855TEST_F(LayerTransactionTest, SetFlagsSecure) {
856 sp<SurfaceControl> layer;
857 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
858 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
859
860 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
861 sp<IGraphicBufferProducer> producer;
862 sp<IGraphicBufferConsumer> consumer;
863 BufferQueue::createBufferQueue(&producer, &consumer);
864 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
865
866 Transaction()
867 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
868 .apply(true);
869 ASSERT_EQ(PERMISSION_DENIED,
870 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
871 false));
872
873 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
874 ASSERT_EQ(NO_ERROR,
875 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
876 false));
877}
878
Chia-I Wu2113bdd2017-11-01 15:16:35 -0700879TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
880 const Rect top(0, 0, 32, 16);
881 const Rect bottom(0, 16, 32, 32);
882 sp<SurfaceControl> layer;
883 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
884
885 ANativeWindow_Buffer buffer;
886 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
887 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
888 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
889 // setTransparentRegionHint always applies to the following buffer
890 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
891 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
892 {
893 SCOPED_TRACE("top transparent");
894 auto shot = screenshot();
895 shot->expectColor(top, Color::BLACK);
896 shot->expectColor(bottom, Color::RED);
897 }
898
899 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
900 {
901 SCOPED_TRACE("transparent region hint pending");
902 auto shot = screenshot();
903 shot->expectColor(top, Color::BLACK);
904 shot->expectColor(bottom, Color::RED);
905 }
906
907 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
908 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
909 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
910 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
911 {
912 SCOPED_TRACE("bottom transparent");
913 auto shot = screenshot();
914 shot->expectColor(top, Color::RED);
915 shot->expectColor(bottom, Color::BLACK);
916 }
917}
918
919TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
920 sp<SurfaceControl> layerTransparent;
921 sp<SurfaceControl> layerR;
922 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
923 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
924
925 // check that transparent region hint is bound by the layer size
926 Transaction()
927 .setTransparentRegionHint(layerTransparent,
928 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
929 .setPosition(layerR, 16, 16)
930 .setLayer(layerR, mLayerZBase + 1)
931 .apply();
932 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
933 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
934 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
935}
936
Chia-I Wua8a515e2017-11-01 15:16:35 -0700937TEST_F(LayerTransactionTest, SetAlphaBasic) {
938 sp<SurfaceControl> layer1;
939 sp<SurfaceControl> layer2;
940 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
941 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
942 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
943 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
944
945 Transaction()
946 .setAlpha(layer1, 0.25f)
947 .setAlpha(layer2, 0.75f)
948 .setPosition(layer2, 16, 0)
949 .setLayer(layer2, mLayerZBase + 1)
950 .apply();
951 {
952 auto shot = screenshot();
953 uint8_t r = 16; // 64 * 0.25f
954 uint8_t g = 48; // 64 * 0.75f
955 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
956 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
957
958 r /= 4; // r * (1.0f - 0.75f)
959 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
960 }
961}
962
963TEST_F(LayerTransactionTest, SetAlphaClamped) {
964 const Color color = {64, 0, 0, 255};
965 sp<SurfaceControl> layer;
966 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
967 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
968
969 Transaction().setAlpha(layer, 2.0f).apply();
970 {
971 SCOPED_TRACE("clamped to 1.0f");
972 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
973 }
974
975 Transaction().setAlpha(layer, -1.0f).apply();
976 {
977 SCOPED_TRACE("clamped to 0.0f");
978 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
979 }
980}
981
Chia-I Wue4ef6102017-11-01 15:16:35 -0700982TEST_F(LayerTransactionTest, SetColorBasic) {
983 sp<SurfaceControl> bufferLayer;
984 sp<SurfaceControl> colorLayer;
985 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
986 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
987 ASSERT_NO_FATAL_FAILURE(
988 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
989
990 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
991 {
992 SCOPED_TRACE("default color");
993 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
994 }
995
996 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
997 const Color expected = {15, 51, 85, 255};
998 // this is handwavy, but the precison loss scaled by 255 (8-bit per
999 // channel) should be less than one
1000 const uint8_t tolerance = 1;
1001 Transaction().setColor(colorLayer, color).apply();
1002 {
1003 SCOPED_TRACE("new color");
1004 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1005 }
1006}
1007
1008TEST_F(LayerTransactionTest, SetColorClamped) {
1009 sp<SurfaceControl> colorLayer;
1010 ASSERT_NO_FATAL_FAILURE(
1011 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1012
1013 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
1014 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1015}
1016
1017TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1018 sp<SurfaceControl> bufferLayer;
1019 sp<SurfaceControl> colorLayer;
1020 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1021 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1022 ASSERT_NO_FATAL_FAILURE(
1023 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1024
1025 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1026 const float alpha = 0.25f;
1027 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1028 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1029 // channel) should be less than one
1030 const uint8_t tolerance = 1;
1031 Transaction()
1032 .setColor(colorLayer, color)
1033 .setAlpha(colorLayer, alpha)
1034 .setLayer(colorLayer, mLayerZBase + 1)
1035 .apply();
1036 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1037 tolerance);
1038}
1039
1040TEST_F(LayerTransactionTest, SetColorWithBuffer) {
1041 sp<SurfaceControl> bufferLayer;
1042 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1043 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1044
1045 // color is ignored
1046 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1047 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1048}
1049
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001050TEST_F(LayerTransactionTest, SetLayerStackBasic) {
1051 sp<SurfaceControl> layer;
1052 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1053 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1054
1055 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1056 {
1057 SCOPED_TRACE("non-existing layer stack");
1058 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1059 }
1060
1061 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1062 {
1063 SCOPED_TRACE("original layer stack");
1064 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1065 }
1066}
1067
Chia-I Wu93853fe2017-11-02 08:30:27 -07001068TEST_F(LayerTransactionTest, SetMatrixBasic) {
1069 sp<SurfaceControl> layer;
1070 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1071 ASSERT_NO_FATAL_FAILURE(
1072 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1073
1074 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1075 {
1076 SCOPED_TRACE("IDENTITY");
1077 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1078 Color::WHITE);
1079 }
1080
1081 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1082 {
1083 SCOPED_TRACE("FLIP_H");
1084 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1085 Color::BLUE);
1086 }
1087
1088 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1089 {
1090 SCOPED_TRACE("FLIP_V");
1091 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1092 Color::GREEN);
1093 }
1094
1095 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1096 {
1097 SCOPED_TRACE("ROT_90");
1098 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1099 Color::GREEN);
1100 }
1101
1102 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1103 {
1104 SCOPED_TRACE("SCALE");
1105 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1106 Color::WHITE, true /* filtered */);
1107 }
1108}
1109
1110TEST_F(LayerTransactionTest, SetMatrixRot45) {
1111 sp<SurfaceControl> layer;
1112 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1113 ASSERT_NO_FATAL_FAILURE(
1114 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1115
1116 const float rot = M_SQRT1_2; // 45 degrees
1117 const float trans = M_SQRT2 * 16.0f;
1118 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1119
1120 auto shot = screenshot();
1121 // check a 8x8 region inside each color
1122 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1123 const int32_t halfL = 4;
1124 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1125 };
1126 const int32_t unit = int32_t(trans / 2);
1127 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1128 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1129 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1130 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1131}
1132
1133TEST_F(LayerTransactionTest, SetMatrixWithResize) {
1134 sp<SurfaceControl> layer;
1135 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1136 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1137
1138 // setMatrix is applied after any pending resize, unlike setPosition
1139 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1140 {
1141 SCOPED_TRACE("resize pending");
1142 auto shot = screenshot();
1143 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1144 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1145 }
1146
1147 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1148 {
1149 SCOPED_TRACE("resize applied");
1150 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1151 }
1152}
1153
1154TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) {
1155 sp<SurfaceControl> layer;
1156 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1157 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1158
1159 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1160 Transaction()
1161 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1162 .setSize(layer, 64, 64)
1163 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1164 .apply();
1165 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1166}
1167
Chia-I Wua56b2042017-11-01 15:16:35 -07001168TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) {
1169 sp<SurfaceControl> layer;
1170 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1171 ASSERT_NO_FATAL_FAILURE(
1172 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1173
1174 // XXX SCALE_CROP is not respected; calling setSize and
1175 // setOverrideScalingMode in separate transactions does not work
1176 // (b/69315456)
1177 Transaction()
1178 .setSize(layer, 64, 16)
1179 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1180 .apply();
1181 {
1182 SCOPED_TRACE("SCALE_TO_WINDOW");
1183 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1184 Color::WHITE, true /* filtered */);
1185 }
1186}
1187
Chia-I Wu04dcca82017-11-02 08:30:27 -07001188TEST_F(LayerTransactionTest, SetCropBasic) {
1189 sp<SurfaceControl> layer;
1190 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1191 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1192 const Rect crop(8, 8, 24, 24);
1193
1194 Transaction().setCrop(layer, crop).apply();
1195 auto shot = screenshot();
1196 shot->expectColor(crop, Color::RED);
1197 shot->expectBorder(crop, Color::BLACK);
1198}
1199
1200TEST_F(LayerTransactionTest, SetCropEmpty) {
1201 sp<SurfaceControl> layer;
1202 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1203 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1204
1205 {
1206 SCOPED_TRACE("empty rect");
1207 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1208 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1209 }
1210
1211 {
1212 SCOPED_TRACE("negative rect");
1213 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1214 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1215 }
1216}
1217
1218TEST_F(LayerTransactionTest, SetCropOutOfBounds) {
1219 sp<SurfaceControl> layer;
1220 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1221 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1222
1223 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1224 auto shot = screenshot();
1225 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1226 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1227}
1228
1229TEST_F(LayerTransactionTest, SetCropWithTranslation) {
1230 sp<SurfaceControl> layer;
1231 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1232 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1233
1234 const Point position(32, 32);
1235 const Rect crop(8, 8, 24, 24);
1236 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1237 auto shot = screenshot();
1238 shot->expectColor(crop + position, Color::RED);
1239 shot->expectBorder(crop + position, Color::BLACK);
1240}
1241
1242TEST_F(LayerTransactionTest, SetCropWithScale) {
1243 sp<SurfaceControl> layer;
1244 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1245 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1246
1247 // crop is affected by matrix
1248 Transaction()
1249 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1250 .setCrop(layer, Rect(8, 8, 24, 24))
1251 .apply();
1252 auto shot = screenshot();
1253 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1254 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1255}
1256
1257TEST_F(LayerTransactionTest, SetCropWithResize) {
1258 sp<SurfaceControl> layer;
1259 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1260 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1261
1262 // setCrop is applied immediately by default, with or without resize pending
1263 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1264 {
1265 SCOPED_TRACE("resize pending");
1266 auto shot = screenshot();
1267 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1268 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1269 }
1270
1271 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1272 {
1273 SCOPED_TRACE("resize applied");
1274 auto shot = screenshot();
1275 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1276 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1277 }
1278}
1279
1280TEST_F(LayerTransactionTest, SetCropWithNextResize) {
1281 sp<SurfaceControl> layer;
1282 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1283 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1284
1285 // request setCrop to be applied with the next resize
1286 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1287 {
1288 SCOPED_TRACE("waiting for next resize");
1289 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1290 }
1291
1292 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1293 {
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
1319 // setCrop is not immediate even with SCALE_TO_WINDOW override
1320 Transaction()
1321 .setCrop(layer, Rect(4, 4, 12, 12))
1322 .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
1352 Transaction().setFinalCrop(layer, crop).apply();
1353 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");
1366 Transaction().setFinalCrop(layer, Rect(8, 8, 8, 8)).apply();
1367 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1368 }
1369
1370 {
1371 SCOPED_TRACE("negative rect");
1372 Transaction().setFinalCrop(layer, Rect(8, 8, 0, 0)).apply();
1373 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
1383 Transaction().setFinalCrop(layer, Rect(-128, -64, 128, 64)).apply();
1384 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
1395 Transaction().setPosition(layer, 16, 16).setFinalCrop(layer, Rect(8, 8, 24, 24)).apply();
1396 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)
1409 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1410 .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
1422 Transaction().setFinalCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1423 {
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()
1446 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1447 .setGeometryAppliesWithResize(layer)
1448 .apply();
1449 {
1450 SCOPED_TRACE("waiting for next resize");
1451 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1452 }
1453
1454 Transaction().setFinalCrop(layer, Rect(4, 4, 12, 12)).apply();
1455 {
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()
1483 .setFinalCrop(layer, Rect(4, 4, 12, 12))
1484 .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
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001507class LayerUpdateTest : public ::testing::Test {
1508protected:
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);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001525 ASSERT_TRUE(mBGSurfaceControl != NULL);
1526 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);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001532 ASSERT_TRUE(mFGSurfaceControl != NULL);
1533 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);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001540 ASSERT_TRUE(mSyncSurfaceControl != NULL);
1541 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);
1655 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1656 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1657 });
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);
1692 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1693 });
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);
1706 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1707 });
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);
1735 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001736 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);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001741 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001742 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);
1887 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1888 });
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);
1903 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1904 });
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
2060 ASSERT_TRUE(mChildNewClient != NULL);
2061 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) {
2172 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002173 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);
chaviwf1961f72017-09-18 16:41:07 -07002252 ASSERT_TRUE(newSurface != NULL);
2253 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:
2321 std::unique_ptr<CaptureLayer> mCapture;
2322};
2323
2324TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2325 auto bgHandle = mBGSurfaceControl->getHandle();
2326 CaptureLayer::captureScreen(&mCapture, bgHandle);
2327 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.
2343 CaptureLayer::captureScreen(&mCapture, fgHandle);
2344 mCapture->expectFGColor(10, 10);
2345 mCapture->expectChildColor(0, 0);
2346}
2347
2348TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2349 auto fgHandle = mFGSurfaceControl->getHandle();
2350
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002351 sp<SurfaceControl> child =
2352 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2353 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002354 fillSurfaceRGBA8(child, 200, 200, 200);
2355
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002356 sp<SurfaceControl> grandchild =
2357 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2358 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002359
2360 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2361 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002362 .show(child)
2363 .setPosition(grandchild, 5, 5)
2364 .show(grandchild)
2365 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002366
2367 // Captures mFGSurfaceControl, its child, and the grandchild.
2368 CaptureLayer::captureScreen(&mCapture, fgHandle);
2369 mCapture->expectFGColor(10, 10);
2370 mCapture->expectChildColor(0, 0);
2371 mCapture->checkPixel(5, 5, 50, 50, 50);
2372}
2373
2374TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002375 sp<SurfaceControl> child =
2376 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2377 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002378 fillSurfaceRGBA8(child, 200, 200, 200);
2379 auto childHandle = child->getHandle();
2380
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002381 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002382
2383 // Captures only the child layer, and not the parent.
2384 CaptureLayer::captureScreen(&mCapture, childHandle);
2385 mCapture->expectChildColor(0, 0);
2386 mCapture->expectChildColor(9, 9);
2387}
2388
2389TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002390 sp<SurfaceControl> child =
2391 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2392 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002393 fillSurfaceRGBA8(child, 200, 200, 200);
2394 auto childHandle = child->getHandle();
2395
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002396 sp<SurfaceControl> grandchild =
2397 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2398 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002399 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2400
2401 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002402 .show(child)
2403 .setPosition(grandchild, 5, 5)
2404 .show(grandchild)
2405 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002406
2407 auto grandchildHandle = grandchild->getHandle();
2408
2409 // Captures only the grandchild.
2410 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2411 mCapture->checkPixel(0, 0, 50, 50, 50);
2412 mCapture->checkPixel(4, 4, 50, 50, 50);
2413}
2414
chaviw7206d492017-11-10 16:16:12 -08002415TEST_F(ScreenCaptureTest, CaptureCrop) {
2416 sp<SurfaceControl> redLayer = mComposerClient->createSurface(
2417 String8("Red surface"),
2418 60, 60, PIXEL_FORMAT_RGBA_8888, 0);
2419 sp<SurfaceControl> blueLayer = mComposerClient->createSurface(
2420 String8("Blue surface"),
2421 30, 30, PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
2422
2423 fillSurfaceRGBA8(redLayer, 255, 0, 0);
2424 fillSurfaceRGBA8(blueLayer, 0, 0, 255);
2425
2426 SurfaceComposerClient::Transaction()
2427 .setLayer(redLayer, INT32_MAX-1)
2428 .show(redLayer)
2429 .show(blueLayer)
2430 .apply(true);
2431
2432 auto redLayerHandle = redLayer->getHandle();
2433
2434 // Capturing full screen should have both red and blue are visible.
2435 CaptureLayer::captureScreen(&mCapture, redLayerHandle);
2436 mCapture->checkPixel(29, 29, 0, 0, 255);
2437 mCapture->checkPixel(30, 30, 255, 0, 0);
2438
2439 Rect crop = Rect(0, 0, 30, 30);
2440 CaptureLayer::captureScreen(&mCapture, redLayerHandle, crop);
2441 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
2442 // area visible.
2443 mCapture->checkPixel(29, 29, 0, 0, 255);
2444 mCapture->checkPixel(30, 30, 0, 0, 0);
2445}
2446
2447TEST_F(ScreenCaptureTest, CaptureSize) {
2448 sp<SurfaceControl> redLayer = mComposerClient->createSurface(
2449 String8("Red surface"),
2450 60, 60, PIXEL_FORMAT_RGBA_8888, 0);
2451 sp<SurfaceControl> blueLayer = mComposerClient->createSurface(
2452 String8("Blue surface"),
2453 30, 30, PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
2454
2455 fillSurfaceRGBA8(redLayer, 255, 0, 0);
2456 fillSurfaceRGBA8(blueLayer, 0, 0, 255);
2457
2458 SurfaceComposerClient::Transaction()
2459 .setLayer(redLayer, INT32_MAX-1)
2460 .show(redLayer)
2461 .show(blueLayer)
2462 .apply(true);
2463
2464 auto redLayerHandle = redLayer->getHandle();
2465
2466 // Capturing full screen should have both red and blue are visible.
2467 CaptureLayer::captureScreen(&mCapture, redLayerHandle);
2468 mCapture->checkPixel(29, 29, 0, 0, 255);
2469 mCapture->checkPixel(30, 30, 255, 0, 0);
2470
2471 CaptureLayer::captureScreen(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
2472 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
2473 mCapture->checkPixel(14, 14, 0, 0, 255);
2474 mCapture->checkPixel(15, 15, 255, 0, 0);
2475 mCapture->checkPixel(29, 29, 255, 0, 0);
2476 mCapture->checkPixel(30, 30, 0, 0, 0);
2477}
2478
2479TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
2480 sp<SurfaceControl> redLayer = mComposerClient->createSurface(
2481 String8("Red surface"),
2482 60, 60, PIXEL_FORMAT_RGBA_8888, 0);
2483
2484 fillSurfaceRGBA8(redLayer, 255, 0, 0);
2485
2486 auto redLayerHandle = redLayer->getHandle();
2487 mComposerClient->destroySurface(redLayerHandle);
2488 SurfaceComposerClient::Transaction().apply(true);
2489
2490 sp<IGraphicBufferProducer> producer;
2491 sp<IGraphicBufferConsumer> consumer;
2492 BufferQueue::createBufferQueue(&producer, &consumer);
2493 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
2494 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2495
2496 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
2497 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, producer, Rect::EMPTY_RECT, 1.0));
2498}
2499
2500}