• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include <algorithm>
18 #include <functional>
19 #include <limits>
20 #include <ostream>
21 
22 #include <gtest/gtest.h>
23 
24 #include <android/native_window.h>
25 
26 #include <gui/ISurfaceComposer.h>
27 #include <gui/LayerState.h>
28 
29 #include <gui/Surface.h>
30 #include <gui/SurfaceComposerClient.h>
31 #include <private/gui/ComposerService.h>
32 #include <private/android_filesystem_config.h>
33 
34 #include <ui/DisplayInfo.h>
35 #include <ui/Rect.h>
36 #include <utils/String8.h>
37 
38 #include <math.h>
39 #include <math/vec3.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42 
43 namespace android {
44 
45 namespace {
46 
47 struct Color {
48     uint8_t r;
49     uint8_t g;
50     uint8_t b;
51     uint8_t a;
52 
53     static const Color RED;
54     static const Color GREEN;
55     static const Color BLUE;
56     static const Color WHITE;
57     static const Color BLACK;
58     static const Color TRANSPARENT;
59 };
60 
61 const Color Color::RED{255, 0, 0, 255};
62 const Color Color::GREEN{0, 255, 0, 255};
63 const Color Color::BLUE{0, 0, 255, 255};
64 const Color Color::WHITE{255, 255, 255, 255};
65 const Color Color::BLACK{0, 0, 0, 255};
66 const Color Color::TRANSPARENT{0, 0, 0, 0};
67 
operator <<(std::ostream & os,const Color & color)68 std::ostream& operator<<(std::ostream& os, const Color& color) {
69     os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
70     return os;
71 }
72 
73 // Fill a region with the specified color.
fillANativeWindowBufferColor(const ANativeWindow_Buffer & buffer,const Rect & rect,const Color & color)74 void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
75                                   const Color& color) {
76     Rect r(0, 0, buffer.width, buffer.height);
77     if (!r.intersect(rect, &r)) {
78         return;
79     }
80 
81     int32_t width = r.right - r.left;
82     int32_t height = r.bottom - r.top;
83 
84     for (int32_t row = 0; row < height; row++) {
85         uint8_t* dst =
86                 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
87         for (int32_t column = 0; column < width; column++) {
88             dst[0] = color.r;
89             dst[1] = color.g;
90             dst[2] = color.b;
91             dst[3] = color.a;
92             dst += 4;
93         }
94     }
95 }
96 
97 // Fill a region with the specified color.
fillBufferColor(const ANativeWindow_Buffer & buffer,const Rect & rect,const Color & color)98 void fillBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect, const Color& color) {
99     int32_t x = rect.left;
100     int32_t y = rect.top;
101     int32_t width = rect.right - rect.left;
102     int32_t height = rect.bottom - rect.top;
103 
104     if (x < 0) {
105         width += x;
106         x = 0;
107     }
108     if (y < 0) {
109         height += y;
110         y = 0;
111     }
112     if (x + width > buffer.width) {
113         x = std::min(x, buffer.width);
114         width = buffer.width - x;
115     }
116     if (y + height > buffer.height) {
117         y = std::min(y, buffer.height);
118         height = buffer.height - y;
119     }
120 
121     for (int32_t j = 0; j < height; j++) {
122         uint8_t* dst = static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (y + j) + x) * 4;
123         for (int32_t i = 0; i < width; i++) {
124             dst[0] = color.r;
125             dst[1] = color.g;
126             dst[2] = color.b;
127             dst[3] = color.a;
128             dst += 4;
129         }
130     }
131 }
132 
133 // Check if a region has the specified color.
expectBufferColor(const sp<GraphicBuffer> & outBuffer,uint8_t * pixels,const Rect & rect,const Color & color,uint8_t tolerance)134 void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
135                        const Color& color, uint8_t tolerance) {
136     int32_t x = rect.left;
137     int32_t y = rect.top;
138     int32_t width = rect.right - rect.left;
139     int32_t height = rect.bottom - rect.top;
140 
141     int32_t bufferWidth = int32_t(outBuffer->getWidth());
142     int32_t bufferHeight = int32_t(outBuffer->getHeight());
143     if (x + width > bufferWidth) {
144         x = std::min(x, bufferWidth);
145         width = bufferWidth - x;
146     }
147     if (y + height > bufferHeight) {
148         y = std::min(y, bufferHeight);
149         height = bufferHeight - y;
150     }
151 
152     auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
153         uint8_t tmp = a >= b ? a - b : b - a;
154         return tmp <= tolerance;
155     };
156     for (int32_t j = 0; j < height; j++) {
157         const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
158         for (int32_t i = 0; i < width; i++) {
159             const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
160             EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
161                     << "pixel @ (" << x + i << ", " << y + j << "): "
162                     << "expected (" << color << "), "
163                     << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
164             src += 4;
165         }
166     }
167 }
168 
169 } // anonymous namespace
170 
171 using Transaction = SurfaceComposerClient::Transaction;
172 
173 // Fill an RGBA_8888 formatted surface with a single color.
fillSurfaceRGBA8(const sp<SurfaceControl> & sc,uint8_t r,uint8_t g,uint8_t b,bool unlock=true)174 static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
175                              bool unlock = true) {
176     ANativeWindow_Buffer outBuffer;
177     sp<Surface> s = sc->getSurface();
178     ASSERT_TRUE(s != nullptr);
179     ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
180     uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
181     for (int y = 0; y < outBuffer.height; y++) {
182         for (int x = 0; x < outBuffer.width; x++) {
183             uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
184             pixel[0] = r;
185             pixel[1] = g;
186             pixel[2] = b;
187             pixel[3] = 255;
188         }
189     }
190     if (unlock) {
191         ASSERT_EQ(NO_ERROR, s->unlockAndPost());
192     }
193 }
194 
195 // A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
196 // individual pixel values for testing purposes.
197 class ScreenCapture : public RefBase {
198 public:
captureScreen(sp<ScreenCapture> * sc,int32_t minLayerZ=0,int32_t maxLayerZ=std::numeric_limits<int32_t>::max ())199     static void captureScreen(sp<ScreenCapture>* sc, int32_t minLayerZ = 0,
200                               int32_t maxLayerZ = std::numeric_limits<int32_t>::max()) {
201         sp<ISurfaceComposer> sf(ComposerService::getComposerService());
202         sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
203         SurfaceComposerClient::Transaction().apply(true);
204 
205         sp<GraphicBuffer> outBuffer;
206         ASSERT_EQ(NO_ERROR,
207                   sf->captureScreen(display, &outBuffer, Rect(), 0, 0, minLayerZ, maxLayerZ,
208                                     false));
209         *sc = new ScreenCapture(outBuffer);
210     }
211 
captureLayers(std::unique_ptr<ScreenCapture> * sc,sp<IBinder> & parentHandle,Rect crop=Rect::EMPTY_RECT,float frameScale=1.0)212     static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
213                               Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
214         sp<ISurfaceComposer> sf(ComposerService::getComposerService());
215         SurfaceComposerClient::Transaction().apply(true);
216 
217         sp<GraphicBuffer> outBuffer;
218         ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
219         *sc = std::make_unique<ScreenCapture>(outBuffer);
220     }
221 
captureChildLayers(std::unique_ptr<ScreenCapture> * sc,sp<IBinder> & parentHandle,Rect crop=Rect::EMPTY_RECT,float frameScale=1.0)222     static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
223                                    Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
224         sp<ISurfaceComposer> sf(ComposerService::getComposerService());
225         SurfaceComposerClient::Transaction().apply(true);
226 
227         sp<GraphicBuffer> outBuffer;
228         ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
229         *sc = std::make_unique<ScreenCapture>(outBuffer);
230     }
231 
expectColor(const Rect & rect,const Color & color,uint8_t tolerance=0)232     void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
233         ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
234         expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
235     }
236 
expectBorder(const Rect & rect,const Color & color,uint8_t tolerance=0)237     void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
238         ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
239         const bool leftBorder = rect.left > 0;
240         const bool topBorder = rect.top > 0;
241         const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
242         const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
243 
244         if (topBorder) {
245             Rect top(rect.left, rect.top - 1, rect.right, rect.top);
246             if (leftBorder) {
247                 top.left -= 1;
248             }
249             if (rightBorder) {
250                 top.right += 1;
251             }
252             expectColor(top, color, tolerance);
253         }
254         if (leftBorder) {
255             Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
256             expectColor(left, color, tolerance);
257         }
258         if (rightBorder) {
259             Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
260             expectColor(right, color, tolerance);
261         }
262         if (bottomBorder) {
263             Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
264             if (leftBorder) {
265                 bottom.left -= 1;
266             }
267             if (rightBorder) {
268                 bottom.right += 1;
269             }
270             expectColor(bottom, color, tolerance);
271         }
272     }
273 
expectQuadrant(const Rect & rect,const Color & topLeft,const Color & topRight,const Color & bottomLeft,const Color & bottomRight,bool filtered=false,uint8_t tolerance=0)274     void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
275                         const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
276                         uint8_t tolerance = 0) {
277         ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
278 
279         const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
280         const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
281         // avoid checking borders due to unspecified filtering behavior
282         const int32_t offsetX = filtered ? 2 : 0;
283         const int32_t offsetY = filtered ? 2 : 0;
284         expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
285                     tolerance);
286         expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
287                     tolerance);
288         expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
289                     tolerance);
290         expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
291                     bottomRight, tolerance);
292     }
293 
checkPixel(uint32_t x,uint32_t y,uint8_t r,uint8_t g,uint8_t b)294     void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
295         ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
296         const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
297         if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
298             String8 err(String8::format("pixel @ (%3d, %3d): "
299                                         "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
300                                         x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
301             EXPECT_EQ(String8(), err) << err.string();
302         }
303     }
304 
expectFGColor(uint32_t x,uint32_t y)305     void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
306 
expectBGColor(uint32_t x,uint32_t y)307     void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
308 
expectChildColor(uint32_t x,uint32_t y)309     void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
310 
ScreenCapture(const sp<GraphicBuffer> & outBuffer)311     ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
312         mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
313     }
314 
~ScreenCapture()315     ~ScreenCapture() { mOutBuffer->unlock(); }
316 
317 private:
318     sp<GraphicBuffer> mOutBuffer;
319     uint8_t* mPixels = nullptr;
320 };
321 
322 class LayerTransactionTest : public ::testing::Test {
323 protected:
SetUp()324     void SetUp() override {
325         mClient = new SurfaceComposerClient;
326         ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
327 
328         ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
329     }
330 
createLayer(const char * name,uint32_t width,uint32_t height,uint32_t flags=0)331     sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
332                                    uint32_t flags = 0) {
333         auto layer =
334                 mClient->createSurface(String8(name), width, height, PIXEL_FORMAT_RGBA_8888, flags);
335         EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
336 
337         status_t error = Transaction()
338                                  .setLayerStack(layer, mDisplayLayerStack)
339                                  .setLayer(layer, mLayerZBase)
340                                  .apply();
341         if (error != NO_ERROR) {
342             ADD_FAILURE() << "failed to initialize SurfaceControl";
343             layer.clear();
344         }
345 
346         return layer;
347     }
348 
getBufferQueueLayerBuffer(const sp<SurfaceControl> & layer)349     ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
350         // wait for previous transactions (such as setSize) to complete
351         Transaction().apply(true);
352 
353         ANativeWindow_Buffer buffer = {};
354         EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
355 
356         return buffer;
357     }
358 
getLayerBuffer(const sp<SurfaceControl> & layer)359     ANativeWindow_Buffer getLayerBuffer(const sp<SurfaceControl>& layer) {
360         // wait for previous transactions (such as setSize) to complete
361         Transaction().apply(true);
362 
363         ANativeWindow_Buffer buffer = {};
364         EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
365 
366         return buffer;
367     }
368 
postLayerBuffer(const sp<SurfaceControl> & layer)369     void postLayerBuffer(const sp<SurfaceControl>& layer) {
370         ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
371 
372         // wait for the newly posted buffer to be latched
373         waitForLayerBuffers();
374     }
375 
postBufferQueueLayerBuffer(const sp<SurfaceControl> & layer)376     void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
377         ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
378 
379         // wait for the newly posted buffer to be latched
380         waitForLayerBuffers();
381     }
382 
fillBufferQueueLayerColor(const sp<SurfaceControl> & layer,const Color & color,int32_t bufferWidth,int32_t bufferHeight)383     virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
384                                            int32_t bufferWidth, int32_t bufferHeight) {
385         ANativeWindow_Buffer buffer;
386         ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
387         fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
388         postBufferQueueLayerBuffer(layer);
389     }
390 
fillLayerColor(const sp<SurfaceControl> & layer,const Color & color)391     void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color) {
392         ANativeWindow_Buffer buffer;
393         ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
394         fillBufferColor(buffer, Rect(0, 0, buffer.width, buffer.height), color);
395         postLayerBuffer(layer);
396     }
397 
fillLayerQuadrant(const sp<SurfaceControl> & layer,const Color & topLeft,const Color & topRight,const Color & bottomLeft,const Color & bottomRight)398     void fillLayerQuadrant(const sp<SurfaceControl>& layer, const Color& topLeft,
399                            const Color& topRight, const Color& bottomLeft,
400                            const Color& bottomRight) {
401         ANativeWindow_Buffer buffer;
402         ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
403         ASSERT_TRUE(buffer.width % 2 == 0 && buffer.height % 2 == 0);
404 
405         const int32_t halfW = buffer.width / 2;
406         const int32_t halfH = buffer.height / 2;
407         fillBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
408         fillBufferColor(buffer, Rect(halfW, 0, buffer.width, halfH), topRight);
409         fillBufferColor(buffer, Rect(0, halfH, halfW, buffer.height), bottomLeft);
410         fillBufferColor(buffer, Rect(halfW, halfH, buffer.width, buffer.height), bottomRight);
411 
412         postLayerBuffer(layer);
413     }
414 
screenshot()415     sp<ScreenCapture> screenshot() {
416         sp<ScreenCapture> screenshot;
417         ScreenCapture::captureScreen(&screenshot, mLayerZBase);
418         return screenshot;
419     }
420 
421     sp<SurfaceComposerClient> mClient;
422 
423     sp<IBinder> mDisplay;
424     uint32_t mDisplayWidth;
425     uint32_t mDisplayHeight;
426     uint32_t mDisplayLayerStack;
427 
428     // leave room for ~256 layers
429     const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
430 
431 private:
SetUpDisplay()432     void SetUpDisplay() {
433         mDisplay = mClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
434         ASSERT_NE(nullptr, mDisplay.get()) << "failed to get built-in display";
435 
436         // get display width/height
437         DisplayInfo info;
438         SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
439         mDisplayWidth = info.w;
440         mDisplayHeight = info.h;
441 
442         // After a new buffer is queued, SurfaceFlinger is notified and will
443         // latch the new buffer on next vsync.  Let's heuristically wait for 3
444         // vsyncs.
445         mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
446 
447         mDisplayLayerStack = 0;
448         // set layer stack (b/68888219)
449         Transaction t;
450         t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
451         t.apply();
452     }
453 
waitForLayerBuffers()454     void waitForLayerBuffers() { usleep(mBufferPostDelay); }
455 
456     int32_t mBufferPostDelay;
457 };
458 
TEST_F(LayerTransactionTest,SetPositionBasic)459 TEST_F(LayerTransactionTest, SetPositionBasic) {
460     sp<SurfaceControl> layer;
461     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
462     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
463 
464     {
465         SCOPED_TRACE("default position");
466         auto shot = screenshot();
467         shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
468         shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
469     }
470 
471     Transaction().setPosition(layer, 5, 10).apply();
472     {
473         SCOPED_TRACE("new position");
474         auto shot = screenshot();
475         shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
476         shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
477     }
478 }
479 
TEST_F(LayerTransactionTest,SetPositionRounding)480 TEST_F(LayerTransactionTest, SetPositionRounding) {
481     sp<SurfaceControl> layer;
482     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
483     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
484 
485     // GLES requires only 4 bits of subpixel precision during rasterization
486     // XXX GLES composition does not match HWC composition due to precision
487     // loss (b/69315223)
488     const float epsilon = 1.0f / 16.0f;
489     Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
490     {
491         SCOPED_TRACE("rounding down");
492         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
493     }
494 
495     Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
496     {
497         SCOPED_TRACE("rounding up");
498         screenshot()->expectColor(Rect(1, 1, 33, 33), Color::RED);
499     }
500 }
501 
TEST_F(LayerTransactionTest,SetPositionOutOfBounds)502 TEST_F(LayerTransactionTest, SetPositionOutOfBounds) {
503     sp<SurfaceControl> layer;
504     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
505     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
506 
507     Transaction().setPosition(layer, -32, -32).apply();
508     {
509         SCOPED_TRACE("negative coordinates");
510         screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
511     }
512 
513     Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
514     {
515         SCOPED_TRACE("positive coordinates");
516         screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
517     }
518 }
519 
TEST_F(LayerTransactionTest,SetPositionPartiallyOutOfBounds)520 TEST_F(LayerTransactionTest, SetPositionPartiallyOutOfBounds) {
521     sp<SurfaceControl> layer;
522     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
523     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
524 
525     // partially out of bounds
526     Transaction().setPosition(layer, -30, -30).apply();
527     {
528         SCOPED_TRACE("negative coordinates");
529         screenshot()->expectColor(Rect(0, 0, 2, 2), Color::RED);
530     }
531 
532     Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
533     {
534         SCOPED_TRACE("positive coordinates");
535         screenshot()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
536                                        mDisplayHeight),
537                                   Color::RED);
538     }
539 }
540 
TEST_F(LayerTransactionTest,SetPositionWithResize)541 TEST_F(LayerTransactionTest, SetPositionWithResize) {
542     sp<SurfaceControl> layer;
543     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
544     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
545 
546     // setPosition is applied immediately by default, with or without resize
547     // pending
548     Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
549     {
550         SCOPED_TRACE("resize pending");
551         auto shot = screenshot();
552         shot->expectColor(Rect(5, 10, 37, 42), Color::RED);
553         shot->expectBorder(Rect(5, 10, 37, 42), Color::BLACK);
554     }
555 
556     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
557     {
558         SCOPED_TRACE("resize applied");
559         screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
560     }
561 }
562 
TEST_F(LayerTransactionTest,SetPositionWithNextResize)563 TEST_F(LayerTransactionTest, SetPositionWithNextResize) {
564     sp<SurfaceControl> layer;
565     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
566     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
567 
568     // request setPosition to be applied with the next resize
569     Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
570     {
571         SCOPED_TRACE("new position pending");
572         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
573     }
574 
575     Transaction().setPosition(layer, 15, 20).apply();
576     {
577         SCOPED_TRACE("pending new position modified");
578         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
579     }
580 
581     Transaction().setSize(layer, 64, 64).apply();
582     {
583         SCOPED_TRACE("resize pending");
584         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
585     }
586 
587     // finally resize and latch the buffer
588     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
589     {
590         SCOPED_TRACE("new position applied");
591         screenshot()->expectColor(Rect(15, 20, 79, 84), Color::RED);
592     }
593 }
594 
TEST_F(LayerTransactionTest,SetPositionWithNextResizeScaleToWindow)595 TEST_F(LayerTransactionTest, SetPositionWithNextResizeScaleToWindow) {
596     sp<SurfaceControl> layer;
597     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
598     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
599 
600     // setPosition is not immediate even with SCALE_TO_WINDOW override
601     Transaction()
602             .setPosition(layer, 5, 10)
603             .setSize(layer, 64, 64)
604             .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
605             .setGeometryAppliesWithResize(layer)
606             .apply();
607     {
608         SCOPED_TRACE("new position pending");
609         screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
610     }
611 
612     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
613     {
614         SCOPED_TRACE("new position applied");
615         screenshot()->expectColor(Rect(5, 10, 69, 74), Color::RED);
616     }
617 }
618 
TEST_F(LayerTransactionTest,SetSizeBasic)619 TEST_F(LayerTransactionTest, SetSizeBasic) {
620     sp<SurfaceControl> layer;
621     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
622     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
623 
624     Transaction().setSize(layer, 64, 64).apply();
625     {
626         SCOPED_TRACE("resize pending");
627         auto shot = screenshot();
628         shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
629         shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
630     }
631 
632     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
633     {
634         SCOPED_TRACE("resize applied");
635         auto shot = screenshot();
636         shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
637         shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
638     }
639 }
640 
TEST_F(LayerTransactionTest,SetSizeInvalid)641 TEST_F(LayerTransactionTest, SetSizeInvalid) {
642     // cannot test robustness against invalid sizes (zero or really huge)
643 }
644 
TEST_F(LayerTransactionTest,SetSizeWithScaleToWindow)645 TEST_F(LayerTransactionTest, SetSizeWithScaleToWindow) {
646     sp<SurfaceControl> layer;
647     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
648     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
649 
650     // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
651     Transaction()
652             .setSize(layer, 64, 64)
653             .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
654             .apply();
655     screenshot()->expectColor(Rect(0, 0, 64, 64), Color::RED);
656 }
657 
TEST_F(LayerTransactionTest,SetZBasic)658 TEST_F(LayerTransactionTest, SetZBasic) {
659     sp<SurfaceControl> layerR;
660     sp<SurfaceControl> layerG;
661     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
662     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
663     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
664     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
665 
666     Transaction().setLayer(layerR, mLayerZBase + 1).apply();
667     {
668         SCOPED_TRACE("layerR");
669         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
670     }
671 
672     Transaction().setLayer(layerG, mLayerZBase + 2).apply();
673     {
674         SCOPED_TRACE("layerG");
675         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
676     }
677 }
678 
TEST_F(LayerTransactionTest,SetZNegative)679 TEST_F(LayerTransactionTest, SetZNegative) {
680     sp<SurfaceControl> layerR;
681     sp<SurfaceControl> layerG;
682     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
683     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
684     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
685     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
686 
687     Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
688     {
689         SCOPED_TRACE("layerR");
690         sp<ScreenCapture> screenshot;
691         ScreenCapture::captureScreen(&screenshot, -2, -1);
692         screenshot->expectColor(Rect(0, 0, 32, 32), Color::RED);
693     }
694 
695     Transaction().setLayer(layerR, -3).apply();
696     {
697         SCOPED_TRACE("layerG");
698         sp<ScreenCapture> screenshot;
699         ScreenCapture::captureScreen(&screenshot, -3, -1);
700         screenshot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
701     }
702 }
703 
TEST_F(LayerTransactionTest,SetRelativeZBasic)704 TEST_F(LayerTransactionTest, SetRelativeZBasic) {
705     sp<SurfaceControl> layerR;
706     sp<SurfaceControl> layerG;
707     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
708     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
709     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
710     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
711 
712     Transaction()
713             .setPosition(layerG, 16, 16)
714             .setRelativeLayer(layerG, layerR->getHandle(), 1)
715             .apply();
716     {
717         SCOPED_TRACE("layerG above");
718         auto shot = screenshot();
719         shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
720         shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
721     }
722 
723     Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
724     {
725         SCOPED_TRACE("layerG below");
726         auto shot = screenshot();
727         shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
728         shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
729     }
730 }
731 
TEST_F(LayerTransactionTest,SetRelativeZNegative)732 TEST_F(LayerTransactionTest, SetRelativeZNegative) {
733     sp<SurfaceControl> layerR;
734     sp<SurfaceControl> layerG;
735     sp<SurfaceControl> layerB;
736     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
737     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
738     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
739     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
740     ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
741     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
742 
743     // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
744     Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
745 
746     sp<ScreenCapture> screenshot;
747     // only layerB is in this range
748     ScreenCapture::captureScreen(&screenshot, -2, -1);
749     screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
750 }
751 
TEST_F(LayerTransactionTest,SetRelativeZGroup)752 TEST_F(LayerTransactionTest, SetRelativeZGroup) {
753     sp<SurfaceControl> layerR;
754     sp<SurfaceControl> layerG;
755     sp<SurfaceControl> layerB;
756     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
757     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
758     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
759     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
760     ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
761     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE));
762 
763     // layerR = 0, layerG = layerR + 3, layerB = 2
764     Transaction()
765             .setPosition(layerG, 8, 8)
766             .setRelativeLayer(layerG, layerR->getHandle(), 3)
767             .setPosition(layerB, 16, 16)
768             .setLayer(layerB, mLayerZBase + 2)
769             .apply();
770     {
771         SCOPED_TRACE("(layerR < layerG) < layerB");
772         auto shot = screenshot();
773         shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
774         shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
775         shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
776     }
777 
778     // layerR = 4, layerG = layerR + 3, layerB = 2
779     Transaction().setLayer(layerR, mLayerZBase + 4).apply();
780     {
781         SCOPED_TRACE("layerB < (layerR < layerG)");
782         auto shot = screenshot();
783         shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
784         shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
785         shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
786     }
787 
788     // layerR = 4, layerG = layerR - 3, layerB = 2
789     Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
790     {
791         SCOPED_TRACE("layerB < (layerG < layerR)");
792         auto shot = screenshot();
793         shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
794         shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
795         shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
796     }
797 
798     // restore to absolute z
799     // layerR = 4, layerG = 0, layerB = 2
800     Transaction().setLayer(layerG, mLayerZBase).apply();
801     {
802         SCOPED_TRACE("layerG < layerB < layerR");
803         auto shot = screenshot();
804         shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
805         shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
806     }
807 
808     // layerR should not affect layerG anymore
809     // layerR = 1, layerG = 0, layerB = 2
810     Transaction().setLayer(layerR, mLayerZBase + 1).apply();
811     {
812         SCOPED_TRACE("layerG < layerR < layerB");
813         auto shot = screenshot();
814         shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
815         shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
816     }
817 }
818 
TEST_F(LayerTransactionTest,SetRelativeZBug64572777)819 TEST_F(LayerTransactionTest, SetRelativeZBug64572777) {
820     sp<SurfaceControl> layerR;
821     sp<SurfaceControl> layerG;
822 
823     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
824     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
825     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
826     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
827 
828     Transaction()
829             .setPosition(layerG, 16, 16)
830             .setRelativeLayer(layerG, layerR->getHandle(), 1)
831             .apply();
832 
833     mClient->destroySurface(layerG->getHandle());
834     // layerG should have been removed
835     screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
836 }
837 
TEST_F(LayerTransactionTest,SetFlagsHidden)838 TEST_F(LayerTransactionTest, SetFlagsHidden) {
839     sp<SurfaceControl> layer;
840     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
841     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
842 
843     Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
844     {
845         SCOPED_TRACE("layer hidden");
846         screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
847     }
848 
849     Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
850     {
851         SCOPED_TRACE("layer shown");
852         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
853     }
854 }
855 
TEST_F(LayerTransactionTest,SetFlagsOpaque)856 TEST_F(LayerTransactionTest, SetFlagsOpaque) {
857     const Color translucentRed = {100, 0, 0, 100};
858     sp<SurfaceControl> layerR;
859     sp<SurfaceControl> layerG;
860     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
861     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed));
862     ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
863     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN));
864 
865     Transaction()
866             .setLayer(layerR, mLayerZBase + 1)
867             .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
868             .apply();
869     {
870         SCOPED_TRACE("layerR opaque");
871         screenshot()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
872     }
873 
874     Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
875     {
876         SCOPED_TRACE("layerR translucent");
877         const uint8_t g = uint8_t(255 - translucentRed.a);
878         screenshot()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
879     }
880 }
881 
TEST_F(LayerTransactionTest,SetFlagsSecure)882 TEST_F(LayerTransactionTest, SetFlagsSecure) {
883     sp<SurfaceControl> layer;
884     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
885     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
886 
887     sp<ISurfaceComposer> composer = ComposerService::getComposerService();
888     sp<GraphicBuffer> outBuffer;
889     Transaction()
890             .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
891             .apply(true);
892     ASSERT_EQ(PERMISSION_DENIED,
893               composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
894                                       false));
895 
896     Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
897     ASSERT_EQ(NO_ERROR,
898               composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, mLayerZBase, mLayerZBase,
899                                       false));
900 }
901 
902 /** RAII Wrapper around get/seteuid */
903 class UIDFaker {
904     uid_t oldId;
905 public:
UIDFaker(uid_t uid)906     UIDFaker(uid_t uid) {
907         oldId = geteuid();
908         seteuid(uid);
909     }
~UIDFaker()910     ~UIDFaker() {
911         seteuid(oldId);
912     }
913 };
914 
TEST_F(LayerTransactionTest,SetFlagsSecureEUidSystem)915 TEST_F(LayerTransactionTest, SetFlagsSecureEUidSystem) {
916     sp<SurfaceControl> layer;
917     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
918     ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
919 
920     sp<ISurfaceComposer> composer = ComposerService::getComposerService();
921     sp<GraphicBuffer> outBuffer;
922     Transaction()
923             .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
924             .apply(true);
925 
926     ASSERT_EQ(PERMISSION_DENIED,
927               composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, 0, INT_MAX, false));
928 
929     UIDFaker f(AID_SYSTEM);
930 
931     // By default the system can capture screenshots with secure layers but they
932     // will be blacked out
933     ASSERT_EQ(NO_ERROR,
934               composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, 0, INT_MAX, false));
935 
936     {
937         SCOPED_TRACE("as system");
938         auto shot = screenshot();
939         shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
940     }
941 
942     // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
943     // to receive them...we are expected to take care with the results.
944     bool outCapturedSecureLayers = false;
945     ASSERT_EQ(NO_ERROR,
946               composer->captureScreen(mDisplay, &outBuffer, outCapturedSecureLayers,
947                       Rect(), 0, 0, 0, INT_MAX, false, ISurfaceComposer::eRotateNone, true));
948     ASSERT_EQ(true, outCapturedSecureLayers);
949     ScreenCapture sc(outBuffer);
950     sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
951 }
952 
TEST_F(LayerTransactionTest,SetTransparentRegionHintBasic)953 TEST_F(LayerTransactionTest, SetTransparentRegionHintBasic) {
954     const Rect top(0, 0, 32, 16);
955     const Rect bottom(0, 16, 32, 32);
956     sp<SurfaceControl> layer;
957     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
958 
959     ANativeWindow_Buffer buffer;
960     ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
961     ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::TRANSPARENT));
962     ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::RED));
963     // setTransparentRegionHint always applies to the following buffer
964     Transaction().setTransparentRegionHint(layer, Region(top)).apply();
965     ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
966     {
967         SCOPED_TRACE("top transparent");
968         auto shot = screenshot();
969         shot->expectColor(top, Color::BLACK);
970         shot->expectColor(bottom, Color::RED);
971     }
972 
973     Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
974     {
975         SCOPED_TRACE("transparent region hint pending");
976         auto shot = screenshot();
977         shot->expectColor(top, Color::BLACK);
978         shot->expectColor(bottom, Color::RED);
979     }
980 
981     ASSERT_NO_FATAL_FAILURE(buffer = getLayerBuffer(layer));
982     ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, top, Color::RED));
983     ASSERT_NO_FATAL_FAILURE(fillBufferColor(buffer, bottom, Color::TRANSPARENT));
984     ASSERT_NO_FATAL_FAILURE(postLayerBuffer(layer));
985     {
986         SCOPED_TRACE("bottom transparent");
987         auto shot = screenshot();
988         shot->expectColor(top, Color::RED);
989         shot->expectColor(bottom, Color::BLACK);
990     }
991 }
992 
TEST_F(LayerTransactionTest,SetTransparentRegionHintOutOfBounds)993 TEST_F(LayerTransactionTest, SetTransparentRegionHintOutOfBounds) {
994     sp<SurfaceControl> layerTransparent;
995     sp<SurfaceControl> layerR;
996     ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
997     ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
998 
999     // check that transparent region hint is bound by the layer size
1000     Transaction()
1001             .setTransparentRegionHint(layerTransparent,
1002                                       Region(Rect(0, 0, mDisplayWidth, mDisplayHeight)))
1003             .setPosition(layerR, 16, 16)
1004             .setLayer(layerR, mLayerZBase + 1)
1005             .apply();
1006     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerTransparent, Color::TRANSPARENT));
1007     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED));
1008     screenshot()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1009 }
1010 
TEST_F(LayerTransactionTest,SetAlphaBasic)1011 TEST_F(LayerTransactionTest, SetAlphaBasic) {
1012     sp<SurfaceControl> layer1;
1013     sp<SurfaceControl> layer2;
1014     ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32));
1015     ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32));
1016     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer1, {64, 0, 0, 255}));
1017     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer2, {0, 64, 0, 255}));
1018 
1019     Transaction()
1020             .setAlpha(layer1, 0.25f)
1021             .setAlpha(layer2, 0.75f)
1022             .setPosition(layer2, 16, 0)
1023             .setLayer(layer2, mLayerZBase + 1)
1024             .apply();
1025     {
1026         auto shot = screenshot();
1027         uint8_t r = 16; // 64 * 0.25f
1028         uint8_t g = 48; // 64 * 0.75f
1029         shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1030         shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1031 
1032         r /= 4; // r * (1.0f - 0.75f)
1033         shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1034     }
1035 }
1036 
TEST_F(LayerTransactionTest,SetAlphaClamped)1037 TEST_F(LayerTransactionTest, SetAlphaClamped) {
1038     const Color color = {64, 0, 0, 255};
1039     sp<SurfaceControl> layer;
1040     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1041     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color));
1042 
1043     Transaction().setAlpha(layer, 2.0f).apply();
1044     {
1045         SCOPED_TRACE("clamped to 1.0f");
1046         screenshot()->expectColor(Rect(0, 0, 32, 32), color);
1047     }
1048 
1049     Transaction().setAlpha(layer, -1.0f).apply();
1050     {
1051         SCOPED_TRACE("clamped to 0.0f");
1052         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1053     }
1054 }
1055 
TEST_F(LayerTransactionTest,SetColorBasic)1056 TEST_F(LayerTransactionTest, SetColorBasic) {
1057     sp<SurfaceControl> bufferLayer;
1058     sp<SurfaceControl> colorLayer;
1059     ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1060     ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1061     ASSERT_NO_FATAL_FAILURE(
1062             colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1063 
1064     Transaction().setLayer(colorLayer, mLayerZBase + 1).apply();
1065     {
1066         SCOPED_TRACE("default color");
1067         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1068     }
1069 
1070     const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1071     const Color expected = {15, 51, 85, 255};
1072     // this is handwavy, but the precison loss scaled by 255 (8-bit per
1073     // channel) should be less than one
1074     const uint8_t tolerance = 1;
1075     Transaction().setColor(colorLayer, color).apply();
1076     {
1077         SCOPED_TRACE("new color");
1078         screenshot()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1079     }
1080 }
1081 
TEST_F(LayerTransactionTest,SetColorClamped)1082 TEST_F(LayerTransactionTest, SetColorClamped) {
1083     sp<SurfaceControl> colorLayer;
1084     ASSERT_NO_FATAL_FAILURE(
1085             colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1086 
1087     Transaction().setColor(colorLayer, half3(2.0f, -1.0f, 0.0f)).apply();
1088     screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1089 }
1090 
TEST_F(LayerTransactionTest,SetColorWithAlpha)1091 TEST_F(LayerTransactionTest, SetColorWithAlpha) {
1092     sp<SurfaceControl> bufferLayer;
1093     sp<SurfaceControl> colorLayer;
1094     ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1095     ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1096     ASSERT_NO_FATAL_FAILURE(
1097             colorLayer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1098 
1099     const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1100     const float alpha = 0.25f;
1101     const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1102     // this is handwavy, but the precison loss scaled by 255 (8-bit per
1103     // channel) should be less than one
1104     const uint8_t tolerance = 1;
1105     Transaction()
1106             .setColor(colorLayer, color)
1107             .setAlpha(colorLayer, alpha)
1108             .setLayer(colorLayer, mLayerZBase + 1)
1109             .apply();
1110     screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1111                               tolerance);
1112 }
1113 
TEST_F(LayerTransactionTest,SetColorWithParentAlpha_Bug74220420)1114 TEST_F(LayerTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1115     sp<SurfaceControl> bufferLayer;
1116     sp<SurfaceControl> parentLayer;
1117     sp<SurfaceControl> colorLayer;
1118     ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1119     ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
1120     ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1121     ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer(
1122             "childWithColor", 32, 32, ISurfaceComposerClient::eFXSurfaceColor));
1123 
1124     const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1125     const float alpha = 0.25f;
1126     const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1127     // this is handwavy, but the precision loss scaled by 255 (8-bit per
1128     // channel) should be less than one
1129     const uint8_t tolerance = 1;
1130     Transaction()
1131             .reparent(colorLayer, parentLayer->getHandle())
1132             .setColor(colorLayer, color)
1133             .setAlpha(parentLayer, alpha)
1134             .setLayer(parentLayer, mLayerZBase + 1)
1135             .apply();
1136     screenshot()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1137                               tolerance);
1138 }
1139 
TEST_F(LayerTransactionTest,SetColorWithBuffer)1140 TEST_F(LayerTransactionTest, SetColorWithBuffer) {
1141     sp<SurfaceControl> bufferLayer;
1142     ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1143     ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED));
1144 
1145     // color is ignored
1146     Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1147     screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1148 }
1149 
TEST_F(LayerTransactionTest,SetLayerStackBasic)1150 TEST_F(LayerTransactionTest, SetLayerStackBasic) {
1151     sp<SurfaceControl> layer;
1152     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1153     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1154 
1155     Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1156     {
1157         SCOPED_TRACE("non-existing layer stack");
1158         screenshot()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
1159     }
1160 
1161     Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1162     {
1163         SCOPED_TRACE("original layer stack");
1164         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1165     }
1166 }
1167 
TEST_F(LayerTransactionTest,SetMatrixBasic)1168 TEST_F(LayerTransactionTest, SetMatrixBasic) {
1169     sp<SurfaceControl> layer;
1170     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1171     ASSERT_NO_FATAL_FAILURE(
1172             fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1173 
1174     Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1175     {
1176         SCOPED_TRACE("IDENTITY");
1177         screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN, Color::BLUE,
1178                                      Color::WHITE);
1179     }
1180 
1181     Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1182     {
1183         SCOPED_TRACE("FLIP_H");
1184         screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
1185                                      Color::BLUE);
1186     }
1187 
1188     Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1189     {
1190         SCOPED_TRACE("FLIP_V");
1191         screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
1192                                      Color::GREEN);
1193     }
1194 
1195     Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1196     {
1197         SCOPED_TRACE("ROT_90");
1198         screenshot()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
1199                                      Color::GREEN);
1200     }
1201 
1202     Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1203     {
1204         SCOPED_TRACE("SCALE");
1205         screenshot()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN, Color::BLUE,
1206                                      Color::WHITE, true /* filtered */);
1207     }
1208 }
1209 
TEST_F(LayerTransactionTest,SetMatrixRot45)1210 TEST_F(LayerTransactionTest, SetMatrixRot45) {
1211     sp<SurfaceControl> layer;
1212     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1213     ASSERT_NO_FATAL_FAILURE(
1214             fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1215 
1216     const float rot = M_SQRT1_2; // 45 degrees
1217     const float trans = M_SQRT2 * 16.0f;
1218     Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1219 
1220     auto shot = screenshot();
1221     // check a 8x8 region inside each color
1222     auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1223         const int32_t halfL = 4;
1224         return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1225     };
1226     const int32_t unit = int32_t(trans / 2);
1227     shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1228     shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1229     shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1230     shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1231 }
1232 
TEST_F(LayerTransactionTest,SetMatrixWithResize)1233 TEST_F(LayerTransactionTest, SetMatrixWithResize) {
1234     sp<SurfaceControl> layer;
1235     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1236     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1237 
1238     // setMatrix is applied after any pending resize, unlike setPosition
1239     Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1240     {
1241         SCOPED_TRACE("resize pending");
1242         auto shot = screenshot();
1243         shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1244         shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1245     }
1246 
1247     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1248     {
1249         SCOPED_TRACE("resize applied");
1250         screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1251     }
1252 }
1253 
TEST_F(LayerTransactionTest,SetMatrixWithScaleToWindow)1254 TEST_F(LayerTransactionTest, SetMatrixWithScaleToWindow) {
1255     sp<SurfaceControl> layer;
1256     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1257     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1258 
1259     // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1260     Transaction()
1261             .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1262             .setSize(layer, 64, 64)
1263             .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1264             .apply();
1265     screenshot()->expectColor(Rect(0, 0, 128, 128), Color::RED);
1266 }
1267 
TEST_F(LayerTransactionTest,SetOverrideScalingModeBasic)1268 TEST_F(LayerTransactionTest, SetOverrideScalingModeBasic) {
1269     sp<SurfaceControl> layer;
1270     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1271     ASSERT_NO_FATAL_FAILURE(
1272             fillLayerQuadrant(layer, Color::RED, Color::GREEN, Color::BLUE, Color::WHITE));
1273 
1274     // XXX SCALE_CROP is not respected; calling setSize and
1275     // setOverrideScalingMode in separate transactions does not work
1276     // (b/69315456)
1277     Transaction()
1278             .setSize(layer, 64, 16)
1279             .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1280             .apply();
1281     {
1282         SCOPED_TRACE("SCALE_TO_WINDOW");
1283         screenshot()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN, Color::BLUE,
1284                                      Color::WHITE, true /* filtered */);
1285     }
1286 }
1287 
TEST_F(LayerTransactionTest,SetCropBasic)1288 TEST_F(LayerTransactionTest, SetCropBasic) {
1289     sp<SurfaceControl> layer;
1290     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1291     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1292     const Rect crop(8, 8, 24, 24);
1293 
1294     Transaction().setCrop(layer, crop).apply();
1295     auto shot = screenshot();
1296     shot->expectColor(crop, Color::RED);
1297     shot->expectBorder(crop, Color::BLACK);
1298 }
1299 
TEST_F(LayerTransactionTest,SetCropEmpty)1300 TEST_F(LayerTransactionTest, SetCropEmpty) {
1301     sp<SurfaceControl> layer;
1302     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1303     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1304 
1305     {
1306         SCOPED_TRACE("empty rect");
1307         Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
1308         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1309     }
1310 
1311     {
1312         SCOPED_TRACE("negative rect");
1313         Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
1314         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1315     }
1316 }
1317 
TEST_F(LayerTransactionTest,SetCropOutOfBounds)1318 TEST_F(LayerTransactionTest, SetCropOutOfBounds) {
1319     sp<SurfaceControl> layer;
1320     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1321     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1322 
1323     Transaction().setCrop(layer, Rect(-128, -64, 128, 64)).apply();
1324     auto shot = screenshot();
1325     shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1326     shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1327 }
1328 
TEST_F(LayerTransactionTest,SetCropWithTranslation)1329 TEST_F(LayerTransactionTest, SetCropWithTranslation) {
1330     sp<SurfaceControl> layer;
1331     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1332     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1333 
1334     const Point position(32, 32);
1335     const Rect crop(8, 8, 24, 24);
1336     Transaction().setPosition(layer, position.x, position.y).setCrop(layer, crop).apply();
1337     auto shot = screenshot();
1338     shot->expectColor(crop + position, Color::RED);
1339     shot->expectBorder(crop + position, Color::BLACK);
1340 }
1341 
TEST_F(LayerTransactionTest,SetCropWithScale)1342 TEST_F(LayerTransactionTest, SetCropWithScale) {
1343     sp<SurfaceControl> layer;
1344     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1345     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1346 
1347     // crop is affected by matrix
1348     Transaction()
1349             .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1350             .setCrop(layer, Rect(8, 8, 24, 24))
1351             .apply();
1352     auto shot = screenshot();
1353     shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1354     shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1355 }
1356 
TEST_F(LayerTransactionTest,SetCropWithResize)1357 TEST_F(LayerTransactionTest, SetCropWithResize) {
1358     sp<SurfaceControl> layer;
1359     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1360     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1361 
1362     // setCrop is applied immediately by default, with or without resize pending
1363     Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1364     {
1365         SCOPED_TRACE("resize pending");
1366         auto shot = screenshot();
1367         shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1368         shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1369     }
1370 
1371     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1372     {
1373         SCOPED_TRACE("resize applied");
1374         auto shot = screenshot();
1375         shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1376         shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1377     }
1378 }
1379 
TEST_F(LayerTransactionTest,SetCropWithNextResize)1380 TEST_F(LayerTransactionTest, SetCropWithNextResize) {
1381     sp<SurfaceControl> layer;
1382     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1383     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1384 
1385     // request setCrop to be applied with the next resize
1386     Transaction().setCrop(layer, Rect(8, 8, 24, 24)).setGeometryAppliesWithResize(layer).apply();
1387     {
1388         SCOPED_TRACE("waiting for next resize");
1389         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1390     }
1391 
1392     Transaction().setCrop(layer, Rect(4, 4, 12, 12)).apply();
1393     {
1394         SCOPED_TRACE("pending crop modified");
1395         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1396     }
1397 
1398     Transaction().setSize(layer, 16, 16).apply();
1399     {
1400         SCOPED_TRACE("resize pending");
1401         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1402     }
1403 
1404     // finally resize
1405     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1406     {
1407         SCOPED_TRACE("new crop applied");
1408         auto shot = screenshot();
1409         shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1410         shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1411     }
1412 }
1413 
TEST_F(LayerTransactionTest,SetCropWithNextResizeScaleToWindow)1414 TEST_F(LayerTransactionTest, SetCropWithNextResizeScaleToWindow) {
1415     sp<SurfaceControl> layer;
1416     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1417     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1418 
1419     // setCrop is not immediate even with SCALE_TO_WINDOW override
1420     Transaction()
1421             .setCrop(layer, Rect(4, 4, 12, 12))
1422             .setSize(layer, 16, 16)
1423             .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1424             .setGeometryAppliesWithResize(layer)
1425             .apply();
1426     {
1427         SCOPED_TRACE("new crop pending");
1428         auto shot = screenshot();
1429         shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1430         shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1431     }
1432 
1433     // XXX crop is never latched without other geometry change (b/69315677)
1434     Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1435     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1436     Transaction().setPosition(layer, 0, 0).apply();
1437     {
1438         SCOPED_TRACE("new crop applied");
1439         auto shot = screenshot();
1440         shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1441         shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1442     }
1443 }
1444 
TEST_F(LayerTransactionTest,SetFinalCropBasic)1445 TEST_F(LayerTransactionTest, SetFinalCropBasic) {
1446     sp<SurfaceControl> layer;
1447     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1448     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1449     const Rect crop(8, 8, 24, 24);
1450 
1451     // same as in SetCropBasic
1452     Transaction().setFinalCrop(layer, crop).apply();
1453     auto shot = screenshot();
1454     shot->expectColor(crop, Color::RED);
1455     shot->expectBorder(crop, Color::BLACK);
1456 }
1457 
TEST_F(LayerTransactionTest,SetFinalCropEmpty)1458 TEST_F(LayerTransactionTest, SetFinalCropEmpty) {
1459     sp<SurfaceControl> layer;
1460     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1461     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1462 
1463     // same as in SetCropEmpty
1464     {
1465         SCOPED_TRACE("empty rect");
1466         Transaction().setFinalCrop(layer, Rect(8, 8, 8, 8)).apply();
1467         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1468     }
1469 
1470     {
1471         SCOPED_TRACE("negative rect");
1472         Transaction().setFinalCrop(layer, Rect(8, 8, 0, 0)).apply();
1473         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1474     }
1475 }
1476 
TEST_F(LayerTransactionTest,SetFinalCropOutOfBounds)1477 TEST_F(LayerTransactionTest, SetFinalCropOutOfBounds) {
1478     sp<SurfaceControl> layer;
1479     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1480     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1481 
1482     // same as in SetCropOutOfBounds
1483     Transaction().setFinalCrop(layer, Rect(-128, -64, 128, 64)).apply();
1484     auto shot = screenshot();
1485     shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1486     shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1487 }
1488 
TEST_F(LayerTransactionTest,SetFinalCropWithTranslation)1489 TEST_F(LayerTransactionTest, SetFinalCropWithTranslation) {
1490     sp<SurfaceControl> layer;
1491     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1492     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1493 
1494     // final crop is applied post-translation
1495     Transaction().setPosition(layer, 16, 16).setFinalCrop(layer, Rect(8, 8, 24, 24)).apply();
1496     auto shot = screenshot();
1497     shot->expectColor(Rect(16, 16, 24, 24), Color::RED);
1498     shot->expectBorder(Rect(16, 16, 24, 24), Color::BLACK);
1499 }
1500 
TEST_F(LayerTransactionTest,SetFinalCropWithScale)1501 TEST_F(LayerTransactionTest, SetFinalCropWithScale) {
1502     sp<SurfaceControl> layer;
1503     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1504     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1505 
1506     // final crop is not affected by matrix
1507     Transaction()
1508             .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1509             .setFinalCrop(layer, Rect(8, 8, 24, 24))
1510             .apply();
1511     auto shot = screenshot();
1512     shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1513     shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1514 }
1515 
TEST_F(LayerTransactionTest,SetFinalCropWithResize)1516 TEST_F(LayerTransactionTest, SetFinalCropWithResize) {
1517     sp<SurfaceControl> layer;
1518     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1519     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1520 
1521     // same as in SetCropWithResize
1522     Transaction().setFinalCrop(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
1523     {
1524         SCOPED_TRACE("resize pending");
1525         auto shot = screenshot();
1526         shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1527         shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1528     }
1529 
1530     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1531     {
1532         SCOPED_TRACE("resize applied");
1533         auto shot = screenshot();
1534         shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1535         shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1536     }
1537 }
1538 
TEST_F(LayerTransactionTest,SetFinalCropWithNextResize)1539 TEST_F(LayerTransactionTest, SetFinalCropWithNextResize) {
1540     sp<SurfaceControl> layer;
1541     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1542     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1543 
1544     // same as in SetCropWithNextResize
1545     Transaction()
1546             .setFinalCrop(layer, Rect(8, 8, 24, 24))
1547             .setGeometryAppliesWithResize(layer)
1548             .apply();
1549     {
1550         SCOPED_TRACE("waiting for next resize");
1551         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1552     }
1553 
1554     Transaction().setFinalCrop(layer, Rect(4, 4, 12, 12)).apply();
1555     {
1556         SCOPED_TRACE("pending final crop modified");
1557         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1558     }
1559 
1560     Transaction().setSize(layer, 16, 16).apply();
1561     {
1562         SCOPED_TRACE("resize pending");
1563         screenshot()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1564     }
1565 
1566     // finally resize
1567     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1568     {
1569         SCOPED_TRACE("new final crop applied");
1570         auto shot = screenshot();
1571         shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1572         shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1573     }
1574 }
1575 
TEST_F(LayerTransactionTest,SetFinalCropWithNextResizeScaleToWindow)1576 TEST_F(LayerTransactionTest, SetFinalCropWithNextResizeScaleToWindow) {
1577     sp<SurfaceControl> layer;
1578     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1579     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1580 
1581     // same as in SetCropWithNextResizeScaleToWindow
1582     Transaction()
1583             .setFinalCrop(layer, Rect(4, 4, 12, 12))
1584             .setSize(layer, 16, 16)
1585             .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1586             .setGeometryAppliesWithResize(layer)
1587             .apply();
1588     {
1589         SCOPED_TRACE("new final crop pending");
1590         auto shot = screenshot();
1591         shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1592         shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
1593     }
1594 
1595     // XXX final crop is never latched without other geometry change (b/69315677)
1596     Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
1597     ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED));
1598     Transaction().setPosition(layer, 0, 0).apply();
1599     {
1600         SCOPED_TRACE("new final crop applied");
1601         auto shot = screenshot();
1602         shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
1603         shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
1604     }
1605 }
1606 
1607 class LayerUpdateTest : public LayerTransactionTest {
1608 protected:
SetUp()1609     virtual void SetUp() {
1610         mComposerClient = new SurfaceComposerClient;
1611         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
1612 
1613         sp<IBinder> display(
1614                 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
1615         DisplayInfo info;
1616         SurfaceComposerClient::getDisplayInfo(display, &info);
1617 
1618         ssize_t displayWidth = info.w;
1619         ssize_t displayHeight = info.h;
1620 
1621         // Background surface
1622         mBGSurfaceControl =
1623                 mComposerClient->createSurface(String8("BG Test Surface"), displayWidth,
1624                                                displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
1625         ASSERT_TRUE(mBGSurfaceControl != nullptr);
1626         ASSERT_TRUE(mBGSurfaceControl->isValid());
1627         fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
1628 
1629         // Foreground surface
1630         mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1631                                                            PIXEL_FORMAT_RGBA_8888, 0);
1632         ASSERT_TRUE(mFGSurfaceControl != nullptr);
1633         ASSERT_TRUE(mFGSurfaceControl->isValid());
1634 
1635         fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1636 
1637         // Synchronization surface
1638         mSyncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1639                                                              PIXEL_FORMAT_RGBA_8888, 0);
1640         ASSERT_TRUE(mSyncSurfaceControl != nullptr);
1641         ASSERT_TRUE(mSyncSurfaceControl->isValid());
1642 
1643         fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1644 
1645         asTransaction([&](Transaction& t) {
1646             t.setDisplayLayerStack(display, 0);
1647 
1648             t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
1649 
1650             t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
1651                     .setPosition(mFGSurfaceControl, 64, 64)
1652                     .show(mFGSurfaceControl);
1653 
1654             t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
1655                     .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
1656                     .show(mSyncSurfaceControl);
1657         });
1658     }
1659 
TearDown()1660     virtual void TearDown() {
1661         mComposerClient->dispose();
1662         mBGSurfaceControl = 0;
1663         mFGSurfaceControl = 0;
1664         mSyncSurfaceControl = 0;
1665         mComposerClient = 0;
1666     }
1667 
waitForPostedBuffers()1668     void waitForPostedBuffers() {
1669         // Since the sync surface is in synchronous mode (i.e. double buffered)
1670         // posting three buffers to it should ensure that at least two
1671         // SurfaceFlinger::handlePageFlip calls have been made, which should
1672         // guaranteed that a buffer posted to another Surface has been retired.
1673         fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1674         fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1675         fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1676     }
1677 
asTransaction(const std::function<void (Transaction &)> & exec)1678     void asTransaction(const std::function<void(Transaction&)>& exec) {
1679         Transaction t;
1680         exec(t);
1681         t.apply(true);
1682     }
1683 
1684     sp<SurfaceComposerClient> mComposerClient;
1685     sp<SurfaceControl> mBGSurfaceControl;
1686     sp<SurfaceControl> mFGSurfaceControl;
1687 
1688     // This surface is used to ensure that the buffers posted to
1689     // mFGSurfaceControl have been picked up by SurfaceFlinger.
1690     sp<SurfaceControl> mSyncSurfaceControl;
1691 };
1692 
TEST_F(LayerUpdateTest,RelativesAreNotDetached)1693 TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
1694     sp<ScreenCapture> sc;
1695 
1696     sp<SurfaceControl> relative = mComposerClient->createSurface(String8("relativeTestSurface"), 10,
1697                                                                  10, PIXEL_FORMAT_RGBA_8888, 0);
1698     fillSurfaceRGBA8(relative, 10, 10, 10);
1699     waitForPostedBuffers();
1700 
1701     Transaction{}
1702             .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
1703             .setPosition(relative, 64, 64)
1704             .apply();
1705 
1706     {
1707         // The relative should be on top of the FG control.
1708         ScreenCapture::captureScreen(&sc);
1709         sc->checkPixel(64, 64, 10, 10, 10);
1710     }
1711     Transaction{}.detachChildren(mFGSurfaceControl).apply();
1712 
1713     {
1714         // Nothing should change at this point.
1715         ScreenCapture::captureScreen(&sc);
1716         sc->checkPixel(64, 64, 10, 10, 10);
1717     }
1718 
1719     Transaction{}.hide(relative).apply();
1720 
1721     {
1722         // Ensure that the relative was actually hidden, rather than
1723         // being left in the detached but visible state.
1724         ScreenCapture::captureScreen(&sc);
1725         sc->expectFGColor(64, 64);
1726     }
1727 }
1728 
1729 class GeometryLatchingTest : public LayerUpdateTest {
1730 protected:
EXPECT_INITIAL_STATE(const char * trace)1731     void EXPECT_INITIAL_STATE(const char* trace) {
1732         SCOPED_TRACE(trace);
1733         ScreenCapture::captureScreen(&sc);
1734         // We find the leading edge of the FG surface.
1735         sc->expectFGColor(127, 127);
1736         sc->expectBGColor(128, 128);
1737     }
1738 
lockAndFillFGBuffer()1739     void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
1740 
unlockFGBuffer()1741     void unlockFGBuffer() {
1742         sp<Surface> s = mFGSurfaceControl->getSurface();
1743         ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1744         waitForPostedBuffers();
1745     }
1746 
completeFGResize()1747     void completeFGResize() {
1748         fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
1749         waitForPostedBuffers();
1750     }
restoreInitialState()1751     void restoreInitialState() {
1752         asTransaction([&](Transaction& t) {
1753             t.setSize(mFGSurfaceControl, 64, 64);
1754             t.setPosition(mFGSurfaceControl, 64, 64);
1755             t.setCrop(mFGSurfaceControl, Rect(0, 0, 64, 64));
1756             t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, -1, -1));
1757         });
1758 
1759         EXPECT_INITIAL_STATE("After restoring initial state");
1760     }
1761     sp<ScreenCapture> sc;
1762 };
1763 
1764 class CropLatchingTest : public GeometryLatchingTest {
1765 protected:
EXPECT_CROPPED_STATE(const char * trace)1766     void EXPECT_CROPPED_STATE(const char* trace) {
1767         SCOPED_TRACE(trace);
1768         ScreenCapture::captureScreen(&sc);
1769         // The edge should be moved back one pixel by our crop.
1770         sc->expectFGColor(126, 126);
1771         sc->expectBGColor(127, 127);
1772         sc->expectBGColor(128, 128);
1773     }
1774 
EXPECT_RESIZE_STATE(const char * trace)1775     void EXPECT_RESIZE_STATE(const char* trace) {
1776         SCOPED_TRACE(trace);
1777         ScreenCapture::captureScreen(&sc);
1778         // The FG is now resized too 128,128 at 64,64
1779         sc->expectFGColor(64, 64);
1780         sc->expectFGColor(191, 191);
1781         sc->expectBGColor(192, 192);
1782     }
1783 };
1784 
1785 // In this test we ensure that setGeometryAppliesWithResize actually demands
1786 // a buffer of the new size, and not just any size.
TEST_F(CropLatchingTest,FinalCropLatchingBufferOldSize)1787 TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) {
1788     EXPECT_INITIAL_STATE("before anything");
1789     // Normally the crop applies immediately even while a resize is pending.
1790     asTransaction([&](Transaction& t) {
1791         t.setSize(mFGSurfaceControl, 128, 128);
1792         t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1793     });
1794 
1795     EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)");
1796 
1797     restoreInitialState();
1798 
1799     // In order to prepare to submit a buffer at the wrong size, we acquire it prior to
1800     // initiating the resize.
1801     lockAndFillFGBuffer();
1802 
1803     asTransaction([&](Transaction& t) {
1804         t.setSize(mFGSurfaceControl, 128, 128);
1805         t.setGeometryAppliesWithResize(mFGSurfaceControl);
1806         t.setFinalCrop(mFGSurfaceControl, Rect(64, 64, 127, 127));
1807     });
1808 
1809     EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)");
1810 
1811     // We now submit our old buffer, at the old size, and ensure it doesn't
1812     // trigger geometry latching.
1813     unlockFGBuffer();
1814 
1815     EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)");
1816 
1817     completeFGResize();
1818 
1819     EXPECT_CROPPED_STATE("after the resize finishes");
1820 }
1821 
TEST_F(LayerUpdateTest,DeferredTransactionTest)1822 TEST_F(LayerUpdateTest, DeferredTransactionTest) {
1823     sp<ScreenCapture> sc;
1824     {
1825         SCOPED_TRACE("before anything");
1826         ScreenCapture::captureScreen(&sc);
1827         sc->expectBGColor(32, 32);
1828         sc->expectFGColor(96, 96);
1829         sc->expectBGColor(160, 160);
1830     }
1831 
1832     // set up two deferred transactions on different frames
1833     asTransaction([&](Transaction& t) {
1834         t.setAlpha(mFGSurfaceControl, 0.75);
1835         t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
1836                                 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
1837     });
1838 
1839     asTransaction([&](Transaction& t) {
1840         t.setPosition(mFGSurfaceControl, 128, 128);
1841         t.deferTransactionUntil(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
1842                                 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
1843     });
1844 
1845     {
1846         SCOPED_TRACE("before any trigger");
1847         ScreenCapture::captureScreen(&sc);
1848         sc->expectBGColor(32, 32);
1849         sc->expectFGColor(96, 96);
1850         sc->expectBGColor(160, 160);
1851     }
1852 
1853     // should trigger the first deferred transaction, but not the second one
1854     fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1855     {
1856         SCOPED_TRACE("after first trigger");
1857         ScreenCapture::captureScreen(&sc);
1858         sc->expectBGColor(32, 32);
1859         sc->checkPixel(96, 96, 162, 63, 96);
1860         sc->expectBGColor(160, 160);
1861     }
1862 
1863     // should show up immediately since it's not deferred
1864     asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
1865 
1866     // trigger the second deferred transaction
1867     fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
1868     {
1869         SCOPED_TRACE("after second trigger");
1870         ScreenCapture::captureScreen(&sc);
1871         sc->expectBGColor(32, 32);
1872         sc->expectBGColor(96, 96);
1873         sc->expectFGColor(160, 160);
1874     }
1875 }
1876 
TEST_F(LayerUpdateTest,LayerWithNoBuffersResizesImmediately)1877 TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
1878     sp<ScreenCapture> sc;
1879 
1880     sp<SurfaceControl> childNoBuffer =
1881             mComposerClient->createSurface(String8("Bufferless child"), 10, 10,
1882                                            PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1883     sp<SurfaceControl> childBuffer =
1884             mComposerClient->createSurface(String8("Buffered child"), 20, 20,
1885                                            PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
1886     fillSurfaceRGBA8(childBuffer, 200, 200, 200);
1887 
1888     SurfaceComposerClient::Transaction{}.show(childNoBuffer).show(childBuffer).apply(true);
1889 
1890     {
1891         ScreenCapture::captureScreen(&sc);
1892         sc->expectChildColor(73, 73);
1893         sc->expectFGColor(74, 74);
1894     }
1895 
1896     SurfaceComposerClient::Transaction{}.setSize(childNoBuffer, 20, 20).apply(true);
1897 
1898     {
1899         ScreenCapture::captureScreen(&sc);
1900         sc->expectChildColor(73, 73);
1901         sc->expectChildColor(74, 74);
1902     }
1903 }
1904 
TEST_F(LayerUpdateTest,MergingTransactions)1905 TEST_F(LayerUpdateTest, MergingTransactions) {
1906     sp<ScreenCapture> sc;
1907     {
1908         SCOPED_TRACE("before move");
1909         ScreenCapture::captureScreen(&sc);
1910         sc->expectBGColor(0, 12);
1911         sc->expectFGColor(75, 75);
1912         sc->expectBGColor(145, 145);
1913     }
1914 
1915     Transaction t1, t2;
1916     t1.setPosition(mFGSurfaceControl, 128, 128);
1917     t2.setPosition(mFGSurfaceControl, 0, 0);
1918     // We expect that the position update from t2 now
1919     // overwrites the position update from t1.
1920     t1.merge(std::move(t2));
1921     t1.apply();
1922 
1923     {
1924         ScreenCapture::captureScreen(&sc);
1925         sc->expectFGColor(1, 1);
1926     }
1927 }
1928 
1929 class ChildLayerTest : public LayerUpdateTest {
1930 protected:
SetUp()1931     void SetUp() override {
1932         LayerUpdateTest::SetUp();
1933         mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
1934                                                 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
1935         fillSurfaceRGBA8(mChild, 200, 200, 200);
1936 
1937         {
1938             SCOPED_TRACE("before anything");
1939             ScreenCapture::captureScreen(&mCapture);
1940             mCapture->expectChildColor(64, 64);
1941         }
1942     }
TearDown()1943     void TearDown() override {
1944         LayerUpdateTest::TearDown();
1945         mChild = 0;
1946     }
1947 
1948     sp<SurfaceControl> mChild;
1949     sp<ScreenCapture> mCapture;
1950 };
1951 
TEST_F(ChildLayerTest,ChildLayerPositioning)1952 TEST_F(ChildLayerTest, ChildLayerPositioning) {
1953     asTransaction([&](Transaction& t) {
1954         t.show(mChild);
1955         t.setPosition(mChild, 10, 10);
1956         t.setPosition(mFGSurfaceControl, 64, 64);
1957     });
1958 
1959     {
1960         ScreenCapture::captureScreen(&mCapture);
1961         // Top left of foreground must now be visible
1962         mCapture->expectFGColor(64, 64);
1963         // But 10 pixels in we should see the child surface
1964         mCapture->expectChildColor(74, 74);
1965         // And 10 more pixels we should be back to the foreground surface
1966         mCapture->expectFGColor(84, 84);
1967     }
1968 
1969     asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
1970 
1971     {
1972         ScreenCapture::captureScreen(&mCapture);
1973         // Top left of foreground should now be at 0, 0
1974         mCapture->expectFGColor(0, 0);
1975         // But 10 pixels in we should see the child surface
1976         mCapture->expectChildColor(10, 10);
1977         // And 10 more pixels we should be back to the foreground surface
1978         mCapture->expectFGColor(20, 20);
1979     }
1980 }
1981 
TEST_F(ChildLayerTest,ChildLayerCropping)1982 TEST_F(ChildLayerTest, ChildLayerCropping) {
1983     asTransaction([&](Transaction& t) {
1984         t.show(mChild);
1985         t.setPosition(mChild, 0, 0);
1986         t.setPosition(mFGSurfaceControl, 0, 0);
1987         t.setCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
1988     });
1989 
1990     {
1991         ScreenCapture::captureScreen(&mCapture);
1992         mCapture->expectChildColor(0, 0);
1993         mCapture->expectChildColor(4, 4);
1994         mCapture->expectBGColor(5, 5);
1995     }
1996 }
1997 
TEST_F(ChildLayerTest,ChildLayerFinalCropping)1998 TEST_F(ChildLayerTest, ChildLayerFinalCropping) {
1999     asTransaction([&](Transaction& t) {
2000         t.show(mChild);
2001         t.setPosition(mChild, 0, 0);
2002         t.setPosition(mFGSurfaceControl, 0, 0);
2003         t.setFinalCrop(mFGSurfaceControl, Rect(0, 0, 5, 5));
2004     });
2005 
2006     {
2007         ScreenCapture::captureScreen(&mCapture);
2008         mCapture->expectChildColor(0, 0);
2009         mCapture->expectChildColor(4, 4);
2010         mCapture->expectBGColor(5, 5);
2011     }
2012 }
2013 
TEST_F(ChildLayerTest,ChildLayerConstraints)2014 TEST_F(ChildLayerTest, ChildLayerConstraints) {
2015     asTransaction([&](Transaction& t) {
2016         t.show(mChild);
2017         t.setPosition(mFGSurfaceControl, 0, 0);
2018         t.setPosition(mChild, 63, 63);
2019     });
2020 
2021     {
2022         ScreenCapture::captureScreen(&mCapture);
2023         mCapture->expectFGColor(0, 0);
2024         // Last pixel in foreground should now be the child.
2025         mCapture->expectChildColor(63, 63);
2026         // But the child should be constrained and the next pixel
2027         // must be the background
2028         mCapture->expectBGColor(64, 64);
2029     }
2030 }
2031 
TEST_F(ChildLayerTest,ChildLayerScaling)2032 TEST_F(ChildLayerTest, ChildLayerScaling) {
2033     asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
2034 
2035     // Find the boundary between the parent and child
2036     {
2037         ScreenCapture::captureScreen(&mCapture);
2038         mCapture->expectChildColor(9, 9);
2039         mCapture->expectFGColor(10, 10);
2040     }
2041 
2042     asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
2043 
2044     // The boundary should be twice as far from the origin now.
2045     // The pixels from the last test should all be child now
2046     {
2047         ScreenCapture::captureScreen(&mCapture);
2048         mCapture->expectChildColor(9, 9);
2049         mCapture->expectChildColor(10, 10);
2050         mCapture->expectChildColor(19, 19);
2051         mCapture->expectFGColor(20, 20);
2052     }
2053 }
2054 
TEST_F(ChildLayerTest,ChildLayerAlpha)2055 TEST_F(ChildLayerTest, ChildLayerAlpha) {
2056     fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
2057     fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
2058     fillSurfaceRGBA8(mChild, 0, 254, 0);
2059     waitForPostedBuffers();
2060 
2061     asTransaction([&](Transaction& t) {
2062         t.show(mChild);
2063         t.setPosition(mChild, 0, 0);
2064         t.setPosition(mFGSurfaceControl, 0, 0);
2065     });
2066 
2067     {
2068         ScreenCapture::captureScreen(&mCapture);
2069         // Unblended child color
2070         mCapture->checkPixel(0, 0, 0, 254, 0);
2071     }
2072 
2073     asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
2074 
2075     {
2076         ScreenCapture::captureScreen(&mCapture);
2077         // Child and BG blended.
2078         mCapture->checkPixel(0, 0, 127, 127, 0);
2079     }
2080 
2081     asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
2082 
2083     {
2084         ScreenCapture::captureScreen(&mCapture);
2085         // Child and BG blended.
2086         mCapture->checkPixel(0, 0, 95, 64, 95);
2087     }
2088 }
2089 
TEST_F(ChildLayerTest,ReparentChildren)2090 TEST_F(ChildLayerTest, ReparentChildren) {
2091     asTransaction([&](Transaction& t) {
2092         t.show(mChild);
2093         t.setPosition(mChild, 10, 10);
2094         t.setPosition(mFGSurfaceControl, 64, 64);
2095     });
2096 
2097     {
2098         ScreenCapture::captureScreen(&mCapture);
2099         // Top left of foreground must now be visible
2100         mCapture->expectFGColor(64, 64);
2101         // But 10 pixels in we should see the child surface
2102         mCapture->expectChildColor(74, 74);
2103         // And 10 more pixels we should be back to the foreground surface
2104         mCapture->expectFGColor(84, 84);
2105     }
2106 
2107     asTransaction([&](Transaction& t) {
2108         t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
2109     });
2110 
2111     {
2112         ScreenCapture::captureScreen(&mCapture);
2113         mCapture->expectFGColor(64, 64);
2114         // In reparenting we should have exposed the entire foreground surface.
2115         mCapture->expectFGColor(74, 74);
2116         // And the child layer should now begin at 10, 10 (since the BG
2117         // layer is at (0, 0)).
2118         mCapture->expectBGColor(9, 9);
2119         mCapture->expectChildColor(10, 10);
2120     }
2121 }
2122 
TEST_F(ChildLayerTest,DetachChildrenSameClient)2123 TEST_F(ChildLayerTest, DetachChildrenSameClient) {
2124     asTransaction([&](Transaction& t) {
2125         t.show(mChild);
2126         t.setPosition(mChild, 10, 10);
2127         t.setPosition(mFGSurfaceControl, 64, 64);
2128     });
2129 
2130     {
2131         ScreenCapture::captureScreen(&mCapture);
2132         // Top left of foreground must now be visible
2133         mCapture->expectFGColor(64, 64);
2134         // But 10 pixels in we should see the child surface
2135         mCapture->expectChildColor(74, 74);
2136         // And 10 more pixels we should be back to the foreground surface
2137         mCapture->expectFGColor(84, 84);
2138     }
2139 
2140     asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
2141 
2142     asTransaction([&](Transaction& t) { t.hide(mChild); });
2143 
2144     // Since the child has the same client as the parent, it will not get
2145     // detached and will be hidden.
2146     {
2147         ScreenCapture::captureScreen(&mCapture);
2148         mCapture->expectFGColor(64, 64);
2149         mCapture->expectFGColor(74, 74);
2150         mCapture->expectFGColor(84, 84);
2151     }
2152 }
2153 
TEST_F(ChildLayerTest,DetachChildrenDifferentClient)2154 TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
2155     sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
2156     sp<SurfaceControl> mChildNewClient =
2157             mNewComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
2158                                               PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2159 
2160     ASSERT_TRUE(mChildNewClient != nullptr);
2161     ASSERT_TRUE(mChildNewClient->isValid());
2162 
2163     fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
2164 
2165     asTransaction([&](Transaction& t) {
2166         t.hide(mChild);
2167         t.show(mChildNewClient);
2168         t.setPosition(mChildNewClient, 10, 10);
2169         t.setPosition(mFGSurfaceControl, 64, 64);
2170     });
2171 
2172     {
2173         ScreenCapture::captureScreen(&mCapture);
2174         // Top left of foreground must now be visible
2175         mCapture->expectFGColor(64, 64);
2176         // But 10 pixels in we should see the child surface
2177         mCapture->expectChildColor(74, 74);
2178         // And 10 more pixels we should be back to the foreground surface
2179         mCapture->expectFGColor(84, 84);
2180     }
2181 
2182     asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
2183 
2184     asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
2185 
2186     // Nothing should have changed.
2187     {
2188         ScreenCapture::captureScreen(&mCapture);
2189         mCapture->expectFGColor(64, 64);
2190         mCapture->expectChildColor(74, 74);
2191         mCapture->expectFGColor(84, 84);
2192     }
2193 }
2194 
TEST_F(ChildLayerTest,ChildrenInheritNonTransformScalingFromParent)2195 TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
2196     asTransaction([&](Transaction& t) {
2197         t.show(mChild);
2198         t.setPosition(mChild, 0, 0);
2199         t.setPosition(mFGSurfaceControl, 0, 0);
2200     });
2201 
2202     {
2203         ScreenCapture::captureScreen(&mCapture);
2204         // We've positioned the child in the top left.
2205         mCapture->expectChildColor(0, 0);
2206         // But it's only 10x10.
2207         mCapture->expectFGColor(10, 10);
2208     }
2209 
2210     asTransaction([&](Transaction& t) {
2211         t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2212         // We cause scaling by 2.
2213         t.setSize(mFGSurfaceControl, 128, 128);
2214     });
2215 
2216     {
2217         ScreenCapture::captureScreen(&mCapture);
2218         // We've positioned the child in the top left.
2219         mCapture->expectChildColor(0, 0);
2220         mCapture->expectChildColor(10, 10);
2221         mCapture->expectChildColor(19, 19);
2222         // And now it should be scaled all the way to 20x20
2223         mCapture->expectFGColor(20, 20);
2224     }
2225 }
2226 
2227 // Regression test for b/37673612
TEST_F(ChildLayerTest,ChildrenWithParentBufferTransform)2228 TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
2229     asTransaction([&](Transaction& t) {
2230         t.show(mChild);
2231         t.setPosition(mChild, 0, 0);
2232         t.setPosition(mFGSurfaceControl, 0, 0);
2233     });
2234 
2235     {
2236         ScreenCapture::captureScreen(&mCapture);
2237         // We've positioned the child in the top left.
2238         mCapture->expectChildColor(0, 0);
2239         // But it's only 10x10.
2240         mCapture->expectFGColor(10, 10);
2241     }
2242     // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
2243     // the WM specified state size.
2244     asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
2245     sp<Surface> s = mFGSurfaceControl->getSurface();
2246     auto anw = static_cast<ANativeWindow*>(s.get());
2247     native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
2248     native_window_set_buffers_dimensions(anw, 64, 128);
2249     fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
2250     waitForPostedBuffers();
2251 
2252     {
2253         // The child should still be in the same place and not have any strange scaling as in
2254         // b/37673612.
2255         ScreenCapture::captureScreen(&mCapture);
2256         mCapture->expectChildColor(0, 0);
2257         mCapture->expectFGColor(10, 10);
2258     }
2259 }
2260 
TEST_F(ChildLayerTest,Bug36858924)2261 TEST_F(ChildLayerTest, Bug36858924) {
2262     // Destroy the child layer
2263     mChild.clear();
2264 
2265     // Now recreate it as hidden
2266     mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10,
2267                                             PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden,
2268                                             mFGSurfaceControl.get());
2269 
2270     // Show the child layer in a deferred transaction
2271     asTransaction([&](Transaction& t) {
2272         t.deferTransactionUntil(mChild, mFGSurfaceControl->getHandle(),
2273                                 mFGSurfaceControl->getSurface()->getNextFrameNumber());
2274         t.show(mChild);
2275     });
2276 
2277     // Render the foreground surface a few times
2278     //
2279     // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
2280     // frame because SurfaceFlinger would never process the deferred transaction and would therefore
2281     // never acquire/release the first buffer
2282     ALOGI("Filling 1");
2283     fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2284     ALOGI("Filling 2");
2285     fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
2286     ALOGI("Filling 3");
2287     fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
2288     ALOGI("Filling 4");
2289     fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
2290 }
2291 
TEST_F(ChildLayerTest,Reparent)2292 TEST_F(ChildLayerTest, Reparent) {
2293     asTransaction([&](Transaction& t) {
2294         t.show(mChild);
2295         t.setPosition(mChild, 10, 10);
2296         t.setPosition(mFGSurfaceControl, 64, 64);
2297     });
2298 
2299     {
2300         ScreenCapture::captureScreen(&mCapture);
2301         // Top left of foreground must now be visible
2302         mCapture->expectFGColor(64, 64);
2303         // But 10 pixels in we should see the child surface
2304         mCapture->expectChildColor(74, 74);
2305         // And 10 more pixels we should be back to the foreground surface
2306         mCapture->expectFGColor(84, 84);
2307     }
2308 
2309     asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
2310 
2311     {
2312         ScreenCapture::captureScreen(&mCapture);
2313         mCapture->expectFGColor(64, 64);
2314         // In reparenting we should have exposed the entire foreground surface.
2315         mCapture->expectFGColor(74, 74);
2316         // And the child layer should now begin at 10, 10 (since the BG
2317         // layer is at (0, 0)).
2318         mCapture->expectBGColor(9, 9);
2319         mCapture->expectChildColor(10, 10);
2320     }
2321 }
2322 
TEST_F(ChildLayerTest,ReparentToNoParent)2323 TEST_F(ChildLayerTest, ReparentToNoParent) {
2324     asTransaction([&](Transaction& t) {
2325         t.show(mChild);
2326         t.setPosition(mChild, 10, 10);
2327         t.setPosition(mFGSurfaceControl, 64, 64);
2328     });
2329 
2330     {
2331         ScreenCapture::captureScreen(&mCapture);
2332         // Top left of foreground must now be visible
2333         mCapture->expectFGColor(64, 64);
2334         // But 10 pixels in we should see the child surface
2335         mCapture->expectChildColor(74, 74);
2336         // And 10 more pixels we should be back to the foreground surface
2337         mCapture->expectFGColor(84, 84);
2338     }
2339     asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
2340     {
2341         ScreenCapture::captureScreen(&mCapture);
2342         // Nothing should have changed.
2343         mCapture->expectFGColor(64, 64);
2344         mCapture->expectChildColor(74, 74);
2345         mCapture->expectFGColor(84, 84);
2346     }
2347 }
2348 
TEST_F(ChildLayerTest,ReparentFromNoParent)2349 TEST_F(ChildLayerTest, ReparentFromNoParent) {
2350     sp<SurfaceControl> newSurface = mComposerClient->createSurface(String8("New Surface"), 10, 10,
2351                                                                    PIXEL_FORMAT_RGBA_8888, 0);
2352     ASSERT_TRUE(newSurface != nullptr);
2353     ASSERT_TRUE(newSurface->isValid());
2354 
2355     fillSurfaceRGBA8(newSurface, 63, 195, 63);
2356     asTransaction([&](Transaction& t) {
2357         t.hide(mChild);
2358         t.show(newSurface);
2359         t.setPosition(newSurface, 10, 10);
2360         t.setLayer(newSurface, INT32_MAX - 2);
2361         t.setPosition(mFGSurfaceControl, 64, 64);
2362     });
2363 
2364     {
2365         ScreenCapture::captureScreen(&mCapture);
2366         // Top left of foreground must now be visible
2367         mCapture->expectFGColor(64, 64);
2368         // At 10, 10 we should see the new surface
2369         mCapture->checkPixel(10, 10, 63, 195, 63);
2370     }
2371 
2372     asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
2373 
2374     {
2375         ScreenCapture::captureScreen(&mCapture);
2376         // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
2377         // mFGSurface, putting it at 74, 74.
2378         mCapture->expectFGColor(64, 64);
2379         mCapture->checkPixel(74, 74, 63, 195, 63);
2380         mCapture->expectFGColor(84, 84);
2381     }
2382 }
2383 
TEST_F(ChildLayerTest,NestedChildren)2384 TEST_F(ChildLayerTest, NestedChildren) {
2385     sp<SurfaceControl> grandchild =
2386             mComposerClient->createSurface(String8("Grandchild surface"), 10, 10,
2387                                            PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
2388     fillSurfaceRGBA8(grandchild, 50, 50, 50);
2389 
2390     {
2391         ScreenCapture::captureScreen(&mCapture);
2392         // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
2393         // which begins at 64, 64
2394         mCapture->checkPixel(64, 64, 50, 50, 50);
2395     }
2396 }
2397 
TEST_F(ChildLayerTest,ChildLayerRelativeLayer)2398 TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
2399     sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 128,
2400                                                                  128, PIXEL_FORMAT_RGBA_8888, 0);
2401     fillSurfaceRGBA8(relative, 255, 255, 255);
2402 
2403     Transaction t;
2404     t.setLayer(relative, INT32_MAX)
2405             .setRelativeLayer(mChild, relative->getHandle(), 1)
2406             .setPosition(mFGSurfaceControl, 0, 0)
2407             .apply(true);
2408 
2409     // We expect that the child should have been elevated above our
2410     // INT_MAX layer even though it's not a child of it.
2411     {
2412         ScreenCapture::captureScreen(&mCapture);
2413         mCapture->expectChildColor(0, 0);
2414         mCapture->expectChildColor(9, 9);
2415         mCapture->checkPixel(10, 10, 255, 255, 255);
2416     }
2417 }
2418 
2419 class ScreenCaptureTest : public LayerUpdateTest {
2420 protected:
2421     std::unique_ptr<ScreenCapture> mCapture;
2422 };
2423 
TEST_F(ScreenCaptureTest,CaptureSingleLayer)2424 TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
2425     auto bgHandle = mBGSurfaceControl->getHandle();
2426     ScreenCapture::captureLayers(&mCapture, bgHandle);
2427     mCapture->expectBGColor(0, 0);
2428     // Doesn't capture FG layer which is at 64, 64
2429     mCapture->expectBGColor(64, 64);
2430 }
2431 
TEST_F(ScreenCaptureTest,CaptureLayerWithChild)2432 TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
2433     auto fgHandle = mFGSurfaceControl->getHandle();
2434 
2435     sp<SurfaceControl> child =
2436             mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2437                                            0, mFGSurfaceControl.get());
2438     fillSurfaceRGBA8(child, 200, 200, 200);
2439 
2440     SurfaceComposerClient::Transaction().show(child).apply(true);
2441 
2442     // Captures mFGSurfaceControl layer and its child.
2443     ScreenCapture::captureLayers(&mCapture, fgHandle);
2444     mCapture->expectFGColor(10, 10);
2445     mCapture->expectChildColor(0, 0);
2446 }
2447 
TEST_F(ScreenCaptureTest,CaptureLayerChildOnly)2448 TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
2449     auto fgHandle = mFGSurfaceControl->getHandle();
2450 
2451     sp<SurfaceControl> child =
2452             mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2453                                            0, mFGSurfaceControl.get());
2454     fillSurfaceRGBA8(child, 200, 200, 200);
2455 
2456     SurfaceComposerClient::Transaction().show(child).apply(true);
2457 
2458     // Captures mFGSurfaceControl's child
2459     ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2460     mCapture->checkPixel(10, 10, 0, 0, 0);
2461     mCapture->expectChildColor(0, 0);
2462 }
2463 
TEST_F(ScreenCaptureTest,CaptureTransparent)2464 TEST_F(ScreenCaptureTest, CaptureTransparent) {
2465     sp<SurfaceControl> child =
2466             mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2467                                            0, mFGSurfaceControl.get());
2468 
2469     fillSurfaceRGBA8(child, 200, 200, 200);
2470 
2471     SurfaceComposerClient::Transaction().show(child).apply(true);
2472 
2473     auto childHandle = child->getHandle();
2474 
2475     // Captures child
2476     ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
2477     mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
2478     // Area outside of child's bounds is transparent.
2479     mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
2480 }
2481 
TEST_F(ScreenCaptureTest,DontCaptureRelativeOutsideTree)2482 TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
2483     auto fgHandle = mFGSurfaceControl->getHandle();
2484 
2485     sp<SurfaceControl> child =
2486             mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2487                                            0, mFGSurfaceControl.get());
2488     sp<SurfaceControl> relative = mComposerClient->createSurface(String8("Relative surface"), 10,
2489                                                                  10, PIXEL_FORMAT_RGBA_8888, 0);
2490     fillSurfaceRGBA8(child, 200, 200, 200);
2491     fillSurfaceRGBA8(relative, 100, 100, 100);
2492 
2493     SurfaceComposerClient::Transaction()
2494             .show(child)
2495             // Set relative layer above fg layer so should be shown above when computing all layers.
2496             .setRelativeLayer(relative, fgHandle, 1)
2497             .show(relative)
2498             .apply(true);
2499 
2500     // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
2501     ScreenCapture::captureLayers(&mCapture, fgHandle);
2502     mCapture->expectFGColor(10, 10);
2503     mCapture->expectChildColor(0, 0);
2504 }
2505 
TEST_F(ScreenCaptureTest,CaptureRelativeInTree)2506 TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
2507     auto fgHandle = mFGSurfaceControl->getHandle();
2508 
2509     sp<SurfaceControl> child =
2510             mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2511                                            0, mFGSurfaceControl.get());
2512     sp<SurfaceControl> relative =
2513             mComposerClient->createSurface(String8("Relative surface"), 10, 10,
2514                                            PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
2515     fillSurfaceRGBA8(child, 200, 200, 200);
2516     fillSurfaceRGBA8(relative, 100, 100, 100);
2517 
2518     SurfaceComposerClient::Transaction()
2519             .show(child)
2520             // Set relative layer below fg layer but relative to child layer so it should be shown
2521             // above child layer.
2522             .setLayer(relative, -1)
2523             .setRelativeLayer(relative, child->getHandle(), 1)
2524             .show(relative)
2525             .apply(true);
2526 
2527     // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
2528     // relative value should be taken into account, placing it above child layer.
2529     ScreenCapture::captureLayers(&mCapture, fgHandle);
2530     mCapture->expectFGColor(10, 10);
2531     // Relative layer is showing on top of child layer
2532     mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
2533 }
2534 
2535 // In the following tests we verify successful skipping of a parent layer,
2536 // so we use the same verification logic and only change how we mutate
2537 // the parent layer to verify that various properties are ignored.
2538 class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
2539 public:
SetUp()2540     void SetUp() override {
2541         LayerUpdateTest::SetUp();
2542 
2543         mChild =
2544             mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2545                     0, mFGSurfaceControl.get());
2546         fillSurfaceRGBA8(mChild, 200, 200, 200);
2547 
2548         SurfaceComposerClient::Transaction().show(mChild).apply(true);
2549     }
2550 
verify()2551     void verify() {
2552         auto fgHandle = mFGSurfaceControl->getHandle();
2553         ScreenCapture::captureChildLayers(&mCapture, fgHandle);
2554         mCapture->checkPixel(10, 10, 0, 0, 0);
2555         mCapture->expectChildColor(0, 0);
2556     }
2557 
2558     std::unique_ptr<ScreenCapture> mCapture;
2559     sp<SurfaceControl> mChild;
2560 };
2561 
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresParentVisibility)2562 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
2563 
2564     SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2565 
2566     // Even though the parent is hidden we should still capture the child.
2567     verify();
2568 }
2569 
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresParentCrop)2570 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
2571 
2572     SurfaceComposerClient::Transaction().setCrop(mFGSurfaceControl, Rect(0, 0, 1, 1)).apply(true);
2573 
2574     // Even though the parent is cropped out we should still capture the child.
2575     verify();
2576 }
2577 
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresTransform)2578 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
2579 
2580     SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2);
2581 
2582     // We should not inherit the parent scaling.
2583     verify();
2584 }
2585 
TEST_F(ScreenCaptureChildOnlyTest,RegressionTest76099859)2586 TEST_F(ScreenCaptureChildOnlyTest, RegressionTest76099859) {
2587     SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
2588 
2589     // Even though the parent is hidden we should still capture the child.
2590     verify();
2591 
2592     // Verify everything was properly hidden when rendering the full-screen.
2593     screenshot()->expectBGColor(0,0);
2594 }
2595 
2596 
TEST_F(ScreenCaptureTest,CaptureLayerWithGrandchild)2597 TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
2598     auto fgHandle = mFGSurfaceControl->getHandle();
2599 
2600     sp<SurfaceControl> child =
2601             mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2602                                            0, mFGSurfaceControl.get());
2603     fillSurfaceRGBA8(child, 200, 200, 200);
2604 
2605     sp<SurfaceControl> grandchild =
2606             mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2607                                            PIXEL_FORMAT_RGBA_8888, 0, child.get());
2608 
2609     fillSurfaceRGBA8(grandchild, 50, 50, 50);
2610     SurfaceComposerClient::Transaction()
2611             .show(child)
2612             .setPosition(grandchild, 5, 5)
2613             .show(grandchild)
2614             .apply(true);
2615 
2616     // Captures mFGSurfaceControl, its child, and the grandchild.
2617     ScreenCapture::captureLayers(&mCapture, fgHandle);
2618     mCapture->expectFGColor(10, 10);
2619     mCapture->expectChildColor(0, 0);
2620     mCapture->checkPixel(5, 5, 50, 50, 50);
2621 }
2622 
TEST_F(ScreenCaptureTest,CaptureChildOnly)2623 TEST_F(ScreenCaptureTest, CaptureChildOnly) {
2624     sp<SurfaceControl> child =
2625             mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2626                                            0, mFGSurfaceControl.get());
2627     fillSurfaceRGBA8(child, 200, 200, 200);
2628     auto childHandle = child->getHandle();
2629 
2630     SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
2631 
2632     // Captures only the child layer, and not the parent.
2633     ScreenCapture::captureLayers(&mCapture, childHandle);
2634     mCapture->expectChildColor(0, 0);
2635     mCapture->expectChildColor(9, 9);
2636 }
2637 
TEST_F(ScreenCaptureTest,CaptureGrandchildOnly)2638 TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
2639     sp<SurfaceControl> child =
2640             mComposerClient->createSurface(String8("Child surface"), 10, 10, PIXEL_FORMAT_RGBA_8888,
2641                                            0, mFGSurfaceControl.get());
2642     fillSurfaceRGBA8(child, 200, 200, 200);
2643     auto childHandle = child->getHandle();
2644 
2645     sp<SurfaceControl> grandchild =
2646             mComposerClient->createSurface(String8("Grandchild surface"), 5, 5,
2647                                            PIXEL_FORMAT_RGBA_8888, 0, child.get());
2648     fillSurfaceRGBA8(grandchild, 50, 50, 50);
2649 
2650     SurfaceComposerClient::Transaction()
2651             .show(child)
2652             .setPosition(grandchild, 5, 5)
2653             .show(grandchild)
2654             .apply(true);
2655 
2656     auto grandchildHandle = grandchild->getHandle();
2657 
2658     // Captures only the grandchild.
2659     ScreenCapture::captureLayers(&mCapture, grandchildHandle);
2660     mCapture->checkPixel(0, 0, 50, 50, 50);
2661     mCapture->checkPixel(4, 4, 50, 50, 50);
2662 }
2663 
TEST_F(ScreenCaptureTest,CaptureCrop)2664 TEST_F(ScreenCaptureTest, CaptureCrop) {
2665     sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2666                                                                  PIXEL_FORMAT_RGBA_8888, 0);
2667     sp<SurfaceControl> blueLayer =
2668             mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2669                                            0, redLayer.get());
2670 
2671     ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2672     ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
2673 
2674     SurfaceComposerClient::Transaction()
2675             .setLayer(redLayer, INT32_MAX - 1)
2676             .show(redLayer)
2677             .show(blueLayer)
2678             .apply(true);
2679 
2680     auto redLayerHandle = redLayer->getHandle();
2681 
2682     // Capturing full screen should have both red and blue are visible.
2683     ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2684     mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2685     // red area below the blue area
2686     mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2687     // red area to the right of the blue area
2688     mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
2689 
2690     Rect crop = Rect(0, 0, 30, 30);
2691     ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
2692     // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
2693     // area visible.
2694     mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2695     mCapture->checkPixel(30, 30, 0, 0, 0);
2696 }
2697 
TEST_F(ScreenCaptureTest,CaptureSize)2698 TEST_F(ScreenCaptureTest, CaptureSize) {
2699     sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2700                                                                  PIXEL_FORMAT_RGBA_8888, 0);
2701     sp<SurfaceControl> blueLayer =
2702             mComposerClient->createSurface(String8("Blue surface"), 30, 30, PIXEL_FORMAT_RGBA_8888,
2703                                            0, redLayer.get());
2704 
2705     ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2706     ASSERT_NO_FATAL_FAILURE(fillLayerColor(blueLayer, Color::BLUE));
2707 
2708     SurfaceComposerClient::Transaction()
2709             .setLayer(redLayer, INT32_MAX - 1)
2710             .show(redLayer)
2711             .show(blueLayer)
2712             .apply(true);
2713 
2714     auto redLayerHandle = redLayer->getHandle();
2715 
2716     // Capturing full screen should have both red and blue are visible.
2717     ScreenCapture::captureLayers(&mCapture, redLayerHandle);
2718     mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
2719     // red area below the blue area
2720     mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
2721     // red area to the right of the blue area
2722     mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
2723 
2724     ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
2725     // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
2726     mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
2727     // red area below the blue area
2728     mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
2729     // red area to the right of the blue area
2730     mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
2731     mCapture->checkPixel(30, 30, 0, 0, 0);
2732 }
2733 
TEST_F(ScreenCaptureTest,CaptureInvalidLayer)2734 TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
2735     sp<SurfaceControl> redLayer = mComposerClient->createSurface(String8("Red surface"), 60, 60,
2736                                                                  PIXEL_FORMAT_RGBA_8888, 0);
2737 
2738     ASSERT_NO_FATAL_FAILURE(fillLayerColor(redLayer, Color::RED));
2739 
2740     auto redLayerHandle = redLayer->getHandle();
2741     mComposerClient->destroySurface(redLayerHandle);
2742     SurfaceComposerClient::Transaction().apply(true);
2743 
2744     sp<GraphicBuffer> outBuffer;
2745 
2746     // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
2747     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2748     ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
2749 }
2750 
2751 
2752 class DereferenceSurfaceControlTest : public LayerTransactionTest {
2753 protected:
SetUp()2754     void SetUp() override {
2755         LayerTransactionTest::SetUp();
2756         bgLayer = createLayer("BG layer", 20, 20);
2757         fillLayerColor(bgLayer, Color::RED);
2758         fgLayer = createLayer("FG layer", 20, 20);
2759         fillLayerColor(fgLayer, Color::BLUE);
2760         Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
2761         {
2762             SCOPED_TRACE("before anything");
2763             auto shot = screenshot();
2764             shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2765         }
2766     }
TearDown()2767     void TearDown() override {
2768         LayerTransactionTest::TearDown();
2769         bgLayer = 0;
2770         fgLayer = 0;
2771     }
2772 
2773     sp<SurfaceControl> bgLayer;
2774     sp<SurfaceControl> fgLayer;
2775 };
2776 
TEST_F(DereferenceSurfaceControlTest,LayerNotInTransaction)2777 TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
2778     fgLayer = nullptr;
2779     {
2780         SCOPED_TRACE("after setting null");
2781         auto shot = screenshot();
2782         shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
2783     }
2784 }
2785 
TEST_F(DereferenceSurfaceControlTest,LayerInTransaction)2786 TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
2787     auto transaction = Transaction().show(fgLayer);
2788     fgLayer = nullptr;
2789     {
2790         SCOPED_TRACE("after setting null");
2791         auto shot = screenshot();
2792         shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
2793     }
2794 }
2795 
2796 } // namespace android
2797