blob: 004ad7d43042735b8379d1361585b1ac8c7a6fa0 [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
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001168class LayerUpdateTest : public ::testing::Test {
1169protected:
1170 virtual void SetUp() {
1171 mComposerClient = new SurfaceComposerClient;
1172 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1173
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001174 sp<IBinder> display(
1175 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001176 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001177 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001178
1179 ssize_t displayWidth = info.w;
1180 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001181
1182 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001183 mBGSurfaceControl =
1184 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1185 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001186 ASSERT_TRUE(mBGSurfaceControl != NULL);
1187 ASSERT_TRUE(mBGSurfaceControl->isValid());
1188 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1189
1190 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001191 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1192 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001193 ASSERT_TRUE(mFGSurfaceControl != NULL);
1194 ASSERT_TRUE(mFGSurfaceControl->isValid());
1195
1196 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1197
1198 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001199 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1200 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001201 ASSERT_TRUE(mSyncSurfaceControl != NULL);
1202 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1203
1204 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1205
Robert Carr4cdc58f2017-08-23 14:22:20 -07001206 asTransaction([&](Transaction& t) {
1207 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001208
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001209 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001210
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001211 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1212 .setPosition(mFGSurfaceControl, 64, 64)
1213 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001214
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001215 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1216 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1217 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001218 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001219 }
1220
1221 virtual void TearDown() {
1222 mComposerClient->dispose();
1223 mBGSurfaceControl = 0;
1224 mFGSurfaceControl = 0;
1225 mSyncSurfaceControl = 0;
1226 mComposerClient = 0;
1227 }
1228
1229 void waitForPostedBuffers() {
1230 // Since the sync surface is in synchronous mode (i.e. double buffered)
1231 // posting three buffers to it should ensure that at least two
1232 // SurfaceFlinger::handlePageFlip calls have been made, which should
1233 // guaranteed that a buffer posted to another Surface has been retired.
1234 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1235 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1236 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1237 }
1238
Robert Carr4cdc58f2017-08-23 14:22:20 -07001239 void asTransaction(const std::function<void(Transaction&)>& exec) {
1240 Transaction t;
1241 exec(t);
1242 t.apply(true);
1243 }
1244
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001245 sp<SurfaceComposerClient> mComposerClient;
1246 sp<SurfaceControl> mBGSurfaceControl;
1247 sp<SurfaceControl> mFGSurfaceControl;
1248
1249 // This surface is used to ensure that the buffers posted to
1250 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1251 sp<SurfaceControl> mSyncSurfaceControl;
1252};
1253
Robert Carr7f619b22017-11-06 12:56:35 -08001254TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1255 sp<ScreenCapture> sc;
1256
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001257 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1258 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001259 fillSurfaceRGBA8(relative, 10, 10, 10);
1260 waitForPostedBuffers();
1261
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001262 Transaction{}
1263 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001264 .setPosition(relative, 64, 64)
1265 .apply();
1266
1267 {
1268 // The relative should be on top of the FG control.
1269 ScreenCapture::captureScreen(&sc);
1270 sc->checkPixel(64, 64, 10, 10, 10);
1271 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001272 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001273
1274 {
1275 // Nothing should change at this point.
1276 ScreenCapture::captureScreen(&sc);
1277 sc->checkPixel(64, 64, 10, 10, 10);
1278 }
1279
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001280 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001281
1282 {
1283 // Ensure that the relative was actually hidden, rather than
1284 // being left in the detached but visible state.
1285 ScreenCapture::captureScreen(&sc);
1286 sc->expectFGColor(64, 64);
1287 }
1288}
1289
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001290TEST_F(LayerUpdateTest, LayerMoveWorks) {
1291 sp<ScreenCapture> sc;
1292 {
1293 SCOPED_TRACE("before move");
1294 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001295 sc->expectBGColor(0, 12);
1296 sc->expectFGColor(75, 75);
1297 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001298 }
1299
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001300 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001301
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001302 {
1303 // This should reflect the new position, but not the new color.
1304 SCOPED_TRACE("after move, before redraw");
1305 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001306 sc->expectBGColor(24, 24);
1307 sc->expectBGColor(75, 75);
1308 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001309 }
1310
1311 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1312 waitForPostedBuffers();
1313 {
1314 // This should reflect the new position and the new color.
1315 SCOPED_TRACE("after redraw");
1316 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001317 sc->expectBGColor(24, 24);
1318 sc->expectBGColor(75, 75);
1319 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001320 }
1321}
1322
1323TEST_F(LayerUpdateTest, LayerResizeWorks) {
1324 sp<ScreenCapture> sc;
1325 {
1326 SCOPED_TRACE("before resize");
1327 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001328 sc->expectBGColor(0, 12);
1329 sc->expectFGColor(75, 75);
1330 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001331 }
1332
Steve Block9d453682011-12-20 16:23:08 +00001333 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001334 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +00001335 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001336 {
1337 // This should not reflect the new size or color because SurfaceFlinger
1338 // has not yet received a buffer of the correct size.
1339 SCOPED_TRACE("after resize, before redraw");
1340 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001341 sc->expectBGColor(0, 12);
1342 sc->expectFGColor(75, 75);
1343 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001344 }
1345
Steve Block9d453682011-12-20 16:23:08 +00001346 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001347 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1348 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +00001349 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001350 {
1351 // This should reflect the new size and the new color.
1352 SCOPED_TRACE("after redraw");
1353 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001354 sc->expectBGColor(24, 24);
1355 sc->checkPixel(75, 75, 63, 195, 63);
1356 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001357 }
1358}
1359
Haixia Shid5750962015-07-27 16:50:49 -07001360TEST_F(LayerUpdateTest, LayerCropWorks) {
1361 sp<ScreenCapture> sc;
1362 {
1363 SCOPED_TRACE("before crop");
1364 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001365 sc->expectBGColor(24, 24);
1366 sc->expectFGColor(75, 75);
1367 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001368 }
1369
Robert Carr4cdc58f2017-08-23 14:22:20 -07001370 asTransaction([&](Transaction& t) {
1371 Rect cropRect(16, 16, 32, 32);
1372 t.setCrop(mFGSurfaceControl, cropRect);
1373 });
Haixia Shid5750962015-07-27 16:50:49 -07001374 {
1375 // This should crop the foreground surface.
1376 SCOPED_TRACE("after crop");
1377 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001378 sc->expectBGColor(24, 24);
1379 sc->expectBGColor(75, 75);
1380 sc->expectFGColor(95, 80);
1381 sc->expectFGColor(80, 95);
1382 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -07001383 }
1384}
1385
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001386TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
1387 sp<ScreenCapture> sc;
1388 {
1389 SCOPED_TRACE("before crop");
1390 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001391 sc->expectBGColor(24, 24);
1392 sc->expectFGColor(75, 75);
1393 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001394 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001395 asTransaction([&](Transaction& t) {
1396 Rect cropRect(16, 16, 32, 32);
1397 t.setFinalCrop(mFGSurfaceControl, cropRect);
1398 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001399 {
1400 // This should crop the foreground surface.
1401 SCOPED_TRACE("after crop");
1402 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001403 sc->expectBGColor(24, 24);
1404 sc->expectBGColor(75, 75);
1405 sc->expectBGColor(95, 80);
1406 sc->expectBGColor(80, 95);
1407 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001408 }
1409}
1410
Haixia Shid5750962015-07-27 16:50:49 -07001411TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
1412 sp<ScreenCapture> sc;
1413 {
1414 SCOPED_TRACE("before setLayer");
1415 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001416 sc->expectBGColor(24, 24);
1417 sc->expectFGColor(75, 75);
1418 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001419 }
1420
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001421 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001422
Haixia Shid5750962015-07-27 16:50:49 -07001423 {
1424 // This should hide the foreground surface beneath the background.
1425 SCOPED_TRACE("after setLayer");
1426 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001427 sc->expectBGColor(24, 24);
1428 sc->expectBGColor(75, 75);
1429 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001430 }
1431}
1432
1433TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1434 sp<ScreenCapture> sc;
1435 {
1436 SCOPED_TRACE("before hide");
1437 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001438 sc->expectBGColor(24, 24);
1439 sc->expectFGColor(75, 75);
1440 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001441 }
1442
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001443 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001444
Haixia Shid5750962015-07-27 16:50:49 -07001445 {
1446 // This should hide the foreground surface.
1447 SCOPED_TRACE("after hide, before show");
1448 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001449 sc->expectBGColor(24, 24);
1450 sc->expectBGColor(75, 75);
1451 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001452 }
1453
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001454 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001455
Haixia Shid5750962015-07-27 16:50:49 -07001456 {
1457 // This should show the foreground surface.
1458 SCOPED_TRACE("after show");
1459 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001460 sc->expectBGColor(24, 24);
1461 sc->expectFGColor(75, 75);
1462 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001463 }
1464}
1465
1466TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1467 sp<ScreenCapture> sc;
1468 {
1469 SCOPED_TRACE("before setAlpha");
1470 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001471 sc->expectBGColor(24, 24);
1472 sc->expectFGColor(75, 75);
1473 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001474 }
1475
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001476 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001477
Haixia Shid5750962015-07-27 16:50:49 -07001478 {
1479 // This should set foreground to be 75% opaque.
1480 SCOPED_TRACE("after setAlpha");
1481 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001482 sc->expectBGColor(24, 24);
1483 sc->checkPixel(75, 75, 162, 63, 96);
1484 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001485 }
1486}
1487
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001488TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1489 sp<ScreenCapture> sc;
1490 {
1491 SCOPED_TRACE("before setLayerStack");
1492 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001493 sc->expectBGColor(24, 24);
1494 sc->expectFGColor(75, 75);
1495 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001496 }
1497
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001498 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001499 {
1500 // This should hide the foreground surface since it goes to a different
1501 // layer stack.
1502 SCOPED_TRACE("after setLayerStack");
1503 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001504 sc->expectBGColor(24, 24);
1505 sc->expectBGColor(75, 75);
1506 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001507 }
1508}
1509
1510TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1511 sp<ScreenCapture> sc;
1512 {
1513 SCOPED_TRACE("before setFlags");
1514 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001515 sc->expectBGColor(24, 24);
1516 sc->expectFGColor(75, 75);
1517 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001518 }
1519
Robert Carr4cdc58f2017-08-23 14:22:20 -07001520 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001521 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001522 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001523 {
1524 // This should hide the foreground surface
1525 SCOPED_TRACE("after setFlags");
1526 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001527 sc->expectBGColor(24, 24);
1528 sc->expectBGColor(75, 75);
1529 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001530 }
1531}
1532
1533TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1534 sp<ScreenCapture> sc;
1535 {
1536 SCOPED_TRACE("before setMatrix");
1537 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001538 sc->expectBGColor(24, 24);
1539 sc->expectFGColor(91, 96);
1540 sc->expectFGColor(96, 101);
1541 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001542 }
1543
Robert Carr4cdc58f2017-08-23 14:22:20 -07001544 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001545 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001546 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001547 {
1548 SCOPED_TRACE("after setMatrix");
1549 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001550 sc->expectBGColor(24, 24);
1551 sc->expectFGColor(91, 96);
1552 sc->expectBGColor(96, 91);
1553 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001554 }
1555}
1556
Robert Carr8d5227b2017-03-16 15:41:03 -07001557class GeometryLatchingTest : public LayerUpdateTest {
1558protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001559 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001560 SCOPED_TRACE(trace);
1561 ScreenCapture::captureScreen(&sc);
1562 // We find the leading edge of the FG surface.
1563 sc->expectFGColor(127, 127);
1564 sc->expectBGColor(128, 128);
1565 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001566
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001567 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001568
1569 void unlockFGBuffer() {
1570 sp<Surface> s = mFGSurfaceControl->getSurface();
1571 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1572 waitForPostedBuffers();
1573 }
1574
Robert Carr8d5227b2017-03-16 15:41:03 -07001575 void completeFGResize() {
1576 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1577 waitForPostedBuffers();
1578 }
1579 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001580 asTransaction([&](Transaction& t) {
1581 t.setSize(mFGSurfaceControl, 64, 64);
1582 t.setPosition(mFGSurfaceControl, 64, 64);
1583 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1584 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1585 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001586
1587 EXPECT_INITIAL_STATE("After restoring initial state");
1588 }
1589 sp<ScreenCapture> sc;
1590};
1591
1592TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1593 EXPECT_INITIAL_STATE("before anything");
1594
1595 // By default position can be updated even while
1596 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001597 asTransaction([&](Transaction& t) {
1598 t.setSize(mFGSurfaceControl, 32, 32);
1599 t.setPosition(mFGSurfaceControl, 100, 100);
1600 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001601
1602 {
1603 SCOPED_TRACE("After moving surface");
1604 ScreenCapture::captureScreen(&sc);
1605 // If we moved, the FG Surface should cover up what was previously BG
1606 // however if we didn't move the FG wouldn't be large enough now.
1607 sc->expectFGColor(163, 163);
1608 }
1609
1610 restoreInitialState();
1611
1612 // Now we repeat with setGeometryAppliesWithResize
1613 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001614 asTransaction([&](Transaction& t) {
1615 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1616 t.setSize(mFGSurfaceControl, 32, 32);
1617 t.setPosition(mFGSurfaceControl, 100, 100);
1618 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001619
1620 {
1621 SCOPED_TRACE("While resize is pending");
1622 ScreenCapture::captureScreen(&sc);
1623 // This time we shouldn't have moved, so the BG color
1624 // should still be visible.
1625 sc->expectBGColor(128, 128);
1626 }
1627
1628 completeFGResize();
1629
1630 {
1631 SCOPED_TRACE("After the resize");
1632 ScreenCapture::captureScreen(&sc);
1633 // But after the resize completes, we should move
1634 // and the FG should be visible here.
1635 sc->expectFGColor(128, 128);
1636 }
1637}
1638
1639class CropLatchingTest : public GeometryLatchingTest {
1640protected:
1641 void EXPECT_CROPPED_STATE(const char* trace) {
1642 SCOPED_TRACE(trace);
1643 ScreenCapture::captureScreen(&sc);
1644 // The edge should be moved back one pixel by our crop.
1645 sc->expectFGColor(126, 126);
1646 sc->expectBGColor(127, 127);
1647 sc->expectBGColor(128, 128);
1648 }
chaviw59f5c562017-06-28 16:39:06 -07001649
1650 void EXPECT_RESIZE_STATE(const char* trace) {
1651 SCOPED_TRACE(trace);
1652 ScreenCapture::captureScreen(&sc);
1653 // The FG is now resized too 128,128 at 64,64
1654 sc->expectFGColor(64, 64);
1655 sc->expectFGColor(191, 191);
1656 sc->expectBGColor(192, 192);
1657 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001658};
1659
1660TEST_F(CropLatchingTest, CropLatching) {
1661 EXPECT_INITIAL_STATE("before anything");
1662 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001663 asTransaction([&](Transaction& t) {
1664 t.setSize(mFGSurfaceControl, 128, 128);
1665 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1666 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001667
1668 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1669
1670 restoreInitialState();
1671
Robert Carr4cdc58f2017-08-23 14:22:20 -07001672 asTransaction([&](Transaction& t) {
1673 t.setSize(mFGSurfaceControl, 128, 128);
1674 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1675 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1676 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001677
1678 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1679
1680 completeFGResize();
1681
1682 EXPECT_CROPPED_STATE("after the resize finishes");
1683}
1684
1685TEST_F(CropLatchingTest, FinalCropLatching) {
1686 EXPECT_INITIAL_STATE("before anything");
1687 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001688 asTransaction([&](Transaction& t) {
1689 t.setSize(mFGSurfaceControl, 128, 128);
1690 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1691 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001692
1693 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1694
1695 restoreInitialState();
1696
Robert Carr4cdc58f2017-08-23 14:22:20 -07001697 asTransaction([&](Transaction& t) {
1698 t.setSize(mFGSurfaceControl, 128, 128);
1699 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1700 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1701 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001702
1703 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1704
1705 completeFGResize();
1706
1707 EXPECT_CROPPED_STATE("after the resize finishes");
1708}
1709
Robert Carr7bf247e2017-05-18 14:02:49 -07001710// In this test we ensure that setGeometryAppliesWithResize actually demands
1711// a buffer of the new size, and not just any size.
1712TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1713 EXPECT_INITIAL_STATE("before anything");
1714 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001715 asTransaction([&](Transaction& t) {
1716 t.setSize(mFGSurfaceControl, 128, 128);
1717 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1718 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001719
1720 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1721
1722 restoreInitialState();
1723
1724 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1725 // initiating the resize.
1726 lockAndFillFGBuffer();
1727
Robert Carr4cdc58f2017-08-23 14:22:20 -07001728 asTransaction([&](Transaction& t) {
1729 t.setSize(mFGSurfaceControl, 128, 128);
1730 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1731 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1732 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001733
1734 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1735
1736 // We now submit our old buffer, at the old size, and ensure it doesn't
1737 // trigger geometry latching.
1738 unlockFGBuffer();
1739
1740 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1741
1742 completeFGResize();
1743
1744 EXPECT_CROPPED_STATE("after the resize finishes");
1745}
1746
1747TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1748 EXPECT_INITIAL_STATE("before anything");
1749 // In this scenario, we attempt to set the final crop a second time while the resize
1750 // is still pending, and ensure we are successful. Success meaning the second crop
1751 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001752 asTransaction([&](Transaction& t) {
1753 t.setSize(mFGSurfaceControl, 128, 128);
1754 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1755 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1756 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001757
chaviw59f5c562017-06-28 16:39:06 -07001758 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1759
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001760 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001761
chaviw59f5c562017-06-28 16:39:06 -07001762 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001763
1764 completeFGResize();
1765
chaviw59f5c562017-06-28 16:39:06 -07001766 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001767}
1768
Pablo Ceballos05289c22016-04-14 15:49:55 -07001769TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1770 sp<ScreenCapture> sc;
1771 {
1772 SCOPED_TRACE("before anything");
1773 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001774 sc->expectBGColor(32, 32);
1775 sc->expectFGColor(96, 96);
1776 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001777 }
1778
1779 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001780 asTransaction([&](Transaction& t) {
1781 t.setAlpha(mFGSurfaceControl, 0.75);
1782 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001783 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001784 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001785
Robert Carr4cdc58f2017-08-23 14:22:20 -07001786 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001787 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001788 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001789 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001790 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001791
1792 {
1793 SCOPED_TRACE("before any trigger");
1794 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001795 sc->expectBGColor(32, 32);
1796 sc->expectFGColor(96, 96);
1797 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001798 }
1799
1800 // should trigger the first deferred transaction, but not the second one
1801 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1802 {
1803 SCOPED_TRACE("after first trigger");
1804 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001805 sc->expectBGColor(32, 32);
1806 sc->checkPixel(96, 96, 162, 63, 96);
1807 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001808 }
1809
1810 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001811 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001812
1813 // trigger the second deferred transaction
1814 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1815 {
1816 SCOPED_TRACE("after second trigger");
1817 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001818 sc->expectBGColor(32, 32);
1819 sc->expectBGColor(96, 96);
1820 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001821 }
1822}
1823
Robert Carrdb66e622017-04-10 16:55:57 -07001824TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1825 sp<ScreenCapture> sc;
1826 {
1827 SCOPED_TRACE("before adding relative surface");
1828 ScreenCapture::captureScreen(&sc);
1829 sc->expectBGColor(24, 24);
1830 sc->expectFGColor(75, 75);
1831 sc->expectBGColor(145, 145);
1832 }
1833
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001834 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1835 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001836 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1837 waitForPostedBuffers();
1838
1839 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001840 asTransaction([&](Transaction& t) {
1841 t.setPosition(relativeSurfaceControl, 64, 64);
1842 t.show(relativeSurfaceControl);
1843 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1844 });
Robert Carrdb66e622017-04-10 16:55:57 -07001845
1846 {
1847 SCOPED_TRACE("after adding relative surface");
1848 ScreenCapture::captureScreen(&sc);
1849 // our relative surface should be visible now.
1850 sc->checkPixel(75, 75, 255, 177, 177);
1851 }
1852
1853 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001854 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001855
1856 {
1857 SCOPED_TRACE("after set layer");
1858 ScreenCapture::captureScreen(&sc);
1859 // now the FG surface should be visible again.
1860 sc->expectFGColor(75, 75);
1861 }
1862}
1863
Robert Carre392b552017-09-19 12:16:05 -07001864TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1865 sp<ScreenCapture> sc;
1866
1867 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001868 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1869 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1870 sp<SurfaceControl> childBuffer =
1871 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1872 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001873 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1874
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001875 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001876
1877 {
1878 ScreenCapture::captureScreen(&sc);
1879 sc->expectChildColor(73, 73);
1880 sc->expectFGColor(74, 74);
1881 }
1882
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001883 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001884
1885 {
1886 ScreenCapture::captureScreen(&sc);
1887 sc->expectChildColor(73, 73);
1888 sc->expectChildColor(74, 74);
1889 }
1890}
1891
Robert Carr2c5f6d22017-09-26 12:30:35 -07001892TEST_F(LayerUpdateTest, MergingTransactions) {
1893 sp<ScreenCapture> sc;
1894 {
1895 SCOPED_TRACE("before move");
1896 ScreenCapture::captureScreen(&sc);
1897 sc->expectBGColor(0, 12);
1898 sc->expectFGColor(75, 75);
1899 sc->expectBGColor(145, 145);
1900 }
1901
1902 Transaction t1, t2;
1903 t1.setPosition(mFGSurfaceControl, 128, 128);
1904 t2.setPosition(mFGSurfaceControl, 0, 0);
1905 // We expect that the position update from t2 now
1906 // overwrites the position update from t1.
1907 t1.merge(std::move(t2));
1908 t1.apply();
1909
1910 {
1911 ScreenCapture::captureScreen(&sc);
1912 sc->expectFGColor(1, 1);
1913 }
1914}
1915
Robert Carr1f0a16a2016-10-24 16:27:39 -07001916class ChildLayerTest : public LayerUpdateTest {
1917protected:
1918 void SetUp() override {
1919 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001920 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1921 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001922 fillSurfaceRGBA8(mChild, 200, 200, 200);
1923
1924 {
1925 SCOPED_TRACE("before anything");
1926 ScreenCapture::captureScreen(&mCapture);
1927 mCapture->expectChildColor(64, 64);
1928 }
1929 }
1930 void TearDown() override {
1931 LayerUpdateTest::TearDown();
1932 mChild = 0;
1933 }
1934
1935 sp<SurfaceControl> mChild;
1936 sp<ScreenCapture> mCapture;
1937};
1938
1939TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001940 asTransaction([&](Transaction& t) {
1941 t.show(mChild);
1942 t.setPosition(mChild, 10, 10);
1943 t.setPosition(mFGSurfaceControl, 64, 64);
1944 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001945
1946 {
1947 ScreenCapture::captureScreen(&mCapture);
1948 // Top left of foreground must now be visible
1949 mCapture->expectFGColor(64, 64);
1950 // But 10 pixels in we should see the child surface
1951 mCapture->expectChildColor(74, 74);
1952 // And 10 more pixels we should be back to the foreground surface
1953 mCapture->expectFGColor(84, 84);
1954 }
1955
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001956 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001957
1958 {
1959 ScreenCapture::captureScreen(&mCapture);
1960 // Top left of foreground should now be at 0, 0
1961 mCapture->expectFGColor(0, 0);
1962 // But 10 pixels in we should see the child surface
1963 mCapture->expectChildColor(10, 10);
1964 // And 10 more pixels we should be back to the foreground surface
1965 mCapture->expectFGColor(20, 20);
1966 }
1967}
1968
Robert Carr41b08b52017-06-01 16:11:34 -07001969TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001970 asTransaction([&](Transaction& t) {
1971 t.show(mChild);
1972 t.setPosition(mChild, 0, 0);
1973 t.setPosition(mFGSurfaceControl, 0, 0);
1974 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1975 });
Robert Carr41b08b52017-06-01 16:11:34 -07001976
1977 {
1978 ScreenCapture::captureScreen(&mCapture);
1979 mCapture->expectChildColor(0, 0);
1980 mCapture->expectChildColor(4, 4);
1981 mCapture->expectBGColor(5, 5);
1982 }
1983}
1984
1985TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001986 asTransaction([&](Transaction& t) {
1987 t.show(mChild);
1988 t.setPosition(mChild, 0, 0);
1989 t.setPosition(mFGSurfaceControl, 0, 0);
1990 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1991 });
Robert Carr41b08b52017-06-01 16:11:34 -07001992
1993 {
1994 ScreenCapture::captureScreen(&mCapture);
1995 mCapture->expectChildColor(0, 0);
1996 mCapture->expectChildColor(4, 4);
1997 mCapture->expectBGColor(5, 5);
1998 }
1999}
2000
Robert Carr1f0a16a2016-10-24 16:27:39 -07002001TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002002 asTransaction([&](Transaction& t) {
2003 t.show(mChild);
2004 t.setPosition(mFGSurfaceControl, 0, 0);
2005 t.setPosition(mChild, 63, 63);
2006 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002007
2008 {
2009 ScreenCapture::captureScreen(&mCapture);
2010 mCapture->expectFGColor(0, 0);
2011 // Last pixel in foreground should now be the child.
2012 mCapture->expectChildColor(63, 63);
2013 // But the child should be constrained and the next pixel
2014 // must be the background
2015 mCapture->expectBGColor(64, 64);
2016 }
2017}
2018
2019TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002020 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002021
2022 // Find the boundary between the parent and child
2023 {
2024 ScreenCapture::captureScreen(&mCapture);
2025 mCapture->expectChildColor(9, 9);
2026 mCapture->expectFGColor(10, 10);
2027 }
2028
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002029 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07002030
2031 // The boundary should be twice as far from the origin now.
2032 // The pixels from the last test should all be child now
2033 {
2034 ScreenCapture::captureScreen(&mCapture);
2035 mCapture->expectChildColor(9, 9);
2036 mCapture->expectChildColor(10, 10);
2037 mCapture->expectChildColor(19, 19);
2038 mCapture->expectFGColor(20, 20);
2039 }
2040}
Robert Carr9524cb32017-02-13 11:32:32 -08002041
Robert Carr6452f122017-03-21 10:41:29 -07002042TEST_F(ChildLayerTest, ChildLayerAlpha) {
2043 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2044 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2045 fillSurfaceRGBA8(mChild, 0, 254, 0);
2046 waitForPostedBuffers();
2047
Robert Carr4cdc58f2017-08-23 14:22:20 -07002048 asTransaction([&](Transaction& t) {
2049 t.show(mChild);
2050 t.setPosition(mChild, 0, 0);
2051 t.setPosition(mFGSurfaceControl, 0, 0);
2052 });
Robert Carr6452f122017-03-21 10:41:29 -07002053
2054 {
2055 ScreenCapture::captureScreen(&mCapture);
2056 // Unblended child color
2057 mCapture->checkPixel(0, 0, 0, 254, 0);
2058 }
2059
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002060 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002061
2062 {
2063 ScreenCapture::captureScreen(&mCapture);
2064 // Child and BG blended.
2065 mCapture->checkPixel(0, 0, 127, 127, 0);
2066 }
2067
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002068 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07002069
2070 {
2071 ScreenCapture::captureScreen(&mCapture);
2072 // Child and BG blended.
2073 mCapture->checkPixel(0, 0, 95, 64, 95);
2074 }
2075}
2076
Robert Carr9524cb32017-02-13 11:32:32 -08002077TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002078 asTransaction([&](Transaction& t) {
2079 t.show(mChild);
2080 t.setPosition(mChild, 10, 10);
2081 t.setPosition(mFGSurfaceControl, 64, 64);
2082 });
Robert Carr9524cb32017-02-13 11:32:32 -08002083
2084 {
2085 ScreenCapture::captureScreen(&mCapture);
2086 // Top left of foreground must now be visible
2087 mCapture->expectFGColor(64, 64);
2088 // But 10 pixels in we should see the child surface
2089 mCapture->expectChildColor(74, 74);
2090 // And 10 more pixels we should be back to the foreground surface
2091 mCapture->expectFGColor(84, 84);
2092 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002093
2094 asTransaction([&](Transaction& t) {
2095 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2096 });
2097
Robert Carr9524cb32017-02-13 11:32:32 -08002098 {
2099 ScreenCapture::captureScreen(&mCapture);
2100 mCapture->expectFGColor(64, 64);
2101 // In reparenting we should have exposed the entire foreground surface.
2102 mCapture->expectFGColor(74, 74);
2103 // And the child layer should now begin at 10, 10 (since the BG
2104 // layer is at (0, 0)).
2105 mCapture->expectBGColor(9, 9);
2106 mCapture->expectChildColor(10, 10);
2107 }
2108}
2109
chaviw161410b02017-07-27 10:46:08 -07002110TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002111 asTransaction([&](Transaction& t) {
2112 t.show(mChild);
2113 t.setPosition(mChild, 10, 10);
2114 t.setPosition(mFGSurfaceControl, 64, 64);
2115 });
Robert Carr9524cb32017-02-13 11:32:32 -08002116
2117 {
2118 ScreenCapture::captureScreen(&mCapture);
2119 // Top left of foreground must now be visible
2120 mCapture->expectFGColor(64, 64);
2121 // But 10 pixels in we should see the child surface
2122 mCapture->expectChildColor(74, 74);
2123 // And 10 more pixels we should be back to the foreground surface
2124 mCapture->expectFGColor(84, 84);
2125 }
2126
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002127 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08002128
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002129 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08002130
chaviw161410b02017-07-27 10:46:08 -07002131 // Since the child has the same client as the parent, it will not get
2132 // detached and will be hidden.
2133 {
2134 ScreenCapture::captureScreen(&mCapture);
2135 mCapture->expectFGColor(64, 64);
2136 mCapture->expectFGColor(74, 74);
2137 mCapture->expectFGColor(84, 84);
2138 }
2139}
2140
2141TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2142 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002143 sp<SurfaceControl> mChildNewClient =
2144 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2145 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07002146
2147 ASSERT_TRUE(mChildNewClient != NULL);
2148 ASSERT_TRUE(mChildNewClient->isValid());
2149
2150 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2151
Robert Carr4cdc58f2017-08-23 14:22:20 -07002152 asTransaction([&](Transaction& t) {
2153 t.hide(mChild);
2154 t.show(mChildNewClient);
2155 t.setPosition(mChildNewClient, 10, 10);
2156 t.setPosition(mFGSurfaceControl, 64, 64);
2157 });
chaviw161410b02017-07-27 10:46:08 -07002158
2159 {
2160 ScreenCapture::captureScreen(&mCapture);
2161 // Top left of foreground must now be visible
2162 mCapture->expectFGColor(64, 64);
2163 // But 10 pixels in we should see the child surface
2164 mCapture->expectChildColor(74, 74);
2165 // And 10 more pixels we should be back to the foreground surface
2166 mCapture->expectFGColor(84, 84);
2167 }
2168
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002169 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002170
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002171 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002172
Robert Carr9524cb32017-02-13 11:32:32 -08002173 // Nothing should have changed.
2174 {
2175 ScreenCapture::captureScreen(&mCapture);
2176 mCapture->expectFGColor(64, 64);
2177 mCapture->expectChildColor(74, 74);
2178 mCapture->expectFGColor(84, 84);
2179 }
2180}
2181
Robert Carr9b429f42017-04-17 14:56:57 -07002182TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002183 asTransaction([&](Transaction& t) {
2184 t.show(mChild);
2185 t.setPosition(mChild, 0, 0);
2186 t.setPosition(mFGSurfaceControl, 0, 0);
2187 });
Robert Carr9b429f42017-04-17 14:56:57 -07002188
2189 {
2190 ScreenCapture::captureScreen(&mCapture);
2191 // We've positioned the child in the top left.
2192 mCapture->expectChildColor(0, 0);
2193 // But it's only 10x10.
2194 mCapture->expectFGColor(10, 10);
2195 }
2196
Robert Carr4cdc58f2017-08-23 14:22:20 -07002197 asTransaction([&](Transaction& t) {
2198 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2199 // We cause scaling by 2.
2200 t.setSize(mFGSurfaceControl, 128, 128);
2201 });
Robert Carr9b429f42017-04-17 14:56:57 -07002202
2203 {
2204 ScreenCapture::captureScreen(&mCapture);
2205 // We've positioned the child in the top left.
2206 mCapture->expectChildColor(0, 0);
2207 mCapture->expectChildColor(10, 10);
2208 mCapture->expectChildColor(19, 19);
2209 // And now it should be scaled all the way to 20x20
2210 mCapture->expectFGColor(20, 20);
2211 }
2212}
2213
Robert Carr1725eee2017-04-26 18:32:15 -07002214// Regression test for b/37673612
2215TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002216 asTransaction([&](Transaction& t) {
2217 t.show(mChild);
2218 t.setPosition(mChild, 0, 0);
2219 t.setPosition(mFGSurfaceControl, 0, 0);
2220 });
Robert Carr1725eee2017-04-26 18:32:15 -07002221
2222 {
2223 ScreenCapture::captureScreen(&mCapture);
2224 // We've positioned the child in the top left.
2225 mCapture->expectChildColor(0, 0);
2226 // But it's only 10x10.
2227 mCapture->expectFGColor(10, 10);
2228 }
Robert Carr1725eee2017-04-26 18:32:15 -07002229 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2230 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002231 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002232 sp<Surface> s = mFGSurfaceControl->getSurface();
2233 auto anw = static_cast<ANativeWindow*>(s.get());
2234 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2235 native_window_set_buffers_dimensions(anw, 64, 128);
2236 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2237 waitForPostedBuffers();
2238
2239 {
2240 // The child should still be in the same place and not have any strange scaling as in
2241 // b/37673612.
2242 ScreenCapture::captureScreen(&mCapture);
2243 mCapture->expectChildColor(0, 0);
2244 mCapture->expectFGColor(10, 10);
2245 }
2246}
2247
Dan Stoza412903f2017-04-27 13:42:17 -07002248TEST_F(ChildLayerTest, Bug36858924) {
2249 // Destroy the child layer
2250 mChild.clear();
2251
2252 // Now recreate it as hidden
2253 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2254 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2255 mFGSurfaceControl.get());
2256
2257 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002258 asTransaction([&](Transaction& t) {
2259 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002260 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002261 t.show(mChild);
2262 });
Dan Stoza412903f2017-04-27 13:42:17 -07002263
2264 // Render the foreground surface a few times
2265 //
2266 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2267 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2268 // never acquire/release the first buffer
2269 ALOGI("Filling 1");
2270 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2271 ALOGI("Filling 2");
2272 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2273 ALOGI("Filling 3");
2274 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2275 ALOGI("Filling 4");
2276 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2277}
2278
chaviwf1961f72017-09-18 16:41:07 -07002279TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002280 asTransaction([&](Transaction& t) {
2281 t.show(mChild);
2282 t.setPosition(mChild, 10, 10);
2283 t.setPosition(mFGSurfaceControl, 64, 64);
2284 });
chaviw06178942017-07-27 10:25:59 -07002285
2286 {
2287 ScreenCapture::captureScreen(&mCapture);
2288 // Top left of foreground must now be visible
2289 mCapture->expectFGColor(64, 64);
2290 // But 10 pixels in we should see the child surface
2291 mCapture->expectChildColor(74, 74);
2292 // And 10 more pixels we should be back to the foreground surface
2293 mCapture->expectFGColor(84, 84);
2294 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002295
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002296 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002297
chaviw06178942017-07-27 10:25:59 -07002298 {
2299 ScreenCapture::captureScreen(&mCapture);
2300 mCapture->expectFGColor(64, 64);
2301 // In reparenting we should have exposed the entire foreground surface.
2302 mCapture->expectFGColor(74, 74);
2303 // And the child layer should now begin at 10, 10 (since the BG
2304 // layer is at (0, 0)).
2305 mCapture->expectBGColor(9, 9);
2306 mCapture->expectChildColor(10, 10);
2307 }
2308}
2309
chaviwf1961f72017-09-18 16:41:07 -07002310TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002311 asTransaction([&](Transaction& t) {
2312 t.show(mChild);
2313 t.setPosition(mChild, 10, 10);
2314 t.setPosition(mFGSurfaceControl, 64, 64);
2315 });
chaviwf1961f72017-09-18 16:41:07 -07002316
2317 {
2318 ScreenCapture::captureScreen(&mCapture);
2319 // Top left of foreground must now be visible
2320 mCapture->expectFGColor(64, 64);
2321 // But 10 pixels in we should see the child surface
2322 mCapture->expectChildColor(74, 74);
2323 // And 10 more pixels we should be back to the foreground surface
2324 mCapture->expectFGColor(84, 84);
2325 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002326 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002327 {
2328 ScreenCapture::captureScreen(&mCapture);
2329 // Nothing should have changed.
2330 mCapture->expectFGColor(64, 64);
2331 mCapture->expectChildColor(74, 74);
2332 mCapture->expectFGColor(84, 84);
2333 }
2334}
2335
2336TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002337 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2338 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07002339 ASSERT_TRUE(newSurface != NULL);
2340 ASSERT_TRUE(newSurface->isValid());
2341
2342 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002343 asTransaction([&](Transaction& t) {
2344 t.hide(mChild);
2345 t.show(newSurface);
2346 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002347 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002348 t.setPosition(mFGSurfaceControl, 64, 64);
2349 });
chaviwf1961f72017-09-18 16:41:07 -07002350
2351 {
2352 ScreenCapture::captureScreen(&mCapture);
2353 // Top left of foreground must now be visible
2354 mCapture->expectFGColor(64, 64);
2355 // At 10, 10 we should see the new surface
2356 mCapture->checkPixel(10, 10, 63, 195, 63);
2357 }
2358
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002359 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002360
2361 {
2362 ScreenCapture::captureScreen(&mCapture);
2363 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2364 // mFGSurface, putting it at 74, 74.
2365 mCapture->expectFGColor(64, 64);
2366 mCapture->checkPixel(74, 74, 63, 195, 63);
2367 mCapture->expectFGColor(84, 84);
2368 }
2369}
2370
chaviwc9674332017-08-28 12:32:18 -07002371TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002372 sp<SurfaceControl> grandchild =
2373 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2374 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002375 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2376
2377 {
2378 ScreenCapture::captureScreen(&mCapture);
2379 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2380 // which begins at 64, 64
2381 mCapture->checkPixel(64, 64, 50, 50, 50);
2382 }
2383}
2384
Robert Carr503c7042017-09-27 15:06:08 -07002385TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002386 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2387 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002388 fillSurfaceRGBA8(relative, 255, 255, 255);
2389
2390 Transaction t;
2391 t.setLayer(relative, INT32_MAX)
2392 .setRelativeLayer(mChild, relative->getHandle(), 1)
2393 .setPosition(mFGSurfaceControl, 0, 0)
2394 .apply(true);
2395
2396 // We expect that the child should have been elevated above our
2397 // INT_MAX layer even though it's not a child of it.
2398 {
2399 ScreenCapture::captureScreen(&mCapture);
2400 mCapture->expectChildColor(0, 0);
2401 mCapture->expectChildColor(9, 9);
2402 mCapture->checkPixel(10, 10, 255, 255, 255);
2403 }
2404}
2405
chaviw13fdc492017-06-27 12:40:18 -07002406class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002407protected:
chaviw13fdc492017-06-27 12:40:18 -07002408 void SetUp() override {
2409 LayerUpdateTest::SetUp();
2410
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002411 mLayerColorControl =
2412 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
2413 PIXEL_FORMAT_RGBA_8888,
2414 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07002415
2416 ASSERT_TRUE(mLayerColorControl != NULL);
2417 ASSERT_TRUE(mLayerColorControl->isValid());
2418
Robert Carr4cdc58f2017-08-23 14:22:20 -07002419 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002420 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002421 t.setPosition(mLayerColorControl, 140, 140);
2422 t.hide(mLayerColorControl);
2423 t.hide(mFGSurfaceControl);
2424 });
chaviw13fdc492017-06-27 12:40:18 -07002425 }
2426
2427 void TearDown() override {
2428 LayerUpdateTest::TearDown();
2429 mLayerColorControl = 0;
2430 }
2431
2432 sp<SurfaceControl> mLayerColorControl;
2433};
2434
2435TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2436 sp<ScreenCapture> sc;
2437
2438 {
2439 SCOPED_TRACE("before setColor");
2440 ScreenCapture::captureScreen(&sc);
2441 sc->expectBGColor(145, 145);
2442 }
2443
Robert Carr4cdc58f2017-08-23 14:22:20 -07002444 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002445 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002446 t.setColor(mLayerColorControl, color);
2447 t.show(mLayerColorControl);
2448 });
chaviw13fdc492017-06-27 12:40:18 -07002449
chaviw13fdc492017-06-27 12:40:18 -07002450 {
2451 // There should now be a color
2452 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002453
chaviw13fdc492017-06-27 12:40:18 -07002454 ScreenCapture::captureScreen(&sc);
2455 sc->checkPixel(145, 145, 43, 207, 131);
2456 }
2457}
2458
2459TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2460 sp<ScreenCapture> sc;
2461 {
2462 SCOPED_TRACE("before setColor");
2463 ScreenCapture::captureScreen(&sc);
2464 sc->expectBGColor(145, 145);
2465 }
2466
Robert Carr4cdc58f2017-08-23 14:22:20 -07002467 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002468 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002469 t.setColor(mLayerColorControl, color);
2470 t.setAlpha(mLayerColorControl, .75f);
2471 t.show(mLayerColorControl);
2472 });
2473
chaviw13fdc492017-06-27 12:40:18 -07002474 {
2475 // There should now be a color with .75 alpha
2476 SCOPED_TRACE("after setColor");
2477 ScreenCapture::captureScreen(&sc);
2478 sc->checkPixel(145, 145, 48, 171, 147);
2479 }
2480}
2481
2482TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2483 sp<ScreenCapture> sc;
2484 {
2485 SCOPED_TRACE("before setColor");
2486 ScreenCapture::captureScreen(&sc);
2487 sc->expectBGColor(145, 145);
2488 }
2489
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002490 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002491
chaviw13fdc492017-06-27 12:40:18 -07002492 {
2493 // There should now be set to 0,0,0 (black) as default.
2494 SCOPED_TRACE("after setColor");
2495 ScreenCapture::captureScreen(&sc);
2496 sc->checkPixel(145, 145, 0, 0, 0);
2497 }
2498}
2499
chaviwa76b2712017-09-20 12:02:26 -07002500class ScreenCaptureTest : public LayerUpdateTest {
2501protected:
2502 std::unique_ptr<CaptureLayer> mCapture;
2503};
2504
2505TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2506 auto bgHandle = mBGSurfaceControl->getHandle();
2507 CaptureLayer::captureScreen(&mCapture, bgHandle);
2508 mCapture->expectBGColor(0, 0);
2509 // Doesn't capture FG layer which is at 64, 64
2510 mCapture->expectBGColor(64, 64);
2511}
2512
2513TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2514 auto fgHandle = mFGSurfaceControl->getHandle();
2515
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002516 sp<SurfaceControl> child =
2517 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2518 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002519 fillSurfaceRGBA8(child, 200, 200, 200);
2520
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002521 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002522
2523 // Captures mFGSurfaceControl layer and its child.
2524 CaptureLayer::captureScreen(&mCapture, fgHandle);
2525 mCapture->expectFGColor(10, 10);
2526 mCapture->expectChildColor(0, 0);
2527}
2528
2529TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2530 auto fgHandle = mFGSurfaceControl->getHandle();
2531
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002532 sp<SurfaceControl> child =
2533 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2534 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002535 fillSurfaceRGBA8(child, 200, 200, 200);
2536
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002537 sp<SurfaceControl> grandchild =
2538 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2539 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002540
2541 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2542 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002543 .show(child)
2544 .setPosition(grandchild, 5, 5)
2545 .show(grandchild)
2546 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002547
2548 // Captures mFGSurfaceControl, its child, and the grandchild.
2549 CaptureLayer::captureScreen(&mCapture, fgHandle);
2550 mCapture->expectFGColor(10, 10);
2551 mCapture->expectChildColor(0, 0);
2552 mCapture->checkPixel(5, 5, 50, 50, 50);
2553}
2554
2555TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002556 sp<SurfaceControl> child =
2557 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2558 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002559 fillSurfaceRGBA8(child, 200, 200, 200);
2560 auto childHandle = child->getHandle();
2561
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002562 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002563
2564 // Captures only the child layer, and not the parent.
2565 CaptureLayer::captureScreen(&mCapture, childHandle);
2566 mCapture->expectChildColor(0, 0);
2567 mCapture->expectChildColor(9, 9);
2568}
2569
2570TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002571 sp<SurfaceControl> child =
2572 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2573 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002574 fillSurfaceRGBA8(child, 200, 200, 200);
2575 auto childHandle = child->getHandle();
2576
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002577 sp<SurfaceControl> grandchild =
2578 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2579 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002580 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2581
2582 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002583 .show(child)
2584 .setPosition(grandchild, 5, 5)
2585 .show(grandchild)
2586 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002587
2588 auto grandchildHandle = grandchild->getHandle();
2589
2590 // Captures only the grandchild.
2591 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2592 mCapture->checkPixel(0, 0, 50, 50, 50);
2593 mCapture->checkPixel(4, 4, 50, 50, 50);
2594}
2595
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002596} // namespace android