blob: 57e5c51d446fd4c86d23d20b1ecb45e8a7f566f9 [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));
139 fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
140 postBufferQueueLayerBuffer(layer);
Chia-I Wu718daf82017-10-20 11:57:17 -0700141 }
142
Marissa Wall61c58622018-07-18 10:12:20 -0700143 virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
144 int32_t bufferWidth, int32_t bufferHeight) {
145 sp<GraphicBuffer> buffer =
146 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
147 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
148 BufferUsage::COMPOSER_OVERLAY,
149 "test");
150 fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
Marissa Wall861616d2018-10-22 12:52:23 -0700151 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -0700152 }
153
154 void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
155 int32_t bufferWidth, int32_t bufferHeight) {
156 switch (mLayerType) {
157 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
158 fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
159 break;
160 case ISurfaceComposerClient::eFXSurfaceBufferState:
161 fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
162 break;
163 default:
164 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
165 }
166 }
167
168 void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
169 int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
Chia-I Wu93853fe2017-11-02 08:30:27 -0700170 const Color& topRight, const Color& bottomLeft,
171 const Color& bottomRight) {
Marissa Wall61c58622018-07-18 10:12:20 -0700172 switch (mLayerType) {
173 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
174 fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
175 bottomLeft, bottomRight);
176 break;
177 case ISurfaceComposerClient::eFXSurfaceBufferState:
178 fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
179 bottomLeft, bottomRight);
180 break;
181 default:
182 ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
183 }
184 }
185
186 virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
187 int32_t bufferHeight, const Color& topLeft,
188 const Color& topRight, const Color& bottomLeft,
189 const Color& bottomRight) {
Chia-I Wu93853fe2017-11-02 08:30:27 -0700190 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700191 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
192 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700193
Marissa Wall61c58622018-07-18 10:12:20 -0700194 const int32_t halfW = bufferWidth / 2;
195 const int32_t halfH = bufferHeight / 2;
196 fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
197 fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
198 fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
199 fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
200 bottomRight);
Chia-I Wu93853fe2017-11-02 08:30:27 -0700201
Marissa Wall61c58622018-07-18 10:12:20 -0700202 postBufferQueueLayerBuffer(layer);
203 }
204
205 virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
206 int32_t bufferHeight, const Color& topLeft,
207 const Color& topRight, const Color& bottomLeft,
208 const Color& bottomRight) {
209 sp<GraphicBuffer> buffer =
210 new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
211 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
212 BufferUsage::COMPOSER_OVERLAY,
213 "test");
214
215 ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
216
217 const int32_t halfW = bufferWidth / 2;
218 const int32_t halfH = bufferHeight / 2;
219 fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
220 fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
221 fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
222 fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
223
224 Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
Chia-I Wu93853fe2017-11-02 08:30:27 -0700225 }
226
chaviw0e3479f2018-09-10 16:49:30 -0700227 std::unique_ptr<ScreenCapture> screenshot() {
228 std::unique_ptr<ScreenCapture> screenshot;
229 ScreenCapture::captureScreen(&screenshot);
Chia-I Wu718daf82017-10-20 11:57:17 -0700230 return screenshot;
231 }
232
Vishnu Nairb927e1f2019-02-19 13:36:15 -0800233 void asTransaction(const std::function<void(Transaction&)>& exec) {
234 Transaction t;
235 exec(t);
236 t.apply(true);
237 }
238
Marissa Wall713b63f2018-10-17 15:42:43 -0700239 static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
240 static BufferGenerator bufferGenerator;
241 return bufferGenerator.get(outBuffer, outFence);
242 }
243
Chia-I Wu718daf82017-10-20 11:57:17 -0700244 sp<SurfaceComposerClient> mClient;
245
246 sp<IBinder> mDisplay;
247 uint32_t mDisplayWidth;
248 uint32_t mDisplayHeight;
249 uint32_t mDisplayLayerStack;
Marissa Wall861616d2018-10-22 12:52:23 -0700250 Rect mDisplayRect = Rect::INVALID_RECT;
Chia-I Wu718daf82017-10-20 11:57:17 -0700251
252 // leave room for ~256 layers
253 const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
254
chaviw0e3479f2018-09-10 16:49:30 -0700255 sp<SurfaceControl> mBlackBgSurface;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -0700256 bool mColorManagementUsed;
257
Chia-I Wu718daf82017-10-20 11:57:17 -0700258private:
259 void SetUpDisplay() {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800260 mDisplay = mClient->getInternalDisplayToken();
261 ASSERT_FALSE(mDisplay == nullptr) << "failed to get display";
Chia-I Wu718daf82017-10-20 11:57:17 -0700262
263 // get display width/height
264 DisplayInfo info;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800265 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplay, &info));
Chia-I Wu718daf82017-10-20 11:57:17 -0700266 mDisplayWidth = info.w;
267 mDisplayHeight = info.h;
Marissa Wall861616d2018-10-22 12:52:23 -0700268 mDisplayRect =
269 Rect(static_cast<int32_t>(mDisplayWidth), static_cast<int32_t>(mDisplayHeight));
Chia-I Wu718daf82017-10-20 11:57:17 -0700270
271 // After a new buffer is queued, SurfaceFlinger is notified and will
272 // latch the new buffer on next vsync. Let's heuristically wait for 3
273 // vsyncs.
274 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
275
276 mDisplayLayerStack = 0;
chaviw0e3479f2018-09-10 16:49:30 -0700277
Vishnu Nair88a11f22018-11-28 18:30:57 -0800278 mBlackBgSurface =
279 createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
280 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
chaviw0e3479f2018-09-10 16:49:30 -0700281
Chia-I Wu718daf82017-10-20 11:57:17 -0700282 // set layer stack (b/68888219)
283 Transaction t;
284 t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
Vishnu Nair60356342018-11-13 13:00:45 -0800285 t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
chaviw0e3479f2018-09-10 16:49:30 -0700286 t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
287 t.setColor(mBlackBgSurface, half3{0, 0, 0});
288 t.setLayer(mBlackBgSurface, mLayerZBase);
Chia-I Wu718daf82017-10-20 11:57:17 -0700289 t.apply();
290 }
291
chaviw0e3479f2018-09-10 16:49:30 -0700292 void waitForLayerBuffers() {
293 // Request an empty transaction to get applied synchronously to ensure the buffer is
294 // latched.
295 Transaction().apply(true);
296 usleep(mBufferPostDelay);
297 }
Chia-I Wu718daf82017-10-20 11:57:17 -0700298
299 int32_t mBufferPostDelay;
Alec Mouri80863a62019-01-17 15:19:35 -0800300
301 friend class LayerRenderPathTestHarness;
302};
Alec Mouri80863a62019-01-17 15:19:35 -0800303
304class LayerRenderPathTestHarness {
305public:
306 LayerRenderPathTestHarness(LayerTransactionTest* delegate, RenderPath renderPath)
307 : mDelegate(delegate), mRenderPath(renderPath) {}
308
309 std::unique_ptr<ScreenCapture> getScreenCapture() {
310 switch (mRenderPath) {
311 case RenderPath::SCREENSHOT:
312 return mDelegate->screenshot();
313 case RenderPath::VIRTUAL_DISPLAY:
314
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800315 const auto mainDisplay = SurfaceComposerClient::getInternalDisplayToken();
Alec Mouri80863a62019-01-17 15:19:35 -0800316 DisplayInfo mainDisplayInfo;
317 SurfaceComposerClient::getDisplayInfo(mainDisplay, &mainDisplayInfo);
318
319 sp<IBinder> vDisplay;
320 sp<IGraphicBufferProducer> producer;
321 sp<IGraphicBufferConsumer> consumer;
322 sp<BufferItemConsumer> itemConsumer;
323 BufferQueue::createBufferQueue(&producer, &consumer);
324
325 consumer->setConsumerName(String8("Virtual disp consumer"));
326 consumer->setDefaultBufferSize(mainDisplayInfo.w, mainDisplayInfo.h);
327
328 itemConsumer = new BufferItemConsumer(consumer,
329 // Sample usage bits from screenrecord
330 GRALLOC_USAGE_HW_VIDEO_ENCODER |
331 GRALLOC_USAGE_SW_READ_OFTEN);
332
333 vDisplay = SurfaceComposerClient::createDisplay(String8("VirtualDisplay"),
334 false /*secure*/);
335
336 SurfaceComposerClient::Transaction t;
337 t.setDisplaySurface(vDisplay, producer);
338 t.setDisplayLayerStack(vDisplay, 0);
339 t.setDisplayProjection(vDisplay, mainDisplayInfo.orientation,
340 Rect(mainDisplayInfo.viewportW, mainDisplayInfo.viewportH),
341 Rect(mainDisplayInfo.w, mainDisplayInfo.h));
342 t.apply();
343 SurfaceComposerClient::Transaction().apply(true);
344 BufferItem item;
345 itemConsumer->acquireBuffer(&item, 0, true);
346 auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer);
347 itemConsumer->releaseBuffer(item);
348 SurfaceComposerClient::destroyDisplay(vDisplay);
349 return sc;
350 }
351 }
352
353protected:
354 LayerTransactionTest* mDelegate;
355 RenderPath mRenderPath;
Chia-I Wu718daf82017-10-20 11:57:17 -0700356};
357
Alec Mouri80863a62019-01-17 15:19:35 -0800358class LayerTypeTransactionHarness : public LayerTransactionTest {
Marissa Wall61c58622018-07-18 10:12:20 -0700359public:
Alec Mouri80863a62019-01-17 15:19:35 -0800360 LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {}
Marissa Wall61c58622018-07-18 10:12:20 -0700361
362 sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
Alec Mouri80863a62019-01-17 15:19:35 -0800363 uint32_t flags = 0, SurfaceControl* parent = nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700364 // if the flags already have a layer type specified, return an error
365 if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
366 return nullptr;
367 }
chaviwf66724d2018-11-28 16:35:21 -0800368 return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent);
Marissa Wall61c58622018-07-18 10:12:20 -0700369 }
370
371 void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
372 int32_t bufferHeight) {
373 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
374 bufferWidth, bufferHeight));
375 }
376
377 void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
378 int32_t bufferHeight, const Color& topLeft, const Color& topRight,
379 const Color& bottomLeft, const Color& bottomRight) {
380 ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
381 bufferWidth, bufferHeight,
382 topLeft, topRight,
383 bottomLeft, bottomRight));
384 }
385
386protected:
387 uint32_t mLayerType;
388};
389
Alec Mouri80863a62019-01-17 15:19:35 -0800390class LayerTypeTransactionTest : public LayerTypeTransactionHarness,
391 public ::testing::WithParamInterface<uint32_t> {
392public:
393 LayerTypeTransactionTest() : LayerTypeTransactionHarness(GetParam()) {}
394};
395
396class LayerTypeAndRenderTypeTransactionTest
397 : public LayerTypeTransactionHarness,
398 public ::testing::WithParamInterface<std::tuple<uint32_t, RenderPath>> {
399public:
400 LayerTypeAndRenderTypeTransactionTest()
401 : LayerTypeTransactionHarness(std::get<0>(GetParam())),
402 mRenderPathHarness(LayerRenderPathTestHarness(this, std::get<1>(GetParam()))) {}
403
404 std::unique_ptr<ScreenCapture> getScreenCapture() {
405 return mRenderPathHarness.getScreenCapture();
406 }
407
408protected:
409 LayerRenderPathTestHarness mRenderPathHarness;
410};
411
Alec Mouri80863a62019-01-17 15:19:35 -0800412::testing::Environment* const binderEnv =
413 ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
414
415class LayerRenderTypeTransactionTest : public LayerTransactionTest,
416 public ::testing::WithParamInterface<RenderPath> {
417public:
418 LayerRenderTypeTransactionTest() : mHarness(LayerRenderPathTestHarness(this, GetParam())) {}
419
420 std::unique_ptr<ScreenCapture> getScreenCapture() { return mHarness.getScreenCapture(); }
421 void setRelativeZBasicHelper(uint32_t layerType);
422 void setRelativeZGroupHelper(uint32_t layerType);
423 void setAlphaBasicHelper(uint32_t layerType);
Valerie Haudd0b7572019-01-29 14:59:27 -0800424 void setBackgroundColorHelper(uint32_t layerType, bool priorColor, bool bufferFill, float alpha,
425 Color finalColor);
Alec Mouri80863a62019-01-17 15:19:35 -0800426
427protected:
428 LayerRenderPathTestHarness mHarness;
429};
430
431INSTANTIATE_TEST_CASE_P(
432 LayerTypeAndRenderTypeTransactionTests, LayerTypeAndRenderTypeTransactionTest,
433 ::testing::Combine(
434 ::testing::Values(
435 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
436 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)),
437 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT)));
438
439INSTANTIATE_TEST_CASE_P(LayerRenderTypeTransactionTests, LayerRenderTypeTransactionTest,
440 ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT));
441
Marissa Wall61c58622018-07-18 10:12:20 -0700442INSTANTIATE_TEST_CASE_P(
443 LayerTypeTransactionTests, LayerTypeTransactionTest,
444 ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
445 static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
446
Alec Mouri80863a62019-01-17 15:19:35 -0800447TEST_P(LayerRenderTypeTransactionTest, SetPositionBasic_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700448 sp<SurfaceControl> layer;
449 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700450 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700451
452 {
453 SCOPED_TRACE("default position");
Marissa Wall861616d2018-10-22 12:52:23 -0700454 const Rect rect(0, 0, 32, 32);
Alec Mouri80863a62019-01-17 15:19:35 -0800455 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700456 shot->expectColor(rect, Color::RED);
457 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700458 }
459
460 Transaction().setPosition(layer, 5, 10).apply();
461 {
462 SCOPED_TRACE("new position");
Marissa Wall861616d2018-10-22 12:52:23 -0700463 const Rect rect(5, 10, 37, 42);
Alec Mouri80863a62019-01-17 15:19:35 -0800464 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700465 shot->expectColor(rect, Color::RED);
466 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700467 }
468}
469
Alec Mouri80863a62019-01-17 15:19:35 -0800470TEST_P(LayerRenderTypeTransactionTest, SetPositionRounding_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700471 sp<SurfaceControl> layer;
472 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700473 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700474
475 // GLES requires only 4 bits of subpixel precision during rasterization
476 // XXX GLES composition does not match HWC composition due to precision
477 // loss (b/69315223)
478 const float epsilon = 1.0f / 16.0f;
479 Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
480 {
481 SCOPED_TRACE("rounding down");
Alec Mouri80863a62019-01-17 15:19:35 -0800482 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700483 }
484
485 Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
486 {
487 SCOPED_TRACE("rounding up");
Alec Mouri80863a62019-01-17 15:19:35 -0800488 getScreenCapture()->expectColor(Rect(1, 1, 33, 33), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700489 }
490}
491
Alec Mouri80863a62019-01-17 15:19:35 -0800492TEST_P(LayerRenderTypeTransactionTest, SetPositionOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700493 sp<SurfaceControl> layer;
494 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700495 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700496
497 Transaction().setPosition(layer, -32, -32).apply();
498 {
499 SCOPED_TRACE("negative coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800500 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700501 }
502
503 Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
504 {
505 SCOPED_TRACE("positive coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800506 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700507 }
508}
509
Alec Mouri80863a62019-01-17 15:19:35 -0800510TEST_P(LayerRenderTypeTransactionTest, SetPositionPartiallyOutOfBounds_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700511 sp<SurfaceControl> layer;
512 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700513 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700514
515 // partially out of bounds
516 Transaction().setPosition(layer, -30, -30).apply();
517 {
518 SCOPED_TRACE("negative coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800519 getScreenCapture()->expectColor(Rect(0, 0, 2, 2), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700520 }
521
522 Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
523 {
524 SCOPED_TRACE("positive coordinates");
Alec Mouri80863a62019-01-17 15:19:35 -0800525 getScreenCapture()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
526 mDisplayHeight),
527 Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700528 }
529}
530
Alec Mouri80863a62019-01-17 15:19:35 -0800531TEST_P(LayerRenderTypeTransactionTest, SetPositionWithResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700532 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700533 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
534 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700535
536 // setPosition is applied immediately by default, with or without resize
537 // pending
538 Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
539 {
540 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800541 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700542 const Rect rect(5, 10, 37, 42);
Marissa Wall61c58622018-07-18 10:12:20 -0700543 shot->expectColor(rect, Color::RED);
544 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu718daf82017-10-20 11:57:17 -0700545 }
546
Marissa Wall861616d2018-10-22 12:52:23 -0700547 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700548 {
549 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800550 getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700551 }
552}
553
Alec Mouri80863a62019-01-17 15:19:35 -0800554TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResize_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700555 sp<SurfaceControl> layer;
556 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700557 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700558
559 // request setPosition to be applied with the next resize
560 Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
561 {
562 SCOPED_TRACE("new position pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800563 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700564 }
565
566 Transaction().setPosition(layer, 15, 20).apply();
567 {
568 SCOPED_TRACE("pending new position modified");
Alec Mouri80863a62019-01-17 15:19:35 -0800569 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700570 }
571
572 Transaction().setSize(layer, 64, 64).apply();
573 {
574 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800575 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700576 }
577
578 // finally resize and latch the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700579 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700580 {
581 SCOPED_TRACE("new position applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800582 getScreenCapture()->expectColor(Rect(15, 20, 79, 84), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700583 }
584}
585
Alec Mouri80863a62019-01-17 15:19:35 -0800586TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu718daf82017-10-20 11:57:17 -0700587 sp<SurfaceControl> layer;
588 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700589 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu718daf82017-10-20 11:57:17 -0700590
591 // setPosition is not immediate even with SCALE_TO_WINDOW override
592 Transaction()
593 .setPosition(layer, 5, 10)
594 .setSize(layer, 64, 64)
595 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
596 .setGeometryAppliesWithResize(layer)
597 .apply();
598 {
599 SCOPED_TRACE("new position pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800600 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700601 }
602
Marissa Wall61c58622018-07-18 10:12:20 -0700603 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu718daf82017-10-20 11:57:17 -0700604 {
605 SCOPED_TRACE("new position applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800606 getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
Chia-I Wu718daf82017-10-20 11:57:17 -0700607 }
608}
609
Alec Mouri80863a62019-01-17 15:19:35 -0800610TEST_P(LayerRenderTypeTransactionTest, SetSizeBasic_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700611 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -0700612 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
613 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700614
615 Transaction().setSize(layer, 64, 64).apply();
616 {
617 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -0800618 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700619 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -0700620 shot->expectColor(rect, Color::RED);
621 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700622 }
623
Marissa Wall861616d2018-10-22 12:52:23 -0700624 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700625 {
626 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -0800627 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -0700628 const Rect rect(0, 0, 64, 64);
629 shot->expectColor(rect, Color::RED);
630 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700631 }
632}
633
Alec Mouri80863a62019-01-17 15:19:35 -0800634TEST_P(LayerTypeAndRenderTypeTransactionTest, SetSizeInvalid) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700635 // cannot test robustness against invalid sizes (zero or really huge)
636}
637
Alec Mouri80863a62019-01-17 15:19:35 -0800638TEST_P(LayerRenderTypeTransactionTest, SetSizeWithScaleToWindow_BufferQueue) {
Chia-I Wu0eaea312017-10-31 10:14:40 -0700639 sp<SurfaceControl> layer;
640 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -0700641 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu0eaea312017-10-31 10:14:40 -0700642
643 // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
644 Transaction()
645 .setSize(layer, 64, 64)
646 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
647 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -0800648 getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
Chia-I Wu0eaea312017-10-31 10:14:40 -0700649}
650
Alec Mouri80863a62019-01-17 15:19:35 -0800651TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZBasic) {
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700652 sp<SurfaceControl> layerR;
653 sp<SurfaceControl> layerG;
654 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700655 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700656 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700657 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700658
659 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
660 {
661 SCOPED_TRACE("layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800662 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700663 }
664
665 Transaction().setLayer(layerG, mLayerZBase + 2).apply();
666 {
667 SCOPED_TRACE("layerG");
Alec Mouri80863a62019-01-17 15:19:35 -0800668 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700669 }
670}
671
Alec Mouri80863a62019-01-17 15:19:35 -0800672TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700673 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800674 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700675 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800676 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700677 sp<SurfaceControl> layerR;
678 sp<SurfaceControl> layerG;
679 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700680 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700681 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700682 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700683
chaviw0e3479f2018-09-10 16:49:30 -0700684 Transaction()
685 .reparent(layerR, parent->getHandle())
686 .reparent(layerG, parent->getHandle())
687 .apply();
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700688 Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
689 {
690 SCOPED_TRACE("layerR");
Alec Mouri80863a62019-01-17 15:19:35 -0800691 auto shot = getScreenCapture();
chaviw0e3479f2018-09-10 16:49:30 -0700692 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700693 }
694
695 Transaction().setLayer(layerR, -3).apply();
696 {
697 SCOPED_TRACE("layerG");
Alec Mouri80863a62019-01-17 15:19:35 -0800698 auto shot = getScreenCapture();
chaviw0e3479f2018-09-10 16:49:30 -0700699 shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
Chia-I Wu0ea0f822017-10-31 10:14:40 -0700700 }
701}
702
Alec Mouri80863a62019-01-17 15:19:35 -0800703void LayerRenderTypeTransactionTest::setRelativeZBasicHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700704 sp<SurfaceControl> layerR;
705 sp<SurfaceControl> layerG;
Marissa Wall861616d2018-10-22 12:52:23 -0700706 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32, layerType));
707 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
708 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32, layerType));
709 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700710
Marissa Wall861616d2018-10-22 12:52:23 -0700711 switch (layerType) {
712 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
713 Transaction()
714 .setPosition(layerG, 16, 16)
715 .setRelativeLayer(layerG, layerR->getHandle(), 1)
716 .apply();
717 break;
718 case ISurfaceComposerClient::eFXSurfaceBufferState:
719 Transaction()
720 .setFrame(layerR, Rect(0, 0, 32, 32))
721 .setFrame(layerG, Rect(16, 16, 48, 48))
722 .setRelativeLayer(layerG, layerR->getHandle(), 1)
723 .apply();
724 break;
725 default:
726 ASSERT_FALSE(true) << "Unsupported layer type";
727 }
Chia-I Wu49313302017-10-31 10:14:40 -0700728 {
729 SCOPED_TRACE("layerG above");
Alec Mouri80863a62019-01-17 15:19:35 -0800730 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700731 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
732 shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
733 }
734
735 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
736 {
737 SCOPED_TRACE("layerG below");
Alec Mouri80863a62019-01-17 15:19:35 -0800738 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700739 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
740 shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
741 }
742}
743
Alec Mouri80863a62019-01-17 15:19:35 -0800744TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -0700745 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
746}
747
Alec Mouri80863a62019-01-17 15:19:35 -0800748TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -0700749 ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
750}
751
Marissa Wall61c58622018-07-18 10:12:20 -0700752TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
chaviw0e3479f2018-09-10 16:49:30 -0700753 sp<SurfaceControl> parent =
Vishnu Nair88a11f22018-11-28 18:30:57 -0800754 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
chaviw0e3479f2018-09-10 16:49:30 -0700755 ISurfaceComposerClient::eFXSurfaceContainer);
Vishnu Nair88a11f22018-11-28 18:30:57 -0800756 Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
Chia-I Wuec2d9852017-11-21 09:21:01 -0800757 sp<SurfaceControl> layerR;
758 sp<SurfaceControl> layerG;
759 sp<SurfaceControl> layerB;
760 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700761 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800762 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700763 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800764 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700765 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800766
chaviw0e3479f2018-09-10 16:49:30 -0700767 Transaction()
768 .reparent(layerB, parent->getHandle())
769 .apply();
770
Chia-I Wuec2d9852017-11-21 09:21:01 -0800771 // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
772 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
773
chaviw0e3479f2018-09-10 16:49:30 -0700774 std::unique_ptr<ScreenCapture> screenshot;
Chia-I Wuec2d9852017-11-21 09:21:01 -0800775 // only layerB is in this range
chaviw0e3479f2018-09-10 16:49:30 -0700776 sp<IBinder> parentHandle = parent->getHandle();
Marissa Wall861616d2018-10-22 12:52:23 -0700777 ScreenCapture::captureLayers(&screenshot, parentHandle, Rect(0, 0, 32, 32));
Chia-I Wuec2d9852017-11-21 09:21:01 -0800778 screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
779}
780
chaviw32377582019-05-13 11:15:19 -0700781TEST_P(LayerTypeTransactionTest, SetLayerAndRelative) {
782 sp<SurfaceControl> parent =
783 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
784 ISurfaceComposerClient::eFXSurfaceColor);
785
786 sp<SurfaceControl> childLayer;
787 ASSERT_NO_FATAL_FAILURE(
788 childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
789 0 /* buffer height */,
790 ISurfaceComposerClient::eFXSurfaceColor,
791 parent.get()));
792 Transaction()
793 .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
794 .setColor(parent, half3{0.0f, 0.0f, 0.0f})
795 .show(childLayer)
796 .show(parent)
797 .setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight))
798 .setCrop_legacy(childLayer, Rect(0, 0, 20, 30))
799 .apply();
800
801 Transaction()
802 .setRelativeLayer(childLayer, parent->getHandle(), -1)
803 .setLayer(childLayer, 1)
804 .apply();
805
806 {
807 SCOPED_TRACE("setLayer above");
808 // Set layer should get applied and place the child above.
809 std::unique_ptr<ScreenCapture> screenshot;
810 ScreenCapture::captureScreen(&screenshot);
811 screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
812 }
813
814 Transaction()
815 .setLayer(childLayer, 1)
816 .setRelativeLayer(childLayer, parent->getHandle(), -1)
817 .apply();
818
819 {
820 SCOPED_TRACE("setRelative below");
821 // Set relative layer should get applied and place the child below.
822 std::unique_ptr<ScreenCapture> screenshot;
823 ScreenCapture::captureScreen(&screenshot);
824 screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
825 }
826}
827
Robert Carr1c5481e2019-07-01 14:42:27 -0700828TEST_P(LayerTypeTransactionTest, HideRelativeParentHidesLayer) {
829 sp<SurfaceControl> parent =
830 LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
831 ISurfaceComposerClient::eFXSurfaceColor);
832 sp<SurfaceControl> relativeParent =
833 LayerTransactionTest::createLayer("RelativeParent", 0 /* buffer width */,
834 0 /* buffer height */, ISurfaceComposerClient::eFXSurfaceColor);
835
836 sp<SurfaceControl> childLayer;
837 ASSERT_NO_FATAL_FAILURE(
838 childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
839 0 /* buffer height */,
840 ISurfaceComposerClient::eFXSurfaceColor,
841 parent.get()));
842 Transaction()
843 .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
844 .setColor(parent, half3{0.0f, 0.0f, 0.0f})
845 .setColor(relativeParent, half3{0.0f, 1.0f, 0.0f})
846 .show(childLayer)
847 .show(parent)
848 .show(relativeParent)
849 .setLayer(parent, mLayerZBase - 1)
850 .setLayer(relativeParent, mLayerZBase)
851 .apply();
852
853 Transaction()
854 .setRelativeLayer(childLayer, relativeParent->getHandle(), 1)
855 .apply();
856
857 {
858 SCOPED_TRACE("setLayer above");
859 // Set layer should get applied and place the child above.
860 std::unique_ptr<ScreenCapture> screenshot;
861 ScreenCapture::captureScreen(&screenshot);
862 screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
863 }
864
865 Transaction()
866 .hide(relativeParent)
867 .apply();
868
869 {
870 SCOPED_TRACE("hide relative parent");
871 // The relative should no longer be visible.
872 std::unique_ptr<ScreenCapture> screenshot;
873 ScreenCapture::captureScreen(&screenshot);
874 screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
875 }
876}
877
Alec Mouri80863a62019-01-17 15:19:35 -0800878void LayerRenderTypeTransactionTest::setRelativeZGroupHelper(uint32_t layerType) {
Chia-I Wu49313302017-10-31 10:14:40 -0700879 sp<SurfaceControl> layerR;
880 sp<SurfaceControl> layerG;
881 sp<SurfaceControl> layerB;
Marissa Wall861616d2018-10-22 12:52:23 -0700882 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test", 32, 32, layerType));
883 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
884 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test", 32, 32, layerType));
885 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
886 ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test", 32, 32, layerType));
887 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerB, Color::BLUE, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700888
889 // layerR = 0, layerG = layerR + 3, layerB = 2
Marissa Wall861616d2018-10-22 12:52:23 -0700890 switch (layerType) {
891 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
892 Transaction()
893 .setPosition(layerG, 8, 8)
894 .setRelativeLayer(layerG, layerR->getHandle(), 3)
895 .setPosition(layerB, 16, 16)
896 .setLayer(layerB, mLayerZBase + 2)
897 .apply();
898 break;
899 case ISurfaceComposerClient::eFXSurfaceBufferState:
900 Transaction()
901 .setFrame(layerR, Rect(0, 0, 32, 32))
902 .setFrame(layerG, Rect(8, 8, 40, 40))
903 .setRelativeLayer(layerG, layerR->getHandle(), 3)
904 .setFrame(layerB, Rect(16, 16, 48, 48))
905 .setLayer(layerB, mLayerZBase + 2)
906 .apply();
907 break;
908 default:
909 ASSERT_FALSE(true) << "Unsupported layer type";
910 }
911
Chia-I Wu49313302017-10-31 10:14:40 -0700912 {
913 SCOPED_TRACE("(layerR < layerG) < layerB");
Alec Mouri80863a62019-01-17 15:19:35 -0800914 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700915 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
916 shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
917 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
918 }
919
920 // layerR = 4, layerG = layerR + 3, layerB = 2
921 Transaction().setLayer(layerR, mLayerZBase + 4).apply();
922 {
923 SCOPED_TRACE("layerB < (layerR < layerG)");
Alec Mouri80863a62019-01-17 15:19:35 -0800924 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700925 shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
926 shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
927 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
928 }
929
930 // layerR = 4, layerG = layerR - 3, layerB = 2
931 Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
932 {
933 SCOPED_TRACE("layerB < (layerG < layerR)");
Alec Mouri80863a62019-01-17 15:19:35 -0800934 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700935 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
936 shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
937 shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
938 }
939
940 // restore to absolute z
941 // layerR = 4, layerG = 0, layerB = 2
942 Transaction().setLayer(layerG, mLayerZBase).apply();
943 {
944 SCOPED_TRACE("layerG < layerB < 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, 48, 48), Color::BLUE);
948 }
949
950 // layerR should not affect layerG anymore
951 // layerR = 1, layerG = 0, layerB = 2
952 Transaction().setLayer(layerR, mLayerZBase + 1).apply();
953 {
954 SCOPED_TRACE("layerG < layerR < layerB");
Alec Mouri80863a62019-01-17 15:19:35 -0800955 auto shot = getScreenCapture();
Chia-I Wu49313302017-10-31 10:14:40 -0700956 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
957 shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
958 }
959}
960
Alec Mouri80863a62019-01-17 15:19:35 -0800961TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -0700962 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
963}
964
Alec Mouri80863a62019-01-17 15:19:35 -0800965TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -0700966 ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
967}
968
Alec Mouri80863a62019-01-17 15:19:35 -0800969TEST_P(LayerTypeAndRenderTypeTransactionTest, SetRelativeZBug64572777) {
Chia-I Wu49313302017-10-31 10:14:40 -0700970 sp<SurfaceControl> layerR;
971 sp<SurfaceControl> layerG;
972
973 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700974 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700975 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700976 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu49313302017-10-31 10:14:40 -0700977
978 Transaction()
979 .setPosition(layerG, 16, 16)
980 .setRelativeLayer(layerG, layerR->getHandle(), 1)
981 .apply();
982
Robert Carr87246532019-02-04 15:20:26 -0800983 layerG.clear();
Chia-I Wu49313302017-10-31 10:14:40 -0700984 // layerG should have been removed
Alec Mouri80863a62019-01-17 15:19:35 -0800985 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu49313302017-10-31 10:14:40 -0700986}
987
Alec Mouri80863a62019-01-17 15:19:35 -0800988TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsHidden) {
Chia-I Wu57b27502017-10-31 10:14:40 -0700989 sp<SurfaceControl> layer;
990 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -0700991 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -0700992
993 Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
994 {
995 SCOPED_TRACE("layer hidden");
Alec Mouri80863a62019-01-17 15:19:35 -0800996 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu57b27502017-10-31 10:14:40 -0700997 }
998
999 Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
1000 {
1001 SCOPED_TRACE("layer shown");
Alec Mouri80863a62019-01-17 15:19:35 -08001002 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu57b27502017-10-31 10:14:40 -07001003 }
1004}
1005
Alec Mouri80863a62019-01-17 15:19:35 -08001006TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsOpaque) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001007 const Color translucentRed = {100, 0, 0, 100};
1008 sp<SurfaceControl> layerR;
1009 sp<SurfaceControl> layerG;
1010 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001011 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001012 ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001013 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001014
1015 Transaction()
1016 .setLayer(layerR, mLayerZBase + 1)
1017 .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
1018 .apply();
1019 {
1020 SCOPED_TRACE("layerR opaque");
Alec Mouri80863a62019-01-17 15:19:35 -08001021 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
Chia-I Wu57b27502017-10-31 10:14:40 -07001022 }
1023
1024 Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
1025 {
1026 SCOPED_TRACE("layerR translucent");
1027 const uint8_t g = uint8_t(255 - translucentRed.a);
Alec Mouri80863a62019-01-17 15:19:35 -08001028 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
Chia-I Wu57b27502017-10-31 10:14:40 -07001029 }
1030}
1031
Marissa Wall61c58622018-07-18 10:12:20 -07001032TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
Chia-I Wu57b27502017-10-31 10:14:40 -07001033 sp<SurfaceControl> layer;
1034 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001035 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu57b27502017-10-31 10:14:40 -07001036
1037 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001038 sp<GraphicBuffer> outBuffer;
Chia-I Wu57b27502017-10-31 10:14:40 -07001039 Transaction()
1040 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1041 .apply(true);
1042 ASSERT_EQ(PERMISSION_DENIED,
chaviw0e3479f2018-09-10 16:49:30 -07001043 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001044
1045 Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
1046 ASSERT_EQ(NO_ERROR,
chaviw0e3479f2018-09-10 16:49:30 -07001047 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
Chia-I Wu57b27502017-10-31 10:14:40 -07001048}
1049
Robert Carrfa8855f2019-02-19 10:05:00 -08001050TEST_F(LayerTransactionTest, SetFlagsSecureEUidSystem) {
1051 sp<SurfaceControl> layer;
1052 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1053 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
1054
1055 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
1056 sp<GraphicBuffer> outBuffer;
1057 Transaction()
1058 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
1059 .apply(true);
1060 ASSERT_EQ(PERMISSION_DENIED,
1061 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1062
1063 UIDFaker f(AID_SYSTEM);
1064
1065 // By default the system can capture screenshots with secure layers but they
1066 // will be blacked out
1067 ASSERT_EQ(NO_ERROR,
1068 composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
1069
1070 {
1071 SCOPED_TRACE("as system");
1072 auto shot = screenshot();
1073 shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
1074 }
1075
1076 // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
1077 // to receive them...we are expected to take care with the results.
Robert Carr108b2c72019-04-02 16:32:58 -07001078 bool outCapturedSecureLayers;
Robert Carrfa8855f2019-02-19 10:05:00 -08001079 ASSERT_EQ(NO_ERROR,
Robert Carr108b2c72019-04-02 16:32:58 -07001080 composer->captureScreen(mDisplay, &outBuffer, outCapturedSecureLayers,
1081 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(), 0,
1082 0, false, ISurfaceComposer::eRotateNone, true));
1083 ASSERT_EQ(true, outCapturedSecureLayers);
Robert Carrfa8855f2019-02-19 10:05:00 -08001084 ScreenCapture sc(outBuffer);
1085 sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
1086}
1087
Alec Mouri80863a62019-01-17 15:19:35 -08001088TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001089 const Rect top(0, 0, 32, 16);
1090 const Rect bottom(0, 16, 32, 32);
1091 sp<SurfaceControl> layer;
1092 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1093
1094 ANativeWindow_Buffer buffer;
Marissa Wall61c58622018-07-18 10:12:20 -07001095 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1096 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
1097 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001098 // setTransparentRegionHint always applies to the following buffer
1099 Transaction().setTransparentRegionHint(layer, Region(top)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001100 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001101 {
1102 SCOPED_TRACE("top transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001103 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001104 shot->expectColor(top, Color::BLACK);
1105 shot->expectColor(bottom, Color::RED);
1106 }
1107
1108 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1109 {
1110 SCOPED_TRACE("transparent region hint pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001111 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001112 shot->expectColor(top, Color::BLACK);
1113 shot->expectColor(bottom, Color::RED);
1114 }
1115
Marissa Wall61c58622018-07-18 10:12:20 -07001116 ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
1117 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
1118 ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
1119 ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001120 {
1121 SCOPED_TRACE("bottom transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001122 auto shot = getScreenCapture();
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001123 shot->expectColor(top, Color::RED);
1124 shot->expectColor(bottom, Color::BLACK);
1125 }
1126}
1127
Alec Mouri80863a62019-01-17 15:19:35 -08001128TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001129 const Rect top(0, 0, 32, 16);
1130 const Rect bottom(0, 16, 32, 32);
1131 sp<SurfaceControl> layer;
1132 ASSERT_NO_FATAL_FAILURE(
1133 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1134
1135 sp<GraphicBuffer> buffer =
1136 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1137 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1138 BufferUsage::COMPOSER_OVERLAY,
1139 "test");
1140
1141 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
1142 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
1143 Transaction()
1144 .setTransparentRegionHint(layer, Region(top))
1145 .setBuffer(layer, buffer)
Marissa Wall861616d2018-10-22 12:52:23 -07001146 .setFrame(layer, Rect(0, 0, 32, 32))
Marissa Wall61c58622018-07-18 10:12:20 -07001147 .apply();
1148 {
1149 SCOPED_TRACE("top transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001150 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001151 shot->expectColor(top, Color::BLACK);
1152 shot->expectColor(bottom, Color::RED);
1153 }
1154
1155 Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
1156 {
1157 SCOPED_TRACE("transparent region hint intermediate");
Alec Mouri80863a62019-01-17 15:19:35 -08001158 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001159 shot->expectColor(top, Color::BLACK);
1160 shot->expectColor(bottom, Color::BLACK);
1161 }
1162
1163 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
1164 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1165 BufferUsage::COMPOSER_OVERLAY,
1166 "test");
1167
1168 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
1169 ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
Marissa Wall861616d2018-10-22 12:52:23 -07001170 Transaction().setBuffer(layer, buffer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07001171 {
1172 SCOPED_TRACE("bottom transparent");
Alec Mouri80863a62019-01-17 15:19:35 -08001173 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07001174 shot->expectColor(top, Color::RED);
1175 shot->expectColor(bottom, Color::BLACK);
1176 }
1177}
1178
Alec Mouri80863a62019-01-17 15:19:35 -08001179TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferQueue) {
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001180 sp<SurfaceControl> layerTransparent;
1181 sp<SurfaceControl> layerR;
1182 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1183 ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
1184
1185 // check that transparent region hint is bound by the layer size
1186 Transaction()
Marissa Wall861616d2018-10-22 12:52:23 -07001187 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001188 .setPosition(layerR, 16, 16)
1189 .setLayer(layerR, mLayerZBase + 1)
1190 .apply();
Marissa Wall861616d2018-10-22 12:52:23 -07001191 ASSERT_NO_FATAL_FAILURE(
1192 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1193 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerR, Color::RED, 32, 32));
Alec Mouri80863a62019-01-17 15:19:35 -08001194 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
Chia-I Wu2113bdd2017-11-01 15:16:35 -07001195}
1196
Alec Mouri80863a62019-01-17 15:19:35 -08001197TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001198 sp<SurfaceControl> layerTransparent;
1199 sp<SurfaceControl> layerR;
1200 ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
1201 ASSERT_NO_FATAL_FAILURE(
1202 layerR = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1203
1204 // check that transparent region hint is bound by the layer size
1205 Transaction()
1206 .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
1207 .setFrame(layerR, Rect(16, 16, 48, 48))
1208 .setLayer(layerR, mLayerZBase + 1)
1209 .apply();
1210 ASSERT_NO_FATAL_FAILURE(
1211 fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
1212 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layerR, Color::RED, 32, 32));
Alec Mouri80863a62019-01-17 15:19:35 -08001213 getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
Marissa Wall861616d2018-10-22 12:52:23 -07001214}
1215
Alec Mouri80863a62019-01-17 15:19:35 -08001216void LayerRenderTypeTransactionTest::setAlphaBasicHelper(uint32_t layerType) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001217 sp<SurfaceControl> layer1;
1218 sp<SurfaceControl> layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07001219 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32, layerType));
1220 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32, layerType));
1221 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer1, {64, 0, 0, 255}, 32, 32));
1222 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer2, {0, 64, 0, 255}, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001223
Marissa Wall861616d2018-10-22 12:52:23 -07001224 switch (layerType) {
1225 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1226 Transaction()
1227 .setAlpha(layer1, 0.25f)
1228 .setAlpha(layer2, 0.75f)
1229 .setPosition(layer2, 16, 0)
1230 .setLayer(layer2, mLayerZBase + 1)
1231 .apply();
1232 break;
1233 case ISurfaceComposerClient::eFXSurfaceBufferState:
1234 Transaction()
1235 .setAlpha(layer1, 0.25f)
1236 .setAlpha(layer2, 0.75f)
1237 .setFrame(layer1, Rect(0, 0, 32, 32))
1238 .setFrame(layer2, Rect(16, 0, 48, 32))
1239 .setLayer(layer2, mLayerZBase + 1)
1240 .apply();
1241 break;
1242 default:
1243 ASSERT_FALSE(true) << "Unsupported layer type";
1244 }
Chia-I Wua8a515e2017-11-01 15:16:35 -07001245 {
Alec Mouri80863a62019-01-17 15:19:35 -08001246 auto shot = getScreenCapture();
Chia-I Wua8a515e2017-11-01 15:16:35 -07001247 uint8_t r = 16; // 64 * 0.25f
1248 uint8_t g = 48; // 64 * 0.75f
1249 shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
1250 shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
1251
1252 r /= 4; // r * (1.0f - 0.75f)
1253 shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
1254 }
1255}
1256
Alec Mouri80863a62019-01-17 15:19:35 -08001257TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferQueue) {
Marissa Wall861616d2018-10-22 12:52:23 -07001258 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
1259}
1260
Alec Mouri80863a62019-01-17 15:19:35 -08001261TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001262 ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
1263}
1264
Alec Mouri80863a62019-01-17 15:19:35 -08001265TEST_P(LayerTypeAndRenderTypeTransactionTest, SetAlphaClamped) {
Chia-I Wua8a515e2017-11-01 15:16:35 -07001266 const Color color = {64, 0, 0, 255};
1267 sp<SurfaceControl> layer;
1268 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001269 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
Chia-I Wua8a515e2017-11-01 15:16:35 -07001270
1271 Transaction().setAlpha(layer, 2.0f).apply();
1272 {
1273 SCOPED_TRACE("clamped to 1.0f");
Alec Mouri80863a62019-01-17 15:19:35 -08001274 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), color);
Chia-I Wua8a515e2017-11-01 15:16:35 -07001275 }
1276
1277 Transaction().setAlpha(layer, -1.0f).apply();
1278 {
1279 SCOPED_TRACE("clamped to 0.0f");
Alec Mouri80863a62019-01-17 15:19:35 -08001280 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wua8a515e2017-11-01 15:16:35 -07001281 }
1282}
1283
Alec Mouri80863a62019-01-17 15:19:35 -08001284TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadius) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001285 sp<SurfaceControl> layer;
1286 const uint8_t size = 64;
1287 const uint8_t testArea = 4;
Lucas Dupina1d0e312018-12-04 22:30:27 -08001288 const float cornerRadius = 20.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001289 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
1290 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
1291
1292 Transaction()
1293 .setCornerRadius(layer, cornerRadius)
1294 .apply();
1295 {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001296 const uint8_t bottom = size - 1;
1297 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001298 auto shot = getScreenCapture();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001299 // Transparent corners
1300 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
Lucas Dupina1d0e312018-12-04 22:30:27 -08001301 shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
1302 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1303 shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
1304 }
1305}
1306
Alec Mouri80863a62019-01-17 15:19:35 -08001307TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildCrop) {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001308 sp<SurfaceControl> parent;
1309 sp<SurfaceControl> child;
1310 const uint8_t size = 64;
1311 const uint8_t testArea = 4;
1312 const float cornerRadius = 20.0f;
1313 ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
1314 ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
1315 ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
1316 ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
1317
1318 Transaction()
1319 .setCornerRadius(parent, cornerRadius)
1320 .reparent(child, parent->getHandle())
1321 .setPosition(child, 0, size / 2)
1322 .apply();
1323 {
1324 const uint8_t bottom = size - 1;
1325 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001326 auto shot = getScreenCapture();
Lucas Dupina1d0e312018-12-04 22:30:27 -08001327 // Top edge of child should not have rounded corners because it's translated in the parent
1328 shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
1329 Color::GREEN);
1330 // But bottom edges should have been clipped according to parent bounds
1331 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1332 shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001333 }
1334}
1335
Alec Mouri80863a62019-01-17 15:19:35 -08001336TEST_P(LayerRenderTypeTransactionTest, SetColorBasic) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001337 sp<SurfaceControl> bufferLayer;
1338 sp<SurfaceControl> colorLayer;
1339 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001340 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001341 ASSERT_NO_FATAL_FAILURE(colorLayer =
1342 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1343 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001344
Vishnu Nair88a11f22018-11-28 18:30:57 -08001345 Transaction()
1346 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1347 .setLayer(colorLayer, mLayerZBase + 1)
1348 .apply();
1349
Chia-I Wue4ef6102017-11-01 15:16:35 -07001350 {
1351 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08001352 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001353 }
1354
1355 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1356 const Color expected = {15, 51, 85, 255};
1357 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1358 // channel) should be less than one
1359 const uint8_t tolerance = 1;
1360 Transaction().setColor(colorLayer, color).apply();
1361 {
1362 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08001363 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001364 }
1365}
1366
Valerie Haudd0b7572019-01-29 14:59:27 -08001367// RED: Color layer base color and BufferQueueLayer/BufferStateLayer fill
1368// BLUE: prior background color
1369// GREEN: final background color
1370// BLACK: no color or fill
1371void LayerRenderTypeTransactionTest::setBackgroundColorHelper(uint32_t layerType, bool priorColor,
1372 bool bufferFill, float alpha,
1373 Color finalColor) {
1374 sp<SurfaceControl> layer;
1375 int32_t width = 500;
1376 int32_t height = 500;
Valerie Haua72e2812019-01-23 13:40:39 -08001377
Valerie Haudd0b7572019-01-29 14:59:27 -08001378 Color fillColor = Color::RED;
1379 Color priorBgColor = Color::BLUE;
1380 Color expectedColor = Color::BLACK;
1381 switch (layerType) {
1382 case ISurfaceComposerClient::eFXSurfaceColor:
1383 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 0, 0, layerType));
1384 Transaction()
1385 .setCrop_legacy(layer, Rect(0, 0, width, height))
1386 .setColor(layer, half3(1.0f, 0, 0))
1387 .apply();
1388 expectedColor = fillColor;
1389 break;
1390 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1391 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
1392 if (bufferFill) {
1393 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, fillColor, width, height));
1394 expectedColor = fillColor;
1395 }
1396 Transaction().setCrop_legacy(layer, Rect(0, 0, width, height)).apply();
1397 break;
1398 case ISurfaceComposerClient::eFXSurfaceBufferState:
1399 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height, layerType));
1400 if (bufferFill) {
1401 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, fillColor, width, height));
1402 expectedColor = fillColor;
1403 }
1404 Transaction().setFrame(layer, Rect(0, 0, width, height)).apply();
1405 break;
1406 default:
1407 GTEST_FAIL() << "Unknown layer type in setBackgroundColorHelper";
1408 return;
Valerie Haua72e2812019-01-23 13:40:39 -08001409 }
1410
Valerie Haudd0b7572019-01-29 14:59:27 -08001411 if (priorColor && layerType != ISurfaceComposerClient::eFXSurfaceColor) {
1412 Transaction()
1413 .setBackgroundColor(layer, half3(0, 0, 1.0f), 1.0f, ui::Dataspace::UNKNOWN)
1414 .apply();
1415 if (!bufferFill) {
1416 expectedColor = priorBgColor;
1417 }
1418 }
1419
1420 {
1421 SCOPED_TRACE("default before setting background color layer");
1422 screenshot()->expectColor(Rect(0, 0, width, height), expectedColor);
1423 }
Valerie Haua72e2812019-01-23 13:40:39 -08001424 Transaction()
Valerie Haudd0b7572019-01-29 14:59:27 -08001425 .setBackgroundColor(layer, half3(0, 1.0f, 0), alpha, ui::Dataspace::UNKNOWN)
Valerie Haua72e2812019-01-23 13:40:39 -08001426 .apply();
1427
1428 {
Valerie Haua72e2812019-01-23 13:40:39 -08001429 auto shot = screenshot();
Valerie Haudd0b7572019-01-29 14:59:27 -08001430 shot->expectColor(Rect(0, 0, width, height), finalColor);
1431 shot->expectBorder(Rect(0, 0, width, height), Color::BLACK);
Valerie Haua72e2812019-01-23 13:40:39 -08001432 }
1433}
1434
Valerie Haudd0b7572019-01-29 14:59:27 -08001435TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_Color_NoEffect) {
1436 bool priorColor = false;
1437 bool bufferFill = false;
1438 float alpha = 1.0f;
1439 Color finalColor = Color::RED;
1440 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceColor,
1441 priorColor, bufferFill, alpha, finalColor));
1442}
Valerie Haua72e2812019-01-23 13:40:39 -08001443
Valerie Haudd0b7572019-01-29 14:59:27 -08001444TEST_P(LayerRenderTypeTransactionTest,
1445 SetBackgroundColor_BufferQueue_BufferFill_NoPriorColor_Basic) {
1446 bool priorColor = false;
1447 bool bufferFill = true;
1448 float alpha = 1.0f;
1449 Color finalColor = Color::RED;
1450 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1451 priorColor, bufferFill, alpha, finalColor));
1452}
Valerie Haua72e2812019-01-23 13:40:39 -08001453
Valerie Haudd0b7572019-01-29 14:59:27 -08001454TEST_P(LayerRenderTypeTransactionTest,
1455 SetBackgroundColor_BufferQueue_NoBufferFill_NoPriorColor_Basic) {
1456 bool priorColor = false;
1457 bool bufferFill = false;
1458 float alpha = 1.0f;
1459 Color finalColor = Color::GREEN;
1460 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1461 priorColor, bufferFill, alpha, finalColor));
1462}
Valerie Haua72e2812019-01-23 13:40:39 -08001463
Valerie Haudd0b7572019-01-29 14:59:27 -08001464TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_BufferQueue_BufferFill_PriorColor_Basic) {
1465 bool priorColor = true;
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}
1472
1473TEST_P(LayerRenderTypeTransactionTest,
1474 SetBackgroundColor_BufferQueue_NoBufferFill_PriorColor_Basic) {
1475 bool priorColor = true;
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}
1482TEST_P(LayerRenderTypeTransactionTest,
1483 SetBackgroundColor_BufferQueue_NoPriorColor_ZeroAlpha_NoEffect) {
1484 bool priorColor = false;
1485 bool bufferFill = false;
1486 float alpha = 0;
1487 Color finalColor = Color::BLACK;
1488 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1489 priorColor, bufferFill, alpha, finalColor));
1490}
1491
1492TEST_P(LayerRenderTypeTransactionTest,
1493 SetBackgroundColor_BufferQueue_PriorColor_ZeroAlpha_DeleteBackground) {
1494 bool priorColor = true;
1495 bool bufferFill = false;
1496 float alpha = 0;
1497 Color finalColor = Color::BLACK;
1498 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1499 priorColor, bufferFill, alpha, finalColor));
1500}
1501
1502TEST_P(LayerRenderTypeTransactionTest,
1503 SetBackgroundColor_BufferState_BufferFill_NoPriorColor_Basic) {
1504 bool priorColor = false;
1505 bool bufferFill = true;
1506 float alpha = 1.0f;
1507 Color finalColor = Color::RED;
Valerie Haua6b15a12019-02-05 14:16:30 -08001508 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001509 priorColor, bufferFill, alpha, finalColor));
1510}
1511
1512TEST_P(LayerRenderTypeTransactionTest,
1513 SetBackgroundColor_BufferState_NoBufferFill_NoPriorColor_Basic) {
1514 bool priorColor = false;
1515 bool bufferFill = false;
1516 float alpha = 1.0f;
1517 Color finalColor = Color::GREEN;
Valerie Haua6b15a12019-02-05 14:16:30 -08001518 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001519 priorColor, bufferFill, alpha, finalColor));
1520}
1521
1522TEST_P(LayerRenderTypeTransactionTest,
1523 SetBackgroundColor_BufferState_NoBufferFill_PriorColor_Basic) {
1524 bool priorColor = true;
1525 bool bufferFill = false;
1526 float alpha = 1.0f;
1527 Color finalColor = Color::GREEN;
Valerie Haua6b15a12019-02-05 14:16:30 -08001528 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001529 priorColor, bufferFill, alpha, finalColor));
1530}
1531
1532TEST_P(LayerRenderTypeTransactionTest,
1533 SetBackgroundColor_BufferState_NoPriorColor_ZeroAlpha_NoEffect) {
1534 bool priorColor = false;
1535 bool bufferFill = false;
1536 float alpha = 0;
1537 Color finalColor = Color::BLACK;
Valerie Haua6b15a12019-02-05 14:16:30 -08001538 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001539 priorColor, bufferFill, alpha, finalColor));
1540}
1541
1542TEST_P(LayerRenderTypeTransactionTest,
1543 SetBackgroundColor_BufferState_PriorColor_ZeroAlpha_DeleteBackground) {
1544 bool priorColor = true;
1545 bool bufferFill = false;
1546 float alpha = 0;
1547 Color finalColor = Color::BLACK;
Valerie Haua6b15a12019-02-05 14:16:30 -08001548 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001549 priorColor, bufferFill, alpha, finalColor));
Valerie Haua72e2812019-01-23 13:40:39 -08001550}
1551
Alec Mouri80863a62019-01-17 15:19:35 -08001552TEST_P(LayerRenderTypeTransactionTest, SetColorClamped) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001553 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001554 ASSERT_NO_FATAL_FAILURE(colorLayer =
1555 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1556 ISurfaceComposerClient::eFXSurfaceColor));
1557 Transaction()
1558 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1559 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1560 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001561
Alec Mouri80863a62019-01-17 15:19:35 -08001562 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001563}
1564
Alec Mouri80863a62019-01-17 15:19:35 -08001565TEST_P(LayerRenderTypeTransactionTest, SetColorWithAlpha) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001566 sp<SurfaceControl> bufferLayer;
1567 sp<SurfaceControl> colorLayer;
1568 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001569 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001570 ASSERT_NO_FATAL_FAILURE(colorLayer =
1571 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1572 ISurfaceComposerClient::eFXSurfaceColor));
1573 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001574
1575 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1576 const float alpha = 0.25f;
1577 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1578 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1579 // channel) should be less than one
1580 const uint8_t tolerance = 1;
1581 Transaction()
1582 .setColor(colorLayer, color)
1583 .setAlpha(colorLayer, alpha)
1584 .setLayer(colorLayer, mLayerZBase + 1)
1585 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001586 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1587 tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001588}
1589
Alec Mouri80863a62019-01-17 15:19:35 -08001590TEST_P(LayerRenderTypeTransactionTest, SetColorWithParentAlpha_Bug74220420) {
Adrian Roosb7a96502018-04-08 11:38:55 -07001591 sp<SurfaceControl> bufferLayer;
1592 sp<SurfaceControl> parentLayer;
1593 sp<SurfaceControl> colorLayer;
1594 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1595 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001596 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001597 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1598 0 /* buffer height */,
1599 ISurfaceComposerClient::eFXSurfaceColor));
1600 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001601 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1602 const float alpha = 0.25f;
1603 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1604 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1605 // channel) should be less than one
1606 const uint8_t tolerance = 1;
1607 Transaction()
1608 .reparent(colorLayer, parentLayer->getHandle())
1609 .setColor(colorLayer, color)
1610 .setAlpha(parentLayer, alpha)
1611 .setLayer(parentLayer, mLayerZBase + 1)
1612 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001613 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1614 tolerance);
Adrian Roosb7a96502018-04-08 11:38:55 -07001615}
1616
Alec Mouri80863a62019-01-17 15:19:35 -08001617TEST_P(LayerTypeAndRenderTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001618 sp<SurfaceControl> bufferLayer;
1619 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001620 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001621
1622 // color is ignored
1623 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001624 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001625}
1626
Alec Mouri80863a62019-01-17 15:19:35 -08001627TEST_P(LayerTypeAndRenderTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001628 sp<SurfaceControl> layer;
1629 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001630 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001631
1632 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1633 {
1634 SCOPED_TRACE("non-existing layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001635 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001636 }
1637
1638 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1639 {
1640 SCOPED_TRACE("original layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001641 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001642 }
1643}
1644
Alec Mouri80863a62019-01-17 15:19:35 -08001645TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001646 sp<SurfaceControl> layer;
1647 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001648 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1649 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001650
1651 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1652 {
1653 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001654 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1655 Color::BLUE, Color::WHITE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001656 }
1657
1658 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1659 {
1660 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001661 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED,
1662 Color::WHITE, Color::BLUE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001663 }
1664
1665 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1666 {
1667 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001668 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE,
1669 Color::RED, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001670 }
1671
1672 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1673 {
1674 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001675 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED,
1676 Color::WHITE, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001677 }
1678
1679 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1680 {
1681 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001682 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN,
1683 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001684 }
1685}
1686
Alec Mouri80863a62019-01-17 15:19:35 -08001687TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001688 sp<SurfaceControl> layer;
1689 ASSERT_NO_FATAL_FAILURE(
1690 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1691 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1692 Color::BLUE, Color::WHITE));
1693
1694 Transaction()
1695 .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
1696 .setFrame(layer, Rect(0, 0, 32, 32))
1697 .apply();
1698 {
1699 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001700 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1701 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001702 }
1703
1704 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
1705 {
1706 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001707 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1708 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001709 }
1710
1711 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
1712 {
1713 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001714 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1715 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001716 }
1717
1718 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
1719 {
1720 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001721 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1722 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001723 }
1724
1725 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
1726 {
1727 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001728 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1729 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001730 }
1731}
1732
Alec Mouri80863a62019-01-17 15:19:35 -08001733TEST_P(LayerRenderTypeTransactionTest, SetMatrixRot45_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001734 sp<SurfaceControl> layer;
1735 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001736 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1737 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001738
1739 const float rot = M_SQRT1_2; // 45 degrees
1740 const float trans = M_SQRT2 * 16.0f;
1741 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1742
Alec Mouri80863a62019-01-17 15:19:35 -08001743 auto shot = getScreenCapture();
Chia-I Wu93853fe2017-11-02 08:30:27 -07001744 // check a 8x8 region inside each color
1745 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1746 const int32_t halfL = 4;
1747 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1748 };
1749 const int32_t unit = int32_t(trans / 2);
1750 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1751 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1752 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1753 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1754}
1755
Alec Mouri80863a62019-01-17 15:19:35 -08001756TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithResize_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001757 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -07001758 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1759 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001760
1761 // setMatrix is applied after any pending resize, unlike setPosition
1762 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1763 {
1764 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001765 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001766 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -07001767 shot->expectColor(rect, Color::RED);
1768 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001769 }
1770
Marissa Wall861616d2018-10-22 12:52:23 -07001771 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001772 {
1773 SCOPED_TRACE("resize applied");
Marissa Wall861616d2018-10-22 12:52:23 -07001774 const Rect rect(0, 0, 128, 128);
Alec Mouri80863a62019-01-17 15:19:35 -08001775 getScreenCapture()->expectColor(rect, Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001776 }
1777}
1778
Alec Mouri80863a62019-01-17 15:19:35 -08001779TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001780 sp<SurfaceControl> layer;
1781 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001782 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001783
1784 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1785 Transaction()
1786 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1787 .setSize(layer, 64, 64)
1788 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1789 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001790 getScreenCapture()->expectColor(Rect(0, 0, 128, 128), Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001791}
1792
Alec Mouri80863a62019-01-17 15:19:35 -08001793TEST_P(LayerRenderTypeTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001794 sp<SurfaceControl> layer;
1795 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001796 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1797 Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001798
1799 // XXX SCALE_CROP is not respected; calling setSize and
1800 // setOverrideScalingMode in separate transactions does not work
1801 // (b/69315456)
1802 Transaction()
1803 .setSize(layer, 64, 16)
1804 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1805 .apply();
1806 {
1807 SCOPED_TRACE("SCALE_TO_WINDOW");
Alec Mouri80863a62019-01-17 15:19:35 -08001808 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN,
1809 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wua56b2042017-11-01 15:16:35 -07001810 }
1811}
1812
Dan Stoza000dd012018-08-01 13:31:52 -07001813TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1814 sp<SurfaceControl> layer;
1815 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1816
1817 sp<IBinder> handle = layer->getHandle();
1818 ASSERT_TRUE(handle != nullptr);
1819
1820 FrameStats frameStats;
1821 mClient->getLayerFrameStats(handle, &frameStats);
1822
1823 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1824}
1825
Alec Mouri80863a62019-01-17 15:19:35 -08001826TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001827 sp<SurfaceControl> layer;
1828 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001829 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001830 const Rect crop(8, 8, 24, 24);
1831
Marissa Wallf58c14b2018-07-24 10:50:43 -07001832 Transaction().setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001833 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001834 shot->expectColor(crop, Color::RED);
1835 shot->expectBorder(crop, Color::BLACK);
1836}
1837
Alec Mouri80863a62019-01-17 15:19:35 -08001838TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001839 sp<SurfaceControl> layer;
1840 ASSERT_NO_FATAL_FAILURE(
1841 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1842 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1843 const Rect crop(8, 8, 24, 24);
1844
1845 Transaction().setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001846 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001847 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
1848 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001849}
1850
Alec Mouri80863a62019-01-17 15:19:35 -08001851TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001852 sp<SurfaceControl> layer;
1853 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001854 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001855
1856 {
1857 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001858 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001859 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07001860 }
1861
1862 {
1863 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001864 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001865 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07001866 }
1867}
1868
Alec Mouri80863a62019-01-17 15:19:35 -08001869TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001870 sp<SurfaceControl> layer;
1871 ASSERT_NO_FATAL_FAILURE(
1872 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1873 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1874
1875 {
1876 SCOPED_TRACE("empty rect");
1877 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
Marissa Wall290ad082019-03-06 13:23:47 -08001878 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07001879 }
1880
1881 {
1882 SCOPED_TRACE("negative rect");
1883 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
Marissa Wall290ad082019-03-06 13:23:47 -08001884 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07001885 }
1886}
1887
Alec Mouri80863a62019-01-17 15:19:35 -08001888TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001889 sp<SurfaceControl> layer;
1890 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001891 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001892
Marissa Wallf58c14b2018-07-24 10:50:43 -07001893 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001894 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001895 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1896 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1897}
1898
Alec Mouri80863a62019-01-17 15:19:35 -08001899TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferState) {
Valerie Hau0bc09152018-12-20 07:42:47 -08001900 sp<SurfaceControl> layer;
Marissa Wall290ad082019-03-06 13:23:47 -08001901 ASSERT_NO_FATAL_FAILURE(
1902 layer = createLayer("test", 32, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
Valerie Hau0bc09152018-12-20 07:42:47 -08001903 sp<GraphicBuffer> buffer =
Marissa Wall290ad082019-03-06 13:23:47 -08001904 new GraphicBuffer(32, 64, PIXEL_FORMAT_RGBA_8888, 1,
Valerie Hau0bc09152018-12-20 07:42:47 -08001905 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1906 BufferUsage::COMPOSER_OVERLAY,
1907 "test");
Marissa Wall290ad082019-03-06 13:23:47 -08001908 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 16), Color::BLUE);
1909 fillGraphicBufferColor(buffer, Rect(0, 16, 32, 64), Color::RED);
1910
1911 Transaction().setFrame(layer, Rect(0, 0, 64, 64)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001912
1913 Transaction().setBuffer(layer, buffer).apply();
1914
1915 // Partially out of bounds in the negative (upper left) direction
Marissa Wall290ad082019-03-06 13:23:47 -08001916 Transaction().setCrop(layer, Rect(-128, -128, 32, 16)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001917 {
1918 SCOPED_TRACE("out of bounds, negative (upper left) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08001919 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001920 shot->expectColor(Rect(0, 0, 64, 64), Color::BLUE);
1921 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001922 }
1923
1924 // Partially out of bounds in the positive (lower right) direction
Marissa Wall290ad082019-03-06 13:23:47 -08001925 Transaction().setCrop(layer, Rect(0, 16, 128, 128)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001926 {
1927 SCOPED_TRACE("out of bounds, positive (lower right) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08001928 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001929 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1930 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001931 }
1932
1933 // Fully out of buffer space bounds
1934 Transaction().setCrop(layer, Rect(-128, -128, -1, -1)).apply();
1935 {
1936 SCOPED_TRACE("Fully out of bounds");
Alec Mouri80863a62019-01-17 15:19:35 -08001937 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001938 shot->expectColor(Rect(0, 0, 64, 16), Color::BLUE);
1939 shot->expectColor(Rect(0, 16, 64, 64), Color::RED);
1940 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001941 }
1942}
1943
Alec Mouri80863a62019-01-17 15:19:35 -08001944TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001945 sp<SurfaceControl> layer;
1946 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001947 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001948
1949 const Point position(32, 32);
1950 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001951 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001952 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001953 shot->expectColor(crop + position, Color::RED);
1954 shot->expectBorder(crop + position, Color::BLACK);
1955}
1956
Alec Mouri80863a62019-01-17 15:19:35 -08001957TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001958 sp<SurfaceControl> layer;
1959 ASSERT_NO_FATAL_FAILURE(
1960 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1961 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1962
Marissa Wall861616d2018-10-22 12:52:23 -07001963 const Rect frame(32, 32, 64, 64);
Marissa Wall61c58622018-07-18 10:12:20 -07001964 const Rect crop(8, 8, 24, 24);
Marissa Wall861616d2018-10-22 12:52:23 -07001965 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001966 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001967 shot->expectColor(frame, Color::RED);
1968 shot->expectBorder(frame, Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001969}
1970
Alec Mouri80863a62019-01-17 15:19:35 -08001971TEST_P(LayerRenderTypeTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001972 sp<SurfaceControl> layer;
1973 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001974 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001975
Marissa Wall861616d2018-10-22 12:52:23 -07001976 // crop_legacy is affected by matrix
Chia-I Wu04dcca82017-11-02 08:30:27 -07001977 Transaction()
1978 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001979 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001980 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001981 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001982 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1983 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1984}
1985
Alec Mouri80863a62019-01-17 15:19:35 -08001986TEST_P(LayerRenderTypeTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001987 sp<SurfaceControl> layer;
1988 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001989 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001990
Marissa Wallf58c14b2018-07-24 10:50:43 -07001991 // setCrop_legacy is applied immediately by default, with or without resize pending
1992 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001993 {
1994 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001995 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001996 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1997 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
1998 }
1999
Marissa Wall61c58622018-07-18 10:12:20 -07002000 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002001 {
2002 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002003 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002004 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
2005 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
2006 }
2007}
2008
Alec Mouri80863a62019-01-17 15:19:35 -08002009TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002010 sp<SurfaceControl> layer;
2011 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002012 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002013
Marissa Wallf58c14b2018-07-24 10:50:43 -07002014 // request setCrop_legacy to be applied with the next resize
2015 Transaction()
2016 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
2017 .setGeometryAppliesWithResize(layer)
2018 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002019 {
2020 SCOPED_TRACE("waiting for next resize");
Alec Mouri80863a62019-01-17 15:19:35 -08002021 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002022 }
2023
Marissa Wallf58c14b2018-07-24 10:50:43 -07002024 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002025 {
2026 SCOPED_TRACE("pending crop modified");
Alec Mouri80863a62019-01-17 15:19:35 -08002027 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002028 }
2029
2030 Transaction().setSize(layer, 16, 16).apply();
2031 {
2032 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002033 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002034 }
2035
2036 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07002037 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002038 {
2039 SCOPED_TRACE("new crop applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002040 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002041 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2042 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2043 }
2044}
2045
Alec Mouri80863a62019-01-17 15:19:35 -08002046TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002047 sp<SurfaceControl> layer;
2048 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002049 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002050
Marissa Wallf58c14b2018-07-24 10:50:43 -07002051 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07002052 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07002053 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07002054 .setSize(layer, 16, 16)
2055 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
2056 .setGeometryAppliesWithResize(layer)
2057 .apply();
2058 {
2059 SCOPED_TRACE("new crop pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002060 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002061 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
2062 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
2063 }
2064
2065 // XXX crop is never latched without other geometry change (b/69315677)
2066 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002067 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002068 Transaction().setPosition(layer, 0, 0).apply();
2069 {
2070 SCOPED_TRACE("new crop applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002071 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002072 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2073 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2074 }
2075}
2076
Alec Mouri80863a62019-01-17 15:19:35 -08002077TEST_P(LayerRenderTypeTransactionTest, SetFrameBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002078 sp<SurfaceControl> layer;
2079 ASSERT_NO_FATAL_FAILURE(
2080 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2081 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2082 const Rect frame(8, 8, 24, 24);
2083
2084 Transaction().setFrame(layer, frame).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002085 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002086 shot->expectColor(frame, Color::RED);
2087 shot->expectBorder(frame, Color::BLACK);
2088}
2089
Alec Mouri80863a62019-01-17 15:19:35 -08002090TEST_P(LayerRenderTypeTransactionTest, SetFrameEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002091 sp<SurfaceControl> layer;
2092 ASSERT_NO_FATAL_FAILURE(
2093 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2094 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2095
Marissa Wall61c58622018-07-18 10:12:20 -07002096 {
Marissa Wall861616d2018-10-22 12:52:23 -07002097 SCOPED_TRACE("empty rect");
2098 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002099 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002100 }
2101
Marissa Wall61c58622018-07-18 10:12:20 -07002102 {
Marissa Wall861616d2018-10-22 12:52:23 -07002103 SCOPED_TRACE("negative rect");
2104 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002105 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002106 }
2107}
2108
Alec Mouri80863a62019-01-17 15:19:35 -08002109TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultParentless_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -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, 10, 10));
2114
2115 // A parentless layer will default to a frame with the same size as the buffer
Alec Mouri80863a62019-01-17 15:19:35 -08002116 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002117 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2118 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall861616d2018-10-22 12:52:23 -07002119}
2120
Alec Mouri80863a62019-01-17 15:19:35 -08002121TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBSParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002122 sp<SurfaceControl> parent, child;
2123 ASSERT_NO_FATAL_FAILURE(
2124 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2125 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2126 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2127
2128 ASSERT_NO_FATAL_FAILURE(
2129 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2130 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2131
2132 Transaction().reparent(child, parent->getHandle()).apply();
2133
2134 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002135 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002136 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2137 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2138}
2139
Alec Mouri80863a62019-01-17 15:19:35 -08002140TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBQParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002141 sp<SurfaceControl> parent, child;
2142 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
2143 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
2144
2145 ASSERT_NO_FATAL_FAILURE(
2146 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2147 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2148
2149 Transaction().reparent(child, parent->getHandle()).apply();
2150
2151 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002152 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002153 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2154 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2155}
2156
Alec Mouri80863a62019-01-17 15:19:35 -08002157TEST_P(LayerRenderTypeTransactionTest, SetFrameUpdate_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002158 sp<SurfaceControl> layer;
2159 ASSERT_NO_FATAL_FAILURE(
2160 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2161 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2162 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
2163
2164 std::this_thread::sleep_for(500ms);
2165
2166 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
2167
Alec Mouri80863a62019-01-17 15:19:35 -08002168 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002169 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2170 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2171}
2172
Alec Mouri80863a62019-01-17 15:19:35 -08002173TEST_P(LayerRenderTypeTransactionTest, SetFrameOutsideBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002174 sp<SurfaceControl> parent, child;
2175 ASSERT_NO_FATAL_FAILURE(
2176 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2177 ASSERT_NO_FATAL_FAILURE(
2178 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2179 Transaction().reparent(child, parent->getHandle()).apply();
2180
2181 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2182 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2183
2184 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2185 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
2186
Alec Mouri80863a62019-01-17 15:19:35 -08002187 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002188 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
2189 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
2190 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2191}
2192
Alec Mouri80863a62019-01-17 15:19:35 -08002193TEST_P(LayerRenderTypeTransactionTest, SetBufferBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002194 sp<SurfaceControl> layer;
2195 ASSERT_NO_FATAL_FAILURE(
2196 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2197
2198 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2199
Alec Mouri80863a62019-01-17 15:19:35 -08002200 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002201 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2202 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002203}
2204
Alec Mouri80863a62019-01-17 15:19:35 -08002205TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleBuffers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002206 sp<SurfaceControl> layer;
2207 ASSERT_NO_FATAL_FAILURE(
2208 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2209
2210 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2211
2212 {
2213 SCOPED_TRACE("set buffer 1");
Alec Mouri80863a62019-01-17 15:19:35 -08002214 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002215 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2216 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002217 }
2218
2219 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
2220
2221 {
2222 SCOPED_TRACE("set buffer 2");
Alec Mouri80863a62019-01-17 15:19:35 -08002223 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002224 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLUE);
2225 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002226 }
2227
2228 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2229
2230 {
2231 SCOPED_TRACE("set buffer 3");
Alec Mouri80863a62019-01-17 15:19:35 -08002232 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002233 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2234 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002235 }
2236}
2237
Alec Mouri80863a62019-01-17 15:19:35 -08002238TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleLayers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002239 sp<SurfaceControl> layer1;
2240 ASSERT_NO_FATAL_FAILURE(
2241 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2242
2243 sp<SurfaceControl> layer2;
2244 ASSERT_NO_FATAL_FAILURE(
2245 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2246
2247 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2248
Marissa Wall861616d2018-10-22 12:52:23 -07002249 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002250 {
2251 SCOPED_TRACE("set layer 1 buffer red");
Alec Mouri80863a62019-01-17 15:19:35 -08002252 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002253 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2254 }
2255
2256 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2257
Marissa Wall861616d2018-10-22 12:52:23 -07002258 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002259 {
2260 SCOPED_TRACE("set layer 2 buffer blue");
Alec Mouri80863a62019-01-17 15:19:35 -08002261 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002262 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2263 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2264 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2265 }
2266
2267 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2268 {
2269 SCOPED_TRACE("set layer 1 buffer green");
Alec Mouri80863a62019-01-17 15:19:35 -08002270 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002271 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2272 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2273 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2274 }
2275
2276 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2277
2278 {
2279 SCOPED_TRACE("set layer 2 buffer white");
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::WHITE);
2282 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2283 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2284 }
2285}
2286
Valerie Haua6b15a12019-02-05 14:16:30 -08002287TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002288 sp<SurfaceControl> layer;
2289 ASSERT_NO_FATAL_FAILURE(
2290 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2291
2292 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2293
2294 std::array<sp<GraphicBuffer>, 10> buffers;
2295
2296 size_t idx = 0;
2297 for (auto& buffer : buffers) {
2298 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2299 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2300 BufferUsage::COMPOSER_OVERLAY,
2301 "test");
2302 Color color = colors[idx % colors.size()];
2303 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2304 idx++;
2305 }
2306
2307 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2308 // cache is working.
2309 idx = 0;
2310 for (auto& buffer : buffers) {
2311 for (int i = 0; i < 2; i++) {
2312 Transaction().setBuffer(layer, buffer).apply();
2313
2314 Color color = colors[idx % colors.size()];
2315 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002316 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2317 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002318 }
2319 idx++;
2320 }
2321}
2322
Valerie Haua6b15a12019-02-05 14:16:30 -08002323TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_LeastRecentlyUsed_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002324 sp<SurfaceControl> layer;
2325 ASSERT_NO_FATAL_FAILURE(
2326 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2327
2328 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2329
2330 std::array<sp<GraphicBuffer>, 70> buffers;
2331
2332 size_t idx = 0;
2333 for (auto& buffer : buffers) {
2334 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2335 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2336 BufferUsage::COMPOSER_OVERLAY,
2337 "test");
2338 Color color = colors[idx % colors.size()];
2339 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2340 idx++;
2341 }
2342
2343 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2344 // cache is working.
2345 idx = 0;
2346 for (auto& buffer : buffers) {
2347 for (int i = 0; i < 2; i++) {
2348 Transaction().setBuffer(layer, buffer).apply();
2349
2350 Color color = colors[idx % colors.size()];
2351 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002352 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2353 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002354 }
2355 idx++;
2356 }
2357}
2358
Valerie Haua6b15a12019-02-05 14:16:30 -08002359TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_DestroyedBuffer_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002360 sp<SurfaceControl> layer;
2361 ASSERT_NO_FATAL_FAILURE(
2362 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2363
2364 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2365
2366 std::array<sp<GraphicBuffer>, 65> buffers;
2367
2368 size_t idx = 0;
2369 for (auto& buffer : buffers) {
2370 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2371 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2372 BufferUsage::COMPOSER_OVERLAY,
2373 "test");
2374 Color color = colors[idx % colors.size()];
2375 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2376 idx++;
2377 }
2378
2379 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2380 // cache is working.
2381 idx = 0;
2382 for (auto& buffer : buffers) {
2383 for (int i = 0; i < 2; i++) {
2384 Transaction().setBuffer(layer, buffer).apply();
2385
2386 Color color = colors[idx % colors.size()];
2387 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002388 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2389 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002390 }
2391 if (idx == 0) {
2392 buffers[0].clear();
2393 }
2394 idx++;
2395 }
2396}
2397
Alec Mouri80863a62019-01-17 15:19:35 -08002398TEST_P(LayerRenderTypeTransactionTest, SetTransformRotate90_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002399 sp<SurfaceControl> layer;
2400 ASSERT_NO_FATAL_FAILURE(
2401 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2402
2403 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2404 Color::BLUE, Color::WHITE));
2405
Marissa Wall861616d2018-10-22 12:52:23 -07002406 Transaction()
2407 .setFrame(layer, Rect(0, 0, 32, 32))
2408 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2409 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002410
Alec Mouri80863a62019-01-17 15:19:35 -08002411 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2412 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002413}
2414
Alec Mouri80863a62019-01-17 15:19:35 -08002415TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipH_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002416 sp<SurfaceControl> layer;
2417 ASSERT_NO_FATAL_FAILURE(
2418 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2419
2420 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2421 Color::BLUE, Color::WHITE));
2422
Marissa Wall861616d2018-10-22 12:52:23 -07002423 Transaction()
2424 .setFrame(layer, Rect(0, 0, 32, 32))
2425 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2426 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002427
Alec Mouri80863a62019-01-17 15:19:35 -08002428 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2429 Color::BLUE, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002430}
2431
Alec Mouri80863a62019-01-17 15:19:35 -08002432TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipV_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002433 sp<SurfaceControl> layer;
2434 ASSERT_NO_FATAL_FAILURE(
2435 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2436
2437 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2438 Color::BLUE, Color::WHITE));
2439
Marissa Wall861616d2018-10-22 12:52:23 -07002440 Transaction()
2441 .setFrame(layer, Rect(0, 0, 32, 32))
2442 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2443 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002444
Alec Mouri80863a62019-01-17 15:19:35 -08002445 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2446 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002447}
2448
2449TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2450 sp<SurfaceControl> layer;
2451 ASSERT_NO_FATAL_FAILURE(
2452 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2453
2454 Transaction().setTransformToDisplayInverse(layer, false).apply();
2455
2456 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2457
2458 Transaction().setTransformToDisplayInverse(layer, true).apply();
2459}
2460
Alec Mouri80863a62019-01-17 15:19:35 -08002461TEST_P(LayerRenderTypeTransactionTest, SetFenceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002462 sp<SurfaceControl> layer;
Marissa Wall713b63f2018-10-17 15:42:43 -07002463 Transaction transaction;
2464 ASSERT_NO_FATAL_FAILURE(
2465 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2466
2467 sp<GraphicBuffer> buffer =
2468 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2469 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2470 BufferUsage::COMPOSER_OVERLAY,
2471 "test");
2472 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2473
2474 sp<Fence> fence;
2475 if (getBuffer(nullptr, &fence) != NO_ERROR) {
2476 GTEST_SUCCEED() << "test not supported";
2477 return;
2478 }
2479
2480 Transaction().setBuffer(layer, buffer).setAcquireFence(layer, fence).apply();
2481
2482 status_t status = fence->wait(1000);
2483 ASSERT_NE(static_cast<status_t>(Fence::Status::Unsignaled), status);
2484 std::this_thread::sleep_for(200ms);
2485
Alec Mouri80863a62019-01-17 15:19:35 -08002486 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002487 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2488 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall713b63f2018-10-17 15:42:43 -07002489}
2490
Alec Mouri80863a62019-01-17 15:19:35 -08002491TEST_P(LayerRenderTypeTransactionTest, SetFenceNull_BufferState) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002492 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07002493 ASSERT_NO_FATAL_FAILURE(
2494 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2495
2496 sp<GraphicBuffer> buffer =
2497 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2498 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2499 BufferUsage::COMPOSER_OVERLAY,
2500 "test");
2501 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2502
Marissa Wallfda30bb2018-10-12 11:34:28 -07002503 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002504
2505 Transaction()
2506 .setBuffer(layer, buffer)
2507 .setAcquireFence(layer, fence)
Marissa Wall61c58622018-07-18 10:12:20 -07002508 .apply();
2509
Alec Mouri80863a62019-01-17 15:19:35 -08002510 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002511 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2512 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002513}
2514
Alec Mouri80863a62019-01-17 15:19:35 -08002515TEST_P(LayerRenderTypeTransactionTest, SetDataspaceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002516 sp<SurfaceControl> layer;
2517 ASSERT_NO_FATAL_FAILURE(
2518 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2519
2520 sp<GraphicBuffer> buffer =
2521 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2522 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2523 BufferUsage::COMPOSER_OVERLAY,
2524 "test");
2525 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2526
2527 Transaction()
2528 .setBuffer(layer, buffer)
2529 .setDataspace(layer, ui::Dataspace::UNKNOWN)
Marissa Wall61c58622018-07-18 10:12:20 -07002530 .apply();
2531
Alec Mouri80863a62019-01-17 15:19:35 -08002532 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002533 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2534 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002535}
2536
Alec Mouri80863a62019-01-17 15:19:35 -08002537TEST_P(LayerRenderTypeTransactionTest, SetHdrMetadataBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002538 sp<SurfaceControl> layer;
2539 ASSERT_NO_FATAL_FAILURE(
2540 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2541
2542 sp<GraphicBuffer> buffer =
2543 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2544 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2545 BufferUsage::COMPOSER_OVERLAY,
2546 "test");
2547 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2548
2549 HdrMetadata hdrMetadata;
2550 hdrMetadata.validTypes = 0;
2551 Transaction()
2552 .setBuffer(layer, buffer)
2553 .setHdrMetadata(layer, hdrMetadata)
Marissa Wall61c58622018-07-18 10:12:20 -07002554 .apply();
2555
Alec Mouri80863a62019-01-17 15:19:35 -08002556 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002557 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2558 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002559}
2560
Alec Mouri80863a62019-01-17 15:19:35 -08002561TEST_P(LayerRenderTypeTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002562 sp<SurfaceControl> layer;
2563 ASSERT_NO_FATAL_FAILURE(
2564 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2565
2566 sp<GraphicBuffer> buffer =
2567 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2568 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2569 BufferUsage::COMPOSER_OVERLAY,
2570 "test");
2571 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2572
2573 Region region;
2574 region.set(32, 32);
2575 Transaction()
2576 .setBuffer(layer, buffer)
2577 .setSurfaceDamageRegion(layer, region)
Marissa Wall61c58622018-07-18 10:12:20 -07002578 .apply();
2579
Alec Mouri80863a62019-01-17 15:19:35 -08002580 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002581 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2582 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002583}
2584
Alec Mouri80863a62019-01-17 15:19:35 -08002585TEST_P(LayerRenderTypeTransactionTest, SetApiBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002586 sp<SurfaceControl> layer;
2587 ASSERT_NO_FATAL_FAILURE(
2588 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2589
2590 sp<GraphicBuffer> buffer =
2591 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2592 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2593 BufferUsage::COMPOSER_OVERLAY,
2594 "test");
2595 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2596
2597 Transaction()
2598 .setBuffer(layer, buffer)
2599 .setApi(layer, NATIVE_WINDOW_API_CPU)
Marissa Wall61c58622018-07-18 10:12:20 -07002600 .apply();
2601
Alec Mouri80863a62019-01-17 15:19:35 -08002602 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002603 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2604 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002605}
2606
2607TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2608 sp<SurfaceControl> layer;
2609 ASSERT_NO_FATAL_FAILURE(
2610 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2611
2612 // verify this doesn't cause a crash
2613 Transaction().setSidebandStream(layer, nullptr).apply();
2614}
2615
Robert Carr54cf5b12019-01-25 14:02:28 -08002616TEST_F(LayerTransactionTest, ReparentToSelf) {
2617 sp<SurfaceControl> layer;
2618 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2619 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2620 Transaction().reparent(layer, layer->getHandle()).apply();
2621
2622 {
2623 // We expect the transaction to be silently dropped, but for SurfaceFlinger
2624 // to still be functioning.
2625 SCOPED_TRACE("after reparent to self");
2626 const Rect rect(0, 0, 32, 32);
2627 auto shot = screenshot();
2628 shot->expectColor(rect, Color::RED);
2629 shot->expectBorder(rect, Color::BLACK);
2630 }
2631}
2632
Alec Mouri80863a62019-01-17 15:19:35 -08002633TEST_P(LayerRenderTypeTransactionTest, SetColorTransformBasic) {
Peiyong Lind3788632018-09-18 16:01:31 -07002634 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002635 ASSERT_NO_FATAL_FAILURE(colorLayer =
2636 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2637 ISurfaceComposerClient::eFXSurfaceColor));
2638 Transaction()
2639 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2640 .setLayer(colorLayer, mLayerZBase + 1)
2641 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002642 {
2643 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002644 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Peiyong Lind3788632018-09-18 16:01:31 -07002645 }
2646
2647 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002648 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002649 mat3 matrix;
2650 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2651 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2652 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002653
2654 // degamma before applying the matrix
2655 if (mColorManagementUsed) {
2656 ColorTransformHelper::DegammaColor(expected);
2657 }
2658
2659 ColorTransformHelper::applyMatrix(expected, matrix);
2660
2661 if (mColorManagementUsed) {
2662 ColorTransformHelper::GammaColor(expected);
2663 }
2664
2665 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2666 uint8_t(expected.b * 255), 255};
2667
2668 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2669 // channel) should be less than one
2670 const uint8_t tolerance = 1;
2671
Peiyong Lind3788632018-09-18 16:01:31 -07002672 Transaction().setColor(colorLayer, color)
2673 .setColorTransform(colorLayer, matrix, vec3()).apply();
2674 {
2675 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002676 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002677 }
2678}
2679
Alec Mouri80863a62019-01-17 15:19:35 -08002680TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnParent) {
chaviwf66724d2018-11-28 16:35:21 -08002681 sp<SurfaceControl> parentLayer;
2682 sp<SurfaceControl> colorLayer;
2683 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2684 0 /* buffer height */,
2685 ISurfaceComposerClient::eFXSurfaceContainer));
2686 ASSERT_NO_FATAL_FAILURE(
2687 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2688 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2689
2690 Transaction()
2691 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2692 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2693 .setLayer(parentLayer, mLayerZBase + 1)
2694 .apply();
2695 {
2696 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002697 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08002698 }
2699
2700 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2701 half3 expected = color;
2702 mat3 matrix;
2703 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2704 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2705 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2706
2707 // degamma before applying the matrix
2708 if (mColorManagementUsed) {
2709 ColorTransformHelper::DegammaColor(expected);
2710 }
2711
2712 ColorTransformHelper::applyMatrix(expected, matrix);
2713
2714 if (mColorManagementUsed) {
2715 ColorTransformHelper::GammaColor(expected);
2716 }
2717
2718 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2719 uint8_t(expected.b * 255), 255};
2720
2721 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2722 // channel) should be less than one
2723 const uint8_t tolerance = 1;
2724
2725 Transaction()
2726 .setColor(colorLayer, color)
2727 .setColorTransform(parentLayer, matrix, vec3())
2728 .apply();
2729 {
2730 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002731 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08002732 }
2733}
2734
Alec Mouri80863a62019-01-17 15:19:35 -08002735TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnChildAndParent) {
chaviwf66724d2018-11-28 16:35:21 -08002736 sp<SurfaceControl> parentLayer;
2737 sp<SurfaceControl> colorLayer;
2738 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2739 0 /* buffer height */,
2740 ISurfaceComposerClient::eFXSurfaceContainer));
2741 ASSERT_NO_FATAL_FAILURE(
2742 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2743 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2744
2745 Transaction()
2746 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2747 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2748 .setLayer(parentLayer, mLayerZBase + 1)
2749 .apply();
2750 {
2751 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002752 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08002753 }
2754
2755 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2756 half3 expected = color;
2757 mat3 matrixChild;
2758 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
2759 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
2760 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
2761 mat3 matrixParent;
2762 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
2763 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
2764 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
2765
2766 // degamma before applying the matrix
2767 if (mColorManagementUsed) {
2768 ColorTransformHelper::DegammaColor(expected);
2769 }
2770
2771 ColorTransformHelper::applyMatrix(expected, matrixChild);
2772 ColorTransformHelper::applyMatrix(expected, matrixParent);
2773
2774 if (mColorManagementUsed) {
2775 ColorTransformHelper::GammaColor(expected);
2776 }
2777
2778 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2779 uint8_t(expected.b * 255), 255};
2780
2781 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2782 // channel) should be less than one
2783 const uint8_t tolerance = 1;
2784
2785 Transaction()
2786 .setColor(colorLayer, color)
2787 .setColorTransform(parentLayer, matrixParent, vec3())
2788 .setColorTransform(colorLayer, matrixChild, vec3())
2789 .apply();
2790 {
2791 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002792 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08002793 }
2794}
2795
Marissa Wallfda30bb2018-10-12 11:34:28 -07002796class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07002797public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07002798 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07002799 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002800 }
2801
Marissa Wall713b63f2018-10-17 15:42:43 -07002802 static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
Valerie Hauaa194562019-02-05 16:21:38 -08002803 const sp<SurfaceControl>& layer = nullptr, bool setBuffer = true,
2804 bool setBackgroundColor = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002805 if (layer) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002806 sp<GraphicBuffer> buffer;
2807 sp<Fence> fence;
Valerie Hauaa194562019-02-05 16:21:38 -08002808 if (setBuffer) {
2809 int err = getBuffer(&buffer, &fence);
2810 if (err != NO_ERROR) {
2811 return err;
2812 }
2813
2814 transaction.setBuffer(layer, buffer);
2815 transaction.setAcquireFence(layer, fence);
Marissa Wall713b63f2018-10-17 15:42:43 -07002816 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002817
Valerie Hauaa194562019-02-05 16:21:38 -08002818 if (setBackgroundColor) {
2819 transaction.setBackgroundColor(layer, /*color*/ half3(1.0f, 0, 0), /*alpha*/ 1.0f,
2820 ui::Dataspace::UNKNOWN);
2821 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002822 }
2823
2824 transaction.addTransactionCompletedCallback(callbackHelper->function,
2825 callbackHelper->getContext());
Marissa Wall713b63f2018-10-17 15:42:43 -07002826 return NO_ERROR;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002827 }
2828
Marissa Wall861616d2018-10-22 12:52:23 -07002829 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2830 bool finalState = false) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08002831 CallbackData callbackData;
2832 ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
2833 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
Marissa Wallfda30bb2018-10-12 11:34:28 -07002834
2835 if (finalState) {
2836 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2837 }
2838 }
2839
Marissa Wall861616d2018-10-22 12:52:23 -07002840 static void waitForCallbacks(CallbackHelper& helper,
2841 const std::vector<ExpectedResult>& expectedResults,
2842 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002843 for (const auto& expectedResult : expectedResults) {
2844 waitForCallback(helper, expectedResult);
2845 }
2846 if (finalState) {
2847 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2848 }
2849 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002850};
2851
Valerie Hauaa194562019-02-05 16:21:38 -08002852TEST_F(LayerCallbackTest, BufferColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002853 sp<SurfaceControl> layer;
2854 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2855
2856 Transaction transaction;
2857 CallbackHelper callback;
Valerie Hauaa194562019-02-05 16:21:38 -08002858 int err = fillTransaction(transaction, &callback, layer, true, true);
Marissa Wall713b63f2018-10-17 15:42:43 -07002859 if (err) {
2860 GTEST_SUCCEED() << "test not supported";
2861 return;
2862 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002863
2864 transaction.apply();
2865
2866 ExpectedResult expected;
2867 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2868 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2869}
2870
Valerie Hauaa194562019-02-05 16:21:38 -08002871TEST_F(LayerCallbackTest, NoBufferNoColor) {
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, false, false);
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
Marissa Wall861616d2018-10-22 12:52:23 -07002883 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002884
2885 ExpectedResult expected;
Marissa Wall713b63f2018-10-17 15:42:43 -07002886 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
2887 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002888 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2889}
2890
Valerie Hauaa194562019-02-05 16:21:38 -08002891TEST_F(LayerCallbackTest, BufferNoColor) {
2892 sp<SurfaceControl> layer;
2893 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2894
2895 Transaction transaction;
2896 CallbackHelper callback;
2897 int err = fillTransaction(transaction, &callback, layer, true, false);
2898 if (err) {
2899 GTEST_SUCCEED() << "test not supported";
2900 return;
2901 }
2902
2903 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
2904
2905 ExpectedResult expected;
2906 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2907 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2908}
2909
2910TEST_F(LayerCallbackTest, NoBufferColor) {
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, false, true);
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 ExpectedResult::Buffer::NOT_ACQUIRED);
2927 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2928}
2929
Marissa Wallfda30bb2018-10-12 11:34:28 -07002930TEST_F(LayerCallbackTest, NoStateChange) {
2931 Transaction transaction;
2932 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002933 int err = fillTransaction(transaction, &callback);
2934 if (err) {
2935 GTEST_SUCCEED() << "test not supported";
2936 return;
2937 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002938
2939 transaction.apply();
2940
2941 ExpectedResult expected;
2942 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2943}
2944
2945TEST_F(LayerCallbackTest, OffScreen) {
2946 sp<SurfaceControl> layer;
2947 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2948
2949 Transaction transaction;
2950 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002951 int err = fillTransaction(transaction, &callback, layer);
2952 if (err) {
2953 GTEST_SUCCEED() << "test not supported";
2954 return;
2955 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002956
Marissa Wall861616d2018-10-22 12:52:23 -07002957 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002958
2959 ExpectedResult expected;
2960 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2961 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2962}
2963
Valerie Hauaa194562019-02-05 16:21:38 -08002964TEST_F(LayerCallbackTest, MergeBufferNoColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002965 sp<SurfaceControl> layer1, layer2;
2966 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2967 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2968
2969 Transaction transaction1, transaction2;
2970 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07002971 int err = fillTransaction(transaction1, &callback1, layer1);
2972 if (err) {
2973 GTEST_SUCCEED() << "test not supported";
2974 return;
2975 }
2976 err = fillTransaction(transaction2, &callback2, layer2);
2977 if (err) {
2978 GTEST_SUCCEED() << "test not supported";
2979 return;
2980 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002981
Marissa Wall861616d2018-10-22 12:52:23 -07002982 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2983 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002984
2985 ExpectedResult expected;
2986 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2987 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2988 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2989}
2990
Valerie Hauaa194562019-02-05 16:21:38 -08002991TEST_F(LayerCallbackTest, MergeNoBufferColor) {
2992 sp<SurfaceControl> layer1, layer2;
2993 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2994 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2995
2996 Transaction transaction1, transaction2;
2997 CallbackHelper callback1, callback2;
2998 int err = fillTransaction(transaction1, &callback1, layer1, false, true);
2999 if (err) {
3000 GTEST_SUCCEED() << "test not supported";
3001 return;
3002 }
3003 err = fillTransaction(transaction2, &callback2, layer2, false, true);
3004 if (err) {
3005 GTEST_SUCCEED() << "test not supported";
3006 return;
3007 }
3008
3009 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3010 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3011
3012 ExpectedResult expected;
3013 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
3014 ExpectedResult::Buffer::NOT_ACQUIRED);
3015 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3016 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3017}
3018
3019TEST_F(LayerCallbackTest, MergeOneBufferOneColor) {
3020 sp<SurfaceControl> layer1, layer2;
3021 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3022 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3023
3024 Transaction transaction1, transaction2;
3025 CallbackHelper callback1, callback2;
3026 int err = fillTransaction(transaction1, &callback1, layer1);
3027 if (err) {
3028 GTEST_SUCCEED() << "test not supported";
3029 return;
3030 }
3031 err = fillTransaction(transaction2, &callback2, layer2, false, true);
3032 if (err) {
3033 GTEST_SUCCEED() << "test not supported";
3034 return;
3035 }
3036
3037 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3038 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3039
3040 ExpectedResult expected;
3041 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer1);
3042 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer2,
3043 ExpectedResult::Buffer::NOT_ACQUIRED);
3044 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3045 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3046}
Marissa Wallfda30bb2018-10-12 11:34:28 -07003047TEST_F(LayerCallbackTest, Merge_SameCallback) {
3048 sp<SurfaceControl> layer1, layer2;
3049 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3050 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3051
3052 Transaction transaction1, transaction2;
3053 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003054 int err = fillTransaction(transaction1, &callback, layer1);
3055 if (err) {
3056 GTEST_SUCCEED() << "test not supported";
3057 return;
3058 }
3059 err = fillTransaction(transaction2, &callback, layer2);
3060 if (err) {
3061 GTEST_SUCCEED() << "test not supported";
3062 return;
3063 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003064
3065 transaction2.merge(std::move(transaction1)).apply();
3066
3067 ExpectedResult expected;
3068 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3069 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3070 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3071}
3072
3073TEST_F(LayerCallbackTest, Merge_SameLayer) {
3074 sp<SurfaceControl> layer;
3075 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3076
3077 Transaction transaction1, transaction2;
3078 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003079 int err = fillTransaction(transaction1, &callback1, layer);
3080 if (err) {
3081 GTEST_SUCCEED() << "test not supported";
3082 return;
3083 }
3084 err = fillTransaction(transaction2, &callback2, layer);
3085 if (err) {
3086 GTEST_SUCCEED() << "test not supported";
3087 return;
3088 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003089
3090 transaction2.merge(std::move(transaction1)).apply();
3091
3092 ExpectedResult expected;
3093 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3094 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3095 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3096}
3097
Marissa Wallfda30bb2018-10-12 11:34:28 -07003098TEST_F(LayerCallbackTest, Merge_DifferentClients) {
3099 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3100 client2(new SurfaceComposerClient);
3101
3102 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3103 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3104
3105 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003106 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003107 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003108 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003109 ISurfaceComposerClient::eFXSurfaceBufferState));
3110
3111 Transaction transaction1, transaction2;
3112 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003113 int err = fillTransaction(transaction1, &callback1, layer1);
3114 if (err) {
3115 GTEST_SUCCEED() << "test not supported";
3116 return;
3117 }
3118 err = fillTransaction(transaction2, &callback2, layer2);
3119 if (err) {
3120 GTEST_SUCCEED() << "test not supported";
3121 return;
3122 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003123
Marissa Wall861616d2018-10-22 12:52:23 -07003124 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3125 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003126
3127 ExpectedResult expected;
3128 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3129 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3130 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3131}
3132
3133TEST_F(LayerCallbackTest, MultipleTransactions) {
3134 sp<SurfaceControl> layer;
3135 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3136
3137 Transaction transaction;
3138 CallbackHelper callback;
3139 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003140 int err = fillTransaction(transaction, &callback, layer);
3141 if (err) {
3142 GTEST_SUCCEED() << "test not supported";
3143 return;
3144 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003145
3146 transaction.apply();
3147
3148 ExpectedResult expected;
3149 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07003150 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003151 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3152 : ExpectedResult::PreviousBuffer::RELEASED);
3153 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3154 }
3155 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3156}
3157
3158TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
3159 sp<SurfaceControl> layer;
3160 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3161
3162 Transaction transaction;
3163 CallbackHelper callback;
3164 for (size_t i = 0; i < 10; i++) {
3165 ExpectedResult expected;
3166
3167 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003168 int err = fillTransaction(transaction, &callback, layer);
3169 if (err) {
3170 GTEST_SUCCEED() << "test not supported";
3171 return;
3172 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003173 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3174 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003175 int err = fillTransaction(transaction, &callback);
3176 if (err) {
3177 GTEST_SUCCEED() << "test not supported";
3178 return;
3179 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003180 }
3181
3182 transaction.apply();
3183
3184 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3185 }
3186 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3187}
3188
3189TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
3190 sp<SurfaceControl> layer;
3191 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3192
3193 Transaction transaction;
3194 CallbackHelper callback;
3195 for (size_t i = 0; i < 10; i++) {
3196 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003197 int err = fillTransaction(transaction, &callback, layer);
3198 if (err) {
3199 GTEST_SUCCEED() << "test not supported";
3200 return;
3201 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003202 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003203 int err = fillTransaction(transaction, &callback);
3204 if (err) {
3205 GTEST_SUCCEED() << "test not supported";
3206 return;
3207 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003208 }
3209
Marissa Wall861616d2018-10-22 12:52:23 -07003210 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003211
3212 ExpectedResult expected;
3213 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
3214 : ExpectedResult::Transaction::NOT_PRESENTED,
Marissa Wall713b63f2018-10-17 15:42:43 -07003215 layer,
3216 (i == 0) ? ExpectedResult::Buffer::ACQUIRED
3217 : ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003218 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
3219 }
3220 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3221}
3222
3223TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
3224 sp<SurfaceControl> layer1, layer2;
3225 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3226 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3227
3228 Transaction transaction1, transaction2;
3229 CallbackHelper callback1, callback2;
3230 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003231 int err = fillTransaction(transaction1, &callback1, layer1);
3232 if (err) {
3233 GTEST_SUCCEED() << "test not supported";
3234 return;
3235 }
3236 err = fillTransaction(transaction2, &callback2, layer2);
3237 if (err) {
3238 GTEST_SUCCEED() << "test not supported";
3239 return;
3240 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003241
Marissa Wall861616d2018-10-22 12:52:23 -07003242 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3243 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003244
3245 ExpectedResult expected;
3246 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003247 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003248 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3249 : ExpectedResult::PreviousBuffer::RELEASED);
3250 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3251 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3252 }
3253 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3254 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3255}
3256
3257TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
3258 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3259 client2(new SurfaceComposerClient);
3260 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3261 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3262
3263 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003264 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003265 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003266 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003267 ISurfaceComposerClient::eFXSurfaceBufferState));
3268
3269 Transaction transaction1, transaction2;
3270 CallbackHelper callback1, callback2;
3271 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003272 int err = fillTransaction(transaction1, &callback1, layer1);
3273 if (err) {
3274 GTEST_SUCCEED() << "test not supported";
3275 return;
3276 }
3277 err = fillTransaction(transaction2, &callback2, layer2);
3278 if (err) {
3279 GTEST_SUCCEED() << "test not supported";
3280 return;
3281 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003282
Marissa Wall861616d2018-10-22 12:52:23 -07003283 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3284 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003285
3286 ExpectedResult expected;
3287 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003288 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003289 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3290 : ExpectedResult::PreviousBuffer::RELEASED);
3291 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3292 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3293 }
3294 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3295 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3296}
3297
3298TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
3299 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3300 client2(new SurfaceComposerClient);
3301 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3302 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3303
3304 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003305 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003306 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003307 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003308 ISurfaceComposerClient::eFXSurfaceBufferState));
3309
3310 Transaction transaction1, transaction2;
3311 CallbackHelper callback1, callback2;
3312
3313 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003314 int err = fillTransaction(transaction1, &callback1, layer1);
3315 if (err) {
3316 GTEST_SUCCEED() << "test not supported";
3317 return;
3318 }
3319 err = fillTransaction(transaction2, &callback2, layer2);
3320 if (err) {
3321 GTEST_SUCCEED() << "test not supported";
3322 return;
3323 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003324
Marissa Wall861616d2018-10-22 12:52:23 -07003325 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3326 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003327
3328 ExpectedResult expected;
3329 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3330 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3331 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3332 expected.reset();
3333
3334 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003335 err = fillTransaction(transaction1, &callback1);
3336 if (err) {
3337 GTEST_SUCCEED() << "test not supported";
3338 return;
3339 }
3340 err = fillTransaction(transaction2, &callback2);
3341 if (err) {
3342 GTEST_SUCCEED() << "test not supported";
3343 return;
3344 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003345
3346 transaction2.merge(std::move(transaction1)).apply();
3347
3348 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3349 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3350}
3351
3352TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
3353 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3354 client2(new SurfaceComposerClient);
3355
3356 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3357 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3358
3359 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003360 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003361 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003362 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003363 ISurfaceComposerClient::eFXSurfaceBufferState));
3364
3365 Transaction transaction1, transaction2;
3366 CallbackHelper callback1, callback2;
3367
3368 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003369 int err = fillTransaction(transaction1, &callback1, layer1);
3370 if (err) {
3371 GTEST_SUCCEED() << "test not supported";
3372 return;
3373 }
3374 err = fillTransaction(transaction2, &callback2, layer2);
3375 if (err) {
3376 GTEST_SUCCEED() << "test not supported";
3377 return;
3378 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003379
Marissa Wall861616d2018-10-22 12:52:23 -07003380 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3381 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003382
3383 ExpectedResult expected;
3384 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3385 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3386 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3387 expected.reset();
3388
3389 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003390 err = fillTransaction(transaction1, &callback1);
3391 if (err) {
3392 GTEST_SUCCEED() << "test not supported";
3393 return;
3394 }
3395 err = fillTransaction(transaction2, &callback2);
3396 if (err) {
3397 GTEST_SUCCEED() << "test not supported";
3398 return;
3399 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003400
Marissa Wall861616d2018-10-22 12:52:23 -07003401 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003402
Marissa Wall713b63f2018-10-17 15:42:43 -07003403 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
3404 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003405 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3406 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3407}
3408
3409TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3410 sp<SurfaceControl> layer;
3411 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3412
3413 Transaction transaction;
3414 CallbackHelper callback;
3415 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003416 for (auto& expected : expectedResults) {
3417 expected.reset();
3418 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall5a68a772018-12-22 17:43:42 -08003419 ExpectedResult::Buffer::ACQUIRED,
3420 ExpectedResult::PreviousBuffer::UNKNOWN);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003421
Marissa Wall713b63f2018-10-17 15:42:43 -07003422 int err = fillTransaction(transaction, &callback, layer);
3423 if (err) {
3424 GTEST_SUCCEED() << "test not supported";
3425 return;
3426 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003427
3428 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003429 }
3430 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3431}
3432
3433TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3434 sp<SurfaceControl> layer;
3435 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3436
Marissa Wall713b63f2018-10-17 15:42:43 -07003437 // Normal call to set up test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003438 Transaction transaction;
3439 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003440 int err = fillTransaction(transaction, &callback, layer);
3441 if (err) {
3442 GTEST_SUCCEED() << "test not supported";
3443 return;
3444 }
3445
3446 transaction.apply();
3447
3448 ExpectedResult expected;
3449 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3450 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3451
3452 // Test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003453 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003454 for (auto& expected : expectedResults) {
3455 expected.reset();
3456
Marissa Wall713b63f2018-10-17 15:42:43 -07003457 err = fillTransaction(transaction, &callback);
3458 if (err) {
3459 GTEST_SUCCEED() << "test not supported";
3460 return;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003461 }
3462
3463 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003464 }
3465 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3466}
3467
3468TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3469 sp<SurfaceControl> layer;
3470 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3471
3472 // Normal call to set up test
3473 Transaction transaction;
3474 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003475 int err = fillTransaction(transaction, &callback, layer);
3476 if (err) {
3477 GTEST_SUCCEED() << "test not supported";
3478 return;
3479 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003480
Marissa Wall861616d2018-10-22 12:52:23 -07003481 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003482
3483 ExpectedResult expectedResult;
3484 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3485 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3486
3487 // Test
3488 std::vector<ExpectedResult> expectedResults(50);
3489 for (auto& expected : expectedResults) {
3490 expected.reset();
Marissa Wall713b63f2018-10-17 15:42:43 -07003491 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
3492 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003493
Marissa Wall713b63f2018-10-17 15:42:43 -07003494 err = fillTransaction(transaction, &callback);
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 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3503}
3504
Marissa Wall17b4e452018-12-26 16:32:34 -08003505TEST_F(LayerCallbackTest, DesiredPresentTime) {
3506 sp<SurfaceControl> layer;
3507 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3508
3509 Transaction transaction;
3510 CallbackHelper callback;
3511 int err = fillTransaction(transaction, &callback, layer);
3512 if (err) {
3513 GTEST_SUCCEED() << "test not supported";
3514 return;
3515 }
3516
3517 // Try to present 100ms in the future
3518 nsecs_t time = systemTime() + (100 * 1e6);
3519
3520 transaction.setDesiredPresentTime(time);
3521 transaction.apply();
3522
3523 ExpectedResult expected;
3524 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3525 expected.addExpectedPresentTime(time);
3526 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3527}
3528
3529TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
3530 sp<SurfaceControl> layer;
3531 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3532
3533 Transaction transaction;
3534 CallbackHelper callback1;
3535 int err = fillTransaction(transaction, &callback1, layer);
3536 if (err) {
3537 GTEST_SUCCEED() << "test not supported";
3538 return;
3539 }
3540
3541 // Try to present 100ms in the future
3542 nsecs_t time = systemTime() + (100 * 1e6);
3543
3544 transaction.setDesiredPresentTime(time);
3545 transaction.apply();
3546
3547 ExpectedResult expected1;
3548 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3549 expected1.addExpectedPresentTime(time);
3550
3551 CallbackHelper callback2;
3552 err = fillTransaction(transaction, &callback2, layer);
3553 if (err) {
3554 GTEST_SUCCEED() << "test not supported";
3555 return;
3556 }
3557
3558 // Try to present 33ms after the first frame
3559 time += (33.3 * 1e6);
3560
3561 transaction.setDesiredPresentTime(time);
3562 transaction.apply();
3563
3564 ExpectedResult expected2;
3565 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3566 ExpectedResult::Buffer::ACQUIRED,
3567 ExpectedResult::PreviousBuffer::RELEASED);
3568 expected2.addExpectedPresentTime(time);
3569
3570 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3571 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3572}
3573
3574TEST_F(LayerCallbackTest, DesiredPresentTime_OutOfOrder) {
3575 sp<SurfaceControl> layer;
3576 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3577
3578 Transaction transaction;
3579 CallbackHelper callback1;
3580 int err = fillTransaction(transaction, &callback1, layer);
3581 if (err) {
3582 GTEST_SUCCEED() << "test not supported";
3583 return;
3584 }
3585
3586 // Try to present 100ms in the future
3587 nsecs_t time = systemTime() + (100 * 1e6);
3588
3589 transaction.setDesiredPresentTime(time);
3590 transaction.apply();
3591
3592 ExpectedResult expected1;
3593 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3594 expected1.addExpectedPresentTime(time);
3595
3596 CallbackHelper callback2;
3597 err = fillTransaction(transaction, &callback2, layer);
3598 if (err) {
3599 GTEST_SUCCEED() << "test not supported";
3600 return;
3601 }
3602
3603 // Try to present 33ms before the previous frame
3604 time -= (33.3 * 1e6);
3605
3606 transaction.setDesiredPresentTime(time);
3607 transaction.apply();
3608
3609 ExpectedResult expected2;
3610 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3611 ExpectedResult::Buffer::ACQUIRED,
3612 ExpectedResult::PreviousBuffer::RELEASED);
3613
3614 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3615 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3616}
3617
3618TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
3619 sp<SurfaceControl> layer;
3620 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3621
3622 Transaction transaction;
3623 CallbackHelper callback;
3624 int err = fillTransaction(transaction, &callback, layer);
3625 if (err) {
3626 GTEST_SUCCEED() << "test not supported";
3627 return;
3628 }
3629
3630 // Try to present 100ms in the past
3631 nsecs_t time = systemTime() - (100 * 1e6);
3632
3633 transaction.setDesiredPresentTime(time);
3634 transaction.apply();
3635
3636 ExpectedResult expected;
3637 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3638 expected.addExpectedPresentTime(systemTime());
3639 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3640}
3641
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003642class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003643protected:
3644 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07003645 LayerTransactionTest::SetUp();
3646 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003647
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08003648 const auto display = SurfaceComposerClient::getInternalDisplayToken();
3649 ASSERT_FALSE(display == nullptr);
3650
Mathias Agopianc666cae2012-07-25 18:56:13 -07003651 DisplayInfo info;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08003652 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
Mathias Agopianc666cae2012-07-25 18:56:13 -07003653
3654 ssize_t displayWidth = info.w;
3655 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003656
3657 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07003658 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
3659 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003660 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003661 ASSERT_TRUE(mBGSurfaceControl->isValid());
3662 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
3663
3664 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07003665 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
3666
Peiyong Lin566a3b42018-01-09 18:22:43 -08003667 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003668 ASSERT_TRUE(mFGSurfaceControl->isValid());
3669
3670 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3671
3672 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07003673 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003674 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003675 ASSERT_TRUE(mSyncSurfaceControl->isValid());
3676
3677 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3678
Robert Carr4cdc58f2017-08-23 14:22:20 -07003679 asTransaction([&](Transaction& t) {
3680 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003681
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003682 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07003683
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003684 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
3685 .setPosition(mFGSurfaceControl, 64, 64)
3686 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003687
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003688 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
3689 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
3690 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003691 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003692 }
3693
3694 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07003695 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003696 mBGSurfaceControl = 0;
3697 mFGSurfaceControl = 0;
3698 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003699 }
3700
3701 void waitForPostedBuffers() {
3702 // Since the sync surface is in synchronous mode (i.e. double buffered)
3703 // posting three buffers to it should ensure that at least two
3704 // SurfaceFlinger::handlePageFlip calls have been made, which should
3705 // guaranteed that a buffer posted to another Surface has been retired.
3706 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3707 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3708 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3709 }
3710
Robert Carr4cdc58f2017-08-23 14:22:20 -07003711
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003712 sp<SurfaceControl> mBGSurfaceControl;
3713 sp<SurfaceControl> mFGSurfaceControl;
3714
3715 // This surface is used to ensure that the buffers posted to
3716 // mFGSurfaceControl have been picked up by SurfaceFlinger.
3717 sp<SurfaceControl> mSyncSurfaceControl;
3718};
3719
Robert Carr7f619b22017-11-06 12:56:35 -08003720TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08003721
chaviw0e3479f2018-09-10 16:49:30 -07003722 std::unique_ptr<ScreenCapture> sc;
3723
3724 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08003725 fillSurfaceRGBA8(relative, 10, 10, 10);
3726 waitForPostedBuffers();
3727
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003728 Transaction{}
3729 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08003730 .setPosition(relative, 64, 64)
3731 .apply();
3732
3733 {
3734 // The relative should be on top of the FG control.
3735 ScreenCapture::captureScreen(&sc);
3736 sc->checkPixel(64, 64, 10, 10, 10);
3737 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003738 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003739
3740 {
3741 // Nothing should change at this point.
3742 ScreenCapture::captureScreen(&sc);
3743 sc->checkPixel(64, 64, 10, 10, 10);
3744 }
3745
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003746 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003747
3748 {
3749 // Ensure that the relative was actually hidden, rather than
3750 // being left in the detached but visible state.
3751 ScreenCapture::captureScreen(&sc);
3752 sc->expectFGColor(64, 64);
3753 }
3754}
3755
Robert Carr8d5227b2017-03-16 15:41:03 -07003756class GeometryLatchingTest : public LayerUpdateTest {
3757protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003758 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07003759 SCOPED_TRACE(trace);
3760 ScreenCapture::captureScreen(&sc);
3761 // We find the leading edge of the FG surface.
3762 sc->expectFGColor(127, 127);
3763 sc->expectBGColor(128, 128);
3764 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003765
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003766 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07003767
3768 void unlockFGBuffer() {
3769 sp<Surface> s = mFGSurfaceControl->getSurface();
3770 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
3771 waitForPostedBuffers();
3772 }
3773
Robert Carr8d5227b2017-03-16 15:41:03 -07003774 void completeFGResize() {
3775 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3776 waitForPostedBuffers();
3777 }
3778 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003779 asTransaction([&](Transaction& t) {
3780 t.setSize(mFGSurfaceControl, 64, 64);
3781 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003782 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003783 });
Robert Carr8d5227b2017-03-16 15:41:03 -07003784
3785 EXPECT_INITIAL_STATE("After restoring initial state");
3786 }
chaviw0e3479f2018-09-10 16:49:30 -07003787 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07003788};
3789
Robert Carr8d5227b2017-03-16 15:41:03 -07003790class CropLatchingTest : public GeometryLatchingTest {
3791protected:
3792 void EXPECT_CROPPED_STATE(const char* trace) {
3793 SCOPED_TRACE(trace);
3794 ScreenCapture::captureScreen(&sc);
3795 // The edge should be moved back one pixel by our crop.
3796 sc->expectFGColor(126, 126);
3797 sc->expectBGColor(127, 127);
3798 sc->expectBGColor(128, 128);
3799 }
chaviw59f5c562017-06-28 16:39:06 -07003800
3801 void EXPECT_RESIZE_STATE(const char* trace) {
3802 SCOPED_TRACE(trace);
3803 ScreenCapture::captureScreen(&sc);
3804 // The FG is now resized too 128,128 at 64,64
3805 sc->expectFGColor(64, 64);
3806 sc->expectFGColor(191, 191);
3807 sc->expectBGColor(192, 192);
3808 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003809};
3810
Pablo Ceballos05289c22016-04-14 15:49:55 -07003811TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003812 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003813 {
3814 SCOPED_TRACE("before anything");
3815 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003816 sc->expectBGColor(32, 32);
3817 sc->expectFGColor(96, 96);
3818 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003819 }
3820
3821 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003822 asTransaction([&](Transaction& t) {
3823 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003824 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3825 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003826 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003827
Robert Carr4cdc58f2017-08-23 14:22:20 -07003828 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003829 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003830 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3831 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003832 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003833
3834 {
3835 SCOPED_TRACE("before any trigger");
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 // should trigger the first deferred transaction, but not the second one
3843 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3844 {
3845 SCOPED_TRACE("after first trigger");
3846 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003847 sc->expectBGColor(32, 32);
3848 sc->checkPixel(96, 96, 162, 63, 96);
3849 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003850 }
3851
3852 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003853 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003854
3855 // trigger the second deferred transaction
3856 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3857 {
3858 SCOPED_TRACE("after second trigger");
3859 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003860 sc->expectBGColor(32, 32);
3861 sc->expectBGColor(96, 96);
3862 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003863 }
3864}
3865
Robert Carre392b552017-09-19 12:16:05 -07003866TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003867 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003868
3869 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003870 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3871 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3872 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3873 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003874 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003875 SurfaceComposerClient::Transaction{}
3876 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3877 .show(childNoBuffer)
3878 .show(childBuffer)
3879 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003880 {
3881 ScreenCapture::captureScreen(&sc);
3882 sc->expectChildColor(73, 73);
3883 sc->expectFGColor(74, 74);
3884 }
Vishnu Nair60356342018-11-13 13:00:45 -08003885 SurfaceComposerClient::Transaction{}
3886 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3887 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003888 {
3889 ScreenCapture::captureScreen(&sc);
3890 sc->expectChildColor(73, 73);
3891 sc->expectChildColor(74, 74);
3892 }
3893}
3894
Robert Carr2c5f6d22017-09-26 12:30:35 -07003895TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003896 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003897 {
3898 SCOPED_TRACE("before move");
3899 ScreenCapture::captureScreen(&sc);
3900 sc->expectBGColor(0, 12);
3901 sc->expectFGColor(75, 75);
3902 sc->expectBGColor(145, 145);
3903 }
3904
3905 Transaction t1, t2;
3906 t1.setPosition(mFGSurfaceControl, 128, 128);
3907 t2.setPosition(mFGSurfaceControl, 0, 0);
3908 // We expect that the position update from t2 now
3909 // overwrites the position update from t1.
3910 t1.merge(std::move(t2));
3911 t1.apply();
3912
3913 {
3914 ScreenCapture::captureScreen(&sc);
3915 sc->expectFGColor(1, 1);
3916 }
3917}
3918
Vishnu Nair996bc422019-07-16 14:15:33 -07003919TEST_F(LayerUpdateTest, MergingTransactionFlags) {
3920 Transaction().hide(mFGSurfaceControl).apply();
3921 std::unique_ptr<ScreenCapture> sc;
3922 {
3923 SCOPED_TRACE("before merge");
3924 ScreenCapture::captureScreen(&sc);
3925 sc->expectBGColor(0, 12);
3926 sc->expectBGColor(75, 75);
3927 sc->expectBGColor(145, 145);
3928 }
3929
3930 Transaction t1, t2;
3931 t1.show(mFGSurfaceControl);
3932 t2.setFlags(mFGSurfaceControl, 0 /* flags */, layer_state_t::eLayerSecure /* mask */);
3933 t1.merge(std::move(t2));
3934 t1.apply();
3935
3936 {
3937 SCOPED_TRACE("after merge");
3938 ScreenCapture::captureScreen(&sc);
3939 sc->expectFGColor(75, 75);
3940 }
3941}
3942
Robert Carr1f0a16a2016-10-24 16:27:39 -07003943class ChildLayerTest : public LayerUpdateTest {
3944protected:
3945 void SetUp() override {
3946 LayerUpdateTest::SetUp();
Vishnu Nairc652ff82019-03-15 12:48:54 -07003947 mChild = createSurface(mClient, "Child surface", 10, 15, PIXEL_FORMAT_RGBA_8888, 0,
Vishnu Nair88a11f22018-11-28 18:30:57 -08003948 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003949 fillSurfaceRGBA8(mChild, 200, 200, 200);
3950
3951 {
3952 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003953 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003954 mCapture->expectChildColor(64, 64);
3955 }
3956 }
3957 void TearDown() override {
3958 LayerUpdateTest::TearDown();
3959 mChild = 0;
3960 }
3961
3962 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003963 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003964};
3965
3966TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003967 asTransaction([&](Transaction& t) {
3968 t.show(mChild);
3969 t.setPosition(mChild, 10, 10);
3970 t.setPosition(mFGSurfaceControl, 64, 64);
3971 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003972
3973 {
chaviw0e3479f2018-09-10 16:49:30 -07003974 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003975 // Top left of foreground must now be visible
3976 mCapture->expectFGColor(64, 64);
3977 // But 10 pixels in we should see the child surface
3978 mCapture->expectChildColor(74, 74);
3979 // And 10 more pixels we should be back to the foreground surface
3980 mCapture->expectFGColor(84, 84);
3981 }
3982
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003983 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003984
3985 {
chaviw0e3479f2018-09-10 16:49:30 -07003986 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003987 // Top left of foreground should now be at 0, 0
3988 mCapture->expectFGColor(0, 0);
3989 // But 10 pixels in we should see the child surface
3990 mCapture->expectChildColor(10, 10);
3991 // And 10 more pixels we should be back to the foreground surface
3992 mCapture->expectFGColor(20, 20);
3993 }
3994}
3995
Robert Carr41b08b52017-06-01 16:11:34 -07003996TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003997 asTransaction([&](Transaction& t) {
3998 t.show(mChild);
3999 t.setPosition(mChild, 0, 0);
4000 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004001 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07004002 });
Robert Carr41b08b52017-06-01 16:11:34 -07004003
4004 {
chaviw0e3479f2018-09-10 16:49:30 -07004005 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07004006 mCapture->expectChildColor(0, 0);
4007 mCapture->expectChildColor(4, 4);
4008 mCapture->expectBGColor(5, 5);
4009 }
4010}
4011
Robert Carr1f0a16a2016-10-24 16:27:39 -07004012TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004013 asTransaction([&](Transaction& t) {
4014 t.show(mChild);
4015 t.setPosition(mFGSurfaceControl, 0, 0);
4016 t.setPosition(mChild, 63, 63);
4017 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004018
4019 {
chaviw0e3479f2018-09-10 16:49:30 -07004020 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004021 mCapture->expectFGColor(0, 0);
4022 // Last pixel in foreground should now be the child.
4023 mCapture->expectChildColor(63, 63);
4024 // But the child should be constrained and the next pixel
4025 // must be the background
4026 mCapture->expectBGColor(64, 64);
4027 }
4028}
4029
4030TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004031 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004032
4033 // Find the boundary between the parent and child
4034 {
chaviw0e3479f2018-09-10 16:49:30 -07004035 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004036 mCapture->expectChildColor(9, 9);
4037 mCapture->expectFGColor(10, 10);
4038 }
4039
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004040 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004041
4042 // The boundary should be twice as far from the origin now.
4043 // The pixels from the last test should all be child now
4044 {
chaviw0e3479f2018-09-10 16:49:30 -07004045 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004046 mCapture->expectChildColor(9, 9);
4047 mCapture->expectChildColor(10, 10);
4048 mCapture->expectChildColor(19, 19);
4049 mCapture->expectFGColor(20, 20);
4050 }
4051}
Robert Carr9524cb32017-02-13 11:32:32 -08004052
Vishnu Nairc652ff82019-03-15 12:48:54 -07004053// A child with a scale transform should be cropped by its parent bounds.
4054TEST_F(ChildLayerTest, ChildLayerScalingCroppedByParent) {
4055 asTransaction([&](Transaction& t) {
4056 t.setPosition(mFGSurfaceControl, 0, 0);
4057 t.setPosition(mChild, 0, 0);
4058 });
4059
4060 // Find the boundary between the parent and child.
4061 {
4062 mCapture = screenshot();
4063 mCapture->expectChildColor(0, 0);
4064 mCapture->expectChildColor(9, 9);
4065 mCapture->expectFGColor(10, 10);
4066 }
4067
4068 asTransaction([&](Transaction& t) { t.setMatrix(mChild, 10.0, 0, 0, 10.0); });
4069
4070 // The child should fill its parent bounds and be cropped by it.
4071 {
4072 mCapture = screenshot();
4073 mCapture->expectChildColor(0, 0);
4074 mCapture->expectChildColor(63, 63);
4075 mCapture->expectBGColor(64, 64);
4076 }
4077}
4078
Robert Carr6452f122017-03-21 10:41:29 -07004079TEST_F(ChildLayerTest, ChildLayerAlpha) {
4080 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
4081 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
4082 fillSurfaceRGBA8(mChild, 0, 254, 0);
4083 waitForPostedBuffers();
4084
Robert Carr4cdc58f2017-08-23 14:22:20 -07004085 asTransaction([&](Transaction& t) {
4086 t.show(mChild);
4087 t.setPosition(mChild, 0, 0);
4088 t.setPosition(mFGSurfaceControl, 0, 0);
4089 });
Robert Carr6452f122017-03-21 10:41:29 -07004090
4091 {
chaviw0e3479f2018-09-10 16:49:30 -07004092 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004093 // Unblended child color
4094 mCapture->checkPixel(0, 0, 0, 254, 0);
4095 }
4096
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004097 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004098
4099 {
chaviw0e3479f2018-09-10 16:49:30 -07004100 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004101 // Child and BG blended.
4102 mCapture->checkPixel(0, 0, 127, 127, 0);
4103 }
4104
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004105 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004106
4107 {
chaviw0e3479f2018-09-10 16:49:30 -07004108 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004109 // Child and BG blended.
4110 mCapture->checkPixel(0, 0, 95, 64, 95);
4111 }
4112}
4113
Robert Carr9524cb32017-02-13 11:32:32 -08004114TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004115 asTransaction([&](Transaction& t) {
4116 t.show(mChild);
4117 t.setPosition(mChild, 10, 10);
4118 t.setPosition(mFGSurfaceControl, 64, 64);
4119 });
Robert Carr9524cb32017-02-13 11:32:32 -08004120
4121 {
chaviw0e3479f2018-09-10 16:49:30 -07004122 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004123 // Top left of foreground must now be visible
4124 mCapture->expectFGColor(64, 64);
4125 // But 10 pixels in we should see the child surface
4126 mCapture->expectChildColor(74, 74);
4127 // And 10 more pixels we should be back to the foreground surface
4128 mCapture->expectFGColor(84, 84);
4129 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004130
4131 asTransaction([&](Transaction& t) {
4132 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
4133 });
4134
Robert Carr9524cb32017-02-13 11:32:32 -08004135 {
chaviw0e3479f2018-09-10 16:49:30 -07004136 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004137 mCapture->expectFGColor(64, 64);
4138 // In reparenting we should have exposed the entire foreground surface.
4139 mCapture->expectFGColor(74, 74);
4140 // And the child layer should now begin at 10, 10 (since the BG
4141 // layer is at (0, 0)).
4142 mCapture->expectBGColor(9, 9);
4143 mCapture->expectChildColor(10, 10);
4144 }
4145}
4146
Robert Carr2e102c92018-10-23 12:11:15 -07004147TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
4148 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004149 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07004150 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
4151
4152 {
4153 SCOPED_TRACE("Grandchild visible");
4154 ScreenCapture::captureScreen(&mCapture);
4155 mCapture->checkPixel(64, 64, 111, 111, 111);
4156 }
4157
Robert Carr87246532019-02-04 15:20:26 -08004158 mChild.clear();
Robert Carr2e102c92018-10-23 12:11:15 -07004159
4160 {
4161 SCOPED_TRACE("After destroying child");
4162 ScreenCapture::captureScreen(&mCapture);
4163 mCapture->expectFGColor(64, 64);
4164 }
4165
4166 asTransaction([&](Transaction& t) {
4167 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
4168 });
4169
4170 {
4171 SCOPED_TRACE("After reparenting grandchild");
4172 ScreenCapture::captureScreen(&mCapture);
4173 mCapture->checkPixel(64, 64, 111, 111, 111);
4174 }
4175}
4176
Vishnu Nair14d76922019-08-05 08:41:20 -07004177TEST_F(ChildLayerTest, ChildrenRelativeZSurvivesParentDestruction) {
4178 sp<SurfaceControl> mGrandChild =
4179 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
4180 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
4181
4182 // draw grand child behind the foreground surface
4183 asTransaction([&](Transaction& t) {
4184 t.setRelativeLayer(mGrandChild, mFGSurfaceControl->getHandle(), -1);
4185 });
4186
4187 {
4188 SCOPED_TRACE("Child visible");
4189 ScreenCapture::captureScreen(&mCapture);
4190 mCapture->checkPixel(64, 64, 200, 200, 200);
4191 }
4192
4193 asTransaction([&](Transaction& t) {
4194 t.reparent(mChild, nullptr);
4195 t.reparentChildren(mChild, mFGSurfaceControl->getHandle());
4196 });
4197
4198 {
4199 SCOPED_TRACE("foreground visible reparenting grandchild");
4200 ScreenCapture::captureScreen(&mCapture);
4201 mCapture->checkPixel(64, 64, 195, 63, 63);
4202 }
4203}
4204
chaviw161410b02017-07-27 10:46:08 -07004205TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004206 asTransaction([&](Transaction& t) {
4207 t.show(mChild);
4208 t.setPosition(mChild, 10, 10);
4209 t.setPosition(mFGSurfaceControl, 64, 64);
4210 });
Robert Carr9524cb32017-02-13 11:32:32 -08004211
4212 {
chaviw0e3479f2018-09-10 16:49:30 -07004213 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004214 // Top left of foreground must now be visible
4215 mCapture->expectFGColor(64, 64);
4216 // But 10 pixels in we should see the child surface
4217 mCapture->expectChildColor(74, 74);
4218 // And 10 more pixels we should be back to the foreground surface
4219 mCapture->expectFGColor(84, 84);
4220 }
4221
chaviw0e3479f2018-09-10 16:49:30 -07004222
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004223 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08004224
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004225 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08004226
chaviw161410b02017-07-27 10:46:08 -07004227 // Since the child has the same client as the parent, it will not get
4228 // detached and will be hidden.
4229 {
chaviw0e3479f2018-09-10 16:49:30 -07004230 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004231 mCapture->expectFGColor(64, 64);
4232 mCapture->expectFGColor(74, 74);
4233 mCapture->expectFGColor(84, 84);
4234 }
4235}
4236
4237TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
4238 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004239 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004240 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
4241 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07004242
chaviw161410b02017-07-27 10:46:08 -07004243 ASSERT_TRUE(mChildNewClient->isValid());
4244
4245 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
4246
Robert Carr4cdc58f2017-08-23 14:22:20 -07004247 asTransaction([&](Transaction& t) {
4248 t.hide(mChild);
4249 t.show(mChildNewClient);
4250 t.setPosition(mChildNewClient, 10, 10);
4251 t.setPosition(mFGSurfaceControl, 64, 64);
4252 });
chaviw161410b02017-07-27 10:46:08 -07004253
4254 {
chaviw0e3479f2018-09-10 16:49:30 -07004255 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004256 // Top left of foreground must now be visible
4257 mCapture->expectFGColor(64, 64);
4258 // But 10 pixels in we should see the child surface
4259 mCapture->expectChildColor(74, 74);
4260 // And 10 more pixels we should be back to the foreground surface
4261 mCapture->expectFGColor(84, 84);
4262 }
4263
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004264 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07004265
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004266 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07004267
Robert Carr9524cb32017-02-13 11:32:32 -08004268 // Nothing should have changed.
4269 {
chaviw0e3479f2018-09-10 16:49:30 -07004270 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004271 mCapture->expectFGColor(64, 64);
4272 mCapture->expectChildColor(74, 74);
4273 mCapture->expectFGColor(84, 84);
4274 }
4275}
4276
chaviw5aedec92018-10-22 10:40:38 -07004277TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
4278 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4279 sp<SurfaceControl> childNewClient =
4280 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4281 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4282
4283 ASSERT_TRUE(childNewClient != nullptr);
4284 ASSERT_TRUE(childNewClient->isValid());
4285
4286 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4287
4288 Transaction()
4289 .hide(mChild)
4290 .show(childNewClient)
4291 .setPosition(childNewClient, 10, 10)
4292 .setPosition(mFGSurfaceControl, 64, 64)
4293 .apply();
4294
4295 {
4296 mCapture = screenshot();
4297 // Top left of foreground must now be visible
4298 mCapture->expectFGColor(64, 64);
4299 // But 10 pixels in we should see the child surface
4300 mCapture->expectChildColor(74, 74);
4301 // And 10 more pixels we should be back to the foreground surface
4302 mCapture->expectFGColor(84, 84);
4303 }
4304
4305 Transaction().detachChildren(mFGSurfaceControl).apply();
4306 Transaction().hide(childNewClient).apply();
4307
4308 // Nothing should have changed.
4309 {
4310 mCapture = screenshot();
4311 mCapture->expectFGColor(64, 64);
4312 mCapture->expectChildColor(74, 74);
4313 mCapture->expectFGColor(84, 84);
4314 }
4315
4316 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
4317 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
4318 32);
4319 Transaction()
4320 .setLayer(newParentSurface, INT32_MAX - 1)
4321 .show(newParentSurface)
4322 .setPosition(newParentSurface, 20, 20)
4323 .reparent(childNewClient, newParentSurface->getHandle())
4324 .apply();
4325 {
4326 mCapture = screenshot();
4327 // Child is now hidden.
4328 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
4329 }
4330}
chaviw43cb3cb2019-05-31 15:23:41 -07004331TEST_F(ChildLayerTest, DetachChildrenWithDeferredTransaction) {
4332 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4333 sp<SurfaceControl> childNewClient =
4334 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4335 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4336
4337 ASSERT_TRUE(childNewClient != nullptr);
4338 ASSERT_TRUE(childNewClient->isValid());
4339
4340 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4341
4342 Transaction()
4343 .hide(mChild)
4344 .show(childNewClient)
4345 .setPosition(childNewClient, 10, 10)
4346 .setPosition(mFGSurfaceControl, 64, 64)
4347 .apply();
4348
4349 {
4350 mCapture = screenshot();
4351 Rect rect = Rect(74, 74, 84, 84);
4352 mCapture->expectBorder(rect, Color{195, 63, 63, 255});
4353 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4354 }
4355
4356 Transaction()
4357 .deferTransactionUntil_legacy(childNewClient, mFGSurfaceControl->getHandle(),
4358 mFGSurfaceControl->getSurface()->getNextFrameNumber())
4359 .apply();
4360 Transaction().detachChildren(mFGSurfaceControl).apply();
4361 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(mFGSurfaceControl, Color::RED, 32, 32));
4362
4363 // BufferLayer can still dequeue buffers even though there's a detached layer with a
4364 // deferred transaction.
4365 {
4366 SCOPED_TRACE("new buffer");
4367 mCapture = screenshot();
4368 Rect rect = Rect(74, 74, 84, 84);
4369 mCapture->expectBorder(rect, Color::RED);
4370 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4371 }
4372}
chaviw5aedec92018-10-22 10:40:38 -07004373
Robert Carr9b429f42017-04-17 14:56:57 -07004374TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004375 asTransaction([&](Transaction& t) {
4376 t.show(mChild);
4377 t.setPosition(mChild, 0, 0);
4378 t.setPosition(mFGSurfaceControl, 0, 0);
4379 });
Robert Carr9b429f42017-04-17 14:56:57 -07004380
4381 {
chaviw0e3479f2018-09-10 16:49:30 -07004382 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004383 // We've positioned the child in the top left.
4384 mCapture->expectChildColor(0, 0);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004385 // But it's only 10x15.
4386 mCapture->expectFGColor(10, 15);
Robert Carr9b429f42017-04-17 14:56:57 -07004387 }
4388
Robert Carr4cdc58f2017-08-23 14:22:20 -07004389 asTransaction([&](Transaction& t) {
4390 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4391 // We cause scaling by 2.
4392 t.setSize(mFGSurfaceControl, 128, 128);
4393 });
Robert Carr9b429f42017-04-17 14:56:57 -07004394
4395 {
chaviw0e3479f2018-09-10 16:49:30 -07004396 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004397 // We've positioned the child in the top left.
4398 mCapture->expectChildColor(0, 0);
4399 mCapture->expectChildColor(10, 10);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004400 mCapture->expectChildColor(19, 29);
4401 // And now it should be scaled all the way to 20x30
4402 mCapture->expectFGColor(20, 30);
Robert Carr9b429f42017-04-17 14:56:57 -07004403 }
4404}
4405
Robert Carr1725eee2017-04-26 18:32:15 -07004406// Regression test for b/37673612
4407TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004408 asTransaction([&](Transaction& t) {
4409 t.show(mChild);
4410 t.setPosition(mChild, 0, 0);
4411 t.setPosition(mFGSurfaceControl, 0, 0);
4412 });
Robert Carr1725eee2017-04-26 18:32:15 -07004413
4414 {
chaviw0e3479f2018-09-10 16:49:30 -07004415 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004416 // We've positioned the child in the top left.
4417 mCapture->expectChildColor(0, 0);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004418 mCapture->expectChildColor(9, 14);
4419 // But it's only 10x15.
4420 mCapture->expectFGColor(10, 15);
Robert Carr1725eee2017-04-26 18:32:15 -07004421 }
Robert Carr1725eee2017-04-26 18:32:15 -07004422 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
4423 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004424 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07004425 sp<Surface> s = mFGSurfaceControl->getSurface();
4426 auto anw = static_cast<ANativeWindow*>(s.get());
4427 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4428 native_window_set_buffers_dimensions(anw, 64, 128);
4429 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4430 waitForPostedBuffers();
4431
4432 {
4433 // The child should still be in the same place and not have any strange scaling as in
4434 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07004435 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004436 mCapture->expectChildColor(0, 0);
4437 mCapture->expectFGColor(10, 10);
4438 }
4439}
4440
Vishnu Nairc652ff82019-03-15 12:48:54 -07004441// A child with a buffer transform from its parents should be cropped by its parent bounds.
4442TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferTransform) {
4443 asTransaction([&](Transaction& t) {
4444 t.show(mChild);
4445 t.setPosition(mChild, 0, 0);
4446 t.setPosition(mFGSurfaceControl, 0, 0);
4447 t.setSize(mChild, 100, 100);
4448 });
4449 fillSurfaceRGBA8(mChild, 200, 200, 200);
4450
4451 {
4452 mCapture = screenshot();
4453
4454 mCapture->expectChildColor(0, 0);
4455 mCapture->expectChildColor(63, 63);
4456 mCapture->expectBGColor(64, 64);
4457 }
4458
4459 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
4460 sp<Surface> s = mFGSurfaceControl->getSurface();
4461 auto anw = static_cast<ANativeWindow*>(s.get());
4462 // Apply a 90 transform on the buffer.
4463 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4464 native_window_set_buffers_dimensions(anw, 64, 128);
4465 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4466 waitForPostedBuffers();
4467
4468 // The child should be cropped by the new parent bounds.
4469 {
4470 mCapture = screenshot();
4471 mCapture->expectChildColor(0, 0);
4472 mCapture->expectChildColor(99, 63);
4473 mCapture->expectFGColor(100, 63);
4474 mCapture->expectBGColor(128, 64);
4475 }
4476}
4477
4478// A child with a scale transform from its parents should be cropped by its parent bounds.
4479TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferScale) {
4480 asTransaction([&](Transaction& t) {
4481 t.show(mChild);
4482 t.setPosition(mChild, 0, 0);
4483 t.setPosition(mFGSurfaceControl, 0, 0);
4484 t.setSize(mChild, 200, 200);
4485 });
4486 fillSurfaceRGBA8(mChild, 200, 200, 200);
4487
4488 {
4489 mCapture = screenshot();
4490
4491 mCapture->expectChildColor(0, 0);
4492 mCapture->expectChildColor(63, 63);
4493 mCapture->expectBGColor(64, 64);
4494 }
4495
4496 asTransaction([&](Transaction& t) {
4497 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4498 // Set a scaling by 2.
4499 t.setSize(mFGSurfaceControl, 128, 128);
4500 });
4501
4502 // Child should inherit its parents scale but should be cropped by its parent bounds.
4503 {
4504 mCapture = screenshot();
4505 mCapture->expectChildColor(0, 0);
4506 mCapture->expectChildColor(127, 127);
4507 mCapture->expectBGColor(128, 128);
4508 }
4509}
4510
4511// Regression test for b/127368943
4512// Child should ignore the buffer transform but apply parent scale transform.
4513TEST_F(ChildLayerTest, ChildrenWithParentBufferTransformAndScale) {
4514 asTransaction([&](Transaction& t) {
4515 t.show(mChild);
4516 t.setPosition(mChild, 0, 0);
4517 t.setPosition(mFGSurfaceControl, 0, 0);
4518 });
4519
4520 {
4521 mCapture = screenshot();
4522 mCapture->expectChildColor(0, 0);
4523 mCapture->expectChildColor(9, 14);
4524 mCapture->expectFGColor(10, 15);
4525 }
4526
4527 // Change the size of the foreground to 128 * 64 so we can test rotation as well.
4528 asTransaction([&](Transaction& t) {
4529 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4530 t.setSize(mFGSurfaceControl, 128, 64);
4531 });
4532 sp<Surface> s = mFGSurfaceControl->getSurface();
4533 auto anw = static_cast<ANativeWindow*>(s.get());
4534 // Apply a 90 transform on the buffer and submit a buffer half the expected size so that we
4535 // have an effective scale of 2.0 applied to the buffer along with a rotation transform.
4536 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4537 native_window_set_buffers_dimensions(anw, 32, 64);
4538 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4539 waitForPostedBuffers();
4540
4541 // The child should ignore the buffer transform but apply the 2.0 scale from parent.
4542 {
4543 mCapture = screenshot();
4544 mCapture->expectChildColor(0, 0);
4545 mCapture->expectChildColor(19, 29);
4546 mCapture->expectFGColor(20, 30);
4547 }
4548}
4549
Dan Stoza412903f2017-04-27 13:42:17 -07004550TEST_F(ChildLayerTest, Bug36858924) {
4551 // Destroy the child layer
4552 mChild.clear();
4553
4554 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08004555 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
4556 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07004557
4558 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07004559 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004560 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
4561 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07004562 t.show(mChild);
4563 });
Dan Stoza412903f2017-04-27 13:42:17 -07004564
4565 // Render the foreground surface a few times
4566 //
4567 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
4568 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
4569 // never acquire/release the first buffer
4570 ALOGI("Filling 1");
4571 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
4572 ALOGI("Filling 2");
4573 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
4574 ALOGI("Filling 3");
4575 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
4576 ALOGI("Filling 4");
4577 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
4578}
4579
chaviwf1961f72017-09-18 16:41:07 -07004580TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004581 asTransaction([&](Transaction& t) {
4582 t.show(mChild);
4583 t.setPosition(mChild, 10, 10);
4584 t.setPosition(mFGSurfaceControl, 64, 64);
4585 });
chaviw06178942017-07-27 10:25:59 -07004586
4587 {
chaviw0e3479f2018-09-10 16:49:30 -07004588 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004589 // Top left of foreground must now be visible
4590 mCapture->expectFGColor(64, 64);
4591 // But 10 pixels in we should see the child surface
4592 mCapture->expectChildColor(74, 74);
4593 // And 10 more pixels we should be back to the foreground surface
4594 mCapture->expectFGColor(84, 84);
4595 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004596
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004597 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07004598
chaviw06178942017-07-27 10:25:59 -07004599 {
chaviw0e3479f2018-09-10 16:49:30 -07004600 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004601 mCapture->expectFGColor(64, 64);
4602 // In reparenting we should have exposed the entire foreground surface.
4603 mCapture->expectFGColor(74, 74);
4604 // And the child layer should now begin at 10, 10 (since the BG
4605 // layer is at (0, 0)).
4606 mCapture->expectBGColor(9, 9);
4607 mCapture->expectChildColor(10, 10);
4608 }
4609}
4610
chaviwf1961f72017-09-18 16:41:07 -07004611TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004612 asTransaction([&](Transaction& t) {
4613 t.show(mChild);
4614 t.setPosition(mChild, 10, 10);
4615 t.setPosition(mFGSurfaceControl, 64, 64);
4616 });
chaviwf1961f72017-09-18 16:41:07 -07004617
4618 {
chaviw0e3479f2018-09-10 16:49:30 -07004619 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004620 // Top left of foreground must now be visible
4621 mCapture->expectFGColor(64, 64);
4622 // But 10 pixels in we should see the child surface
4623 mCapture->expectChildColor(74, 74);
4624 // And 10 more pixels we should be back to the foreground surface
4625 mCapture->expectFGColor(84, 84);
4626 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004627 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07004628 {
chaviw0e3479f2018-09-10 16:49:30 -07004629 mCapture = screenshot();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004630 // The surface should now be offscreen.
chaviwf1961f72017-09-18 16:41:07 -07004631 mCapture->expectFGColor(64, 64);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004632 mCapture->expectFGColor(74, 74);
chaviwf1961f72017-09-18 16:41:07 -07004633 mCapture->expectFGColor(84, 84);
4634 }
4635}
4636
4637TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07004638 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08004639 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07004640 ASSERT_TRUE(newSurface->isValid());
4641
4642 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004643 asTransaction([&](Transaction& t) {
4644 t.hide(mChild);
4645 t.show(newSurface);
4646 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004647 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004648 t.setPosition(mFGSurfaceControl, 64, 64);
4649 });
chaviwf1961f72017-09-18 16:41:07 -07004650
4651 {
chaviw0e3479f2018-09-10 16:49:30 -07004652 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004653 // Top left of foreground must now be visible
4654 mCapture->expectFGColor(64, 64);
4655 // At 10, 10 we should see the new surface
4656 mCapture->checkPixel(10, 10, 63, 195, 63);
4657 }
4658
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004659 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07004660
4661 {
chaviw0e3479f2018-09-10 16:49:30 -07004662 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004663 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
4664 // mFGSurface, putting it at 74, 74.
4665 mCapture->expectFGColor(64, 64);
4666 mCapture->checkPixel(74, 74, 63, 195, 63);
4667 mCapture->expectFGColor(84, 84);
4668 }
4669}
4670
chaviwc9674332017-08-28 12:32:18 -07004671TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004672 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
4673 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07004674 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4675
4676 {
chaviw0e3479f2018-09-10 16:49:30 -07004677 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07004678 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
4679 // which begins at 64, 64
4680 mCapture->checkPixel(64, 64, 50, 50, 50);
4681 }
4682}
4683
Robert Carr503c7042017-09-27 15:06:08 -07004684TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004685 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07004686 fillSurfaceRGBA8(relative, 255, 255, 255);
4687
4688 Transaction t;
4689 t.setLayer(relative, INT32_MAX)
4690 .setRelativeLayer(mChild, relative->getHandle(), 1)
4691 .setPosition(mFGSurfaceControl, 0, 0)
4692 .apply(true);
4693
4694 // We expect that the child should have been elevated above our
4695 // INT_MAX layer even though it's not a child of it.
4696 {
chaviw0e3479f2018-09-10 16:49:30 -07004697 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07004698 mCapture->expectChildColor(0, 0);
4699 mCapture->expectChildColor(9, 9);
4700 mCapture->checkPixel(10, 10, 255, 255, 255);
4701 }
4702}
Vishnu Nairc652ff82019-03-15 12:48:54 -07004703
Vishnu Nair60356342018-11-13 13:00:45 -08004704class BoundlessLayerTest : public LayerUpdateTest {
4705protected:
4706 std::unique_ptr<ScreenCapture> mCapture;
4707};
4708
4709// Verify setting a size on a buffer layer has no effect.
4710TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
4711 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004712 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
4713 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004714 ASSERT_TRUE(bufferLayer->isValid());
4715 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
4716 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
4717 {
4718 mCapture = screenshot();
4719 // Top left of background must now be visible
4720 mCapture->expectBGColor(0, 0);
4721 // Foreground Surface bounds must be color layer
4722 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
4723 // Buffer layer should not extend past buffer bounds
4724 mCapture->expectFGColor(95, 95);
4725 }
4726}
4727
4728// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
4729// which will crop the color layer.
4730TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
4731 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004732 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4733 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004734 ASSERT_TRUE(colorLayer->isValid());
4735 asTransaction([&](Transaction& t) {
4736 t.setColor(colorLayer, half3{0, 0, 0});
4737 t.show(colorLayer);
4738 });
4739 {
4740 mCapture = screenshot();
4741 // Top left of background must now be visible
4742 mCapture->expectBGColor(0, 0);
4743 // Foreground Surface bounds must be color layer
4744 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4745 // Color layer should not extend past foreground bounds
4746 mCapture->expectBGColor(129, 129);
4747 }
4748}
4749
4750// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
4751// a crop which will be used to crop the color layer.
4752TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004753 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4754 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004755 ASSERT_TRUE(cropLayer->isValid());
4756 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004757 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4758 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004759 ASSERT_TRUE(colorLayer->isValid());
4760 asTransaction([&](Transaction& t) {
4761 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
4762 t.setColor(colorLayer, half3{0, 0, 0});
4763 t.show(cropLayer);
4764 t.show(colorLayer);
4765 });
4766 {
4767 mCapture = screenshot();
4768 // Top left of background must now be visible
4769 mCapture->expectBGColor(0, 0);
4770 // Top left of foreground must now be visible
4771 mCapture->expectFGColor(64, 64);
4772 // 5 pixels from the foreground we should see the child surface
4773 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
4774 // 10 pixels from the foreground we should be back to the foreground surface
4775 mCapture->expectFGColor(74, 74);
4776 }
4777}
4778
4779// Verify for boundless layer with no children, their transforms have no effect.
4780TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
4781 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004782 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4783 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004784 ASSERT_TRUE(colorLayer->isValid());
4785 asTransaction([&](Transaction& t) {
4786 t.setPosition(colorLayer, 320, 320);
4787 t.setMatrix(colorLayer, 2, 0, 0, 2);
4788 t.setColor(colorLayer, half3{0, 0, 0});
4789 t.show(colorLayer);
4790 });
4791 {
4792 mCapture = screenshot();
4793 // Top left of background must now be visible
4794 mCapture->expectBGColor(0, 0);
4795 // Foreground Surface bounds must be color layer
4796 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4797 // Color layer should not extend past foreground bounds
4798 mCapture->expectBGColor(129, 129);
4799 }
4800}
4801
4802// Verify for boundless layer with children, their transforms have an effect.
4803TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
4804 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004805 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4806 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004807 ASSERT_TRUE(boundlessLayerRightShift->isValid());
4808 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004809 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4810 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004811 ASSERT_TRUE(boundlessLayerDownShift->isValid());
4812 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004813 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4814 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004815 ASSERT_TRUE(colorLayer->isValid());
4816 asTransaction([&](Transaction& t) {
4817 t.setPosition(boundlessLayerRightShift, 32, 0);
4818 t.show(boundlessLayerRightShift);
4819 t.setPosition(boundlessLayerDownShift, 0, 32);
4820 t.show(boundlessLayerDownShift);
4821 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4822 t.setColor(colorLayer, half3{0, 0, 0});
4823 t.show(colorLayer);
4824 });
4825 {
4826 mCapture = screenshot();
4827 // Top left of background must now be visible
4828 mCapture->expectBGColor(0, 0);
4829 // Top left of foreground must now be visible
4830 mCapture->expectFGColor(64, 64);
4831 // Foreground Surface bounds must be color layer
4832 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
4833 // Color layer should not extend past foreground bounds
4834 mCapture->expectBGColor(129, 129);
4835 }
4836}
4837
4838// Verify child layers do not get clipped if they temporarily move into the negative
4839// coordinate space as the result of an intermediate transformation.
4840TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
4841 sp<SurfaceControl> boundlessLayer =
4842 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4843 0 /* flags */, mFGSurfaceControl.get());
4844 ASSERT_TRUE(boundlessLayer != nullptr);
4845 ASSERT_TRUE(boundlessLayer->isValid());
4846 sp<SurfaceControl> colorLayer =
4847 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4848 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
4849 ASSERT_TRUE(colorLayer != nullptr);
4850 ASSERT_TRUE(colorLayer->isValid());
4851 asTransaction([&](Transaction& t) {
4852 // shift child layer off bounds. If this layer was not boundless, we will
4853 // expect the child layer to be cropped.
4854 t.setPosition(boundlessLayer, 32, 32);
4855 t.show(boundlessLayer);
4856 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4857 // undo shift by parent
4858 t.setPosition(colorLayer, -32, -32);
4859 t.setColor(colorLayer, half3{0, 0, 0});
4860 t.show(colorLayer);
4861 });
4862 {
4863 mCapture = screenshot();
4864 // Top left of background must now be visible
4865 mCapture->expectBGColor(0, 0);
4866 // Foreground Surface bounds must be color layer
4867 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4868 // Color layer should not extend past foreground bounds
4869 mCapture->expectBGColor(129, 129);
4870 }
4871}
4872
4873// Verify for boundless root layers with children, their transforms have an effect.
4874TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004875 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
4876 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08004877 ASSERT_TRUE(rootBoundlessLayer->isValid());
4878 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004879 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4880 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
4881
Vishnu Nair60356342018-11-13 13:00:45 -08004882 ASSERT_TRUE(colorLayer->isValid());
4883 asTransaction([&](Transaction& t) {
4884 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
4885 t.setPosition(rootBoundlessLayer, 32, 32);
4886 t.show(rootBoundlessLayer);
4887 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4888 t.setColor(colorLayer, half3{0, 0, 0});
4889 t.show(colorLayer);
4890 t.hide(mFGSurfaceControl);
4891 });
4892 {
4893 mCapture = screenshot();
4894 // Top left of background must now be visible
4895 mCapture->expectBGColor(0, 0);
4896 // Top left of foreground must now be visible
4897 mCapture->expectBGColor(31, 31);
4898 // Foreground Surface bounds must be color layer
4899 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
4900 // Color layer should not extend past foreground bounds
4901 mCapture->expectBGColor(97, 97);
4902 }
4903}
Robert Carr503c7042017-09-27 15:06:08 -07004904
chaviwa76b2712017-09-20 12:02:26 -07004905class ScreenCaptureTest : public LayerUpdateTest {
4906protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004907 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07004908};
4909
4910TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
4911 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004912 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004913 mCapture->expectBGColor(0, 0);
4914 // Doesn't capture FG layer which is at 64, 64
4915 mCapture->expectBGColor(64, 64);
4916}
4917
4918TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
4919 auto fgHandle = mFGSurfaceControl->getHandle();
4920
Vishnu Nair88a11f22018-11-28 18:30:57 -08004921 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4922 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004923 fillSurfaceRGBA8(child, 200, 200, 200);
4924
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004925 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004926
4927 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004928 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004929 mCapture->expectFGColor(10, 10);
4930 mCapture->expectChildColor(0, 0);
4931}
4932
Robert Carr578038f2018-03-09 12:25:24 -08004933TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
4934 auto fgHandle = mFGSurfaceControl->getHandle();
4935
Vishnu Nair88a11f22018-11-28 18:30:57 -08004936 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4937 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004938 fillSurfaceRGBA8(child, 200, 200, 200);
4939
4940 SurfaceComposerClient::Transaction().show(child).apply(true);
4941
4942 // Captures mFGSurfaceControl's child
4943 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4944 mCapture->checkPixel(10, 10, 0, 0, 0);
4945 mCapture->expectChildColor(0, 0);
4946}
4947
Robert Carr866455f2019-04-02 16:28:26 -07004948TEST_F(ScreenCaptureTest, CaptureLayerExclude) {
4949 auto fgHandle = mFGSurfaceControl->getHandle();
4950
4951 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4952 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4953 fillSurfaceRGBA8(child, 200, 200, 200);
4954 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
4955 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4956 fillSurfaceRGBA8(child2, 200, 0, 200);
4957
4958 SurfaceComposerClient::Transaction()
4959 .show(child)
4960 .show(child2)
4961 .setLayer(child, 1)
4962 .setLayer(child2, 2)
4963 .apply(true);
4964
4965 // Child2 would be visible but its excluded, so we should see child1 color instead.
4966 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
4967 mCapture->checkPixel(10, 10, 0, 0, 0);
4968 mCapture->checkPixel(0, 0, 200, 200, 200);
4969}
4970
4971// Like the last test but verifies that children are also exclude.
4972TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) {
4973 auto fgHandle = mFGSurfaceControl->getHandle();
4974
4975 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4976 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4977 fillSurfaceRGBA8(child, 200, 200, 200);
4978 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
4979 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4980 fillSurfaceRGBA8(child2, 200, 0, 200);
4981 sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10,
4982 PIXEL_FORMAT_RGBA_8888, 0, child2.get());
4983 fillSurfaceRGBA8(child2, 200, 0, 200);
4984
4985 SurfaceComposerClient::Transaction()
4986 .show(child)
4987 .show(child2)
4988 .show(child3)
4989 .setLayer(child, 1)
4990 .setLayer(child2, 2)
4991 .apply(true);
4992
4993 // Child2 would be visible but its excluded, so we should see child1 color instead.
4994 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
4995 mCapture->checkPixel(10, 10, 0, 0, 0);
4996 mCapture->checkPixel(0, 0, 200, 200, 200);
4997}
4998
chaviw50da5042018-04-09 13:49:37 -07004999TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005000 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5001 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07005002
5003 fillSurfaceRGBA8(child, 200, 200, 200);
5004
5005 SurfaceComposerClient::Transaction().show(child).apply(true);
5006
5007 auto childHandle = child->getHandle();
5008
5009 // Captures child
5010 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
5011 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
5012 // Area outside of child's bounds is transparent.
5013 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
5014}
5015
chaviw4b129c22018-04-09 16:19:43 -07005016TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
5017 auto fgHandle = mFGSurfaceControl->getHandle();
5018
Vishnu Nair88a11f22018-11-28 18:30:57 -08005019 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5020 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5021 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07005022 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07005023 fillSurfaceRGBA8(child, 200, 200, 200);
5024 fillSurfaceRGBA8(relative, 100, 100, 100);
5025
5026 SurfaceComposerClient::Transaction()
5027 .show(child)
5028 // Set relative layer above fg layer so should be shown above when computing all layers.
5029 .setRelativeLayer(relative, fgHandle, 1)
5030 .show(relative)
5031 .apply(true);
5032
5033 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
5034 ScreenCapture::captureLayers(&mCapture, fgHandle);
5035 mCapture->expectFGColor(10, 10);
5036 mCapture->expectChildColor(0, 0);
5037}
5038
5039TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
5040 auto fgHandle = mFGSurfaceControl->getHandle();
5041
Vishnu Nair88a11f22018-11-28 18:30:57 -08005042 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5043 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5044 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
5045 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07005046 fillSurfaceRGBA8(child, 200, 200, 200);
5047 fillSurfaceRGBA8(relative, 100, 100, 100);
5048
5049 SurfaceComposerClient::Transaction()
5050 .show(child)
5051 // Set relative layer below fg layer but relative to child layer so it should be shown
5052 // above child layer.
5053 .setLayer(relative, -1)
5054 .setRelativeLayer(relative, child->getHandle(), 1)
5055 .show(relative)
5056 .apply(true);
5057
5058 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
5059 // relative value should be taken into account, placing it above child layer.
5060 ScreenCapture::captureLayers(&mCapture, fgHandle);
5061 mCapture->expectFGColor(10, 10);
5062 // Relative layer is showing on top of child layer
5063 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
5064}
Robert Carr578038f2018-03-09 12:25:24 -08005065
5066// In the following tests we verify successful skipping of a parent layer,
5067// so we use the same verification logic and only change how we mutate
5068// the parent layer to verify that various properties are ignored.
5069class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
5070public:
5071 void SetUp() override {
5072 LayerUpdateTest::SetUp();
5073
Vishnu Nair88a11f22018-11-28 18:30:57 -08005074 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
5075 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08005076 fillSurfaceRGBA8(mChild, 200, 200, 200);
5077
5078 SurfaceComposerClient::Transaction().show(mChild).apply(true);
5079 }
5080
Vishnu Nair333a9572019-02-15 16:05:56 -08005081 void verify(std::function<void()> verifyStartingState) {
5082 // Verify starting state before a screenshot is taken.
5083 verifyStartingState();
5084
5085 // Verify child layer does not inherit any of the properties of its
5086 // parent when its screenshot is captured.
Robert Carr578038f2018-03-09 12:25:24 -08005087 auto fgHandle = mFGSurfaceControl->getHandle();
5088 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
5089 mCapture->checkPixel(10, 10, 0, 0, 0);
5090 mCapture->expectChildColor(0, 0);
Vishnu Nair333a9572019-02-15 16:05:56 -08005091
5092 // Verify all assumptions are still true after the screenshot is taken.
5093 verifyStartingState();
Robert Carr578038f2018-03-09 12:25:24 -08005094 }
5095
5096 std::unique_ptr<ScreenCapture> mCapture;
5097 sp<SurfaceControl> mChild;
5098};
5099
Vishnu Nair333a9572019-02-15 16:05:56 -08005100// Regression test b/76099859
Robert Carr578038f2018-03-09 12:25:24 -08005101TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
5102
5103 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
5104
5105 // Even though the parent is hidden we should still capture the child.
Vishnu Nair333a9572019-02-15 16:05:56 -08005106
5107 // Before and after reparenting, verify child is properly hidden
5108 // when rendering full-screen.
5109 verify([&] { screenshot()->expectBGColor(64, 64); });
Robert Carr578038f2018-03-09 12:25:24 -08005110}
5111
5112TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07005113 SurfaceComposerClient::Transaction()
5114 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
5115 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005116
5117 // Even though the parent is cropped out we should still capture the child.
Vishnu Nair333a9572019-02-15 16:05:56 -08005118
5119 // Before and after reparenting, verify child is cropped by parent.
5120 verify([&] { screenshot()->expectBGColor(65, 65); });
Robert Carr578038f2018-03-09 12:25:24 -08005121}
5122
Vishnu Nair333a9572019-02-15 16:05:56 -08005123// Regression test b/124372894
Robert Carr578038f2018-03-09 12:25:24 -08005124TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
Vishnu Nair333a9572019-02-15 16:05:56 -08005125 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005126
5127 // We should not inherit the parent scaling.
Robert Carr578038f2018-03-09 12:25:24 -08005128
Vishnu Nair333a9572019-02-15 16:05:56 -08005129 // Before and after reparenting, verify child is properly scaled.
5130 verify([&] { screenshot()->expectChildColor(80, 80); });
Robert Carr15eae092018-03-23 13:43:53 -07005131}
5132
5133
chaviwa76b2712017-09-20 12:02:26 -07005134TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
5135 auto fgHandle = mFGSurfaceControl->getHandle();
5136
Vishnu Nair88a11f22018-11-28 18:30:57 -08005137 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5138 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005139 fillSurfaceRGBA8(child, 200, 200, 200);
5140
Vishnu Nair88a11f22018-11-28 18:30:57 -08005141 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5142 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07005143
5144 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5145 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005146 .show(child)
5147 .setPosition(grandchild, 5, 5)
5148 .show(grandchild)
5149 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005150
5151 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005152 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07005153 mCapture->expectFGColor(10, 10);
5154 mCapture->expectChildColor(0, 0);
5155 mCapture->checkPixel(5, 5, 50, 50, 50);
5156}
5157
5158TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005159 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5160 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005161 fillSurfaceRGBA8(child, 200, 200, 200);
5162 auto childHandle = child->getHandle();
5163
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005164 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005165
5166 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005167 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07005168 mCapture->expectChildColor(0, 0);
5169 mCapture->expectChildColor(9, 9);
5170}
5171
5172TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005173 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5174 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005175 fillSurfaceRGBA8(child, 200, 200, 200);
5176 auto childHandle = child->getHandle();
5177
Vishnu Nair88a11f22018-11-28 18:30:57 -08005178 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5179 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07005180 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5181
5182 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005183 .show(child)
5184 .setPosition(grandchild, 5, 5)
5185 .show(grandchild)
5186 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005187
5188 auto grandchildHandle = grandchild->getHandle();
5189
5190 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005191 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07005192 mCapture->checkPixel(0, 0, 50, 50, 50);
5193 mCapture->checkPixel(4, 4, 50, 50, 50);
5194}
5195
chaviw7206d492017-11-10 16:16:12 -08005196TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07005197 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005198 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5199 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005200
Marissa Wall61c58622018-07-18 10:12:20 -07005201 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5202 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005203
5204 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005205 .setLayer(redLayer, INT32_MAX - 1)
5206 .show(redLayer)
5207 .show(blueLayer)
5208 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005209
5210 auto redLayerHandle = redLayer->getHandle();
5211
5212 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005213 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5214 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5215 // red area below the blue area
5216 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5217 // red area to the right of the blue area
5218 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005219
Marissa Wall861616d2018-10-22 12:52:23 -07005220 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005221 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08005222 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
5223 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005224 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08005225 mCapture->checkPixel(30, 30, 0, 0, 0);
5226}
5227
5228TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07005229 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005230 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5231 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005232
Marissa Wall61c58622018-07-18 10:12:20 -07005233 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5234 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005235
5236 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005237 .setLayer(redLayer, INT32_MAX - 1)
5238 .show(redLayer)
5239 .show(blueLayer)
5240 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005241
5242 auto redLayerHandle = redLayer->getHandle();
5243
5244 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005245 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5246 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5247 // red area below the blue area
5248 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5249 // red area to the right of the blue area
5250 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005251
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005252 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08005253 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005254 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
5255 // red area below the blue area
5256 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
5257 // red area to the right of the blue area
5258 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005259 mCapture->checkPixel(30, 30, 0, 0, 0);
5260}
5261
5262TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07005263 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08005264
Marissa Wall61c58622018-07-18 10:12:20 -07005265 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08005266
5267 auto redLayerHandle = redLayer->getHandle();
Robert Carr87246532019-02-04 15:20:26 -08005268 redLayer.clear();
chaviw7206d492017-11-10 16:16:12 -08005269 SurfaceComposerClient::Transaction().apply(true);
5270
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005271 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08005272
5273 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005274 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
5275 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08005276}
5277
chaviw8e3fe5d2018-02-22 10:55:42 -08005278
5279class DereferenceSurfaceControlTest : public LayerTransactionTest {
5280protected:
5281 void SetUp() override {
5282 LayerTransactionTest::SetUp();
5283 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005284 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005285 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005286 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005287 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
5288 {
5289 SCOPED_TRACE("before anything");
5290 auto shot = screenshot();
5291 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5292 }
5293 }
5294 void TearDown() override {
5295 LayerTransactionTest::TearDown();
5296 bgLayer = 0;
5297 fgLayer = 0;
5298 }
5299
5300 sp<SurfaceControl> bgLayer;
5301 sp<SurfaceControl> fgLayer;
5302};
5303
5304TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
5305 fgLayer = nullptr;
5306 {
5307 SCOPED_TRACE("after setting null");
5308 auto shot = screenshot();
5309 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
5310 }
5311}
5312
5313TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
5314 auto transaction = Transaction().show(fgLayer);
5315 fgLayer = nullptr;
5316 {
5317 SCOPED_TRACE("after setting null");
5318 auto shot = screenshot();
5319 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5320 }
5321}
5322
Vishnu Nairb927e1f2019-02-19 13:36:15 -08005323class MultiDisplayLayerBoundsTest : public LayerTransactionTest {
5324protected:
5325 virtual void SetUp() {
5326 LayerTransactionTest::SetUp();
5327 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5328
5329 mMainDisplay = SurfaceComposerClient::getInternalDisplayToken();
5330 SurfaceComposerClient::getDisplayInfo(mMainDisplay, &mMainDisplayInfo);
5331
5332 sp<IGraphicBufferConsumer> consumer;
5333 BufferQueue::createBufferQueue(&mProducer, &consumer);
5334 consumer->setConsumerName(String8("Virtual disp consumer"));
5335 consumer->setDefaultBufferSize(mMainDisplayInfo.w, mMainDisplayInfo.h);
5336 }
5337
5338 virtual void TearDown() {
5339 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
5340 LayerTransactionTest::TearDown();
5341 mColorLayer = 0;
5342 }
5343
5344 void createDisplay(const Rect& layerStackRect, uint32_t layerStack) {
5345 mVirtualDisplay =
5346 SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), false /*secure*/);
5347 asTransaction([&](Transaction& t) {
5348 t.setDisplaySurface(mVirtualDisplay, mProducer);
5349 t.setDisplayLayerStack(mVirtualDisplay, layerStack);
5350 t.setDisplayProjection(mVirtualDisplay, mMainDisplayInfo.orientation, layerStackRect,
5351 Rect(mMainDisplayInfo.w, mMainDisplayInfo.h));
5352 });
5353 }
5354
5355 void createColorLayer(uint32_t layerStack) {
5356 mColorLayer =
5357 createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */,
5358 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
5359 ASSERT_TRUE(mColorLayer != nullptr);
5360 ASSERT_TRUE(mColorLayer->isValid());
5361 asTransaction([&](Transaction& t) {
5362 t.setLayerStack(mColorLayer, layerStack);
5363 t.setCrop_legacy(mColorLayer, Rect(0, 0, 30, 40));
5364 t.setLayer(mColorLayer, INT32_MAX - 2);
5365 t.setColor(mColorLayer,
5366 half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f,
5367 mExpectedColor.b / 255.0f});
5368 t.show(mColorLayer);
5369 });
5370 }
5371
5372 DisplayInfo mMainDisplayInfo;
5373 sp<IBinder> mMainDisplay;
5374 sp<IBinder> mVirtualDisplay;
5375 sp<IGraphicBufferProducer> mProducer;
5376 sp<SurfaceControl> mColorLayer;
5377 Color mExpectedColor = {63, 63, 195, 255};
5378};
5379
5380TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) {
5381 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 1 /* layerStack */);
5382 createColorLayer(1 /* layerStack */);
5383
5384 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5385
5386 // Verify color layer does not render on main display.
5387 std::unique_ptr<ScreenCapture> sc;
5388 ScreenCapture::captureScreen(&sc, mMainDisplay);
5389 sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255});
5390 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5391
5392 // Verify color layer renders correctly on virtual display.
5393 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5394 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5395 sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 0});
5396}
5397
5398TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) {
5399 // Create a display and set its layer stack to the main display's layer stack so
5400 // the contents of the main display are mirrored on to the virtual display.
5401
5402 // Assumption here is that the new mirrored display has the same viewport as the
5403 // primary display that it is mirroring.
5404 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 0 /* layerStack */);
5405 createColorLayer(0 /* layerStack */);
5406
5407 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5408
5409 // Verify color layer renders correctly on main display and it is mirrored on the
5410 // virtual display.
5411 std::unique_ptr<ScreenCapture> sc;
5412 ScreenCapture::captureScreen(&sc, mMainDisplay);
5413 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5414 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5415
5416 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5417 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5418 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5419}
5420
Ady Abrahamdf9df4a2019-03-12 17:32:05 -07005421class DisplayActiveConfigTest : public ::testing::Test {
5422protected:
5423 void SetUp() override {
5424 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
5425 SurfaceComposerClient::getDisplayConfigs(mDisplayToken, &mDisplayconfigs);
5426 EXPECT_GT(mDisplayconfigs.size(), 0);
5427
5428 // set display power to on to make sure config can be changed
5429 SurfaceComposerClient::setDisplayPowerMode(mDisplayToken, HWC_POWER_MODE_NORMAL);
5430 }
5431
5432 sp<IBinder> mDisplayToken;
5433 Vector<DisplayInfo> mDisplayconfigs;
5434};
5435
5436TEST_F(DisplayActiveConfigTest, allConfigsAllowed) {
5437 std::vector<int32_t> allowedConfigs;
5438
5439 // Add all configs to the allowed configs
5440 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5441 allowedConfigs.push_back(i);
5442 }
5443
5444 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5445 EXPECT_EQ(res, NO_ERROR);
5446
5447 std::vector<int32_t> outConfigs;
5448 res = SurfaceComposerClient::getAllowedDisplayConfigs(mDisplayToken, &outConfigs);
5449 EXPECT_EQ(res, NO_ERROR);
5450 EXPECT_EQ(allowedConfigs, outConfigs);
5451}
5452
5453TEST_F(DisplayActiveConfigTest, changeAllowedConfig) {
5454 // we need at least 2 configs available for this test
5455 if (mDisplayconfigs.size() <= 1) return;
5456
5457 int activeConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5458
5459 // We want to set the allowed config to everything but the active config
5460 std::vector<int32_t> allowedConfigs;
5461 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5462 if (i != activeConfig) {
5463 allowedConfigs.push_back(i);
5464 }
5465 }
5466
5467 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5468 EXPECT_EQ(res, NO_ERROR);
5469
5470 // Allow some time for the config change
5471 std::this_thread::sleep_for(200ms);
5472
5473 int newActiveConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5474 EXPECT_NE(activeConfig, newActiveConfig);
5475
5476 // Make sure the new config is part of allowed config
5477 EXPECT_TRUE(std::find(allowedConfigs.begin(), allowedConfigs.end(), newActiveConfig) !=
5478 allowedConfigs.end());
5479}
5480
Vishnu Nairda9c85a2019-06-03 17:26:48 -07005481class RelativeZTest : public LayerTransactionTest {
5482protected:
5483 virtual void SetUp() {
5484 LayerTransactionTest::SetUp();
5485 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5486
5487 const auto display = SurfaceComposerClient::getInternalDisplayToken();
5488 ASSERT_FALSE(display == nullptr);
5489
5490 // Back layer
5491 mBackgroundLayer = createColorLayer("Background layer", Color::RED);
5492
5493 // Front layer
5494 mForegroundLayer = createColorLayer("Foreground layer", Color::GREEN);
5495
5496 asTransaction([&](Transaction& t) {
5497 t.setDisplayLayerStack(display, 0);
5498 t.setLayer(mBackgroundLayer, INT32_MAX - 2).show(mBackgroundLayer);
5499 t.setLayer(mForegroundLayer, INT32_MAX - 1).show(mForegroundLayer);
5500 });
5501 }
5502
5503 virtual void TearDown() {
5504 LayerTransactionTest::TearDown();
5505 mBackgroundLayer = 0;
5506 mForegroundLayer = 0;
5507 }
5508
5509 sp<SurfaceControl> mBackgroundLayer;
5510 sp<SurfaceControl> mForegroundLayer;
5511};
5512
5513// When a layer is reparented offscreen, remove relative z order if the relative parent
5514// is still onscreen so that the layer is not drawn.
5515TEST_F(RelativeZTest, LayerRemoved) {
5516 std::unique_ptr<ScreenCapture> sc;
5517
5518 // Background layer (RED)
5519 // Child layer (WHITE) (relative to foregroud layer)
5520 // Foregroud layer (GREEN)
5521 sp<SurfaceControl> childLayer =
5522 createColorLayer("Child layer", Color::BLUE, mBackgroundLayer.get());
5523
5524 Transaction{}
5525 .setRelativeLayer(childLayer, mForegroundLayer->getHandle(), 1)
5526 .show(childLayer)
5527 .apply();
5528
5529 {
5530 // The childLayer should be in front of the FG control.
5531 ScreenCapture::captureScreen(&sc);
5532 sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b);
5533 }
5534
5535 // Background layer (RED)
5536 // Foregroud layer (GREEN)
5537 Transaction{}.reparent(childLayer, nullptr).apply();
5538
5539 // Background layer (RED)
5540 // Child layer (WHITE)
5541 // Foregroud layer (GREEN)
5542 Transaction{}.reparent(childLayer, mBackgroundLayer->getHandle()).apply();
5543
5544 {
5545 // The relative z info for child layer should be reset, leaving FG control on top.
5546 ScreenCapture::captureScreen(&sc);
5547 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5548 }
5549}
5550
5551// When a layer is reparented offscreen, preseve relative z order if the relative parent
5552// is also offscreen. Regression test b/132613412
5553TEST_F(RelativeZTest, LayerRemovedOffscreenRelativeParent) {
5554 std::unique_ptr<ScreenCapture> sc;
5555
5556 // Background layer (RED)
5557 // Foregroud layer (GREEN)
5558 // child level 1 (WHITE)
5559 // child level 2a (BLUE)
5560 // child level 3 (GREEN) (relative to child level 2b)
5561 // child level 2b (BLACK)
5562 sp<SurfaceControl> childLevel1 =
5563 createColorLayer("child level 1", Color::WHITE, mForegroundLayer.get());
5564 sp<SurfaceControl> childLevel2a =
5565 createColorLayer("child level 2a", Color::BLUE, childLevel1.get());
5566 sp<SurfaceControl> childLevel2b =
5567 createColorLayer("child level 2b", Color::BLACK, childLevel1.get());
5568 sp<SurfaceControl> childLevel3 =
5569 createColorLayer("child level 3", Color::GREEN, childLevel2a.get());
5570
5571 Transaction{}
5572 .setRelativeLayer(childLevel3, childLevel2b->getHandle(), 1)
5573 .show(childLevel2a)
5574 .show(childLevel2b)
5575 .show(childLevel3)
5576 .apply();
5577
5578 {
5579 // The childLevel3 should be in front of childLevel2b.
5580 ScreenCapture::captureScreen(&sc);
5581 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5582 }
5583
5584 // Background layer (RED)
5585 // Foregroud layer (GREEN)
5586 Transaction{}.reparent(childLevel1, nullptr).apply();
5587
5588 // Background layer (RED)
5589 // Foregroud layer (GREEN)
5590 // child level 1 (WHITE)
5591 // child level 2 back (BLUE)
5592 // child level 3 (GREEN) (relative to child level 2b)
5593 // child level 2 front (BLACK)
5594 Transaction{}.reparent(childLevel1, mForegroundLayer->getHandle()).apply();
5595
5596 {
5597 // Nothing should change at this point since relative z info was preserved.
5598 ScreenCapture::captureScreen(&sc);
5599 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5600 }
5601}
5602
Steven Thomas44685cb2019-07-23 16:19:31 -07005603// This test ensures that when we drop an app buffer in SurfaceFlinger, we merge
5604// the dropped buffer's damage region into the next buffer's damage region. If
5605// we don't do this, we'll report an incorrect damage region to hardware
5606// composer, resulting in broken rendering. This test checks the BufferQueue
5607// case.
5608//
5609// Unfortunately, we don't currently have a way to inspect the damage region
5610// SurfaceFlinger sends to hardware composer from a test, so this test requires
5611// the dev to manually watch the device's screen during the test to spot broken
5612// rendering. Because the results can't be automatically verified, this test is
5613// marked disabled.
5614TEST_F(LayerTransactionTest, DISABLED_BufferQueueLayerMergeDamageRegionWhenDroppingBuffers) {
5615 const int width = mDisplayWidth;
5616 const int height = mDisplayHeight;
5617 sp<SurfaceControl> layer;
5618 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
5619 const auto producer = layer->getIGraphicBufferProducer();
5620 const sp<IProducerListener> dummyListener(new DummyProducerListener);
5621 IGraphicBufferProducer::QueueBufferOutput queueBufferOutput;
5622 ASSERT_EQ(OK,
5623 producer->connect(dummyListener, NATIVE_WINDOW_API_CPU, true, &queueBufferOutput));
5624
5625 std::map<int, sp<GraphicBuffer>> slotMap;
5626 auto slotToBuffer = [&](int slot, sp<GraphicBuffer>* buf) {
5627 ASSERT_NE(nullptr, buf);
5628 const auto iter = slotMap.find(slot);
5629 ASSERT_NE(slotMap.end(), iter);
5630 *buf = iter->second;
5631 };
5632
5633 auto dequeue = [&](int* outSlot) {
5634 ASSERT_NE(nullptr, outSlot);
5635 *outSlot = -1;
5636 int slot;
5637 sp<Fence> fence;
5638 uint64_t age;
5639 FrameEventHistoryDelta timestamps;
5640 const status_t dequeueResult =
5641 producer->dequeueBuffer(&slot, &fence, width, height, PIXEL_FORMAT_RGBA_8888,
5642 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
5643 &age, &timestamps);
5644 if (dequeueResult == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
5645 sp<GraphicBuffer> newBuf;
5646 ASSERT_EQ(OK, producer->requestBuffer(slot, &newBuf));
5647 ASSERT_NE(nullptr, newBuf.get());
5648 slotMap[slot] = newBuf;
5649 } else {
5650 ASSERT_EQ(OK, dequeueResult);
5651 }
5652 *outSlot = slot;
5653 };
5654
5655 auto queue = [&](int slot, const Region& damage, nsecs_t displayTime) {
5656 IGraphicBufferProducer::QueueBufferInput input(
5657 /*timestamp=*/displayTime, /*isAutoTimestamp=*/false, HAL_DATASPACE_UNKNOWN,
5658 /*crop=*/Rect::EMPTY_RECT, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
5659 /*transform=*/0, Fence::NO_FENCE);
5660 input.setSurfaceDamage(damage);
5661 IGraphicBufferProducer::QueueBufferOutput output;
5662 ASSERT_EQ(OK, producer->queueBuffer(slot, input, &output));
5663 };
5664
5665 auto fillAndPostBuffers = [&](const Color& color) {
5666 int slot1;
5667 ASSERT_NO_FATAL_FAILURE(dequeue(&slot1));
5668 int slot2;
5669 ASSERT_NO_FATAL_FAILURE(dequeue(&slot2));
5670
5671 sp<GraphicBuffer> buf1;
5672 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot1, &buf1));
5673 sp<GraphicBuffer> buf2;
5674 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot2, &buf2));
5675 fillGraphicBufferColor(buf1, Rect(width, height), color);
5676 fillGraphicBufferColor(buf2, Rect(width, height), color);
5677
5678 const auto displayTime = systemTime() + milliseconds_to_nanoseconds(100);
5679 ASSERT_NO_FATAL_FAILURE(queue(slot1, Region::INVALID_REGION, displayTime));
5680 ASSERT_NO_FATAL_FAILURE(
5681 queue(slot2, Region(Rect(width / 3, height / 3, 2 * width / 3, 2 * height / 3)),
5682 displayTime));
5683 };
5684
5685 const auto startTime = systemTime();
5686 const std::array<Color, 3> colors = {Color::RED, Color::GREEN, Color::BLUE};
5687 int colorIndex = 0;
5688 while (nanoseconds_to_seconds(systemTime() - startTime) < 10) {
5689 ASSERT_NO_FATAL_FAILURE(fillAndPostBuffers(colors[colorIndex++ % colors.size()]));
5690 std::this_thread::sleep_for(1s);
5691 }
5692
5693 ASSERT_EQ(OK, producer->disconnect(NATIVE_WINDOW_API_CPU));
5694}
5695
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005696} // namespace android