blob: ece61df31085a890f20cca6558e2bf45ec2ee4ca [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, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700566 sp<SurfaceControl> layer;
567 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700568 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700569
570 // request setPosition to be applied with the next resize
571 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
572 {
573 SCOPED_TRACE("new position pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800574 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700575 }
576
577 Transaction().setPosition(layer, 15, 20).apply();
578 {
579 SCOPED_TRACE("pending new position modified");
Alec Mouri80863a62019-01-17 15:19:35 -0800580 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700581 }
582
583 Transaction().setSize(layer, 64, 64).apply();
584 {
585 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800586 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700587 }
588
589 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700590 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700591 {
592 SCOPED_TRACE("new position applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800593 getScreenCapture()->expectColor(Rect(15, 20, 79, 84), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700594 }
595}
596
Alec Mouri80863a62019-01-17 15:19:35 -0800597TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700598 sp<SurfaceControl> layer;
599 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700600 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700601
602 // setPosition is not immediate even with SCALE_TO_WINDOW override
603 Transaction()
604 .setPosition(layer, 5, 10)
605 .setSize(layer, 64, 64)
606 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
607 .setGeometryAppliesWithResize(layer)
608 .apply();
609 {
610 SCOPED_TRACE("new position pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800611 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700612 }
613
Marissa Wall61c58622018-07-18 10:12:20 -0700614 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700615 {
616 SCOPED_TRACE("new position applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800617 getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700618 }
619}
620
Alec Mouri80863a62019-01-17 15:19:35 -0800621TEST_P(LayerRenderTypeTransactionTest, SetSizeBasic_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700622 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700623 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
624 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700625
626 Transaction().setSize(layer, 64, 64).apply();
627 {
628 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800629 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700630 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -0700631 shot->expectColor(rect, Color::RED);
632 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700633 }
634
Marissa Wall861616d2018-10-22 12:52:23 -0700635 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700636 {
637 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800638 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700639 const Rect rect(0, 0, 64, 64);
640 shot->expectColor(rect, Color::RED);
641 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700642 }
643}
644
Alec Mouri80863a62019-01-17 15:19:35 -0800645TEST_P(LayerTypeAndRenderTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700646 // cannot test robustness against invalid sizes (zero or really huge)
647}
648
Alec Mouri80863a62019-01-17 15:19:35 -0800649TEST_P(LayerRenderTypeTransactionTest, SetSizeWithScaleToWindow_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700650 sp<SurfaceControl> layer;
651 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700652 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700653
654 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
655 Transaction()
656 .setSize(layer, 64, 64)
657 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
658 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -0800659 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700660}
661
Alec Mouri80863a62019-01-17 15:19:35 -0800662TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700663 sp<SurfaceControl> layerR;
664 sp<SurfaceControl> layerG;
665 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700666 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700667 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700668 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700669
670 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
671 {
672 SCOPED_TRACE("layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800673 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700674 }
675
676 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
677 {
678 SCOPED_TRACE("layerG");
Alec Mouri80863a62019-01-17 15:19:35 -0800679 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700680 }
681}
682
Alec Mouri80863a62019-01-17 15:19:35 -0800683TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700684 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800685 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700686 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800687 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700688 sp<SurfaceControl> layerR;
689 sp<SurfaceControl> layerG;
690 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700691 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700692 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700693 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700694
chaviw0e3479f2018-09-10 16:49:30 -0700695 Transaction()
696 .reparent(layerR, parent->getHandle())
697 .reparent(layerG, parent->getHandle())
698 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700699 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
700 {
701 SCOPED_TRACE("layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800702 auto shot = getScreenCapture();
chaviw0e3479f2018-09-10 16:49:30 -0700703 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700704 }
705
706 Transaction().setLayer(layerR, -3).apply();
707 {
708 SCOPED_TRACE("layerG");
Alec Mouri80863a62019-01-17 15:19:35 -0800709 auto shot = getScreenCapture();
chaviw0e3479f2018-09-10 16:49:30 -0700710 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700711 }
712}
713
Alec Mouri80863a62019-01-17 15:19:35 -0800714void LayerRenderTypeTransactionTest::setRelativeZBasicHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700715 sp<SurfaceControl> layerR;
716 sp<SurfaceControl> layerG;
Marissa Wall861616d2018-10-22 12:52:23 -0700717 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32, layerType));
718 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
719 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32, layerType));
720 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700721
Marissa Wall861616d2018-10-22 12:52:23 -0700722 switch (layerType) {
723 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
724 Transaction()
725 .setPosition(layerG, 16, 16)
726 .setRelativeLayer(layerG, layerR->getHandle(), 1)
727 .apply();
728 break;
729 case ISurfaceComposerClient::eFXSurfaceBufferState:
730 Transaction()
731 .setFrame(layerR, Rect(0, 0, 32, 32))
732 .setFrame(layerG, Rect(16, 16, 48, 48))
733 .setRelativeLayer(layerG, layerR->getHandle(), 1)
734 .apply();
735 break;
736 default:
737 ASSERT_FALSE(true) << "Unsupported layer type";
738 }
Chia-I Wu49313302017-10-31 10:14:40 -0700739 {
740 SCOPED_TRACE("layerG above");
Alec Mouri80863a62019-01-17 15:19:35 -0800741 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700742 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
743 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
744 }
745
746 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
747 {
748 SCOPED_TRACE("layerG below");
Alec Mouri80863a62019-01-17 15:19:35 -0800749 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700750 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
751 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
752 }
753}
754
Alec Mouri80863a62019-01-17 15:19:35 -0800755TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -0700756 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
757}
758
Alec Mouri80863a62019-01-17 15:19:35 -0800759TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -0700760 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
761}
762
Marissa Wall61c58622018-07-18 10:12:20 -0700763TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700764 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800765 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700766 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800767 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wuec2d9852017-11-21 09:21:01 -0800768 sp<SurfaceControl> layerR;
769 sp<SurfaceControl> layerG;
770 sp<SurfaceControl> layerB;
771 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700772 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800773 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700774 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800775 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700776 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800777
chaviw0e3479f2018-09-10 16:49:30 -0700778 Transaction()
779 .reparent(layerB, parent->getHandle())
780 .apply();
781
Chia-I Wuec2d9852017-11-21 09:21:01 -0800782 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
783 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
784
chaviw0e3479f2018-09-10 16:49:30 -0700785 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800786 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700787 sp<IBinder> parentHandle = parent->getHandle();
Marissa Wall861616d2018-10-22 12:52:23 -0700788 ScreenCapture::captureLayers(&screenshot, parentHandle, Rect(0, 0, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800789 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
790}
791
chaviw32377582019-05-13 11:15:19 -0700792TEST_P(LayerTypeTransactionTest, SetLayerAndRelative) {
793 sp<SurfaceControl> parent =
794 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
795 ISurfaceComposerClient::eFXSurfaceColor);
796
797 sp<SurfaceControl> childLayer;
798 ASSERT_NO_FATAL_FAILURE(
799 childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
800 0 /* buffer height */,
801 ISurfaceComposerClient::eFXSurfaceColor,
802 parent.get()));
803 Transaction()
804 .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
805 .setColor(parent, half3{0.0f, 0.0f, 0.0f})
806 .show(childLayer)
807 .show(parent)
808 .setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight))
809 .setCrop_legacy(childLayer, Rect(0, 0, 20, 30))
810 .apply();
811
812 Transaction()
813 .setRelativeLayer(childLayer, parent->getHandle(), -1)
814 .setLayer(childLayer, 1)
815 .apply();
816
817 {
818 SCOPED_TRACE("setLayer above");
819 // Set layer should get applied and place the child above.
820 std::unique_ptr<ScreenCapture> screenshot;
821 ScreenCapture::captureScreen(&screenshot);
822 screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
823 }
824
825 Transaction()
826 .setLayer(childLayer, 1)
827 .setRelativeLayer(childLayer, parent->getHandle(), -1)
828 .apply();
829
830 {
831 SCOPED_TRACE("setRelative below");
832 // Set relative layer should get applied and place the child below.
833 std::unique_ptr<ScreenCapture> screenshot;
834 ScreenCapture::captureScreen(&screenshot);
835 screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
836 }
837}
838
Robert Carr1c5481e2019-07-01 14:42:27 -0700839TEST_P(LayerTypeTransactionTest, HideRelativeParentHidesLayer) {
840 sp<SurfaceControl> parent =
841 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
842 ISurfaceComposerClient::eFXSurfaceColor);
843 sp<SurfaceControl> relativeParent =
844 LayerTransactionTest::createLayer("RelativeParent", 0 /* buffer width */,
845 0 /* buffer height */, ISurfaceComposerClient::eFXSurfaceColor);
846
847 sp<SurfaceControl> childLayer;
848 ASSERT_NO_FATAL_FAILURE(
849 childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
850 0 /* buffer height */,
851 ISurfaceComposerClient::eFXSurfaceColor,
852 parent.get()));
853 Transaction()
854 .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
855 .setColor(parent, half3{0.0f, 0.0f, 0.0f})
856 .setColor(relativeParent, half3{0.0f, 1.0f, 0.0f})
857 .show(childLayer)
858 .show(parent)
859 .show(relativeParent)
860 .setLayer(parent, mLayerZBase - 1)
861 .setLayer(relativeParent, mLayerZBase)
862 .apply();
863
864 Transaction()
865 .setRelativeLayer(childLayer, relativeParent->getHandle(), 1)
866 .apply();
867
868 {
869 SCOPED_TRACE("setLayer above");
870 // Set layer should get applied and place the child above.
871 std::unique_ptr<ScreenCapture> screenshot;
872 ScreenCapture::captureScreen(&screenshot);
873 screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
874 }
875
876 Transaction()
877 .hide(relativeParent)
878 .apply();
879
880 {
881 SCOPED_TRACE("hide relative parent");
882 // The relative should no longer be visible.
883 std::unique_ptr<ScreenCapture> screenshot;
884 ScreenCapture::captureScreen(&screenshot);
885 screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
886 }
887}
888
Alec Mouri80863a62019-01-17 15:19:35 -0800889void LayerRenderTypeTransactionTest::setRelativeZGroupHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700890 sp<SurfaceControl> layerR;
891 sp<SurfaceControl> layerG;
892 sp<SurfaceControl> layerB;
Marissa Wall861616d2018-10-22 12:52:23 -0700893 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test", 32, 32, layerType));
894 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
895 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test", 32, 32, layerType));
896 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
897 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test", 32, 32, layerType));
898 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700899
900 // layerR = 0, layerG = layerR + 3, layerB = 2
Marissa Wall861616d2018-10-22 12:52:23 -0700901 switch (layerType) {
902 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
903 Transaction()
904 .setPosition(layerG, 8, 8)
905 .setRelativeLayer(layerG, layerR->getHandle(), 3)
906 .setPosition(layerB, 16, 16)
907 .setLayer(layerB, mLayerZBase + 2)
908 .apply();
909 break;
910 case ISurfaceComposerClient::eFXSurfaceBufferState:
911 Transaction()
912 .setFrame(layerR, Rect(0, 0, 32, 32))
913 .setFrame(layerG, Rect(8, 8, 40, 40))
914 .setRelativeLayer(layerG, layerR->getHandle(), 3)
915 .setFrame(layerB, Rect(16, 16, 48, 48))
916 .setLayer(layerB, mLayerZBase + 2)
917 .apply();
918 break;
919 default:
920 ASSERT_FALSE(true) << "Unsupported layer type";
921 }
922
Chia-I Wu49313302017-10-31 10:14:40 -0700923 {
924 SCOPED_TRACE("(layerR < layerG) < layerB");
Alec Mouri80863a62019-01-17 15:19:35 -0800925 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700926 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
927 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
928 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
929 }
930
931 // layerR = 4, layerG = layerR + 3, layerB = 2
932 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
933 {
934 SCOPED_TRACE("layerB < (layerR < layerG)");
Alec Mouri80863a62019-01-17 15:19:35 -0800935 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700936 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
937 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
938 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
939 }
940
941 // layerR = 4, layerG = layerR - 3, layerB = 2
942 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
943 {
944 SCOPED_TRACE("layerB < (layerG < layerR)");
Alec Mouri80863a62019-01-17 15:19:35 -0800945 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700946 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
947 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
948 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
949 }
950
951 // restore to absolute z
952 // layerR = 4, layerG = 0, layerB = 2
953 Transaction().setLayer(layerG, mLayerZBase).apply();
954 {
955 SCOPED_TRACE("layerG < layerB < layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800956 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700957 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
958 shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
959 }
960
961 // layerR should not affect layerG anymore
962 // layerR = 1, layerG = 0, layerB = 2
963 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
964 {
965 SCOPED_TRACE("layerG < layerR < layerB");
Alec Mouri80863a62019-01-17 15:19:35 -0800966 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700967 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
968 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
969 }
970}
971
Alec Mouri80863a62019-01-17 15:19:35 -0800972TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -0700973 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
974}
975
Alec Mouri80863a62019-01-17 15:19:35 -0800976TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -0700977 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
978}
979
Alec Mouri80863a62019-01-17 15:19:35 -0800980TEST_P(LayerTypeAndRenderTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -0700981 sp<SurfaceControl> layerR;
982 sp<SurfaceControl> layerG;
983
984 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700985 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700986 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700987 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700988
989 Transaction()
990 .setPosition(layerG, 16, 16)
991 .setRelativeLayer(layerG, layerR->getHandle(), 1)
992 .apply();
993
Robert Carr87246532019-02-04 15:20:26 -0800994 layerG.clear();
Chia-I Wu49313302017-10-31 10:14:40 -0700995 // layerG should have been removed
Alec Mouri80863a62019-01-17 15:19:35 -0800996 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu49313302017-10-31 10:14:40 -0700997}
998
Alec Mouri80863a62019-01-17 15:19:35 -0800999TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001000 sp<SurfaceControl> layer;
1001 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001002 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001003
1004 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
1005 {
1006 SCOPED_TRACE("layer hidden");
Alec Mouri80863a62019-01-17 15:19:35 -08001007 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu57b27502017-10-31 10:14:40 -07001008 }
1009
1010 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1011 {
1012 SCOPED_TRACE("layer shown");
Alec Mouri80863a62019-01-17 15:19:35 -08001013 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu57b27502017-10-31 10:14:40 -07001014 }
1015}
1016
Alec Mouri80863a62019-01-17 15:19:35 -08001017TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001018 const Color translucentRed = {100, 0, 0, 100};
1019 sp<SurfaceControl> layerR;
1020 sp<SurfaceControl> layerG;
1021 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001022 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001023 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001024 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001025
1026 Transaction()
1027 .setLayer(layerR, mLayerZBase + 1)
1028 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1029 .apply();
1030 {
1031 SCOPED_TRACE("layerR opaque");
Alec Mouri80863a62019-01-17 15:19:35 -08001032 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
Chia-I Wu57b27502017-10-31 10:14:40 -07001033 }
1034
1035 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1036 {
1037 SCOPED_TRACE("layerR translucent");
1038 const uint8_t g = uint8_t(255 - translucentRed.a);
Alec Mouri80863a62019-01-17 15:19:35 -08001039 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
Chia-I Wu57b27502017-10-31 10:14:40 -07001040 }
1041}
1042
Marissa Wall61c58622018-07-18 10:12:20 -07001043TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001044 sp<SurfaceControl> layer;
1045 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001046 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001047
1048 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001049 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001050 Transaction()
1051 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1052 .apply(true);
1053 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001054 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001055
1056 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1057 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001058 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001059}
1060
Robert Carrfa8855f2019-02-19 10:05:00 -08001061TEST_F(LayerTransactionTest, SetFlagsSecureEUidSystem) {
1062 sp<SurfaceControl> layer;
1063 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1064 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
1065
1066 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
1067 sp<GraphicBuffer> outBuffer;
1068 Transaction()
1069 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1070 .apply(true);
1071 ASSERT_EQ(PERMISSION_DENIED,
1072 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1073
1074 UIDFaker f(AID_SYSTEM);
1075
1076 // By default the system can capture screenshots with secure layers but they
1077 // will be blacked out
1078 ASSERT_EQ(NO_ERROR,
1079 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1080
1081 {
1082 SCOPED_TRACE("as system");
1083 auto shot = screenshot();
1084 shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1085 }
1086
1087 // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
1088 // to receive them...we are expected to take care with the results.
Robert Carr108b2c72019-04-02 16:32:58 -07001089 bool outCapturedSecureLayers;
Robert Carrfa8855f2019-02-19 10:05:00 -08001090 ASSERT_EQ(NO_ERROR,
Robert Carr108b2c72019-04-02 16:32:58 -07001091 composer->captureScreen(mDisplay, &outBuffer, outCapturedSecureLayers,
1092 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(), 0,
1093 0, false, ISurfaceComposer::eRotateNone, true));
1094 ASSERT_EQ(true, outCapturedSecureLayers);
Robert Carrfa8855f2019-02-19 10:05:00 -08001095 ScreenCapture sc(outBuffer);
1096 sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
1097}
1098
Alec Mouri80863a62019-01-17 15:19:35 -08001099TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001100 const Rect top(0, 0, 32, 16);
1101 const Rect bottom(0, 16, 32, 32);
1102 sp<SurfaceControl> layer;
1103 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1104
1105 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001106 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
Valerie Haue271df92019-09-06 09:23:22 -07001107 ASSERT_NO_FATAL_FAILURE(
1108 TransactionUtils::fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1109 ASSERT_NO_FATAL_FAILURE(
1110 TransactionUtils::fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001111 // setTransparentRegionHint always applies to the following buffer
1112 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001113 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001114 {
1115 SCOPED_TRACE("top transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001116 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001117 shot->expectColor(top, Color::BLACK);
1118 shot->expectColor(bottom, Color::RED);
1119 }
1120
1121 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1122 {
1123 SCOPED_TRACE("transparent region hint pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001124 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001125 shot->expectColor(top, Color::BLACK);
1126 shot->expectColor(bottom, Color::RED);
1127 }
1128
Marissa Wall61c58622018-07-18 10:12:20 -07001129 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
Valerie Haue271df92019-09-06 09:23:22 -07001130 ASSERT_NO_FATAL_FAILURE(
1131 TransactionUtils::fillANativeWindowBufferColor(buffer, top, Color::RED));
1132 ASSERT_NO_FATAL_FAILURE(
1133 TransactionUtils::fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
Marissa Wall61c58622018-07-18 10:12:20 -07001134 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001135 {
1136 SCOPED_TRACE("bottom transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001137 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001138 shot->expectColor(top, Color::RED);
1139 shot->expectColor(bottom, Color::BLACK);
1140 }
1141}
1142
Alec Mouri80863a62019-01-17 15:19:35 -08001143TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001144 const Rect top(0, 0, 32, 16);
1145 const Rect bottom(0, 16, 32, 32);
1146 sp<SurfaceControl> layer;
1147 ASSERT_NO_FATAL_FAILURE(
1148 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1149
1150 sp<GraphicBuffer> buffer =
1151 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1152 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1153 BufferUsage::COMPOSER_OVERLAY,
1154 "test");
1155
Valerie Haue271df92019-09-06 09:23:22 -07001156 ASSERT_NO_FATAL_FAILURE(
1157 TransactionUtils::fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1158 ASSERT_NO_FATAL_FAILURE(TransactionUtils::fillGraphicBufferColor(buffer, bottom, Color::RED));
Marissa Wall61c58622018-07-18 10:12:20 -07001159 Transaction()
1160 .setTransparentRegionHint(layer, Region(top))
1161 .setBuffer(layer, buffer)
Marissa Wall861616d2018-10-22 12:52:23 -07001162 .setFrame(layer, Rect(0, 0, 32, 32))
Marissa Wall61c58622018-07-18 10:12:20 -07001163 .apply();
1164 {
1165 SCOPED_TRACE("top transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001166 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001167 shot->expectColor(top, Color::BLACK);
1168 shot->expectColor(bottom, Color::RED);
1169 }
1170
1171 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1172 {
1173 SCOPED_TRACE("transparent region hint intermediate");
Alec Mouri80863a62019-01-17 15:19:35 -08001174 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001175 shot->expectColor(top, Color::BLACK);
1176 shot->expectColor(bottom, Color::BLACK);
1177 }
1178
1179 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1180 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1181 BufferUsage::COMPOSER_OVERLAY,
1182 "test");
1183
Valerie Haue271df92019-09-06 09:23:22 -07001184 ASSERT_NO_FATAL_FAILURE(TransactionUtils::fillGraphicBufferColor(buffer, top, Color::RED));
1185 ASSERT_NO_FATAL_FAILURE(
1186 TransactionUtils::fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
Marissa Wall861616d2018-10-22 12:52:23 -07001187 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001188 {
1189 SCOPED_TRACE("bottom transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001190 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001191 shot->expectColor(top, Color::RED);
1192 shot->expectColor(bottom, Color::BLACK);
1193 }
1194}
1195
Alec Mouri80863a62019-01-17 15:19:35 -08001196TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001197 sp<SurfaceControl> layerTransparent;
1198 sp<SurfaceControl> layerR;
1199 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1200 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1201
1202 // check that transparent region hint is bound by the layer size
1203 Transaction()
Marissa Wall861616d2018-10-22 12:52:23 -07001204 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001205 .setPosition(layerR, 16, 16)
1206 .setLayer(layerR, mLayerZBase + 1)
1207 .apply();
Marissa Wall861616d2018-10-22 12:52:23 -07001208 ASSERT_NO_FATAL_FAILURE(
1209 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1210 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerR, Color::RED, 32, 32));
Alec Mouri80863a62019-01-17 15:19:35 -08001211 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001212}
1213
Alec Mouri80863a62019-01-17 15:19:35 -08001214TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001215 sp<SurfaceControl> layerTransparent;
1216 sp<SurfaceControl> layerR;
1217 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1218 ASSERT_NO_FATAL_FAILURE(
1219 layerR = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1220
1221 // check that transparent region hint is bound by the layer size
1222 Transaction()
1223 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
1224 .setFrame(layerR, Rect(16, 16, 48, 48))
1225 .setLayer(layerR, mLayerZBase + 1)
1226 .apply();
1227 ASSERT_NO_FATAL_FAILURE(
1228 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1229 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layerR, Color::RED, 32, 32));
Alec Mouri80863a62019-01-17 15:19:35 -08001230 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
Marissa Wall861616d2018-10-22 12:52:23 -07001231}
1232
Alec Mouri80863a62019-01-17 15:19:35 -08001233void LayerRenderTypeTransactionTest::setAlphaBasicHelper(uint32_t layerType) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001234 sp<SurfaceControl> layer1;
1235 sp<SurfaceControl> layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07001236 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32, layerType));
1237 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32, layerType));
1238 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer1, {64, 0, 0, 255}, 32, 32));
1239 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001240
Marissa Wall861616d2018-10-22 12:52:23 -07001241 switch (layerType) {
1242 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1243 Transaction()
1244 .setAlpha(layer1, 0.25f)
1245 .setAlpha(layer2, 0.75f)
1246 .setPosition(layer2, 16, 0)
1247 .setLayer(layer2, mLayerZBase + 1)
1248 .apply();
1249 break;
1250 case ISurfaceComposerClient::eFXSurfaceBufferState:
1251 Transaction()
1252 .setAlpha(layer1, 0.25f)
1253 .setAlpha(layer2, 0.75f)
1254 .setFrame(layer1, Rect(0, 0, 32, 32))
1255 .setFrame(layer2, Rect(16, 0, 48, 32))
1256 .setLayer(layer2, mLayerZBase + 1)
1257 .apply();
1258 break;
1259 default:
1260 ASSERT_FALSE(true) << "Unsupported layer type";
1261 }
Chia-I Wua8a515e2017-11-01 15:16:35 -07001262 {
Alec Mouri80863a62019-01-17 15:19:35 -08001263 auto shot = getScreenCapture();
Chia-I Wua8a515e2017-11-01 15:16:35 -07001264 uint8_t r = 16; // 64 * 0.25f
1265 uint8_t g = 48; // 64 * 0.75f
1266 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1267 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1268
1269 r /= 4; // r * (1.0f - 0.75f)
1270 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1271 }
1272}
1273
Alec Mouri80863a62019-01-17 15:19:35 -08001274TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -07001275 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1276}
1277
Alec Mouri80863a62019-01-17 15:19:35 -08001278TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001279 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1280}
1281
Alec Mouri80863a62019-01-17 15:19:35 -08001282TEST_P(LayerTypeAndRenderTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001283 const Color color = {64, 0, 0, 255};
1284 sp<SurfaceControl> layer;
1285 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001286 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001287
1288 Transaction().setAlpha(layer, 2.0f).apply();
1289 {
1290 SCOPED_TRACE("clamped to 1.0f");
Alec Mouri80863a62019-01-17 15:19:35 -08001291 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), color);
Chia-I Wua8a515e2017-11-01 15:16:35 -07001292 }
1293
1294 Transaction().setAlpha(layer, -1.0f).apply();
1295 {
1296 SCOPED_TRACE("clamped to 0.0f");
Alec Mouri80863a62019-01-17 15:19:35 -08001297 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wua8a515e2017-11-01 15:16:35 -07001298 }
1299}
1300
Alec Mouri80863a62019-01-17 15:19:35 -08001301TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadius) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001302 sp<SurfaceControl> layer;
1303 const uint8_t size = 64;
1304 const uint8_t testArea = 4;
Lucas Dupina1d0e312018-12-04 22:30:27 -08001305 const float cornerRadius = 20.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001306 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1307 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1308
1309 Transaction()
1310 .setCornerRadius(layer, cornerRadius)
Lucas Dupin0b79f8c2019-08-26 17:01:33 -07001311 .setCrop_legacy(layer, Rect(0, 0, size, size))
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001312 .apply();
1313 {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001314 const uint8_t bottom = size - 1;
1315 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001316 auto shot = getScreenCapture();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001317 // Transparent corners
1318 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
Lucas Dupina1d0e312018-12-04 22:30:27 -08001319 shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
1320 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1321 shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
1322 }
1323}
1324
Alec Mouri80863a62019-01-17 15:19:35 -08001325TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildCrop) {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001326 sp<SurfaceControl> parent;
1327 sp<SurfaceControl> child;
1328 const uint8_t size = 64;
1329 const uint8_t testArea = 4;
1330 const float cornerRadius = 20.0f;
1331 ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
1332 ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
1333 ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
1334 ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
1335
1336 Transaction()
1337 .setCornerRadius(parent, cornerRadius)
Lucas Dupin0b79f8c2019-08-26 17:01:33 -07001338 .setCrop_legacy(parent, Rect(0, 0, size, size))
Lucas Dupina1d0e312018-12-04 22:30:27 -08001339 .reparent(child, parent->getHandle())
1340 .setPosition(child, 0, size / 2)
1341 .apply();
1342 {
1343 const uint8_t bottom = size - 1;
1344 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001345 auto shot = getScreenCapture();
Lucas Dupina1d0e312018-12-04 22:30:27 -08001346 // Top edge of child should not have rounded corners because it's translated in the parent
1347 shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
1348 Color::GREEN);
1349 // But bottom edges should have been clipped according to parent bounds
1350 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1351 shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001352 }
1353}
1354
Alec Mouri80863a62019-01-17 15:19:35 -08001355TEST_P(LayerRenderTypeTransactionTest, SetColorBasic) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001356 sp<SurfaceControl> bufferLayer;
1357 sp<SurfaceControl> colorLayer;
1358 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001359 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001360 ASSERT_NO_FATAL_FAILURE(colorLayer =
1361 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1362 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001363
Vishnu Nair88a11f22018-11-28 18:30:57 -08001364 Transaction()
1365 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1366 .setLayer(colorLayer, mLayerZBase + 1)
1367 .apply();
1368
Chia-I Wue4ef6102017-11-01 15:16:35 -07001369 {
1370 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08001371 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001372 }
1373
1374 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1375 const Color expected = {15, 51, 85, 255};
1376 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1377 // channel) should be less than one
1378 const uint8_t tolerance = 1;
1379 Transaction().setColor(colorLayer, color).apply();
1380 {
1381 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08001382 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001383 }
1384}
1385
Valerie Haudd0b7572019-01-29 14:59:27 -08001386// RED: Color layer base color and BufferQueueLayer/BufferStateLayer fill
1387// BLUE: prior background color
1388// GREEN: final background color
1389// BLACK: no color or fill
1390void LayerRenderTypeTransactionTest::setBackgroundColorHelper(uint32_t layerType, bool priorColor,
1391 bool bufferFill, float alpha,
1392 Color finalColor) {
1393 sp<SurfaceControl> layer;
1394 int32_t width = 500;
1395 int32_t height = 500;
Valerie Haua72e2812019-01-23 13:40:39 -08001396
Valerie Haudd0b7572019-01-29 14:59:27 -08001397 Color fillColor = Color::RED;
1398 Color priorBgColor = Color::BLUE;
1399 Color expectedColor = Color::BLACK;
1400 switch (layerType) {
1401 case ISurfaceComposerClient::eFXSurfaceColor:
1402 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 0, 0, layerType));
1403 Transaction()
1404 .setCrop_legacy(layer, Rect(0, 0, width, height))
1405 .setColor(layer, half3(1.0f, 0, 0))
1406 .apply();
1407 expectedColor = fillColor;
1408 break;
1409 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1410 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
1411 if (bufferFill) {
1412 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, fillColor, width, height));
1413 expectedColor = fillColor;
1414 }
1415 Transaction().setCrop_legacy(layer, Rect(0, 0, width, height)).apply();
1416 break;
1417 case ISurfaceComposerClient::eFXSurfaceBufferState:
1418 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height, layerType));
1419 if (bufferFill) {
1420 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, fillColor, width, height));
1421 expectedColor = fillColor;
1422 }
1423 Transaction().setFrame(layer, Rect(0, 0, width, height)).apply();
1424 break;
1425 default:
1426 GTEST_FAIL() << "Unknown layer type in setBackgroundColorHelper";
1427 return;
Valerie Haua72e2812019-01-23 13:40:39 -08001428 }
1429
Valerie Haudd0b7572019-01-29 14:59:27 -08001430 if (priorColor && layerType != ISurfaceComposerClient::eFXSurfaceColor) {
1431 Transaction()
1432 .setBackgroundColor(layer, half3(0, 0, 1.0f), 1.0f, ui::Dataspace::UNKNOWN)
1433 .apply();
1434 if (!bufferFill) {
1435 expectedColor = priorBgColor;
1436 }
1437 }
1438
1439 {
1440 SCOPED_TRACE("default before setting background color layer");
1441 screenshot()->expectColor(Rect(0, 0, width, height), expectedColor);
1442 }
Valerie Haua72e2812019-01-23 13:40:39 -08001443 Transaction()
Valerie Haudd0b7572019-01-29 14:59:27 -08001444 .setBackgroundColor(layer, half3(0, 1.0f, 0), alpha, ui::Dataspace::UNKNOWN)
Valerie Haua72e2812019-01-23 13:40:39 -08001445 .apply();
1446
1447 {
Valerie Haua72e2812019-01-23 13:40:39 -08001448 auto shot = screenshot();
Valerie Haudd0b7572019-01-29 14:59:27 -08001449 shot->expectColor(Rect(0, 0, width, height), finalColor);
1450 shot->expectBorder(Rect(0, 0, width, height), Color::BLACK);
Valerie Haua72e2812019-01-23 13:40:39 -08001451 }
1452}
1453
Valerie Haudd0b7572019-01-29 14:59:27 -08001454TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_Color_NoEffect) {
1455 bool priorColor = false;
1456 bool bufferFill = false;
1457 float alpha = 1.0f;
1458 Color finalColor = Color::RED;
1459 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceColor,
1460 priorColor, bufferFill, alpha, finalColor));
1461}
Valerie Haua72e2812019-01-23 13:40:39 -08001462
Valerie Haudd0b7572019-01-29 14:59:27 -08001463TEST_P(LayerRenderTypeTransactionTest,
1464 SetBackgroundColor_BufferQueue_BufferFill_NoPriorColor_Basic) {
1465 bool priorColor = false;
1466 bool bufferFill = true;
1467 float alpha = 1.0f;
1468 Color finalColor = Color::RED;
1469 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1470 priorColor, bufferFill, alpha, finalColor));
1471}
Valerie Haua72e2812019-01-23 13:40:39 -08001472
Valerie Haudd0b7572019-01-29 14:59:27 -08001473TEST_P(LayerRenderTypeTransactionTest,
1474 SetBackgroundColor_BufferQueue_NoBufferFill_NoPriorColor_Basic) {
1475 bool priorColor = false;
1476 bool bufferFill = false;
1477 float alpha = 1.0f;
1478 Color finalColor = Color::GREEN;
1479 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1480 priorColor, bufferFill, alpha, finalColor));
1481}
Valerie Haua72e2812019-01-23 13:40:39 -08001482
Valerie Haudd0b7572019-01-29 14:59:27 -08001483TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_BufferQueue_BufferFill_PriorColor_Basic) {
1484 bool priorColor = true;
1485 bool bufferFill = true;
1486 float alpha = 1.0f;
1487 Color finalColor = Color::RED;
1488 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1489 priorColor, bufferFill, alpha, finalColor));
1490}
1491
1492TEST_P(LayerRenderTypeTransactionTest,
1493 SetBackgroundColor_BufferQueue_NoBufferFill_PriorColor_Basic) {
1494 bool priorColor = true;
1495 bool bufferFill = false;
1496 float alpha = 1.0f;
1497 Color finalColor = Color::GREEN;
1498 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1499 priorColor, bufferFill, alpha, finalColor));
1500}
1501TEST_P(LayerRenderTypeTransactionTest,
1502 SetBackgroundColor_BufferQueue_NoPriorColor_ZeroAlpha_NoEffect) {
1503 bool priorColor = false;
1504 bool bufferFill = false;
1505 float alpha = 0;
1506 Color finalColor = Color::BLACK;
1507 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1508 priorColor, bufferFill, alpha, finalColor));
1509}
1510
1511TEST_P(LayerRenderTypeTransactionTest,
1512 SetBackgroundColor_BufferQueue_PriorColor_ZeroAlpha_DeleteBackground) {
1513 bool priorColor = true;
1514 bool bufferFill = false;
1515 float alpha = 0;
1516 Color finalColor = Color::BLACK;
1517 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1518 priorColor, bufferFill, alpha, finalColor));
1519}
1520
1521TEST_P(LayerRenderTypeTransactionTest,
1522 SetBackgroundColor_BufferState_BufferFill_NoPriorColor_Basic) {
1523 bool priorColor = false;
1524 bool bufferFill = true;
1525 float alpha = 1.0f;
1526 Color finalColor = Color::RED;
Valerie Haua6b15a12019-02-05 14:16:30 -08001527 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001528 priorColor, bufferFill, alpha, finalColor));
1529}
1530
1531TEST_P(LayerRenderTypeTransactionTest,
1532 SetBackgroundColor_BufferState_NoBufferFill_NoPriorColor_Basic) {
1533 bool priorColor = false;
1534 bool bufferFill = false;
1535 float alpha = 1.0f;
1536 Color finalColor = Color::GREEN;
Valerie Haua6b15a12019-02-05 14:16:30 -08001537 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001538 priorColor, bufferFill, alpha, finalColor));
1539}
1540
1541TEST_P(LayerRenderTypeTransactionTest,
1542 SetBackgroundColor_BufferState_NoBufferFill_PriorColor_Basic) {
1543 bool priorColor = true;
1544 bool bufferFill = false;
1545 float alpha = 1.0f;
1546 Color finalColor = Color::GREEN;
Valerie Haua6b15a12019-02-05 14:16:30 -08001547 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001548 priorColor, bufferFill, alpha, finalColor));
1549}
1550
1551TEST_P(LayerRenderTypeTransactionTest,
1552 SetBackgroundColor_BufferState_NoPriorColor_ZeroAlpha_NoEffect) {
1553 bool priorColor = false;
1554 bool bufferFill = false;
1555 float alpha = 0;
1556 Color finalColor = Color::BLACK;
Valerie Haua6b15a12019-02-05 14:16:30 -08001557 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001558 priorColor, bufferFill, alpha, finalColor));
1559}
1560
1561TEST_P(LayerRenderTypeTransactionTest,
1562 SetBackgroundColor_BufferState_PriorColor_ZeroAlpha_DeleteBackground) {
1563 bool priorColor = true;
1564 bool bufferFill = false;
1565 float alpha = 0;
1566 Color finalColor = Color::BLACK;
Valerie Haua6b15a12019-02-05 14:16:30 -08001567 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001568 priorColor, bufferFill, alpha, finalColor));
Valerie Haua72e2812019-01-23 13:40:39 -08001569}
1570
Alec Mouri80863a62019-01-17 15:19:35 -08001571TEST_P(LayerRenderTypeTransactionTest, SetColorClamped) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001572 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001573 ASSERT_NO_FATAL_FAILURE(colorLayer =
1574 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1575 ISurfaceComposerClient::eFXSurfaceColor));
1576 Transaction()
1577 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1578 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1579 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001580
Alec Mouri80863a62019-01-17 15:19:35 -08001581 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001582}
1583
Alec Mouri80863a62019-01-17 15:19:35 -08001584TEST_P(LayerRenderTypeTransactionTest, SetColorWithAlpha) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001585 sp<SurfaceControl> bufferLayer;
1586 sp<SurfaceControl> colorLayer;
1587 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001588 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001589 ASSERT_NO_FATAL_FAILURE(colorLayer =
1590 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1591 ISurfaceComposerClient::eFXSurfaceColor));
1592 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001593
1594 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1595 const float alpha = 0.25f;
1596 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1597 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1598 // channel) should be less than one
1599 const uint8_t tolerance = 1;
1600 Transaction()
1601 .setColor(colorLayer, color)
1602 .setAlpha(colorLayer, alpha)
1603 .setLayer(colorLayer, mLayerZBase + 1)
1604 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001605 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1606 tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001607}
1608
Alec Mouri80863a62019-01-17 15:19:35 -08001609TEST_P(LayerRenderTypeTransactionTest, SetColorWithParentAlpha_Bug74220420) {
Adrian Roosb7a96502018-04-08 11:38:55 -07001610 sp<SurfaceControl> bufferLayer;
1611 sp<SurfaceControl> parentLayer;
1612 sp<SurfaceControl> colorLayer;
1613 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1614 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001615 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001616 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1617 0 /* buffer height */,
1618 ISurfaceComposerClient::eFXSurfaceColor));
1619 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001620 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1621 const float alpha = 0.25f;
1622 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1623 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1624 // channel) should be less than one
1625 const uint8_t tolerance = 1;
1626 Transaction()
1627 .reparent(colorLayer, parentLayer->getHandle())
1628 .setColor(colorLayer, color)
1629 .setAlpha(parentLayer, alpha)
1630 .setLayer(parentLayer, mLayerZBase + 1)
1631 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001632 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1633 tolerance);
Adrian Roosb7a96502018-04-08 11:38:55 -07001634}
1635
Alec Mouri80863a62019-01-17 15:19:35 -08001636TEST_P(LayerTypeAndRenderTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001637 sp<SurfaceControl> bufferLayer;
1638 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001639 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001640
1641 // color is ignored
1642 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001643 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001644}
1645
Alec Mouri80863a62019-01-17 15:19:35 -08001646TEST_P(LayerTypeAndRenderTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001647 sp<SurfaceControl> layer;
1648 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001649 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001650
1651 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1652 {
1653 SCOPED_TRACE("non-existing layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001654 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001655 }
1656
1657 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1658 {
1659 SCOPED_TRACE("original layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001660 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001661 }
1662}
1663
Alec Mouri80863a62019-01-17 15:19:35 -08001664TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001665 sp<SurfaceControl> layer;
1666 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001667 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1668 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001669
1670 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1671 {
1672 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001673 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1674 Color::BLUE, Color::WHITE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001675 }
1676
1677 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1678 {
1679 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001680 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED,
1681 Color::WHITE, Color::BLUE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001682 }
1683
1684 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1685 {
1686 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001687 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE,
1688 Color::RED, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001689 }
1690
1691 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1692 {
1693 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001694 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED,
1695 Color::WHITE, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001696 }
1697
1698 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1699 {
1700 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001701 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN,
1702 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001703 }
1704}
1705
Alec Mouri80863a62019-01-17 15:19:35 -08001706TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001707 sp<SurfaceControl> layer;
1708 ASSERT_NO_FATAL_FAILURE(
1709 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1710 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1711 Color::BLUE, Color::WHITE));
1712
1713 Transaction()
1714 .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
1715 .setFrame(layer, Rect(0, 0, 32, 32))
1716 .apply();
1717 {
1718 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001719 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1720 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001721 }
1722
1723 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
1724 {
1725 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001726 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1727 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001728 }
1729
1730 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
1731 {
1732 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001733 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1734 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001735 }
1736
1737 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
1738 {
1739 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001740 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1741 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001742 }
1743
1744 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
1745 {
1746 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001747 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1748 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001749 }
1750}
1751
Alec Mouri80863a62019-01-17 15:19:35 -08001752TEST_P(LayerRenderTypeTransactionTest, SetMatrixRot45_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001753 sp<SurfaceControl> layer;
1754 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001755 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1756 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001757
1758 const float rot = M_SQRT1_2; // 45 degrees
1759 const float trans = M_SQRT2 * 16.0f;
1760 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1761
Alec Mouri80863a62019-01-17 15:19:35 -08001762 auto shot = getScreenCapture();
Chia-I Wu93853fe2017-11-02 08:30:27 -07001763 // check a 8x8 region inside each color
1764 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1765 const int32_t halfL = 4;
1766 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1767 };
1768 const int32_t unit = int32_t(trans / 2);
1769 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1770 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1771 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1772 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1773}
1774
Alec Mouri80863a62019-01-17 15:19:35 -08001775TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithResize_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001776 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -07001777 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1778 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001779
1780 // setMatrix is applied after any pending resize, unlike setPosition
1781 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1782 {
1783 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001784 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001785 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -07001786 shot->expectColor(rect, Color::RED);
1787 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001788 }
1789
Marissa Wall861616d2018-10-22 12:52:23 -07001790 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001791 {
1792 SCOPED_TRACE("resize applied");
Marissa Wall861616d2018-10-22 12:52:23 -07001793 const Rect rect(0, 0, 128, 128);
Alec Mouri80863a62019-01-17 15:19:35 -08001794 getScreenCapture()->expectColor(rect, Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001795 }
1796}
1797
Alec Mouri80863a62019-01-17 15:19:35 -08001798TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001799 sp<SurfaceControl> layer;
1800 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001801 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001802
1803 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1804 Transaction()
1805 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1806 .setSize(layer, 64, 64)
1807 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1808 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001809 getScreenCapture()->expectColor(Rect(0, 0, 128, 128), Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001810}
1811
Alec Mouri80863a62019-01-17 15:19:35 -08001812TEST_P(LayerRenderTypeTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001813 sp<SurfaceControl> layer;
1814 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001815 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1816 Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001817
1818 // XXX SCALE_CROP is not respected; calling setSize and
1819 // setOverrideScalingMode in separate transactions does not work
1820 // (b/69315456)
1821 Transaction()
1822 .setSize(layer, 64, 16)
1823 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1824 .apply();
1825 {
1826 SCOPED_TRACE("SCALE_TO_WINDOW");
Alec Mouri80863a62019-01-17 15:19:35 -08001827 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN,
1828 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wua56b2042017-11-01 15:16:35 -07001829 }
1830}
1831
Dan Stoza000dd012018-08-01 13:31:52 -07001832TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1833 sp<SurfaceControl> layer;
1834 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1835
1836 sp<IBinder> handle = layer->getHandle();
1837 ASSERT_TRUE(handle != nullptr);
1838
1839 FrameStats frameStats;
1840 mClient->getLayerFrameStats(handle, &frameStats);
1841
1842 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1843}
1844
Alec Mouri80863a62019-01-17 15:19:35 -08001845TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001846 sp<SurfaceControl> layer;
1847 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001848 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001849 const Rect crop(8, 8, 24, 24);
1850
Marissa Wallf58c14b2018-07-24 10:50:43 -07001851 Transaction().setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001852 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001853 shot->expectColor(crop, Color::RED);
1854 shot->expectBorder(crop, Color::BLACK);
1855}
1856
Alec Mouri80863a62019-01-17 15:19:35 -08001857TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001858 sp<SurfaceControl> layer;
1859 ASSERT_NO_FATAL_FAILURE(
1860 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1861 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1862 const Rect crop(8, 8, 24, 24);
1863
1864 Transaction().setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001865 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001866 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
1867 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001868}
1869
Alec Mouri80863a62019-01-17 15:19:35 -08001870TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001871 sp<SurfaceControl> layer;
1872 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001873 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001874
1875 {
1876 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001877 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001878 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07001879 }
1880
1881 {
1882 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001883 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001884 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07001885 }
1886}
1887
Alec Mouri80863a62019-01-17 15:19:35 -08001888TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001889 sp<SurfaceControl> layer;
1890 ASSERT_NO_FATAL_FAILURE(
1891 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1892 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1893
1894 {
1895 SCOPED_TRACE("empty rect");
1896 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
Marissa Wall290ad082019-03-06 13:23:47 -08001897 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07001898 }
1899
1900 {
1901 SCOPED_TRACE("negative rect");
1902 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
Marissa Wall290ad082019-03-06 13:23:47 -08001903 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07001904 }
1905}
1906
Alec Mouri80863a62019-01-17 15:19:35 -08001907TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_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
Marissa Wallf58c14b2018-07-24 10:50:43 -07001912 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001913 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001914 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1915 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1916}
1917
Alec Mouri80863a62019-01-17 15:19:35 -08001918TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferState) {
Valerie Hau0bc09152018-12-20 07:42:47 -08001919 sp<SurfaceControl> layer;
Marissa Wall290ad082019-03-06 13:23:47 -08001920 ASSERT_NO_FATAL_FAILURE(
1921 layer = createLayer("test", 32, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
Valerie Hau0bc09152018-12-20 07:42:47 -08001922 sp<GraphicBuffer> buffer =
Marissa Wall290ad082019-03-06 13:23:47 -08001923 new GraphicBuffer(32, 64, PIXEL_FORMAT_RGBA_8888, 1,
Valerie Hau0bc09152018-12-20 07:42:47 -08001924 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1925 BufferUsage::COMPOSER_OVERLAY,
1926 "test");
Valerie Haue271df92019-09-06 09:23:22 -07001927 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 16), Color::BLUE);
1928 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 16, 32, 64), Color::RED);
Marissa Wall290ad082019-03-06 13:23:47 -08001929
1930 Transaction().setFrame(layer, Rect(0, 0, 64, 64)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001931
1932 Transaction().setBuffer(layer, buffer).apply();
1933
1934 // Partially out of bounds in the negative (upper left) direction
Marissa Wall290ad082019-03-06 13:23:47 -08001935 Transaction().setCrop(layer, Rect(-128, -128, 32, 16)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001936 {
1937 SCOPED_TRACE("out of bounds, negative (upper left) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08001938 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001939 shot->expectColor(Rect(0, 0, 64, 64), Color::BLUE);
1940 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001941 }
1942
1943 // Partially out of bounds in the positive (lower right) direction
Marissa Wall290ad082019-03-06 13:23:47 -08001944 Transaction().setCrop(layer, Rect(0, 16, 128, 128)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001945 {
1946 SCOPED_TRACE("out of bounds, positive (lower right) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08001947 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001948 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1949 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001950 }
1951
1952 // Fully out of buffer space bounds
1953 Transaction().setCrop(layer, Rect(-128, -128, -1, -1)).apply();
1954 {
1955 SCOPED_TRACE("Fully out of bounds");
Alec Mouri80863a62019-01-17 15:19:35 -08001956 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001957 shot->expectColor(Rect(0, 0, 64, 16), Color::BLUE);
1958 shot->expectColor(Rect(0, 16, 64, 64), Color::RED);
1959 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001960 }
1961}
1962
Alec Mouri80863a62019-01-17 15:19:35 -08001963TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001964 sp<SurfaceControl> layer;
1965 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001966 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001967
1968 const Point position(32, 32);
1969 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001970 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001971 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001972 shot->expectColor(crop + position, Color::RED);
1973 shot->expectBorder(crop + position, Color::BLACK);
1974}
1975
Alec Mouri80863a62019-01-17 15:19:35 -08001976TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001977 sp<SurfaceControl> layer;
1978 ASSERT_NO_FATAL_FAILURE(
1979 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1980 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1981
Marissa Wall861616d2018-10-22 12:52:23 -07001982 const Rect frame(32, 32, 64, 64);
Marissa Wall61c58622018-07-18 10:12:20 -07001983 const Rect crop(8, 8, 24, 24);
Marissa Wall861616d2018-10-22 12:52:23 -07001984 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001985 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001986 shot->expectColor(frame, Color::RED);
1987 shot->expectBorder(frame, Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001988}
1989
Alec Mouri80863a62019-01-17 15:19:35 -08001990TEST_P(LayerRenderTypeTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001991 sp<SurfaceControl> layer;
1992 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001993 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001994
Marissa Wall861616d2018-10-22 12:52:23 -07001995 // crop_legacy is affected by matrix
Chia-I Wu04dcca82017-11-02 08:30:27 -07001996 Transaction()
1997 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001998 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001999 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002000 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002001 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2002 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2003}
2004
Alec Mouri80863a62019-01-17 15:19:35 -08002005TEST_P(LayerRenderTypeTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002006 sp<SurfaceControl> layer;
2007 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002008 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002009
Marissa Wallf58c14b2018-07-24 10:50:43 -07002010 // setCrop_legacy is applied immediately by default, with or without resize pending
2011 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002012 {
2013 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002014 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002015 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
2016 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
2017 }
2018
Marissa Wall61c58622018-07-18 10:12:20 -07002019 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002020 {
2021 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002022 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002023 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
2024 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
2025 }
2026}
2027
Alec Mouri80863a62019-01-17 15:19:35 -08002028TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002029 sp<SurfaceControl> layer;
2030 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002031 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002032
Marissa Wallf58c14b2018-07-24 10:50:43 -07002033 // request setCrop_legacy to be applied with the next resize
2034 Transaction()
2035 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
2036 .setGeometryAppliesWithResize(layer)
2037 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002038 {
2039 SCOPED_TRACE("waiting for next resize");
Alec Mouri80863a62019-01-17 15:19:35 -08002040 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002041 }
2042
Marissa Wallf58c14b2018-07-24 10:50:43 -07002043 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002044 {
2045 SCOPED_TRACE("pending crop modified");
Alec Mouri80863a62019-01-17 15:19:35 -08002046 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002047 }
2048
2049 Transaction().setSize(layer, 16, 16).apply();
2050 {
2051 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002052 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002053 }
2054
2055 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07002056 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002057 {
2058 SCOPED_TRACE("new crop applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002059 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002060 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2061 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2062 }
2063}
2064
Alec Mouri80863a62019-01-17 15:19:35 -08002065TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002066 sp<SurfaceControl> layer;
2067 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002068 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002069
Marissa Wallf58c14b2018-07-24 10:50:43 -07002070 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07002071 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07002072 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07002073 .setSize(layer, 16, 16)
2074 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
2075 .setGeometryAppliesWithResize(layer)
2076 .apply();
2077 {
2078 SCOPED_TRACE("new crop pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002079 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002080 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
2081 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
2082 }
2083
2084 // XXX crop is never latched without other geometry change (b/69315677)
2085 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002086 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002087 Transaction().setPosition(layer, 0, 0).apply();
2088 {
2089 SCOPED_TRACE("new crop applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002090 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002091 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2092 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2093 }
2094}
2095
Alec Mouri80863a62019-01-17 15:19:35 -08002096TEST_P(LayerRenderTypeTransactionTest, SetFrameBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002097 sp<SurfaceControl> layer;
2098 ASSERT_NO_FATAL_FAILURE(
2099 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2100 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2101 const Rect frame(8, 8, 24, 24);
2102
2103 Transaction().setFrame(layer, frame).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002104 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002105 shot->expectColor(frame, Color::RED);
2106 shot->expectBorder(frame, Color::BLACK);
2107}
2108
Alec Mouri80863a62019-01-17 15:19:35 -08002109TEST_P(LayerRenderTypeTransactionTest, SetFrameEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002110 sp<SurfaceControl> layer;
2111 ASSERT_NO_FATAL_FAILURE(
2112 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2113 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2114
Marissa Wall61c58622018-07-18 10:12:20 -07002115 {
Marissa Wall861616d2018-10-22 12:52:23 -07002116 SCOPED_TRACE("empty rect");
2117 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002118 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002119 }
2120
Marissa Wall61c58622018-07-18 10:12:20 -07002121 {
Marissa Wall861616d2018-10-22 12:52:23 -07002122 SCOPED_TRACE("negative rect");
2123 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002124 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002125 }
2126}
2127
Alec Mouri80863a62019-01-17 15:19:35 -08002128TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultParentless_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002129 sp<SurfaceControl> layer;
2130 ASSERT_NO_FATAL_FAILURE(
2131 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2132 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
2133
2134 // A parentless layer will default to a frame with the same size as the buffer
Alec Mouri80863a62019-01-17 15:19:35 -08002135 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002136 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2137 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall861616d2018-10-22 12:52:23 -07002138}
2139
Alec Mouri80863a62019-01-17 15:19:35 -08002140TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBSParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002141 sp<SurfaceControl> parent, child;
2142 ASSERT_NO_FATAL_FAILURE(
2143 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2144 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2145 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2146
2147 ASSERT_NO_FATAL_FAILURE(
2148 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2149 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2150
2151 Transaction().reparent(child, parent->getHandle()).apply();
2152
2153 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002154 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002155 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2156 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2157}
2158
Alec Mouri80863a62019-01-17 15:19:35 -08002159TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBQParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002160 sp<SurfaceControl> parent, child;
2161 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
2162 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
2163
2164 ASSERT_NO_FATAL_FAILURE(
2165 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2166 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2167
2168 Transaction().reparent(child, parent->getHandle()).apply();
2169
2170 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002171 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002172 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2173 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2174}
2175
Alec Mouri80863a62019-01-17 15:19:35 -08002176TEST_P(LayerRenderTypeTransactionTest, SetFrameUpdate_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002177 sp<SurfaceControl> layer;
2178 ASSERT_NO_FATAL_FAILURE(
2179 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2180 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2181 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
2182
2183 std::this_thread::sleep_for(500ms);
2184
2185 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
2186
Alec Mouri80863a62019-01-17 15:19:35 -08002187 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002188 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2189 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2190}
2191
Alec Mouri80863a62019-01-17 15:19:35 -08002192TEST_P(LayerRenderTypeTransactionTest, SetFrameOutsideBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002193 sp<SurfaceControl> parent, child;
2194 ASSERT_NO_FATAL_FAILURE(
2195 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2196 ASSERT_NO_FATAL_FAILURE(
2197 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2198 Transaction().reparent(child, parent->getHandle()).apply();
2199
2200 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2201 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2202
2203 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2204 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
2205
Alec Mouri80863a62019-01-17 15:19:35 -08002206 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002207 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
2208 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
2209 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2210}
2211
Alec Mouri80863a62019-01-17 15:19:35 -08002212TEST_P(LayerRenderTypeTransactionTest, SetBufferBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002213 sp<SurfaceControl> layer;
2214 ASSERT_NO_FATAL_FAILURE(
2215 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2216
2217 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2218
Alec Mouri80863a62019-01-17 15:19:35 -08002219 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002220 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2221 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002222}
2223
Alec Mouri80863a62019-01-17 15:19:35 -08002224TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleBuffers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002225 sp<SurfaceControl> layer;
2226 ASSERT_NO_FATAL_FAILURE(
2227 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2228
2229 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2230
2231 {
2232 SCOPED_TRACE("set buffer 1");
Alec Mouri80863a62019-01-17 15:19:35 -08002233 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002234 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2235 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002236 }
2237
2238 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
2239
2240 {
2241 SCOPED_TRACE("set buffer 2");
Alec Mouri80863a62019-01-17 15:19:35 -08002242 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002243 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLUE);
2244 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002245 }
2246
2247 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2248
2249 {
2250 SCOPED_TRACE("set buffer 3");
Alec Mouri80863a62019-01-17 15:19:35 -08002251 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002252 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2253 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002254 }
2255}
2256
Alec Mouri80863a62019-01-17 15:19:35 -08002257TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleLayers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002258 sp<SurfaceControl> layer1;
2259 ASSERT_NO_FATAL_FAILURE(
2260 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2261
2262 sp<SurfaceControl> layer2;
2263 ASSERT_NO_FATAL_FAILURE(
2264 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2265
2266 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2267
Marissa Wall861616d2018-10-22 12:52:23 -07002268 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002269 {
2270 SCOPED_TRACE("set layer 1 buffer red");
Alec Mouri80863a62019-01-17 15:19:35 -08002271 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002272 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2273 }
2274
2275 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2276
Marissa Wall861616d2018-10-22 12:52:23 -07002277 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002278 {
2279 SCOPED_TRACE("set layer 2 buffer blue");
Alec Mouri80863a62019-01-17 15:19:35 -08002280 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002281 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2282 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2283 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2284 }
2285
2286 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2287 {
2288 SCOPED_TRACE("set layer 1 buffer green");
Alec Mouri80863a62019-01-17 15:19:35 -08002289 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002290 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2291 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2292 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2293 }
2294
2295 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2296
2297 {
2298 SCOPED_TRACE("set layer 2 buffer white");
Alec Mouri80863a62019-01-17 15:19:35 -08002299 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002300 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2301 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2302 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2303 }
2304}
2305
Valerie Haua6b15a12019-02-05 14:16:30 -08002306TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002307 sp<SurfaceControl> layer;
2308 ASSERT_NO_FATAL_FAILURE(
2309 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2310
2311 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2312
2313 std::array<sp<GraphicBuffer>, 10> buffers;
2314
2315 size_t idx = 0;
2316 for (auto& buffer : buffers) {
2317 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2318 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2319 BufferUsage::COMPOSER_OVERLAY,
2320 "test");
2321 Color color = colors[idx % colors.size()];
Valerie Haue271df92019-09-06 09:23:22 -07002322 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
Marissa Wall73411622019-01-25 10:45:41 -08002323 idx++;
2324 }
2325
2326 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2327 // cache is working.
2328 idx = 0;
2329 for (auto& buffer : buffers) {
2330 for (int i = 0; i < 2; i++) {
2331 Transaction().setBuffer(layer, buffer).apply();
2332
2333 Color color = colors[idx % colors.size()];
2334 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002335 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2336 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002337 }
2338 idx++;
2339 }
2340}
2341
Valerie Haua6b15a12019-02-05 14:16:30 -08002342TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_LeastRecentlyUsed_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002343 sp<SurfaceControl> layer;
2344 ASSERT_NO_FATAL_FAILURE(
2345 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2346
2347 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2348
2349 std::array<sp<GraphicBuffer>, 70> buffers;
2350
2351 size_t idx = 0;
2352 for (auto& buffer : buffers) {
2353 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2354 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2355 BufferUsage::COMPOSER_OVERLAY,
2356 "test");
2357 Color color = colors[idx % colors.size()];
Valerie Haue271df92019-09-06 09:23:22 -07002358 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
Marissa Wall73411622019-01-25 10:45:41 -08002359 idx++;
2360 }
2361
2362 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2363 // cache is working.
2364 idx = 0;
2365 for (auto& buffer : buffers) {
2366 for (int i = 0; i < 2; i++) {
2367 Transaction().setBuffer(layer, buffer).apply();
2368
2369 Color color = colors[idx % colors.size()];
2370 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002371 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2372 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002373 }
2374 idx++;
2375 }
2376}
2377
Valerie Haua6b15a12019-02-05 14:16:30 -08002378TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_DestroyedBuffer_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002379 sp<SurfaceControl> layer;
2380 ASSERT_NO_FATAL_FAILURE(
2381 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2382
2383 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2384
2385 std::array<sp<GraphicBuffer>, 65> buffers;
2386
2387 size_t idx = 0;
2388 for (auto& buffer : buffers) {
2389 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2390 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2391 BufferUsage::COMPOSER_OVERLAY,
2392 "test");
2393 Color color = colors[idx % colors.size()];
Valerie Haue271df92019-09-06 09:23:22 -07002394 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
Marissa Wall73411622019-01-25 10:45:41 -08002395 idx++;
2396 }
2397
2398 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2399 // cache is working.
2400 idx = 0;
2401 for (auto& buffer : buffers) {
2402 for (int i = 0; i < 2; i++) {
2403 Transaction().setBuffer(layer, buffer).apply();
2404
2405 Color color = colors[idx % colors.size()];
2406 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002407 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2408 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002409 }
2410 if (idx == 0) {
2411 buffers[0].clear();
2412 }
2413 idx++;
2414 }
2415}
2416
Alec Mouri80863a62019-01-17 15:19:35 -08002417TEST_P(LayerRenderTypeTransactionTest, SetTransformRotate90_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002418 sp<SurfaceControl> layer;
2419 ASSERT_NO_FATAL_FAILURE(
2420 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2421
2422 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2423 Color::BLUE, Color::WHITE));
2424
Marissa Wall861616d2018-10-22 12:52:23 -07002425 Transaction()
2426 .setFrame(layer, Rect(0, 0, 32, 32))
2427 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2428 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002429
Alec Mouri80863a62019-01-17 15:19:35 -08002430 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2431 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002432}
2433
Alec Mouri80863a62019-01-17 15:19:35 -08002434TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipH_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002435 sp<SurfaceControl> layer;
2436 ASSERT_NO_FATAL_FAILURE(
2437 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2438
2439 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2440 Color::BLUE, Color::WHITE));
2441
Marissa Wall861616d2018-10-22 12:52:23 -07002442 Transaction()
2443 .setFrame(layer, Rect(0, 0, 32, 32))
2444 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2445 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002446
Alec Mouri80863a62019-01-17 15:19:35 -08002447 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2448 Color::BLUE, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002449}
2450
Alec Mouri80863a62019-01-17 15:19:35 -08002451TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipV_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002452 sp<SurfaceControl> layer;
2453 ASSERT_NO_FATAL_FAILURE(
2454 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2455
2456 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2457 Color::BLUE, Color::WHITE));
2458
Marissa Wall861616d2018-10-22 12:52:23 -07002459 Transaction()
2460 .setFrame(layer, Rect(0, 0, 32, 32))
2461 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2462 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002463
Alec Mouri80863a62019-01-17 15:19:35 -08002464 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2465 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002466}
2467
2468TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2469 sp<SurfaceControl> layer;
2470 ASSERT_NO_FATAL_FAILURE(
2471 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2472
2473 Transaction().setTransformToDisplayInverse(layer, false).apply();
2474
2475 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2476
2477 Transaction().setTransformToDisplayInverse(layer, true).apply();
2478}
2479
Alec Mouri80863a62019-01-17 15:19:35 -08002480TEST_P(LayerRenderTypeTransactionTest, SetFenceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002481 sp<SurfaceControl> layer;
Marissa Wall713b63f2018-10-17 15:42:43 -07002482 Transaction transaction;
2483 ASSERT_NO_FATAL_FAILURE(
2484 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2485
2486 sp<GraphicBuffer> buffer =
2487 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2488 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2489 BufferUsage::COMPOSER_OVERLAY,
2490 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002491 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall713b63f2018-10-17 15:42:43 -07002492
2493 sp<Fence> fence;
2494 if (getBuffer(nullptr, &fence) != NO_ERROR) {
2495 GTEST_SUCCEED() << "test not supported";
2496 return;
2497 }
2498
2499 Transaction().setBuffer(layer, buffer).setAcquireFence(layer, fence).apply();
2500
2501 status_t status = fence->wait(1000);
2502 ASSERT_NE(static_cast<status_t>(Fence::Status::Unsignaled), status);
2503 std::this_thread::sleep_for(200ms);
2504
Alec Mouri80863a62019-01-17 15:19:35 -08002505 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002506 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2507 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall713b63f2018-10-17 15:42:43 -07002508}
2509
Alec Mouri80863a62019-01-17 15:19:35 -08002510TEST_P(LayerRenderTypeTransactionTest, SetFenceNull_BufferState) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002511 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07002512 ASSERT_NO_FATAL_FAILURE(
2513 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2514
2515 sp<GraphicBuffer> buffer =
2516 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2517 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2518 BufferUsage::COMPOSER_OVERLAY,
2519 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002520 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002521
Marissa Wallfda30bb2018-10-12 11:34:28 -07002522 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002523
2524 Transaction()
2525 .setBuffer(layer, buffer)
2526 .setAcquireFence(layer, fence)
Marissa Wall61c58622018-07-18 10:12:20 -07002527 .apply();
2528
Alec Mouri80863a62019-01-17 15:19:35 -08002529 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002530 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2531 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002532}
2533
Alec Mouri80863a62019-01-17 15:19:35 -08002534TEST_P(LayerRenderTypeTransactionTest, SetDataspaceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002535 sp<SurfaceControl> layer;
2536 ASSERT_NO_FATAL_FAILURE(
2537 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2538
2539 sp<GraphicBuffer> buffer =
2540 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2541 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2542 BufferUsage::COMPOSER_OVERLAY,
2543 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002544 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002545
2546 Transaction()
2547 .setBuffer(layer, buffer)
2548 .setDataspace(layer, ui::Dataspace::UNKNOWN)
Marissa Wall61c58622018-07-18 10:12:20 -07002549 .apply();
2550
Alec Mouri80863a62019-01-17 15:19:35 -08002551 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002552 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2553 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002554}
2555
Alec Mouri80863a62019-01-17 15:19:35 -08002556TEST_P(LayerRenderTypeTransactionTest, SetHdrMetadataBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002557 sp<SurfaceControl> layer;
2558 ASSERT_NO_FATAL_FAILURE(
2559 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2560
2561 sp<GraphicBuffer> buffer =
2562 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2563 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2564 BufferUsage::COMPOSER_OVERLAY,
2565 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002566 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002567
2568 HdrMetadata hdrMetadata;
2569 hdrMetadata.validTypes = 0;
2570 Transaction()
2571 .setBuffer(layer, buffer)
2572 .setHdrMetadata(layer, hdrMetadata)
Marissa Wall61c58622018-07-18 10:12:20 -07002573 .apply();
2574
Alec Mouri80863a62019-01-17 15:19:35 -08002575 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002576 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2577 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002578}
2579
Alec Mouri80863a62019-01-17 15:19:35 -08002580TEST_P(LayerRenderTypeTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002581 sp<SurfaceControl> layer;
2582 ASSERT_NO_FATAL_FAILURE(
2583 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2584
2585 sp<GraphicBuffer> buffer =
2586 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2587 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2588 BufferUsage::COMPOSER_OVERLAY,
2589 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002590 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002591
2592 Region region;
2593 region.set(32, 32);
2594 Transaction()
2595 .setBuffer(layer, buffer)
2596 .setSurfaceDamageRegion(layer, region)
Marissa Wall61c58622018-07-18 10:12:20 -07002597 .apply();
2598
Alec Mouri80863a62019-01-17 15:19:35 -08002599 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002600 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2601 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002602}
2603
Alec Mouri80863a62019-01-17 15:19:35 -08002604TEST_P(LayerRenderTypeTransactionTest, SetApiBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002605 sp<SurfaceControl> layer;
2606 ASSERT_NO_FATAL_FAILURE(
2607 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2608
2609 sp<GraphicBuffer> buffer =
2610 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2611 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2612 BufferUsage::COMPOSER_OVERLAY,
2613 "test");
Valerie Haue271df92019-09-06 09:23:22 -07002614 TransactionUtils::fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07002615
2616 Transaction()
2617 .setBuffer(layer, buffer)
2618 .setApi(layer, NATIVE_WINDOW_API_CPU)
Marissa Wall61c58622018-07-18 10:12:20 -07002619 .apply();
2620
Alec Mouri80863a62019-01-17 15:19:35 -08002621 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002622 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2623 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002624}
2625
2626TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2627 sp<SurfaceControl> layer;
2628 ASSERT_NO_FATAL_FAILURE(
2629 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2630
2631 // verify this doesn't cause a crash
2632 Transaction().setSidebandStream(layer, nullptr).apply();
2633}
2634
Robert Carr54cf5b12019-01-25 14:02:28 -08002635TEST_F(LayerTransactionTest, ReparentToSelf) {
2636 sp<SurfaceControl> layer;
2637 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2638 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2639 Transaction().reparent(layer, layer->getHandle()).apply();
2640
2641 {
2642 // We expect the transaction to be silently dropped, but for SurfaceFlinger
2643 // to still be functioning.
2644 SCOPED_TRACE("after reparent to self");
2645 const Rect rect(0, 0, 32, 32);
2646 auto shot = screenshot();
2647 shot->expectColor(rect, Color::RED);
2648 shot->expectBorder(rect, Color::BLACK);
2649 }
2650}
2651
Alec Mouri80863a62019-01-17 15:19:35 -08002652TEST_P(LayerRenderTypeTransactionTest, SetColorTransformBasic) {
Peiyong Lind3788632018-09-18 16:01:31 -07002653 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002654 ASSERT_NO_FATAL_FAILURE(colorLayer =
2655 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2656 ISurfaceComposerClient::eFXSurfaceColor));
2657 Transaction()
2658 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2659 .setLayer(colorLayer, mLayerZBase + 1)
2660 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002661 {
2662 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002663 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Peiyong Lind3788632018-09-18 16:01:31 -07002664 }
2665
2666 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002667 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002668 mat3 matrix;
2669 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2670 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2671 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002672
2673 // degamma before applying the matrix
2674 if (mColorManagementUsed) {
2675 ColorTransformHelper::DegammaColor(expected);
2676 }
2677
2678 ColorTransformHelper::applyMatrix(expected, matrix);
2679
2680 if (mColorManagementUsed) {
2681 ColorTransformHelper::GammaColor(expected);
2682 }
2683
2684 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2685 uint8_t(expected.b * 255), 255};
2686
2687 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2688 // channel) should be less than one
2689 const uint8_t tolerance = 1;
2690
Peiyong Lind3788632018-09-18 16:01:31 -07002691 Transaction().setColor(colorLayer, color)
2692 .setColorTransform(colorLayer, matrix, vec3()).apply();
2693 {
2694 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002695 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002696 }
2697}
2698
Alec Mouri80863a62019-01-17 15:19:35 -08002699TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnParent) {
chaviwf66724d2018-11-28 16:35:21 -08002700 sp<SurfaceControl> parentLayer;
2701 sp<SurfaceControl> colorLayer;
2702 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2703 0 /* buffer height */,
2704 ISurfaceComposerClient::eFXSurfaceContainer));
2705 ASSERT_NO_FATAL_FAILURE(
2706 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2707 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2708
2709 Transaction()
2710 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2711 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2712 .setLayer(parentLayer, mLayerZBase + 1)
2713 .apply();
2714 {
2715 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002716 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08002717 }
2718
2719 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2720 half3 expected = color;
2721 mat3 matrix;
2722 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2723 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2724 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2725
2726 // degamma before applying the matrix
2727 if (mColorManagementUsed) {
2728 ColorTransformHelper::DegammaColor(expected);
2729 }
2730
2731 ColorTransformHelper::applyMatrix(expected, matrix);
2732
2733 if (mColorManagementUsed) {
2734 ColorTransformHelper::GammaColor(expected);
2735 }
2736
2737 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2738 uint8_t(expected.b * 255), 255};
2739
2740 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2741 // channel) should be less than one
2742 const uint8_t tolerance = 1;
2743
2744 Transaction()
2745 .setColor(colorLayer, color)
2746 .setColorTransform(parentLayer, matrix, vec3())
2747 .apply();
2748 {
2749 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002750 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08002751 }
2752}
2753
Alec Mouri80863a62019-01-17 15:19:35 -08002754TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnChildAndParent) {
chaviwf66724d2018-11-28 16:35:21 -08002755 sp<SurfaceControl> parentLayer;
2756 sp<SurfaceControl> colorLayer;
2757 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2758 0 /* buffer height */,
2759 ISurfaceComposerClient::eFXSurfaceContainer));
2760 ASSERT_NO_FATAL_FAILURE(
2761 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2762 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2763
2764 Transaction()
2765 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2766 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2767 .setLayer(parentLayer, mLayerZBase + 1)
2768 .apply();
2769 {
2770 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002771 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08002772 }
2773
2774 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2775 half3 expected = color;
2776 mat3 matrixChild;
2777 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
2778 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
2779 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
2780 mat3 matrixParent;
2781 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
2782 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
2783 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
2784
2785 // degamma before applying the matrix
2786 if (mColorManagementUsed) {
2787 ColorTransformHelper::DegammaColor(expected);
2788 }
2789
2790 ColorTransformHelper::applyMatrix(expected, matrixChild);
2791 ColorTransformHelper::applyMatrix(expected, matrixParent);
2792
2793 if (mColorManagementUsed) {
2794 ColorTransformHelper::GammaColor(expected);
2795 }
2796
2797 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2798 uint8_t(expected.b * 255), 255};
2799
2800 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2801 // channel) should be less than one
2802 const uint8_t tolerance = 1;
2803
2804 Transaction()
2805 .setColor(colorLayer, color)
2806 .setColorTransform(parentLayer, matrixParent, vec3())
2807 .setColorTransform(colorLayer, matrixChild, vec3())
2808 .apply();
2809 {
2810 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002811 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08002812 }
2813}
2814
Marissa Wallfda30bb2018-10-12 11:34:28 -07002815class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07002816public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07002817 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07002818 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002819 }
2820
Marissa Wall713b63f2018-10-17 15:42:43 -07002821 static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
Valerie Hauaa194562019-02-05 16:21:38 -08002822 const sp<SurfaceControl>& layer = nullptr, bool setBuffer = true,
2823 bool setBackgroundColor = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002824 if (layer) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002825 sp<GraphicBuffer> buffer;
2826 sp<Fence> fence;
Valerie Hauaa194562019-02-05 16:21:38 -08002827 if (setBuffer) {
2828 int err = getBuffer(&buffer, &fence);
2829 if (err != NO_ERROR) {
2830 return err;
2831 }
2832
2833 transaction.setBuffer(layer, buffer);
2834 transaction.setAcquireFence(layer, fence);
Marissa Wall713b63f2018-10-17 15:42:43 -07002835 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002836
Valerie Hauaa194562019-02-05 16:21:38 -08002837 if (setBackgroundColor) {
2838 transaction.setBackgroundColor(layer, /*color*/ half3(1.0f, 0, 0), /*alpha*/ 1.0f,
2839 ui::Dataspace::UNKNOWN);
2840 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002841 }
2842
2843 transaction.addTransactionCompletedCallback(callbackHelper->function,
2844 callbackHelper->getContext());
Marissa Wall713b63f2018-10-17 15:42:43 -07002845 return NO_ERROR;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002846 }
2847
Marissa Wall861616d2018-10-22 12:52:23 -07002848 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2849 bool finalState = false) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08002850 CallbackData callbackData;
2851 ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
2852 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
Marissa Wallfda30bb2018-10-12 11:34:28 -07002853
2854 if (finalState) {
2855 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2856 }
2857 }
2858
Marissa Wall861616d2018-10-22 12:52:23 -07002859 static void waitForCallbacks(CallbackHelper& helper,
2860 const std::vector<ExpectedResult>& expectedResults,
2861 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002862 for (const auto& expectedResult : expectedResults) {
2863 waitForCallback(helper, expectedResult);
2864 }
2865 if (finalState) {
2866 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2867 }
2868 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002869};
2870
Valerie Hauaa194562019-02-05 16:21:38 -08002871TEST_F(LayerCallbackTest, BufferColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002872 sp<SurfaceControl> layer;
2873 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2874
2875 Transaction transaction;
2876 CallbackHelper callback;
Valerie Hauaa194562019-02-05 16:21:38 -08002877 int err = fillTransaction(transaction, &callback, layer, true, true);
Marissa Wall713b63f2018-10-17 15:42:43 -07002878 if (err) {
2879 GTEST_SUCCEED() << "test not supported";
2880 return;
2881 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002882
2883 transaction.apply();
2884
2885 ExpectedResult expected;
2886 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2887 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2888}
2889
Valerie Hauaa194562019-02-05 16:21:38 -08002890TEST_F(LayerCallbackTest, NoBufferNoColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002891 sp<SurfaceControl> layer;
2892 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2893
2894 Transaction transaction;
2895 CallbackHelper callback;
Valerie Hauaa194562019-02-05 16:21:38 -08002896 int err = fillTransaction(transaction, &callback, layer, false, false);
Marissa Wall713b63f2018-10-17 15:42:43 -07002897 if (err) {
2898 GTEST_SUCCEED() << "test not supported";
2899 return;
2900 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002901
Marissa Wall861616d2018-10-22 12:52:23 -07002902 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002903
2904 ExpectedResult expected;
Marissa Wall713b63f2018-10-17 15:42:43 -07002905 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
2906 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002907 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2908}
2909
Valerie Hauaa194562019-02-05 16:21:38 -08002910TEST_F(LayerCallbackTest, BufferNoColor) {
2911 sp<SurfaceControl> layer;
2912 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2913
2914 Transaction transaction;
2915 CallbackHelper callback;
2916 int err = fillTransaction(transaction, &callback, layer, true, false);
2917 if (err) {
2918 GTEST_SUCCEED() << "test not supported";
2919 return;
2920 }
2921
2922 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
2923
2924 ExpectedResult expected;
2925 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2926 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2927}
2928
2929TEST_F(LayerCallbackTest, NoBufferColor) {
2930 sp<SurfaceControl> layer;
2931 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2932
2933 Transaction transaction;
2934 CallbackHelper callback;
2935 int err = fillTransaction(transaction, &callback, layer, false, true);
2936 if (err) {
2937 GTEST_SUCCEED() << "test not supported";
2938 return;
2939 }
2940
2941 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
2942
2943 ExpectedResult expected;
2944 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2945 ExpectedResult::Buffer::NOT_ACQUIRED);
2946 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2947}
2948
Marissa Wallfda30bb2018-10-12 11:34:28 -07002949TEST_F(LayerCallbackTest, NoStateChange) {
2950 Transaction transaction;
2951 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002952 int err = fillTransaction(transaction, &callback);
2953 if (err) {
2954 GTEST_SUCCEED() << "test not supported";
2955 return;
2956 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002957
2958 transaction.apply();
2959
2960 ExpectedResult expected;
2961 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2962}
2963
2964TEST_F(LayerCallbackTest, OffScreen) {
2965 sp<SurfaceControl> layer;
2966 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2967
2968 Transaction transaction;
2969 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002970 int err = fillTransaction(transaction, &callback, layer);
2971 if (err) {
2972 GTEST_SUCCEED() << "test not supported";
2973 return;
2974 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002975
Marissa Wall861616d2018-10-22 12:52:23 -07002976 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002977
2978 ExpectedResult expected;
2979 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2980 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2981}
2982
Valerie Hauaa194562019-02-05 16:21:38 -08002983TEST_F(LayerCallbackTest, MergeBufferNoColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002984 sp<SurfaceControl> layer1, layer2;
2985 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2986 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2987
2988 Transaction transaction1, transaction2;
2989 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07002990 int err = fillTransaction(transaction1, &callback1, layer1);
2991 if (err) {
2992 GTEST_SUCCEED() << "test not supported";
2993 return;
2994 }
2995 err = fillTransaction(transaction2, &callback2, layer2);
2996 if (err) {
2997 GTEST_SUCCEED() << "test not supported";
2998 return;
2999 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003000
Marissa Wall861616d2018-10-22 12:52:23 -07003001 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3002 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003003
3004 ExpectedResult expected;
3005 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3006 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3007 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3008}
3009
Valerie Hauaa194562019-02-05 16:21:38 -08003010TEST_F(LayerCallbackTest, MergeNoBufferColor) {
3011 sp<SurfaceControl> layer1, layer2;
3012 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3013 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3014
3015 Transaction transaction1, transaction2;
3016 CallbackHelper callback1, callback2;
3017 int err = fillTransaction(transaction1, &callback1, layer1, false, true);
3018 if (err) {
3019 GTEST_SUCCEED() << "test not supported";
3020 return;
3021 }
3022 err = fillTransaction(transaction2, &callback2, layer2, false, true);
3023 if (err) {
3024 GTEST_SUCCEED() << "test not supported";
3025 return;
3026 }
3027
3028 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3029 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3030
3031 ExpectedResult expected;
3032 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
3033 ExpectedResult::Buffer::NOT_ACQUIRED);
3034 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3035 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3036}
3037
3038TEST_F(LayerCallbackTest, MergeOneBufferOneColor) {
3039 sp<SurfaceControl> layer1, layer2;
3040 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3041 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3042
3043 Transaction transaction1, transaction2;
3044 CallbackHelper callback1, callback2;
3045 int err = fillTransaction(transaction1, &callback1, layer1);
3046 if (err) {
3047 GTEST_SUCCEED() << "test not supported";
3048 return;
3049 }
3050 err = fillTransaction(transaction2, &callback2, layer2, false, true);
3051 if (err) {
3052 GTEST_SUCCEED() << "test not supported";
3053 return;
3054 }
3055
3056 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3057 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3058
3059 ExpectedResult expected;
3060 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer1);
3061 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer2,
3062 ExpectedResult::Buffer::NOT_ACQUIRED);
3063 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3064 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3065}
Marissa Wallfda30bb2018-10-12 11:34:28 -07003066TEST_F(LayerCallbackTest, Merge_SameCallback) {
3067 sp<SurfaceControl> layer1, layer2;
3068 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3069 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3070
3071 Transaction transaction1, transaction2;
3072 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003073 int err = fillTransaction(transaction1, &callback, layer1);
3074 if (err) {
3075 GTEST_SUCCEED() << "test not supported";
3076 return;
3077 }
3078 err = fillTransaction(transaction2, &callback, layer2);
3079 if (err) {
3080 GTEST_SUCCEED() << "test not supported";
3081 return;
3082 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003083
3084 transaction2.merge(std::move(transaction1)).apply();
3085
3086 ExpectedResult expected;
3087 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3088 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3089 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3090}
3091
3092TEST_F(LayerCallbackTest, Merge_SameLayer) {
3093 sp<SurfaceControl> layer;
3094 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3095
3096 Transaction transaction1, transaction2;
3097 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003098 int err = fillTransaction(transaction1, &callback1, layer);
3099 if (err) {
3100 GTEST_SUCCEED() << "test not supported";
3101 return;
3102 }
3103 err = fillTransaction(transaction2, &callback2, layer);
3104 if (err) {
3105 GTEST_SUCCEED() << "test not supported";
3106 return;
3107 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003108
3109 transaction2.merge(std::move(transaction1)).apply();
3110
3111 ExpectedResult expected;
3112 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3113 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3114 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3115}
3116
Marissa Wallfda30bb2018-10-12 11:34:28 -07003117TEST_F(LayerCallbackTest, Merge_DifferentClients) {
3118 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3119 client2(new SurfaceComposerClient);
3120
3121 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3122 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3123
3124 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003125 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003126 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003127 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003128 ISurfaceComposerClient::eFXSurfaceBufferState));
3129
3130 Transaction transaction1, transaction2;
3131 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003132 int err = fillTransaction(transaction1, &callback1, layer1);
3133 if (err) {
3134 GTEST_SUCCEED() << "test not supported";
3135 return;
3136 }
3137 err = fillTransaction(transaction2, &callback2, layer2);
3138 if (err) {
3139 GTEST_SUCCEED() << "test not supported";
3140 return;
3141 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003142
Marissa Wall861616d2018-10-22 12:52:23 -07003143 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3144 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003145
3146 ExpectedResult expected;
3147 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3148 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3149 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3150}
3151
3152TEST_F(LayerCallbackTest, MultipleTransactions) {
3153 sp<SurfaceControl> layer;
3154 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3155
3156 Transaction transaction;
3157 CallbackHelper callback;
3158 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003159 int err = fillTransaction(transaction, &callback, layer);
3160 if (err) {
3161 GTEST_SUCCEED() << "test not supported";
3162 return;
3163 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003164
3165 transaction.apply();
3166
3167 ExpectedResult expected;
3168 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07003169 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003170 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3171 : ExpectedResult::PreviousBuffer::RELEASED);
3172 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3173 }
3174 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3175}
3176
3177TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
3178 sp<SurfaceControl> layer;
3179 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3180
3181 Transaction transaction;
3182 CallbackHelper callback;
3183 for (size_t i = 0; i < 10; i++) {
3184 ExpectedResult expected;
3185
3186 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003187 int err = fillTransaction(transaction, &callback, layer);
3188 if (err) {
3189 GTEST_SUCCEED() << "test not supported";
3190 return;
3191 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003192 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3193 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003194 int err = fillTransaction(transaction, &callback);
3195 if (err) {
3196 GTEST_SUCCEED() << "test not supported";
3197 return;
3198 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003199 }
3200
3201 transaction.apply();
3202
3203 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3204 }
3205 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3206}
3207
3208TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
3209 sp<SurfaceControl> layer;
3210 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3211
3212 Transaction transaction;
3213 CallbackHelper callback;
3214 for (size_t i = 0; i < 10; i++) {
3215 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003216 int err = fillTransaction(transaction, &callback, layer);
3217 if (err) {
3218 GTEST_SUCCEED() << "test not supported";
3219 return;
3220 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003221 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003222 int err = fillTransaction(transaction, &callback);
3223 if (err) {
3224 GTEST_SUCCEED() << "test not supported";
3225 return;
3226 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003227 }
3228
Marissa Wall861616d2018-10-22 12:52:23 -07003229 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003230
3231 ExpectedResult expected;
3232 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
3233 : ExpectedResult::Transaction::NOT_PRESENTED,
Marissa Wall713b63f2018-10-17 15:42:43 -07003234 layer,
3235 (i == 0) ? ExpectedResult::Buffer::ACQUIRED
3236 : ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003237 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
3238 }
3239 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3240}
3241
3242TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
3243 sp<SurfaceControl> layer1, layer2;
3244 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3245 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3246
3247 Transaction transaction1, transaction2;
3248 CallbackHelper callback1, callback2;
3249 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003250 int err = fillTransaction(transaction1, &callback1, layer1);
3251 if (err) {
3252 GTEST_SUCCEED() << "test not supported";
3253 return;
3254 }
3255 err = fillTransaction(transaction2, &callback2, layer2);
3256 if (err) {
3257 GTEST_SUCCEED() << "test not supported";
3258 return;
3259 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003260
Marissa Wall861616d2018-10-22 12:52:23 -07003261 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3262 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003263
3264 ExpectedResult expected;
3265 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003266 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003267 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3268 : ExpectedResult::PreviousBuffer::RELEASED);
3269 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3270 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3271 }
3272 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3273 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3274}
3275
3276TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
3277 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3278 client2(new SurfaceComposerClient);
3279 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3280 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3281
3282 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003283 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003284 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003285 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003286 ISurfaceComposerClient::eFXSurfaceBufferState));
3287
3288 Transaction transaction1, transaction2;
3289 CallbackHelper callback1, callback2;
3290 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003291 int err = fillTransaction(transaction1, &callback1, layer1);
3292 if (err) {
3293 GTEST_SUCCEED() << "test not supported";
3294 return;
3295 }
3296 err = fillTransaction(transaction2, &callback2, layer2);
3297 if (err) {
3298 GTEST_SUCCEED() << "test not supported";
3299 return;
3300 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003301
Marissa Wall861616d2018-10-22 12:52:23 -07003302 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3303 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003304
3305 ExpectedResult expected;
3306 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003307 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003308 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3309 : ExpectedResult::PreviousBuffer::RELEASED);
3310 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3311 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3312 }
3313 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3314 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3315}
3316
3317TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
3318 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3319 client2(new SurfaceComposerClient);
3320 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3321 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3322
3323 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003324 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003325 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003326 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003327 ISurfaceComposerClient::eFXSurfaceBufferState));
3328
3329 Transaction transaction1, transaction2;
3330 CallbackHelper callback1, callback2;
3331
3332 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003333 int err = fillTransaction(transaction1, &callback1, layer1);
3334 if (err) {
3335 GTEST_SUCCEED() << "test not supported";
3336 return;
3337 }
3338 err = fillTransaction(transaction2, &callback2, layer2);
3339 if (err) {
3340 GTEST_SUCCEED() << "test not supported";
3341 return;
3342 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003343
Marissa Wall861616d2018-10-22 12:52:23 -07003344 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3345 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003346
3347 ExpectedResult expected;
3348 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3349 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3350 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3351 expected.reset();
3352
3353 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003354 err = fillTransaction(transaction1, &callback1);
3355 if (err) {
3356 GTEST_SUCCEED() << "test not supported";
3357 return;
3358 }
3359 err = fillTransaction(transaction2, &callback2);
3360 if (err) {
3361 GTEST_SUCCEED() << "test not supported";
3362 return;
3363 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003364
3365 transaction2.merge(std::move(transaction1)).apply();
3366
3367 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3368 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3369}
3370
3371TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
3372 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3373 client2(new SurfaceComposerClient);
3374
3375 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3376 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3377
3378 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003379 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003380 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003381 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003382 ISurfaceComposerClient::eFXSurfaceBufferState));
3383
3384 Transaction transaction1, transaction2;
3385 CallbackHelper callback1, callback2;
3386
3387 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003388 int err = fillTransaction(transaction1, &callback1, layer1);
3389 if (err) {
3390 GTEST_SUCCEED() << "test not supported";
3391 return;
3392 }
3393 err = fillTransaction(transaction2, &callback2, layer2);
3394 if (err) {
3395 GTEST_SUCCEED() << "test not supported";
3396 return;
3397 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003398
Marissa Wall861616d2018-10-22 12:52:23 -07003399 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3400 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003401
3402 ExpectedResult expected;
3403 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3404 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3405 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3406 expected.reset();
3407
3408 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003409 err = fillTransaction(transaction1, &callback1);
3410 if (err) {
3411 GTEST_SUCCEED() << "test not supported";
3412 return;
3413 }
3414 err = fillTransaction(transaction2, &callback2);
3415 if (err) {
3416 GTEST_SUCCEED() << "test not supported";
3417 return;
3418 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003419
Marissa Wall861616d2018-10-22 12:52:23 -07003420 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003421
Marissa Wall713b63f2018-10-17 15:42:43 -07003422 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
3423 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003424 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3425 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3426}
3427
3428TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3429 sp<SurfaceControl> layer;
3430 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3431
3432 Transaction transaction;
3433 CallbackHelper callback;
3434 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003435 for (auto& expected : expectedResults) {
3436 expected.reset();
3437 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall5a68a772018-12-22 17:43:42 -08003438 ExpectedResult::Buffer::ACQUIRED,
3439 ExpectedResult::PreviousBuffer::UNKNOWN);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003440
Marissa Wall713b63f2018-10-17 15:42:43 -07003441 int err = fillTransaction(transaction, &callback, layer);
3442 if (err) {
3443 GTEST_SUCCEED() << "test not supported";
3444 return;
3445 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003446
3447 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003448 }
3449 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3450}
3451
3452TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3453 sp<SurfaceControl> layer;
3454 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3455
Marissa Wall713b63f2018-10-17 15:42:43 -07003456 // Normal call to set up test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003457 Transaction transaction;
3458 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003459 int err = fillTransaction(transaction, &callback, layer);
3460 if (err) {
3461 GTEST_SUCCEED() << "test not supported";
3462 return;
3463 }
3464
3465 transaction.apply();
3466
3467 ExpectedResult expected;
3468 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3469 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3470
3471 // Test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003472 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003473 for (auto& expected : expectedResults) {
3474 expected.reset();
3475
Marissa Wall713b63f2018-10-17 15:42:43 -07003476 err = fillTransaction(transaction, &callback);
3477 if (err) {
3478 GTEST_SUCCEED() << "test not supported";
3479 return;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003480 }
3481
3482 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003483 }
3484 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3485}
3486
3487TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3488 sp<SurfaceControl> layer;
3489 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3490
3491 // Normal call to set up test
3492 Transaction transaction;
3493 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003494 int err = fillTransaction(transaction, &callback, layer);
3495 if (err) {
3496 GTEST_SUCCEED() << "test not supported";
3497 return;
3498 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003499
Marissa Wall861616d2018-10-22 12:52:23 -07003500 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003501
3502 ExpectedResult expectedResult;
3503 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3504 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3505
3506 // Test
3507 std::vector<ExpectedResult> expectedResults(50);
3508 for (auto& expected : expectedResults) {
3509 expected.reset();
Marissa Wall713b63f2018-10-17 15:42:43 -07003510 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
3511 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003512
Marissa Wall713b63f2018-10-17 15:42:43 -07003513 err = fillTransaction(transaction, &callback);
3514 if (err) {
3515 GTEST_SUCCEED() << "test not supported";
3516 return;
3517 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003518
Marissa Wall861616d2018-10-22 12:52:23 -07003519 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003520 }
3521 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3522}
3523
Marissa Wall17b4e452018-12-26 16:32:34 -08003524TEST_F(LayerCallbackTest, DesiredPresentTime) {
3525 sp<SurfaceControl> layer;
3526 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3527
3528 Transaction transaction;
3529 CallbackHelper callback;
3530 int err = fillTransaction(transaction, &callback, layer);
3531 if (err) {
3532 GTEST_SUCCEED() << "test not supported";
3533 return;
3534 }
3535
3536 // Try to present 100ms in the future
3537 nsecs_t time = systemTime() + (100 * 1e6);
3538
3539 transaction.setDesiredPresentTime(time);
3540 transaction.apply();
3541
3542 ExpectedResult expected;
3543 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3544 expected.addExpectedPresentTime(time);
3545 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3546}
3547
3548TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
3549 sp<SurfaceControl> layer;
3550 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3551
3552 Transaction transaction;
3553 CallbackHelper callback1;
3554 int err = fillTransaction(transaction, &callback1, layer);
3555 if (err) {
3556 GTEST_SUCCEED() << "test not supported";
3557 return;
3558 }
3559
3560 // Try to present 100ms in the future
3561 nsecs_t time = systemTime() + (100 * 1e6);
3562
3563 transaction.setDesiredPresentTime(time);
3564 transaction.apply();
3565
3566 ExpectedResult expected1;
3567 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3568 expected1.addExpectedPresentTime(time);
3569
3570 CallbackHelper callback2;
3571 err = fillTransaction(transaction, &callback2, layer);
3572 if (err) {
3573 GTEST_SUCCEED() << "test not supported";
3574 return;
3575 }
3576
3577 // Try to present 33ms after the first frame
3578 time += (33.3 * 1e6);
3579
3580 transaction.setDesiredPresentTime(time);
3581 transaction.apply();
3582
3583 ExpectedResult expected2;
3584 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3585 ExpectedResult::Buffer::ACQUIRED,
3586 ExpectedResult::PreviousBuffer::RELEASED);
3587 expected2.addExpectedPresentTime(time);
3588
3589 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3590 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3591}
3592
3593TEST_F(LayerCallbackTest, DesiredPresentTime_OutOfOrder) {
3594 sp<SurfaceControl> layer;
3595 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3596
3597 Transaction transaction;
3598 CallbackHelper callback1;
3599 int err = fillTransaction(transaction, &callback1, layer);
3600 if (err) {
3601 GTEST_SUCCEED() << "test not supported";
3602 return;
3603 }
3604
3605 // Try to present 100ms in the future
3606 nsecs_t time = systemTime() + (100 * 1e6);
3607
3608 transaction.setDesiredPresentTime(time);
3609 transaction.apply();
3610
3611 ExpectedResult expected1;
3612 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3613 expected1.addExpectedPresentTime(time);
3614
3615 CallbackHelper callback2;
3616 err = fillTransaction(transaction, &callback2, layer);
3617 if (err) {
3618 GTEST_SUCCEED() << "test not supported";
3619 return;
3620 }
3621
3622 // Try to present 33ms before the previous frame
3623 time -= (33.3 * 1e6);
3624
3625 transaction.setDesiredPresentTime(time);
3626 transaction.apply();
3627
3628 ExpectedResult expected2;
3629 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3630 ExpectedResult::Buffer::ACQUIRED,
3631 ExpectedResult::PreviousBuffer::RELEASED);
3632
3633 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3634 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3635}
3636
3637TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
3638 sp<SurfaceControl> layer;
3639 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3640
3641 Transaction transaction;
3642 CallbackHelper callback;
3643 int err = fillTransaction(transaction, &callback, layer);
3644 if (err) {
3645 GTEST_SUCCEED() << "test not supported";
3646 return;
3647 }
3648
3649 // Try to present 100ms in the past
3650 nsecs_t time = systemTime() - (100 * 1e6);
3651
3652 transaction.setDesiredPresentTime(time);
3653 transaction.apply();
3654
3655 ExpectedResult expected;
3656 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3657 expected.addExpectedPresentTime(systemTime());
3658 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3659}
3660
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003661class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003662protected:
3663 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07003664 LayerTransactionTest::SetUp();
3665 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003666
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08003667 const auto display = SurfaceComposerClient::getInternalDisplayToken();
3668 ASSERT_FALSE(display == nullptr);
3669
Mathias Agopianc666cae2012-07-25 18:56:13 -07003670 DisplayInfo info;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08003671 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
Mathias Agopianc666cae2012-07-25 18:56:13 -07003672
3673 ssize_t displayWidth = info.w;
3674 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003675
3676 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07003677 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
3678 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003679 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003680 ASSERT_TRUE(mBGSurfaceControl->isValid());
Valerie Haue271df92019-09-06 09:23:22 -07003681 TransactionUtils::fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003682
3683 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07003684 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
3685
Peiyong Lin566a3b42018-01-09 18:22:43 -08003686 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003687 ASSERT_TRUE(mFGSurfaceControl->isValid());
3688
Valerie Haue271df92019-09-06 09:23:22 -07003689 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003690
3691 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07003692 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003693 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003694 ASSERT_TRUE(mSyncSurfaceControl->isValid());
3695
Valerie Haue271df92019-09-06 09:23:22 -07003696 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003697
Robert Carr4cdc58f2017-08-23 14:22:20 -07003698 asTransaction([&](Transaction& t) {
3699 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003700
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003701 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07003702
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003703 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
3704 .setPosition(mFGSurfaceControl, 64, 64)
3705 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003706
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003707 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
3708 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
3709 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003710 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003711 }
3712
3713 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07003714 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003715 mBGSurfaceControl = 0;
3716 mFGSurfaceControl = 0;
3717 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003718 }
3719
3720 void waitForPostedBuffers() {
3721 // Since the sync surface is in synchronous mode (i.e. double buffered)
3722 // posting three buffers to it should ensure that at least two
3723 // SurfaceFlinger::handlePageFlip calls have been made, which should
3724 // guaranteed that a buffer posted to another Surface has been retired.
Valerie Haue271df92019-09-06 09:23:22 -07003725 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3726 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3727 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003728 }
3729
Robert Carr4cdc58f2017-08-23 14:22:20 -07003730
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003731 sp<SurfaceControl> mBGSurfaceControl;
3732 sp<SurfaceControl> mFGSurfaceControl;
3733
3734 // This surface is used to ensure that the buffers posted to
3735 // mFGSurfaceControl have been picked up by SurfaceFlinger.
3736 sp<SurfaceControl> mSyncSurfaceControl;
3737};
3738
Robert Carr7f619b22017-11-06 12:56:35 -08003739TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08003740
chaviw0e3479f2018-09-10 16:49:30 -07003741 std::unique_ptr<ScreenCapture> sc;
3742
3743 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Valerie Haue271df92019-09-06 09:23:22 -07003744 TransactionUtils::fillSurfaceRGBA8(relative, 10, 10, 10);
Robert Carr7f619b22017-11-06 12:56:35 -08003745 waitForPostedBuffers();
3746
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003747 Transaction{}
3748 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08003749 .setPosition(relative, 64, 64)
3750 .apply();
3751
3752 {
3753 // The relative should be on top of the FG control.
3754 ScreenCapture::captureScreen(&sc);
3755 sc->checkPixel(64, 64, 10, 10, 10);
3756 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003757 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003758
3759 {
3760 // Nothing should change at this point.
3761 ScreenCapture::captureScreen(&sc);
3762 sc->checkPixel(64, 64, 10, 10, 10);
3763 }
3764
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003765 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003766
3767 {
3768 // Ensure that the relative was actually hidden, rather than
3769 // being left in the detached but visible state.
3770 ScreenCapture::captureScreen(&sc);
3771 sc->expectFGColor(64, 64);
3772 }
3773}
3774
Robert Carr8d5227b2017-03-16 15:41:03 -07003775class GeometryLatchingTest : public LayerUpdateTest {
3776protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003777 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07003778 SCOPED_TRACE(trace);
3779 ScreenCapture::captureScreen(&sc);
3780 // We find the leading edge of the FG surface.
3781 sc->expectFGColor(127, 127);
3782 sc->expectBGColor(128, 128);
3783 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003784
Valerie Haue271df92019-09-06 09:23:22 -07003785 void lockAndFillFGBuffer() {
3786 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false);
3787 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003788
3789 void unlockFGBuffer() {
3790 sp<Surface> s = mFGSurfaceControl->getSurface();
3791 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
3792 waitForPostedBuffers();
3793 }
3794
Robert Carr8d5227b2017-03-16 15:41:03 -07003795 void completeFGResize() {
Valerie Haue271df92019-09-06 09:23:22 -07003796 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
Robert Carr8d5227b2017-03-16 15:41:03 -07003797 waitForPostedBuffers();
3798 }
3799 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003800 asTransaction([&](Transaction& t) {
3801 t.setSize(mFGSurfaceControl, 64, 64);
3802 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003803 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003804 });
Robert Carr8d5227b2017-03-16 15:41:03 -07003805
3806 EXPECT_INITIAL_STATE("After restoring initial state");
3807 }
chaviw0e3479f2018-09-10 16:49:30 -07003808 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07003809};
3810
Robert Carr8d5227b2017-03-16 15:41:03 -07003811class CropLatchingTest : public GeometryLatchingTest {
3812protected:
3813 void EXPECT_CROPPED_STATE(const char* trace) {
3814 SCOPED_TRACE(trace);
3815 ScreenCapture::captureScreen(&sc);
3816 // The edge should be moved back one pixel by our crop.
3817 sc->expectFGColor(126, 126);
3818 sc->expectBGColor(127, 127);
3819 sc->expectBGColor(128, 128);
3820 }
chaviw59f5c562017-06-28 16:39:06 -07003821
3822 void EXPECT_RESIZE_STATE(const char* trace) {
3823 SCOPED_TRACE(trace);
3824 ScreenCapture::captureScreen(&sc);
3825 // The FG is now resized too 128,128 at 64,64
3826 sc->expectFGColor(64, 64);
3827 sc->expectFGColor(191, 191);
3828 sc->expectBGColor(192, 192);
3829 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003830};
3831
Pablo Ceballos05289c22016-04-14 15:49:55 -07003832TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003833 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003834 {
3835 SCOPED_TRACE("before anything");
3836 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003837 sc->expectBGColor(32, 32);
3838 sc->expectFGColor(96, 96);
3839 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003840 }
3841
3842 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003843 asTransaction([&](Transaction& t) {
3844 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003845 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3846 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003847 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003848
Robert Carr4cdc58f2017-08-23 14:22:20 -07003849 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003850 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003851 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3852 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003853 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003854
3855 {
3856 SCOPED_TRACE("before any trigger");
3857 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003858 sc->expectBGColor(32, 32);
3859 sc->expectFGColor(96, 96);
3860 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003861 }
3862
3863 // should trigger the first deferred transaction, but not the second one
Valerie Haue271df92019-09-06 09:23:22 -07003864 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003865 {
3866 SCOPED_TRACE("after first trigger");
3867 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003868 sc->expectBGColor(32, 32);
3869 sc->checkPixel(96, 96, 162, 63, 96);
3870 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003871 }
3872
3873 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003874 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003875
3876 // trigger the second deferred transaction
Valerie Haue271df92019-09-06 09:23:22 -07003877 TransactionUtils::fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003878 {
3879 SCOPED_TRACE("after second trigger");
3880 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003881 sc->expectBGColor(32, 32);
3882 sc->expectBGColor(96, 96);
3883 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003884 }
3885}
3886
Robert Carre392b552017-09-19 12:16:05 -07003887TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003888 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003889
3890 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003891 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3892 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3893 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3894 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Valerie Haue271df92019-09-06 09:23:22 -07003895 TransactionUtils::fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003896 SurfaceComposerClient::Transaction{}
3897 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3898 .show(childNoBuffer)
3899 .show(childBuffer)
3900 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003901 {
3902 ScreenCapture::captureScreen(&sc);
3903 sc->expectChildColor(73, 73);
3904 sc->expectFGColor(74, 74);
3905 }
Vishnu Nair60356342018-11-13 13:00:45 -08003906 SurfaceComposerClient::Transaction{}
3907 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3908 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003909 {
3910 ScreenCapture::captureScreen(&sc);
3911 sc->expectChildColor(73, 73);
3912 sc->expectChildColor(74, 74);
3913 }
3914}
3915
Robert Carr2c5f6d22017-09-26 12:30:35 -07003916TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003917 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003918 {
3919 SCOPED_TRACE("before move");
3920 ScreenCapture::captureScreen(&sc);
3921 sc->expectBGColor(0, 12);
3922 sc->expectFGColor(75, 75);
3923 sc->expectBGColor(145, 145);
3924 }
3925
3926 Transaction t1, t2;
3927 t1.setPosition(mFGSurfaceControl, 128, 128);
3928 t2.setPosition(mFGSurfaceControl, 0, 0);
3929 // We expect that the position update from t2 now
3930 // overwrites the position update from t1.
3931 t1.merge(std::move(t2));
3932 t1.apply();
3933
3934 {
3935 ScreenCapture::captureScreen(&sc);
3936 sc->expectFGColor(1, 1);
3937 }
3938}
3939
Vishnu Nair996bc422019-07-16 14:15:33 -07003940TEST_F(LayerUpdateTest, MergingTransactionFlags) {
3941 Transaction().hide(mFGSurfaceControl).apply();
3942 std::unique_ptr<ScreenCapture> sc;
3943 {
3944 SCOPED_TRACE("before merge");
3945 ScreenCapture::captureScreen(&sc);
3946 sc->expectBGColor(0, 12);
3947 sc->expectBGColor(75, 75);
3948 sc->expectBGColor(145, 145);
3949 }
3950
3951 Transaction t1, t2;
3952 t1.show(mFGSurfaceControl);
3953 t2.setFlags(mFGSurfaceControl, 0 /* flags */, layer_state_t::eLayerSecure /* mask */);
3954 t1.merge(std::move(t2));
3955 t1.apply();
3956
3957 {
3958 SCOPED_TRACE("after merge");
3959 ScreenCapture::captureScreen(&sc);
3960 sc->expectFGColor(75, 75);
3961 }
3962}
3963
Robert Carr1f0a16a2016-10-24 16:27:39 -07003964class ChildLayerTest : public LayerUpdateTest {
3965protected:
3966 void SetUp() override {
3967 LayerUpdateTest::SetUp();
Vishnu Nairc652ff82019-03-15 12:48:54 -07003968 mChild = createSurface(mClient, "Child surface", 10, 15, PIXEL_FORMAT_RGBA_8888, 0,
Vishnu Nair88a11f22018-11-28 18:30:57 -08003969 mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07003970 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
Robert Carr1f0a16a2016-10-24 16:27:39 -07003971
3972 {
3973 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003974 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003975 mCapture->expectChildColor(64, 64);
3976 }
3977 }
3978 void TearDown() override {
3979 LayerUpdateTest::TearDown();
3980 mChild = 0;
3981 }
3982
3983 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003984 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003985};
3986
3987TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003988 asTransaction([&](Transaction& t) {
3989 t.show(mChild);
3990 t.setPosition(mChild, 10, 10);
3991 t.setPosition(mFGSurfaceControl, 64, 64);
3992 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003993
3994 {
chaviw0e3479f2018-09-10 16:49:30 -07003995 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003996 // Top left of foreground must now be visible
3997 mCapture->expectFGColor(64, 64);
3998 // But 10 pixels in we should see the child surface
3999 mCapture->expectChildColor(74, 74);
4000 // And 10 more pixels we should be back to the foreground surface
4001 mCapture->expectFGColor(84, 84);
4002 }
4003
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004004 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004005
4006 {
chaviw0e3479f2018-09-10 16:49:30 -07004007 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004008 // Top left of foreground should now be at 0, 0
4009 mCapture->expectFGColor(0, 0);
4010 // But 10 pixels in we should see the child surface
4011 mCapture->expectChildColor(10, 10);
4012 // And 10 more pixels we should be back to the foreground surface
4013 mCapture->expectFGColor(20, 20);
4014 }
4015}
4016
Robert Carr41b08b52017-06-01 16:11:34 -07004017TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004018 asTransaction([&](Transaction& t) {
4019 t.show(mChild);
4020 t.setPosition(mChild, 0, 0);
4021 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004022 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07004023 });
Robert Carr41b08b52017-06-01 16:11:34 -07004024
4025 {
chaviw0e3479f2018-09-10 16:49:30 -07004026 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07004027 mCapture->expectChildColor(0, 0);
4028 mCapture->expectChildColor(4, 4);
4029 mCapture->expectBGColor(5, 5);
4030 }
4031}
4032
Robert Carr1f0a16a2016-10-24 16:27:39 -07004033TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004034 asTransaction([&](Transaction& t) {
4035 t.show(mChild);
4036 t.setPosition(mFGSurfaceControl, 0, 0);
4037 t.setPosition(mChild, 63, 63);
4038 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004039
4040 {
chaviw0e3479f2018-09-10 16:49:30 -07004041 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004042 mCapture->expectFGColor(0, 0);
4043 // Last pixel in foreground should now be the child.
4044 mCapture->expectChildColor(63, 63);
4045 // But the child should be constrained and the next pixel
4046 // must be the background
4047 mCapture->expectBGColor(64, 64);
4048 }
4049}
4050
4051TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004052 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004053
4054 // Find the boundary between the parent and child
4055 {
chaviw0e3479f2018-09-10 16:49:30 -07004056 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004057 mCapture->expectChildColor(9, 9);
4058 mCapture->expectFGColor(10, 10);
4059 }
4060
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004061 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004062
4063 // The boundary should be twice as far from the origin now.
4064 // The pixels from the last test should all be child now
4065 {
chaviw0e3479f2018-09-10 16:49:30 -07004066 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004067 mCapture->expectChildColor(9, 9);
4068 mCapture->expectChildColor(10, 10);
4069 mCapture->expectChildColor(19, 19);
4070 mCapture->expectFGColor(20, 20);
4071 }
4072}
Robert Carr9524cb32017-02-13 11:32:32 -08004073
Vishnu Nairc652ff82019-03-15 12:48:54 -07004074// A child with a scale transform should be cropped by its parent bounds.
4075TEST_F(ChildLayerTest, ChildLayerScalingCroppedByParent) {
4076 asTransaction([&](Transaction& t) {
4077 t.setPosition(mFGSurfaceControl, 0, 0);
4078 t.setPosition(mChild, 0, 0);
4079 });
4080
4081 // Find the boundary between the parent and child.
4082 {
4083 mCapture = screenshot();
4084 mCapture->expectChildColor(0, 0);
4085 mCapture->expectChildColor(9, 9);
4086 mCapture->expectFGColor(10, 10);
4087 }
4088
4089 asTransaction([&](Transaction& t) { t.setMatrix(mChild, 10.0, 0, 0, 10.0); });
4090
4091 // The child should fill its parent bounds and be cropped by it.
4092 {
4093 mCapture = screenshot();
4094 mCapture->expectChildColor(0, 0);
4095 mCapture->expectChildColor(63, 63);
4096 mCapture->expectBGColor(64, 64);
4097 }
4098}
4099
Robert Carr6452f122017-03-21 10:41:29 -07004100TEST_F(ChildLayerTest, ChildLayerAlpha) {
Valerie Haue271df92019-09-06 09:23:22 -07004101 TransactionUtils::fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
4102 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
4103 TransactionUtils::fillSurfaceRGBA8(mChild, 0, 254, 0);
Robert Carr6452f122017-03-21 10:41:29 -07004104 waitForPostedBuffers();
4105
Robert Carr4cdc58f2017-08-23 14:22:20 -07004106 asTransaction([&](Transaction& t) {
4107 t.show(mChild);
4108 t.setPosition(mChild, 0, 0);
4109 t.setPosition(mFGSurfaceControl, 0, 0);
4110 });
Robert Carr6452f122017-03-21 10:41:29 -07004111
4112 {
chaviw0e3479f2018-09-10 16:49:30 -07004113 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004114 // Unblended child color
4115 mCapture->checkPixel(0, 0, 0, 254, 0);
4116 }
4117
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004118 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004119
4120 {
chaviw0e3479f2018-09-10 16:49:30 -07004121 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004122 // Child and BG blended.
4123 mCapture->checkPixel(0, 0, 127, 127, 0);
4124 }
4125
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004126 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004127
4128 {
chaviw0e3479f2018-09-10 16:49:30 -07004129 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004130 // Child and BG blended.
4131 mCapture->checkPixel(0, 0, 95, 64, 95);
4132 }
4133}
4134
Robert Carr9524cb32017-02-13 11:32:32 -08004135TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004136 asTransaction([&](Transaction& t) {
4137 t.show(mChild);
4138 t.setPosition(mChild, 10, 10);
4139 t.setPosition(mFGSurfaceControl, 64, 64);
4140 });
Robert Carr9524cb32017-02-13 11:32:32 -08004141
4142 {
chaviw0e3479f2018-09-10 16:49:30 -07004143 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004144 // Top left of foreground must now be visible
4145 mCapture->expectFGColor(64, 64);
4146 // But 10 pixels in we should see the child surface
4147 mCapture->expectChildColor(74, 74);
4148 // And 10 more pixels we should be back to the foreground surface
4149 mCapture->expectFGColor(84, 84);
4150 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004151
4152 asTransaction([&](Transaction& t) {
4153 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
4154 });
4155
Robert Carr9524cb32017-02-13 11:32:32 -08004156 {
chaviw0e3479f2018-09-10 16:49:30 -07004157 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004158 mCapture->expectFGColor(64, 64);
4159 // In reparenting we should have exposed the entire foreground surface.
4160 mCapture->expectFGColor(74, 74);
4161 // And the child layer should now begin at 10, 10 (since the BG
4162 // layer is at (0, 0)).
4163 mCapture->expectBGColor(9, 9);
4164 mCapture->expectChildColor(10, 10);
4165 }
4166}
4167
Robert Carr2e102c92018-10-23 12:11:15 -07004168TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
4169 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004170 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Valerie Haue271df92019-09-06 09:23:22 -07004171 TransactionUtils::fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
Robert Carr2e102c92018-10-23 12:11:15 -07004172
4173 {
4174 SCOPED_TRACE("Grandchild visible");
4175 ScreenCapture::captureScreen(&mCapture);
4176 mCapture->checkPixel(64, 64, 111, 111, 111);
4177 }
4178
Robert Carr87246532019-02-04 15:20:26 -08004179 mChild.clear();
Robert Carr2e102c92018-10-23 12:11:15 -07004180
4181 {
4182 SCOPED_TRACE("After destroying child");
4183 ScreenCapture::captureScreen(&mCapture);
4184 mCapture->expectFGColor(64, 64);
4185 }
4186
4187 asTransaction([&](Transaction& t) {
4188 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
4189 });
4190
4191 {
4192 SCOPED_TRACE("After reparenting grandchild");
4193 ScreenCapture::captureScreen(&mCapture);
4194 mCapture->checkPixel(64, 64, 111, 111, 111);
4195 }
4196}
4197
Vishnu Nair14d76922019-08-05 08:41:20 -07004198TEST_F(ChildLayerTest, ChildrenRelativeZSurvivesParentDestruction) {
4199 sp<SurfaceControl> mGrandChild =
4200 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Valerie Haue271df92019-09-06 09:23:22 -07004201 TransactionUtils::fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
Vishnu Nair14d76922019-08-05 08:41:20 -07004202
4203 // draw grand child behind the foreground surface
4204 asTransaction([&](Transaction& t) {
4205 t.setRelativeLayer(mGrandChild, mFGSurfaceControl->getHandle(), -1);
4206 });
4207
4208 {
4209 SCOPED_TRACE("Child visible");
4210 ScreenCapture::captureScreen(&mCapture);
4211 mCapture->checkPixel(64, 64, 200, 200, 200);
4212 }
4213
4214 asTransaction([&](Transaction& t) {
4215 t.reparent(mChild, nullptr);
4216 t.reparentChildren(mChild, mFGSurfaceControl->getHandle());
4217 });
4218
4219 {
4220 SCOPED_TRACE("foreground visible reparenting grandchild");
4221 ScreenCapture::captureScreen(&mCapture);
4222 mCapture->checkPixel(64, 64, 195, 63, 63);
4223 }
4224}
4225
chaviw161410b02017-07-27 10:46:08 -07004226TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004227 asTransaction([&](Transaction& t) {
4228 t.show(mChild);
4229 t.setPosition(mChild, 10, 10);
4230 t.setPosition(mFGSurfaceControl, 64, 64);
4231 });
Robert Carr9524cb32017-02-13 11:32:32 -08004232
4233 {
chaviw0e3479f2018-09-10 16:49:30 -07004234 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004235 // Top left of foreground must now be visible
4236 mCapture->expectFGColor(64, 64);
4237 // But 10 pixels in we should see the child surface
4238 mCapture->expectChildColor(74, 74);
4239 // And 10 more pixels we should be back to the foreground surface
4240 mCapture->expectFGColor(84, 84);
4241 }
4242
chaviw0e3479f2018-09-10 16:49:30 -07004243
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004244 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08004245
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004246 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08004247
chaviw161410b02017-07-27 10:46:08 -07004248 // Since the child has the same client as the parent, it will not get
4249 // detached and will be hidden.
4250 {
chaviw0e3479f2018-09-10 16:49:30 -07004251 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004252 mCapture->expectFGColor(64, 64);
4253 mCapture->expectFGColor(74, 74);
4254 mCapture->expectFGColor(84, 84);
4255 }
4256}
4257
4258TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
4259 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004260 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004261 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
4262 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07004263
chaviw161410b02017-07-27 10:46:08 -07004264 ASSERT_TRUE(mChildNewClient->isValid());
4265
Valerie Haue271df92019-09-06 09:23:22 -07004266 TransactionUtils::fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
chaviw161410b02017-07-27 10:46:08 -07004267
Robert Carr4cdc58f2017-08-23 14:22:20 -07004268 asTransaction([&](Transaction& t) {
4269 t.hide(mChild);
4270 t.show(mChildNewClient);
4271 t.setPosition(mChildNewClient, 10, 10);
4272 t.setPosition(mFGSurfaceControl, 64, 64);
4273 });
chaviw161410b02017-07-27 10:46:08 -07004274
4275 {
chaviw0e3479f2018-09-10 16:49:30 -07004276 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004277 // Top left of foreground must now be visible
4278 mCapture->expectFGColor(64, 64);
4279 // But 10 pixels in we should see the child surface
4280 mCapture->expectChildColor(74, 74);
4281 // And 10 more pixels we should be back to the foreground surface
4282 mCapture->expectFGColor(84, 84);
4283 }
4284
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004285 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07004286
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004287 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07004288
Robert Carr9524cb32017-02-13 11:32:32 -08004289 // Nothing should have changed.
4290 {
chaviw0e3479f2018-09-10 16:49:30 -07004291 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004292 mCapture->expectFGColor(64, 64);
4293 mCapture->expectChildColor(74, 74);
4294 mCapture->expectFGColor(84, 84);
4295 }
4296}
4297
chaviw5aedec92018-10-22 10:40:38 -07004298TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
4299 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4300 sp<SurfaceControl> childNewClient =
4301 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4302 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4303
4304 ASSERT_TRUE(childNewClient != nullptr);
4305 ASSERT_TRUE(childNewClient->isValid());
4306
Valerie Haue271df92019-09-06 09:23:22 -07004307 TransactionUtils::fillSurfaceRGBA8(childNewClient, 200, 200, 200);
chaviw5aedec92018-10-22 10:40:38 -07004308
4309 Transaction()
4310 .hide(mChild)
4311 .show(childNewClient)
4312 .setPosition(childNewClient, 10, 10)
4313 .setPosition(mFGSurfaceControl, 64, 64)
4314 .apply();
4315
4316 {
4317 mCapture = screenshot();
4318 // Top left of foreground must now be visible
4319 mCapture->expectFGColor(64, 64);
4320 // But 10 pixels in we should see the child surface
4321 mCapture->expectChildColor(74, 74);
4322 // And 10 more pixels we should be back to the foreground surface
4323 mCapture->expectFGColor(84, 84);
4324 }
4325
4326 Transaction().detachChildren(mFGSurfaceControl).apply();
4327 Transaction().hide(childNewClient).apply();
4328
4329 // Nothing should have changed.
4330 {
4331 mCapture = screenshot();
4332 mCapture->expectFGColor(64, 64);
4333 mCapture->expectChildColor(74, 74);
4334 mCapture->expectFGColor(84, 84);
4335 }
4336
4337 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
4338 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
4339 32);
4340 Transaction()
4341 .setLayer(newParentSurface, INT32_MAX - 1)
4342 .show(newParentSurface)
4343 .setPosition(newParentSurface, 20, 20)
4344 .reparent(childNewClient, newParentSurface->getHandle())
4345 .apply();
4346 {
4347 mCapture = screenshot();
4348 // Child is now hidden.
4349 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
4350 }
4351}
chaviw43cb3cb2019-05-31 15:23:41 -07004352TEST_F(ChildLayerTest, DetachChildrenWithDeferredTransaction) {
4353 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4354 sp<SurfaceControl> childNewClient =
4355 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4356 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4357
4358 ASSERT_TRUE(childNewClient != nullptr);
4359 ASSERT_TRUE(childNewClient->isValid());
4360
Valerie Haue271df92019-09-06 09:23:22 -07004361 TransactionUtils::fillSurfaceRGBA8(childNewClient, 200, 200, 200);
chaviw43cb3cb2019-05-31 15:23:41 -07004362
4363 Transaction()
4364 .hide(mChild)
4365 .show(childNewClient)
4366 .setPosition(childNewClient, 10, 10)
4367 .setPosition(mFGSurfaceControl, 64, 64)
4368 .apply();
4369
4370 {
4371 mCapture = screenshot();
4372 Rect rect = Rect(74, 74, 84, 84);
4373 mCapture->expectBorder(rect, Color{195, 63, 63, 255});
4374 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4375 }
4376
4377 Transaction()
4378 .deferTransactionUntil_legacy(childNewClient, mFGSurfaceControl->getHandle(),
4379 mFGSurfaceControl->getSurface()->getNextFrameNumber())
4380 .apply();
4381 Transaction().detachChildren(mFGSurfaceControl).apply();
4382 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(mFGSurfaceControl, Color::RED, 32, 32));
4383
4384 // BufferLayer can still dequeue buffers even though there's a detached layer with a
4385 // deferred transaction.
4386 {
4387 SCOPED_TRACE("new buffer");
4388 mCapture = screenshot();
4389 Rect rect = Rect(74, 74, 84, 84);
4390 mCapture->expectBorder(rect, Color::RED);
4391 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4392 }
4393}
chaviw5aedec92018-10-22 10:40:38 -07004394
Robert Carr9b429f42017-04-17 14:56:57 -07004395TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004396 asTransaction([&](Transaction& t) {
4397 t.show(mChild);
4398 t.setPosition(mChild, 0, 0);
4399 t.setPosition(mFGSurfaceControl, 0, 0);
4400 });
Robert Carr9b429f42017-04-17 14:56:57 -07004401
4402 {
chaviw0e3479f2018-09-10 16:49:30 -07004403 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004404 // We've positioned the child in the top left.
4405 mCapture->expectChildColor(0, 0);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004406 // But it's only 10x15.
4407 mCapture->expectFGColor(10, 15);
Robert Carr9b429f42017-04-17 14:56:57 -07004408 }
4409
Robert Carr4cdc58f2017-08-23 14:22:20 -07004410 asTransaction([&](Transaction& t) {
4411 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4412 // We cause scaling by 2.
4413 t.setSize(mFGSurfaceControl, 128, 128);
4414 });
Robert Carr9b429f42017-04-17 14:56:57 -07004415
4416 {
chaviw0e3479f2018-09-10 16:49:30 -07004417 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004418 // We've positioned the child in the top left.
4419 mCapture->expectChildColor(0, 0);
4420 mCapture->expectChildColor(10, 10);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004421 mCapture->expectChildColor(19, 29);
4422 // And now it should be scaled all the way to 20x30
4423 mCapture->expectFGColor(20, 30);
Robert Carr9b429f42017-04-17 14:56:57 -07004424 }
4425}
4426
Robert Carr1725eee2017-04-26 18:32:15 -07004427// Regression test for b/37673612
4428TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004429 asTransaction([&](Transaction& t) {
4430 t.show(mChild);
4431 t.setPosition(mChild, 0, 0);
4432 t.setPosition(mFGSurfaceControl, 0, 0);
4433 });
Robert Carr1725eee2017-04-26 18:32:15 -07004434
4435 {
chaviw0e3479f2018-09-10 16:49:30 -07004436 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004437 // We've positioned the child in the top left.
4438 mCapture->expectChildColor(0, 0);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004439 mCapture->expectChildColor(9, 14);
4440 // But it's only 10x15.
4441 mCapture->expectFGColor(10, 15);
Robert Carr1725eee2017-04-26 18:32:15 -07004442 }
Robert Carr1725eee2017-04-26 18:32:15 -07004443 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
4444 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004445 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07004446 sp<Surface> s = mFGSurfaceControl->getSurface();
4447 auto anw = static_cast<ANativeWindow*>(s.get());
4448 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4449 native_window_set_buffers_dimensions(anw, 64, 128);
Valerie Haue271df92019-09-06 09:23:22 -07004450 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
Robert Carr1725eee2017-04-26 18:32:15 -07004451 waitForPostedBuffers();
4452
4453 {
4454 // The child should still be in the same place and not have any strange scaling as in
4455 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07004456 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004457 mCapture->expectChildColor(0, 0);
4458 mCapture->expectFGColor(10, 10);
4459 }
4460}
4461
Vishnu Nairc652ff82019-03-15 12:48:54 -07004462// A child with a buffer transform from its parents should be cropped by its parent bounds.
4463TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferTransform) {
4464 asTransaction([&](Transaction& t) {
4465 t.show(mChild);
4466 t.setPosition(mChild, 0, 0);
4467 t.setPosition(mFGSurfaceControl, 0, 0);
4468 t.setSize(mChild, 100, 100);
4469 });
Valerie Haue271df92019-09-06 09:23:22 -07004470 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004471
4472 {
4473 mCapture = screenshot();
4474
4475 mCapture->expectChildColor(0, 0);
4476 mCapture->expectChildColor(63, 63);
4477 mCapture->expectBGColor(64, 64);
4478 }
4479
4480 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
4481 sp<Surface> s = mFGSurfaceControl->getSurface();
4482 auto anw = static_cast<ANativeWindow*>(s.get());
4483 // Apply a 90 transform on the buffer.
4484 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4485 native_window_set_buffers_dimensions(anw, 64, 128);
Valerie Haue271df92019-09-06 09:23:22 -07004486 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004487 waitForPostedBuffers();
4488
4489 // The child should be cropped by the new parent bounds.
4490 {
4491 mCapture = screenshot();
4492 mCapture->expectChildColor(0, 0);
4493 mCapture->expectChildColor(99, 63);
4494 mCapture->expectFGColor(100, 63);
4495 mCapture->expectBGColor(128, 64);
4496 }
4497}
4498
4499// A child with a scale transform from its parents should be cropped by its parent bounds.
4500TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferScale) {
4501 asTransaction([&](Transaction& t) {
4502 t.show(mChild);
4503 t.setPosition(mChild, 0, 0);
4504 t.setPosition(mFGSurfaceControl, 0, 0);
4505 t.setSize(mChild, 200, 200);
4506 });
Valerie Haue271df92019-09-06 09:23:22 -07004507 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004508
4509 {
4510 mCapture = screenshot();
4511
4512 mCapture->expectChildColor(0, 0);
4513 mCapture->expectChildColor(63, 63);
4514 mCapture->expectBGColor(64, 64);
4515 }
4516
4517 asTransaction([&](Transaction& t) {
4518 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4519 // Set a scaling by 2.
4520 t.setSize(mFGSurfaceControl, 128, 128);
4521 });
4522
4523 // Child should inherit its parents scale but should be cropped by its parent bounds.
4524 {
4525 mCapture = screenshot();
4526 mCapture->expectChildColor(0, 0);
4527 mCapture->expectChildColor(127, 127);
4528 mCapture->expectBGColor(128, 128);
4529 }
4530}
4531
4532// Regression test for b/127368943
4533// Child should ignore the buffer transform but apply parent scale transform.
4534TEST_F(ChildLayerTest, ChildrenWithParentBufferTransformAndScale) {
4535 asTransaction([&](Transaction& t) {
4536 t.show(mChild);
4537 t.setPosition(mChild, 0, 0);
4538 t.setPosition(mFGSurfaceControl, 0, 0);
4539 });
4540
4541 {
4542 mCapture = screenshot();
4543 mCapture->expectChildColor(0, 0);
4544 mCapture->expectChildColor(9, 14);
4545 mCapture->expectFGColor(10, 15);
4546 }
4547
4548 // Change the size of the foreground to 128 * 64 so we can test rotation as well.
4549 asTransaction([&](Transaction& t) {
4550 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4551 t.setSize(mFGSurfaceControl, 128, 64);
4552 });
4553 sp<Surface> s = mFGSurfaceControl->getSurface();
4554 auto anw = static_cast<ANativeWindow*>(s.get());
4555 // Apply a 90 transform on the buffer and submit a buffer half the expected size so that we
4556 // have an effective scale of 2.0 applied to the buffer along with a rotation transform.
4557 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4558 native_window_set_buffers_dimensions(anw, 32, 64);
Valerie Haue271df92019-09-06 09:23:22 -07004559 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004560 waitForPostedBuffers();
4561
4562 // The child should ignore the buffer transform but apply the 2.0 scale from parent.
4563 {
4564 mCapture = screenshot();
4565 mCapture->expectChildColor(0, 0);
4566 mCapture->expectChildColor(19, 29);
4567 mCapture->expectFGColor(20, 30);
4568 }
4569}
4570
Dan Stoza412903f2017-04-27 13:42:17 -07004571TEST_F(ChildLayerTest, Bug36858924) {
4572 // Destroy the child layer
4573 mChild.clear();
4574
4575 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08004576 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
4577 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07004578
4579 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07004580 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004581 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
4582 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07004583 t.show(mChild);
4584 });
Dan Stoza412903f2017-04-27 13:42:17 -07004585
4586 // Render the foreground surface a few times
4587 //
4588 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
4589 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
4590 // never acquire/release the first buffer
4591 ALOGI("Filling 1");
Valerie Haue271df92019-09-06 09:23:22 -07004592 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
Dan Stoza412903f2017-04-27 13:42:17 -07004593 ALOGI("Filling 2");
Valerie Haue271df92019-09-06 09:23:22 -07004594 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
Dan Stoza412903f2017-04-27 13:42:17 -07004595 ALOGI("Filling 3");
Valerie Haue271df92019-09-06 09:23:22 -07004596 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
Dan Stoza412903f2017-04-27 13:42:17 -07004597 ALOGI("Filling 4");
Valerie Haue271df92019-09-06 09:23:22 -07004598 TransactionUtils::fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
Dan Stoza412903f2017-04-27 13:42:17 -07004599}
4600
chaviwf1961f72017-09-18 16:41:07 -07004601TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004602 asTransaction([&](Transaction& t) {
4603 t.show(mChild);
4604 t.setPosition(mChild, 10, 10);
4605 t.setPosition(mFGSurfaceControl, 64, 64);
4606 });
chaviw06178942017-07-27 10:25:59 -07004607
4608 {
chaviw0e3479f2018-09-10 16:49:30 -07004609 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004610 // Top left of foreground must now be visible
4611 mCapture->expectFGColor(64, 64);
4612 // But 10 pixels in we should see the child surface
4613 mCapture->expectChildColor(74, 74);
4614 // And 10 more pixels we should be back to the foreground surface
4615 mCapture->expectFGColor(84, 84);
4616 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004617
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004618 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07004619
chaviw06178942017-07-27 10:25:59 -07004620 {
chaviw0e3479f2018-09-10 16:49:30 -07004621 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004622 mCapture->expectFGColor(64, 64);
4623 // In reparenting we should have exposed the entire foreground surface.
4624 mCapture->expectFGColor(74, 74);
4625 // And the child layer should now begin at 10, 10 (since the BG
4626 // layer is at (0, 0)).
4627 mCapture->expectBGColor(9, 9);
4628 mCapture->expectChildColor(10, 10);
4629 }
4630}
4631
chaviwf1961f72017-09-18 16:41:07 -07004632TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004633 asTransaction([&](Transaction& t) {
4634 t.show(mChild);
4635 t.setPosition(mChild, 10, 10);
4636 t.setPosition(mFGSurfaceControl, 64, 64);
4637 });
chaviwf1961f72017-09-18 16:41:07 -07004638
4639 {
chaviw0e3479f2018-09-10 16:49:30 -07004640 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004641 // Top left of foreground must now be visible
4642 mCapture->expectFGColor(64, 64);
4643 // But 10 pixels in we should see the child surface
4644 mCapture->expectChildColor(74, 74);
4645 // And 10 more pixels we should be back to the foreground surface
4646 mCapture->expectFGColor(84, 84);
4647 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004648 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07004649 {
chaviw0e3479f2018-09-10 16:49:30 -07004650 mCapture = screenshot();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004651 // The surface should now be offscreen.
chaviwf1961f72017-09-18 16:41:07 -07004652 mCapture->expectFGColor(64, 64);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004653 mCapture->expectFGColor(74, 74);
chaviwf1961f72017-09-18 16:41:07 -07004654 mCapture->expectFGColor(84, 84);
4655 }
4656}
4657
4658TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07004659 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08004660 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07004661 ASSERT_TRUE(newSurface->isValid());
4662
Valerie Haue271df92019-09-06 09:23:22 -07004663 TransactionUtils::fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004664 asTransaction([&](Transaction& t) {
4665 t.hide(mChild);
4666 t.show(newSurface);
4667 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004668 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004669 t.setPosition(mFGSurfaceControl, 64, 64);
4670 });
chaviwf1961f72017-09-18 16:41:07 -07004671
4672 {
chaviw0e3479f2018-09-10 16:49:30 -07004673 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004674 // Top left of foreground must now be visible
4675 mCapture->expectFGColor(64, 64);
4676 // At 10, 10 we should see the new surface
4677 mCapture->checkPixel(10, 10, 63, 195, 63);
4678 }
4679
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004680 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07004681
4682 {
chaviw0e3479f2018-09-10 16:49:30 -07004683 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004684 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
4685 // mFGSurface, putting it at 74, 74.
4686 mCapture->expectFGColor(64, 64);
4687 mCapture->checkPixel(74, 74, 63, 195, 63);
4688 mCapture->expectFGColor(84, 84);
4689 }
4690}
4691
chaviwc9674332017-08-28 12:32:18 -07004692TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004693 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
4694 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Valerie Haue271df92019-09-06 09:23:22 -07004695 TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
chaviwc9674332017-08-28 12:32:18 -07004696
4697 {
chaviw0e3479f2018-09-10 16:49:30 -07004698 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07004699 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
4700 // which begins at 64, 64
4701 mCapture->checkPixel(64, 64, 50, 50, 50);
4702 }
4703}
4704
Robert Carr503c7042017-09-27 15:06:08 -07004705TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004706 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Valerie Haue271df92019-09-06 09:23:22 -07004707 TransactionUtils::fillSurfaceRGBA8(relative, 255, 255, 255);
Robert Carr503c7042017-09-27 15:06:08 -07004708
4709 Transaction t;
4710 t.setLayer(relative, INT32_MAX)
4711 .setRelativeLayer(mChild, relative->getHandle(), 1)
4712 .setPosition(mFGSurfaceControl, 0, 0)
4713 .apply(true);
4714
4715 // We expect that the child should have been elevated above our
4716 // INT_MAX layer even though it's not a child of it.
4717 {
chaviw0e3479f2018-09-10 16:49:30 -07004718 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07004719 mCapture->expectChildColor(0, 0);
4720 mCapture->expectChildColor(9, 9);
4721 mCapture->checkPixel(10, 10, 255, 255, 255);
4722 }
4723}
Vishnu Nairc652ff82019-03-15 12:48:54 -07004724
Vishnu Nair60356342018-11-13 13:00:45 -08004725class BoundlessLayerTest : public LayerUpdateTest {
4726protected:
4727 std::unique_ptr<ScreenCapture> mCapture;
4728};
4729
4730// Verify setting a size on a buffer layer has no effect.
4731TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
4732 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004733 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
4734 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004735 ASSERT_TRUE(bufferLayer->isValid());
4736 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
4737 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
4738 {
4739 mCapture = screenshot();
4740 // Top left of background must now be visible
4741 mCapture->expectBGColor(0, 0);
4742 // Foreground Surface bounds must be color layer
4743 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
4744 // Buffer layer should not extend past buffer bounds
4745 mCapture->expectFGColor(95, 95);
4746 }
4747}
4748
4749// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
4750// which will crop the color layer.
4751TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
4752 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004753 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4754 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004755 ASSERT_TRUE(colorLayer->isValid());
4756 asTransaction([&](Transaction& t) {
4757 t.setColor(colorLayer, half3{0, 0, 0});
4758 t.show(colorLayer);
4759 });
4760 {
4761 mCapture = screenshot();
4762 // Top left of background must now be visible
4763 mCapture->expectBGColor(0, 0);
4764 // Foreground Surface bounds must be color layer
4765 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4766 // Color layer should not extend past foreground bounds
4767 mCapture->expectBGColor(129, 129);
4768 }
4769}
4770
4771// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
4772// a crop which will be used to crop the color layer.
4773TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004774 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4775 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004776 ASSERT_TRUE(cropLayer->isValid());
4777 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004778 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4779 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004780 ASSERT_TRUE(colorLayer->isValid());
4781 asTransaction([&](Transaction& t) {
4782 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
4783 t.setColor(colorLayer, half3{0, 0, 0});
4784 t.show(cropLayer);
4785 t.show(colorLayer);
4786 });
4787 {
4788 mCapture = screenshot();
4789 // Top left of background must now be visible
4790 mCapture->expectBGColor(0, 0);
4791 // Top left of foreground must now be visible
4792 mCapture->expectFGColor(64, 64);
4793 // 5 pixels from the foreground we should see the child surface
4794 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
4795 // 10 pixels from the foreground we should be back to the foreground surface
4796 mCapture->expectFGColor(74, 74);
4797 }
4798}
4799
4800// Verify for boundless layer with no children, their transforms have no effect.
4801TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
4802 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004803 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4804 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004805 ASSERT_TRUE(colorLayer->isValid());
4806 asTransaction([&](Transaction& t) {
4807 t.setPosition(colorLayer, 320, 320);
4808 t.setMatrix(colorLayer, 2, 0, 0, 2);
4809 t.setColor(colorLayer, half3{0, 0, 0});
4810 t.show(colorLayer);
4811 });
4812 {
4813 mCapture = screenshot();
4814 // Top left of background must now be visible
4815 mCapture->expectBGColor(0, 0);
4816 // Foreground Surface bounds must be color layer
4817 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4818 // Color layer should not extend past foreground bounds
4819 mCapture->expectBGColor(129, 129);
4820 }
4821}
4822
4823// Verify for boundless layer with children, their transforms have an effect.
4824TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
4825 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004826 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4827 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004828 ASSERT_TRUE(boundlessLayerRightShift->isValid());
4829 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004830 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4831 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004832 ASSERT_TRUE(boundlessLayerDownShift->isValid());
4833 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004834 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4835 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004836 ASSERT_TRUE(colorLayer->isValid());
4837 asTransaction([&](Transaction& t) {
4838 t.setPosition(boundlessLayerRightShift, 32, 0);
4839 t.show(boundlessLayerRightShift);
4840 t.setPosition(boundlessLayerDownShift, 0, 32);
4841 t.show(boundlessLayerDownShift);
4842 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4843 t.setColor(colorLayer, half3{0, 0, 0});
4844 t.show(colorLayer);
4845 });
4846 {
4847 mCapture = screenshot();
4848 // Top left of background must now be visible
4849 mCapture->expectBGColor(0, 0);
4850 // Top left of foreground must now be visible
4851 mCapture->expectFGColor(64, 64);
4852 // Foreground Surface bounds must be color layer
4853 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
4854 // Color layer should not extend past foreground bounds
4855 mCapture->expectBGColor(129, 129);
4856 }
4857}
4858
4859// Verify child layers do not get clipped if they temporarily move into the negative
4860// coordinate space as the result of an intermediate transformation.
4861TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
4862 sp<SurfaceControl> boundlessLayer =
4863 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4864 0 /* flags */, mFGSurfaceControl.get());
4865 ASSERT_TRUE(boundlessLayer != nullptr);
4866 ASSERT_TRUE(boundlessLayer->isValid());
4867 sp<SurfaceControl> colorLayer =
4868 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4869 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
4870 ASSERT_TRUE(colorLayer != nullptr);
4871 ASSERT_TRUE(colorLayer->isValid());
4872 asTransaction([&](Transaction& t) {
4873 // shift child layer off bounds. If this layer was not boundless, we will
4874 // expect the child layer to be cropped.
4875 t.setPosition(boundlessLayer, 32, 32);
4876 t.show(boundlessLayer);
4877 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4878 // undo shift by parent
4879 t.setPosition(colorLayer, -32, -32);
4880 t.setColor(colorLayer, half3{0, 0, 0});
4881 t.show(colorLayer);
4882 });
4883 {
4884 mCapture = screenshot();
4885 // Top left of background must now be visible
4886 mCapture->expectBGColor(0, 0);
4887 // Foreground Surface bounds must be color layer
4888 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4889 // Color layer should not extend past foreground bounds
4890 mCapture->expectBGColor(129, 129);
4891 }
4892}
4893
4894// Verify for boundless root layers with children, their transforms have an effect.
4895TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004896 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
4897 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08004898 ASSERT_TRUE(rootBoundlessLayer->isValid());
4899 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004900 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4901 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
4902
Vishnu Nair60356342018-11-13 13:00:45 -08004903 ASSERT_TRUE(colorLayer->isValid());
4904 asTransaction([&](Transaction& t) {
4905 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
4906 t.setPosition(rootBoundlessLayer, 32, 32);
4907 t.show(rootBoundlessLayer);
4908 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4909 t.setColor(colorLayer, half3{0, 0, 0});
4910 t.show(colorLayer);
4911 t.hide(mFGSurfaceControl);
4912 });
4913 {
4914 mCapture = screenshot();
4915 // Top left of background must now be visible
4916 mCapture->expectBGColor(0, 0);
4917 // Top left of foreground must now be visible
4918 mCapture->expectBGColor(31, 31);
4919 // Foreground Surface bounds must be color layer
4920 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
4921 // Color layer should not extend past foreground bounds
4922 mCapture->expectBGColor(97, 97);
4923 }
4924}
Robert Carr503c7042017-09-27 15:06:08 -07004925
chaviwa76b2712017-09-20 12:02:26 -07004926class ScreenCaptureTest : public LayerUpdateTest {
4927protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004928 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07004929};
4930
4931TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
4932 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004933 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004934 mCapture->expectBGColor(0, 0);
4935 // Doesn't capture FG layer which is at 64, 64
4936 mCapture->expectBGColor(64, 64);
4937}
4938
4939TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
4940 auto fgHandle = mFGSurfaceControl->getHandle();
4941
Vishnu Nair88a11f22018-11-28 18:30:57 -08004942 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4943 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004944 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviwa76b2712017-09-20 12:02:26 -07004945
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004946 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004947
4948 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004949 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004950 mCapture->expectFGColor(10, 10);
4951 mCapture->expectChildColor(0, 0);
4952}
4953
Robert Carr578038f2018-03-09 12:25:24 -08004954TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
4955 auto fgHandle = mFGSurfaceControl->getHandle();
4956
Vishnu Nair88a11f22018-11-28 18:30:57 -08004957 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4958 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004959 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
Robert Carr578038f2018-03-09 12:25:24 -08004960
4961 SurfaceComposerClient::Transaction().show(child).apply(true);
4962
4963 // Captures mFGSurfaceControl's child
4964 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4965 mCapture->checkPixel(10, 10, 0, 0, 0);
4966 mCapture->expectChildColor(0, 0);
4967}
4968
Robert Carr866455f2019-04-02 16:28:26 -07004969TEST_F(ScreenCaptureTest, CaptureLayerExclude) {
4970 auto fgHandle = mFGSurfaceControl->getHandle();
4971
4972 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4973 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004974 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
Robert Carr866455f2019-04-02 16:28:26 -07004975 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
4976 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004977 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
Robert Carr866455f2019-04-02 16:28:26 -07004978
4979 SurfaceComposerClient::Transaction()
4980 .show(child)
4981 .show(child2)
4982 .setLayer(child, 1)
4983 .setLayer(child2, 2)
4984 .apply(true);
4985
4986 // Child2 would be visible but its excluded, so we should see child1 color instead.
4987 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
4988 mCapture->checkPixel(10, 10, 0, 0, 0);
4989 mCapture->checkPixel(0, 0, 200, 200, 200);
4990}
4991
4992// Like the last test but verifies that children are also exclude.
4993TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) {
4994 auto fgHandle = mFGSurfaceControl->getHandle();
4995
4996 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4997 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07004998 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
Robert Carr866455f2019-04-02 16:28:26 -07004999 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
5000 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07005001 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
Robert Carr866455f2019-04-02 16:28:26 -07005002 sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10,
5003 PIXEL_FORMAT_RGBA_8888, 0, child2.get());
Valerie Haue271df92019-09-06 09:23:22 -07005004 TransactionUtils::fillSurfaceRGBA8(child2, 200, 0, 200);
Robert Carr866455f2019-04-02 16:28:26 -07005005
5006 SurfaceComposerClient::Transaction()
5007 .show(child)
5008 .show(child2)
5009 .show(child3)
5010 .setLayer(child, 1)
5011 .setLayer(child2, 2)
5012 .apply(true);
5013
5014 // Child2 would be visible but its excluded, so we should see child1 color instead.
5015 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
5016 mCapture->checkPixel(10, 10, 0, 0, 0);
5017 mCapture->checkPixel(0, 0, 200, 200, 200);
5018}
5019
chaviw50da5042018-04-09 13:49:37 -07005020TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005021 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5022 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07005023
Valerie Haue271df92019-09-06 09:23:22 -07005024 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviw50da5042018-04-09 13:49:37 -07005025
5026 SurfaceComposerClient::Transaction().show(child).apply(true);
5027
5028 auto childHandle = child->getHandle();
5029
5030 // Captures child
5031 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
5032 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
5033 // Area outside of child's bounds is transparent.
5034 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
5035}
5036
chaviw4b129c22018-04-09 16:19:43 -07005037TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
5038 auto fgHandle = mFGSurfaceControl->getHandle();
5039
Vishnu Nair88a11f22018-11-28 18:30:57 -08005040 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5041 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5042 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07005043 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
Valerie Haue271df92019-09-06 09:23:22 -07005044 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
5045 TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100);
chaviw4b129c22018-04-09 16:19:43 -07005046
5047 SurfaceComposerClient::Transaction()
5048 .show(child)
5049 // Set relative layer above fg layer so should be shown above when computing all layers.
5050 .setRelativeLayer(relative, fgHandle, 1)
5051 .show(relative)
5052 .apply(true);
5053
5054 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
5055 ScreenCapture::captureLayers(&mCapture, fgHandle);
5056 mCapture->expectFGColor(10, 10);
5057 mCapture->expectChildColor(0, 0);
5058}
5059
5060TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
5061 auto fgHandle = mFGSurfaceControl->getHandle();
5062
Vishnu Nair88a11f22018-11-28 18:30:57 -08005063 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5064 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5065 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
5066 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07005067 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
5068 TransactionUtils::fillSurfaceRGBA8(relative, 100, 100, 100);
chaviw4b129c22018-04-09 16:19:43 -07005069
5070 SurfaceComposerClient::Transaction()
5071 .show(child)
5072 // Set relative layer below fg layer but relative to child layer so it should be shown
5073 // above child layer.
5074 .setLayer(relative, -1)
5075 .setRelativeLayer(relative, child->getHandle(), 1)
5076 .show(relative)
5077 .apply(true);
5078
5079 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
5080 // relative value should be taken into account, placing it above child layer.
5081 ScreenCapture::captureLayers(&mCapture, fgHandle);
5082 mCapture->expectFGColor(10, 10);
5083 // Relative layer is showing on top of child layer
5084 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
5085}
Robert Carr578038f2018-03-09 12:25:24 -08005086
5087// In the following tests we verify successful skipping of a parent layer,
5088// so we use the same verification logic and only change how we mutate
5089// the parent layer to verify that various properties are ignored.
5090class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
5091public:
5092 void SetUp() override {
5093 LayerUpdateTest::SetUp();
5094
Vishnu Nair88a11f22018-11-28 18:30:57 -08005095 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
5096 mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07005097 TransactionUtils::fillSurfaceRGBA8(mChild, 200, 200, 200);
Robert Carr578038f2018-03-09 12:25:24 -08005098
5099 SurfaceComposerClient::Transaction().show(mChild).apply(true);
5100 }
5101
Vishnu Nair333a9572019-02-15 16:05:56 -08005102 void verify(std::function<void()> verifyStartingState) {
5103 // Verify starting state before a screenshot is taken.
5104 verifyStartingState();
5105
5106 // Verify child layer does not inherit any of the properties of its
5107 // parent when its screenshot is captured.
Robert Carr578038f2018-03-09 12:25:24 -08005108 auto fgHandle = mFGSurfaceControl->getHandle();
5109 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
5110 mCapture->checkPixel(10, 10, 0, 0, 0);
5111 mCapture->expectChildColor(0, 0);
Vishnu Nair333a9572019-02-15 16:05:56 -08005112
5113 // Verify all assumptions are still true after the screenshot is taken.
5114 verifyStartingState();
Robert Carr578038f2018-03-09 12:25:24 -08005115 }
5116
5117 std::unique_ptr<ScreenCapture> mCapture;
5118 sp<SurfaceControl> mChild;
5119};
5120
Vishnu Nair333a9572019-02-15 16:05:56 -08005121// Regression test b/76099859
Robert Carr578038f2018-03-09 12:25:24 -08005122TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
5123
5124 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
5125
5126 // Even though the parent is hidden we should still capture the child.
Vishnu Nair333a9572019-02-15 16:05:56 -08005127
5128 // Before and after reparenting, verify child is properly hidden
5129 // when rendering full-screen.
5130 verify([&] { screenshot()->expectBGColor(64, 64); });
Robert Carr578038f2018-03-09 12:25:24 -08005131}
5132
5133TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07005134 SurfaceComposerClient::Transaction()
5135 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
5136 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005137
5138 // Even though the parent is cropped out we should still capture the child.
Vishnu Nair333a9572019-02-15 16:05:56 -08005139
5140 // Before and after reparenting, verify child is cropped by parent.
5141 verify([&] { screenshot()->expectBGColor(65, 65); });
Robert Carr578038f2018-03-09 12:25:24 -08005142}
5143
Vishnu Nair333a9572019-02-15 16:05:56 -08005144// Regression test b/124372894
Robert Carr578038f2018-03-09 12:25:24 -08005145TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
Vishnu Nair333a9572019-02-15 16:05:56 -08005146 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005147
5148 // We should not inherit the parent scaling.
Robert Carr578038f2018-03-09 12:25:24 -08005149
Vishnu Nair333a9572019-02-15 16:05:56 -08005150 // Before and after reparenting, verify child is properly scaled.
5151 verify([&] { screenshot()->expectChildColor(80, 80); });
Robert Carr15eae092018-03-23 13:43:53 -07005152}
5153
5154
chaviwa76b2712017-09-20 12:02:26 -07005155TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
5156 auto fgHandle = mFGSurfaceControl->getHandle();
5157
Vishnu Nair88a11f22018-11-28 18:30:57 -08005158 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5159 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07005160 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviwa76b2712017-09-20 12:02:26 -07005161
Vishnu Nair88a11f22018-11-28 18:30:57 -08005162 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5163 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07005164
Valerie Haue271df92019-09-06 09:23:22 -07005165 TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
chaviwa76b2712017-09-20 12:02:26 -07005166 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005167 .show(child)
5168 .setPosition(grandchild, 5, 5)
5169 .show(grandchild)
5170 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005171
5172 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005173 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07005174 mCapture->expectFGColor(10, 10);
5175 mCapture->expectChildColor(0, 0);
5176 mCapture->checkPixel(5, 5, 50, 50, 50);
5177}
5178
5179TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005180 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5181 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07005182 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviwa76b2712017-09-20 12:02:26 -07005183 auto childHandle = child->getHandle();
5184
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005185 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005186
5187 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005188 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07005189 mCapture->expectChildColor(0, 0);
5190 mCapture->expectChildColor(9, 9);
5191}
5192
5193TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005194 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5195 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Valerie Haue271df92019-09-06 09:23:22 -07005196 TransactionUtils::fillSurfaceRGBA8(child, 200, 200, 200);
chaviwa76b2712017-09-20 12:02:26 -07005197 auto childHandle = child->getHandle();
5198
Vishnu Nair88a11f22018-11-28 18:30:57 -08005199 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5200 PIXEL_FORMAT_RGBA_8888, 0, child.get());
Valerie Haue271df92019-09-06 09:23:22 -07005201 TransactionUtils::fillSurfaceRGBA8(grandchild, 50, 50, 50);
chaviwa76b2712017-09-20 12:02:26 -07005202
5203 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005204 .show(child)
5205 .setPosition(grandchild, 5, 5)
5206 .show(grandchild)
5207 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005208
5209 auto grandchildHandle = grandchild->getHandle();
5210
5211 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005212 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07005213 mCapture->checkPixel(0, 0, 50, 50, 50);
5214 mCapture->checkPixel(4, 4, 50, 50, 50);
5215}
5216
chaviw7206d492017-11-10 16:16:12 -08005217TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07005218 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005219 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5220 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005221
Marissa Wall61c58622018-07-18 10:12:20 -07005222 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5223 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005224
5225 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005226 .setLayer(redLayer, INT32_MAX - 1)
5227 .show(redLayer)
5228 .show(blueLayer)
5229 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005230
5231 auto redLayerHandle = redLayer->getHandle();
5232
5233 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005234 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5235 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5236 // red area below the blue area
5237 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5238 // red area to the right of the blue area
5239 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005240
Marissa Wall861616d2018-10-22 12:52:23 -07005241 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005242 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08005243 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
5244 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005245 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08005246 mCapture->checkPixel(30, 30, 0, 0, 0);
5247}
5248
5249TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07005250 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005251 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5252 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005253
Marissa Wall61c58622018-07-18 10:12:20 -07005254 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5255 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005256
5257 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005258 .setLayer(redLayer, INT32_MAX - 1)
5259 .show(redLayer)
5260 .show(blueLayer)
5261 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005262
5263 auto redLayerHandle = redLayer->getHandle();
5264
5265 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005266 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5267 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5268 // red area below the blue area
5269 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5270 // red area to the right of the blue area
5271 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005272
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005273 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08005274 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005275 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
5276 // red area below the blue area
5277 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
5278 // red area to the right of the blue area
5279 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005280 mCapture->checkPixel(30, 30, 0, 0, 0);
5281}
5282
5283TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07005284 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08005285
Marissa Wall61c58622018-07-18 10:12:20 -07005286 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08005287
5288 auto redLayerHandle = redLayer->getHandle();
Robert Carr87246532019-02-04 15:20:26 -08005289 redLayer.clear();
chaviw7206d492017-11-10 16:16:12 -08005290 SurfaceComposerClient::Transaction().apply(true);
5291
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005292 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08005293
5294 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005295 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
5296 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08005297}
5298
chaviw8e3fe5d2018-02-22 10:55:42 -08005299
5300class DereferenceSurfaceControlTest : public LayerTransactionTest {
5301protected:
5302 void SetUp() override {
5303 LayerTransactionTest::SetUp();
5304 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005305 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005306 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005307 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005308 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
5309 {
5310 SCOPED_TRACE("before anything");
5311 auto shot = screenshot();
5312 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5313 }
5314 }
5315 void TearDown() override {
5316 LayerTransactionTest::TearDown();
5317 bgLayer = 0;
5318 fgLayer = 0;
5319 }
5320
5321 sp<SurfaceControl> bgLayer;
5322 sp<SurfaceControl> fgLayer;
5323};
5324
5325TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
5326 fgLayer = nullptr;
5327 {
5328 SCOPED_TRACE("after setting null");
5329 auto shot = screenshot();
5330 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
5331 }
5332}
5333
5334TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
5335 auto transaction = Transaction().show(fgLayer);
5336 fgLayer = nullptr;
5337 {
5338 SCOPED_TRACE("after setting null");
5339 auto shot = screenshot();
5340 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5341 }
5342}
5343
Vishnu Nairb927e1f2019-02-19 13:36:15 -08005344class MultiDisplayLayerBoundsTest : public LayerTransactionTest {
5345protected:
5346 virtual void SetUp() {
5347 LayerTransactionTest::SetUp();
5348 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5349
5350 mMainDisplay = SurfaceComposerClient::getInternalDisplayToken();
5351 SurfaceComposerClient::getDisplayInfo(mMainDisplay, &mMainDisplayInfo);
5352
5353 sp<IGraphicBufferConsumer> consumer;
5354 BufferQueue::createBufferQueue(&mProducer, &consumer);
5355 consumer->setConsumerName(String8("Virtual disp consumer"));
5356 consumer->setDefaultBufferSize(mMainDisplayInfo.w, mMainDisplayInfo.h);
5357 }
5358
5359 virtual void TearDown() {
5360 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
5361 LayerTransactionTest::TearDown();
5362 mColorLayer = 0;
5363 }
5364
5365 void createDisplay(const Rect& layerStackRect, uint32_t layerStack) {
5366 mVirtualDisplay =
5367 SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), false /*secure*/);
5368 asTransaction([&](Transaction& t) {
5369 t.setDisplaySurface(mVirtualDisplay, mProducer);
5370 t.setDisplayLayerStack(mVirtualDisplay, layerStack);
5371 t.setDisplayProjection(mVirtualDisplay, mMainDisplayInfo.orientation, layerStackRect,
5372 Rect(mMainDisplayInfo.w, mMainDisplayInfo.h));
5373 });
5374 }
5375
5376 void createColorLayer(uint32_t layerStack) {
5377 mColorLayer =
5378 createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */,
5379 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
5380 ASSERT_TRUE(mColorLayer != nullptr);
5381 ASSERT_TRUE(mColorLayer->isValid());
5382 asTransaction([&](Transaction& t) {
5383 t.setLayerStack(mColorLayer, layerStack);
5384 t.setCrop_legacy(mColorLayer, Rect(0, 0, 30, 40));
5385 t.setLayer(mColorLayer, INT32_MAX - 2);
5386 t.setColor(mColorLayer,
5387 half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f,
5388 mExpectedColor.b / 255.0f});
5389 t.show(mColorLayer);
5390 });
5391 }
5392
5393 DisplayInfo mMainDisplayInfo;
5394 sp<IBinder> mMainDisplay;
5395 sp<IBinder> mVirtualDisplay;
5396 sp<IGraphicBufferProducer> mProducer;
5397 sp<SurfaceControl> mColorLayer;
5398 Color mExpectedColor = {63, 63, 195, 255};
5399};
5400
5401TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) {
5402 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 1 /* layerStack */);
5403 createColorLayer(1 /* layerStack */);
5404
5405 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5406
5407 // Verify color layer does not render on main display.
5408 std::unique_ptr<ScreenCapture> sc;
5409 ScreenCapture::captureScreen(&sc, mMainDisplay);
5410 sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255});
5411 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5412
5413 // Verify color layer renders correctly on virtual display.
5414 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5415 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5416 sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 0});
5417}
5418
5419TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) {
5420 // Create a display and set its layer stack to the main display's layer stack so
5421 // the contents of the main display are mirrored on to the virtual display.
5422
5423 // Assumption here is that the new mirrored display has the same viewport as the
5424 // primary display that it is mirroring.
5425 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 0 /* layerStack */);
5426 createColorLayer(0 /* layerStack */);
5427
5428 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5429
5430 // Verify color layer renders correctly on main display and it is mirrored on the
5431 // virtual display.
5432 std::unique_ptr<ScreenCapture> sc;
5433 ScreenCapture::captureScreen(&sc, mMainDisplay);
5434 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5435 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5436
5437 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5438 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5439 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5440}
5441
Ady Abrahamdf9df4a2019-03-12 17:32:05 -07005442class DisplayActiveConfigTest : public ::testing::Test {
5443protected:
5444 void SetUp() override {
5445 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
5446 SurfaceComposerClient::getDisplayConfigs(mDisplayToken, &mDisplayconfigs);
5447 EXPECT_GT(mDisplayconfigs.size(), 0);
5448
5449 // set display power to on to make sure config can be changed
5450 SurfaceComposerClient::setDisplayPowerMode(mDisplayToken, HWC_POWER_MODE_NORMAL);
5451 }
5452
5453 sp<IBinder> mDisplayToken;
5454 Vector<DisplayInfo> mDisplayconfigs;
5455};
5456
5457TEST_F(DisplayActiveConfigTest, allConfigsAllowed) {
5458 std::vector<int32_t> allowedConfigs;
5459
5460 // Add all configs to the allowed configs
5461 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5462 allowedConfigs.push_back(i);
5463 }
5464
5465 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5466 EXPECT_EQ(res, NO_ERROR);
5467
5468 std::vector<int32_t> outConfigs;
5469 res = SurfaceComposerClient::getAllowedDisplayConfigs(mDisplayToken, &outConfigs);
5470 EXPECT_EQ(res, NO_ERROR);
5471 EXPECT_EQ(allowedConfigs, outConfigs);
5472}
5473
5474TEST_F(DisplayActiveConfigTest, changeAllowedConfig) {
5475 // we need at least 2 configs available for this test
5476 if (mDisplayconfigs.size() <= 1) return;
5477
5478 int activeConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5479
5480 // We want to set the allowed config to everything but the active config
5481 std::vector<int32_t> allowedConfigs;
5482 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5483 if (i != activeConfig) {
5484 allowedConfigs.push_back(i);
5485 }
5486 }
5487
5488 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5489 EXPECT_EQ(res, NO_ERROR);
5490
5491 // Allow some time for the config change
5492 std::this_thread::sleep_for(200ms);
5493
5494 int newActiveConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5495 EXPECT_NE(activeConfig, newActiveConfig);
5496
5497 // Make sure the new config is part of allowed config
5498 EXPECT_TRUE(std::find(allowedConfigs.begin(), allowedConfigs.end(), newActiveConfig) !=
5499 allowedConfigs.end());
5500}
5501
Vishnu Nairda9c85a2019-06-03 17:26:48 -07005502class RelativeZTest : public LayerTransactionTest {
5503protected:
5504 virtual void SetUp() {
5505 LayerTransactionTest::SetUp();
5506 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5507
5508 const auto display = SurfaceComposerClient::getInternalDisplayToken();
5509 ASSERT_FALSE(display == nullptr);
5510
5511 // Back layer
5512 mBackgroundLayer = createColorLayer("Background layer", Color::RED);
5513
5514 // Front layer
5515 mForegroundLayer = createColorLayer("Foreground layer", Color::GREEN);
5516
5517 asTransaction([&](Transaction& t) {
5518 t.setDisplayLayerStack(display, 0);
5519 t.setLayer(mBackgroundLayer, INT32_MAX - 2).show(mBackgroundLayer);
5520 t.setLayer(mForegroundLayer, INT32_MAX - 1).show(mForegroundLayer);
5521 });
5522 }
5523
5524 virtual void TearDown() {
5525 LayerTransactionTest::TearDown();
5526 mBackgroundLayer = 0;
5527 mForegroundLayer = 0;
5528 }
5529
5530 sp<SurfaceControl> mBackgroundLayer;
5531 sp<SurfaceControl> mForegroundLayer;
5532};
5533
5534// When a layer is reparented offscreen, remove relative z order if the relative parent
5535// is still onscreen so that the layer is not drawn.
5536TEST_F(RelativeZTest, LayerRemoved) {
5537 std::unique_ptr<ScreenCapture> sc;
5538
5539 // Background layer (RED)
5540 // Child layer (WHITE) (relative to foregroud layer)
5541 // Foregroud layer (GREEN)
5542 sp<SurfaceControl> childLayer =
5543 createColorLayer("Child layer", Color::BLUE, mBackgroundLayer.get());
5544
5545 Transaction{}
5546 .setRelativeLayer(childLayer, mForegroundLayer->getHandle(), 1)
5547 .show(childLayer)
5548 .apply();
5549
5550 {
5551 // The childLayer should be in front of the FG control.
5552 ScreenCapture::captureScreen(&sc);
5553 sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b);
5554 }
5555
5556 // Background layer (RED)
5557 // Foregroud layer (GREEN)
5558 Transaction{}.reparent(childLayer, nullptr).apply();
5559
5560 // Background layer (RED)
5561 // Child layer (WHITE)
5562 // Foregroud layer (GREEN)
5563 Transaction{}.reparent(childLayer, mBackgroundLayer->getHandle()).apply();
5564
5565 {
5566 // The relative z info for child layer should be reset, leaving FG control on top.
5567 ScreenCapture::captureScreen(&sc);
5568 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5569 }
5570}
5571
5572// When a layer is reparented offscreen, preseve relative z order if the relative parent
5573// is also offscreen. Regression test b/132613412
5574TEST_F(RelativeZTest, LayerRemovedOffscreenRelativeParent) {
5575 std::unique_ptr<ScreenCapture> sc;
5576
5577 // Background layer (RED)
5578 // Foregroud layer (GREEN)
5579 // child level 1 (WHITE)
5580 // child level 2a (BLUE)
5581 // child level 3 (GREEN) (relative to child level 2b)
5582 // child level 2b (BLACK)
5583 sp<SurfaceControl> childLevel1 =
5584 createColorLayer("child level 1", Color::WHITE, mForegroundLayer.get());
5585 sp<SurfaceControl> childLevel2a =
5586 createColorLayer("child level 2a", Color::BLUE, childLevel1.get());
5587 sp<SurfaceControl> childLevel2b =
5588 createColorLayer("child level 2b", Color::BLACK, childLevel1.get());
5589 sp<SurfaceControl> childLevel3 =
5590 createColorLayer("child level 3", Color::GREEN, childLevel2a.get());
5591
5592 Transaction{}
5593 .setRelativeLayer(childLevel3, childLevel2b->getHandle(), 1)
5594 .show(childLevel2a)
5595 .show(childLevel2b)
5596 .show(childLevel3)
5597 .apply();
5598
5599 {
5600 // The childLevel3 should be in front of childLevel2b.
5601 ScreenCapture::captureScreen(&sc);
5602 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5603 }
5604
5605 // Background layer (RED)
5606 // Foregroud layer (GREEN)
5607 Transaction{}.reparent(childLevel1, nullptr).apply();
5608
5609 // Background layer (RED)
5610 // Foregroud layer (GREEN)
5611 // child level 1 (WHITE)
5612 // child level 2 back (BLUE)
5613 // child level 3 (GREEN) (relative to child level 2b)
5614 // child level 2 front (BLACK)
5615 Transaction{}.reparent(childLevel1, mForegroundLayer->getHandle()).apply();
5616
5617 {
5618 // Nothing should change at this point since relative z info was preserved.
5619 ScreenCapture::captureScreen(&sc);
5620 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5621 }
5622}
5623
Steven Thomas44685cb2019-07-23 16:19:31 -07005624// This test ensures that when we drop an app buffer in SurfaceFlinger, we merge
5625// the dropped buffer's damage region into the next buffer's damage region. If
5626// we don't do this, we'll report an incorrect damage region to hardware
5627// composer, resulting in broken rendering. This test checks the BufferQueue
5628// case.
5629//
5630// Unfortunately, we don't currently have a way to inspect the damage region
5631// SurfaceFlinger sends to hardware composer from a test, so this test requires
5632// the dev to manually watch the device's screen during the test to spot broken
5633// rendering. Because the results can't be automatically verified, this test is
5634// marked disabled.
5635TEST_F(LayerTransactionTest, DISABLED_BufferQueueLayerMergeDamageRegionWhenDroppingBuffers) {
5636 const int width = mDisplayWidth;
5637 const int height = mDisplayHeight;
5638 sp<SurfaceControl> layer;
5639 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
5640 const auto producer = layer->getIGraphicBufferProducer();
5641 const sp<IProducerListener> dummyListener(new DummyProducerListener);
5642 IGraphicBufferProducer::QueueBufferOutput queueBufferOutput;
5643 ASSERT_EQ(OK,
5644 producer->connect(dummyListener, NATIVE_WINDOW_API_CPU, true, &queueBufferOutput));
5645
5646 std::map<int, sp<GraphicBuffer>> slotMap;
5647 auto slotToBuffer = [&](int slot, sp<GraphicBuffer>* buf) {
5648 ASSERT_NE(nullptr, buf);
5649 const auto iter = slotMap.find(slot);
5650 ASSERT_NE(slotMap.end(), iter);
5651 *buf = iter->second;
5652 };
5653
5654 auto dequeue = [&](int* outSlot) {
5655 ASSERT_NE(nullptr, outSlot);
5656 *outSlot = -1;
5657 int slot;
5658 sp<Fence> fence;
5659 uint64_t age;
5660 FrameEventHistoryDelta timestamps;
5661 const status_t dequeueResult =
5662 producer->dequeueBuffer(&slot, &fence, width, height, PIXEL_FORMAT_RGBA_8888,
5663 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
5664 &age, &timestamps);
5665 if (dequeueResult == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
5666 sp<GraphicBuffer> newBuf;
5667 ASSERT_EQ(OK, producer->requestBuffer(slot, &newBuf));
5668 ASSERT_NE(nullptr, newBuf.get());
5669 slotMap[slot] = newBuf;
5670 } else {
5671 ASSERT_EQ(OK, dequeueResult);
5672 }
5673 *outSlot = slot;
5674 };
5675
5676 auto queue = [&](int slot, const Region& damage, nsecs_t displayTime) {
5677 IGraphicBufferProducer::QueueBufferInput input(
5678 /*timestamp=*/displayTime, /*isAutoTimestamp=*/false, HAL_DATASPACE_UNKNOWN,
5679 /*crop=*/Rect::EMPTY_RECT, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
5680 /*transform=*/0, Fence::NO_FENCE);
5681 input.setSurfaceDamage(damage);
5682 IGraphicBufferProducer::QueueBufferOutput output;
5683 ASSERT_EQ(OK, producer->queueBuffer(slot, input, &output));
5684 };
5685
5686 auto fillAndPostBuffers = [&](const Color& color) {
5687 int slot1;
5688 ASSERT_NO_FATAL_FAILURE(dequeue(&slot1));
5689 int slot2;
5690 ASSERT_NO_FATAL_FAILURE(dequeue(&slot2));
5691
5692 sp<GraphicBuffer> buf1;
5693 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot1, &buf1));
5694 sp<GraphicBuffer> buf2;
5695 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot2, &buf2));
Valerie Haue271df92019-09-06 09:23:22 -07005696 TransactionUtils::fillGraphicBufferColor(buf1, Rect(width, height), color);
5697 TransactionUtils::fillGraphicBufferColor(buf2, Rect(width, height), color);
Steven Thomas44685cb2019-07-23 16:19:31 -07005698
5699 const auto displayTime = systemTime() + milliseconds_to_nanoseconds(100);
5700 ASSERT_NO_FATAL_FAILURE(queue(slot1, Region::INVALID_REGION, displayTime));
5701 ASSERT_NO_FATAL_FAILURE(
5702 queue(slot2, Region(Rect(width / 3, height / 3, 2 * width / 3, 2 * height / 3)),
5703 displayTime));
5704 };
5705
5706 const auto startTime = systemTime();
5707 const std::array<Color, 3> colors = {Color::RED, Color::GREEN, Color::BLUE};
5708 int colorIndex = 0;
5709 while (nanoseconds_to_seconds(systemTime() - startTime) < 10) {
5710 ASSERT_NO_FATAL_FAILURE(fillAndPostBuffers(colors[colorIndex++ % colors.size()]));
5711 std::this_thread::sleep_for(1s);
5712 }
5713
5714 ASSERT_EQ(OK, producer->disconnect(NATIVE_WINDOW_API_CPU));
5715}
5716
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005717} // namespace android