blob: 3110fdc3be0192aed22c2115f0e77c5c7201066e [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
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700991class LayerUpdateTest : public ::testing::Test {
992protected:
993 virtual void SetUp() {
994 mComposerClient = new SurfaceComposerClient;
995 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
996
Chia-I Wu1078bbb2017-10-20 11:29:02 -0700997 sp<IBinder> display(
998 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700999 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001000 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -07001001
1002 ssize_t displayWidth = info.w;
1003 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001004
1005 // Background surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001006 mBGSurfaceControl =
1007 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1008 displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001009 ASSERT_TRUE(mBGSurfaceControl != NULL);
1010 ASSERT_TRUE(mBGSurfaceControl->isValid());
1011 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1012
1013 // Foreground surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001014 mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1015 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001016 ASSERT_TRUE(mFGSurfaceControl != NULL);
1017 ASSERT_TRUE(mFGSurfaceControl->isValid());
1018
1019 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1020
1021 // Synchronization surface
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001022 mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1023 PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001024 ASSERT_TRUE(mSyncSurfaceControl != NULL);
1025 ASSERT_TRUE(mSyncSurfaceControl->isValid());
1026
1027 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1028
Robert Carr4cdc58f2017-08-23 14:22:20 -07001029 asTransaction([&](Transaction& t) {
1030 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001031
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001032 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001033
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001034 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1035 .setPosition(mFGSurfaceControl, 64, 64)
1036 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001037
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001038 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1039 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1040 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001041 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001042 }
1043
1044 virtual void TearDown() {
1045 mComposerClient->dispose();
1046 mBGSurfaceControl = 0;
1047 mFGSurfaceControl = 0;
1048 mSyncSurfaceControl = 0;
1049 mComposerClient = 0;
1050 }
1051
1052 void waitForPostedBuffers() {
1053 // Since the sync surface is in synchronous mode (i.e. double buffered)
1054 // posting three buffers to it should ensure that at least two
1055 // SurfaceFlinger::handlePageFlip calls have been made, which should
1056 // guaranteed that a buffer posted to another Surface has been retired.
1057 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1058 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1059 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1060 }
1061
Robert Carr4cdc58f2017-08-23 14:22:20 -07001062 void asTransaction(const std::function<void(Transaction&)>& exec) {
1063 Transaction t;
1064 exec(t);
1065 t.apply(true);
1066 }
1067
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001068 sp<SurfaceComposerClient> mComposerClient;
1069 sp<SurfaceControl> mBGSurfaceControl;
1070 sp<SurfaceControl> mFGSurfaceControl;
1071
1072 // This surface is used to ensure that the buffers posted to
1073 // mFGSurfaceControl have been picked up by SurfaceFlinger.
1074 sp<SurfaceControl> mSyncSurfaceControl;
1075};
1076
Robert Carr7f619b22017-11-06 12:56:35 -08001077TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1078 sp<ScreenCapture> sc;
1079
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001080 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1081 10, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08001082 fillSurfaceRGBA8(relative, 10, 10, 10);
1083 waitForPostedBuffers();
1084
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001085 Transaction{}
1086 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08001087 .setPosition(relative, 64, 64)
1088 .apply();
1089
1090 {
1091 // The relative should be on top of the FG control.
1092 ScreenCapture::captureScreen(&sc);
1093 sc->checkPixel(64, 64, 10, 10, 10);
1094 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001095 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001096
1097 {
1098 // Nothing should change at this point.
1099 ScreenCapture::captureScreen(&sc);
1100 sc->checkPixel(64, 64, 10, 10, 10);
1101 }
1102
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001103 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08001104
1105 {
1106 // Ensure that the relative was actually hidden, rather than
1107 // being left in the detached but visible state.
1108 ScreenCapture::captureScreen(&sc);
1109 sc->expectFGColor(64, 64);
1110 }
1111}
1112
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001113TEST_F(LayerUpdateTest, LayerMoveWorks) {
1114 sp<ScreenCapture> sc;
1115 {
1116 SCOPED_TRACE("before move");
1117 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001118 sc->expectBGColor(0, 12);
1119 sc->expectFGColor(75, 75);
1120 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001121 }
1122
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001123 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 128, 128); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001124
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001125 {
1126 // This should reflect the new position, but not the new color.
1127 SCOPED_TRACE("after move, before redraw");
1128 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001129 sc->expectBGColor(24, 24);
1130 sc->expectBGColor(75, 75);
1131 sc->expectFGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001132 }
1133
1134 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1135 waitForPostedBuffers();
1136 {
1137 // This should reflect the new position and the new color.
1138 SCOPED_TRACE("after redraw");
1139 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001140 sc->expectBGColor(24, 24);
1141 sc->expectBGColor(75, 75);
1142 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001143 }
1144}
1145
1146TEST_F(LayerUpdateTest, LayerResizeWorks) {
1147 sp<ScreenCapture> sc;
1148 {
1149 SCOPED_TRACE("before resize");
1150 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001151 sc->expectBGColor(0, 12);
1152 sc->expectFGColor(75, 75);
1153 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001154 }
1155
Steve Block9d453682011-12-20 16:23:08 +00001156 ALOGD("resizing");
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001157 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 128); });
Steve Block9d453682011-12-20 16:23:08 +00001158 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001159 {
1160 // This should not reflect the new size or color because SurfaceFlinger
1161 // has not yet received a buffer of the correct size.
1162 SCOPED_TRACE("after resize, before redraw");
1163 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001164 sc->expectBGColor(0, 12);
1165 sc->expectFGColor(75, 75);
1166 sc->expectBGColor(145, 145);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001167 }
1168
Steve Block9d453682011-12-20 16:23:08 +00001169 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001170 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
1171 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +00001172 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001173 {
1174 // This should reflect the new size and the new color.
1175 SCOPED_TRACE("after redraw");
1176 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001177 sc->expectBGColor(24, 24);
1178 sc->checkPixel(75, 75, 63, 195, 63);
1179 sc->checkPixel(145, 145, 63, 195, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001180 }
1181}
1182
Haixia Shid5750962015-07-27 16:50:49 -07001183TEST_F(LayerUpdateTest, LayerCropWorks) {
1184 sp<ScreenCapture> sc;
1185 {
1186 SCOPED_TRACE("before crop");
1187 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001188 sc->expectBGColor(24, 24);
1189 sc->expectFGColor(75, 75);
1190 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001191 }
1192
Robert Carr4cdc58f2017-08-23 14:22:20 -07001193 asTransaction([&](Transaction& t) {
1194 Rect cropRect(16, 16, 32, 32);
1195 t.setCrop(mFGSurfaceControl, cropRect);
1196 });
Haixia Shid5750962015-07-27 16:50:49 -07001197 {
1198 // This should crop the foreground surface.
1199 SCOPED_TRACE("after crop");
1200 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001201 sc->expectBGColor(24, 24);
1202 sc->expectBGColor(75, 75);
1203 sc->expectFGColor(95, 80);
1204 sc->expectFGColor(80, 95);
1205 sc->expectBGColor(96, 96);
Haixia Shid5750962015-07-27 16:50:49 -07001206 }
1207}
1208
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001209TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
1210 sp<ScreenCapture> sc;
1211 {
1212 SCOPED_TRACE("before crop");
1213 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001214 sc->expectBGColor(24, 24);
1215 sc->expectFGColor(75, 75);
1216 sc->expectBGColor(145, 145);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001217 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001218 asTransaction([&](Transaction& t) {
1219 Rect cropRect(16, 16, 32, 32);
1220 t.setFinalCrop(mFGSurfaceControl, cropRect);
1221 });
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001222 {
1223 // This should crop the foreground surface.
1224 SCOPED_TRACE("after crop");
1225 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001226 sc->expectBGColor(24, 24);
1227 sc->expectBGColor(75, 75);
1228 sc->expectBGColor(95, 80);
1229 sc->expectBGColor(80, 95);
1230 sc->expectBGColor(96, 96);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001231 }
1232}
1233
Haixia Shid5750962015-07-27 16:50:49 -07001234TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
1235 sp<ScreenCapture> sc;
1236 {
1237 SCOPED_TRACE("before setLayer");
1238 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001239 sc->expectBGColor(24, 24);
1240 sc->expectFGColor(75, 75);
1241 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001242 }
1243
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001244 asTransaction([&](Transaction& t) { t.setLayer(mFGSurfaceControl, INT_MAX - 3); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001245
Haixia Shid5750962015-07-27 16:50:49 -07001246 {
1247 // This should hide the foreground surface beneath the background.
1248 SCOPED_TRACE("after setLayer");
1249 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001250 sc->expectBGColor(24, 24);
1251 sc->expectBGColor(75, 75);
1252 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001253 }
1254}
1255
1256TEST_F(LayerUpdateTest, LayerShowHideWorks) {
1257 sp<ScreenCapture> sc;
1258 {
1259 SCOPED_TRACE("before hide");
1260 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001261 sc->expectBGColor(24, 24);
1262 sc->expectFGColor(75, 75);
1263 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001264 }
1265
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001266 asTransaction([&](Transaction& t) { t.hide(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001267
Haixia Shid5750962015-07-27 16:50:49 -07001268 {
1269 // This should hide the foreground surface.
1270 SCOPED_TRACE("after hide, before show");
1271 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001272 sc->expectBGColor(24, 24);
1273 sc->expectBGColor(75, 75);
1274 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001275 }
1276
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001277 asTransaction([&](Transaction& t) { t.show(mFGSurfaceControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001278
Haixia Shid5750962015-07-27 16:50:49 -07001279 {
1280 // This should show the foreground surface.
1281 SCOPED_TRACE("after show");
1282 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001283 sc->expectBGColor(24, 24);
1284 sc->expectFGColor(75, 75);
1285 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001286 }
1287}
1288
1289TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
1290 sp<ScreenCapture> sc;
1291 {
1292 SCOPED_TRACE("before setAlpha");
1293 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001294 sc->expectBGColor(24, 24);
1295 sc->expectFGColor(75, 75);
1296 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001297 }
1298
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001299 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.75f); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07001300
Haixia Shid5750962015-07-27 16:50:49 -07001301 {
1302 // This should set foreground to be 75% opaque.
1303 SCOPED_TRACE("after setAlpha");
1304 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001305 sc->expectBGColor(24, 24);
1306 sc->checkPixel(75, 75, 162, 63, 96);
1307 sc->expectBGColor(145, 145);
Haixia Shid5750962015-07-27 16:50:49 -07001308 }
1309}
1310
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001311TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
1312 sp<ScreenCapture> sc;
1313 {
1314 SCOPED_TRACE("before setLayerStack");
1315 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001316 sc->expectBGColor(24, 24);
1317 sc->expectFGColor(75, 75);
1318 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001319 }
1320
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001321 asTransaction([&](Transaction& t) { t.setLayerStack(mFGSurfaceControl, 1); });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001322 {
1323 // This should hide the foreground surface since it goes to a different
1324 // layer stack.
1325 SCOPED_TRACE("after setLayerStack");
1326 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001327 sc->expectBGColor(24, 24);
1328 sc->expectBGColor(75, 75);
1329 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001330 }
1331}
1332
1333TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
1334 sp<ScreenCapture> sc;
1335 {
1336 SCOPED_TRACE("before setFlags");
1337 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001338 sc->expectBGColor(24, 24);
1339 sc->expectFGColor(75, 75);
1340 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001341 }
1342
Robert Carr4cdc58f2017-08-23 14:22:20 -07001343 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001344 t.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001345 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001346 {
1347 // This should hide the foreground surface
1348 SCOPED_TRACE("after setFlags");
1349 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001350 sc->expectBGColor(24, 24);
1351 sc->expectBGColor(75, 75);
1352 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001353 }
1354}
1355
1356TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
1357 sp<ScreenCapture> sc;
1358 {
1359 SCOPED_TRACE("before setMatrix");
1360 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001361 sc->expectBGColor(24, 24);
1362 sc->expectFGColor(91, 96);
1363 sc->expectFGColor(96, 101);
1364 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001365 }
1366
Robert Carr4cdc58f2017-08-23 14:22:20 -07001367 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001368 t.setMatrix(mFGSurfaceControl, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001369 });
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001370 {
1371 SCOPED_TRACE("after setMatrix");
1372 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001373 sc->expectBGColor(24, 24);
1374 sc->expectFGColor(91, 96);
1375 sc->expectBGColor(96, 91);
1376 sc->expectBGColor(145, 145);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07001377 }
1378}
1379
Robert Carr8d5227b2017-03-16 15:41:03 -07001380class GeometryLatchingTest : public LayerUpdateTest {
1381protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001382 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001383 SCOPED_TRACE(trace);
1384 ScreenCapture::captureScreen(&sc);
1385 // We find the leading edge of the FG surface.
1386 sc->expectFGColor(127, 127);
1387 sc->expectBGColor(128, 128);
1388 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001389
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001390 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07001391
1392 void unlockFGBuffer() {
1393 sp<Surface> s = mFGSurfaceControl->getSurface();
1394 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1395 waitForPostedBuffers();
1396 }
1397
Robert Carr8d5227b2017-03-16 15:41:03 -07001398 void completeFGResize() {
1399 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1400 waitForPostedBuffers();
1401 }
1402 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001403 asTransaction([&](Transaction& t) {
1404 t.setSize(mFGSurfaceControl, 64, 64);
1405 t.setPosition(mFGSurfaceControl, 64, 64);
1406 t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1407 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1408 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001409
1410 EXPECT_INITIAL_STATE("After restoring initial state");
1411 }
1412 sp<ScreenCapture> sc;
1413};
1414
1415TEST_F(GeometryLatchingTest, SurfacePositionLatching) {
1416 EXPECT_INITIAL_STATE("before anything");
1417
1418 // By default position can be updated even while
1419 // a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001420 asTransaction([&](Transaction& t) {
1421 t.setSize(mFGSurfaceControl, 32, 32);
1422 t.setPosition(mFGSurfaceControl, 100, 100);
1423 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001424
1425 {
1426 SCOPED_TRACE("After moving surface");
1427 ScreenCapture::captureScreen(&sc);
1428 // If we moved, the FG Surface should cover up what was previously BG
1429 // however if we didn't move the FG wouldn't be large enough now.
1430 sc->expectFGColor(163, 163);
1431 }
1432
1433 restoreInitialState();
1434
1435 // Now we repeat with setGeometryAppliesWithResize
1436 // and verify the position DOESN'T latch.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001437 asTransaction([&](Transaction& t) {
1438 t.setGeometryAppliesWithResize(mFGSurfaceControl);
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("While resize is pending");
1445 ScreenCapture::captureScreen(&sc);
1446 // This time we shouldn't have moved, so the BG color
1447 // should still be visible.
1448 sc->expectBGColor(128, 128);
1449 }
1450
1451 completeFGResize();
1452
1453 {
1454 SCOPED_TRACE("After the resize");
1455 ScreenCapture::captureScreen(&sc);
1456 // But after the resize completes, we should move
1457 // and the FG should be visible here.
1458 sc->expectFGColor(128, 128);
1459 }
1460}
1461
1462class CropLatchingTest : public GeometryLatchingTest {
1463protected:
1464 void EXPECT_CROPPED_STATE(const char* trace) {
1465 SCOPED_TRACE(trace);
1466 ScreenCapture::captureScreen(&sc);
1467 // The edge should be moved back one pixel by our crop.
1468 sc->expectFGColor(126, 126);
1469 sc->expectBGColor(127, 127);
1470 sc->expectBGColor(128, 128);
1471 }
chaviw59f5c562017-06-28 16:39:06 -07001472
1473 void EXPECT_RESIZE_STATE(const char* trace) {
1474 SCOPED_TRACE(trace);
1475 ScreenCapture::captureScreen(&sc);
1476 // The FG is now resized too 128,128 at 64,64
1477 sc->expectFGColor(64, 64);
1478 sc->expectFGColor(191, 191);
1479 sc->expectBGColor(192, 192);
1480 }
Robert Carr8d5227b2017-03-16 15:41:03 -07001481};
1482
1483TEST_F(CropLatchingTest, CropLatching) {
1484 EXPECT_INITIAL_STATE("before anything");
1485 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001486 asTransaction([&](Transaction& t) {
1487 t.setSize(mFGSurfaceControl, 128, 128);
1488 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1489 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001490
1491 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1492
1493 restoreInitialState();
1494
Robert Carr4cdc58f2017-08-23 14:22:20 -07001495 asTransaction([&](Transaction& t) {
1496 t.setSize(mFGSurfaceControl, 128, 128);
1497 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1498 t.setCrop(mFGSurfaceControl, Rect(0, 0, 63, 63));
1499 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001500
1501 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1502
1503 completeFGResize();
1504
1505 EXPECT_CROPPED_STATE("after the resize finishes");
1506}
1507
1508TEST_F(CropLatchingTest, FinalCropLatching) {
1509 EXPECT_INITIAL_STATE("before anything");
1510 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001511 asTransaction([&](Transaction& t) {
1512 t.setSize(mFGSurfaceControl, 128, 128);
1513 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1514 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001515
1516 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1517
1518 restoreInitialState();
1519
Robert Carr4cdc58f2017-08-23 14:22:20 -07001520 asTransaction([&](Transaction& t) {
1521 t.setSize(mFGSurfaceControl, 128, 128);
1522 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1523 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1524 });
Robert Carr8d5227b2017-03-16 15:41:03 -07001525
1526 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1527
1528 completeFGResize();
1529
1530 EXPECT_CROPPED_STATE("after the resize finishes");
1531}
1532
Robert Carr7bf247e2017-05-18 14:02:49 -07001533// In this test we ensure that setGeometryAppliesWithResize actually demands
1534// a buffer of the new size, and not just any size.
1535TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1536 EXPECT_INITIAL_STATE("before anything");
1537 // Normally the crop applies immediately even while a resize is pending.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001538 asTransaction([&](Transaction& t) {
1539 t.setSize(mFGSurfaceControl, 128, 128);
1540 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1541 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001542
1543 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1544
1545 restoreInitialState();
1546
1547 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1548 // initiating the resize.
1549 lockAndFillFGBuffer();
1550
Robert Carr4cdc58f2017-08-23 14:22:20 -07001551 asTransaction([&](Transaction& t) {
1552 t.setSize(mFGSurfaceControl, 128, 128);
1553 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1554 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1555 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001556
1557 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1558
1559 // We now submit our old buffer, at the old size, and ensure it doesn't
1560 // trigger geometry latching.
1561 unlockFGBuffer();
1562
1563 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1564
1565 completeFGResize();
1566
1567 EXPECT_CROPPED_STATE("after the resize finishes");
1568}
1569
1570TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) {
1571 EXPECT_INITIAL_STATE("before anything");
1572 // In this scenario, we attempt to set the final crop a second time while the resize
1573 // is still pending, and ensure we are successful. Success meaning the second crop
1574 // is the one which eventually latches and not the first.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001575 asTransaction([&](Transaction& t) {
1576 t.setSize(mFGSurfaceControl, 128, 128);
1577 t.setGeometryAppliesWithResize(mFGSurfaceControl);
1578 t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1579 });
Robert Carr7bf247e2017-05-18 14:02:49 -07001580
chaviw59f5c562017-06-28 16:39:06 -07001581 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize");
1582
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001583 asTransaction([&](Transaction& t) { t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1)); });
Robert Carr7bf247e2017-05-18 14:02:49 -07001584
chaviw59f5c562017-06-28 16:39:06 -07001585 EXPECT_INITIAL_STATE("after setting another crop");
Robert Carr7bf247e2017-05-18 14:02:49 -07001586
1587 completeFGResize();
1588
chaviw59f5c562017-06-28 16:39:06 -07001589 EXPECT_RESIZE_STATE("after the resize finishes");
Robert Carr7bf247e2017-05-18 14:02:49 -07001590}
1591
Pablo Ceballos05289c22016-04-14 15:49:55 -07001592TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1593 sp<ScreenCapture> sc;
1594 {
1595 SCOPED_TRACE("before anything");
1596 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001597 sc->expectBGColor(32, 32);
1598 sc->expectFGColor(96, 96);
1599 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001600 }
1601
1602 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07001603 asTransaction([&](Transaction& t) {
1604 t.setAlpha(mFGSurfaceControl, 0.75);
1605 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001606 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07001607 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001608
Robert Carr4cdc58f2017-08-23 14:22:20 -07001609 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001610 t.setPosition(mFGSurfaceControl, 128, 128);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001611 t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001612 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001613 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001614
1615 {
1616 SCOPED_TRACE("before any trigger");
1617 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001618 sc->expectBGColor(32, 32);
1619 sc->expectFGColor(96, 96);
1620 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001621 }
1622
1623 // should trigger the first deferred transaction, but not the second one
1624 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1625 {
1626 SCOPED_TRACE("after first trigger");
1627 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001628 sc->expectBGColor(32, 32);
1629 sc->checkPixel(96, 96, 162, 63, 96);
1630 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001631 }
1632
1633 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001634 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07001635
1636 // trigger the second deferred transaction
1637 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1638 {
1639 SCOPED_TRACE("after second trigger");
1640 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08001641 sc->expectBGColor(32, 32);
1642 sc->expectBGColor(96, 96);
1643 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07001644 }
1645}
1646
Robert Carrdb66e622017-04-10 16:55:57 -07001647TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) {
1648 sp<ScreenCapture> sc;
1649 {
1650 SCOPED_TRACE("before adding relative surface");
1651 ScreenCapture::captureScreen(&sc);
1652 sc->expectBGColor(24, 24);
1653 sc->expectFGColor(75, 75);
1654 sc->expectBGColor(145, 145);
1655 }
1656
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001657 auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64, 64,
1658 PIXEL_FORMAT_RGBA_8888, 0);
Robert Carrdb66e622017-04-10 16:55:57 -07001659 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177);
1660 waitForPostedBuffers();
1661
1662 // Now we stack the surface above the foreground surface and make sure it is visible.
Robert Carr4cdc58f2017-08-23 14:22:20 -07001663 asTransaction([&](Transaction& t) {
1664 t.setPosition(relativeSurfaceControl, 64, 64);
1665 t.show(relativeSurfaceControl);
1666 t.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1667 });
Robert Carrdb66e622017-04-10 16:55:57 -07001668
1669 {
1670 SCOPED_TRACE("after adding relative surface");
1671 ScreenCapture::captureScreen(&sc);
1672 // our relative surface should be visible now.
1673 sc->checkPixel(75, 75, 255, 177, 177);
1674 }
1675
1676 // A call to setLayer will override a call to setRelativeLayer
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001677 asTransaction([&](Transaction& t) { t.setLayer(relativeSurfaceControl, 0); });
Robert Carrdb66e622017-04-10 16:55:57 -07001678
1679 {
1680 SCOPED_TRACE("after set layer");
1681 ScreenCapture::captureScreen(&sc);
1682 // now the FG surface should be visible again.
1683 sc->expectFGColor(75, 75);
1684 }
1685}
1686
Robert Carre392b552017-09-19 12:16:05 -07001687TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1688 sp<ScreenCapture> sc;
1689
1690 sp<SurfaceControl> childNoBuffer =
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001691 mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1692 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1693 sp<SurfaceControl> childBuffer =
1694 mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1695 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07001696 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1697
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001698 SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001699
1700 {
1701 ScreenCapture::captureScreen(&sc);
1702 sc->expectChildColor(73, 73);
1703 sc->expectFGColor(74, 74);
1704 }
1705
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001706 SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
Robert Carre392b552017-09-19 12:16:05 -07001707
1708 {
1709 ScreenCapture::captureScreen(&sc);
1710 sc->expectChildColor(73, 73);
1711 sc->expectChildColor(74, 74);
1712 }
1713}
1714
Robert Carr2c5f6d22017-09-26 12:30:35 -07001715TEST_F(LayerUpdateTest, MergingTransactions) {
1716 sp<ScreenCapture> sc;
1717 {
1718 SCOPED_TRACE("before move");
1719 ScreenCapture::captureScreen(&sc);
1720 sc->expectBGColor(0, 12);
1721 sc->expectFGColor(75, 75);
1722 sc->expectBGColor(145, 145);
1723 }
1724
1725 Transaction t1, t2;
1726 t1.setPosition(mFGSurfaceControl, 128, 128);
1727 t2.setPosition(mFGSurfaceControl, 0, 0);
1728 // We expect that the position update from t2 now
1729 // overwrites the position update from t1.
1730 t1.merge(std::move(t2));
1731 t1.apply();
1732
1733 {
1734 ScreenCapture::captureScreen(&sc);
1735 sc->expectFGColor(1, 1);
1736 }
1737}
1738
Robert Carr1f0a16a2016-10-24 16:27:39 -07001739class ChildLayerTest : public LayerUpdateTest {
1740protected:
1741 void SetUp() override {
1742 LayerUpdateTest::SetUp();
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001743 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1744 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001745 fillSurfaceRGBA8(mChild, 200, 200, 200);
1746
1747 {
1748 SCOPED_TRACE("before anything");
1749 ScreenCapture::captureScreen(&mCapture);
1750 mCapture->expectChildColor(64, 64);
1751 }
1752 }
1753 void TearDown() override {
1754 LayerUpdateTest::TearDown();
1755 mChild = 0;
1756 }
1757
1758 sp<SurfaceControl> mChild;
1759 sp<ScreenCapture> mCapture;
1760};
1761
1762TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001763 asTransaction([&](Transaction& t) {
1764 t.show(mChild);
1765 t.setPosition(mChild, 10, 10);
1766 t.setPosition(mFGSurfaceControl, 64, 64);
1767 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001768
1769 {
1770 ScreenCapture::captureScreen(&mCapture);
1771 // Top left of foreground must now be visible
1772 mCapture->expectFGColor(64, 64);
1773 // But 10 pixels in we should see the child surface
1774 mCapture->expectChildColor(74, 74);
1775 // And 10 more pixels we should be back to the foreground surface
1776 mCapture->expectFGColor(84, 84);
1777 }
1778
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001779 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001780
1781 {
1782 ScreenCapture::captureScreen(&mCapture);
1783 // Top left of foreground should now be at 0, 0
1784 mCapture->expectFGColor(0, 0);
1785 // But 10 pixels in we should see the child surface
1786 mCapture->expectChildColor(10, 10);
1787 // And 10 more pixels we should be back to the foreground surface
1788 mCapture->expectFGColor(20, 20);
1789 }
1790}
1791
Robert Carr41b08b52017-06-01 16:11:34 -07001792TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001793 asTransaction([&](Transaction& t) {
1794 t.show(mChild);
1795 t.setPosition(mChild, 0, 0);
1796 t.setPosition(mFGSurfaceControl, 0, 0);
1797 t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1798 });
Robert Carr41b08b52017-06-01 16:11:34 -07001799
1800 {
1801 ScreenCapture::captureScreen(&mCapture);
1802 mCapture->expectChildColor(0, 0);
1803 mCapture->expectChildColor(4, 4);
1804 mCapture->expectBGColor(5, 5);
1805 }
1806}
1807
1808TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001809 asTransaction([&](Transaction& t) {
1810 t.show(mChild);
1811 t.setPosition(mChild, 0, 0);
1812 t.setPosition(mFGSurfaceControl, 0, 0);
1813 t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1814 });
Robert Carr41b08b52017-06-01 16:11:34 -07001815
1816 {
1817 ScreenCapture::captureScreen(&mCapture);
1818 mCapture->expectChildColor(0, 0);
1819 mCapture->expectChildColor(4, 4);
1820 mCapture->expectBGColor(5, 5);
1821 }
1822}
1823
Robert Carr1f0a16a2016-10-24 16:27:39 -07001824TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001825 asTransaction([&](Transaction& t) {
1826 t.show(mChild);
1827 t.setPosition(mFGSurfaceControl, 0, 0);
1828 t.setPosition(mChild, 63, 63);
1829 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001830
1831 {
1832 ScreenCapture::captureScreen(&mCapture);
1833 mCapture->expectFGColor(0, 0);
1834 // Last pixel in foreground should now be the child.
1835 mCapture->expectChildColor(63, 63);
1836 // But the child should be constrained and the next pixel
1837 // must be the background
1838 mCapture->expectBGColor(64, 64);
1839 }
1840}
1841
1842TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001843 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001844
1845 // Find the boundary between the parent and child
1846 {
1847 ScreenCapture::captureScreen(&mCapture);
1848 mCapture->expectChildColor(9, 9);
1849 mCapture->expectFGColor(10, 10);
1850 }
1851
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001852 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07001853
1854 // The boundary should be twice as far from the origin now.
1855 // The pixels from the last test should all be child now
1856 {
1857 ScreenCapture::captureScreen(&mCapture);
1858 mCapture->expectChildColor(9, 9);
1859 mCapture->expectChildColor(10, 10);
1860 mCapture->expectChildColor(19, 19);
1861 mCapture->expectFGColor(20, 20);
1862 }
1863}
Robert Carr9524cb32017-02-13 11:32:32 -08001864
Robert Carr6452f122017-03-21 10:41:29 -07001865TEST_F(ChildLayerTest, ChildLayerAlpha) {
1866 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
1867 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
1868 fillSurfaceRGBA8(mChild, 0, 254, 0);
1869 waitForPostedBuffers();
1870
Robert Carr4cdc58f2017-08-23 14:22:20 -07001871 asTransaction([&](Transaction& t) {
1872 t.show(mChild);
1873 t.setPosition(mChild, 0, 0);
1874 t.setPosition(mFGSurfaceControl, 0, 0);
1875 });
Robert Carr6452f122017-03-21 10:41:29 -07001876
1877 {
1878 ScreenCapture::captureScreen(&mCapture);
1879 // Unblended child color
1880 mCapture->checkPixel(0, 0, 0, 254, 0);
1881 }
1882
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001883 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001884
1885 {
1886 ScreenCapture::captureScreen(&mCapture);
1887 // Child and BG blended.
1888 mCapture->checkPixel(0, 0, 127, 127, 0);
1889 }
1890
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001891 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07001892
1893 {
1894 ScreenCapture::captureScreen(&mCapture);
1895 // Child and BG blended.
1896 mCapture->checkPixel(0, 0, 95, 64, 95);
1897 }
1898}
1899
Robert Carr9524cb32017-02-13 11:32:32 -08001900TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001901 asTransaction([&](Transaction& t) {
1902 t.show(mChild);
1903 t.setPosition(mChild, 10, 10);
1904 t.setPosition(mFGSurfaceControl, 64, 64);
1905 });
Robert Carr9524cb32017-02-13 11:32:32 -08001906
1907 {
1908 ScreenCapture::captureScreen(&mCapture);
1909 // Top left of foreground must now be visible
1910 mCapture->expectFGColor(64, 64);
1911 // But 10 pixels in we should see the child surface
1912 mCapture->expectChildColor(74, 74);
1913 // And 10 more pixels we should be back to the foreground surface
1914 mCapture->expectFGColor(84, 84);
1915 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001916
1917 asTransaction([&](Transaction& t) {
1918 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
1919 });
1920
Robert Carr9524cb32017-02-13 11:32:32 -08001921 {
1922 ScreenCapture::captureScreen(&mCapture);
1923 mCapture->expectFGColor(64, 64);
1924 // In reparenting we should have exposed the entire foreground surface.
1925 mCapture->expectFGColor(74, 74);
1926 // And the child layer should now begin at 10, 10 (since the BG
1927 // layer is at (0, 0)).
1928 mCapture->expectBGColor(9, 9);
1929 mCapture->expectChildColor(10, 10);
1930 }
1931}
1932
chaviw161410b02017-07-27 10:46:08 -07001933TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001934 asTransaction([&](Transaction& t) {
1935 t.show(mChild);
1936 t.setPosition(mChild, 10, 10);
1937 t.setPosition(mFGSurfaceControl, 64, 64);
1938 });
Robert Carr9524cb32017-02-13 11:32:32 -08001939
1940 {
1941 ScreenCapture::captureScreen(&mCapture);
1942 // Top left of foreground must now be visible
1943 mCapture->expectFGColor(64, 64);
1944 // But 10 pixels in we should see the child surface
1945 mCapture->expectChildColor(74, 74);
1946 // And 10 more pixels we should be back to the foreground surface
1947 mCapture->expectFGColor(84, 84);
1948 }
1949
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001950 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08001951
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001952 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08001953
chaviw161410b02017-07-27 10:46:08 -07001954 // Since the child has the same client as the parent, it will not get
1955 // detached and will be hidden.
1956 {
1957 ScreenCapture::captureScreen(&mCapture);
1958 mCapture->expectFGColor(64, 64);
1959 mCapture->expectFGColor(74, 74);
1960 mCapture->expectFGColor(84, 84);
1961 }
1962}
1963
1964TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
1965 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001966 sp<SurfaceControl> mChildNewClient =
1967 mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1968 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07001969
1970 ASSERT_TRUE(mChildNewClient != NULL);
1971 ASSERT_TRUE(mChildNewClient->isValid());
1972
1973 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
1974
Robert Carr4cdc58f2017-08-23 14:22:20 -07001975 asTransaction([&](Transaction& t) {
1976 t.hide(mChild);
1977 t.show(mChildNewClient);
1978 t.setPosition(mChildNewClient, 10, 10);
1979 t.setPosition(mFGSurfaceControl, 64, 64);
1980 });
chaviw161410b02017-07-27 10:46:08 -07001981
1982 {
1983 ScreenCapture::captureScreen(&mCapture);
1984 // Top left of foreground must now be visible
1985 mCapture->expectFGColor(64, 64);
1986 // But 10 pixels in we should see the child surface
1987 mCapture->expectChildColor(74, 74);
1988 // And 10 more pixels we should be back to the foreground surface
1989 mCapture->expectFGColor(84, 84);
1990 }
1991
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001992 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07001993
Chia-I Wu1078bbb2017-10-20 11:29:02 -07001994 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07001995
Robert Carr9524cb32017-02-13 11:32:32 -08001996 // Nothing should have changed.
1997 {
1998 ScreenCapture::captureScreen(&mCapture);
1999 mCapture->expectFGColor(64, 64);
2000 mCapture->expectChildColor(74, 74);
2001 mCapture->expectFGColor(84, 84);
2002 }
2003}
2004
Robert Carr9b429f42017-04-17 14:56:57 -07002005TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002006 asTransaction([&](Transaction& t) {
2007 t.show(mChild);
2008 t.setPosition(mChild, 0, 0);
2009 t.setPosition(mFGSurfaceControl, 0, 0);
2010 });
Robert Carr9b429f42017-04-17 14:56:57 -07002011
2012 {
2013 ScreenCapture::captureScreen(&mCapture);
2014 // We've positioned the child in the top left.
2015 mCapture->expectChildColor(0, 0);
2016 // But it's only 10x10.
2017 mCapture->expectFGColor(10, 10);
2018 }
2019
Robert Carr4cdc58f2017-08-23 14:22:20 -07002020 asTransaction([&](Transaction& t) {
2021 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2022 // We cause scaling by 2.
2023 t.setSize(mFGSurfaceControl, 128, 128);
2024 });
Robert Carr9b429f42017-04-17 14:56:57 -07002025
2026 {
2027 ScreenCapture::captureScreen(&mCapture);
2028 // We've positioned the child in the top left.
2029 mCapture->expectChildColor(0, 0);
2030 mCapture->expectChildColor(10, 10);
2031 mCapture->expectChildColor(19, 19);
2032 // And now it should be scaled all the way to 20x20
2033 mCapture->expectFGColor(20, 20);
2034 }
2035}
2036
Robert Carr1725eee2017-04-26 18:32:15 -07002037// Regression test for b/37673612
2038TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002039 asTransaction([&](Transaction& t) {
2040 t.show(mChild);
2041 t.setPosition(mChild, 0, 0);
2042 t.setPosition(mFGSurfaceControl, 0, 0);
2043 });
Robert Carr1725eee2017-04-26 18:32:15 -07002044
2045 {
2046 ScreenCapture::captureScreen(&mCapture);
2047 // We've positioned the child in the top left.
2048 mCapture->expectChildColor(0, 0);
2049 // But it's only 10x10.
2050 mCapture->expectFGColor(10, 10);
2051 }
Robert Carr1725eee2017-04-26 18:32:15 -07002052 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2053 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002054 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07002055 sp<Surface> s = mFGSurfaceControl->getSurface();
2056 auto anw = static_cast<ANativeWindow*>(s.get());
2057 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2058 native_window_set_buffers_dimensions(anw, 64, 128);
2059 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2060 waitForPostedBuffers();
2061
2062 {
2063 // The child should still be in the same place and not have any strange scaling as in
2064 // b/37673612.
2065 ScreenCapture::captureScreen(&mCapture);
2066 mCapture->expectChildColor(0, 0);
2067 mCapture->expectFGColor(10, 10);
2068 }
2069}
2070
Dan Stoza412903f2017-04-27 13:42:17 -07002071TEST_F(ChildLayerTest, Bug36858924) {
2072 // Destroy the child layer
2073 mChild.clear();
2074
2075 // Now recreate it as hidden
2076 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2077 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2078 mFGSurfaceControl.get());
2079
2080 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07002081 asTransaction([&](Transaction& t) {
2082 t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002083 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07002084 t.show(mChild);
2085 });
Dan Stoza412903f2017-04-27 13:42:17 -07002086
2087 // Render the foreground surface a few times
2088 //
2089 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2090 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2091 // never acquire/release the first buffer
2092 ALOGI("Filling 1");
2093 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2094 ALOGI("Filling 2");
2095 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2096 ALOGI("Filling 3");
2097 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2098 ALOGI("Filling 4");
2099 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2100}
2101
chaviwf1961f72017-09-18 16:41:07 -07002102TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002103 asTransaction([&](Transaction& t) {
2104 t.show(mChild);
2105 t.setPosition(mChild, 10, 10);
2106 t.setPosition(mFGSurfaceControl, 64, 64);
2107 });
chaviw06178942017-07-27 10:25:59 -07002108
2109 {
2110 ScreenCapture::captureScreen(&mCapture);
2111 // Top left of foreground must now be visible
2112 mCapture->expectFGColor(64, 64);
2113 // But 10 pixels in we should see the child surface
2114 mCapture->expectChildColor(74, 74);
2115 // And 10 more pixels we should be back to the foreground surface
2116 mCapture->expectFGColor(84, 84);
2117 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07002118
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002119 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002120
chaviw06178942017-07-27 10:25:59 -07002121 {
2122 ScreenCapture::captureScreen(&mCapture);
2123 mCapture->expectFGColor(64, 64);
2124 // In reparenting we should have exposed the entire foreground surface.
2125 mCapture->expectFGColor(74, 74);
2126 // And the child layer should now begin at 10, 10 (since the BG
2127 // layer is at (0, 0)).
2128 mCapture->expectBGColor(9, 9);
2129 mCapture->expectChildColor(10, 10);
2130 }
2131}
2132
chaviwf1961f72017-09-18 16:41:07 -07002133TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002134 asTransaction([&](Transaction& t) {
2135 t.show(mChild);
2136 t.setPosition(mChild, 10, 10);
2137 t.setPosition(mFGSurfaceControl, 64, 64);
2138 });
chaviwf1961f72017-09-18 16:41:07 -07002139
2140 {
2141 ScreenCapture::captureScreen(&mCapture);
2142 // Top left of foreground must now be visible
2143 mCapture->expectFGColor(64, 64);
2144 // But 10 pixels in we should see the child surface
2145 mCapture->expectChildColor(74, 74);
2146 // And 10 more pixels we should be back to the foreground surface
2147 mCapture->expectFGColor(84, 84);
2148 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002149 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07002150 {
2151 ScreenCapture::captureScreen(&mCapture);
2152 // Nothing should have changed.
2153 mCapture->expectFGColor(64, 64);
2154 mCapture->expectChildColor(74, 74);
2155 mCapture->expectFGColor(84, 84);
2156 }
2157}
2158
2159TEST_F(ChildLayerTest, ReparentFromNoParent) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002160 sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2161 PIXEL_FORMAT_RGBA_8888, 0);
chaviwf1961f72017-09-18 16:41:07 -07002162 ASSERT_TRUE(newSurface != NULL);
2163 ASSERT_TRUE(newSurface->isValid());
2164
2165 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002166 asTransaction([&](Transaction& t) {
2167 t.hide(mChild);
2168 t.show(newSurface);
2169 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002170 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002171 t.setPosition(mFGSurfaceControl, 64, 64);
2172 });
chaviwf1961f72017-09-18 16:41:07 -07002173
2174 {
2175 ScreenCapture::captureScreen(&mCapture);
2176 // Top left of foreground must now be visible
2177 mCapture->expectFGColor(64, 64);
2178 // At 10, 10 we should see the new surface
2179 mCapture->checkPixel(10, 10, 63, 195, 63);
2180 }
2181
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002182 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07002183
2184 {
2185 ScreenCapture::captureScreen(&mCapture);
2186 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2187 // mFGSurface, putting it at 74, 74.
2188 mCapture->expectFGColor(64, 64);
2189 mCapture->checkPixel(74, 74, 63, 195, 63);
2190 mCapture->expectFGColor(84, 84);
2191 }
2192}
2193
chaviwc9674332017-08-28 12:32:18 -07002194TEST_F(ChildLayerTest, NestedChildren) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002195 sp<SurfaceControl> grandchild =
2196 mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2197 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07002198 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2199
2200 {
2201 ScreenCapture::captureScreen(&mCapture);
2202 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2203 // which begins at 64, 64
2204 mCapture->checkPixel(64, 64, 50, 50, 50);
2205 }
2206}
2207
Robert Carr503c7042017-09-27 15:06:08 -07002208TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002209 sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2210 128, PIXEL_FORMAT_RGBA_8888, 0);
Robert Carr503c7042017-09-27 15:06:08 -07002211 fillSurfaceRGBA8(relative, 255, 255, 255);
2212
2213 Transaction t;
2214 t.setLayer(relative, INT32_MAX)
2215 .setRelativeLayer(mChild, relative->getHandle(), 1)
2216 .setPosition(mFGSurfaceControl, 0, 0)
2217 .apply(true);
2218
2219 // We expect that the child should have been elevated above our
2220 // INT_MAX layer even though it's not a child of it.
2221 {
2222 ScreenCapture::captureScreen(&mCapture);
2223 mCapture->expectChildColor(0, 0);
2224 mCapture->expectChildColor(9, 9);
2225 mCapture->checkPixel(10, 10, 255, 255, 255);
2226 }
2227}
2228
chaviw13fdc492017-06-27 12:40:18 -07002229class LayerColorTest : public LayerUpdateTest {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002230protected:
chaviw13fdc492017-06-27 12:40:18 -07002231 void SetUp() override {
2232 LayerUpdateTest::SetUp();
2233
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002234 mLayerColorControl =
2235 mComposerClient->createSurface(String8("Layer color surface"), 128, 128,
2236 PIXEL_FORMAT_RGBA_8888,
2237 ISurfaceComposerClient::eFXSurfaceColor);
chaviw13fdc492017-06-27 12:40:18 -07002238
2239 ASSERT_TRUE(mLayerColorControl != NULL);
2240 ASSERT_TRUE(mLayerColorControl->isValid());
2241
Robert Carr4cdc58f2017-08-23 14:22:20 -07002242 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002243 t.setLayer(mLayerColorControl, INT32_MAX - 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002244 t.setPosition(mLayerColorControl, 140, 140);
2245 t.hide(mLayerColorControl);
2246 t.hide(mFGSurfaceControl);
2247 });
chaviw13fdc492017-06-27 12:40:18 -07002248 }
2249
2250 void TearDown() override {
2251 LayerUpdateTest::TearDown();
2252 mLayerColorControl = 0;
2253 }
2254
2255 sp<SurfaceControl> mLayerColorControl;
2256};
2257
2258TEST_F(LayerColorTest, ColorLayerNoAlpha) {
2259 sp<ScreenCapture> sc;
2260
2261 {
2262 SCOPED_TRACE("before setColor");
2263 ScreenCapture::captureScreen(&sc);
2264 sc->expectBGColor(145, 145);
2265 }
2266
Robert Carr4cdc58f2017-08-23 14:22:20 -07002267 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002268 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002269 t.setColor(mLayerColorControl, color);
2270 t.show(mLayerColorControl);
2271 });
chaviw13fdc492017-06-27 12:40:18 -07002272
chaviw13fdc492017-06-27 12:40:18 -07002273 {
2274 // There should now be a color
2275 SCOPED_TRACE("after setColor");
Robert Carr4cdc58f2017-08-23 14:22:20 -07002276
chaviw13fdc492017-06-27 12:40:18 -07002277 ScreenCapture::captureScreen(&sc);
2278 sc->checkPixel(145, 145, 43, 207, 131);
2279 }
2280}
2281
2282TEST_F(LayerColorTest, ColorLayerWithAlpha) {
2283 sp<ScreenCapture> sc;
2284 {
2285 SCOPED_TRACE("before setColor");
2286 ScreenCapture::captureScreen(&sc);
2287 sc->expectBGColor(145, 145);
2288 }
2289
Robert Carr4cdc58f2017-08-23 14:22:20 -07002290 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002291 half3 color(43.0f / 255.0f, 207.0f / 255.0f, 131.0f / 255.0f);
Robert Carr4cdc58f2017-08-23 14:22:20 -07002292 t.setColor(mLayerColorControl, color);
2293 t.setAlpha(mLayerColorControl, .75f);
2294 t.show(mLayerColorControl);
2295 });
2296
chaviw13fdc492017-06-27 12:40:18 -07002297 {
2298 // There should now be a color with .75 alpha
2299 SCOPED_TRACE("after setColor");
2300 ScreenCapture::captureScreen(&sc);
2301 sc->checkPixel(145, 145, 48, 171, 147);
2302 }
2303}
2304
2305TEST_F(LayerColorTest, ColorLayerWithNoColor) {
2306 sp<ScreenCapture> sc;
2307 {
2308 SCOPED_TRACE("before setColor");
2309 ScreenCapture::captureScreen(&sc);
2310 sc->expectBGColor(145, 145);
2311 }
2312
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002313 asTransaction([&](Transaction& t) { t.show(mLayerColorControl); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07002314
chaviw13fdc492017-06-27 12:40:18 -07002315 {
2316 // There should now be set to 0,0,0 (black) as default.
2317 SCOPED_TRACE("after setColor");
2318 ScreenCapture::captureScreen(&sc);
2319 sc->checkPixel(145, 145, 0, 0, 0);
2320 }
2321}
2322
chaviwa76b2712017-09-20 12:02:26 -07002323class ScreenCaptureTest : public LayerUpdateTest {
2324protected:
2325 std::unique_ptr<CaptureLayer> mCapture;
2326};
2327
2328TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2329 auto bgHandle = mBGSurfaceControl->getHandle();
2330 CaptureLayer::captureScreen(&mCapture, bgHandle);
2331 mCapture->expectBGColor(0, 0);
2332 // Doesn't capture FG layer which is at 64, 64
2333 mCapture->expectBGColor(64, 64);
2334}
2335
2336TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2337 auto fgHandle = mFGSurfaceControl->getHandle();
2338
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002339 sp<SurfaceControl> child =
2340 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2341 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002342 fillSurfaceRGBA8(child, 200, 200, 200);
2343
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002344 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002345
2346 // Captures mFGSurfaceControl layer and its child.
2347 CaptureLayer::captureScreen(&mCapture, fgHandle);
2348 mCapture->expectFGColor(10, 10);
2349 mCapture->expectChildColor(0, 0);
2350}
2351
2352TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2353 auto fgHandle = mFGSurfaceControl->getHandle();
2354
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002355 sp<SurfaceControl> child =
2356 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2357 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002358 fillSurfaceRGBA8(child, 200, 200, 200);
2359
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002360 sp<SurfaceControl> grandchild =
2361 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2362 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002363
2364 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2365 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002366 .show(child)
2367 .setPosition(grandchild, 5, 5)
2368 .show(grandchild)
2369 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002370
2371 // Captures mFGSurfaceControl, its child, and the grandchild.
2372 CaptureLayer::captureScreen(&mCapture, fgHandle);
2373 mCapture->expectFGColor(10, 10);
2374 mCapture->expectChildColor(0, 0);
2375 mCapture->checkPixel(5, 5, 50, 50, 50);
2376}
2377
2378TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002379 sp<SurfaceControl> child =
2380 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2381 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002382 fillSurfaceRGBA8(child, 200, 200, 200);
2383 auto childHandle = child->getHandle();
2384
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002385 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002386
2387 // Captures only the child layer, and not the parent.
2388 CaptureLayer::captureScreen(&mCapture, childHandle);
2389 mCapture->expectChildColor(0, 0);
2390 mCapture->expectChildColor(9, 9);
2391}
2392
2393TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002394 sp<SurfaceControl> child =
2395 mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2396 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07002397 fillSurfaceRGBA8(child, 200, 200, 200);
2398 auto childHandle = child->getHandle();
2399
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002400 sp<SurfaceControl> grandchild =
2401 mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2402 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07002403 fillSurfaceRGBA8(grandchild, 50, 50, 50);
2404
2405 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002406 .show(child)
2407 .setPosition(grandchild, 5, 5)
2408 .show(grandchild)
2409 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07002410
2411 auto grandchildHandle = grandchild->getHandle();
2412
2413 // Captures only the grandchild.
2414 CaptureLayer::captureScreen(&mCapture, grandchildHandle);
2415 mCapture->checkPixel(0, 0, 50, 50, 50);
2416 mCapture->checkPixel(4, 4, 50, 50, 50);
2417}
2418
Chia-I Wu1078bbb2017-10-20 11:29:02 -07002419} // namespace android