blob: 7033deafe5ad297795c8d62dee722eda91b822bb [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 Wu718daf82017-10-20 11:57:17 -070053 static const Color BLACK;
Chia-I Wu2113bdd2017-11-01 15:16:35 -070054 static const Color TRANSPARENT;
Chia-I Wu718daf82017-10-20 11:57:17 -070055};
56
57const Color Color::RED{255, 0, 0, 255};
Chia-I Wu0ea0f822017-10-31 10:14:40 -070058const Color Color::GREEN{0, 255, 0, 255};
Chia-I Wu49313302017-10-31 10:14:40 -070059const Color Color::BLUE{0, 0, 255, 255};
Chia-I Wu718daf82017-10-20 11:57:17 -070060const Color Color::BLACK{0, 0, 0, 255};
Chia-I Wu2113bdd2017-11-01 15:16:35 -070061const Color Color::TRANSPARENT{0, 0, 0, 0};
Chia-I Wu718daf82017-10-20 11:57:17 -070062
63std::ostream& operator<<(std::ostream& os, const Color& color) {
64 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
65 return os;
66}
67
68// Fill a region with the specified color.
69void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) {
70 int32_t x = rect.left;
71 int32_t y = rect.top;
72 int32_t width = rect.right - rect.left;
73 int32_t height = rect.bottom - rect.top;
74
75 if (x < 0) {
76 width += x;
77 x = 0;
78 }
79 if (y < 0) {
80 height += y;
81 y = 0;
82 }
83 if (x + width > buffer.width) {
84 x = std::min(x, buffer.width);
85 width = buffer.width - x;
86 }
87 if (y + height > buffer.height) {
88 y = std::min(y, buffer.height);
89 height = buffer.height - y;
90 }
91
92 for (int32_t j = 0; j < height; j++) {
93 uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4;
94 for (int32_t i = 0; i < width; i++) {
95 dst[0] = color.r;
96 dst[1] = color.g;
97 dst[2] = color.b;
98 dst[3] = color.a;
99 dst += 4;
100 }
101 }
102}
103
104// Check if a region has the specified color.
105void expectBufferColor(const CpuConsumer::LockedBuffer& buffer, const Rect& rect,
106 const Color& color, uint8_t tolerance) {
107 int32_t x = rect.left;
108 int32_t y = rect.top;
109 int32_t width = rect.right - rect.left;
110 int32_t height = rect.bottom - rect.top;
111
112 if (x + width > int32_t(buffer.width)) {
113 x = std::min(x, int32_t(buffer.width));
114 width = buffer.width - x;
115 }
116 if (y + height > int32_t(buffer.height)) {
117 y = std::min(y, int32_t(buffer.height));
118 height = buffer.height - y;
119 }
120
121 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
122 uint8_t tmp = a >= b ? a - b : b - a;
123 return tmp <= tolerance;
124 };
125 for (int32_t j = 0; j < height; j++) {
126 const uint8_t* src =
127 static_cast<const uint8_t*>(buffer.data) + (buffer.stride * (y + j) + x) * 4;
128 for (int32_t i = 0; i < width; i++) {
129 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
130 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
131 << "pixel @ (" << x + i << ", " << y + j << "): "
132 << "expected (" << color << "), "
133 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
134 src += 4;
135 }
136 }
137}
138
139} // anonymous namespace
140
Robert Carr4cdc58f2017-08-23 14:22:20 -0700141using Transaction = SurfaceComposerClient::Transaction;
142
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700143// Fill an RGBA_8888 formatted surface with a single color.
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700144static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
145 bool unlock = true) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800146 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700147 sp<Surface> s = sc->getSurface();
148 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800149 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
150 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700151 for (int y = 0; y < outBuffer.height; y++) {
152 for (int x = 0; x < outBuffer.width; x++) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700153 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700154 pixel[0] = r;
155 pixel[1] = g;
156 pixel[2] = b;
157 pixel[3] = 255;
158 }
159 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700160 if (unlock) {
161 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
162 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700163}
164
165// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
166// individual pixel values for testing purposes.
167class ScreenCapture : public RefBase {
168public:
Chia-I Wu718daf82017-10-20 11:57:17 -0700169 static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
170 int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700171 sp<IGraphicBufferProducer> producer;
172 sp<IGraphicBufferConsumer> consumer;
173 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -0700174 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700175 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700176 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Robert Carr4cdc58f2017-08-23 14:22:20 -0700177 SurfaceComposerClient::Transaction().apply(true);
178
Chia-I Wu718daf82017-10-20 11:57:17 -0700179 ASSERT_EQ(NO_ERROR,
180 sf->captureScreen(display, producer, Rect(), 0, 0, minLayerZ, maxLayerZ, false));
Michael Lentine5a16a622015-05-21 13:48:24 -0700181 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700182 }
183
Chia-I Wu718daf82017-10-20 11:57:17 -0700184 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
185 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
186 expectBufferColor(mBuf, rect, color, tolerance);
187 }
188
189 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
190 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
191 const bool leftBorder = rect.left > 0;
192 const bool topBorder = rect.top > 0;
193 const bool rightBorder = rect.right < int32_t(mBuf.width);
194 const bool bottomBorder = rect.bottom < int32_t(mBuf.height);
195
196 if (topBorder) {
197 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
198 if (leftBorder) {
199 top.left -= 1;
200 }
201 if (rightBorder) {
202 top.right += 1;
203 }
204 expectColor(top, color, tolerance);
205 }
206 if (leftBorder) {
207 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
208 expectColor(left, color, tolerance);
209 }
210 if (rightBorder) {
211 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
212 expectColor(right, color, tolerance);
213 }
214 if (bottomBorder) {
215 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
216 if (leftBorder) {
217 bottom.left -= 1;
218 }
219 if (rightBorder) {
220 bottom.right += 1;
221 }
222 expectColor(bottom, color, tolerance);
223 }
224 }
225
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700226 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700227 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
228 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
229 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700230 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
231 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700232 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
233 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700234 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700235 }
236 }
237
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700238 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700239
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700240 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700241
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700242 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700243
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700244private:
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700245 ScreenCapture(const sp<CpuConsumer>& cc) : mCC(cc) {
Michael Lentine5a16a622015-05-21 13:48:24 -0700246 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
247 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700248
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700249 ~ScreenCapture() { mCC->unlockBuffer(mBuf); }
Michael Lentine5a16a622015-05-21 13:48:24 -0700250
251 sp<CpuConsumer> mCC;
252 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700253};
254
chaviwa76b2712017-09-20 12:02:26 -0700255class CaptureLayer {
256public:
257 static void captureScreen(std::unique_ptr<CaptureLayer>* sc, sp<IBinder>& parentHandle) {
258 sp<IGraphicBufferProducer> producer;
259 sp<IGraphicBufferConsumer> consumer;
260 BufferQueue::createBufferQueue(&producer, &consumer);
261 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
262 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700263 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
chaviwa76b2712017-09-20 12:02:26 -0700264 SurfaceComposerClient::Transaction().apply(true);
265 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, producer));
266 *sc = std::make_unique<CaptureLayer>(cpuConsumer);
267 }
268
269 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
270 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuffer.format);
271 const uint8_t* img = static_cast<const uint8_t*>(mBuffer.data);
272 const uint8_t* pixel = img + (4 * (y * mBuffer.stride + x));
273 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
274 String8 err(String8::format("pixel @ (%3d, %3d): "
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700275 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
chaviwa76b2712017-09-20 12:02:26 -0700276 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
277 EXPECT_EQ(String8(), err) << err.string();
278 }
279 }
280
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700281 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
chaviwa76b2712017-09-20 12:02:26 -0700282
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700283 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
chaviwa76b2712017-09-20 12:02:26 -0700284
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700285 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
chaviwa76b2712017-09-20 12:02:26 -0700286
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700287 CaptureLayer(const sp<CpuConsumer>& cc) : mCC(cc) {
chaviwa76b2712017-09-20 12:02:26 -0700288 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuffer));
289 }
290
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700291 ~CaptureLayer() { mCC->unlockBuffer(mBuffer); }
chaviwa76b2712017-09-20 12:02:26 -0700292
293private:
294 sp<CpuConsumer> mCC;
295 CpuConsumer::LockedBuffer mBuffer;
296};
297
Chia-I Wu718daf82017-10-20 11:57:17 -0700298class LayerTransactionTest : public ::testing::Test {
299protected:
300 void SetUp() override {
301 mClient = new SurfaceComposerClient;
302 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
303
304 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
305 }
306
307 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
308 uint32_t flags = 0) {
309 auto layer =
310 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
311 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
312
313 status_t error = Transaction()
314 .setLayerStack(layer, mDisplayLayerStack)
315 .setLayer(layer, mLayerZBase)
316 .apply();
317 if (error != NO_ERROR) {
318 ADD_FAILURE() << "failed to initialize SurfaceControl";
319 layer.clear();
320 }
321
322 return layer;
323 }
324
325 ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
326 // wait for previous transactions (such as setSize) to complete
327 Transaction().apply(true);
328
329 ANativeWindow_Buffer buffer = {};
330 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
331
332 return buffer;
333 }
334
335 void postLayerBuffer(const sp<SurfaceControl>& layer) {
336 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
337
338 // wait for the newly posted buffer to be latched
339 waitForLayerBuffers();
340 }
341
342 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
343 ANativeWindow_Buffer buffer;
344 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
345 fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
346 postLayerBuffer(layer);
347 }
348
349 sp<ScreenCapture> screenshot() {
350 sp<ScreenCapture> screenshot;
351 ScreenCapture::captureScreen(&screenshot, mLayerZBase);
352 return screenshot;
353 }
354
355 sp<SurfaceComposerClient> mClient;
356
357 sp<IBinder> mDisplay;
358 uint32_t mDisplayWidth;
359 uint32_t mDisplayHeight;
360 uint32_t mDisplayLayerStack;
361
362 // leave room for ~256 layers
363 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
364
365private:
366 void SetUpDisplay() {
367 mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
368 ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
369
370 // get display width/height
371 DisplayInfo info;
372 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
373 mDisplayWidth = info.w;
374 mDisplayHeight = info.h;
375
376 // After a new buffer is queued, SurfaceFlinger is notified and will
377 // latch the new buffer on next vsync. Let's heuristically wait for 3
378 // vsyncs.
379 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
380
381 mDisplayLayerStack = 0;
382 // set layer stack (b/68888219)
383 Transaction t;
384 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
385 t.apply();
386 }
387
388 void waitForLayerBuffers() { usleep(mBufferPostDelay); }
389
390 int32_t mBufferPostDelay;
391};
392
393TEST_F(LayerTransactionTest, SetPositionBasic) {
394 sp<SurfaceControl> layer;
395 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
396 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
397
398 {
399 SCOPED_TRACE("default position");
400 auto shot = screenshot();
401 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
402 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
403 }
404
405 Transaction().setPosition(layer, 5, 10).apply();
406 {
407 SCOPED_TRACE("new position");
408 auto shot = screenshot();
409 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
410 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
411 }
412}
413
414TEST_F(LayerTransactionTest, SetPositionRounding) {
415 sp<SurfaceControl> layer;
416 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
417 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
418
419 // GLES requires only 4 bits of subpixel precision during rasterization
420 // XXX GLES composition does not match HWC composition due to precision
421 // loss (b/69315223)
422 const float epsilon = 1.0f / 16.0f;
423 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
424 {
425 SCOPED_TRACE("rounding down");
426 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
427 }
428
429 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
430 {
431 SCOPED_TRACE("rounding up");
432 screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
433 }
434}
435
436TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
437 sp<SurfaceControl> layer;
438 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
439 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
440
441 Transaction().setPosition(layer, -32, -32).apply();
442 {
443 SCOPED_TRACE("negative coordinates");
444 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
445 }
446
447 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
448 {
449 SCOPED_TRACE("positive coordinates");
450 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
451 }
452}
453
454TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
455 sp<SurfaceControl> layer;
456 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
457 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
458
459 // partially out of bounds
460 Transaction().setPosition(layer, -30, -30).apply();
461 {
462 SCOPED_TRACE("negative coordinates");
463 screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
464 }
465
466 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
467 {
468 SCOPED_TRACE("positive coordinates");
469 screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
470 mDisplayHeight),
471 Color::RED);
472 }
473}
474
475TEST_F(LayerTransactionTest, SetPositionWithResize) {
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 // setPosition is applied immediately by default, with or without resize
481 // pending
482 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
483 {
484 SCOPED_TRACE("resize pending");
485 auto shot = screenshot();
486 shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
487 shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
488 }
489
490 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
491 {
492 SCOPED_TRACE("resize applied");
493 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
494 }
495}
496
497TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
498 sp<SurfaceControl> layer;
499 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
500 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
501
502 // request setPosition to be applied with the next resize
503 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
504 {
505 SCOPED_TRACE("new position pending");
506 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
507 }
508
509 Transaction().setPosition(layer, 15, 20).apply();
510 {
511 SCOPED_TRACE("pending new position modified");
512 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
513 }
514
515 Transaction().setSize(layer, 64, 64).apply();
516 {
517 SCOPED_TRACE("resize pending");
518 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
519 }
520
521 // finally resize and latch the buffer
522 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
523 {
524 SCOPED_TRACE("new position applied");
525 screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
526 }
527}
528
529TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
530 sp<SurfaceControl> layer;
531 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
532 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
533
534 // setPosition is not immediate even with SCALE_TO_WINDOW override
535 Transaction()
536 .setPosition(layer, 5, 10)
537 .setSize(layer, 64, 64)
538 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
539 .setGeometryAppliesWithResize(layer)
540 .apply();
541 {
542 SCOPED_TRACE("new position pending");
543 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
544 }
545
546 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
547 {
548 SCOPED_TRACE("new position applied");
549 screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
550 }
551}
552
Chia-I Wu0eaea312017-10-31 10:14:40 -0700553TEST_F(LayerTransactionTest, SetSizeBasic) {
554 sp<SurfaceControl> layer;
555 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
556 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
557
558 Transaction().setSize(layer, 64, 64).apply();
559 {
560 SCOPED_TRACE("resize pending");
561 auto shot = screenshot();
562 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
563 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
564 }
565
566 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
567 {
568 SCOPED_TRACE("resize applied");
569 auto shot = screenshot();
570 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
571 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
572 }
573}
574
575TEST_F(LayerTransactionTest, SetSizeInvalid) {
576 // cannot test robustness against invalid sizes (zero or really huge)
577}
578
579TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
580 sp<SurfaceControl> layer;
581 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
582 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
583
584 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
585 Transaction()
586 .setSize(layer, 64, 64)
587 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
588 .apply();
589 screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
590}
591
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700592TEST_F(LayerTransactionTest, SetZBasic) {
593 sp<SurfaceControl> layerR;
594 sp<SurfaceControl> layerG;
595 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
596 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
597 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
598 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
599
600 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
601 {
602 SCOPED_TRACE("layerR");
603 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
604 }
605
606 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
607 {
608 SCOPED_TRACE("layerG");
609 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
610 }
611}
612
613TEST_F(LayerTransactionTest, SetZNegative) {
614 sp<SurfaceControl> layerR;
615 sp<SurfaceControl> layerG;
616 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
617 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
618 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
619 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
620
621 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
622 {
623 SCOPED_TRACE("layerR");
624 sp<ScreenCapture> screenshot;
625 ScreenCapture::captureScreen(&screenshot, -2, -1);
626 screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
627 }
628
629 Transaction().setLayer(layerR, -3).apply();
630 {
631 SCOPED_TRACE("layerG");
632 sp<ScreenCapture> screenshot;
633 ScreenCapture::captureScreen(&screenshot, -3, -1);
634 screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
635 }
636}
637
Chia-I Wu49313302017-10-31 10:14:40 -0700638TEST_F(LayerTransactionTest, SetRelativeZBasic) {
639 sp<SurfaceControl> layerR;
640 sp<SurfaceControl> layerG;
641 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
642 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
643 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
644 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
645
646 Transaction()
647 .setPosition(layerG, 16, 16)
648 .setRelativeLayer(layerG, layerR->getHandle(), 1)
649 .apply();
650 {
651 SCOPED_TRACE("layerG above");
652 auto shot = screenshot();
653 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
654 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
655 }
656
657 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
658 {
659 SCOPED_TRACE("layerG below");
660 auto shot = screenshot();
661 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
662 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
663 }
664}
665
666TEST_F(LayerTransactionTest, SetRelativeZGroup) {
667 sp<SurfaceControl> layerR;
668 sp<SurfaceControl> layerG;
669 sp<SurfaceControl> layerB;
670 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
671 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
672 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
673 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
674 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
675 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
676
677 // layerR = 0, layerG = layerR + 3, layerB = 2
678 Transaction()
679 .setPosition(layerG, 8, 8)
680 .setRelativeLayer(layerG, layerR->getHandle(), 3)
681 .setPosition(layerB, 16, 16)
682 .setLayer(layerB, mLayerZBase + 2)
683 .apply();
684 {
685 SCOPED_TRACE("(layerR < layerG) < layerB");
686 auto shot = screenshot();
687 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
688 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
689 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
690 }
691
692 // layerR = 4, layerG = layerR + 3, layerB = 2
693 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
694 {
695 SCOPED_TRACE("layerB < (layerR < layerG)");
696 auto shot = screenshot();
697 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
698 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
699 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
700 }
701
702 // layerR = 4, layerG = layerR - 3, layerB = 2
703 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
704 {
705 SCOPED_TRACE("layerB < (layerG < layerR)");
706 auto shot = screenshot();
707 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
708 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
709 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
710 }
711
712 // restore to absolute z
713 // layerR = 4, layerG = 0, layerB = 2
714 Transaction().setLayer(layerG, mLayerZBase).apply();
715 {
716 SCOPED_TRACE("layerG < layerB < layerR");
717 auto shot = screenshot();
718 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
719 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
720 }
721
722 // layerR should not affect layerG anymore
723 // layerR = 1, layerG = 0, layerB = 2
724 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
725 {
726 SCOPED_TRACE("layerG < layerR < layerB");
727 auto shot = screenshot();
728 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
729 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
730 }
731}
732
733TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
734 sp<SurfaceControl> layerR;
735 sp<SurfaceControl> layerG;
736
737 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
738 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
739 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
740 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
741
742 Transaction()
743 .setPosition(layerG, 16, 16)
744 .setRelativeLayer(layerG, layerR->getHandle(), 1)
745 .apply();
746
747 mClient->destroySurface(layerG->getHandle());
748 // layerG should have been removed
749 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
750}
751
Chia-I Wu57b27502017-10-31 10:14:40 -0700752TEST_F(LayerTransactionTest, SetFlagsHidden) {
753 sp<SurfaceControl> layer;
754 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
755 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
756
757 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
758 {
759 SCOPED_TRACE("layer hidden");
760 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
761 }
762
763 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
764 {
765 SCOPED_TRACE("layer shown");
766 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
767 }
768}
769
770TEST_F(LayerTransactionTest, SetFlagsOpaque) {
771 const Color translucentRed = {100, 0, 0, 100};
772 sp<SurfaceControl> layerR;
773 sp<SurfaceControl> layerG;
774 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
775 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
776 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
777 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
778
779 Transaction()
780 .setLayer(layerR, mLayerZBase + 1)
781 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
782 .apply();
783 {
784 SCOPED_TRACE("layerR opaque");
785 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
786 }
787
788 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
789 {
790 SCOPED_TRACE("layerR translucent");
791 const uint8_t g = uint8_t(255 - translucentRed.a);
792 screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
793 }
794}
795
796TEST_F(LayerTransactionTest, SetFlagsSecure) {
797 sp<SurfaceControl> layer;
798 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
799 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
800
801 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
802 sp<IGraphicBufferProducer> producer;
803 sp<IGraphicBufferConsumer> consumer;
804 BufferQueue::createBufferQueue(&producer, &consumer);
805 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
806
807 Transaction()
808 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
809 .apply(true);
810 ASSERT_EQ(PERMISSION_DENIED,
811 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
812 false));
813
814 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
815 ASSERT_EQ(NO_ERROR,
816 composer->captureScreen(mDisplay, producer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
817 false));
818}
819
Chia-I Wu2113bdd2017-11-01 15:16:35 -0700820TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
821 const Rect top(0, 0, 32, 16);
822 const Rect bottom(0, 16, 32, 32);
823 sp<SurfaceControl> layer;
824 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
825
826 ANativeWindow_Buffer buffer;
827 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
828 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
829 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
830 // setTransparentRegionHint always applies to the following buffer
831 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
832 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
833 {
834 SCOPED_TRACE("top transparent");
835 auto shot = screenshot();
836 shot->expectColor(top, Color::BLACK);
837 shot->expectColor(bottom, Color::RED);
838 }
839
840 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
841 {
842 SCOPED_TRACE("transparent region hint pending");
843 auto shot = screenshot();
844 shot->expectColor(top, Color::BLACK);
845 shot->expectColor(bottom, Color::RED);
846 }
847
848 ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
849 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
850 ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
851 ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
852 {
853 SCOPED_TRACE("bottom transparent");
854 auto shot = screenshot();
855 shot->expectColor(top, Color::RED);
856 shot->expectColor(bottom, Color::BLACK);
857 }
858}
859
860TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
861 sp<SurfaceControl> layerTransparent;
862 sp<SurfaceControl> layerR;
863 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
864 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
865
866 // check that transparent region hint is bound by the layer size
867 Transaction()
868 .setTransparentRegionHint(layerTransparent,
869 Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
870 .setPosition(layerR, 16, 16)
871 .setLayer(layerR, mLayerZBase + 1)
872 .apply();
873 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
874 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
875 screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
876}
877
Chia-I Wua8a515e2017-11-01 15:16:35 -0700878TEST_F(LayerTransactionTest, SetAlphaBasic) {
879 sp<SurfaceControl> layer1;
880 sp<SurfaceControl> layer2;
881 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
882 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
883 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
884 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
885
886 Transaction()
887 .setAlpha(layer1, 0.25f)
888 .setAlpha(layer2, 0.75f)
889 .setPosition(layer2, 16, 0)
890 .setLayer(layer2, mLayerZBase + 1)
891 .apply();
892 {
893 auto shot = screenshot();
894 uint8_t r = 16; // 64 * 0.25f
895 uint8_t g = 48; // 64 * 0.75f
896 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
897 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
898
899 r /= 4; // r * (1.0f - 0.75f)
900 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
901 }
902}
903
904TEST_F(LayerTransactionTest, SetAlphaClamped) {
905 const Color color = {64, 0, 0, 255};
906 sp<SurfaceControl> layer;
907 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
908 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
909
910 Transaction().setAlpha(layer, 2.0f).apply();
911 {
912 SCOPED_TRACE("clamped to 1.0f");
913 screenshot()->expectColor(Rect(0, 0, 32, 32), color);
914 }
915
916 Transaction().setAlpha(layer, -1.0f).apply();
917 {
918 SCOPED_TRACE("clamped to 0.0f");
919 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
920 }
921}
922
Chia-I Wue4ef6102017-11-01 15:16:35 -0700923TEST_F(LayerTransactionTest, SetColorBasic) {
924 sp<SurfaceControl> bufferLayer;
925 sp<SurfaceControl> colorLayer;
926 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
927 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
928 ASSERT_NO_FATAL_FAILURE(
929 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
930
931 Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
932 {
933 SCOPED_TRACE("default color");
934 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
935 }
936
937 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
938 const Color expected = {15, 51, 85, 255};
939 // this is handwavy, but the precison loss scaled by 255 (8-bit per
940 // channel) should be less than one
941 const uint8_t tolerance = 1;
942 Transaction().setColor(colorLayer, color).apply();
943 {
944 SCOPED_TRACE("new color");
945 screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
946 }
947}
948
949TEST_F(LayerTransactionTest, SetColorClamped) {
950 sp<SurfaceControl> colorLayer;
951 ASSERT_NO_FATAL_FAILURE(
952 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
953
954 Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
955 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
956}
957
958TEST_F(LayerTransactionTest, SetColorWithAlpha) {
959 sp<SurfaceControl> bufferLayer;
960 sp<SurfaceControl> colorLayer;
961 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
962 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
963 ASSERT_NO_FATAL_FAILURE(
964 colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
965
966 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
967 const float alpha = 0.25f;
968 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
969 // this is handwavy, but the precison loss scaled by 255 (8-bit per
970 // channel) should be less than one
971 const uint8_t tolerance = 1;
972 Transaction()
973 .setColor(colorLayer, color)
974 .setAlpha(colorLayer, alpha)
975 .setLayer(colorLayer, mLayerZBase + 1)
976 .apply();
977 screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
978 tolerance);
979}
980
981TEST_F(LayerTransactionTest, SetColorWithBuffer) {
982 sp<SurfaceControl> bufferLayer;
983 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
984 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
985
986 // color is ignored
987 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
988 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
989}
990
Chia-I Wu3d22f3a2017-11-02 08:30:27 -0700991TEST_F(LayerTransactionTest, SetLayerStackBasic) {
992 sp<SurfaceControl> layer;
993 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
994 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
995
996 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
997 {
998 SCOPED_TRACE("non-existing layer stack");
999 screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1000 }
1001
1002 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1003 {
1004 SCOPED_TRACE("original layer stack");
1005 screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1006 }
1007}
1008
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001009class LayerUpdateTest : public ::testing::Test {
1010protected:
1011 virtual void SetUp() {
1012 mComposerClient = new SurfaceComposerClient;
1013 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1014
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001015 sp<IBinder> display(
1016 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -07001017 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001018 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001019
1020 ssize_t displayWidth = info.w;
1021 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001022
1023 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001024 mBGSurfaceControl =
1025 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1026 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001027 ASSERT_TRUE(mBGSurfaceControl != NULL);
1028 ASSERT_TRUE(mBGSurfaceControl->isValid());
1029 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1030
1031 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001032 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1033 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001034 ASSERT_TRUE(mFGSurfaceControl != NULL);
1035 ASSERT_TRUE(mFGSurfaceControl->isValid());
1036
1037 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1038
1039 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001040 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1041 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001042 ASSERT_TRUE(mSyncSurfaceControl != NULL);
1043 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1044
1045 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1046
Robert Carr4cdc58f2017-08-23 14:22:20 -07001047 asTransaction([&](Transaction& t) {
1048 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001049
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001050 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001051
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001052 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1053 .setPosition(mFGSurfaceControl, 64, 64)
1054 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001055
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001056 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1057 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1058 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001059 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001060 }
1061
1062 virtual void TearDown() {
1063 mComposerClient->dispose();
1064 mBGSurfaceControl = 0;
1065 mFGSurfaceControl = 0;
1066 mSyncSurfaceControl = 0;
1067 mComposerClient = 0;
1068 }
1069
1070 void waitForPostedBuffers() {
1071 // Since the sync surface is in synchronous mode (i.e. double buffered)
1072 // posting three buffers to it should ensure that at least two
1073 // SurfaceFlinger::handlePageFlip calls have been made, which should
1074 // guaranteed that a buffer posted to another Surface has been retired.
1075 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1076 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1077 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1078 }
1079
Robert Carr4cdc58f2017-08-23 14:22:20 -07001080 void asTransaction(const std::function<void(Transaction&)>& exec) {
1081 Transaction t;
1082 exec(t);
1083 t.apply(true);
1084 }
1085
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001086 sp<SurfaceComposerClient> mComposerClient;
1087 sp<SurfaceControl> mBGSurfaceControl;
1088 sp<SurfaceControl> mFGSurfaceControl;
1089
1090 // This surface is used to ensure that the buffers posted to
1091 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1092 sp<SurfaceControl> mSyncSurfaceControl;
1093};
1094
Robert Carr7f619b22017-11-06 12:56:35 -08001095TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1096 sp<ScreenCapture> sc;
1097
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001098 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1099 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001100 fillSurfaceRGBA8(relative, 10, 10, 10);
1101 waitForPostedBuffers();
1102
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001103 Transaction{}
1104 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001105 .setPosition(relative, 64, 64)
1106 .apply();
1107
1108 {
1109 // The relative should be on top of the FG control.
1110 ScreenCapture::captureScreen(&sc);
1111 sc->checkPixel(64, 64, 10, 10, 10);
1112 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001113 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001114
1115 {
1116 // Nothing should change at this point.
1117 ScreenCapture::captureScreen(&sc);
1118 sc->checkPixel(64, 64, 10, 10, 10);
1119 }
1120
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001121 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001122
1123 {
1124 // Ensure that the relative was actually hidden, rather than
1125 // being left in the detached but visible state.
1126 ScreenCapture::captureScreen(&sc);
1127 sc->expectFGColor(64, 64);
1128 }
1129}
1130
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001131TEST_F(LayerUpdateTest, LayerMoveWorks) {
1132 sp<ScreenCapture> sc;
1133 {
1134 SCOPED_TRACE("before move");
1135 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001136 sc->expectBGColor(0, 12);
1137 sc->expectFGColor(75, 75);
1138 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001139 }
1140
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001141 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001142
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001143 {
1144 // This should reflect the new position, but not the new color.
1145 SCOPED_TRACE("after move, before redraw");
1146 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001147 sc->expectBGColor(24, 24);
1148 sc->expectBGColor(75, 75);
1149 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001150 }
1151
1152 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1153 waitForPostedBuffers();
1154 {
1155 // This should reflect the new position and the new color.
1156 SCOPED_TRACE("after redraw");
1157 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001158 sc->expectBGColor(24, 24);
1159 sc->expectBGColor(75, 75);
1160 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001161 }
1162}
1163
1164TEST_F(LayerUpdateTest, LayerResizeWorks) {
1165 sp<ScreenCapture> sc;
1166 {
1167 SCOPED_TRACE("before resize");
1168 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001169 sc->expectBGColor(0, 12);
1170 sc->expectFGColor(75, 75);
1171 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001172 }
1173
Steve Block9d453682011-12-20 16:23:08 +00001174 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001175 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +00001176 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001177 {
1178 // This should not reflect the new size or color because SurfaceFlinger
1179 // has not yet received a buffer of the correct size.
1180 SCOPED_TRACE("after resize, before redraw");
1181 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001182 sc->expectBGColor(0, 12);
1183 sc->expectFGColor(75, 75);
1184 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001185 }
1186
Steve Block9d453682011-12-20 16:23:08 +00001187 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001188 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1189 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +00001190 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001191 {
1192 // This should reflect the new size and the new color.
1193 SCOPED_TRACE("after redraw");
1194 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001195 sc->expectBGColor(24, 24);
1196 sc->checkPixel(75, 75, 63, 195, 63);
1197 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001198 }
1199}
1200
Haixia Shid5750962015-07-27 16:50:49 -07001201TEST_F(LayerUpdateTest, LayerCropWorks) {
1202 sp<ScreenCapture> sc;
1203 {
1204 SCOPED_TRACE("before crop");
1205 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001206 sc->expectBGColor(24, 24);
1207 sc->expectFGColor(75, 75);
1208 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001209 }
1210
Robert Carr4cdc58f2017-08-23 14:22:20 -07001211 asTransaction([&](Transaction& t) {
1212 Rect cropRect(16, 16, 32, 32);
1213 t.setCrop(mFGSurfaceControl, cropRect);
1214 });
Haixia Shid5750962015-07-27 16:50:49 -07001215 {
1216 // This should crop the foreground surface.
1217 SCOPED_TRACE("after crop");
1218 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001219 sc->expectBGColor(24, 24);
1220 sc->expectBGColor(75, 75);
1221 sc->expectFGColor(95, 80);
1222 sc->expectFGColor(80, 95);
1223 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -07001224 }
1225}
1226
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001227TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
1228 sp<ScreenCapture> sc;
1229 {
1230 SCOPED_TRACE("before crop");
1231 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001232 sc->expectBGColor(24, 24);
1233 sc->expectFGColor(75, 75);
1234 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001235 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001236 asTransaction([&](Transaction& t) {
1237 Rect cropRect(16, 16, 32, 32);
1238 t.setFinalCrop(mFGSurfaceControl, cropRect);
1239 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001240 {
1241 // This should crop the foreground surface.
1242 SCOPED_TRACE("after crop");
1243 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001244 sc->expectBGColor(24, 24);
1245 sc->expectBGColor(75, 75);
1246 sc->expectBGColor(95, 80);
1247 sc->expectBGColor(80, 95);
1248 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001249 }
1250}
1251
Haixia Shid5750962015-07-27 16:50:49 -07001252TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
1253 sp<ScreenCapture> sc;
1254 {
1255 SCOPED_TRACE("before setLayer");
1256 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001257 sc->expectBGColor(24, 24);
1258 sc->expectFGColor(75, 75);
1259 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001260 }
1261
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001262 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001263
Haixia Shid5750962015-07-27 16:50:49 -07001264 {
1265 // This should hide the foreground surface beneath the background.
1266 SCOPED_TRACE("after setLayer");
1267 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001268 sc->expectBGColor(24, 24);
1269 sc->expectBGColor(75, 75);
1270 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001271 }
1272}
1273
1274TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1275 sp<ScreenCapture> sc;
1276 {
1277 SCOPED_TRACE("before hide");
1278 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001279 sc->expectBGColor(24, 24);
1280 sc->expectFGColor(75, 75);
1281 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001282 }
1283
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001284 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001285
Haixia Shid5750962015-07-27 16:50:49 -07001286 {
1287 // This should hide the foreground surface.
1288 SCOPED_TRACE("after hide, before show");
1289 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001290 sc->expectBGColor(24, 24);
1291 sc->expectBGColor(75, 75);
1292 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001293 }
1294
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001295 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001296
Haixia Shid5750962015-07-27 16:50:49 -07001297 {
1298 // This should show the foreground surface.
1299 SCOPED_TRACE("after show");
1300 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001301 sc->expectBGColor(24, 24);
1302 sc->expectFGColor(75, 75);
1303 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001304 }
1305}
1306
1307TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1308 sp<ScreenCapture> sc;
1309 {
1310 SCOPED_TRACE("before setAlpha");
1311 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001312 sc->expectBGColor(24, 24);
1313 sc->expectFGColor(75, 75);
1314 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001315 }
1316
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001317 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001318
Haixia Shid5750962015-07-27 16:50:49 -07001319 {
1320 // This should set foreground to be 75% opaque.
1321 SCOPED_TRACE("after setAlpha");
1322 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001323 sc->expectBGColor(24, 24);
1324 sc->checkPixel(75, 75, 162, 63, 96);
1325 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001326 }
1327}
1328
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001329TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1330 sp<ScreenCapture> sc;
1331 {
1332 SCOPED_TRACE("before setLayerStack");
1333 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001334 sc->expectBGColor(24, 24);
1335 sc->expectFGColor(75, 75);
1336 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001337 }
1338
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001339 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001340 {
1341 // This should hide the foreground surface since it goes to a different
1342 // layer stack.
1343 SCOPED_TRACE("after setLayerStack");
1344 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001345 sc->expectBGColor(24, 24);
1346 sc->expectBGColor(75, 75);
1347 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001348 }
1349}
1350
1351TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1352 sp<ScreenCapture> sc;
1353 {
1354 SCOPED_TRACE("before setFlags");
1355 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001356 sc->expectBGColor(24, 24);
1357 sc->expectFGColor(75, 75);
1358 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001359 }
1360
Robert Carr4cdc58f2017-08-23 14:22:20 -07001361 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001362 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001363 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001364 {
1365 // This should hide the foreground surface
1366 SCOPED_TRACE("after setFlags");
1367 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001368 sc->expectBGColor(24, 24);
1369 sc->expectBGColor(75, 75);
1370 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001371 }
1372}
1373
1374TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1375 sp<ScreenCapture> sc;
1376 {
1377 SCOPED_TRACE("before setMatrix");
1378 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001379 sc->expectBGColor(24, 24);
1380 sc->expectFGColor(91, 96);
1381 sc->expectFGColor(96, 101);
1382 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001383 }
1384
Robert Carr4cdc58f2017-08-23 14:22:20 -07001385 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001386 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001387 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001388 {
1389 SCOPED_TRACE("after setMatrix");
1390 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001391 sc->expectBGColor(24, 24);
1392 sc->expectFGColor(91, 96);
1393 sc->expectBGColor(96, 91);
1394 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001395 }
1396}
1397
Robert Carr8d5227b2017-03-16 15:41:03 -07001398class GeometryLatchingTest : public LayerUpdateTest {
1399protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001400 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001401 SCOPED_TRACE(trace);
1402 ScreenCapture::captureScreen(&sc);
1403 // We find the leading edge of the FG surface.
1404 sc->expectFGColor(127, 127);
1405 sc->expectBGColor(128, 128);
1406 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001407
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001408 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001409
1410 void unlockFGBuffer() {
1411 sp<Surface> s = mFGSurfaceControl->getSurface();
1412 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1413 waitForPostedBuffers();
1414 }
1415
Robert Carr8d5227b2017-03-16 15:41:03 -07001416 void completeFGResize() {
1417 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1418 waitForPostedBuffers();
1419 }
1420 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001421 asTransaction([&](Transaction& t) {
1422 t.setSize(mFGSurfaceControl, 64, 64);
1423 t.setPosition(mFGSurfaceControl, 64, 64);
1424 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1425 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1426 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001427
1428 EXPECT_INITIAL_STATE("After restoring initial state");
1429 }
1430 sp<ScreenCapture> sc;
1431};
1432
1433TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1434 EXPECT_INITIAL_STATE("before anything");
1435
1436 // By default position can be updated even while
1437 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001438 asTransaction([&](Transaction& t) {
1439 t.setSize(mFGSurfaceControl, 32, 32);
1440 t.setPosition(mFGSurfaceControl, 100, 100);
1441 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001442
1443 {
1444 SCOPED_TRACE("After moving surface");
1445 ScreenCapture::captureScreen(&sc);
1446 // If we moved, the FG Surface should cover up what was previously BG
1447 // however if we didn't move the FG wouldn't be large enough now.
1448 sc->expectFGColor(163, 163);
1449 }
1450
1451 restoreInitialState();
1452
1453 // Now we repeat with setGeometryAppliesWithResize
1454 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001455 asTransaction([&](Transaction& t) {
1456 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1457 t.setSize(mFGSurfaceControl, 32, 32);
1458 t.setPosition(mFGSurfaceControl, 100, 100);
1459 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001460
1461 {
1462 SCOPED_TRACE("While resize is pending");
1463 ScreenCapture::captureScreen(&sc);
1464 // This time we shouldn't have moved, so the BG color
1465 // should still be visible.
1466 sc->expectBGColor(128, 128);
1467 }
1468
1469 completeFGResize();
1470
1471 {
1472 SCOPED_TRACE("After the resize");
1473 ScreenCapture::captureScreen(&sc);
1474 // But after the resize completes, we should move
1475 // and the FG should be visible here.
1476 sc->expectFGColor(128, 128);
1477 }
1478}
1479
1480class CropLatchingTest : public GeometryLatchingTest {
1481protected:
1482 void EXPECT_CROPPED_STATE(const char* trace) {
1483 SCOPED_TRACE(trace);
1484 ScreenCapture::captureScreen(&sc);
1485 // The edge should be moved back one pixel by our crop.
1486 sc->expectFGColor(126, 126);
1487 sc->expectBGColor(127, 127);
1488 sc->expectBGColor(128, 128);
1489 }
chaviw59f5c562017-06-28 16:39:06 -07001490
1491 void EXPECT_RESIZE_STATE(const char* trace) {
1492 SCOPED_TRACE(trace);
1493 ScreenCapture::captureScreen(&sc);
1494 // The FG is now resized too 128,128 at 64,64
1495 sc->expectFGColor(64, 64);
1496 sc->expectFGColor(191, 191);
1497 sc->expectBGColor(192, 192);
1498 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001499};
1500
1501TEST_F(CropLatchingTest, CropLatching) {
1502 EXPECT_INITIAL_STATE("before anything");
1503 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001504 asTransaction([&](Transaction& t) {
1505 t.setSize(mFGSurfaceControl, 128, 128);
1506 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1507 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001508
1509 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1510
1511 restoreInitialState();
1512
Robert Carr4cdc58f2017-08-23 14:22:20 -07001513 asTransaction([&](Transaction& t) {
1514 t.setSize(mFGSurfaceControl, 128, 128);
1515 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1516 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1517 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001518
1519 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1520
1521 completeFGResize();
1522
1523 EXPECT_CROPPED_STATE("after the resize finishes");
1524}
1525
1526TEST_F(CropLatchingTest, FinalCropLatching) {
1527 EXPECT_INITIAL_STATE("before anything");
1528 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001529 asTransaction([&](Transaction& t) {
1530 t.setSize(mFGSurfaceControl, 128, 128);
1531 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1532 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001533
1534 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1535
1536 restoreInitialState();
1537
Robert Carr4cdc58f2017-08-23 14:22:20 -07001538 asTransaction([&](Transaction& t) {
1539 t.setSize(mFGSurfaceControl, 128, 128);
1540 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1541 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1542 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001543
1544 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1545
1546 completeFGResize();
1547
1548 EXPECT_CROPPED_STATE("after the resize finishes");
1549}
1550
Robert Carr7bf247e2017-05-18 14:02:49 -07001551// In this test we ensure that setGeometryAppliesWithResize actually demands
1552// a buffer of the new size, and not just any size.
1553TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1554 EXPECT_INITIAL_STATE("before anything");
1555 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001556 asTransaction([&](Transaction& t) {
1557 t.setSize(mFGSurfaceControl, 128, 128);
1558 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1559 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001560
1561 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1562
1563 restoreInitialState();
1564
1565 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1566 // initiating the resize.
1567 lockAndFillFGBuffer();
1568
Robert Carr4cdc58f2017-08-23 14:22:20 -07001569 asTransaction([&](Transaction& t) {
1570 t.setSize(mFGSurfaceControl, 128, 128);
1571 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1572 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1573 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001574
1575 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1576
1577 // We now submit our old buffer, at the old size, and ensure it doesn't
1578 // trigger geometry latching.
1579 unlockFGBuffer();
1580
1581 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1582
1583 completeFGResize();
1584
1585 EXPECT_CROPPED_STATE("after the resize finishes");
1586}
1587
1588TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1589 EXPECT_INITIAL_STATE("before anything");
1590 // In this scenario, we attempt to set the final crop a second time while the resize
1591 // is still pending, and ensure we are successful. Success meaning the second crop
1592 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001593 asTransaction([&](Transaction& t) {
1594 t.setSize(mFGSurfaceControl, 128, 128);
1595 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1596 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1597 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001598
chaviw59f5c562017-06-28 16:39:06 -07001599 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1600
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001601 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001602
chaviw59f5c562017-06-28 16:39:06 -07001603 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001604
1605 completeFGResize();
1606
chaviw59f5c562017-06-28 16:39:06 -07001607 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001608}
1609
Pablo Ceballos05289c22016-04-14 15:49:55 -07001610TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1611 sp<ScreenCapture> sc;
1612 {
1613 SCOPED_TRACE("before anything");
1614 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001615 sc->expectBGColor(32, 32);
1616 sc->expectFGColor(96, 96);
1617 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001618 }
1619
1620 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001621 asTransaction([&](Transaction& t) {
1622 t.setAlpha(mFGSurfaceControl, 0.75);
1623 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001624 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001625 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001626
Robert Carr4cdc58f2017-08-23 14:22:20 -07001627 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001628 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001629 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001630 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001631 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001632
1633 {
1634 SCOPED_TRACE("before any trigger");
1635 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001636 sc->expectBGColor(32, 32);
1637 sc->expectFGColor(96, 96);
1638 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001639 }
1640
1641 // should trigger the first deferred transaction, but not the second one
1642 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1643 {
1644 SCOPED_TRACE("after first trigger");
1645 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001646 sc->expectBGColor(32, 32);
1647 sc->checkPixel(96, 96, 162, 63, 96);
1648 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001649 }
1650
1651 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001652 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001653
1654 // trigger the second deferred transaction
1655 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1656 {
1657 SCOPED_TRACE("after second trigger");
1658 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001659 sc->expectBGColor(32, 32);
1660 sc->expectBGColor(96, 96);
1661 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001662 }
1663}
1664
Robert Carrdb66e622017-04-10 16:55:57 -07001665TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1666 sp<ScreenCapture> sc;
1667 {
1668 SCOPED_TRACE("before adding relative surface");
1669 ScreenCapture::captureScreen(&sc);
1670 sc->expectBGColor(24, 24);
1671 sc->expectFGColor(75, 75);
1672 sc->expectBGColor(145, 145);
1673 }
1674
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001675 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1676 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001677 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1678 waitForPostedBuffers();
1679
1680 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001681 asTransaction([&](Transaction& t) {
1682 t.setPosition(relativeSurfaceControl, 64, 64);
1683 t.show(relativeSurfaceControl);
1684 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1685 });
Robert Carrdb66e622017-04-10 16:55:57 -07001686
1687 {
1688 SCOPED_TRACE("after adding relative surface");
1689 ScreenCapture::captureScreen(&sc);
1690 // our relative surface should be visible now.
1691 sc->checkPixel(75, 75, 255, 177, 177);
1692 }
1693
1694 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001695 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001696
1697 {
1698 SCOPED_TRACE("after set layer");
1699 ScreenCapture::captureScreen(&sc);
1700 // now the FG surface should be visible again.
1701 sc->expectFGColor(75, 75);
1702 }
1703}
1704
Robert Carre392b552017-09-19 12:16:05 -07001705TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1706 sp<ScreenCapture> sc;
1707
1708 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001709 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1710 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1711 sp<SurfaceControl> childBuffer =
1712 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1713 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001714 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1715
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001716 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001717
1718 {
1719 ScreenCapture::captureScreen(&sc);
1720 sc->expectChildColor(73, 73);
1721 sc->expectFGColor(74, 74);
1722 }
1723
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001724 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001725
1726 {
1727 ScreenCapture::captureScreen(&sc);
1728 sc->expectChildColor(73, 73);
1729 sc->expectChildColor(74, 74);
1730 }
1731}
1732
Robert Carr2c5f6d22017-09-26 12:30:35 -07001733TEST_F(LayerUpdateTest, MergingTransactions) {
1734 sp<ScreenCapture> sc;
1735 {
1736 SCOPED_TRACE("before move");
1737 ScreenCapture::captureScreen(&sc);
1738 sc->expectBGColor(0, 12);
1739 sc->expectFGColor(75, 75);
1740 sc->expectBGColor(145, 145);
1741 }
1742
1743 Transaction t1, t2;
1744 t1.setPosition(mFGSurfaceControl, 128, 128);
1745 t2.setPosition(mFGSurfaceControl, 0, 0);
1746 // We expect that the position update from t2 now
1747 // overwrites the position update from t1.
1748 t1.merge(std::move(t2));
1749 t1.apply();
1750
1751 {
1752 ScreenCapture::captureScreen(&sc);
1753 sc->expectFGColor(1, 1);
1754 }
1755}
1756
Robert Carr1f0a16a2016-10-24 16:27:39 -07001757class ChildLayerTest : public LayerUpdateTest {
1758protected:
1759 void SetUp() override {
1760 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001761 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1762 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001763 fillSurfaceRGBA8(mChild, 200, 200, 200);
1764
1765 {
1766 SCOPED_TRACE("before anything");
1767 ScreenCapture::captureScreen(&mCapture);
1768 mCapture->expectChildColor(64, 64);
1769 }
1770 }
1771 void TearDown() override {
1772 LayerUpdateTest::TearDown();
1773 mChild = 0;
1774 }
1775
1776 sp<SurfaceControl> mChild;
1777 sp<ScreenCapture> mCapture;
1778};
1779
1780TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001781 asTransaction([&](Transaction& t) {
1782 t.show(mChild);
1783 t.setPosition(mChild, 10, 10);
1784 t.setPosition(mFGSurfaceControl, 64, 64);
1785 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001786
1787 {
1788 ScreenCapture::captureScreen(&mCapture);
1789 // Top left of foreground must now be visible
1790 mCapture->expectFGColor(64, 64);
1791 // But 10 pixels in we should see the child surface
1792 mCapture->expectChildColor(74, 74);
1793 // And 10 more pixels we should be back to the foreground surface
1794 mCapture->expectFGColor(84, 84);
1795 }
1796
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001797 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001798
1799 {
1800 ScreenCapture::captureScreen(&mCapture);
1801 // Top left of foreground should now be at 0, 0
1802 mCapture->expectFGColor(0, 0);
1803 // But 10 pixels in we should see the child surface
1804 mCapture->expectChildColor(10, 10);
1805 // And 10 more pixels we should be back to the foreground surface
1806 mCapture->expectFGColor(20, 20);
1807 }
1808}
1809
Robert Carr41b08b52017-06-01 16:11:34 -07001810TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001811 asTransaction([&](Transaction& t) {
1812 t.show(mChild);
1813 t.setPosition(mChild, 0, 0);
1814 t.setPosition(mFGSurfaceControl, 0, 0);
1815 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1816 });
Robert Carr41b08b52017-06-01 16:11:34 -07001817
1818 {
1819 ScreenCapture::captureScreen(&mCapture);
1820 mCapture->expectChildColor(0, 0);
1821 mCapture->expectChildColor(4, 4);
1822 mCapture->expectBGColor(5, 5);
1823 }
1824}
1825
1826TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001827 asTransaction([&](Transaction& t) {
1828 t.show(mChild);
1829 t.setPosition(mChild, 0, 0);
1830 t.setPosition(mFGSurfaceControl, 0, 0);
1831 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1832 });
Robert Carr41b08b52017-06-01 16:11:34 -07001833
1834 {
1835 ScreenCapture::captureScreen(&mCapture);
1836 mCapture->expectChildColor(0, 0);
1837 mCapture->expectChildColor(4, 4);
1838 mCapture->expectBGColor(5, 5);
1839 }
1840}
1841
Robert Carr1f0a16a2016-10-24 16:27:39 -07001842TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001843 asTransaction([&](Transaction& t) {
1844 t.show(mChild);
1845 t.setPosition(mFGSurfaceControl, 0, 0);
1846 t.setPosition(mChild, 63, 63);
1847 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001848
1849 {
1850 ScreenCapture::captureScreen(&mCapture);
1851 mCapture->expectFGColor(0, 0);
1852 // Last pixel in foreground should now be the child.
1853 mCapture->expectChildColor(63, 63);
1854 // But the child should be constrained and the next pixel
1855 // must be the background
1856 mCapture->expectBGColor(64, 64);
1857 }
1858}
1859
1860TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001861 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001862
1863 // Find the boundary between the parent and child
1864 {
1865 ScreenCapture::captureScreen(&mCapture);
1866 mCapture->expectChildColor(9, 9);
1867 mCapture->expectFGColor(10, 10);
1868 }
1869
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001870 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001871
1872 // The boundary should be twice as far from the origin now.
1873 // The pixels from the last test should all be child now
1874 {
1875 ScreenCapture::captureScreen(&mCapture);
1876 mCapture->expectChildColor(9, 9);
1877 mCapture->expectChildColor(10, 10);
1878 mCapture->expectChildColor(19, 19);
1879 mCapture->expectFGColor(20, 20);
1880 }
1881}
Robert Carr9524cb32017-02-13 11:32:32 -08001882
Robert Carr6452f122017-03-21 10:41:29 -07001883TEST_F(ChildLayerTest, ChildLayerAlpha) {
1884 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1885 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1886 fillSurfaceRGBA8(mChild, 0, 254, 0);
1887 waitForPostedBuffers();
1888
Robert Carr4cdc58f2017-08-23 14:22:20 -07001889 asTransaction([&](Transaction& t) {
1890 t.show(mChild);
1891 t.setPosition(mChild, 0, 0);
1892 t.setPosition(mFGSurfaceControl, 0, 0);
1893 });
Robert Carr6452f122017-03-21 10:41:29 -07001894
1895 {
1896 ScreenCapture::captureScreen(&mCapture);
1897 // Unblended child color
1898 mCapture->checkPixel(0, 0, 0, 254, 0);
1899 }
1900
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001901 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001902
1903 {
1904 ScreenCapture::captureScreen(&mCapture);
1905 // Child and BG blended.
1906 mCapture->checkPixel(0, 0, 127, 127, 0);
1907 }
1908
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001909 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001910
1911 {
1912 ScreenCapture::captureScreen(&mCapture);
1913 // Child and BG blended.
1914 mCapture->checkPixel(0, 0, 95, 64, 95);
1915 }
1916}
1917
Robert Carr9524cb32017-02-13 11:32:32 -08001918TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001919 asTransaction([&](Transaction& t) {
1920 t.show(mChild);
1921 t.setPosition(mChild, 10, 10);
1922 t.setPosition(mFGSurfaceControl, 64, 64);
1923 });
Robert Carr9524cb32017-02-13 11:32:32 -08001924
1925 {
1926 ScreenCapture::captureScreen(&mCapture);
1927 // Top left of foreground must now be visible
1928 mCapture->expectFGColor(64, 64);
1929 // But 10 pixels in we should see the child surface
1930 mCapture->expectChildColor(74, 74);
1931 // And 10 more pixels we should be back to the foreground surface
1932 mCapture->expectFGColor(84, 84);
1933 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001934
1935 asTransaction([&](Transaction& t) {
1936 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1937 });
1938
Robert Carr9524cb32017-02-13 11:32:32 -08001939 {
1940 ScreenCapture::captureScreen(&mCapture);
1941 mCapture->expectFGColor(64, 64);
1942 // In reparenting we should have exposed the entire foreground surface.
1943 mCapture->expectFGColor(74, 74);
1944 // And the child layer should now begin at 10, 10 (since the BG
1945 // layer is at (0, 0)).
1946 mCapture->expectBGColor(9, 9);
1947 mCapture->expectChildColor(10, 10);
1948 }
1949}
1950
chaviw161410b02017-07-27 10:46:08 -07001951TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001952 asTransaction([&](Transaction& t) {
1953 t.show(mChild);
1954 t.setPosition(mChild, 10, 10);
1955 t.setPosition(mFGSurfaceControl, 64, 64);
1956 });
Robert Carr9524cb32017-02-13 11:32:32 -08001957
1958 {
1959 ScreenCapture::captureScreen(&mCapture);
1960 // Top left of foreground must now be visible
1961 mCapture->expectFGColor(64, 64);
1962 // But 10 pixels in we should see the child surface
1963 mCapture->expectChildColor(74, 74);
1964 // And 10 more pixels we should be back to the foreground surface
1965 mCapture->expectFGColor(84, 84);
1966 }
1967
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001968 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08001969
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001970 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08001971
chaviw161410b02017-07-27 10:46:08 -07001972 // Since the child has the same client as the parent, it will not get
1973 // detached and will be hidden.
1974 {
1975 ScreenCapture::captureScreen(&mCapture);
1976 mCapture->expectFGColor(64, 64);
1977 mCapture->expectFGColor(74, 74);
1978 mCapture->expectFGColor(84, 84);
1979 }
1980}
1981
1982TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1983 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001984 sp<SurfaceControl> mChildNewClient =
1985 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1986 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07001987
1988 ASSERT_TRUE(mChildNewClient != NULL);
1989 ASSERT_TRUE(mChildNewClient->isValid());
1990
1991 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1992
Robert Carr4cdc58f2017-08-23 14:22:20 -07001993 asTransaction([&](Transaction& t) {
1994 t.hide(mChild);
1995 t.show(mChildNewClient);
1996 t.setPosition(mChildNewClient, 10, 10);
1997 t.setPosition(mFGSurfaceControl, 64, 64);
1998 });
chaviw161410b02017-07-27 10:46:08 -07001999
2000 {
2001 ScreenCapture::captureScreen(&mCapture);
2002 // Top left of foreground must now be visible
2003 mCapture->expectFGColor(64, 64);
2004 // But 10 pixels in we should see the child surface
2005 mCapture->expectChildColor(74, 74);
2006 // And 10 more pixels we should be back to the foreground surface
2007 mCapture->expectFGColor(84, 84);
2008 }
2009
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002010 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07002011
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002012 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07002013
Robert Carr9524cb32017-02-13 11:32:32 -08002014 // Nothing should have changed.
2015 {
2016 ScreenCapture::captureScreen(&mCapture);
2017 mCapture->expectFGColor(64, 64);
2018 mCapture->expectChildColor(74, 74);
2019 mCapture->expectFGColor(84, 84);
2020 }
2021}
2022
Robert Carr9b429f42017-04-17 14:56:57 -07002023TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002024 asTransaction([&](Transaction& t) {
2025 t.show(mChild);
2026 t.setPosition(mChild, 0, 0);
2027 t.setPosition(mFGSurfaceControl, 0, 0);
2028 });
Robert Carr9b429f42017-04-17 14:56:57 -07002029
2030 {
2031 ScreenCapture::captureScreen(&mCapture);
2032 // We've positioned the child in the top left.
2033 mCapture->expectChildColor(0, 0);
2034 // But it's only 10x10.
2035 mCapture->expectFGColor(10, 10);
2036 }
2037
Robert Carr4cdc58f2017-08-23 14:22:20 -07002038 asTransaction([&](Transaction& t) {
2039 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2040 // We cause scaling by 2.
2041 t.setSize(mFGSurfaceControl, 128, 128);
2042 });
Robert Carr9b429f42017-04-17 14:56:57 -07002043
2044 {
2045 ScreenCapture::captureScreen(&mCapture);
2046 // We've positioned the child in the top left.
2047 mCapture->expectChildColor(0, 0);
2048 mCapture->expectChildColor(10, 10);
2049 mCapture->expectChildColor(19, 19);
2050 // And now it should be scaled all the way to 20x20
2051 mCapture->expectFGColor(20, 20);
2052 }
2053}
2054
Robert Carr1725eee2017-04-26 18:32:15 -07002055// Regression test for b/37673612
2056TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002057 asTransaction([&](Transaction& t) {
2058 t.show(mChild);
2059 t.setPosition(mChild, 0, 0);
2060 t.setPosition(mFGSurfaceControl, 0, 0);
2061 });
Robert Carr1725eee2017-04-26 18:32:15 -07002062
2063 {
2064 ScreenCapture::captureScreen(&mCapture);
2065 // We've positioned the child in the top left.
2066 mCapture->expectChildColor(0, 0);
2067 // But it's only 10x10.
2068 mCapture->expectFGColor(10, 10);
2069 }
Robert Carr1725eee2017-04-26 18:32:15 -07002070 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2071 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002072 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002073 sp<Surface> s = mFGSurfaceControl->getSurface();
2074 auto anw = static_cast<ANativeWindow*>(s.get());
2075 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2076 native_window_set_buffers_dimensions(anw, 64, 128);
2077 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2078 waitForPostedBuffers();
2079
2080 {
2081 // The child should still be in the same place and not have any strange scaling as in
2082 // b/37673612.
2083 ScreenCapture::captureScreen(&mCapture);
2084 mCapture->expectChildColor(0, 0);
2085 mCapture->expectFGColor(10, 10);
2086 }
2087}
2088
Dan Stoza412903f2017-04-27 13:42:17 -07002089TEST_F(ChildLayerTest, Bug36858924) {
2090 // Destroy the child layer
2091 mChild.clear();
2092
2093 // Now recreate it as hidden
2094 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2095 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2096 mFGSurfaceControl.get());
2097
2098 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002099 asTransaction([&](Transaction& t) {
2100 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002101 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002102 t.show(mChild);
2103 });
Dan Stoza412903f2017-04-27 13:42:17 -07002104
2105 // Render the foreground surface a few times
2106 //
2107 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2108 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2109 // never acquire/release the first buffer
2110 ALOGI("Filling 1");
2111 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2112 ALOGI("Filling 2");
2113 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2114 ALOGI("Filling 3");
2115 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2116 ALOGI("Filling 4");
2117 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2118}
2119
chaviwf1961f72017-09-18 16:41:07 -07002120TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002121 asTransaction([&](Transaction& t) {
2122 t.show(mChild);
2123 t.setPosition(mChild, 10, 10);
2124 t.setPosition(mFGSurfaceControl, 64, 64);
2125 });
chaviw06178942017-07-27 10:25:59 -07002126
2127 {
2128 ScreenCapture::captureScreen(&mCapture);
2129 // Top left of foreground must now be visible
2130 mCapture->expectFGColor(64, 64);
2131 // But 10 pixels in we should see the child surface
2132 mCapture->expectChildColor(74, 74);
2133 // And 10 more pixels we should be back to the foreground surface
2134 mCapture->expectFGColor(84, 84);
2135 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002136
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002137 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002138
chaviw06178942017-07-27 10:25:59 -07002139 {
2140 ScreenCapture::captureScreen(&mCapture);
2141 mCapture->expectFGColor(64, 64);
2142 // In reparenting we should have exposed the entire foreground surface.
2143 mCapture->expectFGColor(74, 74);
2144 // And the child layer should now begin at 10, 10 (since the BG
2145 // layer is at (0, 0)).
2146 mCapture->expectBGColor(9, 9);
2147 mCapture->expectChildColor(10, 10);
2148 }
2149}
2150
chaviwf1961f72017-09-18 16:41:07 -07002151TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002152 asTransaction([&](Transaction& t) {
2153 t.show(mChild);
2154 t.setPosition(mChild, 10, 10);
2155 t.setPosition(mFGSurfaceControl, 64, 64);
2156 });
chaviwf1961f72017-09-18 16:41:07 -07002157
2158 {
2159 ScreenCapture::captureScreen(&mCapture);
2160 // Top left of foreground must now be visible
2161 mCapture->expectFGColor(64, 64);
2162 // But 10 pixels in we should see the child surface
2163 mCapture->expectChildColor(74, 74);
2164 // And 10 more pixels we should be back to the foreground surface
2165 mCapture->expectFGColor(84, 84);
2166 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002167 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002168 {
2169 ScreenCapture::captureScreen(&mCapture);
2170 // Nothing should have changed.
2171 mCapture->expectFGColor(64, 64);
2172 mCapture->expectChildColor(74, 74);
2173 mCapture->expectFGColor(84, 84);
2174 }
2175}
2176
2177TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002178 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2179 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07002180 ASSERT_TRUE(newSurface != NULL);
2181 ASSERT_TRUE(newSurface->isValid());
2182
2183 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002184 asTransaction([&](Transaction& t) {
2185 t.hide(mChild);
2186 t.show(newSurface);
2187 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002188 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002189 t.setPosition(mFGSurfaceControl, 64, 64);
2190 });
chaviwf1961f72017-09-18 16:41:07 -07002191
2192 {
2193 ScreenCapture::captureScreen(&mCapture);
2194 // Top left of foreground must now be visible
2195 mCapture->expectFGColor(64, 64);
2196 // At 10, 10 we should see the new surface
2197 mCapture->checkPixel(10, 10, 63, 195, 63);
2198 }
2199
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002200 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002201
2202 {
2203 ScreenCapture::captureScreen(&mCapture);
2204 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2205 // mFGSurface, putting it at 74, 74.
2206 mCapture->expectFGColor(64, 64);
2207 mCapture->checkPixel(74, 74, 63, 195, 63);
2208 mCapture->expectFGColor(84, 84);
2209 }
2210}
2211
chaviwc9674332017-08-28 12:32:18 -07002212TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002213 sp<SurfaceControl> grandchild =
2214 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2215 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002216 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2217
2218 {
2219 ScreenCapture::captureScreen(&mCapture);
2220 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2221 // which begins at 64, 64
2222 mCapture->checkPixel(64, 64, 50, 50, 50);
2223 }
2224}
2225
Robert Carr503c7042017-09-27 15:06:08 -07002226TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002227 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2228 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002229 fillSurfaceRGBA8(relative, 255, 255, 255);
2230
2231 Transaction t;
2232 t.setLayer(relative, INT32_MAX)
2233 .setRelativeLayer(mChild, relative->getHandle(), 1)
2234 .setPosition(mFGSurfaceControl, 0, 0)
2235 .apply(true);
2236
2237 // We expect that the child should have been elevated above our
2238 // INT_MAX layer even though it's not a child of it.
2239 {
2240 ScreenCapture::captureScreen(&mCapture);
2241 mCapture->expectChildColor(0, 0);
2242 mCapture->expectChildColor(9, 9);
2243 mCapture->checkPixel(10, 10, 255, 255, 255);
2244 }
2245}
2246
chaviw13fdc492017-06-27 12:40:18 -07002247class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002248protected:
chaviw13fdc492017-06-27 12:40:18 -07002249 void SetUp() override {
2250 LayerUpdateTest::SetUp();
2251
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002252 mLayerColorControl =
2253 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
2254 PIXEL_FORMAT_RGBA_8888,
2255 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07002256
2257 ASSERT_TRUE(mLayerColorControl != NULL);
2258 ASSERT_TRUE(mLayerColorControl->isValid());
2259
Robert Carr4cdc58f2017-08-23 14:22:20 -07002260 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002261 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002262 t.setPosition(mLayerColorControl, 140, 140);
2263 t.hide(mLayerColorControl);
2264 t.hide(mFGSurfaceControl);
2265 });
chaviw13fdc492017-06-27 12:40:18 -07002266 }
2267
2268 void TearDown() override {
2269 LayerUpdateTest::TearDown();
2270 mLayerColorControl = 0;
2271 }
2272
2273 sp<SurfaceControl> mLayerColorControl;
2274};
2275
2276TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2277 sp<ScreenCapture> sc;
2278
2279 {
2280 SCOPED_TRACE("before setColor");
2281 ScreenCapture::captureScreen(&sc);
2282 sc->expectBGColor(145, 145);
2283 }
2284
Robert Carr4cdc58f2017-08-23 14:22:20 -07002285 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002286 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002287 t.setColor(mLayerColorControl, color);
2288 t.show(mLayerColorControl);
2289 });
chaviw13fdc492017-06-27 12:40:18 -07002290
chaviw13fdc492017-06-27 12:40:18 -07002291 {
2292 // There should now be a color
2293 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002294
chaviw13fdc492017-06-27 12:40:18 -07002295 ScreenCapture::captureScreen(&sc);
2296 sc->checkPixel(145, 145, 43, 207, 131);
2297 }
2298}
2299
2300TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2301 sp<ScreenCapture> sc;
2302 {
2303 SCOPED_TRACE("before setColor");
2304 ScreenCapture::captureScreen(&sc);
2305 sc->expectBGColor(145, 145);
2306 }
2307
Robert Carr4cdc58f2017-08-23 14:22:20 -07002308 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002309 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002310 t.setColor(mLayerColorControl, color);
2311 t.setAlpha(mLayerColorControl, .75f);
2312 t.show(mLayerColorControl);
2313 });
2314
chaviw13fdc492017-06-27 12:40:18 -07002315 {
2316 // There should now be a color with .75 alpha
2317 SCOPED_TRACE("after setColor");
2318 ScreenCapture::captureScreen(&sc);
2319 sc->checkPixel(145, 145, 48, 171, 147);
2320 }
2321}
2322
2323TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2324 sp<ScreenCapture> sc;
2325 {
2326 SCOPED_TRACE("before setColor");
2327 ScreenCapture::captureScreen(&sc);
2328 sc->expectBGColor(145, 145);
2329 }
2330
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002331 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002332
chaviw13fdc492017-06-27 12:40:18 -07002333 {
2334 // There should now be set to 0,0,0 (black) as default.
2335 SCOPED_TRACE("after setColor");
2336 ScreenCapture::captureScreen(&sc);
2337 sc->checkPixel(145, 145, 0, 0, 0);
2338 }
2339}
2340
chaviwa76b2712017-09-20 12:02:26 -07002341class ScreenCaptureTest : public LayerUpdateTest {
2342protected:
2343 std::unique_ptr<CaptureLayer> mCapture;
2344};
2345
2346TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2347 auto bgHandle = mBGSurfaceControl->getHandle();
2348 CaptureLayer::captureScreen(&mCapture, bgHandle);
2349 mCapture->expectBGColor(0, 0);
2350 // Doesn't capture FG layer which is at 64, 64
2351 mCapture->expectBGColor(64, 64);
2352}
2353
2354TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2355 auto fgHandle = mFGSurfaceControl->getHandle();
2356
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002357 sp<SurfaceControl> child =
2358 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2359 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002360 fillSurfaceRGBA8(child, 200, 200, 200);
2361
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002362 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002363
2364 // Captures mFGSurfaceControl layer and its child.
2365 CaptureLayer::captureScreen(&mCapture, fgHandle);
2366 mCapture->expectFGColor(10, 10);
2367 mCapture->expectChildColor(0, 0);
2368}
2369
2370TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2371 auto fgHandle = mFGSurfaceControl->getHandle();
2372
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002373 sp<SurfaceControl> child =
2374 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2375 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002376 fillSurfaceRGBA8(child, 200, 200, 200);
2377
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002378 sp<SurfaceControl> grandchild =
2379 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2380 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002381
2382 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2383 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002384 .show(child)
2385 .setPosition(grandchild, 5, 5)
2386 .show(grandchild)
2387 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002388
2389 // Captures mFGSurfaceControl, its child, and the grandchild.
2390 CaptureLayer::captureScreen(&mCapture, fgHandle);
2391 mCapture->expectFGColor(10, 10);
2392 mCapture->expectChildColor(0, 0);
2393 mCapture->checkPixel(5, 5, 50, 50, 50);
2394}
2395
2396TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002397 sp<SurfaceControl> child =
2398 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2399 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002400 fillSurfaceRGBA8(child, 200, 200, 200);
2401 auto childHandle = child->getHandle();
2402
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002403 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002404
2405 // Captures only the child layer, and not the parent.
2406 CaptureLayer::captureScreen(&mCapture, childHandle);
2407 mCapture->expectChildColor(0, 0);
2408 mCapture->expectChildColor(9, 9);
2409}
2410
2411TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002412 sp<SurfaceControl> child =
2413 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2414 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002415 fillSurfaceRGBA8(child, 200, 200, 200);
2416 auto childHandle = child->getHandle();
2417
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002418 sp<SurfaceControl> grandchild =
2419 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2420 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002421 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2422
2423 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002424 .show(child)
2425 .setPosition(grandchild, 5, 5)
2426 .show(grandchild)
2427 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002428
2429 auto grandchildHandle = grandchild->getHandle();
2430
2431 // Captures only the grandchild.
2432 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2433 mCapture->checkPixel(0, 0, 50, 50, 50);
2434 mCapture->checkPixel(4, 4, 50, 50, 50);
2435}
2436
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002437} // namespace android