blob: ba34fe5cb3f4a44534d25a1073ed6805e1ccb682 [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
705TEST_F(LayerTransactionTest, SetRelativeZGroup) {
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 = 0, layerG = layerR + 3, layerB = 2
717 Transaction()
718 .setPosition(layerG, 8, 8)
719 .setRelativeLayer(layerG, layerR->getHandle(), 3)
720 .setPosition(layerB, 16, 16)
721 .setLayer(layerB, mLayerZBase + 2)
722 .apply();
723 {
724 SCOPED_TRACE("(layerR < layerG) < layerB");
725 auto shot = screenshot();
726 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
727 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
728 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
729 }
730
731 // layerR = 4, layerG = layerR + 3, layerB = 2
732 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
733 {
734 SCOPED_TRACE("layerB < (layerR < layerG)");
735 auto shot = screenshot();
736 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
737 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
738 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
739 }
740
741 // layerR = 4, layerG = layerR - 3, layerB = 2
742 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
743 {
744 SCOPED_TRACE("layerB < (layerG < layerR)");
745 auto shot = screenshot();
746 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
747 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
748 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
749 }
750
751 // restore to absolute z
752 // layerR = 4, layerG = 0, layerB = 2
753 Transaction().setLayer(layerG, mLayerZBase).apply();
754 {
755 SCOPED_TRACE("layerG < layerB < layerR");
756 auto shot = screenshot();
757 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
758 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
759 }
760
761 // layerR should not affect layerG anymore
762 // layerR = 1, layerG = 0, layerB = 2
763 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
764 {
765 SCOPED_TRACE("layerG < layerR < layerB");
766 auto shot = screenshot();
767 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
768 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
769 }
770}
771
772TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
773 sp<SurfaceControl> layerR;
774 sp<SurfaceControl> layerG;
775
776 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
777 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
778 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
779 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
780
781 Transaction()
782 .setPosition(layerG, 16, 16)
783 .setRelativeLayer(layerG, layerR->getHandle(), 1)
784 .apply();
785
786 mClient->destroySurface(layerG->getHandle());
787 // layerG should have been removed
788 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
789}
790
Chia-I Wu57b27502017-10-31 10:14:40 -0700791TEST_F(LayerTransactionTest, SetFlagsHidden) {
792 sp<SurfaceControl> layer;
793 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
794 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
795
796 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
797 {
798 SCOPED_TRACE("layer hidden");
799 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
800 }
801
802 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
803 {
804 SCOPED_TRACE("layer shown");
805 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
806 }
807}
808
809TEST_F(LayerTransactionTest, SetFlagsOpaque) {
810 const Color translucentRed = {100, 0, 0, 100};
811 sp<SurfaceControl> layerR;
812 sp<SurfaceControl> layerG;
813 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
814 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
815 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
816 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
817
818 Transaction()
819 .setLayer(layerR, mLayerZBase + 1)
820 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
821 .apply();
822 {
823 SCOPED_TRACE("layerR opaque");
824 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
825 }
826
827 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
828 {
829 SCOPED_TRACE("layerR translucent");
830 const uint8_t g = uint8_t(255 - translucentRed.a);
831 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
832 }
833}
834
835TEST_F(LayerTransactionTest, SetFlagsSecure) {
836 sp<SurfaceControl> layer;
837 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
838 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
839
840 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
841 sp<IGraphicBufferProducer> producer;
842 sp<IGraphicBufferConsumer> consumer;
843 BufferQueue::createBufferQueue(&producer, &consumer);
844 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
845
846 Transaction()
847 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
848 .apply(true);
849 ASSERT_EQ(PERMISSION_DENIED,
850 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
851 false));
852
853 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
854 ASSERT_EQ(NO_ERROR,
855 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
856 false));
857}
858
Chia-I Wu2113bdd2017-11-01 15:16:35 -0700859TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
860 const Rect top(0, 0, 32, 16);
861 const Rect bottom(0, 16, 32, 32);
862 sp<SurfaceControl> layer;
863 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
864
865 ANativeWindow_Buffer buffer;
866 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
867 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
868 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
869 // setTransparentRegionHint always applies to the following buffer
870 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
871 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
872 {
873 SCOPED_TRACE("top transparent");
874 auto shot = screenshot();
875 shot->expectColor(top, Color::BLACK);
876 shot->expectColor(bottom, Color::RED);
877 }
878
879 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
880 {
881 SCOPED_TRACE("transparent region hint pending");
882 auto shot = screenshot();
883 shot->expectColor(top, Color::BLACK);
884 shot->expectColor(bottom, Color::RED);
885 }
886
887 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
888 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
889 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
890 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
891 {
892 SCOPED_TRACE("bottom transparent");
893 auto shot = screenshot();
894 shot->expectColor(top, Color::RED);
895 shot->expectColor(bottom, Color::BLACK);
896 }
897}
898
899TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
900 sp<SurfaceControl> layerTransparent;
901 sp<SurfaceControl> layerR;
902 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
903 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
904
905 // check that transparent region hint is bound by the layer size
906 Transaction()
907 .setTransparentRegionHint(layerTransparent,
908 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
909 .setPosition(layerR, 16, 16)
910 .setLayer(layerR, mLayerZBase + 1)
911 .apply();
912 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
913 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
914 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
915}
916
Chia-I Wua8a515e2017-11-01 15:16:35 -0700917TEST_F(LayerTransactionTest, SetAlphaBasic) {
918 sp<SurfaceControl> layer1;
919 sp<SurfaceControl> layer2;
920 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
921 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
922 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
923 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
924
925 Transaction()
926 .setAlpha(layer1, 0.25f)
927 .setAlpha(layer2, 0.75f)
928 .setPosition(layer2, 16, 0)
929 .setLayer(layer2, mLayerZBase + 1)
930 .apply();
931 {
932 auto shot = screenshot();
933 uint8_t r = 16; // 64 * 0.25f
934 uint8_t g = 48; // 64 * 0.75f
935 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
936 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
937
938 r /= 4; // r * (1.0f - 0.75f)
939 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
940 }
941}
942
943TEST_F(LayerTransactionTest, SetAlphaClamped) {
944 const Color color = {64, 0, 0, 255};
945 sp<SurfaceControl> layer;
946 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
947 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
948
949 Transaction().setAlpha(layer, 2.0f).apply();
950 {
951 SCOPED_TRACE("clamped to 1.0f");
952 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
953 }
954
955 Transaction().setAlpha(layer, -1.0f).apply();
956 {
957 SCOPED_TRACE("clamped to 0.0f");
958 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
959 }
960}
961
Chia-I Wue4ef6102017-11-01 15:16:35 -0700962TEST_F(LayerTransactionTest, SetColorBasic) {
963 sp<SurfaceControl> bufferLayer;
964 sp<SurfaceControl> colorLayer;
965 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
966 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
967 ASSERT_NO_FATAL_FAILURE(
968 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
969
970 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
971 {
972 SCOPED_TRACE("default color");
973 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
974 }
975
976 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
977 const Color expected = {15, 51, 85, 255};
978 // this is handwavy, but the precison loss scaled by 255 (8-bit per
979 // channel) should be less than one
980 const uint8_t tolerance = 1;
981 Transaction().setColor(colorLayer, color).apply();
982 {
983 SCOPED_TRACE("new color");
984 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
985 }
986}
987
988TEST_F(LayerTransactionTest, SetColorClamped) {
989 sp<SurfaceControl> colorLayer;
990 ASSERT_NO_FATAL_FAILURE(
991 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
992
993 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
994 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
995}
996
997TEST_F(LayerTransactionTest, SetColorWithAlpha) {
998 sp<SurfaceControl> bufferLayer;
999 sp<SurfaceControl> colorLayer;
1000 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1001 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1002 ASSERT_NO_FATAL_FAILURE(
1003 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1004
1005 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1006 const float alpha = 0.25f;
1007 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1008 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1009 // channel) should be less than one
1010 const uint8_t tolerance = 1;
1011 Transaction()
1012 .setColor(colorLayer, color)
1013 .setAlpha(colorLayer, alpha)
1014 .setLayer(colorLayer, mLayerZBase + 1)
1015 .apply();
1016 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1017 tolerance);
1018}
1019
1020TEST_F(LayerTransactionTest, SetColorWithBuffer) {
1021 sp<SurfaceControl> bufferLayer;
1022 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1023 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1024
1025 // color is ignored
1026 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1027 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1028}
1029
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001030TEST_F(LayerTransactionTest, SetLayerStackBasic) {
1031 sp<SurfaceControl> layer;
1032 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1033 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1034
1035 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1036 {
1037 SCOPED_TRACE("non-existing layer stack");
1038 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1039 }
1040
1041 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1042 {
1043 SCOPED_TRACE("original layer stack");
1044 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1045 }
1046}
1047
Chia-I Wu93853fe2017-11-02 08:30:27 -07001048TEST_F(LayerTransactionTest, SetMatrixBasic) {
1049 sp<SurfaceControl> layer;
1050 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1051 ASSERT_NO_FATAL_FAILURE(
1052 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1053
1054 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1055 {
1056 SCOPED_TRACE("IDENTITY");
1057 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1058 Color::WHITE);
1059 }
1060
1061 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1062 {
1063 SCOPED_TRACE("FLIP_H");
1064 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1065 Color::BLUE);
1066 }
1067
1068 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1069 {
1070 SCOPED_TRACE("FLIP_V");
1071 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1072 Color::GREEN);
1073 }
1074
1075 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1076 {
1077 SCOPED_TRACE("ROT_90");
1078 screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1079 Color::GREEN);
1080 }
1081
1082 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1083 {
1084 SCOPED_TRACE("SCALE");
1085 screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1086 Color::WHITE, true /* filtered */);
1087 }
1088}
1089
1090TEST_F(LayerTransactionTest, SetMatrixRot45) {
1091 sp<SurfaceControl> layer;
1092 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1093 ASSERT_NO_FATAL_FAILURE(
1094 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1095
1096 const float rot = M_SQRT1_2; // 45 degrees
1097 const float trans = M_SQRT2 * 16.0f;
1098 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1099
1100 auto shot = screenshot();
1101 // check a 8x8 region inside each color
1102 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1103 const int32_t halfL = 4;
1104 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1105 };
1106 const int32_t unit = int32_t(trans / 2);
1107 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1108 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1109 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1110 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1111}
1112
1113TEST_F(LayerTransactionTest, SetMatrixWithResize) {
1114 sp<SurfaceControl> layer;
1115 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1116 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1117
1118 // setMatrix is applied after any pending resize, unlike setPosition
1119 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1120 {
1121 SCOPED_TRACE("resize pending");
1122 auto shot = screenshot();
1123 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1124 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1125 }
1126
1127 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1128 {
1129 SCOPED_TRACE("resize applied");
1130 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1131 }
1132}
1133
1134TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) {
1135 sp<SurfaceControl> layer;
1136 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1137 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1138
1139 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1140 Transaction()
1141 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1142 .setSize(layer, 64, 64)
1143 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1144 .apply();
1145 screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1146}
1147
Chia-I Wua56b2042017-11-01 15:16:35 -07001148TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) {
1149 sp<SurfaceControl> layer;
1150 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1151 ASSERT_NO_FATAL_FAILURE(
1152 fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1153
1154 // XXX SCALE_CROP is not respected; calling setSize and
1155 // setOverrideScalingMode in separate transactions does not work
1156 // (b/69315456)
1157 Transaction()
1158 .setSize(layer, 64, 16)
1159 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1160 .apply();
1161 {
1162 SCOPED_TRACE("SCALE_TO_WINDOW");
1163 screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1164 Color::WHITE, true /* filtered */);
1165 }
1166}
1167
Chia-I Wu04dcca82017-11-02 08:30:27 -07001168TEST_F(LayerTransactionTest, SetCropBasic) {
1169 sp<SurfaceControl> layer;
1170 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1171 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1172 const Rect crop(8, 8, 24, 24);
1173
1174 Transaction().setCrop(layer, crop).apply();
1175 auto shot = screenshot();
1176 shot->expectColor(crop, Color::RED);
1177 shot->expectBorder(crop, Color::BLACK);
1178}
1179
1180TEST_F(LayerTransactionTest, SetCropEmpty) {
1181 sp<SurfaceControl> layer;
1182 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1183 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1184
1185 {
1186 SCOPED_TRACE("empty rect");
1187 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1188 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1189 }
1190
1191 {
1192 SCOPED_TRACE("negative rect");
1193 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1194 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1195 }
1196}
1197
1198TEST_F(LayerTransactionTest, SetCropOutOfBounds) {
1199 sp<SurfaceControl> layer;
1200 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1201 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1202
1203 Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1204 auto shot = screenshot();
1205 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1206 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1207}
1208
1209TEST_F(LayerTransactionTest, SetCropWithTranslation) {
1210 sp<SurfaceControl> layer;
1211 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1212 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1213
1214 const Point position(32, 32);
1215 const Rect crop(8, 8, 24, 24);
1216 Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1217 auto shot = screenshot();
1218 shot->expectColor(crop + position, Color::RED);
1219 shot->expectBorder(crop + position, Color::BLACK);
1220}
1221
1222TEST_F(LayerTransactionTest, SetCropWithScale) {
1223 sp<SurfaceControl> layer;
1224 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1225 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1226
1227 // crop is affected by matrix
1228 Transaction()
1229 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1230 .setCrop(layer, Rect(8, 8, 24, 24))
1231 .apply();
1232 auto shot = screenshot();
1233 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1234 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1235}
1236
1237TEST_F(LayerTransactionTest, SetCropWithResize) {
1238 sp<SurfaceControl> layer;
1239 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1240 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1241
1242 // setCrop is applied immediately by default, with or without resize pending
1243 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1244 {
1245 SCOPED_TRACE("resize pending");
1246 auto shot = screenshot();
1247 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1248 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1249 }
1250
1251 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1252 {
1253 SCOPED_TRACE("resize applied");
1254 auto shot = screenshot();
1255 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1256 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1257 }
1258}
1259
1260TEST_F(LayerTransactionTest, SetCropWithNextResize) {
1261 sp<SurfaceControl> layer;
1262 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1263 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1264
1265 // request setCrop to be applied with the next resize
1266 Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1267 {
1268 SCOPED_TRACE("waiting for next resize");
1269 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1270 }
1271
1272 Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1273 {
1274 SCOPED_TRACE("pending crop modified");
1275 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1276 }
1277
1278 Transaction().setSize(layer, 16, 16).apply();
1279 {
1280 SCOPED_TRACE("resize pending");
1281 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1282 }
1283
1284 // finally resize
1285 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1286 {
1287 SCOPED_TRACE("new crop applied");
1288 auto shot = screenshot();
1289 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1290 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1291 }
1292}
1293
1294TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow) {
1295 sp<SurfaceControl> layer;
1296 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1297 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1298
1299 // setCrop is not immediate even with SCALE_TO_WINDOW override
1300 Transaction()
1301 .setCrop(layer, Rect(4, 4, 12, 12))
1302 .setSize(layer, 16, 16)
1303 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1304 .setGeometryAppliesWithResize(layer)
1305 .apply();
1306 {
1307 SCOPED_TRACE("new crop pending");
1308 auto shot = screenshot();
1309 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1310 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1311 }
1312
1313 // XXX crop is never latched without other geometry change (b/69315677)
1314 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1315 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1316 Transaction().setPosition(layer, 0, 0).apply();
1317 {
1318 SCOPED_TRACE("new crop applied");
1319 auto shot = screenshot();
1320 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1321 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1322 }
1323}
1324
Chia-I Wucdd71a52017-11-02 08:30:27 -07001325TEST_F(LayerTransactionTest, SetFinalCropBasic) {
1326 sp<SurfaceControl> layer;
1327 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1328 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1329 const Rect crop(8, 8, 24, 24);
1330
1331 // same as in SetCropBasic
1332 Transaction().setFinalCrop(layer, crop).apply();
1333 auto shot = screenshot();
1334 shot->expectColor(crop, Color::RED);
1335 shot->expectBorder(crop, Color::BLACK);
1336}
1337
1338TEST_F(LayerTransactionTest, SetFinalCropEmpty) {
1339 sp<SurfaceControl> layer;
1340 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1341 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1342
1343 // same as in SetCropEmpty
1344 {
1345 SCOPED_TRACE("empty rect");
1346 Transaction().setFinalCrop(layer, Rect(8, 8, 8, 8)).apply();
1347 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1348 }
1349
1350 {
1351 SCOPED_TRACE("negative rect");
1352 Transaction().setFinalCrop(layer, Rect(8, 8, 0, 0)).apply();
1353 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1354 }
1355}
1356
1357TEST_F(LayerTransactionTest, SetFinalCropOutOfBounds) {
1358 sp<SurfaceControl> layer;
1359 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1360 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1361
1362 // same as in SetCropOutOfBounds
1363 Transaction().setFinalCrop(layer, Rect(-128, -64, 128, 64)).apply();
1364 auto shot = screenshot();
1365 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1366 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1367}
1368
1369TEST_F(LayerTransactionTest, SetFinalCropWithTranslation) {
1370 sp<SurfaceControl> layer;
1371 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1372 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1373
1374 // final crop is applied post-translation
1375 Transaction().setPosition(layer, 16, 16).setFinalCrop(layer, Rect(8, 8, 24, 24)).apply();
1376 auto shot = screenshot();
1377 shot->expectColor(Rect(16, 16, 24, 24), Color::RED);
1378 shot->expectBorder(Rect(16, 16, 24, 24), Color::BLACK);
1379}
1380
1381TEST_F(LayerTransactionTest, SetFinalCropWithScale) {
1382 sp<SurfaceControl> layer;
1383 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1384 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1385
1386 // final crop is not affected by matrix
1387 Transaction()
1388 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1389 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1390 .apply();
1391 auto shot = screenshot();
1392 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1393 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1394}
1395
1396TEST_F(LayerTransactionTest, SetFinalCropWithResize) {
1397 sp<SurfaceControl> layer;
1398 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1399 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1400
1401 // same as in SetCropWithResize
1402 Transaction().setFinalCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1403 {
1404 SCOPED_TRACE("resize pending");
1405 auto shot = screenshot();
1406 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1407 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1408 }
1409
1410 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1411 {
1412 SCOPED_TRACE("resize applied");
1413 auto shot = screenshot();
1414 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1415 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1416 }
1417}
1418
1419TEST_F(LayerTransactionTest, SetFinalCropWithNextResize) {
1420 sp<SurfaceControl> layer;
1421 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1422 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1423
1424 // same as in SetCropWithNextResize
1425 Transaction()
1426 .setFinalCrop(layer, Rect(8, 8, 24, 24))
1427 .setGeometryAppliesWithResize(layer)
1428 .apply();
1429 {
1430 SCOPED_TRACE("waiting for next resize");
1431 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1432 }
1433
1434 Transaction().setFinalCrop(layer, Rect(4, 4, 12, 12)).apply();
1435 {
1436 SCOPED_TRACE("pending final crop modified");
1437 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1438 }
1439
1440 Transaction().setSize(layer, 16, 16).apply();
1441 {
1442 SCOPED_TRACE("resize pending");
1443 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1444 }
1445
1446 // finally resize
1447 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1448 {
1449 SCOPED_TRACE("new final crop applied");
1450 auto shot = screenshot();
1451 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1452 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1453 }
1454}
1455
1456TEST_F(LayerTransactionTest, SetFinalCropWithNextResizeScaleToWindow) {
1457 sp<SurfaceControl> layer;
1458 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1459 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1460
1461 // same as in SetCropWithNextResizeScaleToWindow
1462 Transaction()
1463 .setFinalCrop(layer, Rect(4, 4, 12, 12))
1464 .setSize(layer, 16, 16)
1465 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1466 .setGeometryAppliesWithResize(layer)
1467 .apply();
1468 {
1469 SCOPED_TRACE("new final crop pending");
1470 auto shot = screenshot();
1471 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1472 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1473 }
1474
1475 // XXX final crop is never latched without other geometry change (b/69315677)
1476 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1477 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1478 Transaction().setPosition(layer, 0, 0).apply();
1479 {
1480 SCOPED_TRACE("new final crop applied");
1481 auto shot = screenshot();
1482 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1483 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1484 }
1485}
1486
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001487class LayerUpdateTest : public ::testing::Test {
1488protected:
1489 virtual void SetUp() {
1490 mComposerClient = new SurfaceComposerClient;
1491 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1492
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001493 sp<IBinder> display(
1494 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001495 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001496 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001497
1498 ssize_t displayWidth = info.w;
1499 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001500
1501 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001502 mBGSurfaceControl =
1503 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1504 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001505 ASSERT_TRUE(mBGSurfaceControl != NULL);
1506 ASSERT_TRUE(mBGSurfaceControl->isValid());
1507 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1508
1509 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001510 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1511 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001512 ASSERT_TRUE(mFGSurfaceControl != NULL);
1513 ASSERT_TRUE(mFGSurfaceControl->isValid());
1514
1515 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1516
1517 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001518 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1519 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001520 ASSERT_TRUE(mSyncSurfaceControl != NULL);
1521 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1522
1523 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1524
Robert Carr4cdc58f2017-08-23 14:22:20 -07001525 asTransaction([&](Transaction& t) {
1526 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001527
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001528 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001529
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001530 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1531 .setPosition(mFGSurfaceControl, 64, 64)
1532 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001533
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001534 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1535 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1536 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001537 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001538 }
1539
1540 virtual void TearDown() {
1541 mComposerClient->dispose();
1542 mBGSurfaceControl = 0;
1543 mFGSurfaceControl = 0;
1544 mSyncSurfaceControl = 0;
1545 mComposerClient = 0;
1546 }
1547
1548 void waitForPostedBuffers() {
1549 // Since the sync surface is in synchronous mode (i.e. double buffered)
1550 // posting three buffers to it should ensure that at least two
1551 // SurfaceFlinger::handlePageFlip calls have been made, which should
1552 // guaranteed that a buffer posted to another Surface has been retired.
1553 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1554 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1555 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1556 }
1557
Robert Carr4cdc58f2017-08-23 14:22:20 -07001558 void asTransaction(const std::function<void(Transaction&)>& exec) {
1559 Transaction t;
1560 exec(t);
1561 t.apply(true);
1562 }
1563
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001564 sp<SurfaceComposerClient> mComposerClient;
1565 sp<SurfaceControl> mBGSurfaceControl;
1566 sp<SurfaceControl> mFGSurfaceControl;
1567
1568 // This surface is used to ensure that the buffers posted to
1569 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1570 sp<SurfaceControl> mSyncSurfaceControl;
1571};
1572
Robert Carr7f619b22017-11-06 12:56:35 -08001573TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1574 sp<ScreenCapture> sc;
1575
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001576 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1577 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001578 fillSurfaceRGBA8(relative, 10, 10, 10);
1579 waitForPostedBuffers();
1580
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001581 Transaction{}
1582 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001583 .setPosition(relative, 64, 64)
1584 .apply();
1585
1586 {
1587 // The relative should be on top of the FG control.
1588 ScreenCapture::captureScreen(&sc);
1589 sc->checkPixel(64, 64, 10, 10, 10);
1590 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001591 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001592
1593 {
1594 // Nothing should change at this point.
1595 ScreenCapture::captureScreen(&sc);
1596 sc->checkPixel(64, 64, 10, 10, 10);
1597 }
1598
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001599 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001600
1601 {
1602 // Ensure that the relative was actually hidden, rather than
1603 // being left in the detached but visible state.
1604 ScreenCapture::captureScreen(&sc);
1605 sc->expectFGColor(64, 64);
1606 }
1607}
1608
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001609TEST_F(LayerUpdateTest, LayerMoveWorks) {
1610 sp<ScreenCapture> sc;
1611 {
1612 SCOPED_TRACE("before move");
1613 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001614 sc->expectBGColor(0, 12);
1615 sc->expectFGColor(75, 75);
1616 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001617 }
1618
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001619 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001620
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001621 {
1622 // This should reflect the new position, but not the new color.
1623 SCOPED_TRACE("after move, before redraw");
1624 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001625 sc->expectBGColor(24, 24);
1626 sc->expectBGColor(75, 75);
1627 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001628 }
1629
1630 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1631 waitForPostedBuffers();
1632 {
1633 // This should reflect the new position and the new color.
1634 SCOPED_TRACE("after redraw");
1635 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001636 sc->expectBGColor(24, 24);
1637 sc->expectBGColor(75, 75);
1638 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001639 }
1640}
1641
1642TEST_F(LayerUpdateTest, LayerResizeWorks) {
1643 sp<ScreenCapture> sc;
1644 {
1645 SCOPED_TRACE("before resize");
1646 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001647 sc->expectBGColor(0, 12);
1648 sc->expectFGColor(75, 75);
1649 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001650 }
1651
Steve Block9d453682011-12-20 16:23:08 +00001652 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001653 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +00001654 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001655 {
1656 // This should not reflect the new size or color because SurfaceFlinger
1657 // has not yet received a buffer of the correct size.
1658 SCOPED_TRACE("after resize, before redraw");
1659 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001660 sc->expectBGColor(0, 12);
1661 sc->expectFGColor(75, 75);
1662 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001663 }
1664
Steve Block9d453682011-12-20 16:23:08 +00001665 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001666 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1667 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +00001668 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001669 {
1670 // This should reflect the new size and the new color.
1671 SCOPED_TRACE("after redraw");
1672 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001673 sc->expectBGColor(24, 24);
1674 sc->checkPixel(75, 75, 63, 195, 63);
1675 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001676 }
1677}
1678
Haixia Shid5750962015-07-27 16:50:49 -07001679TEST_F(LayerUpdateTest, LayerCropWorks) {
1680 sp<ScreenCapture> sc;
1681 {
1682 SCOPED_TRACE("before crop");
1683 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001684 sc->expectBGColor(24, 24);
1685 sc->expectFGColor(75, 75);
1686 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001687 }
1688
Robert Carr4cdc58f2017-08-23 14:22:20 -07001689 asTransaction([&](Transaction& t) {
1690 Rect cropRect(16, 16, 32, 32);
1691 t.setCrop(mFGSurfaceControl, cropRect);
1692 });
Haixia Shid5750962015-07-27 16:50:49 -07001693 {
1694 // This should crop the foreground surface.
1695 SCOPED_TRACE("after crop");
1696 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001697 sc->expectBGColor(24, 24);
1698 sc->expectBGColor(75, 75);
1699 sc->expectFGColor(95, 80);
1700 sc->expectFGColor(80, 95);
1701 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -07001702 }
1703}
1704
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001705TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
1706 sp<ScreenCapture> sc;
1707 {
1708 SCOPED_TRACE("before crop");
1709 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001710 sc->expectBGColor(24, 24);
1711 sc->expectFGColor(75, 75);
1712 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001713 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001714 asTransaction([&](Transaction& t) {
1715 Rect cropRect(16, 16, 32, 32);
1716 t.setFinalCrop(mFGSurfaceControl, cropRect);
1717 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001718 {
1719 // This should crop the foreground surface.
1720 SCOPED_TRACE("after crop");
1721 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001722 sc->expectBGColor(24, 24);
1723 sc->expectBGColor(75, 75);
1724 sc->expectBGColor(95, 80);
1725 sc->expectBGColor(80, 95);
1726 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001727 }
1728}
1729
Haixia Shid5750962015-07-27 16:50:49 -07001730TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
1731 sp<ScreenCapture> sc;
1732 {
1733 SCOPED_TRACE("before setLayer");
1734 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001735 sc->expectBGColor(24, 24);
1736 sc->expectFGColor(75, 75);
1737 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001738 }
1739
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001740 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001741
Haixia Shid5750962015-07-27 16:50:49 -07001742 {
1743 // This should hide the foreground surface beneath the background.
1744 SCOPED_TRACE("after setLayer");
1745 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001746 sc->expectBGColor(24, 24);
1747 sc->expectBGColor(75, 75);
1748 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001749 }
1750}
1751
1752TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1753 sp<ScreenCapture> sc;
1754 {
1755 SCOPED_TRACE("before hide");
1756 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001757 sc->expectBGColor(24, 24);
1758 sc->expectFGColor(75, 75);
1759 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001760 }
1761
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001762 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001763
Haixia Shid5750962015-07-27 16:50:49 -07001764 {
1765 // This should hide the foreground surface.
1766 SCOPED_TRACE("after hide, before show");
1767 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001768 sc->expectBGColor(24, 24);
1769 sc->expectBGColor(75, 75);
1770 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001771 }
1772
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001773 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001774
Haixia Shid5750962015-07-27 16:50:49 -07001775 {
1776 // This should show the foreground surface.
1777 SCOPED_TRACE("after show");
1778 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001779 sc->expectBGColor(24, 24);
1780 sc->expectFGColor(75, 75);
1781 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001782 }
1783}
1784
1785TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1786 sp<ScreenCapture> sc;
1787 {
1788 SCOPED_TRACE("before setAlpha");
1789 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001790 sc->expectBGColor(24, 24);
1791 sc->expectFGColor(75, 75);
1792 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001793 }
1794
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001795 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001796
Haixia Shid5750962015-07-27 16:50:49 -07001797 {
1798 // This should set foreground to be 75% opaque.
1799 SCOPED_TRACE("after setAlpha");
1800 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001801 sc->expectBGColor(24, 24);
1802 sc->checkPixel(75, 75, 162, 63, 96);
1803 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001804 }
1805}
1806
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001807TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1808 sp<ScreenCapture> sc;
1809 {
1810 SCOPED_TRACE("before setLayerStack");
1811 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001812 sc->expectBGColor(24, 24);
1813 sc->expectFGColor(75, 75);
1814 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001815 }
1816
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001817 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001818 {
1819 // This should hide the foreground surface since it goes to a different
1820 // layer stack.
1821 SCOPED_TRACE("after setLayerStack");
1822 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001823 sc->expectBGColor(24, 24);
1824 sc->expectBGColor(75, 75);
1825 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001826 }
1827}
1828
1829TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1830 sp<ScreenCapture> sc;
1831 {
1832 SCOPED_TRACE("before setFlags");
1833 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001834 sc->expectBGColor(24, 24);
1835 sc->expectFGColor(75, 75);
1836 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001837 }
1838
Robert Carr4cdc58f2017-08-23 14:22:20 -07001839 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001840 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001841 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001842 {
1843 // This should hide the foreground surface
1844 SCOPED_TRACE("after setFlags");
1845 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001846 sc->expectBGColor(24, 24);
1847 sc->expectBGColor(75, 75);
1848 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001849 }
1850}
1851
1852TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1853 sp<ScreenCapture> sc;
1854 {
1855 SCOPED_TRACE("before setMatrix");
1856 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001857 sc->expectBGColor(24, 24);
1858 sc->expectFGColor(91, 96);
1859 sc->expectFGColor(96, 101);
1860 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001861 }
1862
Robert Carr4cdc58f2017-08-23 14:22:20 -07001863 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001864 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001865 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001866 {
1867 SCOPED_TRACE("after setMatrix");
1868 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001869 sc->expectBGColor(24, 24);
1870 sc->expectFGColor(91, 96);
1871 sc->expectBGColor(96, 91);
1872 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001873 }
1874}
1875
Robert Carr8d5227b2017-03-16 15:41:03 -07001876class GeometryLatchingTest : public LayerUpdateTest {
1877protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001878 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001879 SCOPED_TRACE(trace);
1880 ScreenCapture::captureScreen(&sc);
1881 // We find the leading edge of the FG surface.
1882 sc->expectFGColor(127, 127);
1883 sc->expectBGColor(128, 128);
1884 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001885
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001886 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001887
1888 void unlockFGBuffer() {
1889 sp<Surface> s = mFGSurfaceControl->getSurface();
1890 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1891 waitForPostedBuffers();
1892 }
1893
Robert Carr8d5227b2017-03-16 15:41:03 -07001894 void completeFGResize() {
1895 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1896 waitForPostedBuffers();
1897 }
1898 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001899 asTransaction([&](Transaction& t) {
1900 t.setSize(mFGSurfaceControl, 64, 64);
1901 t.setPosition(mFGSurfaceControl, 64, 64);
1902 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1903 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1904 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001905
1906 EXPECT_INITIAL_STATE("After restoring initial state");
1907 }
1908 sp<ScreenCapture> sc;
1909};
1910
1911TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1912 EXPECT_INITIAL_STATE("before anything");
1913
1914 // By default position can be updated even while
1915 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001916 asTransaction([&](Transaction& t) {
1917 t.setSize(mFGSurfaceControl, 32, 32);
1918 t.setPosition(mFGSurfaceControl, 100, 100);
1919 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001920
1921 {
1922 SCOPED_TRACE("After moving surface");
1923 ScreenCapture::captureScreen(&sc);
1924 // If we moved, the FG Surface should cover up what was previously BG
1925 // however if we didn't move the FG wouldn't be large enough now.
1926 sc->expectFGColor(163, 163);
1927 }
1928
1929 restoreInitialState();
1930
1931 // Now we repeat with setGeometryAppliesWithResize
1932 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001933 asTransaction([&](Transaction& t) {
1934 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1935 t.setSize(mFGSurfaceControl, 32, 32);
1936 t.setPosition(mFGSurfaceControl, 100, 100);
1937 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001938
1939 {
1940 SCOPED_TRACE("While resize is pending");
1941 ScreenCapture::captureScreen(&sc);
1942 // This time we shouldn't have moved, so the BG color
1943 // should still be visible.
1944 sc->expectBGColor(128, 128);
1945 }
1946
1947 completeFGResize();
1948
1949 {
1950 SCOPED_TRACE("After the resize");
1951 ScreenCapture::captureScreen(&sc);
1952 // But after the resize completes, we should move
1953 // and the FG should be visible here.
1954 sc->expectFGColor(128, 128);
1955 }
1956}
1957
1958class CropLatchingTest : public GeometryLatchingTest {
1959protected:
1960 void EXPECT_CROPPED_STATE(const char* trace) {
1961 SCOPED_TRACE(trace);
1962 ScreenCapture::captureScreen(&sc);
1963 // The edge should be moved back one pixel by our crop.
1964 sc->expectFGColor(126, 126);
1965 sc->expectBGColor(127, 127);
1966 sc->expectBGColor(128, 128);
1967 }
chaviw59f5c562017-06-28 16:39:06 -07001968
1969 void EXPECT_RESIZE_STATE(const char* trace) {
1970 SCOPED_TRACE(trace);
1971 ScreenCapture::captureScreen(&sc);
1972 // The FG is now resized too 128,128 at 64,64
1973 sc->expectFGColor(64, 64);
1974 sc->expectFGColor(191, 191);
1975 sc->expectBGColor(192, 192);
1976 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001977};
1978
1979TEST_F(CropLatchingTest, CropLatching) {
1980 EXPECT_INITIAL_STATE("before anything");
1981 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001982 asTransaction([&](Transaction& t) {
1983 t.setSize(mFGSurfaceControl, 128, 128);
1984 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1985 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001986
1987 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1988
1989 restoreInitialState();
1990
Robert Carr4cdc58f2017-08-23 14:22:20 -07001991 asTransaction([&](Transaction& t) {
1992 t.setSize(mFGSurfaceControl, 128, 128);
1993 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1994 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1995 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001996
1997 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1998
1999 completeFGResize();
2000
2001 EXPECT_CROPPED_STATE("after the resize finishes");
2002}
2003
2004TEST_F(CropLatchingTest, FinalCropLatching) {
2005 EXPECT_INITIAL_STATE("before anything");
2006 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07002007 asTransaction([&](Transaction& t) {
2008 t.setSize(mFGSurfaceControl, 128, 128);
2009 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
2010 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002011
2012 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
2013
2014 restoreInitialState();
2015
Robert Carr4cdc58f2017-08-23 14:22:20 -07002016 asTransaction([&](Transaction& t) {
2017 t.setSize(mFGSurfaceControl, 128, 128);
2018 t.setGeometryAppliesWithResize(mFGSurfaceControl);
2019 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
2020 });
Robert Carr8d5227b2017-03-16 15:41:03 -07002021
2022 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
2023
2024 completeFGResize();
2025
2026 EXPECT_CROPPED_STATE("after the resize finishes");
2027}
2028
Robert Carr7bf247e2017-05-18 14:02:49 -07002029// In this test we ensure that setGeometryAppliesWithResize actually demands
2030// a buffer of the new size, and not just any size.
2031TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
2032 EXPECT_INITIAL_STATE("before anything");
2033 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07002034 asTransaction([&](Transaction& t) {
2035 t.setSize(mFGSurfaceControl, 128, 128);
2036 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
2037 });
Robert Carr7bf247e2017-05-18 14:02:49 -07002038
2039 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
2040
2041 restoreInitialState();
2042
2043 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
2044 // initiating the resize.
2045 lockAndFillFGBuffer();
2046
Robert Carr4cdc58f2017-08-23 14:22:20 -07002047 asTransaction([&](Transaction& t) {
2048 t.setSize(mFGSurfaceControl, 128, 128);
2049 t.setGeometryAppliesWithResize(mFGSurfaceControl);
2050 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
2051 });
Robert Carr7bf247e2017-05-18 14:02:49 -07002052
2053 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
2054
2055 // We now submit our old buffer, at the old size, and ensure it doesn't
2056 // trigger geometry latching.
2057 unlockFGBuffer();
2058
2059 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
2060
2061 completeFGResize();
2062
2063 EXPECT_CROPPED_STATE("after the resize finishes");
2064}
2065
2066TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
2067 EXPECT_INITIAL_STATE("before anything");
2068 // In this scenario, we attempt to set the final crop a second time while the resize
2069 // is still pending, and ensure we are successful. Success meaning the second crop
2070 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07002071 asTransaction([&](Transaction& t) {
2072 t.setSize(mFGSurfaceControl, 128, 128);
2073 t.setGeometryAppliesWithResize(mFGSurfaceControl);
2074 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
2075 });
Robert Carr7bf247e2017-05-18 14:02:49 -07002076
chaviw59f5c562017-06-28 16:39:06 -07002077 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
2078
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002079 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07002080
chaviw59f5c562017-06-28 16:39:06 -07002081 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07002082
2083 completeFGResize();
2084
chaviw59f5c562017-06-28 16:39:06 -07002085 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07002086}
2087
Pablo Ceballos05289c22016-04-14 15:49:55 -07002088TEST_F(LayerUpdateTest, DeferredTransactionTest) {
2089 sp<ScreenCapture> sc;
2090 {
2091 SCOPED_TRACE("before anything");
2092 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002093 sc->expectBGColor(32, 32);
2094 sc->expectFGColor(96, 96);
2095 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002096 }
2097
2098 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07002099 asTransaction([&](Transaction& t) {
2100 t.setAlpha(mFGSurfaceControl, 0.75);
2101 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002102 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002103 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002104
Robert Carr4cdc58f2017-08-23 14:22:20 -07002105 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002106 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002107 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002108 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002109 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002110
2111 {
2112 SCOPED_TRACE("before any trigger");
2113 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002114 sc->expectBGColor(32, 32);
2115 sc->expectFGColor(96, 96);
2116 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002117 }
2118
2119 // should trigger the first deferred transaction, but not the second one
2120 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2121 {
2122 SCOPED_TRACE("after first trigger");
2123 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002124 sc->expectBGColor(32, 32);
2125 sc->checkPixel(96, 96, 162, 63, 96);
2126 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002127 }
2128
2129 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002130 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07002131
2132 // trigger the second deferred transaction
2133 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
2134 {
2135 SCOPED_TRACE("after second trigger");
2136 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08002137 sc->expectBGColor(32, 32);
2138 sc->expectBGColor(96, 96);
2139 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07002140 }
2141}
2142
Robert Carrdb66e622017-04-10 16:55:57 -07002143TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
2144 sp<ScreenCapture> sc;
2145 {
2146 SCOPED_TRACE("before adding relative surface");
2147 ScreenCapture::captureScreen(&sc);
2148 sc->expectBGColor(24, 24);
2149 sc->expectFGColor(75, 75);
2150 sc->expectBGColor(145, 145);
2151 }
2152
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002153 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
2154 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07002155 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
2156 waitForPostedBuffers();
2157
2158 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07002159 asTransaction([&](Transaction& t) {
2160 t.setPosition(relativeSurfaceControl, 64, 64);
2161 t.show(relativeSurfaceControl);
2162 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
2163 });
Robert Carrdb66e622017-04-10 16:55:57 -07002164
2165 {
2166 SCOPED_TRACE("after adding relative surface");
2167 ScreenCapture::captureScreen(&sc);
2168 // our relative surface should be visible now.
2169 sc->checkPixel(75, 75, 255, 177, 177);
2170 }
2171
2172 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002173 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07002174
2175 {
2176 SCOPED_TRACE("after set layer");
2177 ScreenCapture::captureScreen(&sc);
2178 // now the FG surface should be visible again.
2179 sc->expectFGColor(75, 75);
2180 }
2181}
2182
Robert Carre392b552017-09-19 12:16:05 -07002183TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
2184 sp<ScreenCapture> sc;
2185
2186 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002187 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
2188 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2189 sp<SurfaceControl> childBuffer =
2190 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
2191 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07002192 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
2193
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002194 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002195
2196 {
2197 ScreenCapture::captureScreen(&sc);
2198 sc->expectChildColor(73, 73);
2199 sc->expectFGColor(74, 74);
2200 }
2201
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002202 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07002203
2204 {
2205 ScreenCapture::captureScreen(&sc);
2206 sc->expectChildColor(73, 73);
2207 sc->expectChildColor(74, 74);
2208 }
2209}
2210
Robert Carr2c5f6d22017-09-26 12:30:35 -07002211TEST_F(LayerUpdateTest, MergingTransactions) {
2212 sp<ScreenCapture> sc;
2213 {
2214 SCOPED_TRACE("before move");
2215 ScreenCapture::captureScreen(&sc);
2216 sc->expectBGColor(0, 12);
2217 sc->expectFGColor(75, 75);
2218 sc->expectBGColor(145, 145);
2219 }
2220
2221 Transaction t1, t2;
2222 t1.setPosition(mFGSurfaceControl, 128, 128);
2223 t2.setPosition(mFGSurfaceControl, 0, 0);
2224 // We expect that the position update from t2 now
2225 // overwrites the position update from t1.
2226 t1.merge(std::move(t2));
2227 t1.apply();
2228
2229 {
2230 ScreenCapture::captureScreen(&sc);
2231 sc->expectFGColor(1, 1);
2232 }
2233}
2234
Robert Carr1f0a16a2016-10-24 16:27:39 -07002235class ChildLayerTest : public LayerUpdateTest {
2236protected:
2237 void SetUp() override {
2238 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002239 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2240 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002241 fillSurfaceRGBA8(mChild, 200, 200, 200);
2242
2243 {
2244 SCOPED_TRACE("before anything");
2245 ScreenCapture::captureScreen(&mCapture);
2246 mCapture->expectChildColor(64, 64);
2247 }
2248 }
2249 void TearDown() override {
2250 LayerUpdateTest::TearDown();
2251 mChild = 0;
2252 }
2253
2254 sp<SurfaceControl> mChild;
2255 sp<ScreenCapture> mCapture;
2256};
2257
2258TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002259 asTransaction([&](Transaction& t) {
2260 t.show(mChild);
2261 t.setPosition(mChild, 10, 10);
2262 t.setPosition(mFGSurfaceControl, 64, 64);
2263 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002264
2265 {
2266 ScreenCapture::captureScreen(&mCapture);
2267 // Top left of foreground must now be visible
2268 mCapture->expectFGColor(64, 64);
2269 // But 10 pixels in we should see the child surface
2270 mCapture->expectChildColor(74, 74);
2271 // And 10 more pixels we should be back to the foreground surface
2272 mCapture->expectFGColor(84, 84);
2273 }
2274
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002275 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002276
2277 {
2278 ScreenCapture::captureScreen(&mCapture);
2279 // Top left of foreground should now be at 0, 0
2280 mCapture->expectFGColor(0, 0);
2281 // But 10 pixels in we should see the child surface
2282 mCapture->expectChildColor(10, 10);
2283 // And 10 more pixels we should be back to the foreground surface
2284 mCapture->expectFGColor(20, 20);
2285 }
2286}
2287
Robert Carr41b08b52017-06-01 16:11:34 -07002288TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002289 asTransaction([&](Transaction& t) {
2290 t.show(mChild);
2291 t.setPosition(mChild, 0, 0);
2292 t.setPosition(mFGSurfaceControl, 0, 0);
2293 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
2294 });
Robert Carr41b08b52017-06-01 16:11:34 -07002295
2296 {
2297 ScreenCapture::captureScreen(&mCapture);
2298 mCapture->expectChildColor(0, 0);
2299 mCapture->expectChildColor(4, 4);
2300 mCapture->expectBGColor(5, 5);
2301 }
2302}
2303
2304TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002305 asTransaction([&](Transaction& t) {
2306 t.show(mChild);
2307 t.setPosition(mChild, 0, 0);
2308 t.setPosition(mFGSurfaceControl, 0, 0);
2309 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
2310 });
Robert Carr41b08b52017-06-01 16:11:34 -07002311
2312 {
2313 ScreenCapture::captureScreen(&mCapture);
2314 mCapture->expectChildColor(0, 0);
2315 mCapture->expectChildColor(4, 4);
2316 mCapture->expectBGColor(5, 5);
2317 }
2318}
2319
Robert Carr1f0a16a2016-10-24 16:27:39 -07002320TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002321 asTransaction([&](Transaction& t) {
2322 t.show(mChild);
2323 t.setPosition(mFGSurfaceControl, 0, 0);
2324 t.setPosition(mChild, 63, 63);
2325 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002326
2327 {
2328 ScreenCapture::captureScreen(&mCapture);
2329 mCapture->expectFGColor(0, 0);
2330 // Last pixel in foreground should now be the child.
2331 mCapture->expectChildColor(63, 63);
2332 // But the child should be constrained and the next pixel
2333 // must be the background
2334 mCapture->expectBGColor(64, 64);
2335 }
2336}
2337
2338TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002339 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002340
2341 // Find the boundary between the parent and child
2342 {
2343 ScreenCapture::captureScreen(&mCapture);
2344 mCapture->expectChildColor(9, 9);
2345 mCapture->expectFGColor(10, 10);
2346 }
2347
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002348 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002349
2350 // The boundary should be twice as far from the origin now.
2351 // The pixels from the last test should all be child now
2352 {
2353 ScreenCapture::captureScreen(&mCapture);
2354 mCapture->expectChildColor(9, 9);
2355 mCapture->expectChildColor(10, 10);
2356 mCapture->expectChildColor(19, 19);
2357 mCapture->expectFGColor(20, 20);
2358 }
2359}
Robert Carr9524cb32017-02-13 11:32:32 -08002360
Robert Carr6452f122017-03-21 10:41:29 -07002361TEST_F(ChildLayerTest, ChildLayerAlpha) {
2362 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2363 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2364 fillSurfaceRGBA8(mChild, 0, 254, 0);
2365 waitForPostedBuffers();
2366
Robert Carr4cdc58f2017-08-23 14:22:20 -07002367 asTransaction([&](Transaction& t) {
2368 t.show(mChild);
2369 t.setPosition(mChild, 0, 0);
2370 t.setPosition(mFGSurfaceControl, 0, 0);
2371 });
Robert Carr6452f122017-03-21 10:41:29 -07002372
2373 {
2374 ScreenCapture::captureScreen(&mCapture);
2375 // Unblended child color
2376 mCapture->checkPixel(0, 0, 0, 254, 0);
2377 }
2378
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002379 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002380
2381 {
2382 ScreenCapture::captureScreen(&mCapture);
2383 // Child and BG blended.
2384 mCapture->checkPixel(0, 0, 127, 127, 0);
2385 }
2386
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002387 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002388
2389 {
2390 ScreenCapture::captureScreen(&mCapture);
2391 // Child and BG blended.
2392 mCapture->checkPixel(0, 0, 95, 64, 95);
2393 }
2394}
2395
Robert Carr9524cb32017-02-13 11:32:32 -08002396TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002397 asTransaction([&](Transaction& t) {
2398 t.show(mChild);
2399 t.setPosition(mChild, 10, 10);
2400 t.setPosition(mFGSurfaceControl, 64, 64);
2401 });
Robert Carr9524cb32017-02-13 11:32:32 -08002402
2403 {
2404 ScreenCapture::captureScreen(&mCapture);
2405 // Top left of foreground must now be visible
2406 mCapture->expectFGColor(64, 64);
2407 // But 10 pixels in we should see the child surface
2408 mCapture->expectChildColor(74, 74);
2409 // And 10 more pixels we should be back to the foreground surface
2410 mCapture->expectFGColor(84, 84);
2411 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002412
2413 asTransaction([&](Transaction& t) {
2414 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2415 });
2416
Robert Carr9524cb32017-02-13 11:32:32 -08002417 {
2418 ScreenCapture::captureScreen(&mCapture);
2419 mCapture->expectFGColor(64, 64);
2420 // In reparenting we should have exposed the entire foreground surface.
2421 mCapture->expectFGColor(74, 74);
2422 // And the child layer should now begin at 10, 10 (since the BG
2423 // layer is at (0, 0)).
2424 mCapture->expectBGColor(9, 9);
2425 mCapture->expectChildColor(10, 10);
2426 }
2427}
2428
chaviw161410b02017-07-27 10:46:08 -07002429TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002430 asTransaction([&](Transaction& t) {
2431 t.show(mChild);
2432 t.setPosition(mChild, 10, 10);
2433 t.setPosition(mFGSurfaceControl, 64, 64);
2434 });
Robert Carr9524cb32017-02-13 11:32:32 -08002435
2436 {
2437 ScreenCapture::captureScreen(&mCapture);
2438 // Top left of foreground must now be visible
2439 mCapture->expectFGColor(64, 64);
2440 // But 10 pixels in we should see the child surface
2441 mCapture->expectChildColor(74, 74);
2442 // And 10 more pixels we should be back to the foreground surface
2443 mCapture->expectFGColor(84, 84);
2444 }
2445
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002446 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002447
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002448 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002449
chaviw161410b02017-07-27 10:46:08 -07002450 // Since the child has the same client as the parent, it will not get
2451 // detached and will be hidden.
2452 {
2453 ScreenCapture::captureScreen(&mCapture);
2454 mCapture->expectFGColor(64, 64);
2455 mCapture->expectFGColor(74, 74);
2456 mCapture->expectFGColor(84, 84);
2457 }
2458}
2459
2460TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2461 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002462 sp<SurfaceControl> mChildNewClient =
2463 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2464 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002465
2466 ASSERT_TRUE(mChildNewClient != NULL);
2467 ASSERT_TRUE(mChildNewClient->isValid());
2468
2469 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2470
Robert Carr4cdc58f2017-08-23 14:22:20 -07002471 asTransaction([&](Transaction& t) {
2472 t.hide(mChild);
2473 t.show(mChildNewClient);
2474 t.setPosition(mChildNewClient, 10, 10);
2475 t.setPosition(mFGSurfaceControl, 64, 64);
2476 });
chaviw161410b02017-07-27 10:46:08 -07002477
2478 {
2479 ScreenCapture::captureScreen(&mCapture);
2480 // Top left of foreground must now be visible
2481 mCapture->expectFGColor(64, 64);
2482 // But 10 pixels in we should see the child surface
2483 mCapture->expectChildColor(74, 74);
2484 // And 10 more pixels we should be back to the foreground surface
2485 mCapture->expectFGColor(84, 84);
2486 }
2487
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002488 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002489
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002490 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002491
Robert Carr9524cb32017-02-13 11:32:32 -08002492 // Nothing should have changed.
2493 {
2494 ScreenCapture::captureScreen(&mCapture);
2495 mCapture->expectFGColor(64, 64);
2496 mCapture->expectChildColor(74, 74);
2497 mCapture->expectFGColor(84, 84);
2498 }
2499}
2500
Robert Carr9b429f42017-04-17 14:56:57 -07002501TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002502 asTransaction([&](Transaction& t) {
2503 t.show(mChild);
2504 t.setPosition(mChild, 0, 0);
2505 t.setPosition(mFGSurfaceControl, 0, 0);
2506 });
Robert Carr9b429f42017-04-17 14:56:57 -07002507
2508 {
2509 ScreenCapture::captureScreen(&mCapture);
2510 // We've positioned the child in the top left.
2511 mCapture->expectChildColor(0, 0);
2512 // But it's only 10x10.
2513 mCapture->expectFGColor(10, 10);
2514 }
2515
Robert Carr4cdc58f2017-08-23 14:22:20 -07002516 asTransaction([&](Transaction& t) {
2517 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2518 // We cause scaling by 2.
2519 t.setSize(mFGSurfaceControl, 128, 128);
2520 });
Robert Carr9b429f42017-04-17 14:56:57 -07002521
2522 {
2523 ScreenCapture::captureScreen(&mCapture);
2524 // We've positioned the child in the top left.
2525 mCapture->expectChildColor(0, 0);
2526 mCapture->expectChildColor(10, 10);
2527 mCapture->expectChildColor(19, 19);
2528 // And now it should be scaled all the way to 20x20
2529 mCapture->expectFGColor(20, 20);
2530 }
2531}
2532
Robert Carr1725eee2017-04-26 18:32:15 -07002533// Regression test for b/37673612
2534TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002535 asTransaction([&](Transaction& t) {
2536 t.show(mChild);
2537 t.setPosition(mChild, 0, 0);
2538 t.setPosition(mFGSurfaceControl, 0, 0);
2539 });
Robert Carr1725eee2017-04-26 18:32:15 -07002540
2541 {
2542 ScreenCapture::captureScreen(&mCapture);
2543 // We've positioned the child in the top left.
2544 mCapture->expectChildColor(0, 0);
2545 // But it's only 10x10.
2546 mCapture->expectFGColor(10, 10);
2547 }
Robert Carr1725eee2017-04-26 18:32:15 -07002548 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2549 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002550 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002551 sp<Surface> s = mFGSurfaceControl->getSurface();
2552 auto anw = static_cast<ANativeWindow*>(s.get());
2553 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2554 native_window_set_buffers_dimensions(anw, 64, 128);
2555 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2556 waitForPostedBuffers();
2557
2558 {
2559 // The child should still be in the same place and not have any strange scaling as in
2560 // b/37673612.
2561 ScreenCapture::captureScreen(&mCapture);
2562 mCapture->expectChildColor(0, 0);
2563 mCapture->expectFGColor(10, 10);
2564 }
2565}
2566
Dan Stoza412903f2017-04-27 13:42:17 -07002567TEST_F(ChildLayerTest, Bug36858924) {
2568 // Destroy the child layer
2569 mChild.clear();
2570
2571 // Now recreate it as hidden
2572 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2573 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2574 mFGSurfaceControl.get());
2575
2576 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002577 asTransaction([&](Transaction& t) {
2578 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002579 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002580 t.show(mChild);
2581 });
Dan Stoza412903f2017-04-27 13:42:17 -07002582
2583 // Render the foreground surface a few times
2584 //
2585 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2586 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2587 // never acquire/release the first buffer
2588 ALOGI("Filling 1");
2589 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2590 ALOGI("Filling 2");
2591 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2592 ALOGI("Filling 3");
2593 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2594 ALOGI("Filling 4");
2595 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2596}
2597
chaviwf1961f72017-09-18 16:41:07 -07002598TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002599 asTransaction([&](Transaction& t) {
2600 t.show(mChild);
2601 t.setPosition(mChild, 10, 10);
2602 t.setPosition(mFGSurfaceControl, 64, 64);
2603 });
chaviw06178942017-07-27 10:25:59 -07002604
2605 {
2606 ScreenCapture::captureScreen(&mCapture);
2607 // Top left of foreground must now be visible
2608 mCapture->expectFGColor(64, 64);
2609 // But 10 pixels in we should see the child surface
2610 mCapture->expectChildColor(74, 74);
2611 // And 10 more pixels we should be back to the foreground surface
2612 mCapture->expectFGColor(84, 84);
2613 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002614
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002615 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002616
chaviw06178942017-07-27 10:25:59 -07002617 {
2618 ScreenCapture::captureScreen(&mCapture);
2619 mCapture->expectFGColor(64, 64);
2620 // In reparenting we should have exposed the entire foreground surface.
2621 mCapture->expectFGColor(74, 74);
2622 // And the child layer should now begin at 10, 10 (since the BG
2623 // layer is at (0, 0)).
2624 mCapture->expectBGColor(9, 9);
2625 mCapture->expectChildColor(10, 10);
2626 }
2627}
2628
chaviwf1961f72017-09-18 16:41:07 -07002629TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002630 asTransaction([&](Transaction& t) {
2631 t.show(mChild);
2632 t.setPosition(mChild, 10, 10);
2633 t.setPosition(mFGSurfaceControl, 64, 64);
2634 });
chaviwf1961f72017-09-18 16:41:07 -07002635
2636 {
2637 ScreenCapture::captureScreen(&mCapture);
2638 // Top left of foreground must now be visible
2639 mCapture->expectFGColor(64, 64);
2640 // But 10 pixels in we should see the child surface
2641 mCapture->expectChildColor(74, 74);
2642 // And 10 more pixels we should be back to the foreground surface
2643 mCapture->expectFGColor(84, 84);
2644 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002645 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002646 {
2647 ScreenCapture::captureScreen(&mCapture);
2648 // Nothing should have changed.
2649 mCapture->expectFGColor(64, 64);
2650 mCapture->expectChildColor(74, 74);
2651 mCapture->expectFGColor(84, 84);
2652 }
2653}
2654
2655TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002656 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2657 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07002658 ASSERT_TRUE(newSurface != NULL);
2659 ASSERT_TRUE(newSurface->isValid());
2660
2661 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002662 asTransaction([&](Transaction& t) {
2663 t.hide(mChild);
2664 t.show(newSurface);
2665 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002666 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002667 t.setPosition(mFGSurfaceControl, 64, 64);
2668 });
chaviwf1961f72017-09-18 16:41:07 -07002669
2670 {
2671 ScreenCapture::captureScreen(&mCapture);
2672 // Top left of foreground must now be visible
2673 mCapture->expectFGColor(64, 64);
2674 // At 10, 10 we should see the new surface
2675 mCapture->checkPixel(10, 10, 63, 195, 63);
2676 }
2677
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002678 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002679
2680 {
2681 ScreenCapture::captureScreen(&mCapture);
2682 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2683 // mFGSurface, putting it at 74, 74.
2684 mCapture->expectFGColor(64, 64);
2685 mCapture->checkPixel(74, 74, 63, 195, 63);
2686 mCapture->expectFGColor(84, 84);
2687 }
2688}
2689
chaviwc9674332017-08-28 12:32:18 -07002690TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002691 sp<SurfaceControl> grandchild =
2692 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2693 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002694 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2695
2696 {
2697 ScreenCapture::captureScreen(&mCapture);
2698 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2699 // which begins at 64, 64
2700 mCapture->checkPixel(64, 64, 50, 50, 50);
2701 }
2702}
2703
Robert Carr503c7042017-09-27 15:06:08 -07002704TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002705 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2706 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002707 fillSurfaceRGBA8(relative, 255, 255, 255);
2708
2709 Transaction t;
2710 t.setLayer(relative, INT32_MAX)
2711 .setRelativeLayer(mChild, relative->getHandle(), 1)
2712 .setPosition(mFGSurfaceControl, 0, 0)
2713 .apply(true);
2714
2715 // We expect that the child should have been elevated above our
2716 // INT_MAX layer even though it's not a child of it.
2717 {
2718 ScreenCapture::captureScreen(&mCapture);
2719 mCapture->expectChildColor(0, 0);
2720 mCapture->expectChildColor(9, 9);
2721 mCapture->checkPixel(10, 10, 255, 255, 255);
2722 }
2723}
2724
chaviw13fdc492017-06-27 12:40:18 -07002725class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002726protected:
chaviw13fdc492017-06-27 12:40:18 -07002727 void SetUp() override {
2728 LayerUpdateTest::SetUp();
2729
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002730 mLayerColorControl =
2731 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
2732 PIXEL_FORMAT_RGBA_8888,
2733 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07002734
2735 ASSERT_TRUE(mLayerColorControl != NULL);
2736 ASSERT_TRUE(mLayerColorControl->isValid());
2737
Robert Carr4cdc58f2017-08-23 14:22:20 -07002738 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002739 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002740 t.setPosition(mLayerColorControl, 140, 140);
2741 t.hide(mLayerColorControl);
2742 t.hide(mFGSurfaceControl);
2743 });
chaviw13fdc492017-06-27 12:40:18 -07002744 }
2745
2746 void TearDown() override {
2747 LayerUpdateTest::TearDown();
2748 mLayerColorControl = 0;
2749 }
2750
2751 sp<SurfaceControl> mLayerColorControl;
2752};
2753
2754TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2755 sp<ScreenCapture> sc;
2756
2757 {
2758 SCOPED_TRACE("before setColor");
2759 ScreenCapture::captureScreen(&sc);
2760 sc->expectBGColor(145, 145);
2761 }
2762
Robert Carr4cdc58f2017-08-23 14:22:20 -07002763 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002764 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002765 t.setColor(mLayerColorControl, color);
2766 t.show(mLayerColorControl);
2767 });
chaviw13fdc492017-06-27 12:40:18 -07002768
chaviw13fdc492017-06-27 12:40:18 -07002769 {
2770 // There should now be a color
2771 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002772
chaviw13fdc492017-06-27 12:40:18 -07002773 ScreenCapture::captureScreen(&sc);
2774 sc->checkPixel(145, 145, 43, 207, 131);
2775 }
2776}
2777
2778TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2779 sp<ScreenCapture> sc;
2780 {
2781 SCOPED_TRACE("before setColor");
2782 ScreenCapture::captureScreen(&sc);
2783 sc->expectBGColor(145, 145);
2784 }
2785
Robert Carr4cdc58f2017-08-23 14:22:20 -07002786 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002787 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002788 t.setColor(mLayerColorControl, color);
2789 t.setAlpha(mLayerColorControl, .75f);
2790 t.show(mLayerColorControl);
2791 });
2792
chaviw13fdc492017-06-27 12:40:18 -07002793 {
2794 // There should now be a color with .75 alpha
2795 SCOPED_TRACE("after setColor");
2796 ScreenCapture::captureScreen(&sc);
2797 sc->checkPixel(145, 145, 48, 171, 147);
2798 }
2799}
2800
2801TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2802 sp<ScreenCapture> sc;
2803 {
2804 SCOPED_TRACE("before setColor");
2805 ScreenCapture::captureScreen(&sc);
2806 sc->expectBGColor(145, 145);
2807 }
2808
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002809 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002810
chaviw13fdc492017-06-27 12:40:18 -07002811 {
2812 // There should now be set to 0,0,0 (black) as default.
2813 SCOPED_TRACE("after setColor");
2814 ScreenCapture::captureScreen(&sc);
2815 sc->checkPixel(145, 145, 0, 0, 0);
2816 }
2817}
2818
chaviwa76b2712017-09-20 12:02:26 -07002819class ScreenCaptureTest : public LayerUpdateTest {
2820protected:
2821 std::unique_ptr<CaptureLayer> mCapture;
2822};
2823
2824TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2825 auto bgHandle = mBGSurfaceControl->getHandle();
2826 CaptureLayer::captureScreen(&mCapture, bgHandle);
2827 mCapture->expectBGColor(0, 0);
2828 // Doesn't capture FG layer which is at 64, 64
2829 mCapture->expectBGColor(64, 64);
2830}
2831
2832TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2833 auto fgHandle = mFGSurfaceControl->getHandle();
2834
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002835 sp<SurfaceControl> child =
2836 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2837 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002838 fillSurfaceRGBA8(child, 200, 200, 200);
2839
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002840 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002841
2842 // Captures mFGSurfaceControl layer and its child.
2843 CaptureLayer::captureScreen(&mCapture, fgHandle);
2844 mCapture->expectFGColor(10, 10);
2845 mCapture->expectChildColor(0, 0);
2846}
2847
2848TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2849 auto fgHandle = mFGSurfaceControl->getHandle();
2850
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002851 sp<SurfaceControl> child =
2852 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2853 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002854 fillSurfaceRGBA8(child, 200, 200, 200);
2855
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002856 sp<SurfaceControl> grandchild =
2857 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2858 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002859
2860 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2861 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002862 .show(child)
2863 .setPosition(grandchild, 5, 5)
2864 .show(grandchild)
2865 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002866
2867 // Captures mFGSurfaceControl, its child, and the grandchild.
2868 CaptureLayer::captureScreen(&mCapture, fgHandle);
2869 mCapture->expectFGColor(10, 10);
2870 mCapture->expectChildColor(0, 0);
2871 mCapture->checkPixel(5, 5, 50, 50, 50);
2872}
2873
2874TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002875 sp<SurfaceControl> child =
2876 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2877 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002878 fillSurfaceRGBA8(child, 200, 200, 200);
2879 auto childHandle = child->getHandle();
2880
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002881 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002882
2883 // Captures only the child layer, and not the parent.
2884 CaptureLayer::captureScreen(&mCapture, childHandle);
2885 mCapture->expectChildColor(0, 0);
2886 mCapture->expectChildColor(9, 9);
2887}
2888
2889TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002890 sp<SurfaceControl> child =
2891 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2892 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002893 fillSurfaceRGBA8(child, 200, 200, 200);
2894 auto childHandle = child->getHandle();
2895
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002896 sp<SurfaceControl> grandchild =
2897 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2898 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002899 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2900
2901 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002902 .show(child)
2903 .setPosition(grandchild, 5, 5)
2904 .show(grandchild)
2905 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002906
2907 auto grandchildHandle = grandchild->getHandle();
2908
2909 // Captures only the grandchild.
2910 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2911 mCapture->checkPixel(0, 0, 50, 50, 50);
2912 mCapture->checkPixel(4, 4, 50, 50, 50);
2913}
2914
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002915} // namespace android