blob: d765f68a4632b5a6fc1acfeeec49fb77b70d59f3 [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu718daf82017-10-20 11:57:17 -070017#include <algorithm>
Marissa Wallfda30bb2018-10-12 11:34:28 -070018#include <chrono>
19#include <cinttypes>
Chia-I Wu718daf82017-10-20 11:57:17 -070020#include <functional>
21#include <limits>
22#include <ostream>
23
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070024#include <gtest/gtest.h>
25
Michael Lentine5a16a622015-05-21 13:48:24 -070026#include <android/native_window.h>
27
Alec Mouri80863a62019-01-17 15:19:35 -080028#include <binder/ProcessState.h>
29#include <gui/BufferItemConsumer.h>
Steven Thomas44685cb2019-07-23 16:19:31 -070030#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080031#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070032#include <gui/LayerState.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080033#include <gui/Surface.h>
34#include <gui/SurfaceComposerClient.h>
Ady Abrahamdf9df4a2019-03-12 17:32:05 -070035#include <hardware/hwcomposer_defs.h>
Robert Carrfa8855f2019-02-19 10:05:00 -080036#include <private/android_filesystem_config.h>
Ady Abrahamdf9df4a2019-03-12 17:32:05 -070037#include <private/gui/ComposerService.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080038
Mathias Agopianc666cae2012-07-25 18:56:13 -070039#include <ui/DisplayInfo.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070041#include <math.h>
chaviw13fdc492017-06-27 12:40:18 -070042#include <math/vec3.h>
Robert Carrfa8855f2019-02-19 10:05:00 -080043#include <sys/types.h>
44#include <unistd.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070045
Marissa Wall713b63f2018-10-17 15:42:43 -070046#include "BufferGenerator.h"
Valerie Haue9137b72019-08-27 13:22:18 -070047#include "utils/CallbackUtils.h"
48#include "utils/ColorUtils.h"
49#include "utils/ScreenshotUtils.h"
50#include "utils/TransactionUtils.h"
Marissa Wall713b63f2018-10-17 15:42:43 -070051
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070052namespace android {
53
Marissa Wall61c58622018-07-18 10:12:20 -070054using android::hardware::graphics::common::V1_1::BufferUsage;
chaviwa76b2712017-09-20 12:02:26 -070055
Chia-I Wu718daf82017-10-20 11:57:17 -070056class LayerTransactionTest : public ::testing::Test {
57protected:
58 void SetUp() override {
59 mClient = new SurfaceComposerClient;
60 ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
61
62 ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
Ady Abraham2a6ab2a2018-10-26 14:25:30 -070063
64 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Ady Abraham37965d42018-11-01 13:43:32 -070065 ASSERT_NO_FATAL_FAILURE(sf->getColorManagement(&mColorManagementUsed));
Chia-I Wu718daf82017-10-20 11:57:17 -070066 }
67
chaviw0e3479f2018-09-10 16:49:30 -070068 virtual void TearDown() {
69 mBlackBgSurface = 0;
70 mClient->dispose();
71 mClient = 0;
72 }
73
Marissa Wallfda30bb2018-10-12 11:34:28 -070074 virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
75 const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -080076 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
77 auto layer =
78 createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags, parent);
Chia-I Wu718daf82017-10-20 11:57:17 -070079
Vishnu Nair60356342018-11-13 13:00:45 -080080 Transaction t;
81 t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
Vishnu Nair60356342018-11-13 13:00:45 -080082
83 status_t error = t.apply();
Chia-I Wu718daf82017-10-20 11:57:17 -070084 if (error != NO_ERROR) {
85 ADD_FAILURE() << "failed to initialize SurfaceControl";
86 layer.clear();
87 }
88
89 return layer;
90 }
91
Vishnu Nair88a11f22018-11-28 18:30:57 -080092 virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
93 const char* name, uint32_t width, uint32_t height,
94 PixelFormat format, uint32_t flags,
95 SurfaceControl* parent = nullptr) {
96 auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
97 EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
98 return layer;
99 }
100
Marissa Wallfda30bb2018-10-12 11:34:28 -0700101 virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
chaviwf66724d2018-11-28 16:35:21 -0800102 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
103 return createLayer(mClient, name, width, height, flags, parent);
Marissa Wallfda30bb2018-10-12 11:34:28 -0700104 }
105
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700106 sp<SurfaceControl> createColorLayer(const char* name, const Color& color,
107 SurfaceControl* parent = nullptr) {
108 auto colorLayer = createSurface(mClient, name, 0 /* buffer width */, 0 /* buffer height */,
109 PIXEL_FORMAT_RGBA_8888,
110 ISurfaceComposerClient::eFXSurfaceColor, parent);
111 asTransaction([&](Transaction& t) {
112 t.setColor(colorLayer, half3{color.r / 255.0f, color.g / 255.0f, color.b / 255.0f});
113 t.setAlpha(colorLayer, color.a / 255.0f);
114 });
115 return colorLayer;
116 }
117
Marissa Wall61c58622018-07-18 10:12:20 -0700118 ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700119 // wait for previous transactions (such as setSize) to complete
120 Transaction().apply(true);
121
122 ANativeWindow_Buffer buffer = {};
123 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
124
125 return buffer;
126 }
127
Marissa Wall61c58622018-07-18 10:12:20 -0700128 void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700129 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
130
131 // wait for the newly posted buffer to be latched
132 waitForLayerBuffers();
133 }
134
Marissa Wall61c58622018-07-18 10:12:20 -0700135 virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
136 int32_t bufferWidth, int32_t bufferHeight) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700137 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700138 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
Valerie Haue271df92019-09-06 09:23:22 -0700139 TransactionUtils::fillANativeWindowBufferColor(buffer,
140 Rect(0, 0, bufferWidth, bufferHeight),
141 color);
Marissa Wall61c58622018-07-18 10:12:20 -0700142 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700143 }
144
Marissa Wall61c58622018-07-18 10:12:20 -0700145 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
146 int32_t bufferWidth, int32_t bufferHeight) {
147 sp<GraphicBuffer> buffer =
148 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
149 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
150 BufferUsage::COMPOSER_OVERLAY,
151 "test");
Valerie Haue271df92019-09-06 09:23:22 -0700152 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight),
153 color);
Marissa Wall861616d2018-10-22 12:52:23 -0700154 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -0700155 }
156
157 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
158 int32_t bufferWidth, int32_t bufferHeight) {
159 switch (mLayerType) {
160 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
161 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
162 break;
163 case ISurfaceComposerClient::eFXSurfaceBufferState:
164 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
165 break;
166 default:
167 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
168 }
169 }
170
171 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
172 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700173 const Color& topRight, const Color& bottomLeft,
174 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700175 switch (mLayerType) {
176 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
177 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
178 bottomLeft, bottomRight);
179 break;
180 case ISurfaceComposerClient::eFXSurfaceBufferState:
181 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
182 bottomLeft, bottomRight);
183 break;
184 default:
185 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
186 }
187 }
188
189 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
190 int32_t bufferHeight, const Color& topLeft,
191 const Color& topRight, const Color& bottomLeft,
192 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700193 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700194 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
195 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700196
Marissa Wall61c58622018-07-18 10:12:20 -0700197 const int32_t halfW = bufferWidth / 2;
198 const int32_t halfH = bufferHeight / 2;
Valerie Haue271df92019-09-06 09:23:22 -0700199 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
200 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH),
201 topRight);
202 TransactionUtils::fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight),
203 bottomLeft);
204 TransactionUtils::fillANativeWindowBufferColor(buffer,
205 Rect(halfW, halfH, bufferWidth,
206 bufferHeight),
207 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700208
Marissa Wall61c58622018-07-18 10:12:20 -0700209 postBufferQueueLayerBuffer(layer);
210 }
211
212 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
213 int32_t bufferHeight, const Color& topLeft,
214 const Color& topRight, const Color& bottomLeft,
215 const Color& bottomRight) {
216 sp<GraphicBuffer> buffer =
217 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
218 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
219 BufferUsage::COMPOSER_OVERLAY,
220 "test");
221
222 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
223
224 const int32_t halfW = bufferWidth / 2;
225 const int32_t halfH = bufferHeight / 2;
Valerie Haue271df92019-09-06 09:23:22 -0700226 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
227 TransactionUtils::fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH),
228 topRight);
229 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight),
230 bottomLeft);
231 TransactionUtils::fillGraphicBufferColor(buffer,
232 Rect(halfW, halfH, bufferWidth, bufferHeight),
233 bottomRight);
Marissa Wall61c58622018-07-18 10:12:20 -0700234
235 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700236 }
237
chaviw0e3479f2018-09-10 16:49:30 -0700238 std::unique_ptr<ScreenCapture> screenshot() {
239 std::unique_ptr<ScreenCapture> screenshot;
240 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700241 return screenshot;
242 }
243
Vishnu Nairb927e1f2019-02-19 13:36:15 -0800244 void asTransaction(const std::function<void(Transaction&)>& exec) {
245 Transaction t;
246 exec(t);
247 t.apply(true);
248 }
249
Marissa Wall713b63f2018-10-17 15:42:43 -0700250 static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
251 static BufferGenerator bufferGenerator;
252 return bufferGenerator.get(outBuffer, outFence);
253 }
254
Chia-I Wu718daf82017-10-20 11:57:17 -0700255 sp<SurfaceComposerClient> mClient;
256
257 sp<IBinder> mDisplay;
258 uint32_t mDisplayWidth;
259 uint32_t mDisplayHeight;
260 uint32_t mDisplayLayerStack;
Marissa Wall861616d2018-10-22 12:52:23 -0700261 Rect mDisplayRect = Rect::INVALID_RECT;
Chia-I Wu718daf82017-10-20 11:57:17 -0700262
263 // leave room for ~256 layers
264 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
265
chaviw0e3479f2018-09-10 16:49:30 -0700266 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700267 bool mColorManagementUsed;
268
Chia-I Wu718daf82017-10-20 11:57:17 -0700269private:
270 void SetUpDisplay() {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800271 mDisplay = mClient->getInternalDisplayToken();
272 ASSERT_FALSE(mDisplay == nullptr) << "failed to get display";
Chia-I Wu718daf82017-10-20 11:57:17 -0700273
274 // get display width/height
275 DisplayInfo info;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800276 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplay, &info));
Chia-I Wu718daf82017-10-20 11:57:17 -0700277 mDisplayWidth = info.w;
278 mDisplayHeight = info.h;
Marissa Wall861616d2018-10-22 12:52:23 -0700279 mDisplayRect =
280 Rect(static_cast<int32_t>(mDisplayWidth), static_cast<int32_t>(mDisplayHeight));
Chia-I Wu718daf82017-10-20 11:57:17 -0700281
282 // After a new buffer is queued, SurfaceFlinger is notified and will
283 // latch the new buffer on next vsync. Let's heuristically wait for 3
284 // vsyncs.
285 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
286
287 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700288
Vishnu Nair88a11f22018-11-28 18:30:57 -0800289 mBlackBgSurface =
290 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
291 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
chaviw0e3479f2018-09-10 16:49:30 -0700292
Chia-I Wu718daf82017-10-20 11:57:17 -0700293 // set layer stack (b/68888219)
294 Transaction t;
295 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800296 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700297 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
298 t.setColor(mBlackBgSurface, half3{0, 0, 0});
299 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700300 t.apply();
301 }
302
chaviw0e3479f2018-09-10 16:49:30 -0700303 void waitForLayerBuffers() {
304 // Request an empty transaction to get applied synchronously to ensure the buffer is
305 // latched.
306 Transaction().apply(true);
307 usleep(mBufferPostDelay);
308 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700309
310 int32_t mBufferPostDelay;
Alec Mouri80863a62019-01-17 15:19:35 -0800311
312 friend class LayerRenderPathTestHarness;
313};
Alec Mouri80863a62019-01-17 15:19:35 -0800314
315class LayerRenderPathTestHarness {
316public:
317 LayerRenderPathTestHarness(LayerTransactionTest* delegate, RenderPath renderPath)
318 : mDelegate(delegate), mRenderPath(renderPath) {}
319
320 std::unique_ptr<ScreenCapture> getScreenCapture() {
321 switch (mRenderPath) {
322 case RenderPath::SCREENSHOT:
323 return mDelegate->screenshot();
324 case RenderPath::VIRTUAL_DISPLAY:
325
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800326 const auto mainDisplay = SurfaceComposerClient::getInternalDisplayToken();
Alec Mouri80863a62019-01-17 15:19:35 -0800327 DisplayInfo mainDisplayInfo;
328 SurfaceComposerClient::getDisplayInfo(mainDisplay, &mainDisplayInfo);
329
330 sp<IBinder> vDisplay;
331 sp<IGraphicBufferProducer> producer;
332 sp<IGraphicBufferConsumer> consumer;
333 sp<BufferItemConsumer> itemConsumer;
334 BufferQueue::createBufferQueue(&producer, &consumer);
335
336 consumer->setConsumerName(String8("Virtual disp consumer"));
337 consumer->setDefaultBufferSize(mainDisplayInfo.w, mainDisplayInfo.h);
338
339 itemConsumer = new BufferItemConsumer(consumer,
340 // Sample usage bits from screenrecord
341 GRALLOC_USAGE_HW_VIDEO_ENCODER |
342 GRALLOC_USAGE_SW_READ_OFTEN);
343
344 vDisplay = SurfaceComposerClient::createDisplay(String8("VirtualDisplay"),
345 false /*secure*/);
346
347 SurfaceComposerClient::Transaction t;
348 t.setDisplaySurface(vDisplay, producer);
349 t.setDisplayLayerStack(vDisplay, 0);
350 t.setDisplayProjection(vDisplay, mainDisplayInfo.orientation,
351 Rect(mainDisplayInfo.viewportW, mainDisplayInfo.viewportH),
352 Rect(mainDisplayInfo.w, mainDisplayInfo.h));
353 t.apply();
354 SurfaceComposerClient::Transaction().apply(true);
355 BufferItem item;
356 itemConsumer->acquireBuffer(&item, 0, true);
357 auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer);
358 itemConsumer->releaseBuffer(item);
359 SurfaceComposerClient::destroyDisplay(vDisplay);
360 return sc;
361 }
362 }
363
364protected:
365 LayerTransactionTest* mDelegate;
366 RenderPath mRenderPath;
Chia-I Wu718daf82017-10-20 11:57:17 -0700367};
368
Alec Mouri80863a62019-01-17 15:19:35 -0800369class LayerTypeTransactionHarness : public LayerTransactionTest {
Marissa Wall61c58622018-07-18 10:12:20 -0700370public:
Alec Mouri80863a62019-01-17 15:19:35 -0800371 LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {}
Marissa Wall61c58622018-07-18 10:12:20 -0700372
373 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
Alec Mouri80863a62019-01-17 15:19:35 -0800374 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700375 // if the flags already have a layer type specified, return an error
376 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
377 return nullptr;
378 }
chaviwf66724d2018-11-28 16:35:21 -0800379 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent);
Marissa Wall61c58622018-07-18 10:12:20 -0700380 }
381
382 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
383 int32_t bufferHeight) {
384 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
385 bufferWidth, bufferHeight));
386 }
387
388 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
389 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
390 const Color& bottomLeft, const Color& bottomRight) {
391 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
392 bufferWidth, bufferHeight,
393 topLeft, topRight,
394 bottomLeft, bottomRight));
395 }
396
397protected:
398 uint32_t mLayerType;
399};
400
Alec Mouri80863a62019-01-17 15:19:35 -0800401class LayerTypeTransactionTest : public LayerTypeTransactionHarness,
402 public ::testing::WithParamInterface<uint32_t> {
403public:
404 LayerTypeTransactionTest() : LayerTypeTransactionHarness(GetParam()) {}
405};
406
407class LayerTypeAndRenderTypeTransactionTest
408 : public LayerTypeTransactionHarness,
409 public ::testing::WithParamInterface<std::tuple<uint32_t, RenderPath>> {
410public:
411 LayerTypeAndRenderTypeTransactionTest()
412 : LayerTypeTransactionHarness(std::get<0>(GetParam())),
413 mRenderPathHarness(LayerRenderPathTestHarness(this, std::get<1>(GetParam()))) {}
414
415 std::unique_ptr<ScreenCapture> getScreenCapture() {
416 return mRenderPathHarness.getScreenCapture();
417 }
418
419protected:
420 LayerRenderPathTestHarness mRenderPathHarness;
421};
422
Alec Mouri80863a62019-01-17 15:19:35 -0800423::testing::Environment* const binderEnv =
424 ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
425
426class LayerRenderTypeTransactionTest : public LayerTransactionTest,
427 public ::testing::WithParamInterface<RenderPath> {
428public:
429 LayerRenderTypeTransactionTest() : mHarness(LayerRenderPathTestHarness(this, GetParam())) {}
430
431 std::unique_ptr<ScreenCapture> getScreenCapture() { return mHarness.getScreenCapture(); }
432 void setRelativeZBasicHelper(uint32_t layerType);
433 void setRelativeZGroupHelper(uint32_t layerType);
434 void setAlphaBasicHelper(uint32_t layerType);
Valerie Haudd0b7572019-01-29 14:59:27 -0800435 void setBackgroundColorHelper(uint32_t layerType, bool priorColor, bool bufferFill, float alpha,
436 Color finalColor);
Alec Mouri80863a62019-01-17 15:19:35 -0800437
438protected:
439 LayerRenderPathTestHarness mHarness;
440};
441
442INSTANTIATE_TEST_CASE_P(
443 LayerTypeAndRenderTypeTransactionTests, LayerTypeAndRenderTypeTransactionTest,
444 ::testing::Combine(
445 ::testing::Values(
446 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
447 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)),
448 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT)));
449
450INSTANTIATE_TEST_CASE_P(LayerRenderTypeTransactionTests, LayerRenderTypeTransactionTest,
451 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT));
452
Marissa Wall61c58622018-07-18 10:12:20 -0700453INSTANTIATE_TEST_CASE_P(
454 LayerTypeTransactionTests, LayerTypeTransactionTest,
455 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
456 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
457
Alec Mouri80863a62019-01-17 15:19:35 -0800458TEST_P(LayerRenderTypeTransactionTest, SetPositionBasic_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700459 sp<SurfaceControl> layer;
460 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700461 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700462
463 {
464 SCOPED_TRACE("default position");
Marissa Wall861616d2018-10-22 12:52:23 -0700465 const Rect rect(0, 0, 32, 32);
Alec Mouri80863a62019-01-17 15:19:35 -0800466 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700467 shot->expectColor(rect, Color::RED);
468 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700469 }
470
471 Transaction().setPosition(layer, 5, 10).apply();
472 {
473 SCOPED_TRACE("new position");
Marissa Wall861616d2018-10-22 12:52:23 -0700474 const Rect rect(5, 10, 37, 42);
Alec Mouri80863a62019-01-17 15:19:35 -0800475 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700476 shot->expectColor(rect, Color::RED);
477 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700478 }
479}
480
Alec Mouri80863a62019-01-17 15:19:35 -0800481TEST_P(LayerRenderTypeTransactionTest, SetPositionRounding_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700482 sp<SurfaceControl> layer;
483 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700484 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700485
486 // GLES requires only 4 bits of subpixel precision during rasterization
487 // XXX GLES composition does not match HWC composition due to precision
488 // loss (b/69315223)
489 const float epsilon = 1.0f / 16.0f;
490 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
491 {
492 SCOPED_TRACE("rounding down");
Alec Mouri80863a62019-01-17 15:19:35 -0800493 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700494 }
495
496 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
497 {
498 SCOPED_TRACE("rounding up");
Alec Mouri80863a62019-01-17 15:19:35 -0800499 getScreenCapture()->expectColor(Rect(1, 1, 33, 33), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700500 }
501}
502
Alec Mouri80863a62019-01-17 15:19:35 -0800503TEST_P(LayerRenderTypeTransactionTest, SetPositionOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700504 sp<SurfaceControl> layer;
505 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700506 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700507
508 Transaction().setPosition(layer, -32, -32).apply();
509 {
510 SCOPED_TRACE("negative coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800511 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700512 }
513
514 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
515 {
516 SCOPED_TRACE("positive coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800517 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700518 }
519}
520
Alec Mouri80863a62019-01-17 15:19:35 -0800521TEST_P(LayerRenderTypeTransactionTest, SetPositionPartiallyOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700522 sp<SurfaceControl> layer;
523 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700524 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700525
526 // partially out of bounds
527 Transaction().setPosition(layer, -30, -30).apply();
528 {
529 SCOPED_TRACE("negative coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800530 getScreenCapture()->expectColor(Rect(0, 0, 2, 2), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700531 }
532
533 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
534 {
535 SCOPED_TRACE("positive coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800536 getScreenCapture()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
537 mDisplayHeight),
538 Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700539 }
540}
541
Alec Mouri80863a62019-01-17 15:19:35 -0800542TEST_P(LayerRenderTypeTransactionTest, SetPositionWithResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700543 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700544 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
545 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700546
547 // setPosition is applied immediately by default, with or without resize
548 // pending
549 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
550 {
551 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800552 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700553 const Rect rect(5, 10, 37, 42);
Marissa Wall61c58622018-07-18 10:12:20 -0700554 shot->expectColor(rect, Color::RED);
555 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700556 }
557
Marissa Wall861616d2018-10-22 12:52:23 -0700558 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700559 {
560 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800561 getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700562 }
563}
564
Alec Mouri80863a62019-01-17 15:19:35 -0800565TEST_P(LayerRenderTypeTransactionTest, SetSizeBasic_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700566 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700567 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
568 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700569
570 Transaction().setSize(layer, 64, 64).apply();
571 {
572 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800573 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700574 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -0700575 shot->expectColor(rect, Color::RED);
576 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700577 }
578
Marissa Wall861616d2018-10-22 12:52:23 -0700579 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700580 {
581 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800582 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700583 const Rect rect(0, 0, 64, 64);
584 shot->expectColor(rect, Color::RED);
585 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700586 }
587}
588
Alec Mouri80863a62019-01-17 15:19:35 -0800589TEST_P(LayerTypeAndRenderTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700590 // cannot test robustness against invalid sizes (zero or really huge)
591}
592
Alec Mouri80863a62019-01-17 15:19:35 -0800593TEST_P(LayerRenderTypeTransactionTest, SetSizeWithScaleToWindow_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700594 sp<SurfaceControl> layer;
595 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700596 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700597
598 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
599 Transaction()
600 .setSize(layer, 64, 64)
601 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
602 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -0800603 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700604}
605
Alec Mouri80863a62019-01-17 15:19:35 -0800606TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700607 sp<SurfaceControl> layerR;
608 sp<SurfaceControl> layerG;
609 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700610 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700611 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700612 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700613
614 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
615 {
616 SCOPED_TRACE("layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800617 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700618 }
619
620 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
621 {
622 SCOPED_TRACE("layerG");
Alec Mouri80863a62019-01-17 15:19:35 -0800623 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700624 }
625}
626
Alec Mouri80863a62019-01-17 15:19:35 -0800627TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700628 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800629 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700630 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800631 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700632 sp<SurfaceControl> layerR;
633 sp<SurfaceControl> layerG;
634 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700635 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700636 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700637 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700638
chaviw0e3479f2018-09-10 16:49:30 -0700639 Transaction()
640 .reparent(layerR, parent->getHandle())
641 .reparent(layerG, parent->getHandle())
642 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700643 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
644 {
645 SCOPED_TRACE("layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800646 auto shot = getScreenCapture();
chaviw0e3479f2018-09-10 16:49:30 -0700647 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700648 }
649
650 Transaction().setLayer(layerR, -3).apply();
651 {
652 SCOPED_TRACE("layerG");
Alec Mouri80863a62019-01-17 15:19:35 -0800653 auto shot = getScreenCapture();
chaviw0e3479f2018-09-10 16:49:30 -0700654 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700655 }
656}
657
Alec Mouri80863a62019-01-17 15:19:35 -0800658void LayerRenderTypeTransactionTest::setRelativeZBasicHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700659 sp<SurfaceControl> layerR;
660 sp<SurfaceControl> layerG;
Marissa Wall861616d2018-10-22 12:52:23 -0700661 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32, layerType));
662 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
663 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32, layerType));
664 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700665
Marissa Wall861616d2018-10-22 12:52:23 -0700666 switch (layerType) {
667 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
668 Transaction()
669 .setPosition(layerG, 16, 16)
670 .setRelativeLayer(layerG, layerR->getHandle(), 1)
671 .apply();
672 break;
673 case ISurfaceComposerClient::eFXSurfaceBufferState:
674 Transaction()
675 .setFrame(layerR, Rect(0, 0, 32, 32))
676 .setFrame(layerG, Rect(16, 16, 48, 48))
677 .setRelativeLayer(layerG, layerR->getHandle(), 1)
678 .apply();
679 break;
680 default:
681 ASSERT_FALSE(true) << "Unsupported layer type";
682 }
Chia-I Wu49313302017-10-31 10:14:40 -0700683 {
684 SCOPED_TRACE("layerG above");
Alec Mouri80863a62019-01-17 15:19:35 -0800685 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700686 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
687 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
688 }
689
690 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
691 {
692 SCOPED_TRACE("layerG below");
Alec Mouri80863a62019-01-17 15:19:35 -0800693 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700694 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
695 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
696 }
697}
698
Alec Mouri80863a62019-01-17 15:19:35 -0800699TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -0700700 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
701}
702
Alec Mouri80863a62019-01-17 15:19:35 -0800703TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -0700704 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
705}
706
Marissa Wall61c58622018-07-18 10:12:20 -0700707TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700708 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800709 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700710 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800711 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wuec2d9852017-11-21 09:21:01 -0800712 sp<SurfaceControl> layerR;
713 sp<SurfaceControl> layerG;
714 sp<SurfaceControl> layerB;
715 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700716 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800717 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700718 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800719 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700720 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800721
chaviw0e3479f2018-09-10 16:49:30 -0700722 Transaction()
723 .reparent(layerB, parent->getHandle())
724 .apply();
725
Chia-I Wuec2d9852017-11-21 09:21:01 -0800726 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
727 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
728
chaviw0e3479f2018-09-10 16:49:30 -0700729 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800730 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700731 sp<IBinder> parentHandle = parent->getHandle();
Marissa Wall861616d2018-10-22 12:52:23 -0700732 ScreenCapture::captureLayers(&screenshot, parentHandle, Rect(0, 0, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800733 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
734}
735
chaviw32377582019-05-13 11:15:19 -0700736TEST_P(LayerTypeTransactionTest, SetLayerAndRelative) {
737 sp<SurfaceControl> parent =
738 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
739 ISurfaceComposerClient::eFXSurfaceColor);
740
741 sp<SurfaceControl> childLayer;
742 ASSERT_NO_FATAL_FAILURE(
743 childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
744 0 /* buffer height */,
745 ISurfaceComposerClient::eFXSurfaceColor,
746 parent.get()));
747 Transaction()
748 .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
749 .setColor(parent, half3{0.0f, 0.0f, 0.0f})
750 .show(childLayer)
751 .show(parent)
752 .setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight))
753 .setCrop_legacy(childLayer, Rect(0, 0, 20, 30))
754 .apply();
755
756 Transaction()
757 .setRelativeLayer(childLayer, parent->getHandle(), -1)
758 .setLayer(childLayer, 1)
759 .apply();
760
761 {
762 SCOPED_TRACE("setLayer above");
763 // Set layer should get applied and place the child above.
764 std::unique_ptr<ScreenCapture> screenshot;
765 ScreenCapture::captureScreen(&screenshot);
766 screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
767 }
768
769 Transaction()
770 .setLayer(childLayer, 1)
771 .setRelativeLayer(childLayer, parent->getHandle(), -1)
772 .apply();
773
774 {
775 SCOPED_TRACE("setRelative below");
776 // Set relative layer should get applied and place the child below.
777 std::unique_ptr<ScreenCapture> screenshot;
778 ScreenCapture::captureScreen(&screenshot);
779 screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
780 }
781}
782
Robert Carr1c5481e2019-07-01 14:42:27 -0700783TEST_P(LayerTypeTransactionTest, HideRelativeParentHidesLayer) {
784 sp<SurfaceControl> parent =
785 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
786 ISurfaceComposerClient::eFXSurfaceColor);
787 sp<SurfaceControl> relativeParent =
788 LayerTransactionTest::createLayer("RelativeParent", 0 /* buffer width */,
789 0 /* buffer height */, ISurfaceComposerClient::eFXSurfaceColor);
790
791 sp<SurfaceControl> childLayer;
792 ASSERT_NO_FATAL_FAILURE(
793 childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
794 0 /* buffer height */,
795 ISurfaceComposerClient::eFXSurfaceColor,
796 parent.get()));
797 Transaction()
798 .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
799 .setColor(parent, half3{0.0f, 0.0f, 0.0f})
800 .setColor(relativeParent, half3{0.0f, 1.0f, 0.0f})
801 .show(childLayer)
802 .show(parent)
803 .show(relativeParent)
804 .setLayer(parent, mLayerZBase - 1)
805 .setLayer(relativeParent, mLayerZBase)
806 .apply();
807
808 Transaction()
809 .setRelativeLayer(childLayer, relativeParent->getHandle(), 1)
810 .apply();
811
812 {
813 SCOPED_TRACE("setLayer above");
814 // Set layer should get applied and place the child above.
815 std::unique_ptr<ScreenCapture> screenshot;
816 ScreenCapture::captureScreen(&screenshot);
817 screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
818 }
819
820 Transaction()
821 .hide(relativeParent)
822 .apply();
823
824 {
825 SCOPED_TRACE("hide relative parent");
826 // The relative should no longer be visible.
827 std::unique_ptr<ScreenCapture> screenshot;
828 ScreenCapture::captureScreen(&screenshot);
829 screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
830 }
831}
832
Alec Mouri80863a62019-01-17 15:19:35 -0800833void LayerRenderTypeTransactionTest::setRelativeZGroupHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700834 sp<SurfaceControl> layerR;
835 sp<SurfaceControl> layerG;
836 sp<SurfaceControl> layerB;
Marissa Wall861616d2018-10-22 12:52:23 -0700837 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test", 32, 32, layerType));
838 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
839 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test", 32, 32, layerType));
840 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
841 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test", 32, 32, layerType));
842 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700843
844 // layerR = 0, layerG = layerR + 3, layerB = 2
Marissa Wall861616d2018-10-22 12:52:23 -0700845 switch (layerType) {
846 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
847 Transaction()
848 .setPosition(layerG, 8, 8)
849 .setRelativeLayer(layerG, layerR->getHandle(), 3)
850 .setPosition(layerB, 16, 16)
851 .setLayer(layerB, mLayerZBase + 2)
852 .apply();
853 break;
854 case ISurfaceComposerClient::eFXSurfaceBufferState:
855 Transaction()
856 .setFrame(layerR, Rect(0, 0, 32, 32))
857 .setFrame(layerG, Rect(8, 8, 40, 40))
858 .setRelativeLayer(layerG, layerR->getHandle(), 3)
859 .setFrame(layerB, Rect(16, 16, 48, 48))
860 .setLayer(layerB, mLayerZBase + 2)
861 .apply();
862 break;
863 default:
864 ASSERT_FALSE(true) << "Unsupported layer type";
865 }
866
Chia-I Wu49313302017-10-31 10:14:40 -0700867 {
868 SCOPED_TRACE("(layerR < layerG) < layerB");
Alec Mouri80863a62019-01-17 15:19:35 -0800869 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700870 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
871 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
872 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
873 }
874
875 // layerR = 4, layerG = layerR + 3, layerB = 2
876 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
877 {
878 SCOPED_TRACE("layerB < (layerR < layerG)");
Alec Mouri80863a62019-01-17 15:19:35 -0800879 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700880 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
881 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
882 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
883 }
884
885 // layerR = 4, layerG = layerR - 3, layerB = 2
886 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
887 {
888 SCOPED_TRACE("layerB < (layerG < layerR)");
Alec Mouri80863a62019-01-17 15:19:35 -0800889 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700890 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
891 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
892 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
893 }
894
895 // restore to absolute z
896 // layerR = 4, layerG = 0, layerB = 2
897 Transaction().setLayer(layerG, mLayerZBase).apply();
898 {
899 SCOPED_TRACE("layerG < layerB < layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800900 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700901 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
902 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
903 }
904
905 // layerR should not affect layerG anymore
906 // layerR = 1, layerG = 0, layerB = 2
907 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
908 {
909 SCOPED_TRACE("layerG < layerR < layerB");
Alec Mouri80863a62019-01-17 15:19:35 -0800910 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700911 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
912 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
913 }
914}
915
Alec Mouri80863a62019-01-17 15:19:35 -0800916TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -0700917 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
918}
919
Alec Mouri80863a62019-01-17 15:19:35 -0800920TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -0700921 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
922}
923
Alec Mouri80863a62019-01-17 15:19:35 -0800924TEST_P(LayerTypeAndRenderTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -0700925 sp<SurfaceControl> layerR;
926 sp<SurfaceControl> layerG;
927
928 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700929 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700930 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700931 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700932
933 Transaction()
934 .setPosition(layerG, 16, 16)
935 .setRelativeLayer(layerG, layerR->getHandle(), 1)
936 .apply();
937
Robert Carr87246532019-02-04 15:20:26 -0800938 layerG.clear();
Chia-I Wu49313302017-10-31 10:14:40 -0700939 // layerG should have been removed
Alec Mouri80863a62019-01-17 15:19:35 -0800940 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu49313302017-10-31 10:14:40 -0700941}
942
Alec Mouri80863a62019-01-17 15:19:35 -0800943TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -0700944 sp<SurfaceControl> layer;
945 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700946 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700947
948 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
949 {
950 SCOPED_TRACE("layer hidden");
Alec Mouri80863a62019-01-17 15:19:35 -0800951 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu57b27502017-10-31 10:14:40 -0700952 }
953
954 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
955 {
956 SCOPED_TRACE("layer shown");
Alec Mouri80863a62019-01-17 15:19:35 -0800957 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu57b27502017-10-31 10:14:40 -0700958 }
959}
960
Alec Mouri80863a62019-01-17 15:19:35 -0800961TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -0700962 const Color translucentRed = {100, 0, 0, 100};
963 sp<SurfaceControl> layerR;
964 sp<SurfaceControl> layerG;
965 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700966 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700967 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700968 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700969
970 Transaction()
971 .setLayer(layerR, mLayerZBase + 1)
972 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
973 .apply();
974 {
975 SCOPED_TRACE("layerR opaque");
Alec Mouri80863a62019-01-17 15:19:35 -0800976 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
Chia-I Wu57b27502017-10-31 10:14:40 -0700977 }
978
979 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
980 {
981 SCOPED_TRACE("layerR translucent");
982 const uint8_t g = uint8_t(255 - translucentRed.a);
Alec Mouri80863a62019-01-17 15:19:35 -0800983 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
Chia-I Wu57b27502017-10-31 10:14:40 -0700984 }
985}
986
Marissa Wall61c58622018-07-18 10:12:20 -0700987TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -0700988 sp<SurfaceControl> layer;
989 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700990 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700991
992 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +0000993 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -0700994 Transaction()
995 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
996 .apply(true);
997 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -0700998 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -0700999
1000 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1001 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001002 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001003}
1004
Robert Carrfa8855f2019-02-19 10:05:00 -08001005TEST_F(LayerTransactionTest, SetFlagsSecureEUidSystem) {
1006 sp<SurfaceControl> layer;
1007 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1008 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
1009
1010 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
1011 sp<GraphicBuffer> outBuffer;
1012 Transaction()
1013 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1014 .apply(true);
1015 ASSERT_EQ(PERMISSION_DENIED,
1016 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1017
1018 UIDFaker f(AID_SYSTEM);
1019
1020 // By default the system can capture screenshots with secure layers but they
1021 // will be blacked out
1022 ASSERT_EQ(NO_ERROR,
1023 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1024
1025 {
1026 SCOPED_TRACE("as system");
1027 auto shot = screenshot();
1028 shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1029 }
1030
1031 // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
1032 // to receive them...we are expected to take care with the results.
Robert Carr108b2c72019-04-02 16:32:58 -07001033 bool outCapturedSecureLayers;
Robert Carrfa8855f2019-02-19 10:05:00 -08001034 ASSERT_EQ(NO_ERROR,
Robert Carr108b2c72019-04-02 16:32:58 -07001035 composer->captureScreen(mDisplay, &outBuffer, outCapturedSecureLayers,
1036 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(), 0,
1037 0, false, ISurfaceComposer::eRotateNone, true));
1038 ASSERT_EQ(true, outCapturedSecureLayers);
Robert Carrfa8855f2019-02-19 10:05:00 -08001039 ScreenCapture sc(outBuffer);
1040 sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
1041}
1042
Alec Mouri80863a62019-01-17 15:19:35 -08001043TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001044 const Rect top(0, 0, 32, 16);
1045 const Rect bottom(0, 16, 32, 32);
1046 sp<SurfaceControl> layer;
1047 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1048
1049 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001050 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
Valerie Haue271df92019-09-06 09:23:22 -07001051 ASSERT_NO_FATAL_FAILURE(
1052 TransactionUtils::fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1053 ASSERT_NO_FATAL_FAILURE(
1054 TransactionUtils::fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001055 // setTransparentRegionHint always applies to the following buffer
1056 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001057 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001058 {
1059 SCOPED_TRACE("top transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001060 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001061 shot->expectColor(top, Color::BLACK);
1062 shot->expectColor(bottom, Color::RED);
1063 }
1064
1065 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1066 {
1067 SCOPED_TRACE("transparent region hint pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001068 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001069 shot->expectColor(top, Color::BLACK);
1070 shot->expectColor(bottom, Color::RED);
1071 }
1072
Marissa Wall61c58622018-07-18 10:12:20 -07001073 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
Valerie Haue271df92019-09-06 09:23:22 -07001074 ASSERT_NO_FATAL_FAILURE(
1075 TransactionUtils::fillANativeWindowBufferColor(buffer, top, Color::RED));
1076 ASSERT_NO_FATAL_FAILURE(
1077 TransactionUtils::fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
Marissa Wall61c58622018-07-18 10:12:20 -07001078 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001079 {
1080 SCOPED_TRACE("bottom transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001081 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001082 shot->expectColor(top, Color::RED);
1083 shot->expectColor(bottom, Color::BLACK);
1084 }
1085}
1086
Alec Mouri80863a62019-01-17 15:19:35 -08001087TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001088 const Rect top(0, 0, 32, 16);
1089 const Rect bottom(0, 16, 32, 32);
1090 sp<SurfaceControl> layer;
1091 ASSERT_NO_FATAL_FAILURE(
1092 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1093
1094 sp<GraphicBuffer> buffer =
1095 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1096 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1097 BufferUsage::COMPOSER_OVERLAY,
1098 "test");
1099
Valerie Haue271df92019-09-06 09:23:22 -07001100 ASSERT_NO_FATAL_FAILURE(
1101 TransactionUtils::fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1102 ASSERT_NO_FATAL_FAILURE(TransactionUtils::fillGraphicBufferColor(buffer, bottom, Color::RED));
Marissa Wall61c58622018-07-18 10:12:20 -07001103 Transaction()
1104 .setTransparentRegionHint(layer, Region(top))
1105 .setBuffer(layer, buffer)
Marissa Wall861616d2018-10-22 12:52:23 -07001106 .setFrame(layer, Rect(0, 0, 32, 32))
Marissa Wall61c58622018-07-18 10:12:20 -07001107 .apply();
1108 {
1109 SCOPED_TRACE("top transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001110 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001111 shot->expectColor(top, Color::BLACK);
1112 shot->expectColor(bottom, Color::RED);
1113 }
1114
1115 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1116 {
1117 SCOPED_TRACE("transparent region hint intermediate");
Alec Mouri80863a62019-01-17 15:19:35 -08001118 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001119 shot->expectColor(top, Color::BLACK);
1120 shot->expectColor(bottom, Color::BLACK);
1121 }
1122
1123 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1124 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1125 BufferUsage::COMPOSER_OVERLAY,
1126 "test");
1127
Valerie Haue271df92019-09-06 09:23:22 -07001128 ASSERT_NO_FATAL_FAILURE(TransactionUtils::fillGraphicBufferColor(buffer, top, Color::RED));
1129 ASSERT_NO_FATAL_FAILURE(
1130 TransactionUtils::fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
Marissa Wall861616d2018-10-22 12:52:23 -07001131 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001132 {
1133 SCOPED_TRACE("bottom transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001134 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001135 shot->expectColor(top, Color::RED);
1136 shot->expectColor(bottom, Color::BLACK);
1137 }
1138}
1139
Alec Mouri80863a62019-01-17 15:19:35 -08001140TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001141 sp<SurfaceControl> layerTransparent;
1142 sp<SurfaceControl> layerR;
1143 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1144 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1145
1146 // check that transparent region hint is bound by the layer size
1147 Transaction()
Marissa Wall861616d2018-10-22 12:52:23 -07001148 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001149 .setPosition(layerR, 16, 16)
1150 .setLayer(layerR, mLayerZBase + 1)
1151 .apply();
Marissa Wall861616d2018-10-22 12:52:23 -07001152 ASSERT_NO_FATAL_FAILURE(
1153 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1154 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerR, Color::RED, 32, 32));
Alec Mouri80863a62019-01-17 15:19:35 -08001155 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001156}
1157
Alec Mouri80863a62019-01-17 15:19:35 -08001158TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001159 sp<SurfaceControl> layerTransparent;
1160 sp<SurfaceControl> layerR;
1161 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1162 ASSERT_NO_FATAL_FAILURE(
1163 layerR = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1164
1165 // check that transparent region hint is bound by the layer size
1166 Transaction()
1167 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
1168 .setFrame(layerR, Rect(16, 16, 48, 48))
1169 .setLayer(layerR, mLayerZBase + 1)
1170 .apply();
1171 ASSERT_NO_FATAL_FAILURE(
1172 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1173 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layerR, Color::RED, 32, 32));
Alec Mouri80863a62019-01-17 15:19:35 -08001174 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
Marissa Wall861616d2018-10-22 12:52:23 -07001175}
1176
Alec Mouri80863a62019-01-17 15:19:35 -08001177void LayerRenderTypeTransactionTest::setAlphaBasicHelper(uint32_t layerType) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001178 sp<SurfaceControl> layer1;
1179 sp<SurfaceControl> layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07001180 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32, layerType));
1181 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32, layerType));
1182 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer1, {64, 0, 0, 255}, 32, 32));
1183 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001184
Marissa Wall861616d2018-10-22 12:52:23 -07001185 switch (layerType) {
1186 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1187 Transaction()
1188 .setAlpha(layer1, 0.25f)
1189 .setAlpha(layer2, 0.75f)
1190 .setPosition(layer2, 16, 0)
1191 .setLayer(layer2, mLayerZBase + 1)
1192 .apply();
1193 break;
1194 case ISurfaceComposerClient::eFXSurfaceBufferState:
1195 Transaction()
1196 .setAlpha(layer1, 0.25f)
1197 .setAlpha(layer2, 0.75f)
1198 .setFrame(layer1, Rect(0, 0, 32, 32))
1199 .setFrame(layer2, Rect(16, 0, 48, 32))
1200 .setLayer(layer2, mLayerZBase + 1)
1201 .apply();
1202 break;
1203 default:
1204 ASSERT_FALSE(true) << "Unsupported layer type";
1205 }
Chia-I Wua8a515e2017-11-01 15:16:35 -07001206 {
Alec Mouri80863a62019-01-17 15:19:35 -08001207 auto shot = getScreenCapture();
Chia-I Wua8a515e2017-11-01 15:16:35 -07001208 uint8_t r = 16; // 64 * 0.25f
1209 uint8_t g = 48; // 64 * 0.75f
1210 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1211 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1212
1213 r /= 4; // r * (1.0f - 0.75f)
1214 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1215 }
1216}
1217
Alec Mouri80863a62019-01-17 15:19:35 -08001218TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -07001219 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1220}
1221
Alec Mouri80863a62019-01-17 15:19:35 -08001222TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001223 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1224}
1225
Alec Mouri80863a62019-01-17 15:19:35 -08001226TEST_P(LayerTypeAndRenderTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001227 const Color color = {64, 0, 0, 255};
1228 sp<SurfaceControl> layer;
1229 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001230 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001231
1232 Transaction().setAlpha(layer, 2.0f).apply();
1233 {
1234 SCOPED_TRACE("clamped to 1.0f");
Alec Mouri80863a62019-01-17 15:19:35 -08001235 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), color);
Chia-I Wua8a515e2017-11-01 15:16:35 -07001236 }
1237
1238 Transaction().setAlpha(layer, -1.0f).apply();
1239 {
1240 SCOPED_TRACE("clamped to 0.0f");
Alec Mouri80863a62019-01-17 15:19:35 -08001241 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wua8a515e2017-11-01 15:16:35 -07001242 }
1243}
1244
Alec Mouri80863a62019-01-17 15:19:35 -08001245TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadius) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001246 sp<SurfaceControl> layer;
1247 const uint8_t size = 64;
1248 const uint8_t testArea = 4;
Lucas Dupina1d0e312018-12-04 22:30:27 -08001249 const float cornerRadius = 20.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001250 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1251 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1252
1253 Transaction()
1254 .setCornerRadius(layer, cornerRadius)
Lucas Dupin0b79f8c2019-08-26 17:01:33 -07001255 .setCrop_legacy(layer, Rect(0, 0, size, size))
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001256 .apply();
1257 {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001258 const uint8_t bottom = size - 1;
1259 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001260 auto shot = getScreenCapture();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001261 // Transparent corners
1262 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
Lucas Dupina1d0e312018-12-04 22:30:27 -08001263 shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
1264 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1265 shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
1266 }
1267}
1268
Alec Mouri80863a62019-01-17 15:19:35 -08001269TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildCrop) {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001270 sp<SurfaceControl> parent;
1271 sp<SurfaceControl> child;
1272 const uint8_t size = 64;
1273 const uint8_t testArea = 4;
1274 const float cornerRadius = 20.0f;
1275 ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
1276 ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
1277 ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
1278 ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
1279
1280 Transaction()
1281 .setCornerRadius(parent, cornerRadius)
Lucas Dupin0b79f8c2019-08-26 17:01:33 -07001282 .setCrop_legacy(parent, Rect(0, 0, size, size))
Lucas Dupina1d0e312018-12-04 22:30:27 -08001283 .reparent(child, parent->getHandle())
1284 .setPosition(child, 0, size / 2)
1285 .apply();
1286 {
1287 const uint8_t bottom = size - 1;
1288 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001289 auto shot = getScreenCapture();
Lucas Dupina1d0e312018-12-04 22:30:27 -08001290 // Top edge of child should not have rounded corners because it's translated in the parent
1291 shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
1292 Color::GREEN);
1293 // But bottom edges should have been clipped according to parent bounds
1294 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1295 shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001296 }
1297}
1298
Alec Mouri80863a62019-01-17 15:19:35 -08001299TEST_P(LayerRenderTypeTransactionTest, SetColorBasic) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001300 sp<SurfaceControl> bufferLayer;
1301 sp<SurfaceControl> colorLayer;
1302 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001303 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001304 ASSERT_NO_FATAL_FAILURE(colorLayer =
1305 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1306 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001307
Vishnu Nair88a11f22018-11-28 18:30:57 -08001308 Transaction()
1309 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1310 .setLayer(colorLayer, mLayerZBase + 1)
1311 .apply();
1312
Chia-I Wue4ef6102017-11-01 15:16:35 -07001313 {
1314 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08001315 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001316 }
1317
1318 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1319 const Color expected = {15, 51, 85, 255};
1320 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1321 // channel) should be less than one
1322 const uint8_t tolerance = 1;
1323 Transaction().setColor(colorLayer, color).apply();
1324 {
1325 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08001326 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001327 }
1328}
1329
Valerie Haudd0b7572019-01-29 14:59:27 -08001330// RED: Color layer base color and BufferQueueLayer/BufferStateLayer fill
1331// BLUE: prior background color
1332// GREEN: final background color
1333// BLACK: no color or fill
1334void LayerRenderTypeTransactionTest::setBackgroundColorHelper(uint32_t layerType, bool priorColor,
1335 bool bufferFill, float alpha,
1336 Color finalColor) {
1337 sp<SurfaceControl> layer;
1338 int32_t width = 500;
1339 int32_t height = 500;
Valerie Haua72e2812019-01-23 13:40:39 -08001340
Valerie Haudd0b7572019-01-29 14:59:27 -08001341 Color fillColor = Color::RED;
1342 Color priorBgColor = Color::BLUE;
1343 Color expectedColor = Color::BLACK;
1344 switch (layerType) {
1345 case ISurfaceComposerClient::eFXSurfaceColor:
1346 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 0, 0, layerType));
1347 Transaction()
1348 .setCrop_legacy(layer, Rect(0, 0, width, height))
1349 .setColor(layer, half3(1.0f, 0, 0))
1350 .apply();
1351 expectedColor = fillColor;
1352 break;
1353 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1354 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
1355 if (bufferFill) {
1356 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, fillColor, width, height));
1357 expectedColor = fillColor;
1358 }
1359 Transaction().setCrop_legacy(layer, Rect(0, 0, width, height)).apply();
1360 break;
1361 case ISurfaceComposerClient::eFXSurfaceBufferState:
1362 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height, layerType));
1363 if (bufferFill) {
1364 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, fillColor, width, height));
1365 expectedColor = fillColor;
1366 }
1367 Transaction().setFrame(layer, Rect(0, 0, width, height)).apply();
1368 break;
1369 default:
1370 GTEST_FAIL() << "Unknown layer type in setBackgroundColorHelper";
1371 return;
Valerie Haua72e2812019-01-23 13:40:39 -08001372 }
1373
Valerie Haudd0b7572019-01-29 14:59:27 -08001374 if (priorColor && layerType != ISurfaceComposerClient::eFXSurfaceColor) {
1375 Transaction()
1376 .setBackgroundColor(layer, half3(0, 0, 1.0f), 1.0f, ui::Dataspace::UNKNOWN)
1377 .apply();
1378 if (!bufferFill) {
1379 expectedColor = priorBgColor;
1380 }
1381 }
1382
1383 {
1384 SCOPED_TRACE("default before setting background color layer");
1385 screenshot()->expectColor(Rect(0, 0, width, height), expectedColor);
1386 }
Valerie Haua72e2812019-01-23 13:40:39 -08001387 Transaction()
Valerie Haudd0b7572019-01-29 14:59:27 -08001388 .setBackgroundColor(layer, half3(0, 1.0f, 0), alpha, ui::Dataspace::UNKNOWN)
Valerie Haua72e2812019-01-23 13:40:39 -08001389 .apply();
1390
1391 {
Valerie Haua72e2812019-01-23 13:40:39 -08001392 auto shot = screenshot();
Valerie Haudd0b7572019-01-29 14:59:27 -08001393 shot->expectColor(Rect(0, 0, width, height), finalColor);
1394 shot->expectBorder(Rect(0, 0, width, height), Color::BLACK);
Valerie Haua72e2812019-01-23 13:40:39 -08001395 }
1396}
1397
Valerie Haudd0b7572019-01-29 14:59:27 -08001398TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_Color_NoEffect) {
1399 bool priorColor = false;
1400 bool bufferFill = false;
1401 float alpha = 1.0f;
1402 Color finalColor = Color::RED;
1403 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceColor,
1404 priorColor, bufferFill, alpha, finalColor));
1405}
Valerie Haua72e2812019-01-23 13:40:39 -08001406
Valerie Haudd0b7572019-01-29 14:59:27 -08001407TEST_P(LayerRenderTypeTransactionTest,
1408 SetBackgroundColor_BufferQueue_BufferFill_NoPriorColor_Basic) {
1409 bool priorColor = false;
1410 bool bufferFill = true;
1411 float alpha = 1.0f;
1412 Color finalColor = Color::RED;
1413 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1414 priorColor, bufferFill, alpha, finalColor));
1415}
Valerie Haua72e2812019-01-23 13:40:39 -08001416
Valerie Haudd0b7572019-01-29 14:59:27 -08001417TEST_P(LayerRenderTypeTransactionTest,
1418 SetBackgroundColor_BufferQueue_NoBufferFill_NoPriorColor_Basic) {
1419 bool priorColor = false;
1420 bool bufferFill = false;
1421 float alpha = 1.0f;
1422 Color finalColor = Color::GREEN;
1423 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1424 priorColor, bufferFill, alpha, finalColor));
1425}
Valerie Haua72e2812019-01-23 13:40:39 -08001426
Valerie Haudd0b7572019-01-29 14:59:27 -08001427TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_BufferQueue_BufferFill_PriorColor_Basic) {
1428 bool priorColor = true;
1429 bool bufferFill = true;
1430 float alpha = 1.0f;
1431 Color finalColor = Color::RED;
1432 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1433 priorColor, bufferFill, alpha, finalColor));
1434}
1435
1436TEST_P(LayerRenderTypeTransactionTest,
1437 SetBackgroundColor_BufferQueue_NoBufferFill_PriorColor_Basic) {
1438 bool priorColor = true;
1439 bool bufferFill = false;
1440 float alpha = 1.0f;
1441 Color finalColor = Color::GREEN;
1442 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1443 priorColor, bufferFill, alpha, finalColor));
1444}
1445TEST_P(LayerRenderTypeTransactionTest,
1446 SetBackgroundColor_BufferQueue_NoPriorColor_ZeroAlpha_NoEffect) {
1447 bool priorColor = false;
1448 bool bufferFill = false;
1449 float alpha = 0;
1450 Color finalColor = Color::BLACK;
1451 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1452 priorColor, bufferFill, alpha, finalColor));
1453}
1454
1455TEST_P(LayerRenderTypeTransactionTest,
1456 SetBackgroundColor_BufferQueue_PriorColor_ZeroAlpha_DeleteBackground) {
1457 bool priorColor = true;
1458 bool bufferFill = false;
1459 float alpha = 0;
1460 Color finalColor = Color::BLACK;
1461 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1462 priorColor, bufferFill, alpha, finalColor));
1463}
1464
1465TEST_P(LayerRenderTypeTransactionTest,
1466 SetBackgroundColor_BufferState_BufferFill_NoPriorColor_Basic) {
1467 bool priorColor = false;
1468 bool bufferFill = true;
1469 float alpha = 1.0f;
1470 Color finalColor = Color::RED;
Valerie Haua6b15a12019-02-05 14:16:30 -08001471 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001472 priorColor, bufferFill, alpha, finalColor));
1473}
1474
1475TEST_P(LayerRenderTypeTransactionTest,
1476 SetBackgroundColor_BufferState_NoBufferFill_NoPriorColor_Basic) {
1477 bool priorColor = false;
1478 bool bufferFill = false;
1479 float alpha = 1.0f;
1480 Color finalColor = Color::GREEN;
Valerie Haua6b15a12019-02-05 14:16:30 -08001481 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001482 priorColor, bufferFill, alpha, finalColor));
1483}
1484
1485TEST_P(LayerRenderTypeTransactionTest,
1486 SetBackgroundColor_BufferState_NoBufferFill_PriorColor_Basic) {
1487 bool priorColor = true;
1488 bool bufferFill = false;
1489 float alpha = 1.0f;
1490 Color finalColor = Color::GREEN;
Valerie Haua6b15a12019-02-05 14:16:30 -08001491 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001492 priorColor, bufferFill, alpha, finalColor));
1493}
1494
1495TEST_P(LayerRenderTypeTransactionTest,
1496 SetBackgroundColor_BufferState_NoPriorColor_ZeroAlpha_NoEffect) {
1497 bool priorColor = false;
1498 bool bufferFill = false;
1499 float alpha = 0;
1500 Color finalColor = Color::BLACK;
Valerie Haua6b15a12019-02-05 14:16:30 -08001501 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001502 priorColor, bufferFill, alpha, finalColor));
1503}
1504
1505TEST_P(LayerRenderTypeTransactionTest,
1506 SetBackgroundColor_BufferState_PriorColor_ZeroAlpha_DeleteBackground) {
1507 bool priorColor = true;
1508 bool bufferFill = false;
1509 float alpha = 0;
1510 Color finalColor = Color::BLACK;
Valerie Haua6b15a12019-02-05 14:16:30 -08001511 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001512 priorColor, bufferFill, alpha, finalColor));
Valerie Haua72e2812019-01-23 13:40:39 -08001513}
1514
Alec Mouri80863a62019-01-17 15:19:35 -08001515TEST_P(LayerRenderTypeTransactionTest, SetColorClamped) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001516 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001517 ASSERT_NO_FATAL_FAILURE(colorLayer =
1518 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1519 ISurfaceComposerClient::eFXSurfaceColor));
1520 Transaction()
1521 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1522 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1523 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001524
Alec Mouri80863a62019-01-17 15:19:35 -08001525 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001526}
1527
Alec Mouri80863a62019-01-17 15:19:35 -08001528TEST_P(LayerRenderTypeTransactionTest, SetColorWithAlpha) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001529 sp<SurfaceControl> bufferLayer;
1530 sp<SurfaceControl> colorLayer;
1531 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001532 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001533 ASSERT_NO_FATAL_FAILURE(colorLayer =
1534 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1535 ISurfaceComposerClient::eFXSurfaceColor));
1536 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001537
1538 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1539 const float alpha = 0.25f;
1540 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1541 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1542 // channel) should be less than one
1543 const uint8_t tolerance = 1;
1544 Transaction()
1545 .setColor(colorLayer, color)
1546 .setAlpha(colorLayer, alpha)
1547 .setLayer(colorLayer, mLayerZBase + 1)
1548 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001549 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1550 tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001551}
1552
Alec Mouri80863a62019-01-17 15:19:35 -08001553TEST_P(LayerRenderTypeTransactionTest, SetColorWithParentAlpha_Bug74220420) {
Adrian Roosb7a96502018-04-08 11:38:55 -07001554 sp<SurfaceControl> bufferLayer;
1555 sp<SurfaceControl> parentLayer;
1556 sp<SurfaceControl> colorLayer;
1557 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1558 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001559 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001560 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1561 0 /* buffer height */,
1562 ISurfaceComposerClient::eFXSurfaceColor));
1563 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001564 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1565 const float alpha = 0.25f;
1566 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1567 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1568 // channel) should be less than one
1569 const uint8_t tolerance = 1;
1570 Transaction()
1571 .reparent(colorLayer, parentLayer->getHandle())
1572 .setColor(colorLayer, color)
1573 .setAlpha(parentLayer, alpha)
1574 .setLayer(parentLayer, mLayerZBase + 1)
1575 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001576 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1577 tolerance);
Adrian Roosb7a96502018-04-08 11:38:55 -07001578}
1579
Alec Mouri80863a62019-01-17 15:19:35 -08001580TEST_P(LayerTypeAndRenderTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001581 sp<SurfaceControl> bufferLayer;
1582 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001583 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001584
1585 // color is ignored
1586 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001587 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001588}
1589
Alec Mouri80863a62019-01-17 15:19:35 -08001590TEST_P(LayerTypeAndRenderTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001591 sp<SurfaceControl> layer;
1592 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001593 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001594
1595 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1596 {
1597 SCOPED_TRACE("non-existing layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001598 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001599 }
1600
1601 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1602 {
1603 SCOPED_TRACE("original layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001604 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001605 }
1606}
1607
Alec Mouri80863a62019-01-17 15:19:35 -08001608TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001609 sp<SurfaceControl> layer;
1610 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001611 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1612 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001613
1614 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1615 {
1616 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001617 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1618 Color::BLUE, Color::WHITE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001619 }
1620
1621 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1622 {
1623 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001624 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED,
1625 Color::WHITE, Color::BLUE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001626 }
1627
1628 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1629 {
1630 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001631 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE,
1632 Color::RED, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001633 }
1634
1635 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1636 {
1637 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001638 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED,
1639 Color::WHITE, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001640 }
1641
1642 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1643 {
1644 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001645 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN,
1646 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001647 }
1648}
1649
Alec Mouri80863a62019-01-17 15:19:35 -08001650TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001651 sp<SurfaceControl> layer;
1652 ASSERT_NO_FATAL_FAILURE(
1653 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1654 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1655 Color::BLUE, Color::WHITE));
1656
1657 Transaction()
1658 .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
1659 .setFrame(layer, Rect(0, 0, 32, 32))
1660 .apply();
1661 {
1662 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001663 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1664 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001665 }
1666
1667 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
1668 {
1669 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001670 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1671 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001672 }
1673
1674 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
1675 {
1676 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001677 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1678 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001679 }
1680
1681 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
1682 {
1683 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001684 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1685 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001686 }
1687
1688 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
1689 {
1690 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001691 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1692 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001693 }
1694}
1695
Alec Mouri80863a62019-01-17 15:19:35 -08001696TEST_P(LayerRenderTypeTransactionTest, SetMatrixRot45_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001697 sp<SurfaceControl> layer;
1698 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001699 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1700 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001701
1702 const float rot = M_SQRT1_2; // 45 degrees
1703 const float trans = M_SQRT2 * 16.0f;
1704 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1705
Alec Mouri80863a62019-01-17 15:19:35 -08001706 auto shot = getScreenCapture();
Chia-I Wu93853fe2017-11-02 08:30:27 -07001707 // check a 8x8 region inside each color
1708 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1709 const int32_t halfL = 4;
1710 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1711 };
1712 const int32_t unit = int32_t(trans / 2);
1713 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1714 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1715 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1716 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1717}
1718
Alec Mouri80863a62019-01-17 15:19:35 -08001719TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithResize_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001720 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -07001721 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1722 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001723
1724 // setMatrix is applied after any pending resize, unlike setPosition
1725 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1726 {
1727 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001728 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001729 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -07001730 shot->expectColor(rect, Color::RED);
1731 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001732 }
1733
Marissa Wall861616d2018-10-22 12:52:23 -07001734 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001735 {
1736 SCOPED_TRACE("resize applied");
Marissa Wall861616d2018-10-22 12:52:23 -07001737 const Rect rect(0, 0, 128, 128);
Alec Mouri80863a62019-01-17 15:19:35 -08001738 getScreenCapture()->expectColor(rect, Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001739 }
1740}
1741
Alec Mouri80863a62019-01-17 15:19:35 -08001742TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001743 sp<SurfaceControl> layer;
1744 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001745 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001746
1747 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1748 Transaction()
1749 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1750 .setSize(layer, 64, 64)
1751 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1752 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001753 getScreenCapture()->expectColor(Rect(0, 0, 128, 128), Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001754}
1755
Alec Mouri80863a62019-01-17 15:19:35 -08001756TEST_P(LayerRenderTypeTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001757 sp<SurfaceControl> layer;
1758 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001759 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1760 Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001761
1762 // XXX SCALE_CROP is not respected; calling setSize and
1763 // setOverrideScalingMode in separate transactions does not work
1764 // (b/69315456)
1765 Transaction()
1766 .setSize(layer, 64, 16)
1767 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1768 .apply();
1769 {
1770 SCOPED_TRACE("SCALE_TO_WINDOW");
Alec Mouri80863a62019-01-17 15:19:35 -08001771 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN,
1772 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wua56b2042017-11-01 15:16:35 -07001773 }
1774}
1775
Dan Stoza000dd012018-08-01 13:31:52 -07001776TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1777 sp<SurfaceControl> layer;
1778 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1779
1780 sp<IBinder> handle = layer->getHandle();
1781 ASSERT_TRUE(handle != nullptr);
1782
1783 FrameStats frameStats;
1784 mClient->getLayerFrameStats(handle, &frameStats);
1785
1786 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1787}
1788
Alec Mouri80863a62019-01-17 15:19:35 -08001789TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001790 sp<SurfaceControl> layer;
1791 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001792 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001793 const Rect crop(8, 8, 24, 24);
1794
Marissa Wallf58c14b2018-07-24 10:50:43 -07001795 Transaction().setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001796 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001797 shot->expectColor(crop, Color::RED);
1798 shot->expectBorder(crop, Color::BLACK);
1799}
1800
Alec Mouri80863a62019-01-17 15:19:35 -08001801TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001802 sp<SurfaceControl> layer;
1803 ASSERT_NO_FATAL_FAILURE(
1804 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1805 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1806 const Rect crop(8, 8, 24, 24);
1807
1808 Transaction().setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001809 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001810 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
1811 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001812}
1813
Alec Mouri80863a62019-01-17 15:19:35 -08001814TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001815 sp<SurfaceControl> layer;
1816 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001817 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001818
1819 {
1820 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001821 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001822 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07001823 }
1824
1825 {
1826 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001827 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001828 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07001829 }
1830}
1831
Alec Mouri80863a62019-01-17 15:19:35 -08001832TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001833 sp<SurfaceControl> layer;
1834 ASSERT_NO_FATAL_FAILURE(
1835 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1836 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1837
1838 {
1839 SCOPED_TRACE("empty rect");
1840 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
Marissa Wall290ad082019-03-06 13:23:47 -08001841 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07001842 }
1843
1844 {
1845 SCOPED_TRACE("negative rect");
1846 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
Marissa Wall290ad082019-03-06 13:23:47 -08001847 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07001848 }
1849}
1850
Alec Mouri80863a62019-01-17 15:19:35 -08001851TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001852 sp<SurfaceControl> layer;
1853 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001854 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001855
Marissa Wallf58c14b2018-07-24 10:50:43 -07001856 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001857 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001858 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1859 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1860}
1861
Alec Mouri80863a62019-01-17 15:19:35 -08001862TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferState) {
Valerie Hau0bc09152018-12-20 07:42:47 -08001863 sp<SurfaceControl> layer;
Marissa Wall290ad082019-03-06 13:23:47 -08001864 ASSERT_NO_FATAL_FAILURE(
1865 layer = createLayer("test", 32, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
Valerie Hau0bc09152018-12-20 07:42:47 -08001866 sp<GraphicBuffer> buffer =
Marissa Wall290ad082019-03-06 13:23:47 -08001867 new GraphicBuffer(32, 64, PIXEL_FORMAT_RGBA_8888, 1,
Valerie Hau0bc09152018-12-20 07:42:47 -08001868 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1869 BufferUsage::COMPOSER_OVERLAY,
1870 "test");
Valerie Haue271df92019-09-06 09:23:22 -07001871 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 16), Color::BLUE);
1872 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 16, 32, 64), Color::RED);
Marissa Wall290ad082019-03-06 13:23:47 -08001873
1874 Transaction().setFrame(layer, Rect(0, 0, 64, 64)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001875
1876 Transaction().setBuffer(layer, buffer).apply();
1877
1878 // Partially out of bounds in the negative (upper left) direction
Marissa Wall290ad082019-03-06 13:23:47 -08001879 Transaction().setCrop(layer, Rect(-128, -128, 32, 16)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001880 {
1881 SCOPED_TRACE("out of bounds, negative (upper left) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08001882 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001883 shot->expectColor(Rect(0, 0, 64, 64), Color::BLUE);
1884 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001885 }
1886
1887 // Partially out of bounds in the positive (lower right) direction
Marissa Wall290ad082019-03-06 13:23:47 -08001888 Transaction().setCrop(layer, Rect(0, 16, 128, 128)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001889 {
1890 SCOPED_TRACE("out of bounds, positive (lower right) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08001891 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001892 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1893 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001894 }
1895
1896 // Fully out of buffer space bounds
1897 Transaction().setCrop(layer, Rect(-128, -128, -1, -1)).apply();
1898 {
1899 SCOPED_TRACE("Fully out of bounds");
Alec Mouri80863a62019-01-17 15:19:35 -08001900 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001901 shot->expectColor(Rect(0, 0, 64, 16), Color::BLUE);
1902 shot->expectColor(Rect(0, 16, 64, 64), Color::RED);
1903 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001904 }
1905}
1906
Alec Mouri80863a62019-01-17 15:19:35 -08001907TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001908 sp<SurfaceControl> layer;
1909 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001910 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001911
1912 const Point position(32, 32);
1913 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001914 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001915 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001916 shot->expectColor(crop + position, Color::RED);
1917 shot->expectBorder(crop + position, Color::BLACK);
1918}
1919
Alec Mouri80863a62019-01-17 15:19:35 -08001920TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001921 sp<SurfaceControl> layer;
1922 ASSERT_NO_FATAL_FAILURE(
1923 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1924 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1925
Marissa Wall861616d2018-10-22 12:52:23 -07001926 const Rect frame(32, 32, 64, 64);
Marissa Wall61c58622018-07-18 10:12:20 -07001927 const Rect crop(8, 8, 24, 24);
Marissa Wall861616d2018-10-22 12:52:23 -07001928 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001929 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001930 shot->expectColor(frame, Color::RED);
1931 shot->expectBorder(frame, Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001932}
1933
Alec Mouri80863a62019-01-17 15:19:35 -08001934TEST_P(LayerRenderTypeTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001935 sp<SurfaceControl> layer;
1936 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001937 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001938
Marissa Wall861616d2018-10-22 12:52:23 -07001939 // crop_legacy is affected by matrix
Chia-I Wu04dcca82017-11-02 08:30:27 -07001940 Transaction()
1941 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001942 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001943 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001944 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001945 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1946 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1947}
1948
Alec Mouri80863a62019-01-17 15:19:35 -08001949TEST_P(LayerRenderTypeTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001950 sp<SurfaceControl> layer;
1951 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001952 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001953
Marissa Wallf58c14b2018-07-24 10:50:43 -07001954 // setCrop_legacy is applied immediately by default, with or without resize pending
1955 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001956 {
1957 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001958 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001959 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1960 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1961 }
1962
Marissa Wall61c58622018-07-18 10:12:20 -07001963 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001964 {
1965 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -08001966 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001967 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
1968 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
1969 }
1970}
1971
Alec Mouri80863a62019-01-17 15:19:35 -08001972TEST_P(LayerRenderTypeTransactionTest, SetFrameBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001973 sp<SurfaceControl> layer;
1974 ASSERT_NO_FATAL_FAILURE(
1975 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1976 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1977 const Rect frame(8, 8, 24, 24);
1978
1979 Transaction().setFrame(layer, frame).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001980 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001981 shot->expectColor(frame, Color::RED);
1982 shot->expectBorder(frame, Color::BLACK);
1983}
1984
Alec Mouri80863a62019-01-17 15:19:35 -08001985TEST_P(LayerRenderTypeTransactionTest, SetFrameEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001986 sp<SurfaceControl> layer;
1987 ASSERT_NO_FATAL_FAILURE(
1988 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1989 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1990
Marissa Wall61c58622018-07-18 10:12:20 -07001991 {
Marissa Wall861616d2018-10-22 12:52:23 -07001992 SCOPED_TRACE("empty rect");
1993 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001994 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001995 }
1996
Marissa Wall61c58622018-07-18 10:12:20 -07001997 {
Marissa Wall861616d2018-10-22 12:52:23 -07001998 SCOPED_TRACE("negative rect");
1999 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002000 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002001 }
2002}
2003
Alec Mouri80863a62019-01-17 15:19:35 -08002004TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultParentless_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002005 sp<SurfaceControl> layer;
2006 ASSERT_NO_FATAL_FAILURE(
2007 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2008 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
2009
2010 // A parentless layer will default to a frame with the same size as the buffer
Alec Mouri80863a62019-01-17 15:19:35 -08002011 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002012 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2013 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall861616d2018-10-22 12:52:23 -07002014}
2015
Alec Mouri80863a62019-01-17 15:19:35 -08002016TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBSParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002017 sp<SurfaceControl> parent, child;
2018 ASSERT_NO_FATAL_FAILURE(
2019 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2020 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2021 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2022
2023 ASSERT_NO_FATAL_FAILURE(
2024 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2025 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2026
2027 Transaction().reparent(child, parent->getHandle()).apply();
2028
2029 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002030 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002031 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2032 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2033}
2034
Alec Mouri80863a62019-01-17 15:19:35 -08002035TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBQParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002036 sp<SurfaceControl> parent, child;
2037 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
2038 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
2039
2040 ASSERT_NO_FATAL_FAILURE(
2041 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2042 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2043
2044 Transaction().reparent(child, parent->getHandle()).apply();
2045
2046 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002047 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002048 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2049 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2050}
2051
Alec Mouri80863a62019-01-17 15:19:35 -08002052TEST_P(LayerRenderTypeTransactionTest, SetFrameUpdate_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002053 sp<SurfaceControl> layer;
2054 ASSERT_NO_FATAL_FAILURE(
2055 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2056 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2057 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
2058
2059 std::this_thread::sleep_for(500ms);
2060
2061 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
2062
Alec Mouri80863a62019-01-17 15:19:35 -08002063 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002064 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2065 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2066}
2067
Alec Mouri80863a62019-01-17 15:19:35 -08002068TEST_P(LayerRenderTypeTransactionTest, SetFrameOutsideBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002069 sp<SurfaceControl> parent, child;
2070 ASSERT_NO_FATAL_FAILURE(
2071 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2072 ASSERT_NO_FATAL_FAILURE(
2073 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2074 Transaction().reparent(child, parent->getHandle()).apply();
2075
2076 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2077 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2078
2079 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2080 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
2081
Alec Mouri80863a62019-01-17 15:19:35 -08002082 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002083 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
2084 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
2085 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2086}
2087
Alec Mouri80863a62019-01-17 15:19:35 -08002088TEST_P(LayerRenderTypeTransactionTest, SetBufferBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002089 sp<SurfaceControl> layer;
2090 ASSERT_NO_FATAL_FAILURE(
2091 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2092
2093 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2094
Alec Mouri80863a62019-01-17 15:19:35 -08002095 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002096 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2097 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002098}
2099
Alec Mouri80863a62019-01-17 15:19:35 -08002100TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleBuffers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002101 sp<SurfaceControl> layer;
2102 ASSERT_NO_FATAL_FAILURE(
2103 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2104
2105 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2106
2107 {
2108 SCOPED_TRACE("set buffer 1");
Alec Mouri80863a62019-01-17 15:19:35 -08002109 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002110 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2111 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002112 }
2113
2114 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
2115
2116 {
2117 SCOPED_TRACE("set buffer 2");
Alec Mouri80863a62019-01-17 15:19:35 -08002118 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002119 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLUE);
2120 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002121 }
2122
2123 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2124
2125 {
2126 SCOPED_TRACE("set buffer 3");
Alec Mouri80863a62019-01-17 15:19:35 -08002127 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002128 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2129 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002130 }
2131}
2132
Alec Mouri80863a62019-01-17 15:19:35 -08002133TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleLayers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002134 sp<SurfaceControl> layer1;
2135 ASSERT_NO_FATAL_FAILURE(
2136 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2137
2138 sp<SurfaceControl> layer2;
2139 ASSERT_NO_FATAL_FAILURE(
2140 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2141
2142 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2143
Marissa Wall861616d2018-10-22 12:52:23 -07002144 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002145 {
2146 SCOPED_TRACE("set layer 1 buffer red");
Alec Mouri80863a62019-01-17 15:19:35 -08002147 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002148 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2149 }
2150
2151 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2152
Marissa Wall861616d2018-10-22 12:52:23 -07002153 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002154 {
2155 SCOPED_TRACE("set layer 2 buffer blue");
Alec Mouri80863a62019-01-17 15:19:35 -08002156 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002157 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2158 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2159 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2160 }
2161
2162 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2163 {
2164 SCOPED_TRACE("set layer 1 buffer green");
Alec Mouri80863a62019-01-17 15:19:35 -08002165 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002166 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2167 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2168 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2169 }
2170
2171 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2172
2173 {
2174 SCOPED_TRACE("set layer 2 buffer white");
Alec Mouri80863a62019-01-17 15:19:35 -08002175 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002176 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2177 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2178 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2179 }
2180}
2181
Valerie Haua6b15a12019-02-05 14:16:30 -08002182TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002183 sp<SurfaceControl> layer;
2184 ASSERT_NO_FATAL_FAILURE(
2185 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2186
2187 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2188
2189 std::array<sp<GraphicBuffer>, 10> buffers;
2190
2191 size_t idx = 0;
2192 for (auto& buffer : buffers) {
2193 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2194 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2195 BufferUsage::COMPOSER_OVERLAY,
2196 "test");
2197 Color color = colors[idx % colors.size()];
Valerie Haue271df92019-09-06 09:23:22 -07002198 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
Marissa Wall73411622019-01-25 10:45:41 -08002199 idx++;
2200 }
2201
2202 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2203 // cache is working.
2204 idx = 0;
2205 for (auto& buffer : buffers) {
2206 for (int i = 0; i < 2; i++) {
2207 Transaction().setBuffer(layer, buffer).apply();
2208
2209 Color color = colors[idx % colors.size()];
2210 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002211 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2212 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002213 }
2214 idx++;
2215 }
2216}
2217
Valerie Haua6b15a12019-02-05 14:16:30 -08002218TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_LeastRecentlyUsed_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002219 sp<SurfaceControl> layer;
2220 ASSERT_NO_FATAL_FAILURE(
2221 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2222
2223 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2224
2225 std::array<sp<GraphicBuffer>, 70> buffers;
2226
2227 size_t idx = 0;
2228 for (auto& buffer : buffers) {
2229 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2230 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2231 BufferUsage::COMPOSER_OVERLAY,
2232 "test");
2233 Color color = colors[idx % colors.size()];
Valerie Haue271df92019-09-06 09:23:22 -07002234 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
Marissa Wall73411622019-01-25 10:45:41 -08002235 idx++;
2236 }
2237
2238 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2239 // cache is working.
2240 idx = 0;
2241 for (auto& buffer : buffers) {
2242 for (int i = 0; i < 2; i++) {
2243 Transaction().setBuffer(layer, buffer).apply();
2244
2245 Color color = colors[idx % colors.size()];
2246 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002247 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2248 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002249 }
2250 idx++;
2251 }
2252}
2253
Valerie Haua6b15a12019-02-05 14:16:30 -08002254TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_DestroyedBuffer_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002255 sp<SurfaceControl> layer;
2256 ASSERT_NO_FATAL_FAILURE(
2257 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2258
2259 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2260
2261 std::array<sp<GraphicBuffer>, 65> buffers;
2262
2263 size_t idx = 0;
2264 for (auto& buffer : buffers) {
2265 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2266 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2267 BufferUsage::COMPOSER_OVERLAY,
2268 "test");
2269 Color color = colors[idx % colors.size()];
Valerie Haue271df92019-09-06 09:23:22 -07002270 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
Marissa Wall73411622019-01-25 10:45:41 -08002271 idx++;
2272 }
2273
2274 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2275 // cache is working.
2276 idx = 0;
2277 for (auto& buffer : buffers) {
2278 for (int i = 0; i < 2; i++) {
2279 Transaction().setBuffer(layer, buffer).apply();
2280
2281 Color color = colors[idx % colors.size()];
2282 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002283 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2284 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002285 }
2286 if (idx == 0) {
2287 buffers[0].clear();
2288 }
2289 idx++;
2290 }
2291}
2292
Alec Mouri80863a62019-01-17 15:19:35 -08002293TEST_P(LayerRenderTypeTransactionTest, SetTransformRotate90_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002294 sp<SurfaceControl> layer;
2295 ASSERT_NO_FATAL_FAILURE(
2296 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2297
2298 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2299 Color::BLUE, Color::WHITE));
2300
Marissa Wall861616d2018-10-22 12:52:23 -07002301 Transaction()
2302 .setFrame(layer, Rect(0, 0, 32, 32))
2303 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2304 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002305
Alec Mouri80863a62019-01-17 15:19:35 -08002306 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2307 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002308}
2309
Alec Mouri80863a62019-01-17 15:19:35 -08002310TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipH_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002311 sp<SurfaceControl> layer;
2312 ASSERT_NO_FATAL_FAILURE(
2313 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2314
2315 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2316 Color::BLUE, Color::WHITE));
2317
Marissa Wall861616d2018-10-22 12:52:23 -07002318 Transaction()
2319 .setFrame(layer, Rect(0, 0, 32, 32))
2320 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2321 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002322
Alec Mouri80863a62019-01-17 15:19:35 -08002323 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2324 Color::BLUE, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002325}
2326
Alec Mouri80863a62019-01-17 15:19:35 -08002327TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipV_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002328 sp<SurfaceControl> layer;
2329 ASSERT_NO_FATAL_FAILURE(
2330 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2331
2332 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2333 Color::BLUE, Color::WHITE));
2334
Marissa Wall861616d2018-10-22 12:52:23 -07002335 Transaction()
2336 .setFrame(layer, Rect(0, 0, 32, 32))
2337 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2338 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002339
Alec Mouri80863a62019-01-17 15:19:35 -08002340 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2341 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002342}
2343
2344TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2345 sp<SurfaceControl> layer;
2346 ASSERT_NO_FATAL_FAILURE(
2347 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2348
2349 Transaction().setTransformToDisplayInverse(layer, false).apply();
2350
2351 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2352
2353 Transaction().setTransformToDisplayInverse(layer, true).apply();
2354}
2355
Alec Mouri80863a62019-01-17 15:19:35 -08002356TEST_P(LayerRenderTypeTransactionTest, SetFenceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002357 sp<SurfaceControl> layer;
Marissa Wall713b63f2018-10-17 15:42:43 -07002358 Transaction transaction;
2359 ASSERT_NO_FATAL_FAILURE(
2360 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2361
2362 sp<GraphicBuffer> buffer =
2363 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2364 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2365 BufferUsage::COMPOSER_OVERLAY,
2366 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002367 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall713b63f2018-10-17 15:42:43 -07002368
2369 sp<Fence> fence;
2370 if (getBuffer(nullptr, &fence) != NO_ERROR) {
2371 GTEST_SUCCEED() << "test not supported";
2372 return;
2373 }
2374
2375 Transaction().setBuffer(layer, buffer).setAcquireFence(layer, fence).apply();
2376
2377 status_t status = fence->wait(1000);
2378 ASSERT_NE(static_cast<status_t>(Fence::Status::Unsignaled), status);
2379 std::this_thread::sleep_for(200ms);
2380
Alec Mouri80863a62019-01-17 15:19:35 -08002381 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002382 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2383 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall713b63f2018-10-17 15:42:43 -07002384}
2385
Alec Mouri80863a62019-01-17 15:19:35 -08002386TEST_P(LayerRenderTypeTransactionTest, SetFenceNull_BufferState) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002387 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07002388 ASSERT_NO_FATAL_FAILURE(
2389 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2390
2391 sp<GraphicBuffer> buffer =
2392 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2393 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2394 BufferUsage::COMPOSER_OVERLAY,
2395 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002396 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002397
Marissa Wallfda30bb2018-10-12 11:34:28 -07002398 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002399
2400 Transaction()
2401 .setBuffer(layer, buffer)
2402 .setAcquireFence(layer, fence)
Marissa Wall61c58622018-07-18 10:12:20 -07002403 .apply();
2404
Alec Mouri80863a62019-01-17 15:19:35 -08002405 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002406 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2407 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002408}
2409
Alec Mouri80863a62019-01-17 15:19:35 -08002410TEST_P(LayerRenderTypeTransactionTest, SetDataspaceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002411 sp<SurfaceControl> layer;
2412 ASSERT_NO_FATAL_FAILURE(
2413 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2414
2415 sp<GraphicBuffer> buffer =
2416 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2417 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2418 BufferUsage::COMPOSER_OVERLAY,
2419 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002420 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002421
2422 Transaction()
2423 .setBuffer(layer, buffer)
2424 .setDataspace(layer, ui::Dataspace::UNKNOWN)
Marissa Wall61c58622018-07-18 10:12:20 -07002425 .apply();
2426
Alec Mouri80863a62019-01-17 15:19:35 -08002427 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002428 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2429 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002430}
2431
Alec Mouri80863a62019-01-17 15:19:35 -08002432TEST_P(LayerRenderTypeTransactionTest, SetHdrMetadataBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002433 sp<SurfaceControl> layer;
2434 ASSERT_NO_FATAL_FAILURE(
2435 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2436
2437 sp<GraphicBuffer> buffer =
2438 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2439 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2440 BufferUsage::COMPOSER_OVERLAY,
2441 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002442 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002443
2444 HdrMetadata hdrMetadata;
2445 hdrMetadata.validTypes = 0;
2446 Transaction()
2447 .setBuffer(layer, buffer)
2448 .setHdrMetadata(layer, hdrMetadata)
Marissa Wall61c58622018-07-18 10:12:20 -07002449 .apply();
2450
Alec Mouri80863a62019-01-17 15:19:35 -08002451 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002452 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2453 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002454}
2455
Alec Mouri80863a62019-01-17 15:19:35 -08002456TEST_P(LayerRenderTypeTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002457 sp<SurfaceControl> layer;
2458 ASSERT_NO_FATAL_FAILURE(
2459 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2460
2461 sp<GraphicBuffer> buffer =
2462 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2463 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2464 BufferUsage::COMPOSER_OVERLAY,
2465 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002466 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002467
2468 Region region;
2469 region.set(32, 32);
2470 Transaction()
2471 .setBuffer(layer, buffer)
2472 .setSurfaceDamageRegion(layer, region)
Marissa Wall61c58622018-07-18 10:12:20 -07002473 .apply();
2474
Alec Mouri80863a62019-01-17 15:19:35 -08002475 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002476 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2477 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002478}
2479
Alec Mouri80863a62019-01-17 15:19:35 -08002480TEST_P(LayerRenderTypeTransactionTest, SetApiBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002481 sp<SurfaceControl> layer;
2482 ASSERT_NO_FATAL_FAILURE(
2483 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2484
2485 sp<GraphicBuffer> buffer =
2486 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2487 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2488 BufferUsage::COMPOSER_OVERLAY,
2489 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002490 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002491
2492 Transaction()
2493 .setBuffer(layer, buffer)
2494 .setApi(layer, NATIVE_WINDOW_API_CPU)
Marissa Wall61c58622018-07-18 10:12:20 -07002495 .apply();
2496
Alec Mouri80863a62019-01-17 15:19:35 -08002497 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002498 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2499 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002500}
2501
2502TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2503 sp<SurfaceControl> layer;
2504 ASSERT_NO_FATAL_FAILURE(
2505 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2506
2507 // verify this doesn't cause a crash
2508 Transaction().setSidebandStream(layer, nullptr).apply();
2509}
2510
Robert Carr54cf5b12019-01-25 14:02:28 -08002511TEST_F(LayerTransactionTest, ReparentToSelf) {
2512 sp<SurfaceControl> layer;
2513 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2514 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2515 Transaction().reparent(layer, layer->getHandle()).apply();
2516
2517 {
2518 // We expect the transaction to be silently dropped, but for SurfaceFlinger
2519 // to still be functioning.
2520 SCOPED_TRACE("after reparent to self");
2521 const Rect rect(0, 0, 32, 32);
2522 auto shot = screenshot();
2523 shot->expectColor(rect, Color::RED);
2524 shot->expectBorder(rect, Color::BLACK);
2525 }
2526}
2527
Alec Mouri80863a62019-01-17 15:19:35 -08002528TEST_P(LayerRenderTypeTransactionTest, SetColorTransformBasic) {
Peiyong Lind3788632018-09-18 16:01:31 -07002529 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002530 ASSERT_NO_FATAL_FAILURE(colorLayer =
2531 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2532 ISurfaceComposerClient::eFXSurfaceColor));
2533 Transaction()
2534 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2535 .setLayer(colorLayer, mLayerZBase + 1)
2536 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002537 {
2538 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002539 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Peiyong Lind3788632018-09-18 16:01:31 -07002540 }
2541
2542 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002543 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002544 mat3 matrix;
2545 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2546 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2547 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002548
2549 // degamma before applying the matrix
2550 if (mColorManagementUsed) {
2551 ColorTransformHelper::DegammaColor(expected);
2552 }
2553
2554 ColorTransformHelper::applyMatrix(expected, matrix);
2555
2556 if (mColorManagementUsed) {
2557 ColorTransformHelper::GammaColor(expected);
2558 }
2559
2560 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2561 uint8_t(expected.b * 255), 255};
2562
2563 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2564 // channel) should be less than one
2565 const uint8_t tolerance = 1;
2566
Peiyong Lind3788632018-09-18 16:01:31 -07002567 Transaction().setColor(colorLayer, color)
2568 .setColorTransform(colorLayer, matrix, vec3()).apply();
2569 {
2570 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002571 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002572 }
2573}
2574
Alec Mouri80863a62019-01-17 15:19:35 -08002575TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnParent) {
chaviwf66724d2018-11-28 16:35:21 -08002576 sp<SurfaceControl> parentLayer;
2577 sp<SurfaceControl> colorLayer;
2578 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2579 0 /* buffer height */,
2580 ISurfaceComposerClient::eFXSurfaceContainer));
2581 ASSERT_NO_FATAL_FAILURE(
2582 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2583 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2584
2585 Transaction()
2586 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2587 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2588 .setLayer(parentLayer, mLayerZBase + 1)
2589 .apply();
2590 {
2591 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002592 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08002593 }
2594
2595 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2596 half3 expected = color;
2597 mat3 matrix;
2598 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2599 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2600 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2601
2602 // degamma before applying the matrix
2603 if (mColorManagementUsed) {
2604 ColorTransformHelper::DegammaColor(expected);
2605 }
2606
2607 ColorTransformHelper::applyMatrix(expected, matrix);
2608
2609 if (mColorManagementUsed) {
2610 ColorTransformHelper::GammaColor(expected);
2611 }
2612
2613 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2614 uint8_t(expected.b * 255), 255};
2615
2616 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2617 // channel) should be less than one
2618 const uint8_t tolerance = 1;
2619
2620 Transaction()
2621 .setColor(colorLayer, color)
2622 .setColorTransform(parentLayer, matrix, vec3())
2623 .apply();
2624 {
2625 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002626 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08002627 }
2628}
2629
Alec Mouri80863a62019-01-17 15:19:35 -08002630TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnChildAndParent) {
chaviwf66724d2018-11-28 16:35:21 -08002631 sp<SurfaceControl> parentLayer;
2632 sp<SurfaceControl> colorLayer;
2633 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2634 0 /* buffer height */,
2635 ISurfaceComposerClient::eFXSurfaceContainer));
2636 ASSERT_NO_FATAL_FAILURE(
2637 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2638 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2639
2640 Transaction()
2641 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2642 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2643 .setLayer(parentLayer, mLayerZBase + 1)
2644 .apply();
2645 {
2646 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002647 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08002648 }
2649
2650 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2651 half3 expected = color;
2652 mat3 matrixChild;
2653 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
2654 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
2655 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
2656 mat3 matrixParent;
2657 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
2658 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
2659 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
2660
2661 // degamma before applying the matrix
2662 if (mColorManagementUsed) {
2663 ColorTransformHelper::DegammaColor(expected);
2664 }
2665
2666 ColorTransformHelper::applyMatrix(expected, matrixChild);
2667 ColorTransformHelper::applyMatrix(expected, matrixParent);
2668
2669 if (mColorManagementUsed) {
2670 ColorTransformHelper::GammaColor(expected);
2671 }
2672
2673 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2674 uint8_t(expected.b * 255), 255};
2675
2676 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2677 // channel) should be less than one
2678 const uint8_t tolerance = 1;
2679
2680 Transaction()
2681 .setColor(colorLayer, color)
2682 .setColorTransform(parentLayer, matrixParent, vec3())
2683 .setColorTransform(colorLayer, matrixChild, vec3())
2684 .apply();
2685 {
2686 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002687 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08002688 }
2689}
2690
Marissa Wallfda30bb2018-10-12 11:34:28 -07002691class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07002692public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07002693 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07002694 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002695 }
2696
Marissa Wall713b63f2018-10-17 15:42:43 -07002697 static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
Valerie Hauaa194562019-02-05 16:21:38 -08002698 const sp<SurfaceControl>& layer = nullptr, bool setBuffer = true,
2699 bool setBackgroundColor = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002700 if (layer) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002701 sp<GraphicBuffer> buffer;
2702 sp<Fence> fence;
Valerie Hauaa194562019-02-05 16:21:38 -08002703 if (setBuffer) {
2704 int err = getBuffer(&buffer, &fence);
2705 if (err != NO_ERROR) {
2706 return err;
2707 }
2708
2709 transaction.setBuffer(layer, buffer);
2710 transaction.setAcquireFence(layer, fence);
Marissa Wall713b63f2018-10-17 15:42:43 -07002711 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002712
Valerie Hauaa194562019-02-05 16:21:38 -08002713 if (setBackgroundColor) {
2714 transaction.setBackgroundColor(layer, /*color*/ half3(1.0f, 0, 0), /*alpha*/ 1.0f,
2715 ui::Dataspace::UNKNOWN);
2716 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002717 }
2718
2719 transaction.addTransactionCompletedCallback(callbackHelper->function,
2720 callbackHelper->getContext());
Marissa Wall713b63f2018-10-17 15:42:43 -07002721 return NO_ERROR;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002722 }
2723
Marissa Wall861616d2018-10-22 12:52:23 -07002724 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2725 bool finalState = false) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08002726 CallbackData callbackData;
2727 ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
2728 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
Marissa Wallfda30bb2018-10-12 11:34:28 -07002729
2730 if (finalState) {
2731 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2732 }
2733 }
2734
Marissa Wall861616d2018-10-22 12:52:23 -07002735 static void waitForCallbacks(CallbackHelper& helper,
2736 const std::vector<ExpectedResult>& expectedResults,
2737 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002738 for (const auto& expectedResult : expectedResults) {
2739 waitForCallback(helper, expectedResult);
2740 }
2741 if (finalState) {
2742 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2743 }
2744 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002745};
2746
Valerie Hauaa194562019-02-05 16:21:38 -08002747TEST_F(LayerCallbackTest, BufferColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002748 sp<SurfaceControl> layer;
2749 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2750
2751 Transaction transaction;
2752 CallbackHelper callback;
Valerie Hauaa194562019-02-05 16:21:38 -08002753 int err = fillTransaction(transaction, &callback, layer, true, true);
Marissa Wall713b63f2018-10-17 15:42:43 -07002754 if (err) {
2755 GTEST_SUCCEED() << "test not supported";
2756 return;
2757 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002758
2759 transaction.apply();
2760
2761 ExpectedResult expected;
2762 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2763 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2764}
2765
Valerie Hauaa194562019-02-05 16:21:38 -08002766TEST_F(LayerCallbackTest, NoBufferNoColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002767 sp<SurfaceControl> layer;
2768 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2769
2770 Transaction transaction;
2771 CallbackHelper callback;
Valerie Hauaa194562019-02-05 16:21:38 -08002772 int err = fillTransaction(transaction, &callback, layer, false, false);
Marissa Wall713b63f2018-10-17 15:42:43 -07002773 if (err) {
2774 GTEST_SUCCEED() << "test not supported";
2775 return;
2776 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002777
Marissa Wall861616d2018-10-22 12:52:23 -07002778 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002779
2780 ExpectedResult expected;
Marissa Wall713b63f2018-10-17 15:42:43 -07002781 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
2782 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002783 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2784}
2785
Valerie Hauaa194562019-02-05 16:21:38 -08002786TEST_F(LayerCallbackTest, BufferNoColor) {
2787 sp<SurfaceControl> layer;
2788 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2789
2790 Transaction transaction;
2791 CallbackHelper callback;
2792 int err = fillTransaction(transaction, &callback, layer, true, false);
2793 if (err) {
2794 GTEST_SUCCEED() << "test not supported";
2795 return;
2796 }
2797
2798 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
2799
2800 ExpectedResult expected;
2801 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2802 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2803}
2804
2805TEST_F(LayerCallbackTest, NoBufferColor) {
2806 sp<SurfaceControl> layer;
2807 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2808
2809 Transaction transaction;
2810 CallbackHelper callback;
2811 int err = fillTransaction(transaction, &callback, layer, false, true);
2812 if (err) {
2813 GTEST_SUCCEED() << "test not supported";
2814 return;
2815 }
2816
2817 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
2818
2819 ExpectedResult expected;
2820 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2821 ExpectedResult::Buffer::NOT_ACQUIRED);
2822 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2823}
2824
Marissa Wallfda30bb2018-10-12 11:34:28 -07002825TEST_F(LayerCallbackTest, NoStateChange) {
2826 Transaction transaction;
2827 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002828 int err = fillTransaction(transaction, &callback);
2829 if (err) {
2830 GTEST_SUCCEED() << "test not supported";
2831 return;
2832 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002833
2834 transaction.apply();
2835
2836 ExpectedResult expected;
2837 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2838}
2839
2840TEST_F(LayerCallbackTest, OffScreen) {
2841 sp<SurfaceControl> layer;
2842 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2843
2844 Transaction transaction;
2845 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002846 int err = fillTransaction(transaction, &callback, layer);
2847 if (err) {
2848 GTEST_SUCCEED() << "test not supported";
2849 return;
2850 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002851
Marissa Wall861616d2018-10-22 12:52:23 -07002852 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002853
2854 ExpectedResult expected;
2855 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2856 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2857}
2858
Valerie Hauaa194562019-02-05 16:21:38 -08002859TEST_F(LayerCallbackTest, MergeBufferNoColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002860 sp<SurfaceControl> layer1, layer2;
2861 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2862 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2863
2864 Transaction transaction1, transaction2;
2865 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07002866 int err = fillTransaction(transaction1, &callback1, layer1);
2867 if (err) {
2868 GTEST_SUCCEED() << "test not supported";
2869 return;
2870 }
2871 err = fillTransaction(transaction2, &callback2, layer2);
2872 if (err) {
2873 GTEST_SUCCEED() << "test not supported";
2874 return;
2875 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002876
Marissa Wall861616d2018-10-22 12:52:23 -07002877 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2878 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002879
2880 ExpectedResult expected;
2881 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2882 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2883 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2884}
2885
Valerie Hauaa194562019-02-05 16:21:38 -08002886TEST_F(LayerCallbackTest, MergeNoBufferColor) {
2887 sp<SurfaceControl> layer1, layer2;
2888 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2889 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2890
2891 Transaction transaction1, transaction2;
2892 CallbackHelper callback1, callback2;
2893 int err = fillTransaction(transaction1, &callback1, layer1, false, true);
2894 if (err) {
2895 GTEST_SUCCEED() << "test not supported";
2896 return;
2897 }
2898 err = fillTransaction(transaction2, &callback2, layer2, false, true);
2899 if (err) {
2900 GTEST_SUCCEED() << "test not supported";
2901 return;
2902 }
2903
2904 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2905 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
2906
2907 ExpectedResult expected;
2908 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
2909 ExpectedResult::Buffer::NOT_ACQUIRED);
2910 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2911 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2912}
2913
2914TEST_F(LayerCallbackTest, MergeOneBufferOneColor) {
2915 sp<SurfaceControl> layer1, layer2;
2916 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2917 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2918
2919 Transaction transaction1, transaction2;
2920 CallbackHelper callback1, callback2;
2921 int err = fillTransaction(transaction1, &callback1, layer1);
2922 if (err) {
2923 GTEST_SUCCEED() << "test not supported";
2924 return;
2925 }
2926 err = fillTransaction(transaction2, &callback2, layer2, false, true);
2927 if (err) {
2928 GTEST_SUCCEED() << "test not supported";
2929 return;
2930 }
2931
2932 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2933 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
2934
2935 ExpectedResult expected;
2936 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer1);
2937 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer2,
2938 ExpectedResult::Buffer::NOT_ACQUIRED);
2939 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2940 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2941}
Marissa Wallfda30bb2018-10-12 11:34:28 -07002942TEST_F(LayerCallbackTest, Merge_SameCallback) {
2943 sp<SurfaceControl> layer1, layer2;
2944 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2945 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2946
2947 Transaction transaction1, transaction2;
2948 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002949 int err = fillTransaction(transaction1, &callback, layer1);
2950 if (err) {
2951 GTEST_SUCCEED() << "test not supported";
2952 return;
2953 }
2954 err = fillTransaction(transaction2, &callback, layer2);
2955 if (err) {
2956 GTEST_SUCCEED() << "test not supported";
2957 return;
2958 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002959
2960 transaction2.merge(std::move(transaction1)).apply();
2961
2962 ExpectedResult expected;
2963 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2964 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
2965 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2966}
2967
2968TEST_F(LayerCallbackTest, Merge_SameLayer) {
2969 sp<SurfaceControl> layer;
2970 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2971
2972 Transaction transaction1, transaction2;
2973 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07002974 int err = fillTransaction(transaction1, &callback1, layer);
2975 if (err) {
2976 GTEST_SUCCEED() << "test not supported";
2977 return;
2978 }
2979 err = fillTransaction(transaction2, &callback2, layer);
2980 if (err) {
2981 GTEST_SUCCEED() << "test not supported";
2982 return;
2983 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002984
2985 transaction2.merge(std::move(transaction1)).apply();
2986
2987 ExpectedResult expected;
2988 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2989 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2990 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2991}
2992
Marissa Wallfda30bb2018-10-12 11:34:28 -07002993TEST_F(LayerCallbackTest, Merge_DifferentClients) {
2994 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
2995 client2(new SurfaceComposerClient);
2996
2997 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
2998 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
2999
3000 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003001 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003002 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003003 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003004 ISurfaceComposerClient::eFXSurfaceBufferState));
3005
3006 Transaction transaction1, transaction2;
3007 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003008 int err = fillTransaction(transaction1, &callback1, layer1);
3009 if (err) {
3010 GTEST_SUCCEED() << "test not supported";
3011 return;
3012 }
3013 err = fillTransaction(transaction2, &callback2, layer2);
3014 if (err) {
3015 GTEST_SUCCEED() << "test not supported";
3016 return;
3017 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003018
Marissa Wall861616d2018-10-22 12:52:23 -07003019 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3020 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003021
3022 ExpectedResult expected;
3023 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3024 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3025 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3026}
3027
3028TEST_F(LayerCallbackTest, MultipleTransactions) {
3029 sp<SurfaceControl> layer;
3030 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3031
3032 Transaction transaction;
3033 CallbackHelper callback;
3034 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003035 int err = fillTransaction(transaction, &callback, layer);
3036 if (err) {
3037 GTEST_SUCCEED() << "test not supported";
3038 return;
3039 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003040
3041 transaction.apply();
3042
3043 ExpectedResult expected;
3044 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07003045 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003046 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3047 : ExpectedResult::PreviousBuffer::RELEASED);
3048 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3049 }
3050 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3051}
3052
3053TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
3054 sp<SurfaceControl> layer;
3055 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3056
3057 Transaction transaction;
3058 CallbackHelper callback;
3059 for (size_t i = 0; i < 10; i++) {
3060 ExpectedResult expected;
3061
3062 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003063 int err = fillTransaction(transaction, &callback, layer);
3064 if (err) {
3065 GTEST_SUCCEED() << "test not supported";
3066 return;
3067 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003068 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3069 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003070 int err = fillTransaction(transaction, &callback);
3071 if (err) {
3072 GTEST_SUCCEED() << "test not supported";
3073 return;
3074 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003075 }
3076
3077 transaction.apply();
3078
3079 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3080 }
3081 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3082}
3083
3084TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
3085 sp<SurfaceControl> layer;
3086 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3087
3088 Transaction transaction;
3089 CallbackHelper callback;
3090 for (size_t i = 0; i < 10; i++) {
3091 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003092 int err = fillTransaction(transaction, &callback, layer);
3093 if (err) {
3094 GTEST_SUCCEED() << "test not supported";
3095 return;
3096 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003097 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003098 int err = fillTransaction(transaction, &callback);
3099 if (err) {
3100 GTEST_SUCCEED() << "test not supported";
3101 return;
3102 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003103 }
3104
Marissa Wall861616d2018-10-22 12:52:23 -07003105 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003106
3107 ExpectedResult expected;
3108 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
3109 : ExpectedResult::Transaction::NOT_PRESENTED,
Marissa Wall713b63f2018-10-17 15:42:43 -07003110 layer,
3111 (i == 0) ? ExpectedResult::Buffer::ACQUIRED
3112 : ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003113 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
3114 }
3115 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3116}
3117
3118TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
3119 sp<SurfaceControl> layer1, layer2;
3120 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3121 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3122
3123 Transaction transaction1, transaction2;
3124 CallbackHelper callback1, callback2;
3125 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003126 int err = fillTransaction(transaction1, &callback1, layer1);
3127 if (err) {
3128 GTEST_SUCCEED() << "test not supported";
3129 return;
3130 }
3131 err = fillTransaction(transaction2, &callback2, layer2);
3132 if (err) {
3133 GTEST_SUCCEED() << "test not supported";
3134 return;
3135 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003136
Marissa Wall861616d2018-10-22 12:52:23 -07003137 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3138 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003139
3140 ExpectedResult expected;
3141 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003142 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003143 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3144 : ExpectedResult::PreviousBuffer::RELEASED);
3145 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3146 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3147 }
3148 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3149 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3150}
3151
3152TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
3153 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3154 client2(new SurfaceComposerClient);
3155 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3156 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3157
3158 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003159 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003160 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003161 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003162 ISurfaceComposerClient::eFXSurfaceBufferState));
3163
3164 Transaction transaction1, transaction2;
3165 CallbackHelper callback1, callback2;
3166 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003167 int err = fillTransaction(transaction1, &callback1, layer1);
3168 if (err) {
3169 GTEST_SUCCEED() << "test not supported";
3170 return;
3171 }
3172 err = fillTransaction(transaction2, &callback2, layer2);
3173 if (err) {
3174 GTEST_SUCCEED() << "test not supported";
3175 return;
3176 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003177
Marissa Wall861616d2018-10-22 12:52:23 -07003178 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3179 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003180
3181 ExpectedResult expected;
3182 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003183 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003184 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3185 : ExpectedResult::PreviousBuffer::RELEASED);
3186 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3187 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3188 }
3189 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3190 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3191}
3192
3193TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
3194 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3195 client2(new SurfaceComposerClient);
3196 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3197 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3198
3199 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003200 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003201 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003202 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003203 ISurfaceComposerClient::eFXSurfaceBufferState));
3204
3205 Transaction transaction1, transaction2;
3206 CallbackHelper callback1, callback2;
3207
3208 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003209 int err = fillTransaction(transaction1, &callback1, layer1);
3210 if (err) {
3211 GTEST_SUCCEED() << "test not supported";
3212 return;
3213 }
3214 err = fillTransaction(transaction2, &callback2, layer2);
3215 if (err) {
3216 GTEST_SUCCEED() << "test not supported";
3217 return;
3218 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003219
Marissa Wall861616d2018-10-22 12:52:23 -07003220 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3221 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003222
3223 ExpectedResult expected;
3224 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3225 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3226 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3227 expected.reset();
3228
3229 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003230 err = fillTransaction(transaction1, &callback1);
3231 if (err) {
3232 GTEST_SUCCEED() << "test not supported";
3233 return;
3234 }
3235 err = fillTransaction(transaction2, &callback2);
3236 if (err) {
3237 GTEST_SUCCEED() << "test not supported";
3238 return;
3239 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003240
3241 transaction2.merge(std::move(transaction1)).apply();
3242
3243 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3244 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3245}
3246
3247TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
3248 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3249 client2(new SurfaceComposerClient);
3250
3251 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3252 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3253
3254 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003255 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003256 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003257 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003258 ISurfaceComposerClient::eFXSurfaceBufferState));
3259
3260 Transaction transaction1, transaction2;
3261 CallbackHelper callback1, callback2;
3262
3263 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003264 int err = fillTransaction(transaction1, &callback1, layer1);
3265 if (err) {
3266 GTEST_SUCCEED() << "test not supported";
3267 return;
3268 }
3269 err = fillTransaction(transaction2, &callback2, layer2);
3270 if (err) {
3271 GTEST_SUCCEED() << "test not supported";
3272 return;
3273 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003274
Marissa Wall861616d2018-10-22 12:52:23 -07003275 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3276 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003277
3278 ExpectedResult expected;
3279 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3280 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3281 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3282 expected.reset();
3283
3284 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003285 err = fillTransaction(transaction1, &callback1);
3286 if (err) {
3287 GTEST_SUCCEED() << "test not supported";
3288 return;
3289 }
3290 err = fillTransaction(transaction2, &callback2);
3291 if (err) {
3292 GTEST_SUCCEED() << "test not supported";
3293 return;
3294 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003295
Marissa Wall861616d2018-10-22 12:52:23 -07003296 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003297
Marissa Wall713b63f2018-10-17 15:42:43 -07003298 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
3299 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003300 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3301 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3302}
3303
3304TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3305 sp<SurfaceControl> layer;
3306 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3307
3308 Transaction transaction;
3309 CallbackHelper callback;
3310 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003311 for (auto& expected : expectedResults) {
3312 expected.reset();
3313 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall5a68a772018-12-22 17:43:42 -08003314 ExpectedResult::Buffer::ACQUIRED,
3315 ExpectedResult::PreviousBuffer::UNKNOWN);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003316
Marissa Wall713b63f2018-10-17 15:42:43 -07003317 int err = fillTransaction(transaction, &callback, layer);
3318 if (err) {
3319 GTEST_SUCCEED() << "test not supported";
3320 return;
3321 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003322
3323 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003324 }
3325 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3326}
3327
3328TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3329 sp<SurfaceControl> layer;
3330 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3331
Marissa Wall713b63f2018-10-17 15:42:43 -07003332 // Normal call to set up test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003333 Transaction transaction;
3334 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003335 int err = fillTransaction(transaction, &callback, layer);
3336 if (err) {
3337 GTEST_SUCCEED() << "test not supported";
3338 return;
3339 }
3340
3341 transaction.apply();
3342
3343 ExpectedResult expected;
3344 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3345 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3346
3347 // Test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003348 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003349 for (auto& expected : expectedResults) {
3350 expected.reset();
3351
Marissa Wall713b63f2018-10-17 15:42:43 -07003352 err = fillTransaction(transaction, &callback);
3353 if (err) {
3354 GTEST_SUCCEED() << "test not supported";
3355 return;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003356 }
3357
3358 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003359 }
3360 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3361}
3362
3363TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3364 sp<SurfaceControl> layer;
3365 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3366
3367 // Normal call to set up test
3368 Transaction transaction;
3369 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003370 int err = fillTransaction(transaction, &callback, layer);
3371 if (err) {
3372 GTEST_SUCCEED() << "test not supported";
3373 return;
3374 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003375
Marissa Wall861616d2018-10-22 12:52:23 -07003376 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003377
3378 ExpectedResult expectedResult;
3379 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3380 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3381
3382 // Test
3383 std::vector<ExpectedResult> expectedResults(50);
3384 for (auto& expected : expectedResults) {
3385 expected.reset();
Marissa Wall713b63f2018-10-17 15:42:43 -07003386 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
3387 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003388
Marissa Wall713b63f2018-10-17 15:42:43 -07003389 err = fillTransaction(transaction, &callback);
3390 if (err) {
3391 GTEST_SUCCEED() << "test not supported";
3392 return;
3393 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003394
Marissa Wall861616d2018-10-22 12:52:23 -07003395 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003396 }
3397 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3398}
3399
Marissa Wall17b4e452018-12-26 16:32:34 -08003400TEST_F(LayerCallbackTest, DesiredPresentTime) {
3401 sp<SurfaceControl> layer;
3402 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3403
3404 Transaction transaction;
3405 CallbackHelper callback;
3406 int err = fillTransaction(transaction, &callback, layer);
3407 if (err) {
3408 GTEST_SUCCEED() << "test not supported";
3409 return;
3410 }
3411
3412 // Try to present 100ms in the future
3413 nsecs_t time = systemTime() + (100 * 1e6);
3414
3415 transaction.setDesiredPresentTime(time);
3416 transaction.apply();
3417
3418 ExpectedResult expected;
3419 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3420 expected.addExpectedPresentTime(time);
3421 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3422}
3423
3424TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
3425 sp<SurfaceControl> layer;
3426 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3427
3428 Transaction transaction;
3429 CallbackHelper callback1;
3430 int err = fillTransaction(transaction, &callback1, layer);
3431 if (err) {
3432 GTEST_SUCCEED() << "test not supported";
3433 return;
3434 }
3435
3436 // Try to present 100ms in the future
3437 nsecs_t time = systemTime() + (100 * 1e6);
3438
3439 transaction.setDesiredPresentTime(time);
3440 transaction.apply();
3441
3442 ExpectedResult expected1;
3443 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3444 expected1.addExpectedPresentTime(time);
3445
3446 CallbackHelper callback2;
3447 err = fillTransaction(transaction, &callback2, layer);
3448 if (err) {
3449 GTEST_SUCCEED() << "test not supported";
3450 return;
3451 }
3452
3453 // Try to present 33ms after the first frame
3454 time += (33.3 * 1e6);
3455
3456 transaction.setDesiredPresentTime(time);
3457 transaction.apply();
3458
3459 ExpectedResult expected2;
3460 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3461 ExpectedResult::Buffer::ACQUIRED,
3462 ExpectedResult::PreviousBuffer::RELEASED);
3463 expected2.addExpectedPresentTime(time);
3464
3465 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3466 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3467}
3468
3469TEST_F(LayerCallbackTest, DesiredPresentTime_OutOfOrder) {
3470 sp<SurfaceControl> layer;
3471 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3472
3473 Transaction transaction;
3474 CallbackHelper callback1;
3475 int err = fillTransaction(transaction, &callback1, layer);
3476 if (err) {
3477 GTEST_SUCCEED() << "test not supported";
3478 return;
3479 }
3480
3481 // Try to present 100ms in the future
3482 nsecs_t time = systemTime() + (100 * 1e6);
3483
3484 transaction.setDesiredPresentTime(time);
3485 transaction.apply();
3486
3487 ExpectedResult expected1;
3488 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3489 expected1.addExpectedPresentTime(time);
3490
3491 CallbackHelper callback2;
3492 err = fillTransaction(transaction, &callback2, layer);
3493 if (err) {
3494 GTEST_SUCCEED() << "test not supported";
3495 return;
3496 }
3497
3498 // Try to present 33ms before the previous frame
3499 time -= (33.3 * 1e6);
3500
3501 transaction.setDesiredPresentTime(time);
3502 transaction.apply();
3503
3504 ExpectedResult expected2;
3505 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3506 ExpectedResult::Buffer::ACQUIRED,
3507 ExpectedResult::PreviousBuffer::RELEASED);
3508
3509 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3510 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3511}
3512
3513TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
3514 sp<SurfaceControl> layer;
3515 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3516
3517 Transaction transaction;
3518 CallbackHelper callback;
3519 int err = fillTransaction(transaction, &callback, layer);
3520 if (err) {
3521 GTEST_SUCCEED() << "test not supported";
3522 return;
3523 }
3524
3525 // Try to present 100ms in the past
3526 nsecs_t time = systemTime() - (100 * 1e6);
3527
3528 transaction.setDesiredPresentTime(time);
3529 transaction.apply();
3530
3531 ExpectedResult expected;
3532 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3533 expected.addExpectedPresentTime(systemTime());
3534 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3535}
3536
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003537class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003538protected:
3539 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07003540 LayerTransactionTest::SetUp();
3541 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003542
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08003543 const auto display = SurfaceComposerClient::getInternalDisplayToken();
3544 ASSERT_FALSE(display == nullptr);
3545
Mathias Agopianc666cae2012-07-25 18:56:13 -07003546 DisplayInfo info;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08003547 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
Mathias Agopianc666cae2012-07-25 18:56:13 -07003548
3549 ssize_t displayWidth = info.w;
3550 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003551
3552 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07003553 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
3554 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003555 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003556 ASSERT_TRUE(mBGSurfaceControl->isValid());
Valerie Haue271df92019-09-06 09:23:22 -07003557 TransactionUtils::fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003558
3559 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07003560 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
3561
Peiyong Lin566a3b42018-01-09 18:22:43 -08003562 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003563 ASSERT_TRUE(mFGSurfaceControl->isValid());
3564
Valerie Haue271df92019-09-06 09:23:22 -07003565 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003566
3567 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07003568 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003569 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003570 ASSERT_TRUE(mSyncSurfaceControl->isValid());
3571
Valerie Haue271df92019-09-06 09:23:22 -07003572 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003573
Robert Carr4cdc58f2017-08-23 14:22:20 -07003574 asTransaction([&](Transaction& t) {
3575 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003576
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003577 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07003578
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003579 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
3580 .setPosition(mFGSurfaceControl, 64, 64)
3581 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003582
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003583 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
3584 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
3585 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003586 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003587 }
3588
3589 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07003590 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003591 mBGSurfaceControl = 0;
3592 mFGSurfaceControl = 0;
3593 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003594 }
3595
3596 void waitForPostedBuffers() {
3597 // Since the sync surface is in synchronous mode (i.e. double buffered)
3598 // posting three buffers to it should ensure that at least two
3599 // SurfaceFlinger::handlePageFlip calls have been made, which should
3600 // guaranteed that a buffer posted to another Surface has been retired.
Valerie Haue271df92019-09-06 09:23:22 -07003601 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3602 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3603 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003604 }
3605
Robert Carr4cdc58f2017-08-23 14:22:20 -07003606
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003607 sp<SurfaceControl> mBGSurfaceControl;
3608 sp<SurfaceControl> mFGSurfaceControl;
3609
3610 // This surface is used to ensure that the buffers posted to
3611 // mFGSurfaceControl have been picked up by SurfaceFlinger.
3612 sp<SurfaceControl> mSyncSurfaceControl;
3613};
3614
Robert Carr7f619b22017-11-06 12:56:35 -08003615TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08003616
chaviw0e3479f2018-09-10 16:49:30 -07003617 std::unique_ptr<ScreenCapture> sc;
3618
3619 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Valerie Haue271df92019-09-06 09:23:22 -07003620 TransactionUtils::fillSurfaceRGBA8(relative, 10, 10, 10);
Robert Carr7f619b22017-11-06 12:56:35 -08003621 waitForPostedBuffers();
3622
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003623 Transaction{}
3624 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08003625 .setPosition(relative, 64, 64)
3626 .apply();
3627
3628 {
3629 // The relative should be on top of the FG control.
3630 ScreenCapture::captureScreen(&sc);
3631 sc->checkPixel(64, 64, 10, 10, 10);
3632 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003633 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003634
3635 {
3636 // Nothing should change at this point.
3637 ScreenCapture::captureScreen(&sc);
3638 sc->checkPixel(64, 64, 10, 10, 10);
3639 }
3640
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003641 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003642
3643 {
3644 // Ensure that the relative was actually hidden, rather than
3645 // being left in the detached but visible state.
3646 ScreenCapture::captureScreen(&sc);
3647 sc->expectFGColor(64, 64);
3648 }
3649}
3650
Robert Carr8d5227b2017-03-16 15:41:03 -07003651class GeometryLatchingTest : public LayerUpdateTest {
3652protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003653 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07003654 SCOPED_TRACE(trace);
3655 ScreenCapture::captureScreen(&sc);
3656 // We find the leading edge of the FG surface.
3657 sc->expectFGColor(127, 127);
3658 sc->expectBGColor(128, 128);
3659 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003660
Valerie Haue271df92019-09-06 09:23:22 -07003661 void lockAndFillFGBuffer() {
3662 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false);
3663 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003664
3665 void unlockFGBuffer() {
3666 sp<Surface> s = mFGSurfaceControl->getSurface();
3667 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
3668 waitForPostedBuffers();
3669 }
3670
Robert Carr8d5227b2017-03-16 15:41:03 -07003671 void completeFGResize() {
Valerie Haue271df92019-09-06 09:23:22 -07003672 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
Robert Carr8d5227b2017-03-16 15:41:03 -07003673 waitForPostedBuffers();
3674 }
3675 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003676 asTransaction([&](Transaction& t) {
3677 t.setSize(mFGSurfaceControl, 64, 64);
3678 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003679 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003680 });
Robert Carr8d5227b2017-03-16 15:41:03 -07003681
3682 EXPECT_INITIAL_STATE("After restoring initial state");
3683 }
chaviw0e3479f2018-09-10 16:49:30 -07003684 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07003685};
3686
Robert Carr8d5227b2017-03-16 15:41:03 -07003687class CropLatchingTest : public GeometryLatchingTest {
3688protected:
3689 void EXPECT_CROPPED_STATE(const char* trace) {
3690 SCOPED_TRACE(trace);
3691 ScreenCapture::captureScreen(&sc);
3692 // The edge should be moved back one pixel by our crop.
3693 sc->expectFGColor(126, 126);
3694 sc->expectBGColor(127, 127);
3695 sc->expectBGColor(128, 128);
3696 }
chaviw59f5c562017-06-28 16:39:06 -07003697
3698 void EXPECT_RESIZE_STATE(const char* trace) {
3699 SCOPED_TRACE(trace);
3700 ScreenCapture::captureScreen(&sc);
3701 // The FG is now resized too 128,128 at 64,64
3702 sc->expectFGColor(64, 64);
3703 sc->expectFGColor(191, 191);
3704 sc->expectBGColor(192, 192);
3705 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003706};
3707
Pablo Ceballos05289c22016-04-14 15:49:55 -07003708TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003709 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003710 {
3711 SCOPED_TRACE("before anything");
3712 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003713 sc->expectBGColor(32, 32);
3714 sc->expectFGColor(96, 96);
3715 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003716 }
3717
3718 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003719 asTransaction([&](Transaction& t) {
3720 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003721 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3722 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003723 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003724
Robert Carr4cdc58f2017-08-23 14:22:20 -07003725 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003726 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003727 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3728 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003729 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003730
3731 {
3732 SCOPED_TRACE("before any trigger");
3733 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003734 sc->expectBGColor(32, 32);
3735 sc->expectFGColor(96, 96);
3736 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003737 }
3738
3739 // should trigger the first deferred transaction, but not the second one
Valerie Haue271df92019-09-06 09:23:22 -07003740 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003741 {
3742 SCOPED_TRACE("after first trigger");
3743 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003744 sc->expectBGColor(32, 32);
3745 sc->checkPixel(96, 96, 162, 63, 96);
3746 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003747 }
3748
3749 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003750 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003751
3752 // trigger the second deferred transaction
Valerie Haue271df92019-09-06 09:23:22 -07003753 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003754 {
3755 SCOPED_TRACE("after second trigger");
3756 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003757 sc->expectBGColor(32, 32);
3758 sc->expectBGColor(96, 96);
3759 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003760 }
3761}
3762
Robert Carre392b552017-09-19 12:16:05 -07003763TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003764 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003765
3766 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003767 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3768 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3769 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3770 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Valerie Haue271df92019-09-06 09:23:22 -07003771 TransactionUtils::fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003772 SurfaceComposerClient::Transaction{}
3773 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3774 .show(childNoBuffer)
3775 .show(childBuffer)
3776 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003777 {
3778 ScreenCapture::captureScreen(&sc);
3779 sc->expectChildColor(73, 73);
3780 sc->expectFGColor(74, 74);
3781 }
Vishnu Nair60356342018-11-13 13:00:45 -08003782 SurfaceComposerClient::Transaction{}
3783 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3784 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003785 {
3786 ScreenCapture::captureScreen(&sc);
3787 sc->expectChildColor(73, 73);
3788 sc->expectChildColor(74, 74);
3789 }
3790}
3791
Robert Carr2c5f6d22017-09-26 12:30:35 -07003792TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003793 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003794 {
3795 SCOPED_TRACE("before move");
3796 ScreenCapture::captureScreen(&sc);
3797 sc->expectBGColor(0, 12);
3798 sc->expectFGColor(75, 75);
3799 sc->expectBGColor(145, 145);
3800 }
3801
3802 Transaction t1, t2;
3803 t1.setPosition(mFGSurfaceControl, 128, 128);
3804 t2.setPosition(mFGSurfaceControl, 0, 0);
3805 // We expect that the position update from t2 now
3806 // overwrites the position update from t1.
3807 t1.merge(std::move(t2));
3808 t1.apply();
3809
3810 {
3811 ScreenCapture::captureScreen(&sc);
3812 sc->expectFGColor(1, 1);
3813 }
3814}
3815
Vishnu Nair996bc422019-07-16 14:15:33 -07003816TEST_F(LayerUpdateTest, MergingTransactionFlags) {
3817 Transaction().hide(mFGSurfaceControl).apply();
3818 std::unique_ptr<ScreenCapture> sc;
3819 {
3820 SCOPED_TRACE("before merge");
3821 ScreenCapture::captureScreen(&sc);
3822 sc->expectBGColor(0, 12);
3823 sc->expectBGColor(75, 75);
3824 sc->expectBGColor(145, 145);
3825 }
3826
3827 Transaction t1, t2;
3828 t1.show(mFGSurfaceControl);
3829 t2.setFlags(mFGSurfaceControl, 0 /* flags */, layer_state_t::eLayerSecure /* mask */);
3830 t1.merge(std::move(t2));
3831 t1.apply();
3832
3833 {
3834 SCOPED_TRACE("after merge");
3835 ScreenCapture::captureScreen(&sc);
3836 sc->expectFGColor(75, 75);
3837 }
3838}
3839
Robert Carr1f0a16a2016-10-24 16:27:39 -07003840class ChildLayerTest : public LayerUpdateTest {
3841protected:
3842 void SetUp() override {
3843 LayerUpdateTest::SetUp();
Vishnu Nairc652ff82019-03-15 12:48:54 -07003844 mChild = createSurface(mClient, "Child surface", 10, 15, PIXEL_FORMAT_RGBA_8888, 0,
Vishnu Nair88a11f22018-11-28 18:30:57 -08003845 mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07003846 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
Robert Carr1f0a16a2016-10-24 16:27:39 -07003847
3848 {
3849 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003850 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003851 mCapture->expectChildColor(64, 64);
3852 }
3853 }
3854 void TearDown() override {
3855 LayerUpdateTest::TearDown();
3856 mChild = 0;
3857 }
3858
3859 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003860 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003861};
3862
3863TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003864 asTransaction([&](Transaction& t) {
3865 t.show(mChild);
3866 t.setPosition(mChild, 10, 10);
3867 t.setPosition(mFGSurfaceControl, 64, 64);
3868 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003869
3870 {
chaviw0e3479f2018-09-10 16:49:30 -07003871 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003872 // Top left of foreground must now be visible
3873 mCapture->expectFGColor(64, 64);
3874 // But 10 pixels in we should see the child surface
3875 mCapture->expectChildColor(74, 74);
3876 // And 10 more pixels we should be back to the foreground surface
3877 mCapture->expectFGColor(84, 84);
3878 }
3879
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003880 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003881
3882 {
chaviw0e3479f2018-09-10 16:49:30 -07003883 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003884 // Top left of foreground should now be at 0, 0
3885 mCapture->expectFGColor(0, 0);
3886 // But 10 pixels in we should see the child surface
3887 mCapture->expectChildColor(10, 10);
3888 // And 10 more pixels we should be back to the foreground surface
3889 mCapture->expectFGColor(20, 20);
3890 }
3891}
3892
Robert Carr41b08b52017-06-01 16:11:34 -07003893TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003894 asTransaction([&](Transaction& t) {
3895 t.show(mChild);
3896 t.setPosition(mChild, 0, 0);
3897 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003898 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003899 });
Robert Carr41b08b52017-06-01 16:11:34 -07003900
3901 {
chaviw0e3479f2018-09-10 16:49:30 -07003902 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07003903 mCapture->expectChildColor(0, 0);
3904 mCapture->expectChildColor(4, 4);
3905 mCapture->expectBGColor(5, 5);
3906 }
3907}
3908
Robert Carr1f0a16a2016-10-24 16:27:39 -07003909TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003910 asTransaction([&](Transaction& t) {
3911 t.show(mChild);
3912 t.setPosition(mFGSurfaceControl, 0, 0);
3913 t.setPosition(mChild, 63, 63);
3914 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003915
3916 {
chaviw0e3479f2018-09-10 16:49:30 -07003917 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003918 mCapture->expectFGColor(0, 0);
3919 // Last pixel in foreground should now be the child.
3920 mCapture->expectChildColor(63, 63);
3921 // But the child should be constrained and the next pixel
3922 // must be the background
3923 mCapture->expectBGColor(64, 64);
3924 }
3925}
3926
3927TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003928 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003929
3930 // Find the boundary between the parent and child
3931 {
chaviw0e3479f2018-09-10 16:49:30 -07003932 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003933 mCapture->expectChildColor(9, 9);
3934 mCapture->expectFGColor(10, 10);
3935 }
3936
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003937 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003938
3939 // The boundary should be twice as far from the origin now.
3940 // The pixels from the last test should all be child now
3941 {
chaviw0e3479f2018-09-10 16:49:30 -07003942 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003943 mCapture->expectChildColor(9, 9);
3944 mCapture->expectChildColor(10, 10);
3945 mCapture->expectChildColor(19, 19);
3946 mCapture->expectFGColor(20, 20);
3947 }
3948}
Robert Carr9524cb32017-02-13 11:32:32 -08003949
Vishnu Nairc652ff82019-03-15 12:48:54 -07003950// A child with a scale transform should be cropped by its parent bounds.
3951TEST_F(ChildLayerTest, ChildLayerScalingCroppedByParent) {
3952 asTransaction([&](Transaction& t) {
3953 t.setPosition(mFGSurfaceControl, 0, 0);
3954 t.setPosition(mChild, 0, 0);
3955 });
3956
3957 // Find the boundary between the parent and child.
3958 {
3959 mCapture = screenshot();
3960 mCapture->expectChildColor(0, 0);
3961 mCapture->expectChildColor(9, 9);
3962 mCapture->expectFGColor(10, 10);
3963 }
3964
3965 asTransaction([&](Transaction& t) { t.setMatrix(mChild, 10.0, 0, 0, 10.0); });
3966
3967 // The child should fill its parent bounds and be cropped by it.
3968 {
3969 mCapture = screenshot();
3970 mCapture->expectChildColor(0, 0);
3971 mCapture->expectChildColor(63, 63);
3972 mCapture->expectBGColor(64, 64);
3973 }
3974}
3975
Robert Carr6452f122017-03-21 10:41:29 -07003976TEST_F(ChildLayerTest, ChildLayerAlpha) {
Valerie Haue271df92019-09-06 09:23:22 -07003977 TransactionUtils::fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
3978 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
3979 TransactionUtils::fillSurfaceRGBA8(mChild, 0, 254, 0);
Robert Carr6452f122017-03-21 10:41:29 -07003980 waitForPostedBuffers();
3981
Robert Carr4cdc58f2017-08-23 14:22:20 -07003982 asTransaction([&](Transaction& t) {
3983 t.show(mChild);
3984 t.setPosition(mChild, 0, 0);
3985 t.setPosition(mFGSurfaceControl, 0, 0);
3986 });
Robert Carr6452f122017-03-21 10:41:29 -07003987
3988 {
chaviw0e3479f2018-09-10 16:49:30 -07003989 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003990 // Unblended child color
3991 mCapture->checkPixel(0, 0, 0, 254, 0);
3992 }
3993
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003994 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07003995
3996 {
chaviw0e3479f2018-09-10 16:49:30 -07003997 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07003998 // Child and BG blended.
3999 mCapture->checkPixel(0, 0, 127, 127, 0);
4000 }
4001
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004002 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004003
4004 {
chaviw0e3479f2018-09-10 16:49:30 -07004005 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004006 // Child and BG blended.
4007 mCapture->checkPixel(0, 0, 95, 64, 95);
4008 }
4009}
4010
Robert Carr9524cb32017-02-13 11:32:32 -08004011TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004012 asTransaction([&](Transaction& t) {
4013 t.show(mChild);
4014 t.setPosition(mChild, 10, 10);
4015 t.setPosition(mFGSurfaceControl, 64, 64);
4016 });
Robert Carr9524cb32017-02-13 11:32:32 -08004017
4018 {
chaviw0e3479f2018-09-10 16:49:30 -07004019 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004020 // Top left of foreground must now be visible
4021 mCapture->expectFGColor(64, 64);
4022 // But 10 pixels in we should see the child surface
4023 mCapture->expectChildColor(74, 74);
4024 // And 10 more pixels we should be back to the foreground surface
4025 mCapture->expectFGColor(84, 84);
4026 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004027
4028 asTransaction([&](Transaction& t) {
4029 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
4030 });
4031
Robert Carr9524cb32017-02-13 11:32:32 -08004032 {
chaviw0e3479f2018-09-10 16:49:30 -07004033 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004034 mCapture->expectFGColor(64, 64);
4035 // In reparenting we should have exposed the entire foreground surface.
4036 mCapture->expectFGColor(74, 74);
4037 // And the child layer should now begin at 10, 10 (since the BG
4038 // layer is at (0, 0)).
4039 mCapture->expectBGColor(9, 9);
4040 mCapture->expectChildColor(10, 10);
4041 }
4042}
4043
Robert Carr2e102c92018-10-23 12:11:15 -07004044TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
4045 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004046 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Valerie Haue271df92019-09-06 09:23:22 -07004047 TransactionUtils::fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
Robert Carr2e102c92018-10-23 12:11:15 -07004048
4049 {
4050 SCOPED_TRACE("Grandchild visible");
4051 ScreenCapture::captureScreen(&mCapture);
4052 mCapture->checkPixel(64, 64, 111, 111, 111);
4053 }
4054
Robert Carr87246532019-02-04 15:20:26 -08004055 mChild.clear();
Robert Carr2e102c92018-10-23 12:11:15 -07004056
4057 {
4058 SCOPED_TRACE("After destroying child");
4059 ScreenCapture::captureScreen(&mCapture);
4060 mCapture->expectFGColor(64, 64);
4061 }
4062
4063 asTransaction([&](Transaction& t) {
4064 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
4065 });
4066
4067 {
4068 SCOPED_TRACE("After reparenting grandchild");
4069 ScreenCapture::captureScreen(&mCapture);
4070 mCapture->checkPixel(64, 64, 111, 111, 111);
4071 }
4072}
4073
Vishnu Nair14d76922019-08-05 08:41:20 -07004074TEST_F(ChildLayerTest, ChildrenRelativeZSurvivesParentDestruction) {
4075 sp<SurfaceControl> mGrandChild =
4076 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Valerie Haue271df92019-09-06 09:23:22 -07004077 TransactionUtils::fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
Vishnu Nair14d76922019-08-05 08:41:20 -07004078
4079 // draw grand child behind the foreground surface
4080 asTransaction([&](Transaction& t) {
4081 t.setRelativeLayer(mGrandChild, mFGSurfaceControl->getHandle(), -1);
4082 });
4083
4084 {
4085 SCOPED_TRACE("Child visible");
4086 ScreenCapture::captureScreen(&mCapture);
4087 mCapture->checkPixel(64, 64, 200, 200, 200);
4088 }
4089
4090 asTransaction([&](Transaction& t) {
4091 t.reparent(mChild, nullptr);
4092 t.reparentChildren(mChild, mFGSurfaceControl->getHandle());
4093 });
4094
4095 {
4096 SCOPED_TRACE("foreground visible reparenting grandchild");
4097 ScreenCapture::captureScreen(&mCapture);
4098 mCapture->checkPixel(64, 64, 195, 63, 63);
4099 }
4100}
4101
chaviw161410b02017-07-27 10:46:08 -07004102TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004103 asTransaction([&](Transaction& t) {
4104 t.show(mChild);
4105 t.setPosition(mChild, 10, 10);
4106 t.setPosition(mFGSurfaceControl, 64, 64);
4107 });
Robert Carr9524cb32017-02-13 11:32:32 -08004108
4109 {
chaviw0e3479f2018-09-10 16:49:30 -07004110 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004111 // Top left of foreground must now be visible
4112 mCapture->expectFGColor(64, 64);
4113 // But 10 pixels in we should see the child surface
4114 mCapture->expectChildColor(74, 74);
4115 // And 10 more pixels we should be back to the foreground surface
4116 mCapture->expectFGColor(84, 84);
4117 }
4118
chaviw0e3479f2018-09-10 16:49:30 -07004119
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004120 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08004121
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004122 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08004123
chaviw161410b02017-07-27 10:46:08 -07004124 // Since the child has the same client as the parent, it will not get
4125 // detached and will be hidden.
4126 {
chaviw0e3479f2018-09-10 16:49:30 -07004127 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004128 mCapture->expectFGColor(64, 64);
4129 mCapture->expectFGColor(74, 74);
4130 mCapture->expectFGColor(84, 84);
4131 }
4132}
4133
4134TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
4135 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004136 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004137 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
4138 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07004139
chaviw161410b02017-07-27 10:46:08 -07004140 ASSERT_TRUE(mChildNewClient->isValid());
4141
Valerie Haue271df92019-09-06 09:23:22 -07004142 TransactionUtils::fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
chaviw161410b02017-07-27 10:46:08 -07004143
Robert Carr4cdc58f2017-08-23 14:22:20 -07004144 asTransaction([&](Transaction& t) {
4145 t.hide(mChild);
4146 t.show(mChildNewClient);
4147 t.setPosition(mChildNewClient, 10, 10);
4148 t.setPosition(mFGSurfaceControl, 64, 64);
4149 });
chaviw161410b02017-07-27 10:46:08 -07004150
4151 {
chaviw0e3479f2018-09-10 16:49:30 -07004152 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004153 // Top left of foreground must now be visible
4154 mCapture->expectFGColor(64, 64);
4155 // But 10 pixels in we should see the child surface
4156 mCapture->expectChildColor(74, 74);
4157 // And 10 more pixels we should be back to the foreground surface
4158 mCapture->expectFGColor(84, 84);
4159 }
4160
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004161 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07004162
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004163 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07004164
Robert Carr9524cb32017-02-13 11:32:32 -08004165 // Nothing should have changed.
4166 {
chaviw0e3479f2018-09-10 16:49:30 -07004167 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004168 mCapture->expectFGColor(64, 64);
4169 mCapture->expectChildColor(74, 74);
4170 mCapture->expectFGColor(84, 84);
4171 }
4172}
4173
chaviw5aedec92018-10-22 10:40:38 -07004174TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
4175 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4176 sp<SurfaceControl> childNewClient =
4177 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4178 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4179
4180 ASSERT_TRUE(childNewClient != nullptr);
4181 ASSERT_TRUE(childNewClient->isValid());
4182
Valerie Haue271df92019-09-06 09:23:22 -07004183 TransactionUtils::fillSurfaceRGBA8(childNewClient, 200, 200, 200);
chaviw5aedec92018-10-22 10:40:38 -07004184
4185 Transaction()
4186 .hide(mChild)
4187 .show(childNewClient)
4188 .setPosition(childNewClient, 10, 10)
4189 .setPosition(mFGSurfaceControl, 64, 64)
4190 .apply();
4191
4192 {
4193 mCapture = screenshot();
4194 // Top left of foreground must now be visible
4195 mCapture->expectFGColor(64, 64);
4196 // But 10 pixels in we should see the child surface
4197 mCapture->expectChildColor(74, 74);
4198 // And 10 more pixels we should be back to the foreground surface
4199 mCapture->expectFGColor(84, 84);
4200 }
4201
4202 Transaction().detachChildren(mFGSurfaceControl).apply();
4203 Transaction().hide(childNewClient).apply();
4204
4205 // Nothing should have changed.
4206 {
4207 mCapture = screenshot();
4208 mCapture->expectFGColor(64, 64);
4209 mCapture->expectChildColor(74, 74);
4210 mCapture->expectFGColor(84, 84);
4211 }
4212
4213 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
4214 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
4215 32);
4216 Transaction()
4217 .setLayer(newParentSurface, INT32_MAX - 1)
4218 .show(newParentSurface)
4219 .setPosition(newParentSurface, 20, 20)
4220 .reparent(childNewClient, newParentSurface->getHandle())
4221 .apply();
4222 {
4223 mCapture = screenshot();
4224 // Child is now hidden.
4225 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
4226 }
4227}
chaviw43cb3cb2019-05-31 15:23:41 -07004228TEST_F(ChildLayerTest, DetachChildrenWithDeferredTransaction) {
4229 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4230 sp<SurfaceControl> childNewClient =
4231 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4232 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4233
4234 ASSERT_TRUE(childNewClient != nullptr);
4235 ASSERT_TRUE(childNewClient->isValid());
4236
Valerie Haue271df92019-09-06 09:23:22 -07004237 TransactionUtils::fillSurfaceRGBA8(childNewClient, 200, 200, 200);
chaviw43cb3cb2019-05-31 15:23:41 -07004238
4239 Transaction()
4240 .hide(mChild)
4241 .show(childNewClient)
4242 .setPosition(childNewClient, 10, 10)
4243 .setPosition(mFGSurfaceControl, 64, 64)
4244 .apply();
4245
4246 {
4247 mCapture = screenshot();
4248 Rect rect = Rect(74, 74, 84, 84);
4249 mCapture->expectBorder(rect, Color{195, 63, 63, 255});
4250 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4251 }
4252
4253 Transaction()
4254 .deferTransactionUntil_legacy(childNewClient, mFGSurfaceControl->getHandle(),
4255 mFGSurfaceControl->getSurface()->getNextFrameNumber())
4256 .apply();
4257 Transaction().detachChildren(mFGSurfaceControl).apply();
4258 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(mFGSurfaceControl, Color::RED, 32, 32));
4259
4260 // BufferLayer can still dequeue buffers even though there's a detached layer with a
4261 // deferred transaction.
4262 {
4263 SCOPED_TRACE("new buffer");
4264 mCapture = screenshot();
4265 Rect rect = Rect(74, 74, 84, 84);
4266 mCapture->expectBorder(rect, Color::RED);
4267 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4268 }
4269}
chaviw5aedec92018-10-22 10:40:38 -07004270
Robert Carr9b429f42017-04-17 14:56:57 -07004271TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004272 asTransaction([&](Transaction& t) {
4273 t.show(mChild);
4274 t.setPosition(mChild, 0, 0);
4275 t.setPosition(mFGSurfaceControl, 0, 0);
4276 });
Robert Carr9b429f42017-04-17 14:56:57 -07004277
4278 {
chaviw0e3479f2018-09-10 16:49:30 -07004279 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004280 // We've positioned the child in the top left.
4281 mCapture->expectChildColor(0, 0);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004282 // But it's only 10x15.
4283 mCapture->expectFGColor(10, 15);
Robert Carr9b429f42017-04-17 14:56:57 -07004284 }
4285
Robert Carr4cdc58f2017-08-23 14:22:20 -07004286 asTransaction([&](Transaction& t) {
4287 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4288 // We cause scaling by 2.
4289 t.setSize(mFGSurfaceControl, 128, 128);
4290 });
Robert Carr9b429f42017-04-17 14:56:57 -07004291
4292 {
chaviw0e3479f2018-09-10 16:49:30 -07004293 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004294 // We've positioned the child in the top left.
4295 mCapture->expectChildColor(0, 0);
4296 mCapture->expectChildColor(10, 10);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004297 mCapture->expectChildColor(19, 29);
4298 // And now it should be scaled all the way to 20x30
4299 mCapture->expectFGColor(20, 30);
Robert Carr9b429f42017-04-17 14:56:57 -07004300 }
4301}
4302
Robert Carr1725eee2017-04-26 18:32:15 -07004303// Regression test for b/37673612
4304TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004305 asTransaction([&](Transaction& t) {
4306 t.show(mChild);
4307 t.setPosition(mChild, 0, 0);
4308 t.setPosition(mFGSurfaceControl, 0, 0);
4309 });
Robert Carr1725eee2017-04-26 18:32:15 -07004310
4311 {
chaviw0e3479f2018-09-10 16:49:30 -07004312 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004313 // We've positioned the child in the top left.
4314 mCapture->expectChildColor(0, 0);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004315 mCapture->expectChildColor(9, 14);
4316 // But it's only 10x15.
4317 mCapture->expectFGColor(10, 15);
Robert Carr1725eee2017-04-26 18:32:15 -07004318 }
Robert Carr1725eee2017-04-26 18:32:15 -07004319 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
4320 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004321 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07004322 sp<Surface> s = mFGSurfaceControl->getSurface();
4323 auto anw = static_cast<ANativeWindow*>(s.get());
4324 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4325 native_window_set_buffers_dimensions(anw, 64, 128);
Valerie Haue271df92019-09-06 09:23:22 -07004326 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
Robert Carr1725eee2017-04-26 18:32:15 -07004327 waitForPostedBuffers();
4328
4329 {
4330 // The child should still be in the same place and not have any strange scaling as in
4331 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07004332 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004333 mCapture->expectChildColor(0, 0);
4334 mCapture->expectFGColor(10, 10);
4335 }
4336}
4337
Vishnu Nairc652ff82019-03-15 12:48:54 -07004338// A child with a buffer transform from its parents should be cropped by its parent bounds.
4339TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferTransform) {
4340 asTransaction([&](Transaction& t) {
4341 t.show(mChild);
4342 t.setPosition(mChild, 0, 0);
4343 t.setPosition(mFGSurfaceControl, 0, 0);
4344 t.setSize(mChild, 100, 100);
4345 });
Valerie Haue271df92019-09-06 09:23:22 -07004346 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004347
4348 {
4349 mCapture = screenshot();
4350
4351 mCapture->expectChildColor(0, 0);
4352 mCapture->expectChildColor(63, 63);
4353 mCapture->expectBGColor(64, 64);
4354 }
4355
4356 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
4357 sp<Surface> s = mFGSurfaceControl->getSurface();
4358 auto anw = static_cast<ANativeWindow*>(s.get());
4359 // Apply a 90 transform on the buffer.
4360 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4361 native_window_set_buffers_dimensions(anw, 64, 128);
Valerie Haue271df92019-09-06 09:23:22 -07004362 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004363 waitForPostedBuffers();
4364
4365 // The child should be cropped by the new parent bounds.
4366 {
4367 mCapture = screenshot();
4368 mCapture->expectChildColor(0, 0);
4369 mCapture->expectChildColor(99, 63);
4370 mCapture->expectFGColor(100, 63);
4371 mCapture->expectBGColor(128, 64);
4372 }
4373}
4374
4375// A child with a scale transform from its parents should be cropped by its parent bounds.
4376TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferScale) {
4377 asTransaction([&](Transaction& t) {
4378 t.show(mChild);
4379 t.setPosition(mChild, 0, 0);
4380 t.setPosition(mFGSurfaceControl, 0, 0);
4381 t.setSize(mChild, 200, 200);
4382 });
Valerie Haue271df92019-09-06 09:23:22 -07004383 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004384
4385 {
4386 mCapture = screenshot();
4387
4388 mCapture->expectChildColor(0, 0);
4389 mCapture->expectChildColor(63, 63);
4390 mCapture->expectBGColor(64, 64);
4391 }
4392
4393 asTransaction([&](Transaction& t) {
4394 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4395 // Set a scaling by 2.
4396 t.setSize(mFGSurfaceControl, 128, 128);
4397 });
4398
4399 // Child should inherit its parents scale but should be cropped by its parent bounds.
4400 {
4401 mCapture = screenshot();
4402 mCapture->expectChildColor(0, 0);
4403 mCapture->expectChildColor(127, 127);
4404 mCapture->expectBGColor(128, 128);
4405 }
4406}
4407
4408// Regression test for b/127368943
4409// Child should ignore the buffer transform but apply parent scale transform.
4410TEST_F(ChildLayerTest, ChildrenWithParentBufferTransformAndScale) {
4411 asTransaction([&](Transaction& t) {
4412 t.show(mChild);
4413 t.setPosition(mChild, 0, 0);
4414 t.setPosition(mFGSurfaceControl, 0, 0);
4415 });
4416
4417 {
4418 mCapture = screenshot();
4419 mCapture->expectChildColor(0, 0);
4420 mCapture->expectChildColor(9, 14);
4421 mCapture->expectFGColor(10, 15);
4422 }
4423
4424 // Change the size of the foreground to 128 * 64 so we can test rotation as well.
4425 asTransaction([&](Transaction& t) {
4426 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4427 t.setSize(mFGSurfaceControl, 128, 64);
4428 });
4429 sp<Surface> s = mFGSurfaceControl->getSurface();
4430 auto anw = static_cast<ANativeWindow*>(s.get());
4431 // Apply a 90 transform on the buffer and submit a buffer half the expected size so that we
4432 // have an effective scale of 2.0 applied to the buffer along with a rotation transform.
4433 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4434 native_window_set_buffers_dimensions(anw, 32, 64);
Valerie Haue271df92019-09-06 09:23:22 -07004435 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004436 waitForPostedBuffers();
4437
4438 // The child should ignore the buffer transform but apply the 2.0 scale from parent.
4439 {
4440 mCapture = screenshot();
4441 mCapture->expectChildColor(0, 0);
4442 mCapture->expectChildColor(19, 29);
4443 mCapture->expectFGColor(20, 30);
4444 }
4445}
4446
Dan Stoza412903f2017-04-27 13:42:17 -07004447TEST_F(ChildLayerTest, Bug36858924) {
4448 // Destroy the child layer
4449 mChild.clear();
4450
4451 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08004452 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
4453 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07004454
4455 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07004456 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004457 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
4458 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07004459 t.show(mChild);
4460 });
Dan Stoza412903f2017-04-27 13:42:17 -07004461
4462 // Render the foreground surface a few times
4463 //
4464 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
4465 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
4466 // never acquire/release the first buffer
4467 ALOGI("Filling 1");
Valerie Haue271df92019-09-06 09:23:22 -07004468 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
Dan Stoza412903f2017-04-27 13:42:17 -07004469 ALOGI("Filling 2");
Valerie Haue271df92019-09-06 09:23:22 -07004470 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
Dan Stoza412903f2017-04-27 13:42:17 -07004471 ALOGI("Filling 3");
Valerie Haue271df92019-09-06 09:23:22 -07004472 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
Dan Stoza412903f2017-04-27 13:42:17 -07004473 ALOGI("Filling 4");
Valerie Haue271df92019-09-06 09:23:22 -07004474 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
Dan Stoza412903f2017-04-27 13:42:17 -07004475}
4476
chaviwf1961f72017-09-18 16:41:07 -07004477TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004478 asTransaction([&](Transaction& t) {
4479 t.show(mChild);
4480 t.setPosition(mChild, 10, 10);
4481 t.setPosition(mFGSurfaceControl, 64, 64);
4482 });
chaviw06178942017-07-27 10:25:59 -07004483
4484 {
chaviw0e3479f2018-09-10 16:49:30 -07004485 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004486 // Top left of foreground must now be visible
4487 mCapture->expectFGColor(64, 64);
4488 // But 10 pixels in we should see the child surface
4489 mCapture->expectChildColor(74, 74);
4490 // And 10 more pixels we should be back to the foreground surface
4491 mCapture->expectFGColor(84, 84);
4492 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004493
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004494 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07004495
chaviw06178942017-07-27 10:25:59 -07004496 {
chaviw0e3479f2018-09-10 16:49:30 -07004497 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004498 mCapture->expectFGColor(64, 64);
4499 // In reparenting we should have exposed the entire foreground surface.
4500 mCapture->expectFGColor(74, 74);
4501 // And the child layer should now begin at 10, 10 (since the BG
4502 // layer is at (0, 0)).
4503 mCapture->expectBGColor(9, 9);
4504 mCapture->expectChildColor(10, 10);
4505 }
4506}
4507
chaviwf1961f72017-09-18 16:41:07 -07004508TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004509 asTransaction([&](Transaction& t) {
4510 t.show(mChild);
4511 t.setPosition(mChild, 10, 10);
4512 t.setPosition(mFGSurfaceControl, 64, 64);
4513 });
chaviwf1961f72017-09-18 16:41:07 -07004514
4515 {
chaviw0e3479f2018-09-10 16:49:30 -07004516 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004517 // Top left of foreground must now be visible
4518 mCapture->expectFGColor(64, 64);
4519 // But 10 pixels in we should see the child surface
4520 mCapture->expectChildColor(74, 74);
4521 // And 10 more pixels we should be back to the foreground surface
4522 mCapture->expectFGColor(84, 84);
4523 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004524 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07004525 {
chaviw0e3479f2018-09-10 16:49:30 -07004526 mCapture = screenshot();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004527 // The surface should now be offscreen.
chaviwf1961f72017-09-18 16:41:07 -07004528 mCapture->expectFGColor(64, 64);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004529 mCapture->expectFGColor(74, 74);
chaviwf1961f72017-09-18 16:41:07 -07004530 mCapture->expectFGColor(84, 84);
4531 }
4532}
4533
4534TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07004535 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08004536 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07004537 ASSERT_TRUE(newSurface->isValid());
4538
Valerie Haue271df92019-09-06 09:23:22 -07004539 TransactionUtils::fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004540 asTransaction([&](Transaction& t) {
4541 t.hide(mChild);
4542 t.show(newSurface);
4543 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004544 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004545 t.setPosition(mFGSurfaceControl, 64, 64);
4546 });
chaviwf1961f72017-09-18 16:41:07 -07004547
4548 {
chaviw0e3479f2018-09-10 16:49:30 -07004549 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004550 // Top left of foreground must now be visible
4551 mCapture->expectFGColor(64, 64);
4552 // At 10, 10 we should see the new surface
4553 mCapture->checkPixel(10, 10, 63, 195, 63);
4554 }
4555
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004556 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07004557
4558 {
chaviw0e3479f2018-09-10 16:49:30 -07004559 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004560 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
4561 // mFGSurface, putting it at 74, 74.
4562 mCapture->expectFGColor(64, 64);
4563 mCapture->checkPixel(74, 74, 63, 195, 63);
4564 mCapture->expectFGColor(84, 84);
4565 }
4566}
4567
chaviwc9674332017-08-28 12:32:18 -07004568TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004569 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
4570 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Valerie Haue271df92019-09-06 09:23:22 -07004571 TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
chaviwc9674332017-08-28 12:32:18 -07004572
4573 {
chaviw0e3479f2018-09-10 16:49:30 -07004574 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07004575 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
4576 // which begins at 64, 64
4577 mCapture->checkPixel(64, 64, 50, 50, 50);
4578 }
4579}
4580
Robert Carr503c7042017-09-27 15:06:08 -07004581TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004582 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Valerie Haue271df92019-09-06 09:23:22 -07004583 TransactionUtils::fillSurfaceRGBA8(relative, 255, 255, 255);
Robert Carr503c7042017-09-27 15:06:08 -07004584
4585 Transaction t;
4586 t.setLayer(relative, INT32_MAX)
4587 .setRelativeLayer(mChild, relative->getHandle(), 1)
4588 .setPosition(mFGSurfaceControl, 0, 0)
4589 .apply(true);
4590
4591 // We expect that the child should have been elevated above our
4592 // INT_MAX layer even though it's not a child of it.
4593 {
chaviw0e3479f2018-09-10 16:49:30 -07004594 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07004595 mCapture->expectChildColor(0, 0);
4596 mCapture->expectChildColor(9, 9);
4597 mCapture->checkPixel(10, 10, 255, 255, 255);
4598 }
4599}
Vishnu Nairc652ff82019-03-15 12:48:54 -07004600
Vishnu Nair60356342018-11-13 13:00:45 -08004601class BoundlessLayerTest : public LayerUpdateTest {
4602protected:
4603 std::unique_ptr<ScreenCapture> mCapture;
4604};
4605
4606// Verify setting a size on a buffer layer has no effect.
4607TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
4608 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004609 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
4610 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004611 ASSERT_TRUE(bufferLayer->isValid());
4612 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
4613 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
4614 {
4615 mCapture = screenshot();
4616 // Top left of background must now be visible
4617 mCapture->expectBGColor(0, 0);
4618 // Foreground Surface bounds must be color layer
4619 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
4620 // Buffer layer should not extend past buffer bounds
4621 mCapture->expectFGColor(95, 95);
4622 }
4623}
4624
4625// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
4626// which will crop the color layer.
4627TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
4628 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004629 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4630 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004631 ASSERT_TRUE(colorLayer->isValid());
4632 asTransaction([&](Transaction& t) {
4633 t.setColor(colorLayer, half3{0, 0, 0});
4634 t.show(colorLayer);
4635 });
4636 {
4637 mCapture = screenshot();
4638 // Top left of background must now be visible
4639 mCapture->expectBGColor(0, 0);
4640 // Foreground Surface bounds must be color layer
4641 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4642 // Color layer should not extend past foreground bounds
4643 mCapture->expectBGColor(129, 129);
4644 }
4645}
4646
4647// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
4648// a crop which will be used to crop the color layer.
4649TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004650 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4651 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004652 ASSERT_TRUE(cropLayer->isValid());
4653 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004654 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4655 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004656 ASSERT_TRUE(colorLayer->isValid());
4657 asTransaction([&](Transaction& t) {
4658 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
4659 t.setColor(colorLayer, half3{0, 0, 0});
4660 t.show(cropLayer);
4661 t.show(colorLayer);
4662 });
4663 {
4664 mCapture = screenshot();
4665 // Top left of background must now be visible
4666 mCapture->expectBGColor(0, 0);
4667 // Top left of foreground must now be visible
4668 mCapture->expectFGColor(64, 64);
4669 // 5 pixels from the foreground we should see the child surface
4670 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
4671 // 10 pixels from the foreground we should be back to the foreground surface
4672 mCapture->expectFGColor(74, 74);
4673 }
4674}
4675
4676// Verify for boundless layer with no children, their transforms have no effect.
4677TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
4678 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004679 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4680 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004681 ASSERT_TRUE(colorLayer->isValid());
4682 asTransaction([&](Transaction& t) {
4683 t.setPosition(colorLayer, 320, 320);
4684 t.setMatrix(colorLayer, 2, 0, 0, 2);
4685 t.setColor(colorLayer, half3{0, 0, 0});
4686 t.show(colorLayer);
4687 });
4688 {
4689 mCapture = screenshot();
4690 // Top left of background must now be visible
4691 mCapture->expectBGColor(0, 0);
4692 // Foreground Surface bounds must be color layer
4693 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4694 // Color layer should not extend past foreground bounds
4695 mCapture->expectBGColor(129, 129);
4696 }
4697}
4698
4699// Verify for boundless layer with children, their transforms have an effect.
4700TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
4701 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004702 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4703 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004704 ASSERT_TRUE(boundlessLayerRightShift->isValid());
4705 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004706 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4707 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004708 ASSERT_TRUE(boundlessLayerDownShift->isValid());
4709 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004710 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4711 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004712 ASSERT_TRUE(colorLayer->isValid());
4713 asTransaction([&](Transaction& t) {
4714 t.setPosition(boundlessLayerRightShift, 32, 0);
4715 t.show(boundlessLayerRightShift);
4716 t.setPosition(boundlessLayerDownShift, 0, 32);
4717 t.show(boundlessLayerDownShift);
4718 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4719 t.setColor(colorLayer, half3{0, 0, 0});
4720 t.show(colorLayer);
4721 });
4722 {
4723 mCapture = screenshot();
4724 // Top left of background must now be visible
4725 mCapture->expectBGColor(0, 0);
4726 // Top left of foreground must now be visible
4727 mCapture->expectFGColor(64, 64);
4728 // Foreground Surface bounds must be color layer
4729 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
4730 // Color layer should not extend past foreground bounds
4731 mCapture->expectBGColor(129, 129);
4732 }
4733}
4734
4735// Verify child layers do not get clipped if they temporarily move into the negative
4736// coordinate space as the result of an intermediate transformation.
4737TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
4738 sp<SurfaceControl> boundlessLayer =
4739 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4740 0 /* flags */, mFGSurfaceControl.get());
4741 ASSERT_TRUE(boundlessLayer != nullptr);
4742 ASSERT_TRUE(boundlessLayer->isValid());
4743 sp<SurfaceControl> colorLayer =
4744 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4745 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
4746 ASSERT_TRUE(colorLayer != nullptr);
4747 ASSERT_TRUE(colorLayer->isValid());
4748 asTransaction([&](Transaction& t) {
4749 // shift child layer off bounds. If this layer was not boundless, we will
4750 // expect the child layer to be cropped.
4751 t.setPosition(boundlessLayer, 32, 32);
4752 t.show(boundlessLayer);
4753 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4754 // undo shift by parent
4755 t.setPosition(colorLayer, -32, -32);
4756 t.setColor(colorLayer, half3{0, 0, 0});
4757 t.show(colorLayer);
4758 });
4759 {
4760 mCapture = screenshot();
4761 // Top left of background must now be visible
4762 mCapture->expectBGColor(0, 0);
4763 // Foreground Surface bounds must be color layer
4764 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4765 // Color layer should not extend past foreground bounds
4766 mCapture->expectBGColor(129, 129);
4767 }
4768}
4769
4770// Verify for boundless root layers with children, their transforms have an effect.
4771TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004772 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
4773 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08004774 ASSERT_TRUE(rootBoundlessLayer->isValid());
4775 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004776 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4777 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
4778
Vishnu Nair60356342018-11-13 13:00:45 -08004779 ASSERT_TRUE(colorLayer->isValid());
4780 asTransaction([&](Transaction& t) {
4781 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
4782 t.setPosition(rootBoundlessLayer, 32, 32);
4783 t.show(rootBoundlessLayer);
4784 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4785 t.setColor(colorLayer, half3{0, 0, 0});
4786 t.show(colorLayer);
4787 t.hide(mFGSurfaceControl);
4788 });
4789 {
4790 mCapture = screenshot();
4791 // Top left of background must now be visible
4792 mCapture->expectBGColor(0, 0);
4793 // Top left of foreground must now be visible
4794 mCapture->expectBGColor(31, 31);
4795 // Foreground Surface bounds must be color layer
4796 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
4797 // Color layer should not extend past foreground bounds
4798 mCapture->expectBGColor(97, 97);
4799 }
4800}
Robert Carr503c7042017-09-27 15:06:08 -07004801
chaviwa76b2712017-09-20 12:02:26 -07004802class ScreenCaptureTest : public LayerUpdateTest {
4803protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004804 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07004805};
4806
4807TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
4808 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004809 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004810 mCapture->expectBGColor(0, 0);
4811 // Doesn't capture FG layer which is at 64, 64
4812 mCapture->expectBGColor(64, 64);
4813}
4814
4815TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
4816 auto fgHandle = mFGSurfaceControl->getHandle();
4817
Vishnu Nair88a11f22018-11-28 18:30:57 -08004818 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4819 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004820 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviwa76b2712017-09-20 12:02:26 -07004821
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004822 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004823
4824 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004825 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004826 mCapture->expectFGColor(10, 10);
4827 mCapture->expectChildColor(0, 0);
4828}
4829
Robert Carr578038f2018-03-09 12:25:24 -08004830TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
4831 auto fgHandle = mFGSurfaceControl->getHandle();
4832
Vishnu Nair88a11f22018-11-28 18:30:57 -08004833 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4834 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004835 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
Robert Carr578038f2018-03-09 12:25:24 -08004836
4837 SurfaceComposerClient::Transaction().show(child).apply(true);
4838
4839 // Captures mFGSurfaceControl's child
4840 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4841 mCapture->checkPixel(10, 10, 0, 0, 0);
4842 mCapture->expectChildColor(0, 0);
4843}
4844
Robert Carr866455f2019-04-02 16:28:26 -07004845TEST_F(ScreenCaptureTest, CaptureLayerExclude) {
4846 auto fgHandle = mFGSurfaceControl->getHandle();
4847
4848 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4849 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004850 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
Robert Carr866455f2019-04-02 16:28:26 -07004851 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
4852 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004853 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
Robert Carr866455f2019-04-02 16:28:26 -07004854
4855 SurfaceComposerClient::Transaction()
4856 .show(child)
4857 .show(child2)
4858 .setLayer(child, 1)
4859 .setLayer(child2, 2)
4860 .apply(true);
4861
4862 // Child2 would be visible but its excluded, so we should see child1 color instead.
4863 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
4864 mCapture->checkPixel(10, 10, 0, 0, 0);
4865 mCapture->checkPixel(0, 0, 200, 200, 200);
4866}
4867
4868// Like the last test but verifies that children are also exclude.
4869TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) {
4870 auto fgHandle = mFGSurfaceControl->getHandle();
4871
4872 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4873 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004874 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
Robert Carr866455f2019-04-02 16:28:26 -07004875 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
4876 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004877 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
Robert Carr866455f2019-04-02 16:28:26 -07004878 sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10,
4879 PIXEL_FORMAT_RGBA_8888, 0, child2.get());
Valerie Haue271df92019-09-06 09:23:22 -07004880 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
Robert Carr866455f2019-04-02 16:28:26 -07004881
4882 SurfaceComposerClient::Transaction()
4883 .show(child)
4884 .show(child2)
4885 .show(child3)
4886 .setLayer(child, 1)
4887 .setLayer(child2, 2)
4888 .apply(true);
4889
4890 // Child2 would be visible but its excluded, so we should see child1 color instead.
4891 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
4892 mCapture->checkPixel(10, 10, 0, 0, 0);
4893 mCapture->checkPixel(0, 0, 200, 200, 200);
4894}
4895
chaviw50da5042018-04-09 13:49:37 -07004896TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004897 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4898 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07004899
Valerie Haue271df92019-09-06 09:23:22 -07004900 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviw50da5042018-04-09 13:49:37 -07004901
4902 SurfaceComposerClient::Transaction().show(child).apply(true);
4903
4904 auto childHandle = child->getHandle();
4905
4906 // Captures child
4907 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
4908 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
4909 // Area outside of child's bounds is transparent.
4910 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
4911}
4912
chaviw4b129c22018-04-09 16:19:43 -07004913TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
4914 auto fgHandle = mFGSurfaceControl->getHandle();
4915
Vishnu Nair88a11f22018-11-28 18:30:57 -08004916 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4917 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4918 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07004919 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
Valerie Haue271df92019-09-06 09:23:22 -07004920 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
4921 TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100);
chaviw4b129c22018-04-09 16:19:43 -07004922
4923 SurfaceComposerClient::Transaction()
4924 .show(child)
4925 // Set relative layer above fg layer so should be shown above when computing all layers.
4926 .setRelativeLayer(relative, fgHandle, 1)
4927 .show(relative)
4928 .apply(true);
4929
4930 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
4931 ScreenCapture::captureLayers(&mCapture, fgHandle);
4932 mCapture->expectFGColor(10, 10);
4933 mCapture->expectChildColor(0, 0);
4934}
4935
4936TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
4937 auto fgHandle = mFGSurfaceControl->getHandle();
4938
Vishnu Nair88a11f22018-11-28 18:30:57 -08004939 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4940 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4941 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
4942 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004943 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
4944 TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100);
chaviw4b129c22018-04-09 16:19:43 -07004945
4946 SurfaceComposerClient::Transaction()
4947 .show(child)
4948 // Set relative layer below fg layer but relative to child layer so it should be shown
4949 // above child layer.
4950 .setLayer(relative, -1)
4951 .setRelativeLayer(relative, child->getHandle(), 1)
4952 .show(relative)
4953 .apply(true);
4954
4955 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
4956 // relative value should be taken into account, placing it above child layer.
4957 ScreenCapture::captureLayers(&mCapture, fgHandle);
4958 mCapture->expectFGColor(10, 10);
4959 // Relative layer is showing on top of child layer
4960 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
4961}
Robert Carr578038f2018-03-09 12:25:24 -08004962
4963// In the following tests we verify successful skipping of a parent layer,
4964// so we use the same verification logic and only change how we mutate
4965// the parent layer to verify that various properties are ignored.
4966class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
4967public:
4968 void SetUp() override {
4969 LayerUpdateTest::SetUp();
4970
Vishnu Nair88a11f22018-11-28 18:30:57 -08004971 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
4972 mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004973 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
Robert Carr578038f2018-03-09 12:25:24 -08004974
4975 SurfaceComposerClient::Transaction().show(mChild).apply(true);
4976 }
4977
Vishnu Nair333a9572019-02-15 16:05:56 -08004978 void verify(std::function<void()> verifyStartingState) {
4979 // Verify starting state before a screenshot is taken.
4980 verifyStartingState();
4981
4982 // Verify child layer does not inherit any of the properties of its
4983 // parent when its screenshot is captured.
Robert Carr578038f2018-03-09 12:25:24 -08004984 auto fgHandle = mFGSurfaceControl->getHandle();
4985 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4986 mCapture->checkPixel(10, 10, 0, 0, 0);
4987 mCapture->expectChildColor(0, 0);
Vishnu Nair333a9572019-02-15 16:05:56 -08004988
4989 // Verify all assumptions are still true after the screenshot is taken.
4990 verifyStartingState();
Robert Carr578038f2018-03-09 12:25:24 -08004991 }
4992
4993 std::unique_ptr<ScreenCapture> mCapture;
4994 sp<SurfaceControl> mChild;
4995};
4996
Vishnu Nair333a9572019-02-15 16:05:56 -08004997// Regression test b/76099859
Robert Carr578038f2018-03-09 12:25:24 -08004998TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
4999
5000 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
5001
5002 // Even though the parent is hidden we should still capture the child.
Vishnu Nair333a9572019-02-15 16:05:56 -08005003
5004 // Before and after reparenting, verify child is properly hidden
5005 // when rendering full-screen.
5006 verify([&] { screenshot()->expectBGColor(64, 64); });
Robert Carr578038f2018-03-09 12:25:24 -08005007}
5008
5009TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07005010 SurfaceComposerClient::Transaction()
5011 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
5012 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005013
5014 // Even though the parent is cropped out we should still capture the child.
Vishnu Nair333a9572019-02-15 16:05:56 -08005015
5016 // Before and after reparenting, verify child is cropped by parent.
5017 verify([&] { screenshot()->expectBGColor(65, 65); });
Robert Carr578038f2018-03-09 12:25:24 -08005018}
5019
Vishnu Nair333a9572019-02-15 16:05:56 -08005020// Regression test b/124372894
Robert Carr578038f2018-03-09 12:25:24 -08005021TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
Vishnu Nair333a9572019-02-15 16:05:56 -08005022 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005023
5024 // We should not inherit the parent scaling.
Robert Carr578038f2018-03-09 12:25:24 -08005025
Vishnu Nair333a9572019-02-15 16:05:56 -08005026 // Before and after reparenting, verify child is properly scaled.
5027 verify([&] { screenshot()->expectChildColor(80, 80); });
Robert Carr15eae092018-03-23 13:43:53 -07005028}
5029
5030
chaviwa76b2712017-09-20 12:02:26 -07005031TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
5032 auto fgHandle = mFGSurfaceControl->getHandle();
5033
Vishnu Nair88a11f22018-11-28 18:30:57 -08005034 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5035 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07005036 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviwa76b2712017-09-20 12:02:26 -07005037
Vishnu Nair88a11f22018-11-28 18:30:57 -08005038 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5039 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07005040
Valerie Haue271df92019-09-06 09:23:22 -07005041 TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
chaviwa76b2712017-09-20 12:02:26 -07005042 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005043 .show(child)
5044 .setPosition(grandchild, 5, 5)
5045 .show(grandchild)
5046 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005047
5048 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005049 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07005050 mCapture->expectFGColor(10, 10);
5051 mCapture->expectChildColor(0, 0);
5052 mCapture->checkPixel(5, 5, 50, 50, 50);
5053}
5054
5055TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005056 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5057 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07005058 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviwa76b2712017-09-20 12:02:26 -07005059 auto childHandle = child->getHandle();
5060
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005061 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005062
5063 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005064 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07005065 mCapture->expectChildColor(0, 0);
5066 mCapture->expectChildColor(9, 9);
5067}
5068
5069TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005070 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5071 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07005072 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviwa76b2712017-09-20 12:02:26 -07005073 auto childHandle = child->getHandle();
5074
Vishnu Nair88a11f22018-11-28 18:30:57 -08005075 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5076 PIXEL_FORMAT_RGBA_8888, 0, child.get());
Valerie Haue271df92019-09-06 09:23:22 -07005077 TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
chaviwa76b2712017-09-20 12:02:26 -07005078
5079 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005080 .show(child)
5081 .setPosition(grandchild, 5, 5)
5082 .show(grandchild)
5083 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005084
5085 auto grandchildHandle = grandchild->getHandle();
5086
5087 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005088 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07005089 mCapture->checkPixel(0, 0, 50, 50, 50);
5090 mCapture->checkPixel(4, 4, 50, 50, 50);
5091}
5092
chaviw7206d492017-11-10 16:16:12 -08005093TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07005094 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005095 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5096 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005097
Marissa Wall61c58622018-07-18 10:12:20 -07005098 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5099 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005100
5101 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005102 .setLayer(redLayer, INT32_MAX - 1)
5103 .show(redLayer)
5104 .show(blueLayer)
5105 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005106
5107 auto redLayerHandle = redLayer->getHandle();
5108
5109 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005110 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5111 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5112 // red area below the blue area
5113 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5114 // red area to the right of the blue area
5115 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005116
Marissa Wall861616d2018-10-22 12:52:23 -07005117 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005118 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08005119 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
5120 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005121 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08005122 mCapture->checkPixel(30, 30, 0, 0, 0);
5123}
5124
5125TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07005126 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005127 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5128 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005129
Marissa Wall61c58622018-07-18 10:12:20 -07005130 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5131 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005132
5133 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005134 .setLayer(redLayer, INT32_MAX - 1)
5135 .show(redLayer)
5136 .show(blueLayer)
5137 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005138
5139 auto redLayerHandle = redLayer->getHandle();
5140
5141 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005142 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5143 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5144 // red area below the blue area
5145 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5146 // red area to the right of the blue area
5147 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005148
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005149 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08005150 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005151 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
5152 // red area below the blue area
5153 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
5154 // red area to the right of the blue area
5155 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005156 mCapture->checkPixel(30, 30, 0, 0, 0);
5157}
5158
5159TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07005160 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08005161
Marissa Wall61c58622018-07-18 10:12:20 -07005162 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08005163
5164 auto redLayerHandle = redLayer->getHandle();
Robert Carr87246532019-02-04 15:20:26 -08005165 redLayer.clear();
chaviw7206d492017-11-10 16:16:12 -08005166 SurfaceComposerClient::Transaction().apply(true);
5167
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005168 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08005169
5170 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005171 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
5172 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08005173}
5174
chaviw8e3fe5d2018-02-22 10:55:42 -08005175
5176class DereferenceSurfaceControlTest : public LayerTransactionTest {
5177protected:
5178 void SetUp() override {
5179 LayerTransactionTest::SetUp();
5180 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005181 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005182 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005183 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005184 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
5185 {
5186 SCOPED_TRACE("before anything");
5187 auto shot = screenshot();
5188 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5189 }
5190 }
5191 void TearDown() override {
5192 LayerTransactionTest::TearDown();
5193 bgLayer = 0;
5194 fgLayer = 0;
5195 }
5196
5197 sp<SurfaceControl> bgLayer;
5198 sp<SurfaceControl> fgLayer;
5199};
5200
5201TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
5202 fgLayer = nullptr;
5203 {
5204 SCOPED_TRACE("after setting null");
5205 auto shot = screenshot();
5206 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
5207 }
5208}
5209
5210TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
5211 auto transaction = Transaction().show(fgLayer);
5212 fgLayer = nullptr;
5213 {
5214 SCOPED_TRACE("after setting null");
5215 auto shot = screenshot();
5216 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5217 }
5218}
5219
Vishnu Nairb927e1f2019-02-19 13:36:15 -08005220class MultiDisplayLayerBoundsTest : public LayerTransactionTest {
5221protected:
5222 virtual void SetUp() {
5223 LayerTransactionTest::SetUp();
5224 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5225
5226 mMainDisplay = SurfaceComposerClient::getInternalDisplayToken();
5227 SurfaceComposerClient::getDisplayInfo(mMainDisplay, &mMainDisplayInfo);
5228
5229 sp<IGraphicBufferConsumer> consumer;
5230 BufferQueue::createBufferQueue(&mProducer, &consumer);
5231 consumer->setConsumerName(String8("Virtual disp consumer"));
5232 consumer->setDefaultBufferSize(mMainDisplayInfo.w, mMainDisplayInfo.h);
5233 }
5234
5235 virtual void TearDown() {
5236 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
5237 LayerTransactionTest::TearDown();
5238 mColorLayer = 0;
5239 }
5240
5241 void createDisplay(const Rect& layerStackRect, uint32_t layerStack) {
5242 mVirtualDisplay =
5243 SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), false /*secure*/);
5244 asTransaction([&](Transaction& t) {
5245 t.setDisplaySurface(mVirtualDisplay, mProducer);
5246 t.setDisplayLayerStack(mVirtualDisplay, layerStack);
5247 t.setDisplayProjection(mVirtualDisplay, mMainDisplayInfo.orientation, layerStackRect,
5248 Rect(mMainDisplayInfo.w, mMainDisplayInfo.h));
5249 });
5250 }
5251
5252 void createColorLayer(uint32_t layerStack) {
5253 mColorLayer =
5254 createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */,
5255 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
5256 ASSERT_TRUE(mColorLayer != nullptr);
5257 ASSERT_TRUE(mColorLayer->isValid());
5258 asTransaction([&](Transaction& t) {
5259 t.setLayerStack(mColorLayer, layerStack);
5260 t.setCrop_legacy(mColorLayer, Rect(0, 0, 30, 40));
5261 t.setLayer(mColorLayer, INT32_MAX - 2);
5262 t.setColor(mColorLayer,
5263 half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f,
5264 mExpectedColor.b / 255.0f});
5265 t.show(mColorLayer);
5266 });
5267 }
5268
5269 DisplayInfo mMainDisplayInfo;
5270 sp<IBinder> mMainDisplay;
5271 sp<IBinder> mVirtualDisplay;
5272 sp<IGraphicBufferProducer> mProducer;
5273 sp<SurfaceControl> mColorLayer;
5274 Color mExpectedColor = {63, 63, 195, 255};
5275};
5276
5277TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) {
5278 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 1 /* layerStack */);
5279 createColorLayer(1 /* layerStack */);
5280
5281 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5282
5283 // Verify color layer does not render on main display.
5284 std::unique_ptr<ScreenCapture> sc;
5285 ScreenCapture::captureScreen(&sc, mMainDisplay);
5286 sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255});
5287 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5288
5289 // Verify color layer renders correctly on virtual display.
5290 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5291 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5292 sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 0});
5293}
5294
5295TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) {
5296 // Create a display and set its layer stack to the main display's layer stack so
5297 // the contents of the main display are mirrored on to the virtual display.
5298
5299 // Assumption here is that the new mirrored display has the same viewport as the
5300 // primary display that it is mirroring.
5301 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 0 /* layerStack */);
5302 createColorLayer(0 /* layerStack */);
5303
5304 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5305
5306 // Verify color layer renders correctly on main display and it is mirrored on the
5307 // virtual display.
5308 std::unique_ptr<ScreenCapture> sc;
5309 ScreenCapture::captureScreen(&sc, mMainDisplay);
5310 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5311 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5312
5313 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5314 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5315 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5316}
5317
Ady Abrahamdf9df4a2019-03-12 17:32:05 -07005318class DisplayActiveConfigTest : public ::testing::Test {
5319protected:
5320 void SetUp() override {
5321 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
5322 SurfaceComposerClient::getDisplayConfigs(mDisplayToken, &mDisplayconfigs);
5323 EXPECT_GT(mDisplayconfigs.size(), 0);
5324
5325 // set display power to on to make sure config can be changed
5326 SurfaceComposerClient::setDisplayPowerMode(mDisplayToken, HWC_POWER_MODE_NORMAL);
5327 }
5328
5329 sp<IBinder> mDisplayToken;
5330 Vector<DisplayInfo> mDisplayconfigs;
5331};
5332
5333TEST_F(DisplayActiveConfigTest, allConfigsAllowed) {
5334 std::vector<int32_t> allowedConfigs;
5335
5336 // Add all configs to the allowed configs
5337 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5338 allowedConfigs.push_back(i);
5339 }
5340
5341 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5342 EXPECT_EQ(res, NO_ERROR);
5343
5344 std::vector<int32_t> outConfigs;
5345 res = SurfaceComposerClient::getAllowedDisplayConfigs(mDisplayToken, &outConfigs);
5346 EXPECT_EQ(res, NO_ERROR);
5347 EXPECT_EQ(allowedConfigs, outConfigs);
5348}
5349
5350TEST_F(DisplayActiveConfigTest, changeAllowedConfig) {
5351 // we need at least 2 configs available for this test
5352 if (mDisplayconfigs.size() <= 1) return;
5353
5354 int activeConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5355
5356 // We want to set the allowed config to everything but the active config
5357 std::vector<int32_t> allowedConfigs;
5358 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5359 if (i != activeConfig) {
5360 allowedConfigs.push_back(i);
5361 }
5362 }
5363
5364 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5365 EXPECT_EQ(res, NO_ERROR);
5366
5367 // Allow some time for the config change
5368 std::this_thread::sleep_for(200ms);
5369
5370 int newActiveConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5371 EXPECT_NE(activeConfig, newActiveConfig);
5372
5373 // Make sure the new config is part of allowed config
5374 EXPECT_TRUE(std::find(allowedConfigs.begin(), allowedConfigs.end(), newActiveConfig) !=
5375 allowedConfigs.end());
5376}
5377
Vishnu Nairda9c85a2019-06-03 17:26:48 -07005378class RelativeZTest : public LayerTransactionTest {
5379protected:
5380 virtual void SetUp() {
5381 LayerTransactionTest::SetUp();
5382 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5383
5384 const auto display = SurfaceComposerClient::getInternalDisplayToken();
5385 ASSERT_FALSE(display == nullptr);
5386
5387 // Back layer
5388 mBackgroundLayer = createColorLayer("Background layer", Color::RED);
5389
5390 // Front layer
5391 mForegroundLayer = createColorLayer("Foreground layer", Color::GREEN);
5392
5393 asTransaction([&](Transaction& t) {
5394 t.setDisplayLayerStack(display, 0);
5395 t.setLayer(mBackgroundLayer, INT32_MAX - 2).show(mBackgroundLayer);
5396 t.setLayer(mForegroundLayer, INT32_MAX - 1).show(mForegroundLayer);
5397 });
5398 }
5399
5400 virtual void TearDown() {
5401 LayerTransactionTest::TearDown();
5402 mBackgroundLayer = 0;
5403 mForegroundLayer = 0;
5404 }
5405
5406 sp<SurfaceControl> mBackgroundLayer;
5407 sp<SurfaceControl> mForegroundLayer;
5408};
5409
5410// When a layer is reparented offscreen, remove relative z order if the relative parent
5411// is still onscreen so that the layer is not drawn.
5412TEST_F(RelativeZTest, LayerRemoved) {
5413 std::unique_ptr<ScreenCapture> sc;
5414
5415 // Background layer (RED)
5416 // Child layer (WHITE) (relative to foregroud layer)
5417 // Foregroud layer (GREEN)
5418 sp<SurfaceControl> childLayer =
5419 createColorLayer("Child layer", Color::BLUE, mBackgroundLayer.get());
5420
5421 Transaction{}
5422 .setRelativeLayer(childLayer, mForegroundLayer->getHandle(), 1)
5423 .show(childLayer)
5424 .apply();
5425
5426 {
5427 // The childLayer should be in front of the FG control.
5428 ScreenCapture::captureScreen(&sc);
5429 sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b);
5430 }
5431
5432 // Background layer (RED)
5433 // Foregroud layer (GREEN)
5434 Transaction{}.reparent(childLayer, nullptr).apply();
5435
5436 // Background layer (RED)
5437 // Child layer (WHITE)
5438 // Foregroud layer (GREEN)
5439 Transaction{}.reparent(childLayer, mBackgroundLayer->getHandle()).apply();
5440
5441 {
5442 // The relative z info for child layer should be reset, leaving FG control on top.
5443 ScreenCapture::captureScreen(&sc);
5444 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5445 }
5446}
5447
5448// When a layer is reparented offscreen, preseve relative z order if the relative parent
5449// is also offscreen. Regression test b/132613412
5450TEST_F(RelativeZTest, LayerRemovedOffscreenRelativeParent) {
5451 std::unique_ptr<ScreenCapture> sc;
5452
5453 // Background layer (RED)
5454 // Foregroud layer (GREEN)
5455 // child level 1 (WHITE)
5456 // child level 2a (BLUE)
5457 // child level 3 (GREEN) (relative to child level 2b)
5458 // child level 2b (BLACK)
5459 sp<SurfaceControl> childLevel1 =
5460 createColorLayer("child level 1", Color::WHITE, mForegroundLayer.get());
5461 sp<SurfaceControl> childLevel2a =
5462 createColorLayer("child level 2a", Color::BLUE, childLevel1.get());
5463 sp<SurfaceControl> childLevel2b =
5464 createColorLayer("child level 2b", Color::BLACK, childLevel1.get());
5465 sp<SurfaceControl> childLevel3 =
5466 createColorLayer("child level 3", Color::GREEN, childLevel2a.get());
5467
5468 Transaction{}
5469 .setRelativeLayer(childLevel3, childLevel2b->getHandle(), 1)
5470 .show(childLevel2a)
5471 .show(childLevel2b)
5472 .show(childLevel3)
5473 .apply();
5474
5475 {
5476 // The childLevel3 should be in front of childLevel2b.
5477 ScreenCapture::captureScreen(&sc);
5478 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5479 }
5480
5481 // Background layer (RED)
5482 // Foregroud layer (GREEN)
5483 Transaction{}.reparent(childLevel1, nullptr).apply();
5484
5485 // Background layer (RED)
5486 // Foregroud layer (GREEN)
5487 // child level 1 (WHITE)
5488 // child level 2 back (BLUE)
5489 // child level 3 (GREEN) (relative to child level 2b)
5490 // child level 2 front (BLACK)
5491 Transaction{}.reparent(childLevel1, mForegroundLayer->getHandle()).apply();
5492
5493 {
5494 // Nothing should change at this point since relative z info was preserved.
5495 ScreenCapture::captureScreen(&sc);
5496 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5497 }
5498}
5499
Steven Thomas44685cb2019-07-23 16:19:31 -07005500// This test ensures that when we drop an app buffer in SurfaceFlinger, we merge
5501// the dropped buffer's damage region into the next buffer's damage region. If
5502// we don't do this, we'll report an incorrect damage region to hardware
5503// composer, resulting in broken rendering. This test checks the BufferQueue
5504// case.
5505//
5506// Unfortunately, we don't currently have a way to inspect the damage region
5507// SurfaceFlinger sends to hardware composer from a test, so this test requires
5508// the dev to manually watch the device's screen during the test to spot broken
5509// rendering. Because the results can't be automatically verified, this test is
5510// marked disabled.
5511TEST_F(LayerTransactionTest, DISABLED_BufferQueueLayerMergeDamageRegionWhenDroppingBuffers) {
5512 const int width = mDisplayWidth;
5513 const int height = mDisplayHeight;
5514 sp<SurfaceControl> layer;
5515 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
5516 const auto producer = layer->getIGraphicBufferProducer();
5517 const sp<IProducerListener> dummyListener(new DummyProducerListener);
5518 IGraphicBufferProducer::QueueBufferOutput queueBufferOutput;
5519 ASSERT_EQ(OK,
5520 producer->connect(dummyListener, NATIVE_WINDOW_API_CPU, true, &queueBufferOutput));
5521
5522 std::map<int, sp<GraphicBuffer>> slotMap;
5523 auto slotToBuffer = [&](int slot, sp<GraphicBuffer>* buf) {
5524 ASSERT_NE(nullptr, buf);
5525 const auto iter = slotMap.find(slot);
5526 ASSERT_NE(slotMap.end(), iter);
5527 *buf = iter->second;
5528 };
5529
5530 auto dequeue = [&](int* outSlot) {
5531 ASSERT_NE(nullptr, outSlot);
5532 *outSlot = -1;
5533 int slot;
5534 sp<Fence> fence;
5535 uint64_t age;
5536 FrameEventHistoryDelta timestamps;
5537 const status_t dequeueResult =
5538 producer->dequeueBuffer(&slot, &fence, width, height, PIXEL_FORMAT_RGBA_8888,
5539 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
5540 &age, &timestamps);
5541 if (dequeueResult == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
5542 sp<GraphicBuffer> newBuf;
5543 ASSERT_EQ(OK, producer->requestBuffer(slot, &newBuf));
5544 ASSERT_NE(nullptr, newBuf.get());
5545 slotMap[slot] = newBuf;
5546 } else {
5547 ASSERT_EQ(OK, dequeueResult);
5548 }
5549 *outSlot = slot;
5550 };
5551
5552 auto queue = [&](int slot, const Region& damage, nsecs_t displayTime) {
5553 IGraphicBufferProducer::QueueBufferInput input(
5554 /*timestamp=*/displayTime, /*isAutoTimestamp=*/false, HAL_DATASPACE_UNKNOWN,
5555 /*crop=*/Rect::EMPTY_RECT, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
5556 /*transform=*/0, Fence::NO_FENCE);
5557 input.setSurfaceDamage(damage);
5558 IGraphicBufferProducer::QueueBufferOutput output;
5559 ASSERT_EQ(OK, producer->queueBuffer(slot, input, &output));
5560 };
5561
5562 auto fillAndPostBuffers = [&](const Color& color) {
5563 int slot1;
5564 ASSERT_NO_FATAL_FAILURE(dequeue(&slot1));
5565 int slot2;
5566 ASSERT_NO_FATAL_FAILURE(dequeue(&slot2));
5567
5568 sp<GraphicBuffer> buf1;
5569 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot1, &buf1));
5570 sp<GraphicBuffer> buf2;
5571 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot2, &buf2));
Valerie Haue271df92019-09-06 09:23:22 -07005572 TransactionUtils::fillGraphicBufferColor(buf1, Rect(width, height), color);
5573 TransactionUtils::fillGraphicBufferColor(buf2, Rect(width, height), color);
Steven Thomas44685cb2019-07-23 16:19:31 -07005574
5575 const auto displayTime = systemTime() + milliseconds_to_nanoseconds(100);
5576 ASSERT_NO_FATAL_FAILURE(queue(slot1, Region::INVALID_REGION, displayTime));
5577 ASSERT_NO_FATAL_FAILURE(
5578 queue(slot2, Region(Rect(width / 3, height / 3, 2 * width / 3, 2 * height / 3)),
5579 displayTime));
5580 };
5581
5582 const auto startTime = systemTime();
5583 const std::array<Color, 3> colors = {Color::RED, Color::GREEN, Color::BLUE};
5584 int colorIndex = 0;
5585 while (nanoseconds_to_seconds(systemTime() - startTime) < 10) {
5586 ASSERT_NO_FATAL_FAILURE(fillAndPostBuffers(colors[colorIndex++ % colors.size()]));
5587 std::this_thread::sleep_for(1s);
5588 }
5589
5590 ASSERT_EQ(OK, producer->disconnect(NATIVE_WINDOW_API_CPU));
5591}
5592
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005593} // namespace android