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 <chrono>
19 #include <cinttypes>
20 #include <functional>
21 #include <limits>
22 #include <ostream>
23 #include <thread>
24
25 #include <gtest/gtest.h>
26
27 #include <android/native_window.h>
28
29 #include <binder/ProcessState.h>
30 #include <gui/BufferItemConsumer.h>
31 #include <gui/ISurfaceComposer.h>
32 #include <gui/LayerState.h>
33 #include <gui/Surface.h>
34 #include <gui/SurfaceComposerClient.h>
35 #include <hardware/hwcomposer_defs.h>
36 #include <private/android_filesystem_config.h>
37 #include <private/gui/ComposerService.h>
38
39 #include <ui/ColorSpace.h>
40 #include <ui/DisplayInfo.h>
41 #include <ui/Rect.h>
42 #include <utils/String8.h>
43
44 #include <math.h>
45 #include <math/vec3.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48
49 #include "BufferGenerator.h"
50
51 namespace android {
52
53 namespace {
54
55 struct Color {
56 uint8_t r;
57 uint8_t g;
58 uint8_t b;
59 uint8_t a;
60
61 static const Color RED;
62 static const Color GREEN;
63 static const Color BLUE;
64 static const Color WHITE;
65 static const Color BLACK;
66 static const Color TRANSPARENT;
67 };
68
69 const Color Color::RED{255, 0, 0, 255};
70 const Color Color::GREEN{0, 255, 0, 255};
71 const Color Color::BLUE{0, 0, 255, 255};
72 const Color Color::WHITE{255, 255, 255, 255};
73 const Color Color::BLACK{0, 0, 0, 255};
74 const Color Color::TRANSPARENT{0, 0, 0, 0};
75
76 using android::hardware::graphics::common::V1_1::BufferUsage;
77 using namespace std::chrono_literals;
78
operator <<(std::ostream & os,const Color & color)79 std::ostream& operator<<(std::ostream& os, const Color& color) {
80 os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
81 return os;
82 }
83
84 // Fill a region with the specified color.
fillANativeWindowBufferColor(const ANativeWindow_Buffer & buffer,const Rect & rect,const Color & color)85 void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
86 const Color& color) {
87 Rect r(0, 0, buffer.width, buffer.height);
88 if (!r.intersect(rect, &r)) {
89 return;
90 }
91
92 int32_t width = r.right - r.left;
93 int32_t height = r.bottom - r.top;
94
95 for (int32_t row = 0; row < height; row++) {
96 uint8_t* dst =
97 static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
98 for (int32_t column = 0; column < width; column++) {
99 dst[0] = color.r;
100 dst[1] = color.g;
101 dst[2] = color.b;
102 dst[3] = color.a;
103 dst += 4;
104 }
105 }
106 }
107
108 // Fill a region with the specified color.
fillGraphicBufferColor(const sp<GraphicBuffer> & buffer,const Rect & rect,const Color & color)109 void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
110 Rect r(0, 0, buffer->width, buffer->height);
111 if (!r.intersect(rect, &r)) {
112 return;
113 }
114
115 int32_t width = r.right - r.left;
116 int32_t height = r.bottom - r.top;
117
118 uint8_t* pixels;
119 buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
120 reinterpret_cast<void**>(&pixels));
121
122 for (int32_t row = 0; row < height; row++) {
123 uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
124 for (int32_t column = 0; column < width; column++) {
125 dst[0] = color.r;
126 dst[1] = color.g;
127 dst[2] = color.b;
128 dst[3] = color.a;
129 dst += 4;
130 }
131 }
132 buffer->unlock();
133 }
134
135 // 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)136 void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
137 const Color& color, uint8_t tolerance) {
138 int32_t x = rect.left;
139 int32_t y = rect.top;
140 int32_t width = rect.right - rect.left;
141 int32_t height = rect.bottom - rect.top;
142
143 int32_t bufferWidth = int32_t(outBuffer->getWidth());
144 int32_t bufferHeight = int32_t(outBuffer->getHeight());
145 if (x + width > bufferWidth) {
146 x = std::min(x, bufferWidth);
147 width = bufferWidth - x;
148 }
149 if (y + height > bufferHeight) {
150 y = std::min(y, bufferHeight);
151 height = bufferHeight - y;
152 }
153
154 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
155 uint8_t tmp = a >= b ? a - b : b - a;
156 return tmp <= tolerance;
157 };
158 for (int32_t j = 0; j < height; j++) {
159 const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
160 for (int32_t i = 0; i < width; i++) {
161 const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
162 EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
163 << "pixel @ (" << x + i << ", " << y + j << "): "
164 << "expected (" << color << "), "
165 << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
166 src += 4;
167 }
168 }
169 }
170
171 } // anonymous namespace
172
173 using Transaction = SurfaceComposerClient::Transaction;
174
175 // 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)176 static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
177 bool unlock = true) {
178 ANativeWindow_Buffer outBuffer;
179 sp<Surface> s = sc->getSurface();
180 ASSERT_TRUE(s != nullptr);
181 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
182 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
183 for (int y = 0; y < outBuffer.height; y++) {
184 for (int x = 0; x < outBuffer.width; x++) {
185 uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
186 pixel[0] = r;
187 pixel[1] = g;
188 pixel[2] = b;
189 pixel[3] = 255;
190 }
191 }
192 if (unlock) {
193 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
194 }
195 }
196
197 // A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
198 // individual pixel values for testing purposes.
199 class ScreenCapture : public RefBase {
200 public:
captureScreen(std::unique_ptr<ScreenCapture> * sc)201 static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
202 captureScreen(sc, SurfaceComposerClient::getInternalDisplayToken());
203 }
204
captureScreen(std::unique_ptr<ScreenCapture> * sc,sp<IBinder> displayToken)205 static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) {
206 const auto sf = ComposerService::getComposerService();
207 SurfaceComposerClient::Transaction().apply(true);
208
209 sp<GraphicBuffer> outBuffer;
210 ASSERT_EQ(NO_ERROR, sf->captureScreen(displayToken, &outBuffer, Rect(), 0, 0, false));
211 *sc = std::make_unique<ScreenCapture>(outBuffer);
212 }
213
captureLayers(std::unique_ptr<ScreenCapture> * sc,sp<IBinder> & parentHandle,Rect crop=Rect::EMPTY_RECT,float frameScale=1.0)214 static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
215 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
216 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
217 SurfaceComposerClient::Transaction().apply(true);
218
219 sp<GraphicBuffer> outBuffer;
220 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
221 *sc = std::make_unique<ScreenCapture>(outBuffer);
222 }
223
captureChildLayers(std::unique_ptr<ScreenCapture> * sc,sp<IBinder> & parentHandle,Rect crop=Rect::EMPTY_RECT,float frameScale=1.0)224 static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
225 Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
226 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
227 SurfaceComposerClient::Transaction().apply(true);
228
229 sp<GraphicBuffer> outBuffer;
230 ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
231 *sc = std::make_unique<ScreenCapture>(outBuffer);
232 }
233
captureChildLayersExcluding(std::unique_ptr<ScreenCapture> * sc,sp<IBinder> & parentHandle,std::unordered_set<sp<IBinder>,ISurfaceComposer::SpHash<IBinder>> excludeLayers)234 static void captureChildLayersExcluding(
235 std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
236 std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>> excludeLayers) {
237 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
238 SurfaceComposerClient::Transaction().apply(true);
239
240 sp<GraphicBuffer> outBuffer;
241 ASSERT_EQ(NO_ERROR,
242 sf->captureLayers(parentHandle, &outBuffer, ui::Dataspace::V0_SRGB,
243 ui::PixelFormat::RGBA_8888, Rect::EMPTY_RECT, excludeLayers,
244 1.0f, true));
245 *sc = std::make_unique<ScreenCapture>(outBuffer);
246 }
247
expectColor(const Rect & rect,const Color & color,uint8_t tolerance=0)248 void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
249 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
250 expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
251 }
252
expectBorder(const Rect & rect,const Color & color,uint8_t tolerance=0)253 void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
254 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
255 const bool leftBorder = rect.left > 0;
256 const bool topBorder = rect.top > 0;
257 const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
258 const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
259
260 if (topBorder) {
261 Rect top(rect.left, rect.top - 1, rect.right, rect.top);
262 if (leftBorder) {
263 top.left -= 1;
264 }
265 if (rightBorder) {
266 top.right += 1;
267 }
268 expectColor(top, color, tolerance);
269 }
270 if (leftBorder) {
271 Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
272 expectColor(left, color, tolerance);
273 }
274 if (rightBorder) {
275 Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
276 expectColor(right, color, tolerance);
277 }
278 if (bottomBorder) {
279 Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
280 if (leftBorder) {
281 bottom.left -= 1;
282 }
283 if (rightBorder) {
284 bottom.right += 1;
285 }
286 expectColor(bottom, color, tolerance);
287 }
288 }
289
expectQuadrant(const Rect & rect,const Color & topLeft,const Color & topRight,const Color & bottomLeft,const Color & bottomRight,bool filtered=false,uint8_t tolerance=0)290 void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
291 const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
292 uint8_t tolerance = 0) {
293 ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
294
295 const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
296 const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
297 // avoid checking borders due to unspecified filtering behavior
298 const int32_t offsetX = filtered ? 2 : 0;
299 const int32_t offsetY = filtered ? 2 : 0;
300 expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
301 tolerance);
302 expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
303 tolerance);
304 expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
305 tolerance);
306 expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
307 bottomRight, tolerance);
308 }
309
checkPixel(uint32_t x,uint32_t y,uint8_t r,uint8_t g,uint8_t b)310 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
311 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
312 const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
313 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
314 String8 err(String8::format("pixel @ (%3d, %3d): "
315 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
316 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
317 EXPECT_EQ(String8(), err) << err.string();
318 }
319 }
320
expectFGColor(uint32_t x,uint32_t y)321 void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
322
expectBGColor(uint32_t x,uint32_t y)323 void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
324
expectChildColor(uint32_t x,uint32_t y)325 void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
326
ScreenCapture(const sp<GraphicBuffer> & outBuffer)327 explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
328 mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
329 }
330
~ScreenCapture()331 ~ScreenCapture() { mOutBuffer->unlock(); }
332
333 private:
334 sp<GraphicBuffer> mOutBuffer;
335 uint8_t* mPixels = nullptr;
336 };
337
338 class LayerTransactionTest : public ::testing::Test {
339 protected:
SetUp()340 void SetUp() override {
341 mClient = new SurfaceComposerClient;
342 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
343
344 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
345
346 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
347 ASSERT_NO_FATAL_FAILURE(sf->getColorManagement(&mColorManagementUsed));
348 }
349
TearDown()350 virtual void TearDown() {
351 mBlackBgSurface = 0;
352 mClient->dispose();
353 mClient = 0;
354 }
355
createLayer(const sp<SurfaceComposerClient> & client,const char * name,uint32_t width,uint32_t height,uint32_t flags=0,SurfaceControl * parent=nullptr)356 virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
357 const char* name, uint32_t width, uint32_t height,
358 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
359 auto layer =
360 createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags, parent);
361
362 Transaction t;
363 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
364
365 status_t error = t.apply();
366 if (error != NO_ERROR) {
367 ADD_FAILURE() << "failed to initialize SurfaceControl";
368 layer.clear();
369 }
370
371 return layer;
372 }
373
createSurface(const sp<SurfaceComposerClient> & client,const char * name,uint32_t width,uint32_t height,PixelFormat format,uint32_t flags,SurfaceControl * parent=nullptr)374 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
375 const char* name, uint32_t width, uint32_t height,
376 PixelFormat format, uint32_t flags,
377 SurfaceControl* parent = nullptr) {
378 auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
379 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
380 return layer;
381 }
382
createLayer(const char * name,uint32_t width,uint32_t height,uint32_t flags=0,SurfaceControl * parent=nullptr)383 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
384 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
385 return createLayer(mClient, name, width, height, flags, parent);
386 }
387
createColorLayer(const char * name,const Color & color,SurfaceControl * parent=nullptr)388 sp<SurfaceControl> createColorLayer(const char* name, const Color& color,
389 SurfaceControl* parent = nullptr) {
390 auto colorLayer = createSurface(mClient, name, 0 /* buffer width */, 0 /* buffer height */,
391 PIXEL_FORMAT_RGBA_8888,
392 ISurfaceComposerClient::eFXSurfaceColor, parent);
393 asTransaction([&](Transaction& t) {
394 t.setColor(colorLayer, half3{color.r / 255.0f, color.g / 255.0f, color.b / 255.0f});
395 t.setAlpha(colorLayer, color.a / 255.0f);
396 });
397 return colorLayer;
398 }
399
getBufferQueueLayerBuffer(const sp<SurfaceControl> & layer)400 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
401 // wait for previous transactions (such as setSize) to complete
402 Transaction().apply(true);
403
404 ANativeWindow_Buffer buffer = {};
405 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
406
407 return buffer;
408 }
409
postBufferQueueLayerBuffer(const sp<SurfaceControl> & layer)410 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
411 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
412
413 // wait for the newly posted buffer to be latched
414 waitForLayerBuffers();
415 }
416
fillBufferQueueLayerColor(const sp<SurfaceControl> & layer,const Color & color,int32_t bufferWidth,int32_t bufferHeight)417 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
418 int32_t bufferWidth, int32_t bufferHeight) {
419 ANativeWindow_Buffer buffer;
420 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
421 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
422 postBufferQueueLayerBuffer(layer);
423 }
424
fillBufferStateLayerColor(const sp<SurfaceControl> & layer,const Color & color,int32_t bufferWidth,int32_t bufferHeight)425 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
426 int32_t bufferWidth, int32_t bufferHeight) {
427 sp<GraphicBuffer> buffer =
428 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
429 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
430 BufferUsage::COMPOSER_OVERLAY,
431 "test");
432 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
433 Transaction().setBuffer(layer, buffer).apply();
434 }
435
fillLayerColor(uint32_t mLayerType,const sp<SurfaceControl> & layer,const Color & color,int32_t bufferWidth,int32_t bufferHeight)436 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
437 int32_t bufferWidth, int32_t bufferHeight) {
438 switch (mLayerType) {
439 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
440 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
441 break;
442 case ISurfaceComposerClient::eFXSurfaceBufferState:
443 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
444 break;
445 default:
446 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
447 }
448 }
449
fillLayerQuadrant(uint32_t mLayerType,const sp<SurfaceControl> & layer,int32_t bufferWidth,int32_t bufferHeight,const Color & topLeft,const Color & topRight,const Color & bottomLeft,const Color & bottomRight)450 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
451 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
452 const Color& topRight, const Color& bottomLeft,
453 const Color& bottomRight) {
454 switch (mLayerType) {
455 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
456 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
457 bottomLeft, bottomRight);
458 break;
459 case ISurfaceComposerClient::eFXSurfaceBufferState:
460 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
461 bottomLeft, bottomRight);
462 break;
463 default:
464 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
465 }
466 }
467
fillBufferQueueLayerQuadrant(const sp<SurfaceControl> & layer,int32_t bufferWidth,int32_t bufferHeight,const Color & topLeft,const Color & topRight,const Color & bottomLeft,const Color & bottomRight)468 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
469 int32_t bufferHeight, const Color& topLeft,
470 const Color& topRight, const Color& bottomLeft,
471 const Color& bottomRight) {
472 ANativeWindow_Buffer buffer;
473 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
474 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
475
476 const int32_t halfW = bufferWidth / 2;
477 const int32_t halfH = bufferHeight / 2;
478 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
479 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
480 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
481 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
482 bottomRight);
483
484 postBufferQueueLayerBuffer(layer);
485 }
486
fillBufferStateLayerQuadrant(const sp<SurfaceControl> & layer,int32_t bufferWidth,int32_t bufferHeight,const Color & topLeft,const Color & topRight,const Color & bottomLeft,const Color & bottomRight)487 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
488 int32_t bufferHeight, const Color& topLeft,
489 const Color& topRight, const Color& bottomLeft,
490 const Color& bottomRight) {
491 sp<GraphicBuffer> buffer =
492 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
493 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
494 BufferUsage::COMPOSER_OVERLAY,
495 "test");
496
497 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
498
499 const int32_t halfW = bufferWidth / 2;
500 const int32_t halfH = bufferHeight / 2;
501 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
502 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
503 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
504 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
505
506 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
507 }
508
screenshot()509 std::unique_ptr<ScreenCapture> screenshot() {
510 std::unique_ptr<ScreenCapture> screenshot;
511 ScreenCapture::captureScreen(&screenshot);
512 return screenshot;
513 }
514
asTransaction(const std::function<void (Transaction &)> & exec)515 void asTransaction(const std::function<void(Transaction&)>& exec) {
516 Transaction t;
517 exec(t);
518 t.apply(true);
519 }
520
getBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence)521 static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
522 static BufferGenerator bufferGenerator;
523 return bufferGenerator.get(outBuffer, outFence);
524 }
525
526 sp<SurfaceComposerClient> mClient;
527
528 sp<IBinder> mDisplay;
529 uint32_t mDisplayWidth;
530 uint32_t mDisplayHeight;
531 uint32_t mDisplayLayerStack;
532 Rect mDisplayRect = Rect::INVALID_RECT;
533
534 // leave room for ~256 layers
535 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
536
537 sp<SurfaceControl> mBlackBgSurface;
538 bool mColorManagementUsed;
539
540 private:
SetUpDisplay()541 void SetUpDisplay() {
542 mDisplay = mClient->getInternalDisplayToken();
543 ASSERT_FALSE(mDisplay == nullptr) << "failed to get display";
544
545 // get display width/height
546 DisplayInfo info;
547 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplay, &info));
548 mDisplayWidth = info.w;
549 mDisplayHeight = info.h;
550 mDisplayRect =
551 Rect(static_cast<int32_t>(mDisplayWidth), static_cast<int32_t>(mDisplayHeight));
552
553 // After a new buffer is queued, SurfaceFlinger is notified and will
554 // latch the new buffer on next vsync. Let's heuristically wait for 3
555 // vsyncs.
556 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
557
558 mDisplayLayerStack = 0;
559
560 mBlackBgSurface =
561 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
562 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
563
564 // set layer stack (b/68888219)
565 Transaction t;
566 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
567 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
568 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
569 t.setColor(mBlackBgSurface, half3{0, 0, 0});
570 t.setLayer(mBlackBgSurface, mLayerZBase);
571 t.apply();
572 }
573
waitForLayerBuffers()574 void waitForLayerBuffers() {
575 // Request an empty transaction to get applied synchronously to ensure the buffer is
576 // latched.
577 Transaction().apply(true);
578 usleep(mBufferPostDelay);
579 }
580
581 int32_t mBufferPostDelay;
582
583 friend class LayerRenderPathTestHarness;
584 };
585 enum class RenderPath { SCREENSHOT, VIRTUAL_DISPLAY };
586
587 class LayerRenderPathTestHarness {
588 public:
LayerRenderPathTestHarness(LayerTransactionTest * delegate,RenderPath renderPath)589 LayerRenderPathTestHarness(LayerTransactionTest* delegate, RenderPath renderPath)
590 : mDelegate(delegate), mRenderPath(renderPath) {}
591
getScreenCapture()592 std::unique_ptr<ScreenCapture> getScreenCapture() {
593 switch (mRenderPath) {
594 case RenderPath::SCREENSHOT:
595 return mDelegate->screenshot();
596 case RenderPath::VIRTUAL_DISPLAY:
597
598 const auto mainDisplay = SurfaceComposerClient::getInternalDisplayToken();
599 DisplayInfo mainDisplayInfo;
600 SurfaceComposerClient::getDisplayInfo(mainDisplay, &mainDisplayInfo);
601
602 sp<IBinder> vDisplay;
603 sp<IGraphicBufferProducer> producer;
604 sp<IGraphicBufferConsumer> consumer;
605 sp<BufferItemConsumer> itemConsumer;
606 BufferQueue::createBufferQueue(&producer, &consumer);
607
608 consumer->setConsumerName(String8("Virtual disp consumer"));
609 consumer->setDefaultBufferSize(mainDisplayInfo.w, mainDisplayInfo.h);
610
611 itemConsumer = new BufferItemConsumer(consumer,
612 // Sample usage bits from screenrecord
613 GRALLOC_USAGE_HW_VIDEO_ENCODER |
614 GRALLOC_USAGE_SW_READ_OFTEN);
615
616 vDisplay = SurfaceComposerClient::createDisplay(String8("VirtualDisplay"),
617 false /*secure*/);
618
619 SurfaceComposerClient::Transaction t;
620 t.setDisplaySurface(vDisplay, producer);
621 t.setDisplayLayerStack(vDisplay, 0);
622 t.setDisplayProjection(vDisplay, mainDisplayInfo.orientation,
623 Rect(mainDisplayInfo.viewportW, mainDisplayInfo.viewportH),
624 Rect(mainDisplayInfo.w, mainDisplayInfo.h));
625 t.apply();
626 SurfaceComposerClient::Transaction().apply(true);
627 BufferItem item;
628 itemConsumer->acquireBuffer(&item, 0, true);
629 auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer);
630 itemConsumer->releaseBuffer(item);
631 SurfaceComposerClient::destroyDisplay(vDisplay);
632 return sc;
633 }
634 }
635
636 protected:
637 LayerTransactionTest* mDelegate;
638 RenderPath mRenderPath;
639 };
640
641 class LayerTypeTransactionHarness : public LayerTransactionTest {
642 public:
LayerTypeTransactionHarness(uint32_t layerType)643 LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {}
644
createLayer(const char * name,uint32_t width,uint32_t height,uint32_t flags=0,SurfaceControl * parent=nullptr)645 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
646 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
647 // if the flags already have a layer type specified, return an error
648 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
649 return nullptr;
650 }
651 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent);
652 }
653
fillLayerColor(const sp<SurfaceControl> & layer,const Color & color,int32_t bufferWidth,int32_t bufferHeight)654 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
655 int32_t bufferHeight) {
656 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
657 bufferWidth, bufferHeight));
658 }
659
fillLayerQuadrant(const sp<SurfaceControl> & layer,int32_t bufferWidth,int32_t bufferHeight,const Color & topLeft,const Color & topRight,const Color & bottomLeft,const Color & bottomRight)660 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
661 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
662 const Color& bottomLeft, const Color& bottomRight) {
663 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
664 bufferWidth, bufferHeight,
665 topLeft, topRight,
666 bottomLeft, bottomRight));
667 }
668
669 protected:
670 uint32_t mLayerType;
671 };
672
673 class LayerTypeTransactionTest : public LayerTypeTransactionHarness,
674 public ::testing::WithParamInterface<uint32_t> {
675 public:
LayerTypeTransactionTest()676 LayerTypeTransactionTest() : LayerTypeTransactionHarness(GetParam()) {}
677 };
678
679 class LayerTypeAndRenderTypeTransactionTest
680 : public LayerTypeTransactionHarness,
681 public ::testing::WithParamInterface<std::tuple<uint32_t, RenderPath>> {
682 public:
LayerTypeAndRenderTypeTransactionTest()683 LayerTypeAndRenderTypeTransactionTest()
684 : LayerTypeTransactionHarness(std::get<0>(GetParam())),
685 mRenderPathHarness(LayerRenderPathTestHarness(this, std::get<1>(GetParam()))) {}
686
getScreenCapture()687 std::unique_ptr<ScreenCapture> getScreenCapture() {
688 return mRenderPathHarness.getScreenCapture();
689 }
690
691 protected:
692 LayerRenderPathTestHarness mRenderPathHarness;
693 };
694
695 // Environment for starting up binder threads. This is required for testing
696 // virtual displays, as BufferQueue parameters may be queried over binder.
697 class BinderEnvironment : public ::testing::Environment {
698 public:
SetUp()699 void SetUp() override { ProcessState::self()->startThreadPool(); }
700 };
701
702 ::testing::Environment* const binderEnv =
703 ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
704
705 class LayerRenderTypeTransactionTest : public LayerTransactionTest,
706 public ::testing::WithParamInterface<RenderPath> {
707 public:
LayerRenderTypeTransactionTest()708 LayerRenderTypeTransactionTest() : mHarness(LayerRenderPathTestHarness(this, GetParam())) {}
709
getScreenCapture()710 std::unique_ptr<ScreenCapture> getScreenCapture() { return mHarness.getScreenCapture(); }
711 void setRelativeZBasicHelper(uint32_t layerType);
712 void setRelativeZGroupHelper(uint32_t layerType);
713 void setAlphaBasicHelper(uint32_t layerType);
714 void setBackgroundColorHelper(uint32_t layerType, bool priorColor, bool bufferFill, float alpha,
715 Color finalColor);
716
717 protected:
718 LayerRenderPathTestHarness mHarness;
719 };
720
721 INSTANTIATE_TEST_CASE_P(
722 LayerTypeAndRenderTypeTransactionTests, LayerTypeAndRenderTypeTransactionTest,
723 ::testing::Combine(
724 ::testing::Values(
725 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
726 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)),
727 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT)));
728
729 INSTANTIATE_TEST_CASE_P(LayerRenderTypeTransactionTests, LayerRenderTypeTransactionTest,
730 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT));
731
732 INSTANTIATE_TEST_CASE_P(
733 LayerTypeTransactionTests, LayerTypeTransactionTest,
734 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
735 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
736
TEST_P(LayerRenderTypeTransactionTest,SetPositionBasic_BufferQueue)737 TEST_P(LayerRenderTypeTransactionTest, SetPositionBasic_BufferQueue) {
738 sp<SurfaceControl> layer;
739 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
740 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
741
742 {
743 SCOPED_TRACE("default position");
744 const Rect rect(0, 0, 32, 32);
745 auto shot = getScreenCapture();
746 shot->expectColor(rect, Color::RED);
747 shot->expectBorder(rect, Color::BLACK);
748 }
749
750 Transaction().setPosition(layer, 5, 10).apply();
751 {
752 SCOPED_TRACE("new position");
753 const Rect rect(5, 10, 37, 42);
754 auto shot = getScreenCapture();
755 shot->expectColor(rect, Color::RED);
756 shot->expectBorder(rect, Color::BLACK);
757 }
758 }
759
TEST_P(LayerRenderTypeTransactionTest,SetPositionRounding_BufferQueue)760 TEST_P(LayerRenderTypeTransactionTest, SetPositionRounding_BufferQueue) {
761 sp<SurfaceControl> layer;
762 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
763 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
764
765 // GLES requires only 4 bits of subpixel precision during rasterization
766 // XXX GLES composition does not match HWC composition due to precision
767 // loss (b/69315223)
768 const float epsilon = 1.0f / 16.0f;
769 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
770 {
771 SCOPED_TRACE("rounding down");
772 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
773 }
774
775 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
776 {
777 SCOPED_TRACE("rounding up");
778 getScreenCapture()->expectColor(Rect(1, 1, 33, 33), Color::RED);
779 }
780 }
781
TEST_P(LayerRenderTypeTransactionTest,SetPositionOutOfBounds_BufferQueue)782 TEST_P(LayerRenderTypeTransactionTest, SetPositionOutOfBounds_BufferQueue) {
783 sp<SurfaceControl> layer;
784 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
785 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
786
787 Transaction().setPosition(layer, -32, -32).apply();
788 {
789 SCOPED_TRACE("negative coordinates");
790 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
791 }
792
793 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
794 {
795 SCOPED_TRACE("positive coordinates");
796 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
797 }
798 }
799
TEST_P(LayerRenderTypeTransactionTest,SetPositionPartiallyOutOfBounds_BufferQueue)800 TEST_P(LayerRenderTypeTransactionTest, SetPositionPartiallyOutOfBounds_BufferQueue) {
801 sp<SurfaceControl> layer;
802 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
803 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
804
805 // partially out of bounds
806 Transaction().setPosition(layer, -30, -30).apply();
807 {
808 SCOPED_TRACE("negative coordinates");
809 getScreenCapture()->expectColor(Rect(0, 0, 2, 2), Color::RED);
810 }
811
812 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
813 {
814 SCOPED_TRACE("positive coordinates");
815 getScreenCapture()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
816 mDisplayHeight),
817 Color::RED);
818 }
819 }
820
TEST_P(LayerRenderTypeTransactionTest,SetPositionWithResize_BufferQueue)821 TEST_P(LayerRenderTypeTransactionTest, SetPositionWithResize_BufferQueue) {
822 sp<SurfaceControl> layer;
823 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
824 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
825
826 // setPosition is applied immediately by default, with or without resize
827 // pending
828 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
829 {
830 SCOPED_TRACE("resize pending");
831 auto shot = getScreenCapture();
832 const Rect rect(5, 10, 37, 42);
833 shot->expectColor(rect, Color::RED);
834 shot->expectBorder(rect, Color::BLACK);
835 }
836
837 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
838 {
839 SCOPED_TRACE("resize applied");
840 getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
841 }
842 }
843
TEST_P(LayerRenderTypeTransactionTest,SetPositionWithNextResize_BufferQueue)844 TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResize_BufferQueue) {
845 sp<SurfaceControl> layer;
846 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
847 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
848
849 // request setPosition to be applied with the next resize
850 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
851 {
852 SCOPED_TRACE("new position pending");
853 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
854 }
855
856 Transaction().setPosition(layer, 15, 20).apply();
857 {
858 SCOPED_TRACE("pending new position modified");
859 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
860 }
861
862 Transaction().setSize(layer, 64, 64).apply();
863 {
864 SCOPED_TRACE("resize pending");
865 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
866 }
867
868 // finally resize and latch the buffer
869 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
870 {
871 SCOPED_TRACE("new position applied");
872 getScreenCapture()->expectColor(Rect(15, 20, 79, 84), Color::RED);
873 }
874 }
875
TEST_P(LayerRenderTypeTransactionTest,SetPositionWithNextResizeScaleToWindow_BufferQueue)876 TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
877 sp<SurfaceControl> layer;
878 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
879 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
880
881 // setPosition is not immediate even with SCALE_TO_WINDOW override
882 Transaction()
883 .setPosition(layer, 5, 10)
884 .setSize(layer, 64, 64)
885 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
886 .setGeometryAppliesWithResize(layer)
887 .apply();
888 {
889 SCOPED_TRACE("new position pending");
890 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
891 }
892
893 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
894 {
895 SCOPED_TRACE("new position applied");
896 getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
897 }
898 }
899
TEST_P(LayerRenderTypeTransactionTest,SetSizeBasic_BufferQueue)900 TEST_P(LayerRenderTypeTransactionTest, SetSizeBasic_BufferQueue) {
901 sp<SurfaceControl> layer;
902 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
903 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
904
905 Transaction().setSize(layer, 64, 64).apply();
906 {
907 SCOPED_TRACE("resize pending");
908 auto shot = getScreenCapture();
909 const Rect rect(0, 0, 32, 32);
910 shot->expectColor(rect, Color::RED);
911 shot->expectBorder(rect, Color::BLACK);
912 }
913
914 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
915 {
916 SCOPED_TRACE("resize applied");
917 auto shot = getScreenCapture();
918 const Rect rect(0, 0, 64, 64);
919 shot->expectColor(rect, Color::RED);
920 shot->expectBorder(rect, Color::BLACK);
921 }
922 }
923
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetSizeInvalid)924 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetSizeInvalid) {
925 // cannot test robustness against invalid sizes (zero or really huge)
926 }
927
TEST_P(LayerRenderTypeTransactionTest,SetSizeWithScaleToWindow_BufferQueue)928 TEST_P(LayerRenderTypeTransactionTest, SetSizeWithScaleToWindow_BufferQueue) {
929 sp<SurfaceControl> layer;
930 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
931 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
932
933 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
934 Transaction()
935 .setSize(layer, 64, 64)
936 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
937 .apply();
938 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
939 }
940
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetZBasic)941 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZBasic) {
942 sp<SurfaceControl> layerR;
943 sp<SurfaceControl> layerG;
944 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
945 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
946 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
947 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
948
949 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
950 {
951 SCOPED_TRACE("layerR");
952 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
953 }
954
955 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
956 {
957 SCOPED_TRACE("layerG");
958 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
959 }
960 }
961
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetZNegative)962 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZNegative) {
963 sp<SurfaceControl> parent =
964 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
965 ISurfaceComposerClient::eFXSurfaceContainer);
966 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
967 sp<SurfaceControl> layerR;
968 sp<SurfaceControl> layerG;
969 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
970 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
971 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
972 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
973
974 Transaction()
975 .reparent(layerR, parent->getHandle())
976 .reparent(layerG, parent->getHandle())
977 .apply();
978 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
979 {
980 SCOPED_TRACE("layerR");
981 auto shot = getScreenCapture();
982 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
983 }
984
985 Transaction().setLayer(layerR, -3).apply();
986 {
987 SCOPED_TRACE("layerG");
988 auto shot = getScreenCapture();
989 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
990 }
991 }
992
setRelativeZBasicHelper(uint32_t layerType)993 void LayerRenderTypeTransactionTest::setRelativeZBasicHelper(uint32_t layerType) {
994 sp<SurfaceControl> layerR;
995 sp<SurfaceControl> layerG;
996 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32, layerType));
997 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
998 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32, layerType));
999 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
1000
1001 switch (layerType) {
1002 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1003 Transaction()
1004 .setPosition(layerG, 16, 16)
1005 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1006 .apply();
1007 break;
1008 case ISurfaceComposerClient::eFXSurfaceBufferState:
1009 Transaction()
1010 .setFrame(layerR, Rect(0, 0, 32, 32))
1011 .setFrame(layerG, Rect(16, 16, 48, 48))
1012 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1013 .apply();
1014 break;
1015 default:
1016 ASSERT_FALSE(true) << "Unsupported layer type";
1017 }
1018 {
1019 SCOPED_TRACE("layerG above");
1020 auto shot = getScreenCapture();
1021 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1022 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
1023 }
1024
1025 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
1026 {
1027 SCOPED_TRACE("layerG below");
1028 auto shot = getScreenCapture();
1029 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1030 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
1031 }
1032 }
1033
TEST_P(LayerRenderTypeTransactionTest,SetRelativeZBasic_BufferQueue)1034 TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferQueue) {
1035 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1036 }
1037
TEST_P(LayerRenderTypeTransactionTest,SetRelativeZBasic_BufferState)1038 TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferState) {
1039 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1040 }
1041
TEST_P(LayerTypeTransactionTest,SetRelativeZNegative)1042 TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
1043 sp<SurfaceControl> parent =
1044 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
1045 ISurfaceComposerClient::eFXSurfaceContainer);
1046 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
1047 sp<SurfaceControl> layerR;
1048 sp<SurfaceControl> layerG;
1049 sp<SurfaceControl> layerB;
1050 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1051 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
1052 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
1053 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
1054 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
1055 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
1056
1057 Transaction()
1058 .reparent(layerB, parent->getHandle())
1059 .apply();
1060
1061 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
1062 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
1063
1064 std::unique_ptr<ScreenCapture> screenshot;
1065 // only layerB is in this range
1066 sp<IBinder> parentHandle = parent->getHandle();
1067 ScreenCapture::captureLayers(&screenshot, parentHandle, Rect(0, 0, 32, 32));
1068 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
1069 }
1070
TEST_P(LayerTypeTransactionTest,SetLayerAndRelative)1071 TEST_P(LayerTypeTransactionTest, SetLayerAndRelative) {
1072 sp<SurfaceControl> parent =
1073 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
1074 ISurfaceComposerClient::eFXSurfaceColor);
1075
1076 sp<SurfaceControl> childLayer;
1077 ASSERT_NO_FATAL_FAILURE(
1078 childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
1079 0 /* buffer height */,
1080 ISurfaceComposerClient::eFXSurfaceColor,
1081 parent.get()));
1082 Transaction()
1083 .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
1084 .setColor(parent, half3{0.0f, 0.0f, 0.0f})
1085 .show(childLayer)
1086 .show(parent)
1087 .setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight))
1088 .setCrop_legacy(childLayer, Rect(0, 0, 20, 30))
1089 .apply();
1090
1091 Transaction()
1092 .setRelativeLayer(childLayer, parent->getHandle(), -1)
1093 .setLayer(childLayer, 1)
1094 .apply();
1095
1096 {
1097 SCOPED_TRACE("setLayer above");
1098 // Set layer should get applied and place the child above.
1099 std::unique_ptr<ScreenCapture> screenshot;
1100 ScreenCapture::captureScreen(&screenshot);
1101 screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
1102 }
1103
1104 Transaction()
1105 .setLayer(childLayer, 1)
1106 .setRelativeLayer(childLayer, parent->getHandle(), -1)
1107 .apply();
1108
1109 {
1110 SCOPED_TRACE("setRelative below");
1111 // Set relative layer should get applied and place the child below.
1112 std::unique_ptr<ScreenCapture> screenshot;
1113 ScreenCapture::captureScreen(&screenshot);
1114 screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
1115 }
1116 }
1117
TEST_P(LayerTypeTransactionTest,HideRelativeParentHidesLayer)1118 TEST_P(LayerTypeTransactionTest, HideRelativeParentHidesLayer) {
1119 sp<SurfaceControl> parent =
1120 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
1121 ISurfaceComposerClient::eFXSurfaceColor);
1122 sp<SurfaceControl> relativeParent =
1123 LayerTransactionTest::createLayer("RelativeParent", 0 /* buffer width */,
1124 0 /* buffer height */, ISurfaceComposerClient::eFXSurfaceColor);
1125
1126 sp<SurfaceControl> childLayer;
1127 ASSERT_NO_FATAL_FAILURE(
1128 childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
1129 0 /* buffer height */,
1130 ISurfaceComposerClient::eFXSurfaceColor,
1131 parent.get()));
1132 Transaction()
1133 .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
1134 .setColor(parent, half3{0.0f, 0.0f, 0.0f})
1135 .setColor(relativeParent, half3{0.0f, 1.0f, 0.0f})
1136 .show(childLayer)
1137 .show(parent)
1138 .show(relativeParent)
1139 .setLayer(parent, mLayerZBase - 1)
1140 .setLayer(relativeParent, mLayerZBase)
1141 .apply();
1142
1143 Transaction()
1144 .setRelativeLayer(childLayer, relativeParent->getHandle(), 1)
1145 .apply();
1146
1147 {
1148 SCOPED_TRACE("setLayer above");
1149 // Set layer should get applied and place the child above.
1150 std::unique_ptr<ScreenCapture> screenshot;
1151 ScreenCapture::captureScreen(&screenshot);
1152 screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
1153 }
1154
1155 Transaction()
1156 .hide(relativeParent)
1157 .apply();
1158
1159 {
1160 SCOPED_TRACE("hide relative parent");
1161 // The relative should no longer be visible.
1162 std::unique_ptr<ScreenCapture> screenshot;
1163 ScreenCapture::captureScreen(&screenshot);
1164 screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
1165 }
1166 }
1167
setRelativeZGroupHelper(uint32_t layerType)1168 void LayerRenderTypeTransactionTest::setRelativeZGroupHelper(uint32_t layerType) {
1169 sp<SurfaceControl> layerR;
1170 sp<SurfaceControl> layerG;
1171 sp<SurfaceControl> layerB;
1172 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test", 32, 32, layerType));
1173 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
1174 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test", 32, 32, layerType));
1175 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
1176 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test", 32, 32, layerType));
1177 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerB, Color::BLUE, 32, 32));
1178
1179 // layerR = 0, layerG = layerR + 3, layerB = 2
1180 switch (layerType) {
1181 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1182 Transaction()
1183 .setPosition(layerG, 8, 8)
1184 .setRelativeLayer(layerG, layerR->getHandle(), 3)
1185 .setPosition(layerB, 16, 16)
1186 .setLayer(layerB, mLayerZBase + 2)
1187 .apply();
1188 break;
1189 case ISurfaceComposerClient::eFXSurfaceBufferState:
1190 Transaction()
1191 .setFrame(layerR, Rect(0, 0, 32, 32))
1192 .setFrame(layerG, Rect(8, 8, 40, 40))
1193 .setRelativeLayer(layerG, layerR->getHandle(), 3)
1194 .setFrame(layerB, Rect(16, 16, 48, 48))
1195 .setLayer(layerB, mLayerZBase + 2)
1196 .apply();
1197 break;
1198 default:
1199 ASSERT_FALSE(true) << "Unsupported layer type";
1200 }
1201
1202 {
1203 SCOPED_TRACE("(layerR < layerG) < layerB");
1204 auto shot = getScreenCapture();
1205 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
1206 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
1207 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
1208 }
1209
1210 // layerR = 4, layerG = layerR + 3, layerB = 2
1211 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
1212 {
1213 SCOPED_TRACE("layerB < (layerR < layerG)");
1214 auto shot = getScreenCapture();
1215 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
1216 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
1217 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
1218 }
1219
1220 // layerR = 4, layerG = layerR - 3, layerB = 2
1221 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
1222 {
1223 SCOPED_TRACE("layerB < (layerG < layerR)");
1224 auto shot = getScreenCapture();
1225 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1226 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
1227 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
1228 }
1229
1230 // restore to absolute z
1231 // layerR = 4, layerG = 0, layerB = 2
1232 Transaction().setLayer(layerG, mLayerZBase).apply();
1233 {
1234 SCOPED_TRACE("layerG < layerB < layerR");
1235 auto shot = getScreenCapture();
1236 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1237 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
1238 }
1239
1240 // layerR should not affect layerG anymore
1241 // layerR = 1, layerG = 0, layerB = 2
1242 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
1243 {
1244 SCOPED_TRACE("layerG < layerR < layerB");
1245 auto shot = getScreenCapture();
1246 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
1247 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
1248 }
1249 }
1250
TEST_P(LayerRenderTypeTransactionTest,SetRelativeZGroup_BufferQueue)1251 TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferQueue) {
1252 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1253 }
1254
TEST_P(LayerRenderTypeTransactionTest,SetRelativeZGroup_BufferState)1255 TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferState) {
1256 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1257 }
1258
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetRelativeZBug64572777)1259 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetRelativeZBug64572777) {
1260 sp<SurfaceControl> layerR;
1261 sp<SurfaceControl> layerG;
1262
1263 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1264 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
1265 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
1266 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
1267
1268 Transaction()
1269 .setPosition(layerG, 16, 16)
1270 .setRelativeLayer(layerG, layerR->getHandle(), 1)
1271 .apply();
1272
1273 layerG.clear();
1274 // layerG should have been removed
1275 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1276 }
1277
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetFlagsHidden)1278 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsHidden) {
1279 sp<SurfaceControl> layer;
1280 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1281 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
1282
1283 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
1284 {
1285 SCOPED_TRACE("layer hidden");
1286 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
1287 }
1288
1289 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1290 {
1291 SCOPED_TRACE("layer shown");
1292 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1293 }
1294 }
1295
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetFlagsOpaque)1296 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsOpaque) {
1297 const Color translucentRed = {100, 0, 0, 100};
1298 sp<SurfaceControl> layerR;
1299 sp<SurfaceControl> layerG;
1300 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1301 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
1302 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
1303 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
1304
1305 Transaction()
1306 .setLayer(layerR, mLayerZBase + 1)
1307 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1308 .apply();
1309 {
1310 SCOPED_TRACE("layerR opaque");
1311 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
1312 }
1313
1314 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1315 {
1316 SCOPED_TRACE("layerR translucent");
1317 const uint8_t g = uint8_t(255 - translucentRed.a);
1318 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
1319 }
1320 }
1321
TEST_P(LayerTypeTransactionTest,SetFlagsSecure)1322 TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
1323 sp<SurfaceControl> layer;
1324 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1325 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
1326
1327 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
1328 sp<GraphicBuffer> outBuffer;
1329 Transaction()
1330 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1331 .apply(true);
1332 ASSERT_EQ(PERMISSION_DENIED,
1333 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1334
1335 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1336 ASSERT_EQ(NO_ERROR,
1337 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1338 }
1339
1340 /** RAII Wrapper around get/seteuid */
1341 class UIDFaker {
1342 uid_t oldId;
1343 public:
UIDFaker(uid_t uid)1344 UIDFaker(uid_t uid) {
1345 oldId = geteuid();
1346 seteuid(uid);
1347 }
~UIDFaker()1348 ~UIDFaker() {
1349 seteuid(oldId);
1350 }
1351 };
1352
TEST_F(LayerTransactionTest,SetFlagsSecureEUidSystem)1353 TEST_F(LayerTransactionTest, SetFlagsSecureEUidSystem) {
1354 sp<SurfaceControl> layer;
1355 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1356 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
1357
1358 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
1359 sp<GraphicBuffer> outBuffer;
1360 Transaction()
1361 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1362 .apply(true);
1363 ASSERT_EQ(PERMISSION_DENIED,
1364 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1365
1366 UIDFaker f(AID_SYSTEM);
1367
1368 // By default the system can capture screenshots with secure layers but they
1369 // will be blacked out
1370 ASSERT_EQ(NO_ERROR,
1371 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1372
1373 {
1374 SCOPED_TRACE("as system");
1375 auto shot = screenshot();
1376 shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1377 }
1378
1379 // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
1380 // to receive them...we are expected to take care with the results.
1381 bool outCapturedSecureLayers;
1382 ASSERT_EQ(NO_ERROR,
1383 composer->captureScreen(mDisplay, &outBuffer, outCapturedSecureLayers,
1384 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(), 0,
1385 0, false, ISurfaceComposer::eRotateNone, true));
1386 ASSERT_EQ(true, outCapturedSecureLayers);
1387 ScreenCapture sc(outBuffer);
1388 sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
1389 }
1390
TEST_P(LayerRenderTypeTransactionTest,SetTransparentRegionHintBasic_BufferQueue)1391 TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
1392 const Rect top(0, 0, 32, 16);
1393 const Rect bottom(0, 16, 32, 32);
1394 sp<SurfaceControl> layer;
1395 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1396
1397 ANativeWindow_Buffer buffer;
1398 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1399 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1400 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
1401 // setTransparentRegionHint always applies to the following buffer
1402 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
1403 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
1404 {
1405 SCOPED_TRACE("top transparent");
1406 auto shot = getScreenCapture();
1407 shot->expectColor(top, Color::BLACK);
1408 shot->expectColor(bottom, Color::RED);
1409 }
1410
1411 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1412 {
1413 SCOPED_TRACE("transparent region hint pending");
1414 auto shot = getScreenCapture();
1415 shot->expectColor(top, Color::BLACK);
1416 shot->expectColor(bottom, Color::RED);
1417 }
1418
1419 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1420 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1421 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1422 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
1423 {
1424 SCOPED_TRACE("bottom transparent");
1425 auto shot = getScreenCapture();
1426 shot->expectColor(top, Color::RED);
1427 shot->expectColor(bottom, Color::BLACK);
1428 }
1429 }
1430
TEST_P(LayerRenderTypeTransactionTest,SetTransparentRegionHintBasic_BufferState)1431 TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferState) {
1432 const Rect top(0, 0, 32, 16);
1433 const Rect bottom(0, 16, 32, 32);
1434 sp<SurfaceControl> layer;
1435 ASSERT_NO_FATAL_FAILURE(
1436 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1437
1438 sp<GraphicBuffer> buffer =
1439 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1440 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1441 BufferUsage::COMPOSER_OVERLAY,
1442 "test");
1443
1444 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1445 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1446 Transaction()
1447 .setTransparentRegionHint(layer, Region(top))
1448 .setBuffer(layer, buffer)
1449 .setFrame(layer, Rect(0, 0, 32, 32))
1450 .apply();
1451 {
1452 SCOPED_TRACE("top transparent");
1453 auto shot = getScreenCapture();
1454 shot->expectColor(top, Color::BLACK);
1455 shot->expectColor(bottom, Color::RED);
1456 }
1457
1458 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1459 {
1460 SCOPED_TRACE("transparent region hint intermediate");
1461 auto shot = getScreenCapture();
1462 shot->expectColor(top, Color::BLACK);
1463 shot->expectColor(bottom, Color::BLACK);
1464 }
1465
1466 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1467 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1468 BufferUsage::COMPOSER_OVERLAY,
1469 "test");
1470
1471 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1472 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
1473 Transaction().setBuffer(layer, buffer).apply();
1474 {
1475 SCOPED_TRACE("bottom transparent");
1476 auto shot = getScreenCapture();
1477 shot->expectColor(top, Color::RED);
1478 shot->expectColor(bottom, Color::BLACK);
1479 }
1480 }
1481
TEST_P(LayerRenderTypeTransactionTest,SetTransparentRegionHintOutOfBounds_BufferQueue)1482 TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferQueue) {
1483 sp<SurfaceControl> layerTransparent;
1484 sp<SurfaceControl> layerR;
1485 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1486 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1487
1488 // check that transparent region hint is bound by the layer size
1489 Transaction()
1490 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
1491 .setPosition(layerR, 16, 16)
1492 .setLayer(layerR, mLayerZBase + 1)
1493 .apply();
1494 ASSERT_NO_FATAL_FAILURE(
1495 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1496 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerR, Color::RED, 32, 32));
1497 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1498 }
1499
TEST_P(LayerRenderTypeTransactionTest,SetTransparentRegionHintOutOfBounds_BufferState)1500 TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferState) {
1501 sp<SurfaceControl> layerTransparent;
1502 sp<SurfaceControl> layerR;
1503 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1504 ASSERT_NO_FATAL_FAILURE(
1505 layerR = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1506
1507 // check that transparent region hint is bound by the layer size
1508 Transaction()
1509 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
1510 .setFrame(layerR, Rect(16, 16, 48, 48))
1511 .setLayer(layerR, mLayerZBase + 1)
1512 .apply();
1513 ASSERT_NO_FATAL_FAILURE(
1514 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1515 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layerR, Color::RED, 32, 32));
1516 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
1517 }
1518
setAlphaBasicHelper(uint32_t layerType)1519 void LayerRenderTypeTransactionTest::setAlphaBasicHelper(uint32_t layerType) {
1520 sp<SurfaceControl> layer1;
1521 sp<SurfaceControl> layer2;
1522 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32, layerType));
1523 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32, layerType));
1524 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer1, {64, 0, 0, 255}, 32, 32));
1525 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer2, {0, 64, 0, 255}, 32, 32));
1526
1527 switch (layerType) {
1528 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1529 Transaction()
1530 .setAlpha(layer1, 0.25f)
1531 .setAlpha(layer2, 0.75f)
1532 .setPosition(layer2, 16, 0)
1533 .setLayer(layer2, mLayerZBase + 1)
1534 .apply();
1535 break;
1536 case ISurfaceComposerClient::eFXSurfaceBufferState:
1537 Transaction()
1538 .setAlpha(layer1, 0.25f)
1539 .setAlpha(layer2, 0.75f)
1540 .setFrame(layer1, Rect(0, 0, 32, 32))
1541 .setFrame(layer2, Rect(16, 0, 48, 32))
1542 .setLayer(layer2, mLayerZBase + 1)
1543 .apply();
1544 break;
1545 default:
1546 ASSERT_FALSE(true) << "Unsupported layer type";
1547 }
1548 {
1549 auto shot = getScreenCapture();
1550 uint8_t r = 16; // 64 * 0.25f
1551 uint8_t g = 48; // 64 * 0.75f
1552 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1553 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1554
1555 r /= 4; // r * (1.0f - 0.75f)
1556 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1557 }
1558 }
1559
TEST_P(LayerRenderTypeTransactionTest,SetAlphaBasic_BufferQueue)1560 TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferQueue) {
1561 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1562 }
1563
TEST_P(LayerRenderTypeTransactionTest,SetAlphaBasic_BufferState)1564 TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferState) {
1565 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1566 }
1567
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetAlphaClamped)1568 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetAlphaClamped) {
1569 const Color color = {64, 0, 0, 255};
1570 sp<SurfaceControl> layer;
1571 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1572 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
1573
1574 Transaction().setAlpha(layer, 2.0f).apply();
1575 {
1576 SCOPED_TRACE("clamped to 1.0f");
1577 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), color);
1578 }
1579
1580 Transaction().setAlpha(layer, -1.0f).apply();
1581 {
1582 SCOPED_TRACE("clamped to 0.0f");
1583 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1584 }
1585 }
1586
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadius)1587 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadius) {
1588 sp<SurfaceControl> layer;
1589 const uint8_t size = 64;
1590 const uint8_t testArea = 4;
1591 const float cornerRadius = 20.0f;
1592 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1593 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1594
1595 Transaction()
1596 .setCornerRadius(layer, cornerRadius)
1597 .apply();
1598 {
1599 const uint8_t bottom = size - 1;
1600 const uint8_t right = size - 1;
1601 auto shot = getScreenCapture();
1602 // Transparent corners
1603 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
1604 shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
1605 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1606 shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
1607 }
1608 }
1609
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetCornerRadiusChildCrop)1610 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildCrop) {
1611 sp<SurfaceControl> parent;
1612 sp<SurfaceControl> child;
1613 const uint8_t size = 64;
1614 const uint8_t testArea = 4;
1615 const float cornerRadius = 20.0f;
1616 ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
1617 ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
1618 ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
1619 ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
1620
1621 Transaction()
1622 .setCornerRadius(parent, cornerRadius)
1623 .reparent(child, parent->getHandle())
1624 .setPosition(child, 0, size / 2)
1625 .apply();
1626 {
1627 const uint8_t bottom = size - 1;
1628 const uint8_t right = size - 1;
1629 auto shot = getScreenCapture();
1630 // Top edge of child should not have rounded corners because it's translated in the parent
1631 shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
1632 Color::GREEN);
1633 // But bottom edges should have been clipped according to parent bounds
1634 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1635 shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
1636 }
1637 }
1638
TEST_P(LayerRenderTypeTransactionTest,SetColorBasic)1639 TEST_P(LayerRenderTypeTransactionTest, SetColorBasic) {
1640 sp<SurfaceControl> bufferLayer;
1641 sp<SurfaceControl> colorLayer;
1642 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1643 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
1644 ASSERT_NO_FATAL_FAILURE(colorLayer =
1645 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1646 ISurfaceComposerClient::eFXSurfaceColor));
1647
1648 Transaction()
1649 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1650 .setLayer(colorLayer, mLayerZBase + 1)
1651 .apply();
1652
1653 {
1654 SCOPED_TRACE("default color");
1655 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1656 }
1657
1658 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1659 const Color expected = {15, 51, 85, 255};
1660 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1661 // channel) should be less than one
1662 const uint8_t tolerance = 1;
1663 Transaction().setColor(colorLayer, color).apply();
1664 {
1665 SCOPED_TRACE("new color");
1666 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
1667 }
1668 }
1669
1670 // RED: Color layer base color and BufferQueueLayer/BufferStateLayer fill
1671 // BLUE: prior background color
1672 // GREEN: final background color
1673 // BLACK: no color or fill
setBackgroundColorHelper(uint32_t layerType,bool priorColor,bool bufferFill,float alpha,Color finalColor)1674 void LayerRenderTypeTransactionTest::setBackgroundColorHelper(uint32_t layerType, bool priorColor,
1675 bool bufferFill, float alpha,
1676 Color finalColor) {
1677 sp<SurfaceControl> layer;
1678 int32_t width = 500;
1679 int32_t height = 500;
1680
1681 Color fillColor = Color::RED;
1682 Color priorBgColor = Color::BLUE;
1683 Color expectedColor = Color::BLACK;
1684 switch (layerType) {
1685 case ISurfaceComposerClient::eFXSurfaceColor:
1686 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 0, 0, layerType));
1687 Transaction()
1688 .setCrop_legacy(layer, Rect(0, 0, width, height))
1689 .setColor(layer, half3(1.0f, 0, 0))
1690 .apply();
1691 expectedColor = fillColor;
1692 break;
1693 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1694 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
1695 if (bufferFill) {
1696 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, fillColor, width, height));
1697 expectedColor = fillColor;
1698 }
1699 Transaction().setCrop_legacy(layer, Rect(0, 0, width, height)).apply();
1700 break;
1701 case ISurfaceComposerClient::eFXSurfaceBufferState:
1702 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height, layerType));
1703 if (bufferFill) {
1704 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, fillColor, width, height));
1705 expectedColor = fillColor;
1706 }
1707 Transaction().setFrame(layer, Rect(0, 0, width, height)).apply();
1708 break;
1709 default:
1710 GTEST_FAIL() << "Unknown layer type in setBackgroundColorHelper";
1711 return;
1712 }
1713
1714 if (priorColor && layerType != ISurfaceComposerClient::eFXSurfaceColor) {
1715 Transaction()
1716 .setBackgroundColor(layer, half3(0, 0, 1.0f), 1.0f, ui::Dataspace::UNKNOWN)
1717 .apply();
1718 if (!bufferFill) {
1719 expectedColor = priorBgColor;
1720 }
1721 }
1722
1723 {
1724 SCOPED_TRACE("default before setting background color layer");
1725 screenshot()->expectColor(Rect(0, 0, width, height), expectedColor);
1726 }
1727 Transaction()
1728 .setBackgroundColor(layer, half3(0, 1.0f, 0), alpha, ui::Dataspace::UNKNOWN)
1729 .apply();
1730
1731 {
1732 auto shot = screenshot();
1733 shot->expectColor(Rect(0, 0, width, height), finalColor);
1734 shot->expectBorder(Rect(0, 0, width, height), Color::BLACK);
1735 }
1736 }
1737
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_Color_NoEffect)1738 TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_Color_NoEffect) {
1739 bool priorColor = false;
1740 bool bufferFill = false;
1741 float alpha = 1.0f;
1742 Color finalColor = Color::RED;
1743 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceColor,
1744 priorColor, bufferFill, alpha, finalColor));
1745 }
1746
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferQueue_BufferFill_NoPriorColor_Basic)1747 TEST_P(LayerRenderTypeTransactionTest,
1748 SetBackgroundColor_BufferQueue_BufferFill_NoPriorColor_Basic) {
1749 bool priorColor = false;
1750 bool bufferFill = true;
1751 float alpha = 1.0f;
1752 Color finalColor = Color::RED;
1753 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1754 priorColor, bufferFill, alpha, finalColor));
1755 }
1756
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferQueue_NoBufferFill_NoPriorColor_Basic)1757 TEST_P(LayerRenderTypeTransactionTest,
1758 SetBackgroundColor_BufferQueue_NoBufferFill_NoPriorColor_Basic) {
1759 bool priorColor = false;
1760 bool bufferFill = false;
1761 float alpha = 1.0f;
1762 Color finalColor = Color::GREEN;
1763 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1764 priorColor, bufferFill, alpha, finalColor));
1765 }
1766
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferQueue_BufferFill_PriorColor_Basic)1767 TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_BufferQueue_BufferFill_PriorColor_Basic) {
1768 bool priorColor = true;
1769 bool bufferFill = true;
1770 float alpha = 1.0f;
1771 Color finalColor = Color::RED;
1772 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1773 priorColor, bufferFill, alpha, finalColor));
1774 }
1775
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferQueue_NoBufferFill_PriorColor_Basic)1776 TEST_P(LayerRenderTypeTransactionTest,
1777 SetBackgroundColor_BufferQueue_NoBufferFill_PriorColor_Basic) {
1778 bool priorColor = true;
1779 bool bufferFill = false;
1780 float alpha = 1.0f;
1781 Color finalColor = Color::GREEN;
1782 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1783 priorColor, bufferFill, alpha, finalColor));
1784 }
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferQueue_NoPriorColor_ZeroAlpha_NoEffect)1785 TEST_P(LayerRenderTypeTransactionTest,
1786 SetBackgroundColor_BufferQueue_NoPriorColor_ZeroAlpha_NoEffect) {
1787 bool priorColor = false;
1788 bool bufferFill = false;
1789 float alpha = 0;
1790 Color finalColor = Color::BLACK;
1791 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1792 priorColor, bufferFill, alpha, finalColor));
1793 }
1794
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferQueue_PriorColor_ZeroAlpha_DeleteBackground)1795 TEST_P(LayerRenderTypeTransactionTest,
1796 SetBackgroundColor_BufferQueue_PriorColor_ZeroAlpha_DeleteBackground) {
1797 bool priorColor = true;
1798 bool bufferFill = false;
1799 float alpha = 0;
1800 Color finalColor = Color::BLACK;
1801 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1802 priorColor, bufferFill, alpha, finalColor));
1803 }
1804
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferState_BufferFill_NoPriorColor_Basic)1805 TEST_P(LayerRenderTypeTransactionTest,
1806 SetBackgroundColor_BufferState_BufferFill_NoPriorColor_Basic) {
1807 bool priorColor = false;
1808 bool bufferFill = true;
1809 float alpha = 1.0f;
1810 Color finalColor = Color::RED;
1811 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
1812 priorColor, bufferFill, alpha, finalColor));
1813 }
1814
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferState_NoBufferFill_NoPriorColor_Basic)1815 TEST_P(LayerRenderTypeTransactionTest,
1816 SetBackgroundColor_BufferState_NoBufferFill_NoPriorColor_Basic) {
1817 bool priorColor = false;
1818 bool bufferFill = false;
1819 float alpha = 1.0f;
1820 Color finalColor = Color::GREEN;
1821 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
1822 priorColor, bufferFill, alpha, finalColor));
1823 }
1824
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferState_NoBufferFill_PriorColor_Basic)1825 TEST_P(LayerRenderTypeTransactionTest,
1826 SetBackgroundColor_BufferState_NoBufferFill_PriorColor_Basic) {
1827 bool priorColor = true;
1828 bool bufferFill = false;
1829 float alpha = 1.0f;
1830 Color finalColor = Color::GREEN;
1831 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
1832 priorColor, bufferFill, alpha, finalColor));
1833 }
1834
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferState_NoPriorColor_ZeroAlpha_NoEffect)1835 TEST_P(LayerRenderTypeTransactionTest,
1836 SetBackgroundColor_BufferState_NoPriorColor_ZeroAlpha_NoEffect) {
1837 bool priorColor = false;
1838 bool bufferFill = false;
1839 float alpha = 0;
1840 Color finalColor = Color::BLACK;
1841 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
1842 priorColor, bufferFill, alpha, finalColor));
1843 }
1844
TEST_P(LayerRenderTypeTransactionTest,SetBackgroundColor_BufferState_PriorColor_ZeroAlpha_DeleteBackground)1845 TEST_P(LayerRenderTypeTransactionTest,
1846 SetBackgroundColor_BufferState_PriorColor_ZeroAlpha_DeleteBackground) {
1847 bool priorColor = true;
1848 bool bufferFill = false;
1849 float alpha = 0;
1850 Color finalColor = Color::BLACK;
1851 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
1852 priorColor, bufferFill, alpha, finalColor));
1853 }
1854
TEST_P(LayerRenderTypeTransactionTest,SetColorClamped)1855 TEST_P(LayerRenderTypeTransactionTest, SetColorClamped) {
1856 sp<SurfaceControl> colorLayer;
1857 ASSERT_NO_FATAL_FAILURE(colorLayer =
1858 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1859 ISurfaceComposerClient::eFXSurfaceColor));
1860 Transaction()
1861 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1862 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1863 .apply();
1864
1865 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1866 }
1867
TEST_P(LayerRenderTypeTransactionTest,SetColorWithAlpha)1868 TEST_P(LayerRenderTypeTransactionTest, SetColorWithAlpha) {
1869 sp<SurfaceControl> bufferLayer;
1870 sp<SurfaceControl> colorLayer;
1871 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1872 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
1873 ASSERT_NO_FATAL_FAILURE(colorLayer =
1874 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1875 ISurfaceComposerClient::eFXSurfaceColor));
1876 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
1877
1878 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1879 const float alpha = 0.25f;
1880 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1881 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1882 // channel) should be less than one
1883 const uint8_t tolerance = 1;
1884 Transaction()
1885 .setColor(colorLayer, color)
1886 .setAlpha(colorLayer, alpha)
1887 .setLayer(colorLayer, mLayerZBase + 1)
1888 .apply();
1889 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1890 tolerance);
1891 }
1892
TEST_P(LayerRenderTypeTransactionTest,SetColorWithParentAlpha_Bug74220420)1893 TEST_P(LayerRenderTypeTransactionTest, SetColorWithParentAlpha_Bug74220420) {
1894 sp<SurfaceControl> bufferLayer;
1895 sp<SurfaceControl> parentLayer;
1896 sp<SurfaceControl> colorLayer;
1897 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1898 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
1899 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
1900 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1901 0 /* buffer height */,
1902 ISurfaceComposerClient::eFXSurfaceColor));
1903 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
1904 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1905 const float alpha = 0.25f;
1906 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1907 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1908 // channel) should be less than one
1909 const uint8_t tolerance = 1;
1910 Transaction()
1911 .reparent(colorLayer, parentLayer->getHandle())
1912 .setColor(colorLayer, color)
1913 .setAlpha(parentLayer, alpha)
1914 .setLayer(parentLayer, mLayerZBase + 1)
1915 .apply();
1916 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1917 tolerance);
1918 }
1919
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetColorWithBuffer)1920 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetColorWithBuffer) {
1921 sp<SurfaceControl> bufferLayer;
1922 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
1923 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
1924
1925 // color is ignored
1926 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
1927 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1928 }
1929
TEST_P(LayerTypeAndRenderTypeTransactionTest,SetLayerStackBasic)1930 TEST_P(LayerTypeAndRenderTypeTransactionTest, SetLayerStackBasic) {
1931 sp<SurfaceControl> layer;
1932 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1933 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
1934
1935 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1936 {
1937 SCOPED_TRACE("non-existing layer stack");
1938 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
1939 }
1940
1941 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1942 {
1943 SCOPED_TRACE("original layer stack");
1944 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
1945 }
1946 }
1947
TEST_P(LayerRenderTypeTransactionTest,SetMatrixBasic_BufferQueue)1948 TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferQueue) {
1949 sp<SurfaceControl> layer;
1950 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1951 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1952 Color::BLUE, Color::WHITE));
1953
1954 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1955 {
1956 SCOPED_TRACE("IDENTITY");
1957 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1958 Color::BLUE, Color::WHITE);
1959 }
1960
1961 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1962 {
1963 SCOPED_TRACE("FLIP_H");
1964 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED,
1965 Color::WHITE, Color::BLUE);
1966 }
1967
1968 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1969 {
1970 SCOPED_TRACE("FLIP_V");
1971 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE,
1972 Color::RED, Color::GREEN);
1973 }
1974
1975 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1976 {
1977 SCOPED_TRACE("ROT_90");
1978 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED,
1979 Color::WHITE, Color::GREEN);
1980 }
1981
1982 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1983 {
1984 SCOPED_TRACE("SCALE");
1985 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN,
1986 Color::BLUE, Color::WHITE, true /* filtered */);
1987 }
1988 }
1989
TEST_P(LayerRenderTypeTransactionTest,SetMatrixBasic_BufferState)1990 TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferState) {
1991 sp<SurfaceControl> layer;
1992 ASSERT_NO_FATAL_FAILURE(
1993 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1994 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1995 Color::BLUE, Color::WHITE));
1996
1997 Transaction()
1998 .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
1999 .setFrame(layer, Rect(0, 0, 32, 32))
2000 .apply();
2001 {
2002 SCOPED_TRACE("IDENTITY");
2003 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
2004 Color::BLUE, Color::WHITE);
2005 }
2006
2007 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
2008 {
2009 SCOPED_TRACE("FLIP_H");
2010 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
2011 Color::BLUE, Color::WHITE);
2012 }
2013
2014 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
2015 {
2016 SCOPED_TRACE("FLIP_V");
2017 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
2018 Color::BLUE, Color::WHITE);
2019 }
2020
2021 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
2022 {
2023 SCOPED_TRACE("ROT_90");
2024 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
2025 Color::BLUE, Color::WHITE);
2026 }
2027
2028 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
2029 {
2030 SCOPED_TRACE("SCALE");
2031 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
2032 Color::BLUE, Color::WHITE);
2033 }
2034 }
2035
TEST_P(LayerRenderTypeTransactionTest,SetMatrixRot45_BufferQueue)2036 TEST_P(LayerRenderTypeTransactionTest, SetMatrixRot45_BufferQueue) {
2037 sp<SurfaceControl> layer;
2038 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2039 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2040 Color::BLUE, Color::WHITE));
2041
2042 const float rot = M_SQRT1_2; // 45 degrees
2043 const float trans = M_SQRT2 * 16.0f;
2044 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
2045
2046 auto shot = getScreenCapture();
2047 // check a 8x8 region inside each color
2048 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
2049 const int32_t halfL = 4;
2050 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
2051 };
2052 const int32_t unit = int32_t(trans / 2);
2053 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
2054 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
2055 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
2056 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
2057 }
2058
TEST_P(LayerRenderTypeTransactionTest,SetMatrixWithResize_BufferQueue)2059 TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithResize_BufferQueue) {
2060 sp<SurfaceControl> layer;
2061 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2062 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2063
2064 // setMatrix is applied after any pending resize, unlike setPosition
2065 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
2066 {
2067 SCOPED_TRACE("resize pending");
2068 auto shot = getScreenCapture();
2069 const Rect rect(0, 0, 32, 32);
2070 shot->expectColor(rect, Color::RED);
2071 shot->expectBorder(rect, Color::BLACK);
2072 }
2073
2074 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
2075 {
2076 SCOPED_TRACE("resize applied");
2077 const Rect rect(0, 0, 128, 128);
2078 getScreenCapture()->expectColor(rect, Color::RED);
2079 }
2080 }
2081
TEST_P(LayerRenderTypeTransactionTest,SetMatrixWithScaleToWindow_BufferQueue)2082 TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
2083 sp<SurfaceControl> layer;
2084 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2085 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2086
2087 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
2088 Transaction()
2089 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
2090 .setSize(layer, 64, 64)
2091 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
2092 .apply();
2093 getScreenCapture()->expectColor(Rect(0, 0, 128, 128), Color::RED);
2094 }
2095
TEST_P(LayerRenderTypeTransactionTest,SetOverrideScalingModeBasic_BufferQueue)2096 TEST_P(LayerRenderTypeTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
2097 sp<SurfaceControl> layer;
2098 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2099 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2100 Color::BLUE, Color::WHITE));
2101
2102 // XXX SCALE_CROP is not respected; calling setSize and
2103 // setOverrideScalingMode in separate transactions does not work
2104 // (b/69315456)
2105 Transaction()
2106 .setSize(layer, 64, 16)
2107 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
2108 .apply();
2109 {
2110 SCOPED_TRACE("SCALE_TO_WINDOW");
2111 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN,
2112 Color::BLUE, Color::WHITE, true /* filtered */);
2113 }
2114 }
2115
TEST_P(LayerTypeTransactionTest,RefreshRateIsInitialized)2116 TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
2117 sp<SurfaceControl> layer;
2118 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2119
2120 sp<IBinder> handle = layer->getHandle();
2121 ASSERT_TRUE(handle != nullptr);
2122
2123 FrameStats frameStats;
2124 mClient->getLayerFrameStats(handle, &frameStats);
2125
2126 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
2127 }
2128
TEST_P(LayerRenderTypeTransactionTest,SetCropBasic_BufferQueue)2129 TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferQueue) {
2130 sp<SurfaceControl> layer;
2131 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2132 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2133 const Rect crop(8, 8, 24, 24);
2134
2135 Transaction().setCrop_legacy(layer, crop).apply();
2136 auto shot = getScreenCapture();
2137 shot->expectColor(crop, Color::RED);
2138 shot->expectBorder(crop, Color::BLACK);
2139 }
2140
TEST_P(LayerRenderTypeTransactionTest,SetCropBasic_BufferState)2141 TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferState) {
2142 sp<SurfaceControl> layer;
2143 ASSERT_NO_FATAL_FAILURE(
2144 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2145 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2146 const Rect crop(8, 8, 24, 24);
2147
2148 Transaction().setCrop(layer, crop).apply();
2149 auto shot = getScreenCapture();
2150 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2151 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2152 }
2153
TEST_P(LayerRenderTypeTransactionTest,SetCropEmpty_BufferQueue)2154 TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferQueue) {
2155 sp<SurfaceControl> layer;
2156 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2157 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2158
2159 {
2160 SCOPED_TRACE("empty rect");
2161 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
2162 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
2163 }
2164
2165 {
2166 SCOPED_TRACE("negative rect");
2167 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
2168 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
2169 }
2170 }
2171
TEST_P(LayerRenderTypeTransactionTest,SetCropEmpty_BufferState)2172 TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferState) {
2173 sp<SurfaceControl> layer;
2174 ASSERT_NO_FATAL_FAILURE(
2175 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2176 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2177
2178 {
2179 SCOPED_TRACE("empty rect");
2180 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
2181 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2182 }
2183
2184 {
2185 SCOPED_TRACE("negative rect");
2186 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
2187 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2188 }
2189 }
2190
TEST_P(LayerRenderTypeTransactionTest,SetCropOutOfBounds_BufferQueue)2191 TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferQueue) {
2192 sp<SurfaceControl> layer;
2193 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2194 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2195
2196 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
2197 auto shot = getScreenCapture();
2198 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
2199 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2200 }
2201
TEST_P(LayerRenderTypeTransactionTest,SetCropOutOfBounds_BufferState)2202 TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferState) {
2203 sp<SurfaceControl> layer;
2204 ASSERT_NO_FATAL_FAILURE(
2205 layer = createLayer("test", 32, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2206 sp<GraphicBuffer> buffer =
2207 new GraphicBuffer(32, 64, PIXEL_FORMAT_RGBA_8888, 1,
2208 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2209 BufferUsage::COMPOSER_OVERLAY,
2210 "test");
2211 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 16), Color::BLUE);
2212 fillGraphicBufferColor(buffer, Rect(0, 16, 32, 64), Color::RED);
2213
2214 Transaction().setFrame(layer, Rect(0, 0, 64, 64)).apply();
2215
2216 Transaction().setBuffer(layer, buffer).apply();
2217
2218 // Partially out of bounds in the negative (upper left) direction
2219 Transaction().setCrop(layer, Rect(-128, -128, 32, 16)).apply();
2220 {
2221 SCOPED_TRACE("out of bounds, negative (upper left) direction");
2222 auto shot = getScreenCapture();
2223 shot->expectColor(Rect(0, 0, 64, 64), Color::BLUE);
2224 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
2225 }
2226
2227 // Partially out of bounds in the positive (lower right) direction
2228 Transaction().setCrop(layer, Rect(0, 16, 128, 128)).apply();
2229 {
2230 SCOPED_TRACE("out of bounds, positive (lower right) direction");
2231 auto shot = getScreenCapture();
2232 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2233 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
2234 }
2235
2236 // Fully out of buffer space bounds
2237 Transaction().setCrop(layer, Rect(-128, -128, -1, -1)).apply();
2238 {
2239 SCOPED_TRACE("Fully out of bounds");
2240 auto shot = getScreenCapture();
2241 shot->expectColor(Rect(0, 0, 64, 16), Color::BLUE);
2242 shot->expectColor(Rect(0, 16, 64, 64), Color::RED);
2243 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
2244 }
2245 }
2246
TEST_P(LayerRenderTypeTransactionTest,SetCropWithTranslation_BufferQueue)2247 TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferQueue) {
2248 sp<SurfaceControl> layer;
2249 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2250 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2251
2252 const Point position(32, 32);
2253 const Rect crop(8, 8, 24, 24);
2254 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
2255 auto shot = getScreenCapture();
2256 shot->expectColor(crop + position, Color::RED);
2257 shot->expectBorder(crop + position, Color::BLACK);
2258 }
2259
TEST_P(LayerRenderTypeTransactionTest,SetCropWithTranslation_BufferState)2260 TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferState) {
2261 sp<SurfaceControl> layer;
2262 ASSERT_NO_FATAL_FAILURE(
2263 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2264 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2265
2266 const Rect frame(32, 32, 64, 64);
2267 const Rect crop(8, 8, 24, 24);
2268 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
2269 auto shot = getScreenCapture();
2270 shot->expectColor(frame, Color::RED);
2271 shot->expectBorder(frame, Color::BLACK);
2272 }
2273
TEST_P(LayerRenderTypeTransactionTest,SetCropWithScale_BufferQueue)2274 TEST_P(LayerRenderTypeTransactionTest, SetCropWithScale_BufferQueue) {
2275 sp<SurfaceControl> layer;
2276 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2277 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2278
2279 // crop_legacy is affected by matrix
2280 Transaction()
2281 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
2282 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
2283 .apply();
2284 auto shot = getScreenCapture();
2285 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2286 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2287 }
2288
TEST_P(LayerRenderTypeTransactionTest,SetCropWithResize_BufferQueue)2289 TEST_P(LayerRenderTypeTransactionTest, SetCropWithResize_BufferQueue) {
2290 sp<SurfaceControl> layer;
2291 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2292 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2293
2294 // setCrop_legacy is applied immediately by default, with or without resize pending
2295 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
2296 {
2297 SCOPED_TRACE("resize pending");
2298 auto shot = getScreenCapture();
2299 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
2300 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
2301 }
2302
2303 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
2304 {
2305 SCOPED_TRACE("resize applied");
2306 auto shot = getScreenCapture();
2307 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
2308 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
2309 }
2310 }
2311
TEST_P(LayerRenderTypeTransactionTest,SetCropWithNextResize_BufferQueue)2312 TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResize_BufferQueue) {
2313 sp<SurfaceControl> layer;
2314 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2315 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2316
2317 // request setCrop_legacy to be applied with the next resize
2318 Transaction()
2319 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
2320 .setGeometryAppliesWithResize(layer)
2321 .apply();
2322 {
2323 SCOPED_TRACE("waiting for next resize");
2324 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
2325 }
2326
2327 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
2328 {
2329 SCOPED_TRACE("pending crop modified");
2330 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
2331 }
2332
2333 Transaction().setSize(layer, 16, 16).apply();
2334 {
2335 SCOPED_TRACE("resize pending");
2336 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
2337 }
2338
2339 // finally resize
2340 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
2341 {
2342 SCOPED_TRACE("new crop applied");
2343 auto shot = getScreenCapture();
2344 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2345 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2346 }
2347 }
2348
TEST_P(LayerRenderTypeTransactionTest,SetCropWithNextResizeScaleToWindow_BufferQueue)2349 TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
2350 sp<SurfaceControl> layer;
2351 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2352 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2353
2354 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
2355 Transaction()
2356 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
2357 .setSize(layer, 16, 16)
2358 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
2359 .setGeometryAppliesWithResize(layer)
2360 .apply();
2361 {
2362 SCOPED_TRACE("new crop pending");
2363 auto shot = getScreenCapture();
2364 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
2365 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
2366 }
2367
2368 // XXX crop is never latched without other geometry change (b/69315677)
2369 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
2370 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
2371 Transaction().setPosition(layer, 0, 0).apply();
2372 {
2373 SCOPED_TRACE("new crop applied");
2374 auto shot = getScreenCapture();
2375 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2376 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2377 }
2378 }
2379
TEST_P(LayerRenderTypeTransactionTest,SetFrameBasic_BufferState)2380 TEST_P(LayerRenderTypeTransactionTest, SetFrameBasic_BufferState) {
2381 sp<SurfaceControl> layer;
2382 ASSERT_NO_FATAL_FAILURE(
2383 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2384 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2385 const Rect frame(8, 8, 24, 24);
2386
2387 Transaction().setFrame(layer, frame).apply();
2388 auto shot = getScreenCapture();
2389 shot->expectColor(frame, Color::RED);
2390 shot->expectBorder(frame, Color::BLACK);
2391 }
2392
TEST_P(LayerRenderTypeTransactionTest,SetFrameEmpty_BufferState)2393 TEST_P(LayerRenderTypeTransactionTest, SetFrameEmpty_BufferState) {
2394 sp<SurfaceControl> layer;
2395 ASSERT_NO_FATAL_FAILURE(
2396 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2397 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2398
2399 {
2400 SCOPED_TRACE("empty rect");
2401 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
2402 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2403 }
2404
2405 {
2406 SCOPED_TRACE("negative rect");
2407 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
2408 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2409 }
2410 }
2411
TEST_P(LayerRenderTypeTransactionTest,SetFrameDefaultParentless_BufferState)2412 TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultParentless_BufferState) {
2413 sp<SurfaceControl> layer;
2414 ASSERT_NO_FATAL_FAILURE(
2415 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2416 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
2417
2418 // A parentless layer will default to a frame with the same size as the buffer
2419 auto shot = getScreenCapture();
2420 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2421 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2422 }
2423
TEST_P(LayerRenderTypeTransactionTest,SetFrameDefaultBSParent_BufferState)2424 TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBSParent_BufferState) {
2425 sp<SurfaceControl> parent, child;
2426 ASSERT_NO_FATAL_FAILURE(
2427 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2428 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2429 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2430
2431 ASSERT_NO_FATAL_FAILURE(
2432 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2433 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2434
2435 Transaction().reparent(child, parent->getHandle()).apply();
2436
2437 // A layer will default to the frame of its parent
2438 auto shot = getScreenCapture();
2439 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2440 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2441 }
2442
TEST_P(LayerRenderTypeTransactionTest,SetFrameDefaultBQParent_BufferState)2443 TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBQParent_BufferState) {
2444 sp<SurfaceControl> parent, child;
2445 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
2446 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
2447
2448 ASSERT_NO_FATAL_FAILURE(
2449 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2450 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2451
2452 Transaction().reparent(child, parent->getHandle()).apply();
2453
2454 // A layer will default to the frame of its parent
2455 auto shot = getScreenCapture();
2456 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2457 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2458 }
2459
TEST_P(LayerRenderTypeTransactionTest,SetFrameUpdate_BufferState)2460 TEST_P(LayerRenderTypeTransactionTest, SetFrameUpdate_BufferState) {
2461 sp<SurfaceControl> layer;
2462 ASSERT_NO_FATAL_FAILURE(
2463 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2464 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2465 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
2466
2467 std::this_thread::sleep_for(500ms);
2468
2469 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
2470
2471 auto shot = getScreenCapture();
2472 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2473 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2474 }
2475
TEST_P(LayerRenderTypeTransactionTest,SetFrameOutsideBounds_BufferState)2476 TEST_P(LayerRenderTypeTransactionTest, SetFrameOutsideBounds_BufferState) {
2477 sp<SurfaceControl> parent, child;
2478 ASSERT_NO_FATAL_FAILURE(
2479 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2480 ASSERT_NO_FATAL_FAILURE(
2481 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2482 Transaction().reparent(child, parent->getHandle()).apply();
2483
2484 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2485 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2486
2487 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2488 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
2489
2490 auto shot = getScreenCapture();
2491 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
2492 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
2493 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2494 }
2495
TEST_P(LayerRenderTypeTransactionTest,SetBufferBasic_BufferState)2496 TEST_P(LayerRenderTypeTransactionTest, SetBufferBasic_BufferState) {
2497 sp<SurfaceControl> layer;
2498 ASSERT_NO_FATAL_FAILURE(
2499 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2500
2501 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2502
2503 auto shot = getScreenCapture();
2504 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2505 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2506 }
2507
TEST_P(LayerRenderTypeTransactionTest,SetBufferMultipleBuffers_BufferState)2508 TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleBuffers_BufferState) {
2509 sp<SurfaceControl> layer;
2510 ASSERT_NO_FATAL_FAILURE(
2511 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2512
2513 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2514
2515 {
2516 SCOPED_TRACE("set buffer 1");
2517 auto shot = getScreenCapture();
2518 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2519 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2520 }
2521
2522 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
2523
2524 {
2525 SCOPED_TRACE("set buffer 2");
2526 auto shot = getScreenCapture();
2527 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLUE);
2528 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2529 }
2530
2531 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2532
2533 {
2534 SCOPED_TRACE("set buffer 3");
2535 auto shot = getScreenCapture();
2536 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2537 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2538 }
2539 }
2540
TEST_P(LayerRenderTypeTransactionTest,SetBufferMultipleLayers_BufferState)2541 TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleLayers_BufferState) {
2542 sp<SurfaceControl> layer1;
2543 ASSERT_NO_FATAL_FAILURE(
2544 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2545
2546 sp<SurfaceControl> layer2;
2547 ASSERT_NO_FATAL_FAILURE(
2548 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2549
2550 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2551
2552 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
2553 {
2554 SCOPED_TRACE("set layer 1 buffer red");
2555 auto shot = getScreenCapture();
2556 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2557 }
2558
2559 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2560
2561 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
2562 {
2563 SCOPED_TRACE("set layer 2 buffer blue");
2564 auto shot = getScreenCapture();
2565 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2566 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2567 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2568 }
2569
2570 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2571 {
2572 SCOPED_TRACE("set layer 1 buffer green");
2573 auto shot = getScreenCapture();
2574 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2575 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2576 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2577 }
2578
2579 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2580
2581 {
2582 SCOPED_TRACE("set layer 2 buffer white");
2583 auto shot = getScreenCapture();
2584 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2585 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2586 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2587 }
2588 }
2589
TEST_P(LayerRenderTypeTransactionTest,SetBufferCaching_BufferState)2590 TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_BufferState) {
2591 sp<SurfaceControl> layer;
2592 ASSERT_NO_FATAL_FAILURE(
2593 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2594
2595 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2596
2597 std::array<sp<GraphicBuffer>, 10> buffers;
2598
2599 size_t idx = 0;
2600 for (auto& buffer : buffers) {
2601 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2602 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2603 BufferUsage::COMPOSER_OVERLAY,
2604 "test");
2605 Color color = colors[idx % colors.size()];
2606 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2607 idx++;
2608 }
2609
2610 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2611 // cache is working.
2612 idx = 0;
2613 for (auto& buffer : buffers) {
2614 for (int i = 0; i < 2; i++) {
2615 Transaction().setBuffer(layer, buffer).apply();
2616
2617 Color color = colors[idx % colors.size()];
2618 auto shot = screenshot();
2619 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2620 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2621 }
2622 idx++;
2623 }
2624 }
2625
TEST_P(LayerRenderTypeTransactionTest,SetBufferCaching_LeastRecentlyUsed_BufferState)2626 TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_LeastRecentlyUsed_BufferState) {
2627 sp<SurfaceControl> layer;
2628 ASSERT_NO_FATAL_FAILURE(
2629 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2630
2631 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2632
2633 std::array<sp<GraphicBuffer>, 70> buffers;
2634
2635 size_t idx = 0;
2636 for (auto& buffer : buffers) {
2637 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2638 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2639 BufferUsage::COMPOSER_OVERLAY,
2640 "test");
2641 Color color = colors[idx % colors.size()];
2642 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2643 idx++;
2644 }
2645
2646 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2647 // cache is working.
2648 idx = 0;
2649 for (auto& buffer : buffers) {
2650 for (int i = 0; i < 2; i++) {
2651 Transaction().setBuffer(layer, buffer).apply();
2652
2653 Color color = colors[idx % colors.size()];
2654 auto shot = screenshot();
2655 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2656 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2657 }
2658 idx++;
2659 }
2660 }
2661
TEST_P(LayerRenderTypeTransactionTest,SetBufferCaching_DestroyedBuffer_BufferState)2662 TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_DestroyedBuffer_BufferState) {
2663 sp<SurfaceControl> layer;
2664 ASSERT_NO_FATAL_FAILURE(
2665 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2666
2667 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2668
2669 std::array<sp<GraphicBuffer>, 65> buffers;
2670
2671 size_t idx = 0;
2672 for (auto& buffer : buffers) {
2673 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2674 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2675 BufferUsage::COMPOSER_OVERLAY,
2676 "test");
2677 Color color = colors[idx % colors.size()];
2678 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2679 idx++;
2680 }
2681
2682 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2683 // cache is working.
2684 idx = 0;
2685 for (auto& buffer : buffers) {
2686 for (int i = 0; i < 2; i++) {
2687 Transaction().setBuffer(layer, buffer).apply();
2688
2689 Color color = colors[idx % colors.size()];
2690 auto shot = screenshot();
2691 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2692 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2693 }
2694 if (idx == 0) {
2695 buffers[0].clear();
2696 }
2697 idx++;
2698 }
2699 }
2700
TEST_P(LayerRenderTypeTransactionTest,SetTransformRotate90_BufferState)2701 TEST_P(LayerRenderTypeTransactionTest, SetTransformRotate90_BufferState) {
2702 sp<SurfaceControl> layer;
2703 ASSERT_NO_FATAL_FAILURE(
2704 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2705
2706 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2707 Color::BLUE, Color::WHITE));
2708
2709 Transaction()
2710 .setFrame(layer, Rect(0, 0, 32, 32))
2711 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2712 .apply();
2713
2714 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2715 Color::GREEN, true /* filtered */);
2716 }
2717
TEST_P(LayerRenderTypeTransactionTest,SetTransformFlipH_BufferState)2718 TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipH_BufferState) {
2719 sp<SurfaceControl> layer;
2720 ASSERT_NO_FATAL_FAILURE(
2721 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2722
2723 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2724 Color::BLUE, Color::WHITE));
2725
2726 Transaction()
2727 .setFrame(layer, Rect(0, 0, 32, 32))
2728 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2729 .apply();
2730
2731 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2732 Color::BLUE, true /* filtered */);
2733 }
2734
TEST_P(LayerRenderTypeTransactionTest,SetTransformFlipV_BufferState)2735 TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipV_BufferState) {
2736 sp<SurfaceControl> layer;
2737 ASSERT_NO_FATAL_FAILURE(
2738 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2739
2740 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2741 Color::BLUE, Color::WHITE));
2742
2743 Transaction()
2744 .setFrame(layer, Rect(0, 0, 32, 32))
2745 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2746 .apply();
2747
2748 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2749 Color::GREEN, true /* filtered */);
2750 }
2751
TEST_F(LayerTransactionTest,SetTransformToDisplayInverse_BufferState)2752 TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2753 sp<SurfaceControl> layer;
2754 ASSERT_NO_FATAL_FAILURE(
2755 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2756
2757 Transaction().setTransformToDisplayInverse(layer, false).apply();
2758
2759 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2760
2761 Transaction().setTransformToDisplayInverse(layer, true).apply();
2762 }
2763
TEST_P(LayerRenderTypeTransactionTest,SetFenceBasic_BufferState)2764 TEST_P(LayerRenderTypeTransactionTest, SetFenceBasic_BufferState) {
2765 sp<SurfaceControl> layer;
2766 Transaction transaction;
2767 ASSERT_NO_FATAL_FAILURE(
2768 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2769
2770 sp<GraphicBuffer> buffer =
2771 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2772 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2773 BufferUsage::COMPOSER_OVERLAY,
2774 "test");
2775 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2776
2777 sp<Fence> fence;
2778 if (getBuffer(nullptr, &fence) != NO_ERROR) {
2779 GTEST_SUCCEED() << "test not supported";
2780 return;
2781 }
2782
2783 Transaction().setBuffer(layer, buffer).setAcquireFence(layer, fence).apply();
2784
2785 status_t status = fence->wait(1000);
2786 ASSERT_NE(static_cast<status_t>(Fence::Status::Unsignaled), status);
2787 std::this_thread::sleep_for(200ms);
2788
2789 auto shot = getScreenCapture();
2790 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2791 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2792 }
2793
TEST_P(LayerRenderTypeTransactionTest,SetFenceNull_BufferState)2794 TEST_P(LayerRenderTypeTransactionTest, SetFenceNull_BufferState) {
2795 sp<SurfaceControl> layer;
2796 ASSERT_NO_FATAL_FAILURE(
2797 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2798
2799 sp<GraphicBuffer> buffer =
2800 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2801 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2802 BufferUsage::COMPOSER_OVERLAY,
2803 "test");
2804 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2805
2806 sp<Fence> fence = Fence::NO_FENCE;
2807
2808 Transaction()
2809 .setBuffer(layer, buffer)
2810 .setAcquireFence(layer, fence)
2811 .apply();
2812
2813 auto shot = getScreenCapture();
2814 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2815 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2816 }
2817
TEST_P(LayerRenderTypeTransactionTest,SetDataspaceBasic_BufferState)2818 TEST_P(LayerRenderTypeTransactionTest, SetDataspaceBasic_BufferState) {
2819 sp<SurfaceControl> layer;
2820 ASSERT_NO_FATAL_FAILURE(
2821 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2822
2823 sp<GraphicBuffer> buffer =
2824 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2825 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2826 BufferUsage::COMPOSER_OVERLAY,
2827 "test");
2828 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2829
2830 Transaction()
2831 .setBuffer(layer, buffer)
2832 .setDataspace(layer, ui::Dataspace::UNKNOWN)
2833 .apply();
2834
2835 auto shot = getScreenCapture();
2836 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2837 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2838 }
2839
TEST_P(LayerRenderTypeTransactionTest,SetHdrMetadataBasic_BufferState)2840 TEST_P(LayerRenderTypeTransactionTest, SetHdrMetadataBasic_BufferState) {
2841 sp<SurfaceControl> layer;
2842 ASSERT_NO_FATAL_FAILURE(
2843 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2844
2845 sp<GraphicBuffer> buffer =
2846 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2847 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2848 BufferUsage::COMPOSER_OVERLAY,
2849 "test");
2850 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2851
2852 HdrMetadata hdrMetadata;
2853 hdrMetadata.validTypes = 0;
2854 Transaction()
2855 .setBuffer(layer, buffer)
2856 .setHdrMetadata(layer, hdrMetadata)
2857 .apply();
2858
2859 auto shot = getScreenCapture();
2860 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2861 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2862 }
2863
TEST_P(LayerRenderTypeTransactionTest,SetSurfaceDamageRegionBasic_BufferState)2864 TEST_P(LayerRenderTypeTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
2865 sp<SurfaceControl> layer;
2866 ASSERT_NO_FATAL_FAILURE(
2867 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2868
2869 sp<GraphicBuffer> buffer =
2870 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2871 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2872 BufferUsage::COMPOSER_OVERLAY,
2873 "test");
2874 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2875
2876 Region region;
2877 region.set(32, 32);
2878 Transaction()
2879 .setBuffer(layer, buffer)
2880 .setSurfaceDamageRegion(layer, region)
2881 .apply();
2882
2883 auto shot = getScreenCapture();
2884 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2885 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2886 }
2887
TEST_P(LayerRenderTypeTransactionTest,SetApiBasic_BufferState)2888 TEST_P(LayerRenderTypeTransactionTest, SetApiBasic_BufferState) {
2889 sp<SurfaceControl> layer;
2890 ASSERT_NO_FATAL_FAILURE(
2891 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2892
2893 sp<GraphicBuffer> buffer =
2894 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2895 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2896 BufferUsage::COMPOSER_OVERLAY,
2897 "test");
2898 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2899
2900 Transaction()
2901 .setBuffer(layer, buffer)
2902 .setApi(layer, NATIVE_WINDOW_API_CPU)
2903 .apply();
2904
2905 auto shot = getScreenCapture();
2906 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2907 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
2908 }
2909
TEST_F(LayerTransactionTest,SetSidebandStreamNull_BufferState)2910 TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2911 sp<SurfaceControl> layer;
2912 ASSERT_NO_FATAL_FAILURE(
2913 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2914
2915 // verify this doesn't cause a crash
2916 Transaction().setSidebandStream(layer, nullptr).apply();
2917 }
2918
TEST_F(LayerTransactionTest,ReparentToSelf)2919 TEST_F(LayerTransactionTest, ReparentToSelf) {
2920 sp<SurfaceControl> layer;
2921 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2922 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2923 Transaction().reparent(layer, layer->getHandle()).apply();
2924
2925 {
2926 // We expect the transaction to be silently dropped, but for SurfaceFlinger
2927 // to still be functioning.
2928 SCOPED_TRACE("after reparent to self");
2929 const Rect rect(0, 0, 32, 32);
2930 auto shot = screenshot();
2931 shot->expectColor(rect, Color::RED);
2932 shot->expectBorder(rect, Color::BLACK);
2933 }
2934 }
2935
2936 class ColorTransformHelper {
2937 public:
DegammaColorSingle(half & s)2938 static void DegammaColorSingle(half& s) {
2939 if (s <= 0.03928f)
2940 s = s / 12.92f;
2941 else
2942 s = pow((s + 0.055f) / 1.055f, 2.4f);
2943 }
2944
DegammaColor(half3 & color)2945 static void DegammaColor(half3& color) {
2946 DegammaColorSingle(color.r);
2947 DegammaColorSingle(color.g);
2948 DegammaColorSingle(color.b);
2949 }
2950
GammaColorSingle(half & s)2951 static void GammaColorSingle(half& s) {
2952 if (s <= 0.0031308f) {
2953 s = s * 12.92f;
2954 } else {
2955 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
2956 }
2957 }
2958
GammaColor(half3 & color)2959 static void GammaColor(half3& color) {
2960 GammaColorSingle(color.r);
2961 GammaColorSingle(color.g);
2962 GammaColorSingle(color.b);
2963 }
2964
applyMatrix(half3 & color,const mat3 & mat)2965 static void applyMatrix(half3& color, const mat3& mat) {
2966 half3 ret = half3(0);
2967
2968 for (int i = 0; i < 3; i++) {
2969 for (int j = 0; j < 3; j++) {
2970 ret[i] = ret[i] + color[j] * mat[j][i];
2971 }
2972 }
2973 color = ret;
2974 }
2975 };
2976
TEST_P(LayerRenderTypeTransactionTest,SetColorTransformBasic)2977 TEST_P(LayerRenderTypeTransactionTest, SetColorTransformBasic) {
2978 sp<SurfaceControl> colorLayer;
2979 ASSERT_NO_FATAL_FAILURE(colorLayer =
2980 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2981 ISurfaceComposerClient::eFXSurfaceColor));
2982 Transaction()
2983 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2984 .setLayer(colorLayer, mLayerZBase + 1)
2985 .apply();
2986 {
2987 SCOPED_TRACE("default color");
2988 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
2989 }
2990
2991 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2992 half3 expected = color;
2993 mat3 matrix;
2994 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2995 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2996 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2997
2998 // degamma before applying the matrix
2999 if (mColorManagementUsed) {
3000 ColorTransformHelper::DegammaColor(expected);
3001 }
3002
3003 ColorTransformHelper::applyMatrix(expected, matrix);
3004
3005 if (mColorManagementUsed) {
3006 ColorTransformHelper::GammaColor(expected);
3007 }
3008
3009 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
3010 uint8_t(expected.b * 255), 255};
3011
3012 // this is handwavy, but the precison loss scaled by 255 (8-bit per
3013 // channel) should be less than one
3014 const uint8_t tolerance = 1;
3015
3016 Transaction().setColor(colorLayer, color)
3017 .setColorTransform(colorLayer, matrix, vec3()).apply();
3018 {
3019 SCOPED_TRACE("new color");
3020 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
3021 }
3022 }
3023
TEST_P(LayerRenderTypeTransactionTest,SetColorTransformOnParent)3024 TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnParent) {
3025 sp<SurfaceControl> parentLayer;
3026 sp<SurfaceControl> colorLayer;
3027 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
3028 0 /* buffer height */,
3029 ISurfaceComposerClient::eFXSurfaceContainer));
3030 ASSERT_NO_FATAL_FAILURE(
3031 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
3032 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
3033
3034 Transaction()
3035 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
3036 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
3037 .setLayer(parentLayer, mLayerZBase + 1)
3038 .apply();
3039 {
3040 SCOPED_TRACE("default color");
3041 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
3042 }
3043
3044 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
3045 half3 expected = color;
3046 mat3 matrix;
3047 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
3048 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
3049 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
3050
3051 // degamma before applying the matrix
3052 if (mColorManagementUsed) {
3053 ColorTransformHelper::DegammaColor(expected);
3054 }
3055
3056 ColorTransformHelper::applyMatrix(expected, matrix);
3057
3058 if (mColorManagementUsed) {
3059 ColorTransformHelper::GammaColor(expected);
3060 }
3061
3062 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
3063 uint8_t(expected.b * 255), 255};
3064
3065 // this is handwavy, but the precison loss scaled by 255 (8-bit per
3066 // channel) should be less than one
3067 const uint8_t tolerance = 1;
3068
3069 Transaction()
3070 .setColor(colorLayer, color)
3071 .setColorTransform(parentLayer, matrix, vec3())
3072 .apply();
3073 {
3074 SCOPED_TRACE("new color");
3075 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
3076 }
3077 }
3078
TEST_P(LayerRenderTypeTransactionTest,SetColorTransformOnChildAndParent)3079 TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnChildAndParent) {
3080 sp<SurfaceControl> parentLayer;
3081 sp<SurfaceControl> colorLayer;
3082 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
3083 0 /* buffer height */,
3084 ISurfaceComposerClient::eFXSurfaceContainer));
3085 ASSERT_NO_FATAL_FAILURE(
3086 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
3087 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
3088
3089 Transaction()
3090 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
3091 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
3092 .setLayer(parentLayer, mLayerZBase + 1)
3093 .apply();
3094 {
3095 SCOPED_TRACE("default color");
3096 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
3097 }
3098
3099 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
3100 half3 expected = color;
3101 mat3 matrixChild;
3102 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
3103 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
3104 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
3105 mat3 matrixParent;
3106 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
3107 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
3108 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
3109
3110 // degamma before applying the matrix
3111 if (mColorManagementUsed) {
3112 ColorTransformHelper::DegammaColor(expected);
3113 }
3114
3115 ColorTransformHelper::applyMatrix(expected, matrixChild);
3116 ColorTransformHelper::applyMatrix(expected, matrixParent);
3117
3118 if (mColorManagementUsed) {
3119 ColorTransformHelper::GammaColor(expected);
3120 }
3121
3122 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
3123 uint8_t(expected.b * 255), 255};
3124
3125 // this is handwavy, but the precison loss scaled by 255 (8-bit per
3126 // channel) should be less than one
3127 const uint8_t tolerance = 1;
3128
3129 Transaction()
3130 .setColor(colorLayer, color)
3131 .setColorTransform(parentLayer, matrixParent, vec3())
3132 .setColorTransform(colorLayer, matrixChild, vec3())
3133 .apply();
3134 {
3135 SCOPED_TRACE("new color");
3136 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
3137 }
3138 }
3139
3140 struct CallbackData {
3141 CallbackData() = default;
CallbackDataandroid::CallbackData3142 CallbackData(nsecs_t time, const sp<Fence>& fence,
3143 const std::vector<SurfaceControlStats>& stats)
3144 : latchTime(time), presentFence(fence), surfaceControlStats(stats) {}
3145
3146 nsecs_t latchTime;
3147 sp<Fence> presentFence;
3148 std::vector<SurfaceControlStats> surfaceControlStats;
3149 };
3150
3151 class ExpectedResult {
3152 public:
3153 enum Transaction {
3154 NOT_PRESENTED = 0,
3155 PRESENTED,
3156 };
3157
3158 enum Buffer {
3159 NOT_ACQUIRED = 0,
3160 ACQUIRED,
3161 };
3162
3163 enum PreviousBuffer {
3164 NOT_RELEASED = 0,
3165 RELEASED,
3166 UNKNOWN,
3167 };
3168
reset()3169 void reset() {
3170 mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
3171 mExpectedSurfaceResults.clear();
3172 }
3173
addSurface(ExpectedResult::Transaction transactionResult,const sp<SurfaceControl> & layer,ExpectedResult::Buffer bufferResult=ACQUIRED,ExpectedResult::PreviousBuffer previousBufferResult=NOT_RELEASED)3174 void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
3175 ExpectedResult::Buffer bufferResult = ACQUIRED,
3176 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
3177 mTransactionResult = transactionResult;
3178 mExpectedSurfaceResults.emplace(std::piecewise_construct, std::forward_as_tuple(layer),
3179 std::forward_as_tuple(bufferResult, previousBufferResult));
3180 }
3181
addSurfaces(ExpectedResult::Transaction transactionResult,const std::vector<sp<SurfaceControl>> & layers,ExpectedResult::Buffer bufferResult=ACQUIRED,ExpectedResult::PreviousBuffer previousBufferResult=NOT_RELEASED)3182 void addSurfaces(ExpectedResult::Transaction transactionResult,
3183 const std::vector<sp<SurfaceControl>>& layers,
3184 ExpectedResult::Buffer bufferResult = ACQUIRED,
3185 ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
3186 for (const auto& layer : layers) {
3187 addSurface(transactionResult, layer, bufferResult, previousBufferResult);
3188 }
3189 }
3190
addExpectedPresentTime(nsecs_t expectedPresentTime)3191 void addExpectedPresentTime(nsecs_t expectedPresentTime) {
3192 mExpectedPresentTime = expectedPresentTime;
3193 }
3194
verifyCallbackData(const CallbackData & callbackData) const3195 void verifyCallbackData(const CallbackData& callbackData) const {
3196 const auto& [latchTime, presentFence, surfaceControlStats] = callbackData;
3197 if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
3198 ASSERT_GE(latchTime, 0) << "bad latch time";
3199 ASSERT_NE(presentFence, nullptr);
3200 if (mExpectedPresentTime >= 0) {
3201 ASSERT_EQ(presentFence->wait(3000), NO_ERROR);
3202 ASSERT_GE(presentFence->getSignalTime(), mExpectedPresentTime - nsecs_t(5 * 1e6));
3203 // if the panel is running at 30 hz, at the worst case, our expected time just
3204 // misses vsync and we have to wait another 33.3ms
3205 ASSERT_LE(presentFence->getSignalTime(),
3206 mExpectedPresentTime + nsecs_t(66.666666 * 1e6));
3207 }
3208 } else {
3209 ASSERT_EQ(presentFence, nullptr) << "transaction shouldn't have been presented";
3210 ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
3211 }
3212
3213 ASSERT_EQ(surfaceControlStats.size(), mExpectedSurfaceResults.size())
3214 << "wrong number of surfaces";
3215
3216 for (const auto& stats : surfaceControlStats) {
3217 ASSERT_NE(stats.surfaceControl, nullptr) << "returned null surface control";
3218
3219 const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
3220 ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
3221 << "unexpected surface control";
3222 expectedSurfaceResult->second.verifySurfaceControlStats(stats, latchTime);
3223 }
3224 }
3225
3226 private:
3227 class ExpectedSurfaceResult {
3228 public:
ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,ExpectedResult::PreviousBuffer previousBufferResult)3229 ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
3230 ExpectedResult::PreviousBuffer previousBufferResult)
3231 : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
3232
verifySurfaceControlStats(const SurfaceControlStats & surfaceControlStats,nsecs_t latchTime) const3233 void verifySurfaceControlStats(const SurfaceControlStats& surfaceControlStats,
3234 nsecs_t latchTime) const {
3235 const auto& [surfaceControl, acquireTime, previousReleaseFence] = surfaceControlStats;
3236
3237 ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
3238 << "bad acquire time";
3239 ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
3240
3241 if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED) {
3242 ASSERT_NE(previousReleaseFence, nullptr)
3243 << "failed to set release prev buffer fence";
3244 } else if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::NOT_RELEASED) {
3245 ASSERT_EQ(previousReleaseFence, nullptr)
3246 << "should not have set released prev buffer fence";
3247 }
3248 }
3249
3250 private:
3251 ExpectedResult::Buffer mBufferResult;
3252 ExpectedResult::PreviousBuffer mPreviousBufferResult;
3253 };
3254
3255 struct SCHash {
operator ()android::ExpectedResult::SCHash3256 std::size_t operator()(const sp<SurfaceControl>& sc) const {
3257 return std::hash<IBinder*>{}(sc->getHandle().get());
3258 }
3259 };
3260 ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
3261 nsecs_t mExpectedPresentTime = -1;
3262 std::unordered_map<sp<SurfaceControl>, ExpectedSurfaceResult, SCHash> mExpectedSurfaceResults;
3263 };
3264
3265 class CallbackHelper {
3266 public:
function(void * callbackContext,nsecs_t latchTime,const sp<Fence> & presentFence,const std::vector<SurfaceControlStats> & stats)3267 static void function(void* callbackContext, nsecs_t latchTime, const sp<Fence>& presentFence,
3268 const std::vector<SurfaceControlStats>& stats) {
3269 if (!callbackContext) {
3270 ALOGE("failed to get callback context");
3271 }
3272 CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
3273 std::lock_guard lock(helper->mMutex);
3274 helper->mCallbackDataQueue.emplace(latchTime, presentFence, stats);
3275 helper->mConditionVariable.notify_all();
3276 }
3277
getCallbackData(CallbackData * outData)3278 void getCallbackData(CallbackData* outData) {
3279 std::unique_lock lock(mMutex);
3280
3281 if (mCallbackDataQueue.empty()) {
3282 ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
3283 std::cv_status::timeout)
3284 << "did not receive callback";
3285 }
3286
3287 *outData = std::move(mCallbackDataQueue.front());
3288 mCallbackDataQueue.pop();
3289 }
3290
verifyFinalState()3291 void verifyFinalState() {
3292 // Wait to see if there are extra callbacks
3293 std::this_thread::sleep_for(500ms);
3294
3295 std::lock_guard lock(mMutex);
3296 EXPECT_EQ(mCallbackDataQueue.size(), 0) << "extra callbacks received";
3297 mCallbackDataQueue = {};
3298 }
3299
getContext()3300 void* getContext() { return static_cast<void*>(this); }
3301
3302 std::mutex mMutex;
3303 std::condition_variable mConditionVariable;
3304 std::queue<CallbackData> mCallbackDataQueue;
3305 };
3306
3307 class LayerCallbackTest : public LayerTransactionTest {
3308 public:
createBufferStateLayer()3309 virtual sp<SurfaceControl> createBufferStateLayer() {
3310 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
3311 }
3312
fillTransaction(Transaction & transaction,CallbackHelper * callbackHelper,const sp<SurfaceControl> & layer=nullptr,bool setBuffer=true,bool setBackgroundColor=false)3313 static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
3314 const sp<SurfaceControl>& layer = nullptr, bool setBuffer = true,
3315 bool setBackgroundColor = false) {
3316 if (layer) {
3317 sp<GraphicBuffer> buffer;
3318 sp<Fence> fence;
3319 if (setBuffer) {
3320 int err = getBuffer(&buffer, &fence);
3321 if (err != NO_ERROR) {
3322 return err;
3323 }
3324
3325 transaction.setBuffer(layer, buffer);
3326 transaction.setAcquireFence(layer, fence);
3327 }
3328
3329 if (setBackgroundColor) {
3330 transaction.setBackgroundColor(layer, /*color*/ half3(1.0f, 0, 0), /*alpha*/ 1.0f,
3331 ui::Dataspace::UNKNOWN);
3332 }
3333 }
3334
3335 transaction.addTransactionCompletedCallback(callbackHelper->function,
3336 callbackHelper->getContext());
3337 return NO_ERROR;
3338 }
3339
waitForCallback(CallbackHelper & helper,const ExpectedResult & expectedResult,bool finalState=false)3340 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
3341 bool finalState = false) {
3342 CallbackData callbackData;
3343 ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
3344 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
3345
3346 if (finalState) {
3347 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
3348 }
3349 }
3350
waitForCallbacks(CallbackHelper & helper,const std::vector<ExpectedResult> & expectedResults,bool finalState=false)3351 static void waitForCallbacks(CallbackHelper& helper,
3352 const std::vector<ExpectedResult>& expectedResults,
3353 bool finalState = false) {
3354 for (const auto& expectedResult : expectedResults) {
3355 waitForCallback(helper, expectedResult);
3356 }
3357 if (finalState) {
3358 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
3359 }
3360 }
3361 };
3362
TEST_F(LayerCallbackTest,BufferColor)3363 TEST_F(LayerCallbackTest, BufferColor) {
3364 sp<SurfaceControl> layer;
3365 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3366
3367 Transaction transaction;
3368 CallbackHelper callback;
3369 int err = fillTransaction(transaction, &callback, layer, true, true);
3370 if (err) {
3371 GTEST_SUCCEED() << "test not supported";
3372 return;
3373 }
3374
3375 transaction.apply();
3376
3377 ExpectedResult expected;
3378 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3379 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3380 }
3381
TEST_F(LayerCallbackTest,NoBufferNoColor)3382 TEST_F(LayerCallbackTest, NoBufferNoColor) {
3383 sp<SurfaceControl> layer;
3384 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3385
3386 Transaction transaction;
3387 CallbackHelper callback;
3388 int err = fillTransaction(transaction, &callback, layer, false, false);
3389 if (err) {
3390 GTEST_SUCCEED() << "test not supported";
3391 return;
3392 }
3393
3394 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
3395
3396 ExpectedResult expected;
3397 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
3398 ExpectedResult::Buffer::NOT_ACQUIRED);
3399 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3400 }
3401
TEST_F(LayerCallbackTest,BufferNoColor)3402 TEST_F(LayerCallbackTest, BufferNoColor) {
3403 sp<SurfaceControl> layer;
3404 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3405
3406 Transaction transaction;
3407 CallbackHelper callback;
3408 int err = fillTransaction(transaction, &callback, layer, true, false);
3409 if (err) {
3410 GTEST_SUCCEED() << "test not supported";
3411 return;
3412 }
3413
3414 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
3415
3416 ExpectedResult expected;
3417 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3418 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3419 }
3420
TEST_F(LayerCallbackTest,NoBufferColor)3421 TEST_F(LayerCallbackTest, NoBufferColor) {
3422 sp<SurfaceControl> layer;
3423 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3424
3425 Transaction transaction;
3426 CallbackHelper callback;
3427 int err = fillTransaction(transaction, &callback, layer, false, true);
3428 if (err) {
3429 GTEST_SUCCEED() << "test not supported";
3430 return;
3431 }
3432
3433 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
3434
3435 ExpectedResult expected;
3436 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3437 ExpectedResult::Buffer::NOT_ACQUIRED);
3438 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3439 }
3440
TEST_F(LayerCallbackTest,NoStateChange)3441 TEST_F(LayerCallbackTest, NoStateChange) {
3442 Transaction transaction;
3443 CallbackHelper callback;
3444 int err = fillTransaction(transaction, &callback);
3445 if (err) {
3446 GTEST_SUCCEED() << "test not supported";
3447 return;
3448 }
3449
3450 transaction.apply();
3451
3452 ExpectedResult expected;
3453 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3454 }
3455
TEST_F(LayerCallbackTest,OffScreen)3456 TEST_F(LayerCallbackTest, OffScreen) {
3457 sp<SurfaceControl> layer;
3458 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3459
3460 Transaction transaction;
3461 CallbackHelper callback;
3462 int err = fillTransaction(transaction, &callback, layer);
3463 if (err) {
3464 GTEST_SUCCEED() << "test not supported";
3465 return;
3466 }
3467
3468 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
3469
3470 ExpectedResult expected;
3471 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3472 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3473 }
3474
TEST_F(LayerCallbackTest,MergeBufferNoColor)3475 TEST_F(LayerCallbackTest, MergeBufferNoColor) {
3476 sp<SurfaceControl> layer1, layer2;
3477 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3478 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3479
3480 Transaction transaction1, transaction2;
3481 CallbackHelper callback1, callback2;
3482 int err = fillTransaction(transaction1, &callback1, layer1);
3483 if (err) {
3484 GTEST_SUCCEED() << "test not supported";
3485 return;
3486 }
3487 err = fillTransaction(transaction2, &callback2, layer2);
3488 if (err) {
3489 GTEST_SUCCEED() << "test not supported";
3490 return;
3491 }
3492
3493 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3494 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3495
3496 ExpectedResult expected;
3497 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3498 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3499 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3500 }
3501
TEST_F(LayerCallbackTest,MergeNoBufferColor)3502 TEST_F(LayerCallbackTest, MergeNoBufferColor) {
3503 sp<SurfaceControl> layer1, layer2;
3504 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3505 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3506
3507 Transaction transaction1, transaction2;
3508 CallbackHelper callback1, callback2;
3509 int err = fillTransaction(transaction1, &callback1, layer1, false, true);
3510 if (err) {
3511 GTEST_SUCCEED() << "test not supported";
3512 return;
3513 }
3514 err = fillTransaction(transaction2, &callback2, layer2, false, true);
3515 if (err) {
3516 GTEST_SUCCEED() << "test not supported";
3517 return;
3518 }
3519
3520 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3521 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3522
3523 ExpectedResult expected;
3524 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
3525 ExpectedResult::Buffer::NOT_ACQUIRED);
3526 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3527 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3528 }
3529
TEST_F(LayerCallbackTest,MergeOneBufferOneColor)3530 TEST_F(LayerCallbackTest, MergeOneBufferOneColor) {
3531 sp<SurfaceControl> layer1, layer2;
3532 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3533 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3534
3535 Transaction transaction1, transaction2;
3536 CallbackHelper callback1, callback2;
3537 int err = fillTransaction(transaction1, &callback1, layer1);
3538 if (err) {
3539 GTEST_SUCCEED() << "test not supported";
3540 return;
3541 }
3542 err = fillTransaction(transaction2, &callback2, layer2, false, true);
3543 if (err) {
3544 GTEST_SUCCEED() << "test not supported";
3545 return;
3546 }
3547
3548 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3549 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3550
3551 ExpectedResult expected;
3552 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer1);
3553 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer2,
3554 ExpectedResult::Buffer::NOT_ACQUIRED);
3555 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3556 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3557 }
TEST_F(LayerCallbackTest,Merge_SameCallback)3558 TEST_F(LayerCallbackTest, Merge_SameCallback) {
3559 sp<SurfaceControl> layer1, layer2;
3560 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3561 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3562
3563 Transaction transaction1, transaction2;
3564 CallbackHelper callback;
3565 int err = fillTransaction(transaction1, &callback, layer1);
3566 if (err) {
3567 GTEST_SUCCEED() << "test not supported";
3568 return;
3569 }
3570 err = fillTransaction(transaction2, &callback, layer2);
3571 if (err) {
3572 GTEST_SUCCEED() << "test not supported";
3573 return;
3574 }
3575
3576 transaction2.merge(std::move(transaction1)).apply();
3577
3578 ExpectedResult expected;
3579 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3580 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3581 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3582 }
3583
TEST_F(LayerCallbackTest,Merge_SameLayer)3584 TEST_F(LayerCallbackTest, Merge_SameLayer) {
3585 sp<SurfaceControl> layer;
3586 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3587
3588 Transaction transaction1, transaction2;
3589 CallbackHelper callback1, callback2;
3590 int err = fillTransaction(transaction1, &callback1, layer);
3591 if (err) {
3592 GTEST_SUCCEED() << "test not supported";
3593 return;
3594 }
3595 err = fillTransaction(transaction2, &callback2, layer);
3596 if (err) {
3597 GTEST_SUCCEED() << "test not supported";
3598 return;
3599 }
3600
3601 transaction2.merge(std::move(transaction1)).apply();
3602
3603 ExpectedResult expected;
3604 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3605 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3606 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3607 }
3608
TEST_F(LayerCallbackTest,Merge_DifferentClients)3609 TEST_F(LayerCallbackTest, Merge_DifferentClients) {
3610 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3611 client2(new SurfaceComposerClient);
3612
3613 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3614 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3615
3616 sp<SurfaceControl> layer1, layer2;
3617 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
3618 ISurfaceComposerClient::eFXSurfaceBufferState));
3619 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
3620 ISurfaceComposerClient::eFXSurfaceBufferState));
3621
3622 Transaction transaction1, transaction2;
3623 CallbackHelper callback1, callback2;
3624 int err = fillTransaction(transaction1, &callback1, layer1);
3625 if (err) {
3626 GTEST_SUCCEED() << "test not supported";
3627 return;
3628 }
3629 err = fillTransaction(transaction2, &callback2, layer2);
3630 if (err) {
3631 GTEST_SUCCEED() << "test not supported";
3632 return;
3633 }
3634
3635 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3636 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3637
3638 ExpectedResult expected;
3639 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3640 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3641 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3642 }
3643
TEST_F(LayerCallbackTest,MultipleTransactions)3644 TEST_F(LayerCallbackTest, MultipleTransactions) {
3645 sp<SurfaceControl> layer;
3646 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3647
3648 Transaction transaction;
3649 CallbackHelper callback;
3650 for (size_t i = 0; i < 10; i++) {
3651 int err = fillTransaction(transaction, &callback, layer);
3652 if (err) {
3653 GTEST_SUCCEED() << "test not supported";
3654 return;
3655 }
3656
3657 transaction.apply();
3658
3659 ExpectedResult expected;
3660 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3661 ExpectedResult::Buffer::ACQUIRED,
3662 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3663 : ExpectedResult::PreviousBuffer::RELEASED);
3664 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3665 }
3666 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3667 }
3668
TEST_F(LayerCallbackTest,MultipleTransactions_NoStateChange)3669 TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
3670 sp<SurfaceControl> layer;
3671 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3672
3673 Transaction transaction;
3674 CallbackHelper callback;
3675 for (size_t i = 0; i < 10; i++) {
3676 ExpectedResult expected;
3677
3678 if (i == 0) {
3679 int err = fillTransaction(transaction, &callback, layer);
3680 if (err) {
3681 GTEST_SUCCEED() << "test not supported";
3682 return;
3683 }
3684 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3685 } else {
3686 int err = fillTransaction(transaction, &callback);
3687 if (err) {
3688 GTEST_SUCCEED() << "test not supported";
3689 return;
3690 }
3691 }
3692
3693 transaction.apply();
3694
3695 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3696 }
3697 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3698 }
3699
TEST_F(LayerCallbackTest,MultipleTransactions_SameStateChange)3700 TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
3701 sp<SurfaceControl> layer;
3702 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3703
3704 Transaction transaction;
3705 CallbackHelper callback;
3706 for (size_t i = 0; i < 10; i++) {
3707 if (i == 0) {
3708 int err = fillTransaction(transaction, &callback, layer);
3709 if (err) {
3710 GTEST_SUCCEED() << "test not supported";
3711 return;
3712 }
3713 } else {
3714 int err = fillTransaction(transaction, &callback);
3715 if (err) {
3716 GTEST_SUCCEED() << "test not supported";
3717 return;
3718 }
3719 }
3720
3721 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
3722
3723 ExpectedResult expected;
3724 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
3725 : ExpectedResult::Transaction::NOT_PRESENTED,
3726 layer,
3727 (i == 0) ? ExpectedResult::Buffer::ACQUIRED
3728 : ExpectedResult::Buffer::NOT_ACQUIRED);
3729 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
3730 }
3731 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3732 }
3733
TEST_F(LayerCallbackTest,MultipleTransactions_Merge)3734 TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
3735 sp<SurfaceControl> layer1, layer2;
3736 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3737 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3738
3739 Transaction transaction1, transaction2;
3740 CallbackHelper callback1, callback2;
3741 for (size_t i = 0; i < 10; i++) {
3742 int err = fillTransaction(transaction1, &callback1, layer1);
3743 if (err) {
3744 GTEST_SUCCEED() << "test not supported";
3745 return;
3746 }
3747 err = fillTransaction(transaction2, &callback2, layer2);
3748 if (err) {
3749 GTEST_SUCCEED() << "test not supported";
3750 return;
3751 }
3752
3753 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3754 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3755
3756 ExpectedResult expected;
3757 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
3758 ExpectedResult::Buffer::ACQUIRED,
3759 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3760 : ExpectedResult::PreviousBuffer::RELEASED);
3761 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3762 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3763 }
3764 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3765 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3766 }
3767
TEST_F(LayerCallbackTest,MultipleTransactions_Merge_DifferentClients)3768 TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
3769 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3770 client2(new SurfaceComposerClient);
3771 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3772 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3773
3774 sp<SurfaceControl> layer1, layer2;
3775 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
3776 ISurfaceComposerClient::eFXSurfaceBufferState));
3777 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
3778 ISurfaceComposerClient::eFXSurfaceBufferState));
3779
3780 Transaction transaction1, transaction2;
3781 CallbackHelper callback1, callback2;
3782 for (size_t i = 0; i < 10; i++) {
3783 int err = fillTransaction(transaction1, &callback1, layer1);
3784 if (err) {
3785 GTEST_SUCCEED() << "test not supported";
3786 return;
3787 }
3788 err = fillTransaction(transaction2, &callback2, layer2);
3789 if (err) {
3790 GTEST_SUCCEED() << "test not supported";
3791 return;
3792 }
3793
3794 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3795 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3796
3797 ExpectedResult expected;
3798 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
3799 ExpectedResult::Buffer::ACQUIRED,
3800 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3801 : ExpectedResult::PreviousBuffer::RELEASED);
3802 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3803 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3804 }
3805 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3806 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3807 }
3808
TEST_F(LayerCallbackTest,MultipleTransactions_Merge_DifferentClients_NoStateChange)3809 TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
3810 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3811 client2(new SurfaceComposerClient);
3812 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3813 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3814
3815 sp<SurfaceControl> layer1, layer2;
3816 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
3817 ISurfaceComposerClient::eFXSurfaceBufferState));
3818 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
3819 ISurfaceComposerClient::eFXSurfaceBufferState));
3820
3821 Transaction transaction1, transaction2;
3822 CallbackHelper callback1, callback2;
3823
3824 // Normal call to set up test
3825 int err = fillTransaction(transaction1, &callback1, layer1);
3826 if (err) {
3827 GTEST_SUCCEED() << "test not supported";
3828 return;
3829 }
3830 err = fillTransaction(transaction2, &callback2, layer2);
3831 if (err) {
3832 GTEST_SUCCEED() << "test not supported";
3833 return;
3834 }
3835
3836 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3837 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3838
3839 ExpectedResult expected;
3840 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3841 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3842 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3843 expected.reset();
3844
3845 // Test
3846 err = fillTransaction(transaction1, &callback1);
3847 if (err) {
3848 GTEST_SUCCEED() << "test not supported";
3849 return;
3850 }
3851 err = fillTransaction(transaction2, &callback2);
3852 if (err) {
3853 GTEST_SUCCEED() << "test not supported";
3854 return;
3855 }
3856
3857 transaction2.merge(std::move(transaction1)).apply();
3858
3859 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3860 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3861 }
3862
TEST_F(LayerCallbackTest,MultipleTransactions_Merge_DifferentClients_SameStateChange)3863 TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
3864 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3865 client2(new SurfaceComposerClient);
3866
3867 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3868 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3869
3870 sp<SurfaceControl> layer1, layer2;
3871 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
3872 ISurfaceComposerClient::eFXSurfaceBufferState));
3873 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
3874 ISurfaceComposerClient::eFXSurfaceBufferState));
3875
3876 Transaction transaction1, transaction2;
3877 CallbackHelper callback1, callback2;
3878
3879 // Normal call to set up test
3880 int err = fillTransaction(transaction1, &callback1, layer1);
3881 if (err) {
3882 GTEST_SUCCEED() << "test not supported";
3883 return;
3884 }
3885 err = fillTransaction(transaction2, &callback2, layer2);
3886 if (err) {
3887 GTEST_SUCCEED() << "test not supported";
3888 return;
3889 }
3890
3891 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3892 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3893
3894 ExpectedResult expected;
3895 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3896 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3897 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3898 expected.reset();
3899
3900 // Test
3901 err = fillTransaction(transaction1, &callback1);
3902 if (err) {
3903 GTEST_SUCCEED() << "test not supported";
3904 return;
3905 }
3906 err = fillTransaction(transaction2, &callback2);
3907 if (err) {
3908 GTEST_SUCCEED() << "test not supported";
3909 return;
3910 }
3911
3912 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3913
3914 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
3915 ExpectedResult::Buffer::NOT_ACQUIRED);
3916 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3917 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3918 }
3919
TEST_F(LayerCallbackTest,MultipleTransactions_SingleFrame)3920 TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3921 sp<SurfaceControl> layer;
3922 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3923
3924 Transaction transaction;
3925 CallbackHelper callback;
3926 std::vector<ExpectedResult> expectedResults(50);
3927 for (auto& expected : expectedResults) {
3928 expected.reset();
3929 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3930 ExpectedResult::Buffer::ACQUIRED,
3931 ExpectedResult::PreviousBuffer::UNKNOWN);
3932
3933 int err = fillTransaction(transaction, &callback, layer);
3934 if (err) {
3935 GTEST_SUCCEED() << "test not supported";
3936 return;
3937 }
3938
3939 transaction.apply();
3940 }
3941 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3942 }
3943
TEST_F(LayerCallbackTest,MultipleTransactions_SingleFrame_NoStateChange)3944 TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3945 sp<SurfaceControl> layer;
3946 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3947
3948 // Normal call to set up test
3949 Transaction transaction;
3950 CallbackHelper callback;
3951 int err = fillTransaction(transaction, &callback, layer);
3952 if (err) {
3953 GTEST_SUCCEED() << "test not supported";
3954 return;
3955 }
3956
3957 transaction.apply();
3958
3959 ExpectedResult expected;
3960 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3961 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3962
3963 // Test
3964 std::vector<ExpectedResult> expectedResults(50);
3965 for (auto& expected : expectedResults) {
3966 expected.reset();
3967
3968 err = fillTransaction(transaction, &callback);
3969 if (err) {
3970 GTEST_SUCCEED() << "test not supported";
3971 return;
3972 }
3973
3974 transaction.apply();
3975 }
3976 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3977 }
3978
TEST_F(LayerCallbackTest,MultipleTransactions_SingleFrame_SameStateChange)3979 TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3980 sp<SurfaceControl> layer;
3981 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3982
3983 // Normal call to set up test
3984 Transaction transaction;
3985 CallbackHelper callback;
3986 int err = fillTransaction(transaction, &callback, layer);
3987 if (err) {
3988 GTEST_SUCCEED() << "test not supported";
3989 return;
3990 }
3991
3992 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
3993
3994 ExpectedResult expectedResult;
3995 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3996 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3997
3998 // Test
3999 std::vector<ExpectedResult> expectedResults(50);
4000 for (auto& expected : expectedResults) {
4001 expected.reset();
4002 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
4003 ExpectedResult::Buffer::NOT_ACQUIRED);
4004
4005 err = fillTransaction(transaction, &callback);
4006 if (err) {
4007 GTEST_SUCCEED() << "test not supported";
4008 return;
4009 }
4010
4011 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
4012 }
4013 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
4014 }
4015
TEST_F(LayerCallbackTest,DesiredPresentTime)4016 TEST_F(LayerCallbackTest, DesiredPresentTime) {
4017 sp<SurfaceControl> layer;
4018 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
4019
4020 Transaction transaction;
4021 CallbackHelper callback;
4022 int err = fillTransaction(transaction, &callback, layer);
4023 if (err) {
4024 GTEST_SUCCEED() << "test not supported";
4025 return;
4026 }
4027
4028 // Try to present 100ms in the future
4029 nsecs_t time = systemTime() + (100 * 1e6);
4030
4031 transaction.setDesiredPresentTime(time);
4032 transaction.apply();
4033
4034 ExpectedResult expected;
4035 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
4036 expected.addExpectedPresentTime(time);
4037 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
4038 }
4039
TEST_F(LayerCallbackTest,DesiredPresentTime_Multiple)4040 TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
4041 sp<SurfaceControl> layer;
4042 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
4043
4044 Transaction transaction;
4045 CallbackHelper callback1;
4046 int err = fillTransaction(transaction, &callback1, layer);
4047 if (err) {
4048 GTEST_SUCCEED() << "test not supported";
4049 return;
4050 }
4051
4052 // Try to present 100ms in the future
4053 nsecs_t time = systemTime() + (100 * 1e6);
4054
4055 transaction.setDesiredPresentTime(time);
4056 transaction.apply();
4057
4058 ExpectedResult expected1;
4059 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
4060 expected1.addExpectedPresentTime(time);
4061
4062 CallbackHelper callback2;
4063 err = fillTransaction(transaction, &callback2, layer);
4064 if (err) {
4065 GTEST_SUCCEED() << "test not supported";
4066 return;
4067 }
4068
4069 // Try to present 33ms after the first frame
4070 time += (33.3 * 1e6);
4071
4072 transaction.setDesiredPresentTime(time);
4073 transaction.apply();
4074
4075 ExpectedResult expected2;
4076 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
4077 ExpectedResult::Buffer::ACQUIRED,
4078 ExpectedResult::PreviousBuffer::RELEASED);
4079 expected2.addExpectedPresentTime(time);
4080
4081 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
4082 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
4083 }
4084
TEST_F(LayerCallbackTest,DesiredPresentTime_OutOfOrder)4085 TEST_F(LayerCallbackTest, DesiredPresentTime_OutOfOrder) {
4086 sp<SurfaceControl> layer;
4087 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
4088
4089 Transaction transaction;
4090 CallbackHelper callback1;
4091 int err = fillTransaction(transaction, &callback1, layer);
4092 if (err) {
4093 GTEST_SUCCEED() << "test not supported";
4094 return;
4095 }
4096
4097 // Try to present 100ms in the future
4098 nsecs_t time = systemTime() + (100 * 1e6);
4099
4100 transaction.setDesiredPresentTime(time);
4101 transaction.apply();
4102
4103 ExpectedResult expected1;
4104 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
4105 expected1.addExpectedPresentTime(time);
4106
4107 CallbackHelper callback2;
4108 err = fillTransaction(transaction, &callback2, layer);
4109 if (err) {
4110 GTEST_SUCCEED() << "test not supported";
4111 return;
4112 }
4113
4114 // Try to present 33ms before the previous frame
4115 time -= (33.3 * 1e6);
4116
4117 transaction.setDesiredPresentTime(time);
4118 transaction.apply();
4119
4120 ExpectedResult expected2;
4121 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
4122 ExpectedResult::Buffer::ACQUIRED,
4123 ExpectedResult::PreviousBuffer::RELEASED);
4124
4125 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
4126 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
4127 }
4128
TEST_F(LayerCallbackTest,DesiredPresentTime_Past)4129 TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
4130 sp<SurfaceControl> layer;
4131 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
4132
4133 Transaction transaction;
4134 CallbackHelper callback;
4135 int err = fillTransaction(transaction, &callback, layer);
4136 if (err) {
4137 GTEST_SUCCEED() << "test not supported";
4138 return;
4139 }
4140
4141 // Try to present 100ms in the past
4142 nsecs_t time = systemTime() - (100 * 1e6);
4143
4144 transaction.setDesiredPresentTime(time);
4145 transaction.apply();
4146
4147 ExpectedResult expected;
4148 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
4149 expected.addExpectedPresentTime(systemTime());
4150 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
4151 }
4152
4153 class LayerUpdateTest : public LayerTransactionTest {
4154 protected:
SetUp()4155 virtual void SetUp() {
4156 LayerTransactionTest::SetUp();
4157 ASSERT_EQ(NO_ERROR, mClient->initCheck());
4158
4159 const auto display = SurfaceComposerClient::getInternalDisplayToken();
4160 ASSERT_FALSE(display == nullptr);
4161
4162 DisplayInfo info;
4163 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
4164
4165 ssize_t displayWidth = info.w;
4166 ssize_t displayHeight = info.h;
4167
4168 // Background surface
4169 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
4170 displayHeight, 0);
4171 ASSERT_TRUE(mBGSurfaceControl != nullptr);
4172 ASSERT_TRUE(mBGSurfaceControl->isValid());
4173 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
4174
4175 // Foreground surface
4176 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
4177
4178 ASSERT_TRUE(mFGSurfaceControl != nullptr);
4179 ASSERT_TRUE(mFGSurfaceControl->isValid());
4180
4181 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4182
4183 // Synchronization surface
4184 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
4185 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
4186 ASSERT_TRUE(mSyncSurfaceControl->isValid());
4187
4188 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4189
4190 asTransaction([&](Transaction& t) {
4191 t.setDisplayLayerStack(display, 0);
4192
4193 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
4194
4195 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
4196 .setPosition(mFGSurfaceControl, 64, 64)
4197 .show(mFGSurfaceControl);
4198
4199 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
4200 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
4201 .show(mSyncSurfaceControl);
4202 });
4203 }
4204
TearDown()4205 virtual void TearDown() {
4206 LayerTransactionTest::TearDown();
4207 mBGSurfaceControl = 0;
4208 mFGSurfaceControl = 0;
4209 mSyncSurfaceControl = 0;
4210 }
4211
waitForPostedBuffers()4212 void waitForPostedBuffers() {
4213 // Since the sync surface is in synchronous mode (i.e. double buffered)
4214 // posting three buffers to it should ensure that at least two
4215 // SurfaceFlinger::handlePageFlip calls have been made, which should
4216 // guaranteed that a buffer posted to another Surface has been retired.
4217 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4218 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4219 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4220 }
4221
4222
4223 sp<SurfaceControl> mBGSurfaceControl;
4224 sp<SurfaceControl> mFGSurfaceControl;
4225
4226 // This surface is used to ensure that the buffers posted to
4227 // mFGSurfaceControl have been picked up by SurfaceFlinger.
4228 sp<SurfaceControl> mSyncSurfaceControl;
4229 };
4230
TEST_F(LayerUpdateTest,RelativesAreNotDetached)4231 TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
4232
4233 std::unique_ptr<ScreenCapture> sc;
4234
4235 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
4236 fillSurfaceRGBA8(relative, 10, 10, 10);
4237 waitForPostedBuffers();
4238
4239 Transaction{}
4240 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
4241 .setPosition(relative, 64, 64)
4242 .apply();
4243
4244 {
4245 // The relative should be on top of the FG control.
4246 ScreenCapture::captureScreen(&sc);
4247 sc->checkPixel(64, 64, 10, 10, 10);
4248 }
4249 Transaction{}.detachChildren(mFGSurfaceControl).apply();
4250
4251 {
4252 // Nothing should change at this point.
4253 ScreenCapture::captureScreen(&sc);
4254 sc->checkPixel(64, 64, 10, 10, 10);
4255 }
4256
4257 Transaction{}.hide(relative).apply();
4258
4259 {
4260 // Ensure that the relative was actually hidden, rather than
4261 // being left in the detached but visible state.
4262 ScreenCapture::captureScreen(&sc);
4263 sc->expectFGColor(64, 64);
4264 }
4265 }
4266
4267 class GeometryLatchingTest : public LayerUpdateTest {
4268 protected:
EXPECT_INITIAL_STATE(const char * trace)4269 void EXPECT_INITIAL_STATE(const char* trace) {
4270 SCOPED_TRACE(trace);
4271 ScreenCapture::captureScreen(&sc);
4272 // We find the leading edge of the FG surface.
4273 sc->expectFGColor(127, 127);
4274 sc->expectBGColor(128, 128);
4275 }
4276
lockAndFillFGBuffer()4277 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
4278
unlockFGBuffer()4279 void unlockFGBuffer() {
4280 sp<Surface> s = mFGSurfaceControl->getSurface();
4281 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
4282 waitForPostedBuffers();
4283 }
4284
completeFGResize()4285 void completeFGResize() {
4286 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4287 waitForPostedBuffers();
4288 }
restoreInitialState()4289 void restoreInitialState() {
4290 asTransaction([&](Transaction& t) {
4291 t.setSize(mFGSurfaceControl, 64, 64);
4292 t.setPosition(mFGSurfaceControl, 64, 64);
4293 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
4294 });
4295
4296 EXPECT_INITIAL_STATE("After restoring initial state");
4297 }
4298 std::unique_ptr<ScreenCapture> sc;
4299 };
4300
4301 class CropLatchingTest : public GeometryLatchingTest {
4302 protected:
EXPECT_CROPPED_STATE(const char * trace)4303 void EXPECT_CROPPED_STATE(const char* trace) {
4304 SCOPED_TRACE(trace);
4305 ScreenCapture::captureScreen(&sc);
4306 // The edge should be moved back one pixel by our crop.
4307 sc->expectFGColor(126, 126);
4308 sc->expectBGColor(127, 127);
4309 sc->expectBGColor(128, 128);
4310 }
4311
EXPECT_RESIZE_STATE(const char * trace)4312 void EXPECT_RESIZE_STATE(const char* trace) {
4313 SCOPED_TRACE(trace);
4314 ScreenCapture::captureScreen(&sc);
4315 // The FG is now resized too 128,128 at 64,64
4316 sc->expectFGColor(64, 64);
4317 sc->expectFGColor(191, 191);
4318 sc->expectBGColor(192, 192);
4319 }
4320 };
4321
TEST_F(LayerUpdateTest,DeferredTransactionTest)4322 TEST_F(LayerUpdateTest, DeferredTransactionTest) {
4323 std::unique_ptr<ScreenCapture> sc;
4324 {
4325 SCOPED_TRACE("before anything");
4326 ScreenCapture::captureScreen(&sc);
4327 sc->expectBGColor(32, 32);
4328 sc->expectFGColor(96, 96);
4329 sc->expectBGColor(160, 160);
4330 }
4331
4332 // set up two deferred transactions on different frames
4333 asTransaction([&](Transaction& t) {
4334 t.setAlpha(mFGSurfaceControl, 0.75);
4335 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
4336 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
4337 });
4338
4339 asTransaction([&](Transaction& t) {
4340 t.setPosition(mFGSurfaceControl, 128, 128);
4341 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
4342 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
4343 });
4344
4345 {
4346 SCOPED_TRACE("before any trigger");
4347 ScreenCapture::captureScreen(&sc);
4348 sc->expectBGColor(32, 32);
4349 sc->expectFGColor(96, 96);
4350 sc->expectBGColor(160, 160);
4351 }
4352
4353 // should trigger the first deferred transaction, but not the second one
4354 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4355 {
4356 SCOPED_TRACE("after first trigger");
4357 ScreenCapture::captureScreen(&sc);
4358 sc->expectBGColor(32, 32);
4359 sc->checkPixel(96, 96, 162, 63, 96);
4360 sc->expectBGColor(160, 160);
4361 }
4362
4363 // should show up immediately since it's not deferred
4364 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
4365
4366 // trigger the second deferred transaction
4367 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
4368 {
4369 SCOPED_TRACE("after second trigger");
4370 ScreenCapture::captureScreen(&sc);
4371 sc->expectBGColor(32, 32);
4372 sc->expectBGColor(96, 96);
4373 sc->expectFGColor(160, 160);
4374 }
4375 }
4376
TEST_F(LayerUpdateTest,LayerWithNoBuffersResizesImmediately)4377 TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
4378 std::unique_ptr<ScreenCapture> sc;
4379
4380 sp<SurfaceControl> childNoBuffer =
4381 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
4382 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4383 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
4384 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
4385 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
4386 SurfaceComposerClient::Transaction{}
4387 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
4388 .show(childNoBuffer)
4389 .show(childBuffer)
4390 .apply(true);
4391 {
4392 ScreenCapture::captureScreen(&sc);
4393 sc->expectChildColor(73, 73);
4394 sc->expectFGColor(74, 74);
4395 }
4396 SurfaceComposerClient::Transaction{}
4397 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
4398 .apply(true);
4399 {
4400 ScreenCapture::captureScreen(&sc);
4401 sc->expectChildColor(73, 73);
4402 sc->expectChildColor(74, 74);
4403 }
4404 }
4405
TEST_F(LayerUpdateTest,MergingTransactions)4406 TEST_F(LayerUpdateTest, MergingTransactions) {
4407 std::unique_ptr<ScreenCapture> sc;
4408 {
4409 SCOPED_TRACE("before move");
4410 ScreenCapture::captureScreen(&sc);
4411 sc->expectBGColor(0, 12);
4412 sc->expectFGColor(75, 75);
4413 sc->expectBGColor(145, 145);
4414 }
4415
4416 Transaction t1, t2;
4417 t1.setPosition(mFGSurfaceControl, 128, 128);
4418 t2.setPosition(mFGSurfaceControl, 0, 0);
4419 // We expect that the position update from t2 now
4420 // overwrites the position update from t1.
4421 t1.merge(std::move(t2));
4422 t1.apply();
4423
4424 {
4425 ScreenCapture::captureScreen(&sc);
4426 sc->expectFGColor(1, 1);
4427 }
4428 }
4429
4430 class ChildLayerTest : public LayerUpdateTest {
4431 protected:
SetUp()4432 void SetUp() override {
4433 LayerUpdateTest::SetUp();
4434 mChild = createSurface(mClient, "Child surface", 10, 15, PIXEL_FORMAT_RGBA_8888, 0,
4435 mFGSurfaceControl.get());
4436 fillSurfaceRGBA8(mChild, 200, 200, 200);
4437
4438 {
4439 SCOPED_TRACE("before anything");
4440 mCapture = screenshot();
4441 mCapture->expectChildColor(64, 64);
4442 }
4443 }
TearDown()4444 void TearDown() override {
4445 LayerUpdateTest::TearDown();
4446 mChild = 0;
4447 }
4448
4449 sp<SurfaceControl> mChild;
4450 std::unique_ptr<ScreenCapture> mCapture;
4451 };
4452
TEST_F(ChildLayerTest,ChildLayerPositioning)4453 TEST_F(ChildLayerTest, ChildLayerPositioning) {
4454 asTransaction([&](Transaction& t) {
4455 t.show(mChild);
4456 t.setPosition(mChild, 10, 10);
4457 t.setPosition(mFGSurfaceControl, 64, 64);
4458 });
4459
4460 {
4461 mCapture = screenshot();
4462 // Top left of foreground must now be visible
4463 mCapture->expectFGColor(64, 64);
4464 // But 10 pixels in we should see the child surface
4465 mCapture->expectChildColor(74, 74);
4466 // And 10 more pixels we should be back to the foreground surface
4467 mCapture->expectFGColor(84, 84);
4468 }
4469
4470 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
4471
4472 {
4473 mCapture = screenshot();
4474 // Top left of foreground should now be at 0, 0
4475 mCapture->expectFGColor(0, 0);
4476 // But 10 pixels in we should see the child surface
4477 mCapture->expectChildColor(10, 10);
4478 // And 10 more pixels we should be back to the foreground surface
4479 mCapture->expectFGColor(20, 20);
4480 }
4481 }
4482
TEST_F(ChildLayerTest,ChildLayerCropping)4483 TEST_F(ChildLayerTest, ChildLayerCropping) {
4484 asTransaction([&](Transaction& t) {
4485 t.show(mChild);
4486 t.setPosition(mChild, 0, 0);
4487 t.setPosition(mFGSurfaceControl, 0, 0);
4488 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
4489 });
4490
4491 {
4492 mCapture = screenshot();
4493 mCapture->expectChildColor(0, 0);
4494 mCapture->expectChildColor(4, 4);
4495 mCapture->expectBGColor(5, 5);
4496 }
4497 }
4498
TEST_F(ChildLayerTest,ChildLayerConstraints)4499 TEST_F(ChildLayerTest, ChildLayerConstraints) {
4500 asTransaction([&](Transaction& t) {
4501 t.show(mChild);
4502 t.setPosition(mFGSurfaceControl, 0, 0);
4503 t.setPosition(mChild, 63, 63);
4504 });
4505
4506 {
4507 mCapture = screenshot();
4508 mCapture->expectFGColor(0, 0);
4509 // Last pixel in foreground should now be the child.
4510 mCapture->expectChildColor(63, 63);
4511 // But the child should be constrained and the next pixel
4512 // must be the background
4513 mCapture->expectBGColor(64, 64);
4514 }
4515 }
4516
TEST_F(ChildLayerTest,ChildLayerScaling)4517 TEST_F(ChildLayerTest, ChildLayerScaling) {
4518 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
4519
4520 // Find the boundary between the parent and child
4521 {
4522 mCapture = screenshot();
4523 mCapture->expectChildColor(9, 9);
4524 mCapture->expectFGColor(10, 10);
4525 }
4526
4527 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
4528
4529 // The boundary should be twice as far from the origin now.
4530 // The pixels from the last test should all be child now
4531 {
4532 mCapture = screenshot();
4533 mCapture->expectChildColor(9, 9);
4534 mCapture->expectChildColor(10, 10);
4535 mCapture->expectChildColor(19, 19);
4536 mCapture->expectFGColor(20, 20);
4537 }
4538 }
4539
4540 // A child with a scale transform should be cropped by its parent bounds.
TEST_F(ChildLayerTest,ChildLayerScalingCroppedByParent)4541 TEST_F(ChildLayerTest, ChildLayerScalingCroppedByParent) {
4542 asTransaction([&](Transaction& t) {
4543 t.setPosition(mFGSurfaceControl, 0, 0);
4544 t.setPosition(mChild, 0, 0);
4545 });
4546
4547 // Find the boundary between the parent and child.
4548 {
4549 mCapture = screenshot();
4550 mCapture->expectChildColor(0, 0);
4551 mCapture->expectChildColor(9, 9);
4552 mCapture->expectFGColor(10, 10);
4553 }
4554
4555 asTransaction([&](Transaction& t) { t.setMatrix(mChild, 10.0, 0, 0, 10.0); });
4556
4557 // The child should fill its parent bounds and be cropped by it.
4558 {
4559 mCapture = screenshot();
4560 mCapture->expectChildColor(0, 0);
4561 mCapture->expectChildColor(63, 63);
4562 mCapture->expectBGColor(64, 64);
4563 }
4564 }
4565
TEST_F(ChildLayerTest,ChildLayerAlpha)4566 TEST_F(ChildLayerTest, ChildLayerAlpha) {
4567 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
4568 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
4569 fillSurfaceRGBA8(mChild, 0, 254, 0);
4570 waitForPostedBuffers();
4571
4572 asTransaction([&](Transaction& t) {
4573 t.show(mChild);
4574 t.setPosition(mChild, 0, 0);
4575 t.setPosition(mFGSurfaceControl, 0, 0);
4576 });
4577
4578 {
4579 mCapture = screenshot();
4580 // Unblended child color
4581 mCapture->checkPixel(0, 0, 0, 254, 0);
4582 }
4583
4584 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
4585
4586 {
4587 mCapture = screenshot();
4588 // Child and BG blended.
4589 mCapture->checkPixel(0, 0, 127, 127, 0);
4590 }
4591
4592 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
4593
4594 {
4595 mCapture = screenshot();
4596 // Child and BG blended.
4597 mCapture->checkPixel(0, 0, 95, 64, 95);
4598 }
4599 }
4600
TEST_F(ChildLayerTest,ReparentChildren)4601 TEST_F(ChildLayerTest, ReparentChildren) {
4602 asTransaction([&](Transaction& t) {
4603 t.show(mChild);
4604 t.setPosition(mChild, 10, 10);
4605 t.setPosition(mFGSurfaceControl, 64, 64);
4606 });
4607
4608 {
4609 mCapture = screenshot();
4610 // Top left of foreground must now be visible
4611 mCapture->expectFGColor(64, 64);
4612 // But 10 pixels in we should see the child surface
4613 mCapture->expectChildColor(74, 74);
4614 // And 10 more pixels we should be back to the foreground surface
4615 mCapture->expectFGColor(84, 84);
4616 }
4617
4618 asTransaction([&](Transaction& t) {
4619 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
4620 });
4621
4622 {
4623 mCapture = screenshot();
4624 mCapture->expectFGColor(64, 64);
4625 // In reparenting we should have exposed the entire foreground surface.
4626 mCapture->expectFGColor(74, 74);
4627 // And the child layer should now begin at 10, 10 (since the BG
4628 // layer is at (0, 0)).
4629 mCapture->expectBGColor(9, 9);
4630 mCapture->expectChildColor(10, 10);
4631 }
4632 }
4633
TEST_F(ChildLayerTest,ChildrenSurviveParentDestruction)4634 TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
4635 sp<SurfaceControl> mGrandChild =
4636 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
4637 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
4638
4639 {
4640 SCOPED_TRACE("Grandchild visible");
4641 ScreenCapture::captureScreen(&mCapture);
4642 mCapture->checkPixel(64, 64, 111, 111, 111);
4643 }
4644
4645 mChild.clear();
4646
4647 {
4648 SCOPED_TRACE("After destroying child");
4649 ScreenCapture::captureScreen(&mCapture);
4650 mCapture->expectFGColor(64, 64);
4651 }
4652
4653 asTransaction([&](Transaction& t) {
4654 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
4655 });
4656
4657 {
4658 SCOPED_TRACE("After reparenting grandchild");
4659 ScreenCapture::captureScreen(&mCapture);
4660 mCapture->checkPixel(64, 64, 111, 111, 111);
4661 }
4662 }
4663
TEST_F(ChildLayerTest,DetachChildrenSameClient)4664 TEST_F(ChildLayerTest, DetachChildrenSameClient) {
4665 asTransaction([&](Transaction& t) {
4666 t.show(mChild);
4667 t.setPosition(mChild, 10, 10);
4668 t.setPosition(mFGSurfaceControl, 64, 64);
4669 });
4670
4671 {
4672 mCapture = screenshot();
4673 // Top left of foreground must now be visible
4674 mCapture->expectFGColor(64, 64);
4675 // But 10 pixels in we should see the child surface
4676 mCapture->expectChildColor(74, 74);
4677 // And 10 more pixels we should be back to the foreground surface
4678 mCapture->expectFGColor(84, 84);
4679 }
4680
4681
4682 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
4683
4684 asTransaction([&](Transaction& t) { t.hide(mChild); });
4685
4686 // Since the child has the same client as the parent, it will not get
4687 // detached and will be hidden.
4688 {
4689 mCapture = screenshot();
4690 mCapture->expectFGColor(64, 64);
4691 mCapture->expectFGColor(74, 74);
4692 mCapture->expectFGColor(84, 84);
4693 }
4694 }
4695
TEST_F(ChildLayerTest,DetachChildrenDifferentClient)4696 TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
4697 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
4698 sp<SurfaceControl> mChildNewClient =
4699 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
4700 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4701
4702 ASSERT_TRUE(mChildNewClient->isValid());
4703
4704 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
4705
4706 asTransaction([&](Transaction& t) {
4707 t.hide(mChild);
4708 t.show(mChildNewClient);
4709 t.setPosition(mChildNewClient, 10, 10);
4710 t.setPosition(mFGSurfaceControl, 64, 64);
4711 });
4712
4713 {
4714 mCapture = screenshot();
4715 // Top left of foreground must now be visible
4716 mCapture->expectFGColor(64, 64);
4717 // But 10 pixels in we should see the child surface
4718 mCapture->expectChildColor(74, 74);
4719 // And 10 more pixels we should be back to the foreground surface
4720 mCapture->expectFGColor(84, 84);
4721 }
4722
4723 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
4724
4725 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
4726
4727 // Nothing should have changed.
4728 {
4729 mCapture = screenshot();
4730 mCapture->expectFGColor(64, 64);
4731 mCapture->expectChildColor(74, 74);
4732 mCapture->expectFGColor(84, 84);
4733 }
4734 }
4735
TEST_F(ChildLayerTest,DetachChildrenThenAttach)4736 TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
4737 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4738 sp<SurfaceControl> childNewClient =
4739 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4740 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4741
4742 ASSERT_TRUE(childNewClient != nullptr);
4743 ASSERT_TRUE(childNewClient->isValid());
4744
4745 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4746
4747 Transaction()
4748 .hide(mChild)
4749 .show(childNewClient)
4750 .setPosition(childNewClient, 10, 10)
4751 .setPosition(mFGSurfaceControl, 64, 64)
4752 .apply();
4753
4754 {
4755 mCapture = screenshot();
4756 // Top left of foreground must now be visible
4757 mCapture->expectFGColor(64, 64);
4758 // But 10 pixels in we should see the child surface
4759 mCapture->expectChildColor(74, 74);
4760 // And 10 more pixels we should be back to the foreground surface
4761 mCapture->expectFGColor(84, 84);
4762 }
4763
4764 Transaction().detachChildren(mFGSurfaceControl).apply();
4765 Transaction().hide(childNewClient).apply();
4766
4767 // Nothing should have changed.
4768 {
4769 mCapture = screenshot();
4770 mCapture->expectFGColor(64, 64);
4771 mCapture->expectChildColor(74, 74);
4772 mCapture->expectFGColor(84, 84);
4773 }
4774
4775 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
4776 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
4777 32);
4778 Transaction()
4779 .setLayer(newParentSurface, INT32_MAX - 1)
4780 .show(newParentSurface)
4781 .setPosition(newParentSurface, 20, 20)
4782 .reparent(childNewClient, newParentSurface->getHandle())
4783 .apply();
4784 {
4785 mCapture = screenshot();
4786 // Child is now hidden.
4787 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
4788 }
4789 }
TEST_F(ChildLayerTest,DetachChildrenWithDeferredTransaction)4790 TEST_F(ChildLayerTest, DetachChildrenWithDeferredTransaction) {
4791 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4792 sp<SurfaceControl> childNewClient =
4793 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4794 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4795
4796 ASSERT_TRUE(childNewClient != nullptr);
4797 ASSERT_TRUE(childNewClient->isValid());
4798
4799 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4800
4801 Transaction()
4802 .hide(mChild)
4803 .show(childNewClient)
4804 .setPosition(childNewClient, 10, 10)
4805 .setPosition(mFGSurfaceControl, 64, 64)
4806 .apply();
4807
4808 {
4809 mCapture = screenshot();
4810 Rect rect = Rect(74, 74, 84, 84);
4811 mCapture->expectBorder(rect, Color{195, 63, 63, 255});
4812 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4813 }
4814
4815 Transaction()
4816 .deferTransactionUntil_legacy(childNewClient, mFGSurfaceControl->getHandle(),
4817 mFGSurfaceControl->getSurface()->getNextFrameNumber())
4818 .apply();
4819 Transaction().detachChildren(mFGSurfaceControl).apply();
4820 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(mFGSurfaceControl, Color::RED, 32, 32));
4821
4822 // BufferLayer can still dequeue buffers even though there's a detached layer with a
4823 // deferred transaction.
4824 {
4825 SCOPED_TRACE("new buffer");
4826 mCapture = screenshot();
4827 Rect rect = Rect(74, 74, 84, 84);
4828 mCapture->expectBorder(rect, Color::RED);
4829 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4830 }
4831 }
4832
TEST_F(ChildLayerTest,ChildrenInheritNonTransformScalingFromParent)4833 TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
4834 asTransaction([&](Transaction& t) {
4835 t.show(mChild);
4836 t.setPosition(mChild, 0, 0);
4837 t.setPosition(mFGSurfaceControl, 0, 0);
4838 });
4839
4840 {
4841 mCapture = screenshot();
4842 // We've positioned the child in the top left.
4843 mCapture->expectChildColor(0, 0);
4844 // But it's only 10x15.
4845 mCapture->expectFGColor(10, 15);
4846 }
4847
4848 asTransaction([&](Transaction& t) {
4849 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4850 // We cause scaling by 2.
4851 t.setSize(mFGSurfaceControl, 128, 128);
4852 });
4853
4854 {
4855 mCapture = screenshot();
4856 // We've positioned the child in the top left.
4857 mCapture->expectChildColor(0, 0);
4858 mCapture->expectChildColor(10, 10);
4859 mCapture->expectChildColor(19, 29);
4860 // And now it should be scaled all the way to 20x30
4861 mCapture->expectFGColor(20, 30);
4862 }
4863 }
4864
4865 // Regression test for b/37673612
TEST_F(ChildLayerTest,ChildrenWithParentBufferTransform)4866 TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
4867 asTransaction([&](Transaction& t) {
4868 t.show(mChild);
4869 t.setPosition(mChild, 0, 0);
4870 t.setPosition(mFGSurfaceControl, 0, 0);
4871 });
4872
4873 {
4874 mCapture = screenshot();
4875 // We've positioned the child in the top left.
4876 mCapture->expectChildColor(0, 0);
4877 mCapture->expectChildColor(9, 14);
4878 // But it's only 10x15.
4879 mCapture->expectFGColor(10, 15);
4880 }
4881 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
4882 // the WM specified state size.
4883 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
4884 sp<Surface> s = mFGSurfaceControl->getSurface();
4885 auto anw = static_cast<ANativeWindow*>(s.get());
4886 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4887 native_window_set_buffers_dimensions(anw, 64, 128);
4888 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4889 waitForPostedBuffers();
4890
4891 {
4892 // The child should still be in the same place and not have any strange scaling as in
4893 // b/37673612.
4894 mCapture = screenshot();
4895 mCapture->expectChildColor(0, 0);
4896 mCapture->expectFGColor(10, 10);
4897 }
4898 }
4899
4900 // A child with a buffer transform from its parents should be cropped by its parent bounds.
TEST_F(ChildLayerTest,ChildCroppedByParentWithBufferTransform)4901 TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferTransform) {
4902 asTransaction([&](Transaction& t) {
4903 t.show(mChild);
4904 t.setPosition(mChild, 0, 0);
4905 t.setPosition(mFGSurfaceControl, 0, 0);
4906 t.setSize(mChild, 100, 100);
4907 });
4908 fillSurfaceRGBA8(mChild, 200, 200, 200);
4909
4910 {
4911 mCapture = screenshot();
4912
4913 mCapture->expectChildColor(0, 0);
4914 mCapture->expectChildColor(63, 63);
4915 mCapture->expectBGColor(64, 64);
4916 }
4917
4918 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
4919 sp<Surface> s = mFGSurfaceControl->getSurface();
4920 auto anw = static_cast<ANativeWindow*>(s.get());
4921 // Apply a 90 transform on the buffer.
4922 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4923 native_window_set_buffers_dimensions(anw, 64, 128);
4924 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4925 waitForPostedBuffers();
4926
4927 // The child should be cropped by the new parent bounds.
4928 {
4929 mCapture = screenshot();
4930 mCapture->expectChildColor(0, 0);
4931 mCapture->expectChildColor(99, 63);
4932 mCapture->expectFGColor(100, 63);
4933 mCapture->expectBGColor(128, 64);
4934 }
4935 }
4936
4937 // A child with a scale transform from its parents should be cropped by its parent bounds.
TEST_F(ChildLayerTest,ChildCroppedByParentWithBufferScale)4938 TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferScale) {
4939 asTransaction([&](Transaction& t) {
4940 t.show(mChild);
4941 t.setPosition(mChild, 0, 0);
4942 t.setPosition(mFGSurfaceControl, 0, 0);
4943 t.setSize(mChild, 200, 200);
4944 });
4945 fillSurfaceRGBA8(mChild, 200, 200, 200);
4946
4947 {
4948 mCapture = screenshot();
4949
4950 mCapture->expectChildColor(0, 0);
4951 mCapture->expectChildColor(63, 63);
4952 mCapture->expectBGColor(64, 64);
4953 }
4954
4955 asTransaction([&](Transaction& t) {
4956 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4957 // Set a scaling by 2.
4958 t.setSize(mFGSurfaceControl, 128, 128);
4959 });
4960
4961 // Child should inherit its parents scale but should be cropped by its parent bounds.
4962 {
4963 mCapture = screenshot();
4964 mCapture->expectChildColor(0, 0);
4965 mCapture->expectChildColor(127, 127);
4966 mCapture->expectBGColor(128, 128);
4967 }
4968 }
4969
4970 // Regression test for b/127368943
4971 // Child should ignore the buffer transform but apply parent scale transform.
TEST_F(ChildLayerTest,ChildrenWithParentBufferTransformAndScale)4972 TEST_F(ChildLayerTest, ChildrenWithParentBufferTransformAndScale) {
4973 asTransaction([&](Transaction& t) {
4974 t.show(mChild);
4975 t.setPosition(mChild, 0, 0);
4976 t.setPosition(mFGSurfaceControl, 0, 0);
4977 });
4978
4979 {
4980 mCapture = screenshot();
4981 mCapture->expectChildColor(0, 0);
4982 mCapture->expectChildColor(9, 14);
4983 mCapture->expectFGColor(10, 15);
4984 }
4985
4986 // Change the size of the foreground to 128 * 64 so we can test rotation as well.
4987 asTransaction([&](Transaction& t) {
4988 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4989 t.setSize(mFGSurfaceControl, 128, 64);
4990 });
4991 sp<Surface> s = mFGSurfaceControl->getSurface();
4992 auto anw = static_cast<ANativeWindow*>(s.get());
4993 // Apply a 90 transform on the buffer and submit a buffer half the expected size so that we
4994 // have an effective scale of 2.0 applied to the buffer along with a rotation transform.
4995 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4996 native_window_set_buffers_dimensions(anw, 32, 64);
4997 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4998 waitForPostedBuffers();
4999
5000 // The child should ignore the buffer transform but apply the 2.0 scale from parent.
5001 {
5002 mCapture = screenshot();
5003 mCapture->expectChildColor(0, 0);
5004 mCapture->expectChildColor(19, 29);
5005 mCapture->expectFGColor(20, 30);
5006 }
5007 }
5008
TEST_F(ChildLayerTest,Bug36858924)5009 TEST_F(ChildLayerTest, Bug36858924) {
5010 // Destroy the child layer
5011 mChild.clear();
5012
5013 // Now recreate it as hidden
5014 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
5015 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
5016
5017 // Show the child layer in a deferred transaction
5018 asTransaction([&](Transaction& t) {
5019 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
5020 mFGSurfaceControl->getSurface()->getNextFrameNumber());
5021 t.show(mChild);
5022 });
5023
5024 // Render the foreground surface a few times
5025 //
5026 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
5027 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
5028 // never acquire/release the first buffer
5029 ALOGI("Filling 1");
5030 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
5031 ALOGI("Filling 2");
5032 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
5033 ALOGI("Filling 3");
5034 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
5035 ALOGI("Filling 4");
5036 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
5037 }
5038
TEST_F(ChildLayerTest,Reparent)5039 TEST_F(ChildLayerTest, Reparent) {
5040 asTransaction([&](Transaction& t) {
5041 t.show(mChild);
5042 t.setPosition(mChild, 10, 10);
5043 t.setPosition(mFGSurfaceControl, 64, 64);
5044 });
5045
5046 {
5047 mCapture = screenshot();
5048 // Top left of foreground must now be visible
5049 mCapture->expectFGColor(64, 64);
5050 // But 10 pixels in we should see the child surface
5051 mCapture->expectChildColor(74, 74);
5052 // And 10 more pixels we should be back to the foreground surface
5053 mCapture->expectFGColor(84, 84);
5054 }
5055
5056 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
5057
5058 {
5059 mCapture = screenshot();
5060 mCapture->expectFGColor(64, 64);
5061 // In reparenting we should have exposed the entire foreground surface.
5062 mCapture->expectFGColor(74, 74);
5063 // And the child layer should now begin at 10, 10 (since the BG
5064 // layer is at (0, 0)).
5065 mCapture->expectBGColor(9, 9);
5066 mCapture->expectChildColor(10, 10);
5067 }
5068 }
5069
TEST_F(ChildLayerTest,ReparentToNoParent)5070 TEST_F(ChildLayerTest, ReparentToNoParent) {
5071 asTransaction([&](Transaction& t) {
5072 t.show(mChild);
5073 t.setPosition(mChild, 10, 10);
5074 t.setPosition(mFGSurfaceControl, 64, 64);
5075 });
5076
5077 {
5078 mCapture = screenshot();
5079 // Top left of foreground must now be visible
5080 mCapture->expectFGColor(64, 64);
5081 // But 10 pixels in we should see the child surface
5082 mCapture->expectChildColor(74, 74);
5083 // And 10 more pixels we should be back to the foreground surface
5084 mCapture->expectFGColor(84, 84);
5085 }
5086 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
5087 {
5088 mCapture = screenshot();
5089 // The surface should now be offscreen.
5090 mCapture->expectFGColor(64, 64);
5091 mCapture->expectFGColor(74, 74);
5092 mCapture->expectFGColor(84, 84);
5093 }
5094 }
5095
TEST_F(ChildLayerTest,ReparentFromNoParent)5096 TEST_F(ChildLayerTest, ReparentFromNoParent) {
5097 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
5098 ASSERT_TRUE(newSurface != nullptr);
5099 ASSERT_TRUE(newSurface->isValid());
5100
5101 fillSurfaceRGBA8(newSurface, 63, 195, 63);
5102 asTransaction([&](Transaction& t) {
5103 t.hide(mChild);
5104 t.show(newSurface);
5105 t.setPosition(newSurface, 10, 10);
5106 t.setLayer(newSurface, INT32_MAX - 2);
5107 t.setPosition(mFGSurfaceControl, 64, 64);
5108 });
5109
5110 {
5111 mCapture = screenshot();
5112 // Top left of foreground must now be visible
5113 mCapture->expectFGColor(64, 64);
5114 // At 10, 10 we should see the new surface
5115 mCapture->checkPixel(10, 10, 63, 195, 63);
5116 }
5117
5118 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
5119
5120 {
5121 mCapture = screenshot();
5122 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
5123 // mFGSurface, putting it at 74, 74.
5124 mCapture->expectFGColor(64, 64);
5125 mCapture->checkPixel(74, 74, 63, 195, 63);
5126 mCapture->expectFGColor(84, 84);
5127 }
5128 }
5129
TEST_F(ChildLayerTest,NestedChildren)5130 TEST_F(ChildLayerTest, NestedChildren) {
5131 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
5132 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
5133 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5134
5135 {
5136 mCapture = screenshot();
5137 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
5138 // which begins at 64, 64
5139 mCapture->checkPixel(64, 64, 50, 50, 50);
5140 }
5141 }
5142
TEST_F(ChildLayerTest,ChildLayerRelativeLayer)5143 TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
5144 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
5145 fillSurfaceRGBA8(relative, 255, 255, 255);
5146
5147 Transaction t;
5148 t.setLayer(relative, INT32_MAX)
5149 .setRelativeLayer(mChild, relative->getHandle(), 1)
5150 .setPosition(mFGSurfaceControl, 0, 0)
5151 .apply(true);
5152
5153 // We expect that the child should have been elevated above our
5154 // INT_MAX layer even though it's not a child of it.
5155 {
5156 mCapture = screenshot();
5157 mCapture->expectChildColor(0, 0);
5158 mCapture->expectChildColor(9, 9);
5159 mCapture->checkPixel(10, 10, 255, 255, 255);
5160 }
5161 }
5162
5163 class BoundlessLayerTest : public LayerUpdateTest {
5164 protected:
5165 std::unique_ptr<ScreenCapture> mCapture;
5166 };
5167
5168 // Verify setting a size on a buffer layer has no effect.
TEST_F(BoundlessLayerTest,BufferLayerIgnoresSize)5169 TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
5170 sp<SurfaceControl> bufferLayer =
5171 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
5172 mFGSurfaceControl.get());
5173 ASSERT_TRUE(bufferLayer->isValid());
5174 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
5175 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
5176 {
5177 mCapture = screenshot();
5178 // Top left of background must now be visible
5179 mCapture->expectBGColor(0, 0);
5180 // Foreground Surface bounds must be color layer
5181 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
5182 // Buffer layer should not extend past buffer bounds
5183 mCapture->expectFGColor(95, 95);
5184 }
5185 }
5186
5187 // Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
5188 // which will crop the color layer.
TEST_F(BoundlessLayerTest,BoundlessColorLayerFillsParentBufferBounds)5189 TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
5190 sp<SurfaceControl> colorLayer =
5191 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5192 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
5193 ASSERT_TRUE(colorLayer->isValid());
5194 asTransaction([&](Transaction& t) {
5195 t.setColor(colorLayer, half3{0, 0, 0});
5196 t.show(colorLayer);
5197 });
5198 {
5199 mCapture = screenshot();
5200 // Top left of background must now be visible
5201 mCapture->expectBGColor(0, 0);
5202 // Foreground Surface bounds must be color layer
5203 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
5204 // Color layer should not extend past foreground bounds
5205 mCapture->expectBGColor(129, 129);
5206 }
5207 }
5208
5209 // Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
5210 // a crop which will be used to crop the color layer.
TEST_F(BoundlessLayerTest,BoundlessColorLayerFillsParentCropBounds)5211 TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
5212 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5213 0 /* flags */, mFGSurfaceControl.get());
5214 ASSERT_TRUE(cropLayer->isValid());
5215 sp<SurfaceControl> colorLayer =
5216 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5217 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
5218 ASSERT_TRUE(colorLayer->isValid());
5219 asTransaction([&](Transaction& t) {
5220 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
5221 t.setColor(colorLayer, half3{0, 0, 0});
5222 t.show(cropLayer);
5223 t.show(colorLayer);
5224 });
5225 {
5226 mCapture = screenshot();
5227 // Top left of background must now be visible
5228 mCapture->expectBGColor(0, 0);
5229 // Top left of foreground must now be visible
5230 mCapture->expectFGColor(64, 64);
5231 // 5 pixels from the foreground we should see the child surface
5232 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
5233 // 10 pixels from the foreground we should be back to the foreground surface
5234 mCapture->expectFGColor(74, 74);
5235 }
5236 }
5237
5238 // Verify for boundless layer with no children, their transforms have no effect.
TEST_F(BoundlessLayerTest,BoundlessColorLayerTransformHasNoEffect)5239 TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
5240 sp<SurfaceControl> colorLayer =
5241 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5242 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
5243 ASSERT_TRUE(colorLayer->isValid());
5244 asTransaction([&](Transaction& t) {
5245 t.setPosition(colorLayer, 320, 320);
5246 t.setMatrix(colorLayer, 2, 0, 0, 2);
5247 t.setColor(colorLayer, half3{0, 0, 0});
5248 t.show(colorLayer);
5249 });
5250 {
5251 mCapture = screenshot();
5252 // Top left of background must now be visible
5253 mCapture->expectBGColor(0, 0);
5254 // Foreground Surface bounds must be color layer
5255 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
5256 // Color layer should not extend past foreground bounds
5257 mCapture->expectBGColor(129, 129);
5258 }
5259 }
5260
5261 // Verify for boundless layer with children, their transforms have an effect.
TEST_F(BoundlessLayerTest,IntermediateBoundlessLayerCanSetTransform)5262 TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
5263 sp<SurfaceControl> boundlessLayerRightShift =
5264 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
5265 0 /* flags */, mFGSurfaceControl.get());
5266 ASSERT_TRUE(boundlessLayerRightShift->isValid());
5267 sp<SurfaceControl> boundlessLayerDownShift =
5268 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
5269 0 /* flags */, boundlessLayerRightShift.get());
5270 ASSERT_TRUE(boundlessLayerDownShift->isValid());
5271 sp<SurfaceControl> colorLayer =
5272 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5273 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
5274 ASSERT_TRUE(colorLayer->isValid());
5275 asTransaction([&](Transaction& t) {
5276 t.setPosition(boundlessLayerRightShift, 32, 0);
5277 t.show(boundlessLayerRightShift);
5278 t.setPosition(boundlessLayerDownShift, 0, 32);
5279 t.show(boundlessLayerDownShift);
5280 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
5281 t.setColor(colorLayer, half3{0, 0, 0});
5282 t.show(colorLayer);
5283 });
5284 {
5285 mCapture = screenshot();
5286 // Top left of background must now be visible
5287 mCapture->expectBGColor(0, 0);
5288 // Top left of foreground must now be visible
5289 mCapture->expectFGColor(64, 64);
5290 // Foreground Surface bounds must be color layer
5291 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
5292 // Color layer should not extend past foreground bounds
5293 mCapture->expectBGColor(129, 129);
5294 }
5295 }
5296
5297 // Verify child layers do not get clipped if they temporarily move into the negative
5298 // coordinate space as the result of an intermediate transformation.
TEST_F(BoundlessLayerTest,IntermediateBoundlessLayerDoNotCrop)5299 TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
5300 sp<SurfaceControl> boundlessLayer =
5301 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
5302 0 /* flags */, mFGSurfaceControl.get());
5303 ASSERT_TRUE(boundlessLayer != nullptr);
5304 ASSERT_TRUE(boundlessLayer->isValid());
5305 sp<SurfaceControl> colorLayer =
5306 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
5307 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
5308 ASSERT_TRUE(colorLayer != nullptr);
5309 ASSERT_TRUE(colorLayer->isValid());
5310 asTransaction([&](Transaction& t) {
5311 // shift child layer off bounds. If this layer was not boundless, we will
5312 // expect the child layer to be cropped.
5313 t.setPosition(boundlessLayer, 32, 32);
5314 t.show(boundlessLayer);
5315 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
5316 // undo shift by parent
5317 t.setPosition(colorLayer, -32, -32);
5318 t.setColor(colorLayer, half3{0, 0, 0});
5319 t.show(colorLayer);
5320 });
5321 {
5322 mCapture = screenshot();
5323 // Top left of background must now be visible
5324 mCapture->expectBGColor(0, 0);
5325 // Foreground Surface bounds must be color layer
5326 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
5327 // Color layer should not extend past foreground bounds
5328 mCapture->expectBGColor(129, 129);
5329 }
5330 }
5331
5332 // Verify for boundless root layers with children, their transforms have an effect.
TEST_F(BoundlessLayerTest,RootBoundlessLayerCanSetTransform)5333 TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
5334 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
5335 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
5336 ASSERT_TRUE(rootBoundlessLayer->isValid());
5337 sp<SurfaceControl> colorLayer =
5338 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
5339 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
5340
5341 ASSERT_TRUE(colorLayer->isValid());
5342 asTransaction([&](Transaction& t) {
5343 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
5344 t.setPosition(rootBoundlessLayer, 32, 32);
5345 t.show(rootBoundlessLayer);
5346 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
5347 t.setColor(colorLayer, half3{0, 0, 0});
5348 t.show(colorLayer);
5349 t.hide(mFGSurfaceControl);
5350 });
5351 {
5352 mCapture = screenshot();
5353 // Top left of background must now be visible
5354 mCapture->expectBGColor(0, 0);
5355 // Top left of foreground must now be visible
5356 mCapture->expectBGColor(31, 31);
5357 // Foreground Surface bounds must be color layer
5358 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
5359 // Color layer should not extend past foreground bounds
5360 mCapture->expectBGColor(97, 97);
5361 }
5362 }
5363
5364 class ScreenCaptureTest : public LayerUpdateTest {
5365 protected:
5366 std::unique_ptr<ScreenCapture> mCapture;
5367 };
5368
TEST_F(ScreenCaptureTest,CaptureSingleLayer)5369 TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
5370 auto bgHandle = mBGSurfaceControl->getHandle();
5371 ScreenCapture::captureLayers(&mCapture, bgHandle);
5372 mCapture->expectBGColor(0, 0);
5373 // Doesn't capture FG layer which is at 64, 64
5374 mCapture->expectBGColor(64, 64);
5375 }
5376
TEST_F(ScreenCaptureTest,CaptureLayerWithChild)5377 TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
5378 auto fgHandle = mFGSurfaceControl->getHandle();
5379
5380 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5381 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5382 fillSurfaceRGBA8(child, 200, 200, 200);
5383
5384 SurfaceComposerClient::Transaction().show(child).apply(true);
5385
5386 // Captures mFGSurfaceControl layer and its child.
5387 ScreenCapture::captureLayers(&mCapture, fgHandle);
5388 mCapture->expectFGColor(10, 10);
5389 mCapture->expectChildColor(0, 0);
5390 }
5391
TEST_F(ScreenCaptureTest,CaptureLayerChildOnly)5392 TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
5393 auto fgHandle = mFGSurfaceControl->getHandle();
5394
5395 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5396 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5397 fillSurfaceRGBA8(child, 200, 200, 200);
5398
5399 SurfaceComposerClient::Transaction().show(child).apply(true);
5400
5401 // Captures mFGSurfaceControl's child
5402 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
5403 mCapture->checkPixel(10, 10, 0, 0, 0);
5404 mCapture->expectChildColor(0, 0);
5405 }
5406
TEST_F(ScreenCaptureTest,CaptureLayerExclude)5407 TEST_F(ScreenCaptureTest, CaptureLayerExclude) {
5408 auto fgHandle = mFGSurfaceControl->getHandle();
5409
5410 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5411 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5412 fillSurfaceRGBA8(child, 200, 200, 200);
5413 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
5414 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5415 fillSurfaceRGBA8(child2, 200, 0, 200);
5416
5417 SurfaceComposerClient::Transaction()
5418 .show(child)
5419 .show(child2)
5420 .setLayer(child, 1)
5421 .setLayer(child2, 2)
5422 .apply(true);
5423
5424 // Child2 would be visible but its excluded, so we should see child1 color instead.
5425 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
5426 mCapture->checkPixel(10, 10, 0, 0, 0);
5427 mCapture->checkPixel(0, 0, 200, 200, 200);
5428 }
5429
5430 // Like the last test but verifies that children are also exclude.
TEST_F(ScreenCaptureTest,CaptureLayerExcludeTree)5431 TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) {
5432 auto fgHandle = mFGSurfaceControl->getHandle();
5433
5434 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5435 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5436 fillSurfaceRGBA8(child, 200, 200, 200);
5437 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
5438 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5439 fillSurfaceRGBA8(child2, 200, 0, 200);
5440 sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10,
5441 PIXEL_FORMAT_RGBA_8888, 0, child2.get());
5442 fillSurfaceRGBA8(child2, 200, 0, 200);
5443
5444 SurfaceComposerClient::Transaction()
5445 .show(child)
5446 .show(child2)
5447 .show(child3)
5448 .setLayer(child, 1)
5449 .setLayer(child2, 2)
5450 .apply(true);
5451
5452 // Child2 would be visible but its excluded, so we should see child1 color instead.
5453 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
5454 mCapture->checkPixel(10, 10, 0, 0, 0);
5455 mCapture->checkPixel(0, 0, 200, 200, 200);
5456 }
5457
TEST_F(ScreenCaptureTest,CaptureTransparent)5458 TEST_F(ScreenCaptureTest, CaptureTransparent) {
5459 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5460 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5461
5462 fillSurfaceRGBA8(child, 200, 200, 200);
5463
5464 SurfaceComposerClient::Transaction().show(child).apply(true);
5465
5466 auto childHandle = child->getHandle();
5467
5468 // Captures child
5469 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
5470 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
5471 // Area outside of child's bounds is transparent.
5472 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
5473 }
5474
TEST_F(ScreenCaptureTest,DontCaptureRelativeOutsideTree)5475 TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
5476 auto fgHandle = mFGSurfaceControl->getHandle();
5477
5478 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5479 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5480 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
5481 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
5482 fillSurfaceRGBA8(child, 200, 200, 200);
5483 fillSurfaceRGBA8(relative, 100, 100, 100);
5484
5485 SurfaceComposerClient::Transaction()
5486 .show(child)
5487 // Set relative layer above fg layer so should be shown above when computing all layers.
5488 .setRelativeLayer(relative, fgHandle, 1)
5489 .show(relative)
5490 .apply(true);
5491
5492 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
5493 ScreenCapture::captureLayers(&mCapture, fgHandle);
5494 mCapture->expectFGColor(10, 10);
5495 mCapture->expectChildColor(0, 0);
5496 }
5497
TEST_F(ScreenCaptureTest,CaptureRelativeInTree)5498 TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
5499 auto fgHandle = mFGSurfaceControl->getHandle();
5500
5501 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5502 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5503 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
5504 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5505 fillSurfaceRGBA8(child, 200, 200, 200);
5506 fillSurfaceRGBA8(relative, 100, 100, 100);
5507
5508 SurfaceComposerClient::Transaction()
5509 .show(child)
5510 // Set relative layer below fg layer but relative to child layer so it should be shown
5511 // above child layer.
5512 .setLayer(relative, -1)
5513 .setRelativeLayer(relative, child->getHandle(), 1)
5514 .show(relative)
5515 .apply(true);
5516
5517 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
5518 // relative value should be taken into account, placing it above child layer.
5519 ScreenCapture::captureLayers(&mCapture, fgHandle);
5520 mCapture->expectFGColor(10, 10);
5521 // Relative layer is showing on top of child layer
5522 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
5523 }
5524
5525 // In the following tests we verify successful skipping of a parent layer,
5526 // so we use the same verification logic and only change how we mutate
5527 // the parent layer to verify that various properties are ignored.
5528 class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
5529 public:
SetUp()5530 void SetUp() override {
5531 LayerUpdateTest::SetUp();
5532
5533 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
5534 mFGSurfaceControl.get());
5535 fillSurfaceRGBA8(mChild, 200, 200, 200);
5536
5537 SurfaceComposerClient::Transaction().show(mChild).apply(true);
5538 }
5539
verify(std::function<void ()> verifyStartingState)5540 void verify(std::function<void()> verifyStartingState) {
5541 // Verify starting state before a screenshot is taken.
5542 verifyStartingState();
5543
5544 // Verify child layer does not inherit any of the properties of its
5545 // parent when its screenshot is captured.
5546 auto fgHandle = mFGSurfaceControl->getHandle();
5547 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
5548 mCapture->checkPixel(10, 10, 0, 0, 0);
5549 mCapture->expectChildColor(0, 0);
5550
5551 // Verify all assumptions are still true after the screenshot is taken.
5552 verifyStartingState();
5553 }
5554
5555 std::unique_ptr<ScreenCapture> mCapture;
5556 sp<SurfaceControl> mChild;
5557 };
5558
5559 // Regression test b/76099859
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresParentVisibility)5560 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
5561
5562 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
5563
5564 // Even though the parent is hidden we should still capture the child.
5565
5566 // Before and after reparenting, verify child is properly hidden
5567 // when rendering full-screen.
5568 verify([&] { screenshot()->expectBGColor(64, 64); });
5569 }
5570
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresParentCrop)5571 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
5572 SurfaceComposerClient::Transaction()
5573 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
5574 .apply(true);
5575
5576 // Even though the parent is cropped out we should still capture the child.
5577
5578 // Before and after reparenting, verify child is cropped by parent.
5579 verify([&] { screenshot()->expectBGColor(65, 65); });
5580 }
5581
5582 // Regression test b/124372894
TEST_F(ScreenCaptureChildOnlyTest,CaptureLayerIgnoresTransform)5583 TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
5584 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
5585
5586 // We should not inherit the parent scaling.
5587
5588 // Before and after reparenting, verify child is properly scaled.
5589 verify([&] { screenshot()->expectChildColor(80, 80); });
5590 }
5591
5592
TEST_F(ScreenCaptureTest,CaptureLayerWithGrandchild)5593 TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
5594 auto fgHandle = mFGSurfaceControl->getHandle();
5595
5596 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5597 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5598 fillSurfaceRGBA8(child, 200, 200, 200);
5599
5600 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5601 PIXEL_FORMAT_RGBA_8888, 0, child.get());
5602
5603 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5604 SurfaceComposerClient::Transaction()
5605 .show(child)
5606 .setPosition(grandchild, 5, 5)
5607 .show(grandchild)
5608 .apply(true);
5609
5610 // Captures mFGSurfaceControl, its child, and the grandchild.
5611 ScreenCapture::captureLayers(&mCapture, fgHandle);
5612 mCapture->expectFGColor(10, 10);
5613 mCapture->expectChildColor(0, 0);
5614 mCapture->checkPixel(5, 5, 50, 50, 50);
5615 }
5616
TEST_F(ScreenCaptureTest,CaptureChildOnly)5617 TEST_F(ScreenCaptureTest, CaptureChildOnly) {
5618 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5619 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5620 fillSurfaceRGBA8(child, 200, 200, 200);
5621 auto childHandle = child->getHandle();
5622
5623 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
5624
5625 // Captures only the child layer, and not the parent.
5626 ScreenCapture::captureLayers(&mCapture, childHandle);
5627 mCapture->expectChildColor(0, 0);
5628 mCapture->expectChildColor(9, 9);
5629 }
5630
TEST_F(ScreenCaptureTest,CaptureGrandchildOnly)5631 TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
5632 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5633 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5634 fillSurfaceRGBA8(child, 200, 200, 200);
5635 auto childHandle = child->getHandle();
5636
5637 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5638 PIXEL_FORMAT_RGBA_8888, 0, child.get());
5639 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5640
5641 SurfaceComposerClient::Transaction()
5642 .show(child)
5643 .setPosition(grandchild, 5, 5)
5644 .show(grandchild)
5645 .apply(true);
5646
5647 auto grandchildHandle = grandchild->getHandle();
5648
5649 // Captures only the grandchild.
5650 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
5651 mCapture->checkPixel(0, 0, 50, 50, 50);
5652 mCapture->checkPixel(4, 4, 50, 50, 50);
5653 }
5654
TEST_F(ScreenCaptureTest,CaptureCrop)5655 TEST_F(ScreenCaptureTest, CaptureCrop) {
5656 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
5657 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5658 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
5659
5660 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5661 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
5662
5663 SurfaceComposerClient::Transaction()
5664 .setLayer(redLayer, INT32_MAX - 1)
5665 .show(redLayer)
5666 .show(blueLayer)
5667 .apply(true);
5668
5669 auto redLayerHandle = redLayer->getHandle();
5670
5671 // Capturing full screen should have both red and blue are visible.
5672 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5673 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5674 // red area below the blue area
5675 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5676 // red area to the right of the blue area
5677 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
5678
5679 const Rect crop = Rect(0, 0, 30, 30);
5680 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
5681 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
5682 // area visible.
5683 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5684 mCapture->checkPixel(30, 30, 0, 0, 0);
5685 }
5686
TEST_F(ScreenCaptureTest,CaptureSize)5687 TEST_F(ScreenCaptureTest, CaptureSize) {
5688 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
5689 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5690 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
5691
5692 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5693 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
5694
5695 SurfaceComposerClient::Transaction()
5696 .setLayer(redLayer, INT32_MAX - 1)
5697 .show(redLayer)
5698 .show(blueLayer)
5699 .apply(true);
5700
5701 auto redLayerHandle = redLayer->getHandle();
5702
5703 // Capturing full screen should have both red and blue are visible.
5704 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5705 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5706 // red area below the blue area
5707 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5708 // red area to the right of the blue area
5709 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
5710
5711 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
5712 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
5713 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
5714 // red area below the blue area
5715 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
5716 // red area to the right of the blue area
5717 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
5718 mCapture->checkPixel(30, 30, 0, 0, 0);
5719 }
5720
TEST_F(ScreenCaptureTest,CaptureInvalidLayer)5721 TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
5722 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
5723
5724 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5725
5726 auto redLayerHandle = redLayer->getHandle();
5727 redLayer.clear();
5728 SurfaceComposerClient::Transaction().apply(true);
5729
5730 sp<GraphicBuffer> outBuffer;
5731
5732 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
5733 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
5734 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
5735 }
5736
5737
5738 class DereferenceSurfaceControlTest : public LayerTransactionTest {
5739 protected:
SetUp()5740 void SetUp() override {
5741 LayerTransactionTest::SetUp();
5742 bgLayer = createLayer("BG layer", 20, 20);
5743 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
5744 fgLayer = createLayer("FG layer", 20, 20);
5745 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
5746 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
5747 {
5748 SCOPED_TRACE("before anything");
5749 auto shot = screenshot();
5750 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5751 }
5752 }
TearDown()5753 void TearDown() override {
5754 LayerTransactionTest::TearDown();
5755 bgLayer = 0;
5756 fgLayer = 0;
5757 }
5758
5759 sp<SurfaceControl> bgLayer;
5760 sp<SurfaceControl> fgLayer;
5761 };
5762
TEST_F(DereferenceSurfaceControlTest,LayerNotInTransaction)5763 TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
5764 fgLayer = nullptr;
5765 {
5766 SCOPED_TRACE("after setting null");
5767 auto shot = screenshot();
5768 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
5769 }
5770 }
5771
TEST_F(DereferenceSurfaceControlTest,LayerInTransaction)5772 TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
5773 auto transaction = Transaction().show(fgLayer);
5774 fgLayer = nullptr;
5775 {
5776 SCOPED_TRACE("after setting null");
5777 auto shot = screenshot();
5778 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5779 }
5780 }
5781
5782 class MultiDisplayLayerBoundsTest : public LayerTransactionTest {
5783 protected:
SetUp()5784 virtual void SetUp() {
5785 LayerTransactionTest::SetUp();
5786 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5787
5788 mMainDisplay = SurfaceComposerClient::getInternalDisplayToken();
5789 SurfaceComposerClient::getDisplayInfo(mMainDisplay, &mMainDisplayInfo);
5790
5791 sp<IGraphicBufferConsumer> consumer;
5792 BufferQueue::createBufferQueue(&mProducer, &consumer);
5793 consumer->setConsumerName(String8("Virtual disp consumer"));
5794 consumer->setDefaultBufferSize(mMainDisplayInfo.w, mMainDisplayInfo.h);
5795 }
5796
TearDown()5797 virtual void TearDown() {
5798 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
5799 LayerTransactionTest::TearDown();
5800 mColorLayer = 0;
5801 }
5802
createDisplay(const Rect & layerStackRect,uint32_t layerStack)5803 void createDisplay(const Rect& layerStackRect, uint32_t layerStack) {
5804 mVirtualDisplay =
5805 SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), false /*secure*/);
5806 asTransaction([&](Transaction& t) {
5807 t.setDisplaySurface(mVirtualDisplay, mProducer);
5808 t.setDisplayLayerStack(mVirtualDisplay, layerStack);
5809 t.setDisplayProjection(mVirtualDisplay, mMainDisplayInfo.orientation, layerStackRect,
5810 Rect(mMainDisplayInfo.w, mMainDisplayInfo.h));
5811 });
5812 }
5813
createColorLayer(uint32_t layerStack)5814 void createColorLayer(uint32_t layerStack) {
5815 mColorLayer =
5816 createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */,
5817 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
5818 ASSERT_TRUE(mColorLayer != nullptr);
5819 ASSERT_TRUE(mColorLayer->isValid());
5820 asTransaction([&](Transaction& t) {
5821 t.setLayerStack(mColorLayer, layerStack);
5822 t.setCrop_legacy(mColorLayer, Rect(0, 0, 30, 40));
5823 t.setLayer(mColorLayer, INT32_MAX - 2);
5824 t.setColor(mColorLayer,
5825 half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f,
5826 mExpectedColor.b / 255.0f});
5827 t.show(mColorLayer);
5828 });
5829 }
5830
5831 DisplayInfo mMainDisplayInfo;
5832 sp<IBinder> mMainDisplay;
5833 sp<IBinder> mVirtualDisplay;
5834 sp<IGraphicBufferProducer> mProducer;
5835 sp<SurfaceControl> mColorLayer;
5836 Color mExpectedColor = {63, 63, 195, 255};
5837 };
5838
TEST_F(MultiDisplayLayerBoundsTest,RenderLayerInVirtualDisplay)5839 TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) {
5840 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 1 /* layerStack */);
5841 createColorLayer(1 /* layerStack */);
5842
5843 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5844
5845 // Verify color layer does not render on main display.
5846 std::unique_ptr<ScreenCapture> sc;
5847 ScreenCapture::captureScreen(&sc, mMainDisplay);
5848 sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255});
5849 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5850
5851 // Verify color layer renders correctly on virtual display.
5852 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5853 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5854 sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 0});
5855 }
5856
TEST_F(MultiDisplayLayerBoundsTest,RenderLayerInMirroredVirtualDisplay)5857 TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) {
5858 // Create a display and set its layer stack to the main display's layer stack so
5859 // the contents of the main display are mirrored on to the virtual display.
5860
5861 // Assumption here is that the new mirrored display has the same viewport as the
5862 // primary display that it is mirroring.
5863 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 0 /* layerStack */);
5864 createColorLayer(0 /* layerStack */);
5865
5866 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5867
5868 // Verify color layer renders correctly on main display and it is mirrored on the
5869 // virtual display.
5870 std::unique_ptr<ScreenCapture> sc;
5871 ScreenCapture::captureScreen(&sc, mMainDisplay);
5872 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5873 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5874
5875 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5876 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5877 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5878 }
5879
5880 class DisplayActiveConfigTest : public ::testing::Test {
5881 protected:
SetUp()5882 void SetUp() override {
5883 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
5884 SurfaceComposerClient::getDisplayConfigs(mDisplayToken, &mDisplayconfigs);
5885 EXPECT_GT(mDisplayconfigs.size(), 0);
5886
5887 // set display power to on to make sure config can be changed
5888 SurfaceComposerClient::setDisplayPowerMode(mDisplayToken, HWC_POWER_MODE_NORMAL);
5889 }
5890
5891 sp<IBinder> mDisplayToken;
5892 Vector<DisplayInfo> mDisplayconfigs;
5893 };
5894
TEST_F(DisplayActiveConfigTest,allConfigsAllowed)5895 TEST_F(DisplayActiveConfigTest, allConfigsAllowed) {
5896 std::vector<int32_t> allowedConfigs;
5897
5898 // Add all configs to the allowed configs
5899 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5900 allowedConfigs.push_back(i);
5901 }
5902
5903 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5904 EXPECT_EQ(res, NO_ERROR);
5905
5906 std::vector<int32_t> outConfigs;
5907 res = SurfaceComposerClient::getAllowedDisplayConfigs(mDisplayToken, &outConfigs);
5908 EXPECT_EQ(res, NO_ERROR);
5909 EXPECT_EQ(allowedConfigs, outConfigs);
5910 }
5911
TEST_F(DisplayActiveConfigTest,changeAllowedConfig)5912 TEST_F(DisplayActiveConfigTest, changeAllowedConfig) {
5913 // we need at least 2 configs available for this test
5914 if (mDisplayconfigs.size() <= 1) return;
5915
5916 int activeConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5917
5918 // We want to set the allowed config to everything but the active config
5919 std::vector<int32_t> allowedConfigs;
5920 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5921 if (i != activeConfig) {
5922 allowedConfigs.push_back(i);
5923 }
5924 }
5925
5926 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5927 EXPECT_EQ(res, NO_ERROR);
5928
5929 // Allow some time for the config change
5930 std::this_thread::sleep_for(200ms);
5931
5932 int newActiveConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5933 EXPECT_NE(activeConfig, newActiveConfig);
5934
5935 // Make sure the new config is part of allowed config
5936 EXPECT_TRUE(std::find(allowedConfigs.begin(), allowedConfigs.end(), newActiveConfig) !=
5937 allowedConfigs.end());
5938 }
5939
5940 class RelativeZTest : public LayerTransactionTest {
5941 protected:
SetUp()5942 virtual void SetUp() {
5943 LayerTransactionTest::SetUp();
5944 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5945
5946 const auto display = SurfaceComposerClient::getInternalDisplayToken();
5947 ASSERT_FALSE(display == nullptr);
5948
5949 // Back layer
5950 mBackgroundLayer = createColorLayer("Background layer", Color::RED);
5951
5952 // Front layer
5953 mForegroundLayer = createColorLayer("Foreground layer", Color::GREEN);
5954
5955 asTransaction([&](Transaction& t) {
5956 t.setDisplayLayerStack(display, 0);
5957 t.setLayer(mBackgroundLayer, INT32_MAX - 2).show(mBackgroundLayer);
5958 t.setLayer(mForegroundLayer, INT32_MAX - 1).show(mForegroundLayer);
5959 });
5960 }
5961
TearDown()5962 virtual void TearDown() {
5963 LayerTransactionTest::TearDown();
5964 mBackgroundLayer = 0;
5965 mForegroundLayer = 0;
5966 }
5967
5968 sp<SurfaceControl> mBackgroundLayer;
5969 sp<SurfaceControl> mForegroundLayer;
5970 };
5971
5972 // When a layer is reparented offscreen, remove relative z order if the relative parent
5973 // is still onscreen so that the layer is not drawn.
TEST_F(RelativeZTest,LayerRemoved)5974 TEST_F(RelativeZTest, LayerRemoved) {
5975 std::unique_ptr<ScreenCapture> sc;
5976
5977 // Background layer (RED)
5978 // Child layer (WHITE) (relative to foregroud layer)
5979 // Foregroud layer (GREEN)
5980 sp<SurfaceControl> childLayer =
5981 createColorLayer("Child layer", Color::BLUE, mBackgroundLayer.get());
5982
5983 Transaction{}
5984 .setRelativeLayer(childLayer, mForegroundLayer->getHandle(), 1)
5985 .show(childLayer)
5986 .apply();
5987
5988 {
5989 // The childLayer should be in front of the FG control.
5990 ScreenCapture::captureScreen(&sc);
5991 sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b);
5992 }
5993
5994 // Background layer (RED)
5995 // Foregroud layer (GREEN)
5996 Transaction{}.reparent(childLayer, nullptr).apply();
5997
5998 // Background layer (RED)
5999 // Child layer (WHITE)
6000 // Foregroud layer (GREEN)
6001 Transaction{}.reparent(childLayer, mBackgroundLayer->getHandle()).apply();
6002
6003 {
6004 // The relative z info for child layer should be reset, leaving FG control on top.
6005 ScreenCapture::captureScreen(&sc);
6006 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
6007 }
6008 }
6009
6010 // When a layer is reparented offscreen, preseve relative z order if the relative parent
6011 // is also offscreen. Regression test b/132613412
TEST_F(RelativeZTest,LayerRemovedOffscreenRelativeParent)6012 TEST_F(RelativeZTest, LayerRemovedOffscreenRelativeParent) {
6013 std::unique_ptr<ScreenCapture> sc;
6014
6015 // Background layer (RED)
6016 // Foregroud layer (GREEN)
6017 // child level 1 (WHITE)
6018 // child level 2a (BLUE)
6019 // child level 3 (GREEN) (relative to child level 2b)
6020 // child level 2b (BLACK)
6021 sp<SurfaceControl> childLevel1 =
6022 createColorLayer("child level 1", Color::WHITE, mForegroundLayer.get());
6023 sp<SurfaceControl> childLevel2a =
6024 createColorLayer("child level 2a", Color::BLUE, childLevel1.get());
6025 sp<SurfaceControl> childLevel2b =
6026 createColorLayer("child level 2b", Color::BLACK, childLevel1.get());
6027 sp<SurfaceControl> childLevel3 =
6028 createColorLayer("child level 3", Color::GREEN, childLevel2a.get());
6029
6030 Transaction{}
6031 .setRelativeLayer(childLevel3, childLevel2b->getHandle(), 1)
6032 .show(childLevel2a)
6033 .show(childLevel2b)
6034 .show(childLevel3)
6035 .apply();
6036
6037 {
6038 // The childLevel3 should be in front of childLevel2b.
6039 ScreenCapture::captureScreen(&sc);
6040 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
6041 }
6042
6043 // Background layer (RED)
6044 // Foregroud layer (GREEN)
6045 Transaction{}.reparent(childLevel1, nullptr).apply();
6046
6047 // Background layer (RED)
6048 // Foregroud layer (GREEN)
6049 // child level 1 (WHITE)
6050 // child level 2 back (BLUE)
6051 // child level 3 (GREEN) (relative to child level 2b)
6052 // child level 2 front (BLACK)
6053 Transaction{}.reparent(childLevel1, mForegroundLayer->getHandle()).apply();
6054
6055 {
6056 // Nothing should change at this point since relative z info was preserved.
6057 ScreenCapture::captureScreen(&sc);
6058 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
6059 }
6060 }
6061
6062 } // namespace android
6063