blob: 6ede562018ce979f81c85a0483a4aa76697d291a [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:
279 static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle) {
280 sp<IGraphicBufferProducer> producer;
281 sp<IGraphicBufferConsumer> consumer;
282 BufferQueue::createBufferQueue(&producer, &consumer);
283 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
284 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700285 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
chaviwa76b2712017-09-20 12:02:26 -0700286 SurfaceComposerClient::Transaction().apply(true);
287 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer));
288 *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
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001629TEST_F(LayerUpdateTest, LayerMoveWorks) {
1630 sp<ScreenCapture> sc;
1631 {
1632 SCOPED_TRACE("before move");
1633 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001634 sc->expectBGColor(0, 12);
1635 sc->expectFGColor(75, 75);
1636 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001637 }
1638
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001639 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001640
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001641 {
1642 // This should reflect the new position, but not the new color.
1643 SCOPED_TRACE("after move, before redraw");
1644 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001645 sc->expectBGColor(24, 24);
1646 sc->expectBGColor(75, 75);
1647 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001648 }
1649
1650 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1651 waitForPostedBuffers();
1652 {
1653 // This should reflect the new position and the new color.
1654 SCOPED_TRACE("after redraw");
1655 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001656 sc->expectBGColor(24, 24);
1657 sc->expectBGColor(75, 75);
1658 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001659 }
1660}
1661
1662TEST_F(LayerUpdateTest, LayerResizeWorks) {
1663 sp<ScreenCapture> sc;
1664 {
1665 SCOPED_TRACE("before resize");
1666 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001667 sc->expectBGColor(0, 12);
1668 sc->expectFGColor(75, 75);
1669 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001670 }
1671
Steve Block9d453682011-12-20 16:23:08 +00001672 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001673 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +00001674 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001675 {
1676 // This should not reflect the new size or color because SurfaceFlinger
1677 // has not yet received a buffer of the correct size.
1678 SCOPED_TRACE("after resize, before redraw");
1679 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001680 sc->expectBGColor(0, 12);
1681 sc->expectFGColor(75, 75);
1682 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001683 }
1684
Steve Block9d453682011-12-20 16:23:08 +00001685 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001686 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1687 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +00001688 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001689 {
1690 // This should reflect the new size and the new color.
1691 SCOPED_TRACE("after redraw");
1692 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001693 sc->expectBGColor(24, 24);
1694 sc->checkPixel(75, 75, 63, 195, 63);
1695 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001696 }
1697}
1698
Haixia Shid5750962015-07-27 16:50:49 -07001699TEST_F(LayerUpdateTest, LayerCropWorks) {
1700 sp<ScreenCapture> sc;
1701 {
1702 SCOPED_TRACE("before crop");
1703 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001704 sc->expectBGColor(24, 24);
1705 sc->expectFGColor(75, 75);
1706 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001707 }
1708
Robert Carr4cdc58f2017-08-23 14:22:20 -07001709 asTransaction([&](Transaction& t) {
1710 Rect cropRect(16, 16, 32, 32);
1711 t.setCrop(mFGSurfaceControl, cropRect);
1712 });
Haixia Shid5750962015-07-27 16:50:49 -07001713 {
1714 // This should crop the foreground surface.
1715 SCOPED_TRACE("after crop");
1716 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001717 sc->expectBGColor(24, 24);
1718 sc->expectBGColor(75, 75);
1719 sc->expectFGColor(95, 80);
1720 sc->expectFGColor(80, 95);
1721 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -07001722 }
1723}
1724
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001725TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
1726 sp<ScreenCapture> sc;
1727 {
1728 SCOPED_TRACE("before crop");
1729 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001730 sc->expectBGColor(24, 24);
1731 sc->expectFGColor(75, 75);
1732 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001733 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001734 asTransaction([&](Transaction& t) {
1735 Rect cropRect(16, 16, 32, 32);
1736 t.setFinalCrop(mFGSurfaceControl, cropRect);
1737 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001738 {
1739 // This should crop the foreground surface.
1740 SCOPED_TRACE("after crop");
1741 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001742 sc->expectBGColor(24, 24);
1743 sc->expectBGColor(75, 75);
1744 sc->expectBGColor(95, 80);
1745 sc->expectBGColor(80, 95);
1746 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001747 }
1748}
1749
Haixia Shid5750962015-07-27 16:50:49 -07001750TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
1751 sp<ScreenCapture> sc;
1752 {
1753 SCOPED_TRACE("before setLayer");
1754 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001755 sc->expectBGColor(24, 24);
1756 sc->expectFGColor(75, 75);
1757 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001758 }
1759
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001760 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001761
Haixia Shid5750962015-07-27 16:50:49 -07001762 {
1763 // This should hide the foreground surface beneath the background.
1764 SCOPED_TRACE("after setLayer");
1765 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001766 sc->expectBGColor(24, 24);
1767 sc->expectBGColor(75, 75);
1768 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001769 }
1770}
1771
1772TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1773 sp<ScreenCapture> sc;
1774 {
1775 SCOPED_TRACE("before hide");
1776 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001777 sc->expectBGColor(24, 24);
1778 sc->expectFGColor(75, 75);
1779 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001780 }
1781
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001782 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001783
Haixia Shid5750962015-07-27 16:50:49 -07001784 {
1785 // This should hide the foreground surface.
1786 SCOPED_TRACE("after hide, before show");
1787 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001788 sc->expectBGColor(24, 24);
1789 sc->expectBGColor(75, 75);
1790 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001791 }
1792
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001793 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001794
Haixia Shid5750962015-07-27 16:50:49 -07001795 {
1796 // This should show the foreground surface.
1797 SCOPED_TRACE("after show");
1798 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001799 sc->expectBGColor(24, 24);
1800 sc->expectFGColor(75, 75);
1801 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001802 }
1803}
1804
1805TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1806 sp<ScreenCapture> sc;
1807 {
1808 SCOPED_TRACE("before setAlpha");
1809 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001810 sc->expectBGColor(24, 24);
1811 sc->expectFGColor(75, 75);
1812 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001813 }
1814
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001815 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001816
Haixia Shid5750962015-07-27 16:50:49 -07001817 {
1818 // This should set foreground to be 75% opaque.
1819 SCOPED_TRACE("after setAlpha");
1820 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001821 sc->expectBGColor(24, 24);
1822 sc->checkPixel(75, 75, 162, 63, 96);
1823 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001824 }
1825}
1826
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001827TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1828 sp<ScreenCapture> sc;
1829 {
1830 SCOPED_TRACE("before setLayerStack");
1831 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001832 sc->expectBGColor(24, 24);
1833 sc->expectFGColor(75, 75);
1834 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001835 }
1836
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001837 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001838 {
1839 // This should hide the foreground surface since it goes to a different
1840 // layer stack.
1841 SCOPED_TRACE("after setLayerStack");
1842 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001843 sc->expectBGColor(24, 24);
1844 sc->expectBGColor(75, 75);
1845 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001846 }
1847}
1848
1849TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1850 sp<ScreenCapture> sc;
1851 {
1852 SCOPED_TRACE("before setFlags");
1853 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001854 sc->expectBGColor(24, 24);
1855 sc->expectFGColor(75, 75);
1856 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001857 }
1858
Robert Carr4cdc58f2017-08-23 14:22:20 -07001859 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001860 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001861 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001862 {
1863 // This should hide the foreground surface
1864 SCOPED_TRACE("after setFlags");
1865 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001866 sc->expectBGColor(24, 24);
1867 sc->expectBGColor(75, 75);
1868 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001869 }
1870}
1871
1872TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1873 sp<ScreenCapture> sc;
1874 {
1875 SCOPED_TRACE("before setMatrix");
1876 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001877 sc->expectBGColor(24, 24);
1878 sc->expectFGColor(91, 96);
1879 sc->expectFGColor(96, 101);
1880 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001881 }
1882
Robert Carr4cdc58f2017-08-23 14:22:20 -07001883 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001884 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001885 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001886 {
1887 SCOPED_TRACE("after setMatrix");
1888 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001889 sc->expectBGColor(24, 24);
1890 sc->expectFGColor(91, 96);
1891 sc->expectBGColor(96, 91);
1892 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001893 }
1894}
1895
Robert Carr8d5227b2017-03-16 15:41:03 -07001896class GeometryLatchingTest : public LayerUpdateTest {
1897protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001898 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001899 SCOPED_TRACE(trace);
1900 ScreenCapture::captureScreen(&sc);
1901 // We find the leading edge of the FG surface.
1902 sc->expectFGColor(127, 127);
1903 sc->expectBGColor(128, 128);
1904 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001905
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001906 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001907
1908 void unlockFGBuffer() {
1909 sp<Surface> s = mFGSurfaceControl->getSurface();
1910 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1911 waitForPostedBuffers();
1912 }
1913
Robert Carr8d5227b2017-03-16 15:41:03 -07001914 void completeFGResize() {
1915 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1916 waitForPostedBuffers();
1917 }
1918 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001919 asTransaction([&](Transaction& t) {
1920 t.setSize(mFGSurfaceControl, 64, 64);
1921 t.setPosition(mFGSurfaceControl, 64, 64);
1922 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1923 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1924 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001925
1926 EXPECT_INITIAL_STATE("After restoring initial state");
1927 }
1928 sp<ScreenCapture> sc;
1929};
1930
1931TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1932 EXPECT_INITIAL_STATE("before anything");
1933
1934 // By default position can be updated even while
1935 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001936 asTransaction([&](Transaction& t) {
1937 t.setSize(mFGSurfaceControl, 32, 32);
1938 t.setPosition(mFGSurfaceControl, 100, 100);
1939 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001940
1941 {
1942 SCOPED_TRACE("After moving surface");
1943 ScreenCapture::captureScreen(&sc);
1944 // If we moved, the FG Surface should cover up what was previously BG
1945 // however if we didn't move the FG wouldn't be large enough now.
1946 sc->expectFGColor(163, 163);
1947 }
1948
1949 restoreInitialState();
1950
1951 // Now we repeat with setGeometryAppliesWithResize
1952 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001953 asTransaction([&](Transaction& t) {
1954 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1955 t.setSize(mFGSurfaceControl, 32, 32);
1956 t.setPosition(mFGSurfaceControl, 100, 100);
1957 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001958
1959 {
1960 SCOPED_TRACE("While resize is pending");
1961 ScreenCapture::captureScreen(&sc);
1962 // This time we shouldn't have moved, so the BG color
1963 // should still be visible.
1964 sc->expectBGColor(128, 128);
1965 }
1966
1967 completeFGResize();
1968
1969 {
1970 SCOPED_TRACE("After the resize");
1971 ScreenCapture::captureScreen(&sc);
1972 // But after the resize completes, we should move
1973 // and the FG should be visible here.
1974 sc->expectFGColor(128, 128);
1975 }
1976}
1977
1978class CropLatchingTest : public GeometryLatchingTest {
1979protected:
1980 void EXPECT_CROPPED_STATE(const char* trace) {
1981 SCOPED_TRACE(trace);
1982 ScreenCapture::captureScreen(&sc);
1983 // The edge should be moved back one pixel by our crop.
1984 sc->expectFGColor(126, 126);
1985 sc->expectBGColor(127, 127);
1986 sc->expectBGColor(128, 128);
1987 }
chaviw59f5c562017-06-28 16:39:06 -07001988
1989 void EXPECT_RESIZE_STATE(const char* trace) {
1990 SCOPED_TRACE(trace);
1991 ScreenCapture::captureScreen(&sc);
1992 // The FG is now resized too 128,128 at 64,64
1993 sc->expectFGColor(64, 64);
1994 sc->expectFGColor(191, 191);
1995 sc->expectBGColor(192, 192);
1996 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001997};
1998
1999TEST_F(CropLatchingTest, CropLatching) {
2000 EXPECT_INITIAL_STATE("before anything");
2001 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07002002 asTransaction([&](Transaction& t) {
2003 t.setSize(mFGSurfaceControl, 128, 128);
2004 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
2005 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002006
2007 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
2008
2009 restoreInitialState();
2010
Robert Carr4cdc58f2017-08-23 14:22:20 -07002011 asTransaction([&](Transaction& t) {
2012 t.setSize(mFGSurfaceControl, 128, 128);
2013 t.setGeometryAppliesWithResize(mFGSurfaceControl);
2014 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
2015 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002016
2017 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
2018
2019 completeFGResize();
2020
2021 EXPECT_CROPPED_STATE("after the resize finishes");
2022}
2023
2024TEST_F(CropLatchingTest, FinalCropLatching) {
2025 EXPECT_INITIAL_STATE("before anything");
2026 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07002027 asTransaction([&](Transaction& t) {
2028 t.setSize(mFGSurfaceControl, 128, 128);
2029 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
2030 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002031
2032 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
2033
2034 restoreInitialState();
2035
Robert Carr4cdc58f2017-08-23 14:22:20 -07002036 asTransaction([&](Transaction& t) {
2037 t.setSize(mFGSurfaceControl, 128, 128);
2038 t.setGeometryAppliesWithResize(mFGSurfaceControl);
2039 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
2040 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002041
2042 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
2043
2044 completeFGResize();
2045
2046 EXPECT_CROPPED_STATE("after the resize finishes");
2047}
2048
Robert Carr7bf247e2017-05-18 14:02:49 -07002049// In this test we ensure that setGeometryAppliesWithResize actually demands
2050// a buffer of the new size, and not just any size.
2051TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
2052 EXPECT_INITIAL_STATE("before anything");
2053 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07002054 asTransaction([&](Transaction& t) {
2055 t.setSize(mFGSurfaceControl, 128, 128);
2056 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
2057 });
Robert Carr7bf247e2017-05-18 14:02:49 -07002058
2059 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
2060
2061 restoreInitialState();
2062
2063 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
2064 // initiating the resize.
2065 lockAndFillFGBuffer();
2066
Robert Carr4cdc58f2017-08-23 14:22:20 -07002067 asTransaction([&](Transaction& t) {
2068 t.setSize(mFGSurfaceControl, 128, 128);
2069 t.setGeometryAppliesWithResize(mFGSurfaceControl);
2070 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
2071 });
Robert Carr7bf247e2017-05-18 14:02:49 -07002072
2073 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
2074
2075 // We now submit our old buffer, at the old size, and ensure it doesn't
2076 // trigger geometry latching.
2077 unlockFGBuffer();
2078
2079 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
2080
2081 completeFGResize();
2082
2083 EXPECT_CROPPED_STATE("after the resize finishes");
2084}
2085
2086TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
2087 EXPECT_INITIAL_STATE("before anything");
2088 // In this scenario, we attempt to set the final crop a second time while the resize
2089 // is still pending, and ensure we are successful. Success meaning the second crop
2090 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07002091 asTransaction([&](Transaction& t) {
2092 t.setSize(mFGSurfaceControl, 128, 128);
2093 t.setGeometryAppliesWithResize(mFGSurfaceControl);
2094 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
2095 });
Robert Carr7bf247e2017-05-18 14:02:49 -07002096
chaviw59f5c562017-06-28 16:39:06 -07002097 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
2098
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002099 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07002100
chaviw59f5c562017-06-28 16:39:06 -07002101 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07002102
2103 completeFGResize();
2104
chaviw59f5c562017-06-28 16:39:06 -07002105 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07002106}
2107
Pablo Ceballos05289c22016-04-14 15:49:55 -07002108TEST_F(LayerUpdateTest, DeferredTransactionTest) {
2109 sp<ScreenCapture> sc;
2110 {
2111 SCOPED_TRACE("before anything");
2112 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002113 sc->expectBGColor(32, 32);
2114 sc->expectFGColor(96, 96);
2115 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002116 }
2117
2118 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07002119 asTransaction([&](Transaction& t) {
2120 t.setAlpha(mFGSurfaceControl, 0.75);
2121 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002122 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002123 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002124
Robert Carr4cdc58f2017-08-23 14:22:20 -07002125 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002126 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002127 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002128 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002129 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002130
2131 {
2132 SCOPED_TRACE("before any trigger");
2133 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002134 sc->expectBGColor(32, 32);
2135 sc->expectFGColor(96, 96);
2136 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002137 }
2138
2139 // should trigger the first deferred transaction, but not the second one
2140 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2141 {
2142 SCOPED_TRACE("after first trigger");
2143 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002144 sc->expectBGColor(32, 32);
2145 sc->checkPixel(96, 96, 162, 63, 96);
2146 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002147 }
2148
2149 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002150 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002151
2152 // trigger the second deferred transaction
2153 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2154 {
2155 SCOPED_TRACE("after second trigger");
2156 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002157 sc->expectBGColor(32, 32);
2158 sc->expectBGColor(96, 96);
2159 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002160 }
2161}
2162
Robert Carrdb66e622017-04-10 16:55:57 -07002163TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
2164 sp<ScreenCapture> sc;
2165 {
2166 SCOPED_TRACE("before adding relative surface");
2167 ScreenCapture::captureScreen(&sc);
2168 sc->expectBGColor(24, 24);
2169 sc->expectFGColor(75, 75);
2170 sc->expectBGColor(145, 145);
2171 }
2172
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002173 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
2174 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07002175 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
2176 waitForPostedBuffers();
2177
2178 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07002179 asTransaction([&](Transaction& t) {
2180 t.setPosition(relativeSurfaceControl, 64, 64);
2181 t.show(relativeSurfaceControl);
2182 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
2183 });
Robert Carrdb66e622017-04-10 16:55:57 -07002184
2185 {
2186 SCOPED_TRACE("after adding relative surface");
2187 ScreenCapture::captureScreen(&sc);
2188 // our relative surface should be visible now.
2189 sc->checkPixel(75, 75, 255, 177, 177);
2190 }
2191
2192 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002193 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07002194
2195 {
2196 SCOPED_TRACE("after set layer");
2197 ScreenCapture::captureScreen(&sc);
2198 // now the FG surface should be visible again.
2199 sc->expectFGColor(75, 75);
2200 }
2201}
2202
Robert Carre392b552017-09-19 12:16:05 -07002203TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
2204 sp<ScreenCapture> sc;
2205
2206 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002207 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
2208 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2209 sp<SurfaceControl> childBuffer =
2210 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
2211 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07002212 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
2213
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002214 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002215
2216 {
2217 ScreenCapture::captureScreen(&sc);
2218 sc->expectChildColor(73, 73);
2219 sc->expectFGColor(74, 74);
2220 }
2221
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002222 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002223
2224 {
2225 ScreenCapture::captureScreen(&sc);
2226 sc->expectChildColor(73, 73);
2227 sc->expectChildColor(74, 74);
2228 }
2229}
2230
Robert Carr2c5f6d22017-09-26 12:30:35 -07002231TEST_F(LayerUpdateTest, MergingTransactions) {
2232 sp<ScreenCapture> sc;
2233 {
2234 SCOPED_TRACE("before move");
2235 ScreenCapture::captureScreen(&sc);
2236 sc->expectBGColor(0, 12);
2237 sc->expectFGColor(75, 75);
2238 sc->expectBGColor(145, 145);
2239 }
2240
2241 Transaction t1, t2;
2242 t1.setPosition(mFGSurfaceControl, 128, 128);
2243 t2.setPosition(mFGSurfaceControl, 0, 0);
2244 // We expect that the position update from t2 now
2245 // overwrites the position update from t1.
2246 t1.merge(std::move(t2));
2247 t1.apply();
2248
2249 {
2250 ScreenCapture::captureScreen(&sc);
2251 sc->expectFGColor(1, 1);
2252 }
2253}
2254
Robert Carr1f0a16a2016-10-24 16:27:39 -07002255class ChildLayerTest : public LayerUpdateTest {
2256protected:
2257 void SetUp() override {
2258 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002259 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2260 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002261 fillSurfaceRGBA8(mChild, 200, 200, 200);
2262
2263 {
2264 SCOPED_TRACE("before anything");
2265 ScreenCapture::captureScreen(&mCapture);
2266 mCapture->expectChildColor(64, 64);
2267 }
2268 }
2269 void TearDown() override {
2270 LayerUpdateTest::TearDown();
2271 mChild = 0;
2272 }
2273
2274 sp<SurfaceControl> mChild;
2275 sp<ScreenCapture> mCapture;
2276};
2277
2278TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002279 asTransaction([&](Transaction& t) {
2280 t.show(mChild);
2281 t.setPosition(mChild, 10, 10);
2282 t.setPosition(mFGSurfaceControl, 64, 64);
2283 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002284
2285 {
2286 ScreenCapture::captureScreen(&mCapture);
2287 // Top left of foreground must now be visible
2288 mCapture->expectFGColor(64, 64);
2289 // But 10 pixels in we should see the child surface
2290 mCapture->expectChildColor(74, 74);
2291 // And 10 more pixels we should be back to the foreground surface
2292 mCapture->expectFGColor(84, 84);
2293 }
2294
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002295 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002296
2297 {
2298 ScreenCapture::captureScreen(&mCapture);
2299 // Top left of foreground should now be at 0, 0
2300 mCapture->expectFGColor(0, 0);
2301 // But 10 pixels in we should see the child surface
2302 mCapture->expectChildColor(10, 10);
2303 // And 10 more pixels we should be back to the foreground surface
2304 mCapture->expectFGColor(20, 20);
2305 }
2306}
2307
Robert Carr41b08b52017-06-01 16:11:34 -07002308TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002309 asTransaction([&](Transaction& t) {
2310 t.show(mChild);
2311 t.setPosition(mChild, 0, 0);
2312 t.setPosition(mFGSurfaceControl, 0, 0);
2313 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
2314 });
Robert Carr41b08b52017-06-01 16:11:34 -07002315
2316 {
2317 ScreenCapture::captureScreen(&mCapture);
2318 mCapture->expectChildColor(0, 0);
2319 mCapture->expectChildColor(4, 4);
2320 mCapture->expectBGColor(5, 5);
2321 }
2322}
2323
2324TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002325 asTransaction([&](Transaction& t) {
2326 t.show(mChild);
2327 t.setPosition(mChild, 0, 0);
2328 t.setPosition(mFGSurfaceControl, 0, 0);
2329 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
2330 });
Robert Carr41b08b52017-06-01 16:11:34 -07002331
2332 {
2333 ScreenCapture::captureScreen(&mCapture);
2334 mCapture->expectChildColor(0, 0);
2335 mCapture->expectChildColor(4, 4);
2336 mCapture->expectBGColor(5, 5);
2337 }
2338}
2339
Robert Carr1f0a16a2016-10-24 16:27:39 -07002340TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002341 asTransaction([&](Transaction& t) {
2342 t.show(mChild);
2343 t.setPosition(mFGSurfaceControl, 0, 0);
2344 t.setPosition(mChild, 63, 63);
2345 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002346
2347 {
2348 ScreenCapture::captureScreen(&mCapture);
2349 mCapture->expectFGColor(0, 0);
2350 // Last pixel in foreground should now be the child.
2351 mCapture->expectChildColor(63, 63);
2352 // But the child should be constrained and the next pixel
2353 // must be the background
2354 mCapture->expectBGColor(64, 64);
2355 }
2356}
2357
2358TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002359 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002360
2361 // Find the boundary between the parent and child
2362 {
2363 ScreenCapture::captureScreen(&mCapture);
2364 mCapture->expectChildColor(9, 9);
2365 mCapture->expectFGColor(10, 10);
2366 }
2367
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002368 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002369
2370 // The boundary should be twice as far from the origin now.
2371 // The pixels from the last test should all be child now
2372 {
2373 ScreenCapture::captureScreen(&mCapture);
2374 mCapture->expectChildColor(9, 9);
2375 mCapture->expectChildColor(10, 10);
2376 mCapture->expectChildColor(19, 19);
2377 mCapture->expectFGColor(20, 20);
2378 }
2379}
Robert Carr9524cb32017-02-13 11:32:32 -08002380
Robert Carr6452f122017-03-21 10:41:29 -07002381TEST_F(ChildLayerTest, ChildLayerAlpha) {
2382 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2383 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2384 fillSurfaceRGBA8(mChild, 0, 254, 0);
2385 waitForPostedBuffers();
2386
Robert Carr4cdc58f2017-08-23 14:22:20 -07002387 asTransaction([&](Transaction& t) {
2388 t.show(mChild);
2389 t.setPosition(mChild, 0, 0);
2390 t.setPosition(mFGSurfaceControl, 0, 0);
2391 });
Robert Carr6452f122017-03-21 10:41:29 -07002392
2393 {
2394 ScreenCapture::captureScreen(&mCapture);
2395 // Unblended child color
2396 mCapture->checkPixel(0, 0, 0, 254, 0);
2397 }
2398
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002399 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002400
2401 {
2402 ScreenCapture::captureScreen(&mCapture);
2403 // Child and BG blended.
2404 mCapture->checkPixel(0, 0, 127, 127, 0);
2405 }
2406
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002407 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002408
2409 {
2410 ScreenCapture::captureScreen(&mCapture);
2411 // Child and BG blended.
2412 mCapture->checkPixel(0, 0, 95, 64, 95);
2413 }
2414}
2415
Robert Carr9524cb32017-02-13 11:32:32 -08002416TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002417 asTransaction([&](Transaction& t) {
2418 t.show(mChild);
2419 t.setPosition(mChild, 10, 10);
2420 t.setPosition(mFGSurfaceControl, 64, 64);
2421 });
Robert Carr9524cb32017-02-13 11:32:32 -08002422
2423 {
2424 ScreenCapture::captureScreen(&mCapture);
2425 // Top left of foreground must now be visible
2426 mCapture->expectFGColor(64, 64);
2427 // But 10 pixels in we should see the child surface
2428 mCapture->expectChildColor(74, 74);
2429 // And 10 more pixels we should be back to the foreground surface
2430 mCapture->expectFGColor(84, 84);
2431 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002432
2433 asTransaction([&](Transaction& t) {
2434 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2435 });
2436
Robert Carr9524cb32017-02-13 11:32:32 -08002437 {
2438 ScreenCapture::captureScreen(&mCapture);
2439 mCapture->expectFGColor(64, 64);
2440 // In reparenting we should have exposed the entire foreground surface.
2441 mCapture->expectFGColor(74, 74);
2442 // And the child layer should now begin at 10, 10 (since the BG
2443 // layer is at (0, 0)).
2444 mCapture->expectBGColor(9, 9);
2445 mCapture->expectChildColor(10, 10);
2446 }
2447}
2448
chaviw161410b02017-07-27 10:46:08 -07002449TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002450 asTransaction([&](Transaction& t) {
2451 t.show(mChild);
2452 t.setPosition(mChild, 10, 10);
2453 t.setPosition(mFGSurfaceControl, 64, 64);
2454 });
Robert Carr9524cb32017-02-13 11:32:32 -08002455
2456 {
2457 ScreenCapture::captureScreen(&mCapture);
2458 // Top left of foreground must now be visible
2459 mCapture->expectFGColor(64, 64);
2460 // But 10 pixels in we should see the child surface
2461 mCapture->expectChildColor(74, 74);
2462 // And 10 more pixels we should be back to the foreground surface
2463 mCapture->expectFGColor(84, 84);
2464 }
2465
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002466 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002467
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002468 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002469
chaviw161410b02017-07-27 10:46:08 -07002470 // Since the child has the same client as the parent, it will not get
2471 // detached and will be hidden.
2472 {
2473 ScreenCapture::captureScreen(&mCapture);
2474 mCapture->expectFGColor(64, 64);
2475 mCapture->expectFGColor(74, 74);
2476 mCapture->expectFGColor(84, 84);
2477 }
2478}
2479
2480TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2481 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002482 sp<SurfaceControl> mChildNewClient =
2483 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2484 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002485
2486 ASSERT_TRUE(mChildNewClient != NULL);
2487 ASSERT_TRUE(mChildNewClient->isValid());
2488
2489 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2490
Robert Carr4cdc58f2017-08-23 14:22:20 -07002491 asTransaction([&](Transaction& t) {
2492 t.hide(mChild);
2493 t.show(mChildNewClient);
2494 t.setPosition(mChildNewClient, 10, 10);
2495 t.setPosition(mFGSurfaceControl, 64, 64);
2496 });
chaviw161410b02017-07-27 10:46:08 -07002497
2498 {
2499 ScreenCapture::captureScreen(&mCapture);
2500 // Top left of foreground must now be visible
2501 mCapture->expectFGColor(64, 64);
2502 // But 10 pixels in we should see the child surface
2503 mCapture->expectChildColor(74, 74);
2504 // And 10 more pixels we should be back to the foreground surface
2505 mCapture->expectFGColor(84, 84);
2506 }
2507
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002508 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002509
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002510 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002511
Robert Carr9524cb32017-02-13 11:32:32 -08002512 // Nothing should have changed.
2513 {
2514 ScreenCapture::captureScreen(&mCapture);
2515 mCapture->expectFGColor(64, 64);
2516 mCapture->expectChildColor(74, 74);
2517 mCapture->expectFGColor(84, 84);
2518 }
2519}
2520
Robert Carr9b429f42017-04-17 14:56:57 -07002521TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002522 asTransaction([&](Transaction& t) {
2523 t.show(mChild);
2524 t.setPosition(mChild, 0, 0);
2525 t.setPosition(mFGSurfaceControl, 0, 0);
2526 });
Robert Carr9b429f42017-04-17 14:56:57 -07002527
2528 {
2529 ScreenCapture::captureScreen(&mCapture);
2530 // We've positioned the child in the top left.
2531 mCapture->expectChildColor(0, 0);
2532 // But it's only 10x10.
2533 mCapture->expectFGColor(10, 10);
2534 }
2535
Robert Carr4cdc58f2017-08-23 14:22:20 -07002536 asTransaction([&](Transaction& t) {
2537 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2538 // We cause scaling by 2.
2539 t.setSize(mFGSurfaceControl, 128, 128);
2540 });
Robert Carr9b429f42017-04-17 14:56:57 -07002541
2542 {
2543 ScreenCapture::captureScreen(&mCapture);
2544 // We've positioned the child in the top left.
2545 mCapture->expectChildColor(0, 0);
2546 mCapture->expectChildColor(10, 10);
2547 mCapture->expectChildColor(19, 19);
2548 // And now it should be scaled all the way to 20x20
2549 mCapture->expectFGColor(20, 20);
2550 }
2551}
2552
Robert Carr1725eee2017-04-26 18:32:15 -07002553// Regression test for b/37673612
2554TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002555 asTransaction([&](Transaction& t) {
2556 t.show(mChild);
2557 t.setPosition(mChild, 0, 0);
2558 t.setPosition(mFGSurfaceControl, 0, 0);
2559 });
Robert Carr1725eee2017-04-26 18:32:15 -07002560
2561 {
2562 ScreenCapture::captureScreen(&mCapture);
2563 // We've positioned the child in the top left.
2564 mCapture->expectChildColor(0, 0);
2565 // But it's only 10x10.
2566 mCapture->expectFGColor(10, 10);
2567 }
Robert Carr1725eee2017-04-26 18:32:15 -07002568 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2569 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002570 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002571 sp<Surface> s = mFGSurfaceControl->getSurface();
2572 auto anw = static_cast<ANativeWindow*>(s.get());
2573 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2574 native_window_set_buffers_dimensions(anw, 64, 128);
2575 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2576 waitForPostedBuffers();
2577
2578 {
2579 // The child should still be in the same place and not have any strange scaling as in
2580 // b/37673612.
2581 ScreenCapture::captureScreen(&mCapture);
2582 mCapture->expectChildColor(0, 0);
2583 mCapture->expectFGColor(10, 10);
2584 }
2585}
2586
Dan Stoza412903f2017-04-27 13:42:17 -07002587TEST_F(ChildLayerTest, Bug36858924) {
2588 // Destroy the child layer
2589 mChild.clear();
2590
2591 // Now recreate it as hidden
2592 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2593 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2594 mFGSurfaceControl.get());
2595
2596 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002597 asTransaction([&](Transaction& t) {
2598 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002599 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002600 t.show(mChild);
2601 });
Dan Stoza412903f2017-04-27 13:42:17 -07002602
2603 // Render the foreground surface a few times
2604 //
2605 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2606 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2607 // never acquire/release the first buffer
2608 ALOGI("Filling 1");
2609 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2610 ALOGI("Filling 2");
2611 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2612 ALOGI("Filling 3");
2613 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2614 ALOGI("Filling 4");
2615 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2616}
2617
chaviwf1961f72017-09-18 16:41:07 -07002618TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002619 asTransaction([&](Transaction& t) {
2620 t.show(mChild);
2621 t.setPosition(mChild, 10, 10);
2622 t.setPosition(mFGSurfaceControl, 64, 64);
2623 });
chaviw06178942017-07-27 10:25:59 -07002624
2625 {
2626 ScreenCapture::captureScreen(&mCapture);
2627 // Top left of foreground must now be visible
2628 mCapture->expectFGColor(64, 64);
2629 // But 10 pixels in we should see the child surface
2630 mCapture->expectChildColor(74, 74);
2631 // And 10 more pixels we should be back to the foreground surface
2632 mCapture->expectFGColor(84, 84);
2633 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002634
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002635 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002636
chaviw06178942017-07-27 10:25:59 -07002637 {
2638 ScreenCapture::captureScreen(&mCapture);
2639 mCapture->expectFGColor(64, 64);
2640 // In reparenting we should have exposed the entire foreground surface.
2641 mCapture->expectFGColor(74, 74);
2642 // And the child layer should now begin at 10, 10 (since the BG
2643 // layer is at (0, 0)).
2644 mCapture->expectBGColor(9, 9);
2645 mCapture->expectChildColor(10, 10);
2646 }
2647}
2648
chaviwf1961f72017-09-18 16:41:07 -07002649TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002650 asTransaction([&](Transaction& t) {
2651 t.show(mChild);
2652 t.setPosition(mChild, 10, 10);
2653 t.setPosition(mFGSurfaceControl, 64, 64);
2654 });
chaviwf1961f72017-09-18 16:41:07 -07002655
2656 {
2657 ScreenCapture::captureScreen(&mCapture);
2658 // Top left of foreground must now be visible
2659 mCapture->expectFGColor(64, 64);
2660 // But 10 pixels in we should see the child surface
2661 mCapture->expectChildColor(74, 74);
2662 // And 10 more pixels we should be back to the foreground surface
2663 mCapture->expectFGColor(84, 84);
2664 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002665 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002666 {
2667 ScreenCapture::captureScreen(&mCapture);
2668 // Nothing should have changed.
2669 mCapture->expectFGColor(64, 64);
2670 mCapture->expectChildColor(74, 74);
2671 mCapture->expectFGColor(84, 84);
2672 }
2673}
2674
2675TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002676 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2677 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07002678 ASSERT_TRUE(newSurface != NULL);
2679 ASSERT_TRUE(newSurface->isValid());
2680
2681 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002682 asTransaction([&](Transaction& t) {
2683 t.hide(mChild);
2684 t.show(newSurface);
2685 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002686 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002687 t.setPosition(mFGSurfaceControl, 64, 64);
2688 });
chaviwf1961f72017-09-18 16:41:07 -07002689
2690 {
2691 ScreenCapture::captureScreen(&mCapture);
2692 // Top left of foreground must now be visible
2693 mCapture->expectFGColor(64, 64);
2694 // At 10, 10 we should see the new surface
2695 mCapture->checkPixel(10, 10, 63, 195, 63);
2696 }
2697
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002698 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002699
2700 {
2701 ScreenCapture::captureScreen(&mCapture);
2702 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2703 // mFGSurface, putting it at 74, 74.
2704 mCapture->expectFGColor(64, 64);
2705 mCapture->checkPixel(74, 74, 63, 195, 63);
2706 mCapture->expectFGColor(84, 84);
2707 }
2708}
2709
chaviwc9674332017-08-28 12:32:18 -07002710TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002711 sp<SurfaceControl> grandchild =
2712 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2713 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002714 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2715
2716 {
2717 ScreenCapture::captureScreen(&mCapture);
2718 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2719 // which begins at 64, 64
2720 mCapture->checkPixel(64, 64, 50, 50, 50);
2721 }
2722}
2723
Robert Carr503c7042017-09-27 15:06:08 -07002724TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002725 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2726 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002727 fillSurfaceRGBA8(relative, 255, 255, 255);
2728
2729 Transaction t;
2730 t.setLayer(relative, INT32_MAX)
2731 .setRelativeLayer(mChild, relative->getHandle(), 1)
2732 .setPosition(mFGSurfaceControl, 0, 0)
2733 .apply(true);
2734
2735 // We expect that the child should have been elevated above our
2736 // INT_MAX layer even though it's not a child of it.
2737 {
2738 ScreenCapture::captureScreen(&mCapture);
2739 mCapture->expectChildColor(0, 0);
2740 mCapture->expectChildColor(9, 9);
2741 mCapture->checkPixel(10, 10, 255, 255, 255);
2742 }
2743}
2744
chaviw13fdc492017-06-27 12:40:18 -07002745class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002746protected:
chaviw13fdc492017-06-27 12:40:18 -07002747 void SetUp() override {
2748 LayerUpdateTest::SetUp();
2749
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002750 mLayerColorControl =
2751 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
2752 PIXEL_FORMAT_RGBA_8888,
2753 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07002754
2755 ASSERT_TRUE(mLayerColorControl != NULL);
2756 ASSERT_TRUE(mLayerColorControl->isValid());
2757
Robert Carr4cdc58f2017-08-23 14:22:20 -07002758 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002759 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002760 t.setPosition(mLayerColorControl, 140, 140);
2761 t.hide(mLayerColorControl);
2762 t.hide(mFGSurfaceControl);
2763 });
chaviw13fdc492017-06-27 12:40:18 -07002764 }
2765
2766 void TearDown() override {
2767 LayerUpdateTest::TearDown();
2768 mLayerColorControl = 0;
2769 }
2770
2771 sp<SurfaceControl> mLayerColorControl;
2772};
2773
2774TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2775 sp<ScreenCapture> sc;
2776
2777 {
2778 SCOPED_TRACE("before setColor");
2779 ScreenCapture::captureScreen(&sc);
2780 sc->expectBGColor(145, 145);
2781 }
2782
Robert Carr4cdc58f2017-08-23 14:22:20 -07002783 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002784 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002785 t.setColor(mLayerColorControl, color);
2786 t.show(mLayerColorControl);
2787 });
chaviw13fdc492017-06-27 12:40:18 -07002788
chaviw13fdc492017-06-27 12:40:18 -07002789 {
2790 // There should now be a color
2791 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002792
chaviw13fdc492017-06-27 12:40:18 -07002793 ScreenCapture::captureScreen(&sc);
2794 sc->checkPixel(145, 145, 43, 207, 131);
2795 }
2796}
2797
2798TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2799 sp<ScreenCapture> sc;
2800 {
2801 SCOPED_TRACE("before setColor");
2802 ScreenCapture::captureScreen(&sc);
2803 sc->expectBGColor(145, 145);
2804 }
2805
Robert Carr4cdc58f2017-08-23 14:22:20 -07002806 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002807 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002808 t.setColor(mLayerColorControl, color);
2809 t.setAlpha(mLayerColorControl, .75f);
2810 t.show(mLayerColorControl);
2811 });
2812
chaviw13fdc492017-06-27 12:40:18 -07002813 {
2814 // There should now be a color with .75 alpha
2815 SCOPED_TRACE("after setColor");
2816 ScreenCapture::captureScreen(&sc);
2817 sc->checkPixel(145, 145, 48, 171, 147);
2818 }
2819}
2820
2821TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2822 sp<ScreenCapture> sc;
2823 {
2824 SCOPED_TRACE("before setColor");
2825 ScreenCapture::captureScreen(&sc);
2826 sc->expectBGColor(145, 145);
2827 }
2828
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002829 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002830
chaviw13fdc492017-06-27 12:40:18 -07002831 {
2832 // There should now be set to 0,0,0 (black) as default.
2833 SCOPED_TRACE("after setColor");
2834 ScreenCapture::captureScreen(&sc);
2835 sc->checkPixel(145, 145, 0, 0, 0);
2836 }
2837}
2838
chaviwa76b2712017-09-20 12:02:26 -07002839class ScreenCaptureTest : public LayerUpdateTest {
2840protected:
2841 std::unique_ptr<CaptureLayer> mCapture;
2842};
2843
2844TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2845 auto bgHandle = mBGSurfaceControl->getHandle();
2846 CaptureLayer::captureScreen(&mCapture, bgHandle);
2847 mCapture->expectBGColor(0, 0);
2848 // Doesn't capture FG layer which is at 64, 64
2849 mCapture->expectBGColor(64, 64);
2850}
2851
2852TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2853 auto fgHandle = mFGSurfaceControl->getHandle();
2854
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002855 sp<SurfaceControl> child =
2856 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2857 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002858 fillSurfaceRGBA8(child, 200, 200, 200);
2859
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002860 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002861
2862 // Captures mFGSurfaceControl layer and its child.
2863 CaptureLayer::captureScreen(&mCapture, fgHandle);
2864 mCapture->expectFGColor(10, 10);
2865 mCapture->expectChildColor(0, 0);
2866}
2867
2868TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2869 auto fgHandle = mFGSurfaceControl->getHandle();
2870
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002871 sp<SurfaceControl> child =
2872 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2873 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002874 fillSurfaceRGBA8(child, 200, 200, 200);
2875
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002876 sp<SurfaceControl> grandchild =
2877 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2878 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002879
2880 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2881 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002882 .show(child)
2883 .setPosition(grandchild, 5, 5)
2884 .show(grandchild)
2885 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002886
2887 // Captures mFGSurfaceControl, its child, and the grandchild.
2888 CaptureLayer::captureScreen(&mCapture, fgHandle);
2889 mCapture->expectFGColor(10, 10);
2890 mCapture->expectChildColor(0, 0);
2891 mCapture->checkPixel(5, 5, 50, 50, 50);
2892}
2893
2894TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002895 sp<SurfaceControl> child =
2896 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2897 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002898 fillSurfaceRGBA8(child, 200, 200, 200);
2899 auto childHandle = child->getHandle();
2900
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002901 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002902
2903 // Captures only the child layer, and not the parent.
2904 CaptureLayer::captureScreen(&mCapture, childHandle);
2905 mCapture->expectChildColor(0, 0);
2906 mCapture->expectChildColor(9, 9);
2907}
2908
2909TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002910 sp<SurfaceControl> child =
2911 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2912 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002913 fillSurfaceRGBA8(child, 200, 200, 200);
2914 auto childHandle = child->getHandle();
2915
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002916 sp<SurfaceControl> grandchild =
2917 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2918 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002919 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2920
2921 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002922 .show(child)
2923 .setPosition(grandchild, 5, 5)
2924 .show(grandchild)
2925 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002926
2927 auto grandchildHandle = grandchild->getHandle();
2928
2929 // Captures only the grandchild.
2930 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2931 mCapture->checkPixel(0, 0, 50, 50, 50);
2932 mCapture->checkPixel(4, 4, 50, 50, 50);
2933}
2934
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002935} // namespace android