blob: acb263afd8d11f5700b61d2e547361f9050c67c5 [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)
Lucas Dupin0b79f8c2019-08-26 17:01:33 -07001294 .setCrop_legacy(layer, Rect(0, 0, size, size))
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001295 .apply();
1296 {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001297 const uint8_t bottom = size - 1;
1298 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001299 auto shot = getScreenCapture();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001300 // Transparent corners
1301 shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
Lucas Dupina1d0e312018-12-04 22:30:27 -08001302 shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
1303 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1304 shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
1305 }
1306}
1307
Alec Mouri80863a62019-01-17 15:19:35 -08001308TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildCrop) {
Lucas Dupina1d0e312018-12-04 22:30:27 -08001309 sp<SurfaceControl> parent;
1310 sp<SurfaceControl> child;
1311 const uint8_t size = 64;
1312 const uint8_t testArea = 4;
1313 const float cornerRadius = 20.0f;
1314 ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
1315 ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
1316 ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
1317 ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
1318
1319 Transaction()
1320 .setCornerRadius(parent, cornerRadius)
Lucas Dupin0b79f8c2019-08-26 17:01:33 -07001321 .setCrop_legacy(parent, Rect(0, 0, size, size))
Lucas Dupina1d0e312018-12-04 22:30:27 -08001322 .reparent(child, parent->getHandle())
1323 .setPosition(child, 0, size / 2)
1324 .apply();
1325 {
1326 const uint8_t bottom = size - 1;
1327 const uint8_t right = size - 1;
Alec Mouri80863a62019-01-17 15:19:35 -08001328 auto shot = getScreenCapture();
Lucas Dupina1d0e312018-12-04 22:30:27 -08001329 // Top edge of child should not have rounded corners because it's translated in the parent
1330 shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
1331 Color::GREEN);
1332 // But bottom edges should have been clipped according to parent bounds
1333 shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
1334 shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001335 }
1336}
1337
Alec Mouri80863a62019-01-17 15:19:35 -08001338TEST_P(LayerRenderTypeTransactionTest, SetColorBasic) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001339 sp<SurfaceControl> bufferLayer;
1340 sp<SurfaceControl> colorLayer;
1341 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001342 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001343 ASSERT_NO_FATAL_FAILURE(colorLayer =
1344 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1345 ISurfaceComposerClient::eFXSurfaceColor));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001346
Vishnu Nair88a11f22018-11-28 18:30:57 -08001347 Transaction()
1348 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1349 .setLayer(colorLayer, mLayerZBase + 1)
1350 .apply();
1351
Chia-I Wue4ef6102017-11-01 15:16:35 -07001352 {
1353 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08001354 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001355 }
1356
1357 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1358 const Color expected = {15, 51, 85, 255};
1359 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1360 // channel) should be less than one
1361 const uint8_t tolerance = 1;
1362 Transaction().setColor(colorLayer, color).apply();
1363 {
1364 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08001365 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001366 }
1367}
1368
Valerie Haudd0b7572019-01-29 14:59:27 -08001369// RED: Color layer base color and BufferQueueLayer/BufferStateLayer fill
1370// BLUE: prior background color
1371// GREEN: final background color
1372// BLACK: no color or fill
1373void LayerRenderTypeTransactionTest::setBackgroundColorHelper(uint32_t layerType, bool priorColor,
1374 bool bufferFill, float alpha,
1375 Color finalColor) {
1376 sp<SurfaceControl> layer;
1377 int32_t width = 500;
1378 int32_t height = 500;
Valerie Haua72e2812019-01-23 13:40:39 -08001379
Valerie Haudd0b7572019-01-29 14:59:27 -08001380 Color fillColor = Color::RED;
1381 Color priorBgColor = Color::BLUE;
1382 Color expectedColor = Color::BLACK;
1383 switch (layerType) {
1384 case ISurfaceComposerClient::eFXSurfaceColor:
1385 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 0, 0, layerType));
1386 Transaction()
1387 .setCrop_legacy(layer, Rect(0, 0, width, height))
1388 .setColor(layer, half3(1.0f, 0, 0))
1389 .apply();
1390 expectedColor = fillColor;
1391 break;
1392 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
1393 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
1394 if (bufferFill) {
1395 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, fillColor, width, height));
1396 expectedColor = fillColor;
1397 }
1398 Transaction().setCrop_legacy(layer, Rect(0, 0, width, height)).apply();
1399 break;
1400 case ISurfaceComposerClient::eFXSurfaceBufferState:
1401 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height, layerType));
1402 if (bufferFill) {
1403 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, fillColor, width, height));
1404 expectedColor = fillColor;
1405 }
1406 Transaction().setFrame(layer, Rect(0, 0, width, height)).apply();
1407 break;
1408 default:
1409 GTEST_FAIL() << "Unknown layer type in setBackgroundColorHelper";
1410 return;
Valerie Haua72e2812019-01-23 13:40:39 -08001411 }
1412
Valerie Haudd0b7572019-01-29 14:59:27 -08001413 if (priorColor && layerType != ISurfaceComposerClient::eFXSurfaceColor) {
1414 Transaction()
1415 .setBackgroundColor(layer, half3(0, 0, 1.0f), 1.0f, ui::Dataspace::UNKNOWN)
1416 .apply();
1417 if (!bufferFill) {
1418 expectedColor = priorBgColor;
1419 }
1420 }
1421
1422 {
1423 SCOPED_TRACE("default before setting background color layer");
1424 screenshot()->expectColor(Rect(0, 0, width, height), expectedColor);
1425 }
Valerie Haua72e2812019-01-23 13:40:39 -08001426 Transaction()
Valerie Haudd0b7572019-01-29 14:59:27 -08001427 .setBackgroundColor(layer, half3(0, 1.0f, 0), alpha, ui::Dataspace::UNKNOWN)
Valerie Haua72e2812019-01-23 13:40:39 -08001428 .apply();
1429
1430 {
Valerie Haua72e2812019-01-23 13:40:39 -08001431 auto shot = screenshot();
Valerie Haudd0b7572019-01-29 14:59:27 -08001432 shot->expectColor(Rect(0, 0, width, height), finalColor);
1433 shot->expectBorder(Rect(0, 0, width, height), Color::BLACK);
Valerie Haua72e2812019-01-23 13:40:39 -08001434 }
1435}
1436
Valerie Haudd0b7572019-01-29 14:59:27 -08001437TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_Color_NoEffect) {
1438 bool priorColor = false;
1439 bool bufferFill = false;
1440 float alpha = 1.0f;
1441 Color finalColor = Color::RED;
1442 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceColor,
1443 priorColor, bufferFill, alpha, finalColor));
1444}
Valerie Haua72e2812019-01-23 13:40:39 -08001445
Valerie Haudd0b7572019-01-29 14:59:27 -08001446TEST_P(LayerRenderTypeTransactionTest,
1447 SetBackgroundColor_BufferQueue_BufferFill_NoPriorColor_Basic) {
1448 bool priorColor = false;
1449 bool bufferFill = true;
1450 float alpha = 1.0f;
1451 Color finalColor = Color::RED;
1452 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1453 priorColor, bufferFill, alpha, finalColor));
1454}
Valerie Haua72e2812019-01-23 13:40:39 -08001455
Valerie Haudd0b7572019-01-29 14:59:27 -08001456TEST_P(LayerRenderTypeTransactionTest,
1457 SetBackgroundColor_BufferQueue_NoBufferFill_NoPriorColor_Basic) {
1458 bool priorColor = false;
1459 bool bufferFill = false;
1460 float alpha = 1.0f;
1461 Color finalColor = Color::GREEN;
1462 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1463 priorColor, bufferFill, alpha, finalColor));
1464}
Valerie Haua72e2812019-01-23 13:40:39 -08001465
Valerie Haudd0b7572019-01-29 14:59:27 -08001466TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_BufferQueue_BufferFill_PriorColor_Basic) {
1467 bool priorColor = true;
1468 bool bufferFill = true;
1469 float alpha = 1.0f;
1470 Color finalColor = Color::RED;
1471 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1472 priorColor, bufferFill, alpha, finalColor));
1473}
1474
1475TEST_P(LayerRenderTypeTransactionTest,
1476 SetBackgroundColor_BufferQueue_NoBufferFill_PriorColor_Basic) {
1477 bool priorColor = true;
1478 bool bufferFill = false;
1479 float alpha = 1.0f;
1480 Color finalColor = Color::GREEN;
1481 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1482 priorColor, bufferFill, alpha, finalColor));
1483}
1484TEST_P(LayerRenderTypeTransactionTest,
1485 SetBackgroundColor_BufferQueue_NoPriorColor_ZeroAlpha_NoEffect) {
1486 bool priorColor = false;
1487 bool bufferFill = false;
1488 float alpha = 0;
1489 Color finalColor = Color::BLACK;
1490 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1491 priorColor, bufferFill, alpha, finalColor));
1492}
1493
1494TEST_P(LayerRenderTypeTransactionTest,
1495 SetBackgroundColor_BufferQueue_PriorColor_ZeroAlpha_DeleteBackground) {
1496 bool priorColor = true;
1497 bool bufferFill = false;
1498 float alpha = 0;
1499 Color finalColor = Color::BLACK;
1500 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
1501 priorColor, bufferFill, alpha, finalColor));
1502}
1503
1504TEST_P(LayerRenderTypeTransactionTest,
1505 SetBackgroundColor_BufferState_BufferFill_NoPriorColor_Basic) {
1506 bool priorColor = false;
1507 bool bufferFill = true;
1508 float alpha = 1.0f;
1509 Color finalColor = Color::RED;
Valerie Haua6b15a12019-02-05 14:16:30 -08001510 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001511 priorColor, bufferFill, alpha, finalColor));
1512}
1513
1514TEST_P(LayerRenderTypeTransactionTest,
1515 SetBackgroundColor_BufferState_NoBufferFill_NoPriorColor_Basic) {
1516 bool priorColor = false;
1517 bool bufferFill = false;
1518 float alpha = 1.0f;
1519 Color finalColor = Color::GREEN;
Valerie Haua6b15a12019-02-05 14:16:30 -08001520 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001521 priorColor, bufferFill, alpha, finalColor));
1522}
1523
1524TEST_P(LayerRenderTypeTransactionTest,
1525 SetBackgroundColor_BufferState_NoBufferFill_PriorColor_Basic) {
1526 bool priorColor = true;
1527 bool bufferFill = false;
1528 float alpha = 1.0f;
1529 Color finalColor = Color::GREEN;
Valerie Haua6b15a12019-02-05 14:16:30 -08001530 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001531 priorColor, bufferFill, alpha, finalColor));
1532}
1533
1534TEST_P(LayerRenderTypeTransactionTest,
1535 SetBackgroundColor_BufferState_NoPriorColor_ZeroAlpha_NoEffect) {
1536 bool priorColor = false;
1537 bool bufferFill = false;
1538 float alpha = 0;
1539 Color finalColor = Color::BLACK;
Valerie Haua6b15a12019-02-05 14:16:30 -08001540 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001541 priorColor, bufferFill, alpha, finalColor));
1542}
1543
1544TEST_P(LayerRenderTypeTransactionTest,
1545 SetBackgroundColor_BufferState_PriorColor_ZeroAlpha_DeleteBackground) {
1546 bool priorColor = true;
1547 bool bufferFill = false;
1548 float alpha = 0;
1549 Color finalColor = Color::BLACK;
Valerie Haua6b15a12019-02-05 14:16:30 -08001550 ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
Valerie Haudd0b7572019-01-29 14:59:27 -08001551 priorColor, bufferFill, alpha, finalColor));
Valerie Haua72e2812019-01-23 13:40:39 -08001552}
1553
Alec Mouri80863a62019-01-17 15:19:35 -08001554TEST_P(LayerRenderTypeTransactionTest, SetColorClamped) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001555 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08001556 ASSERT_NO_FATAL_FAILURE(colorLayer =
1557 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1558 ISurfaceComposerClient::eFXSurfaceColor));
1559 Transaction()
1560 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
1561 .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
1562 .apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001563
Alec Mouri80863a62019-01-17 15:19:35 -08001564 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001565}
1566
Alec Mouri80863a62019-01-17 15:19:35 -08001567TEST_P(LayerRenderTypeTransactionTest, SetColorWithAlpha) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001568 sp<SurfaceControl> bufferLayer;
1569 sp<SurfaceControl> colorLayer;
1570 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001571 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001572 ASSERT_NO_FATAL_FAILURE(colorLayer =
1573 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
1574 ISurfaceComposerClient::eFXSurfaceColor));
1575 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Chia-I Wue4ef6102017-11-01 15:16:35 -07001576
1577 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1578 const float alpha = 0.25f;
1579 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1580 // this is handwavy, but the precison loss scaled by 255 (8-bit per
1581 // channel) should be less than one
1582 const uint8_t tolerance = 1;
1583 Transaction()
1584 .setColor(colorLayer, color)
1585 .setAlpha(colorLayer, alpha)
1586 .setLayer(colorLayer, mLayerZBase + 1)
1587 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001588 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1589 tolerance);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001590}
1591
Alec Mouri80863a62019-01-17 15:19:35 -08001592TEST_P(LayerRenderTypeTransactionTest, SetColorWithParentAlpha_Bug74220420) {
Adrian Roosb7a96502018-04-08 11:38:55 -07001593 sp<SurfaceControl> bufferLayer;
1594 sp<SurfaceControl> parentLayer;
1595 sp<SurfaceControl> colorLayer;
1596 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
1597 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001598 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
Vishnu Nair88a11f22018-11-28 18:30:57 -08001599 ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
1600 0 /* buffer height */,
1601 ISurfaceComposerClient::eFXSurfaceColor));
1602 Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
Adrian Roosb7a96502018-04-08 11:38:55 -07001603 const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
1604 const float alpha = 0.25f;
1605 const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
1606 // this is handwavy, but the precision loss scaled by 255 (8-bit per
1607 // channel) should be less than one
1608 const uint8_t tolerance = 1;
1609 Transaction()
1610 .reparent(colorLayer, parentLayer->getHandle())
1611 .setColor(colorLayer, color)
1612 .setAlpha(parentLayer, alpha)
1613 .setLayer(parentLayer, mLayerZBase + 1)
1614 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001615 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
1616 tolerance);
Adrian Roosb7a96502018-04-08 11:38:55 -07001617}
1618
Alec Mouri80863a62019-01-17 15:19:35 -08001619TEST_P(LayerTypeAndRenderTypeTransactionTest, SetColorWithBuffer) {
Chia-I Wue4ef6102017-11-01 15:16:35 -07001620 sp<SurfaceControl> bufferLayer;
1621 ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001622 ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
Chia-I Wue4ef6102017-11-01 15:16:35 -07001623
1624 // color is ignored
1625 Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001626 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wue4ef6102017-11-01 15:16:35 -07001627}
1628
Alec Mouri80863a62019-01-17 15:19:35 -08001629TEST_P(LayerTypeAndRenderTypeTransactionTest, SetLayerStackBasic) {
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001630 sp<SurfaceControl> layer;
1631 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001632 ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001633
1634 Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
1635 {
1636 SCOPED_TRACE("non-existing layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001637 getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001638 }
1639
1640 Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
1641 {
1642 SCOPED_TRACE("original layer stack");
Alec Mouri80863a62019-01-17 15:19:35 -08001643 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu3d22f3a2017-11-02 08:30:27 -07001644 }
1645}
1646
Alec Mouri80863a62019-01-17 15:19:35 -08001647TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001648 sp<SurfaceControl> layer;
1649 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001650 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1651 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001652
1653 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
1654 {
1655 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001656 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1657 Color::BLUE, Color::WHITE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001658 }
1659
1660 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
1661 {
1662 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001663 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED,
1664 Color::WHITE, Color::BLUE);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001665 }
1666
1667 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
1668 {
1669 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001670 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE,
1671 Color::RED, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001672 }
1673
1674 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
1675 {
1676 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001677 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED,
1678 Color::WHITE, Color::GREEN);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001679 }
1680
1681 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
1682 {
1683 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001684 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN,
1685 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001686 }
1687}
1688
Alec Mouri80863a62019-01-17 15:19:35 -08001689TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07001690 sp<SurfaceControl> layer;
1691 ASSERT_NO_FATAL_FAILURE(
1692 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1693 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1694 Color::BLUE, Color::WHITE));
1695
1696 Transaction()
1697 .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
1698 .setFrame(layer, Rect(0, 0, 32, 32))
1699 .apply();
1700 {
1701 SCOPED_TRACE("IDENTITY");
Alec Mouri80863a62019-01-17 15:19:35 -08001702 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1703 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001704 }
1705
1706 Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
1707 {
1708 SCOPED_TRACE("FLIP_H");
Alec Mouri80863a62019-01-17 15:19:35 -08001709 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1710 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001711 }
1712
1713 Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
1714 {
1715 SCOPED_TRACE("FLIP_V");
Alec Mouri80863a62019-01-17 15:19:35 -08001716 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1717 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001718 }
1719
1720 Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
1721 {
1722 SCOPED_TRACE("ROT_90");
Alec Mouri80863a62019-01-17 15:19:35 -08001723 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1724 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001725 }
1726
1727 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
1728 {
1729 SCOPED_TRACE("SCALE");
Alec Mouri80863a62019-01-17 15:19:35 -08001730 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
1731 Color::BLUE, Color::WHITE);
Marissa Wall861616d2018-10-22 12:52:23 -07001732 }
1733}
1734
Alec Mouri80863a62019-01-17 15:19:35 -08001735TEST_P(LayerRenderTypeTransactionTest, SetMatrixRot45_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001736 sp<SurfaceControl> layer;
1737 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001738 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1739 Color::BLUE, Color::WHITE));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001740
1741 const float rot = M_SQRT1_2; // 45 degrees
1742 const float trans = M_SQRT2 * 16.0f;
1743 Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
1744
Alec Mouri80863a62019-01-17 15:19:35 -08001745 auto shot = getScreenCapture();
Chia-I Wu93853fe2017-11-02 08:30:27 -07001746 // check a 8x8 region inside each color
1747 auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
1748 const int32_t halfL = 4;
1749 return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
1750 };
1751 const int32_t unit = int32_t(trans / 2);
1752 shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
1753 shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
1754 shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
1755 shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
1756}
1757
Alec Mouri80863a62019-01-17 15:19:35 -08001758TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithResize_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001759 sp<SurfaceControl> layer;
Marissa Wall861616d2018-10-22 12:52:23 -07001760 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1761 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001762
1763 // setMatrix is applied after any pending resize, unlike setPosition
1764 Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
1765 {
1766 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001767 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001768 const Rect rect(0, 0, 32, 32);
Marissa Wall61c58622018-07-18 10:12:20 -07001769 shot->expectColor(rect, Color::RED);
1770 shot->expectBorder(rect, Color::BLACK);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001771 }
1772
Marissa Wall861616d2018-10-22 12:52:23 -07001773 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001774 {
1775 SCOPED_TRACE("resize applied");
Marissa Wall861616d2018-10-22 12:52:23 -07001776 const Rect rect(0, 0, 128, 128);
Alec Mouri80863a62019-01-17 15:19:35 -08001777 getScreenCapture()->expectColor(rect, Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001778 }
1779}
1780
Alec Mouri80863a62019-01-17 15:19:35 -08001781TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
Chia-I Wu93853fe2017-11-02 08:30:27 -07001782 sp<SurfaceControl> layer;
1783 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001784 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu93853fe2017-11-02 08:30:27 -07001785
1786 // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
1787 Transaction()
1788 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
1789 .setSize(layer, 64, 64)
1790 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1791 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001792 getScreenCapture()->expectColor(Rect(0, 0, 128, 128), Color::RED);
Chia-I Wu93853fe2017-11-02 08:30:27 -07001793}
1794
Alec Mouri80863a62019-01-17 15:19:35 -08001795TEST_P(LayerRenderTypeTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
Chia-I Wua56b2042017-11-01 15:16:35 -07001796 sp<SurfaceControl> layer;
1797 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall861616d2018-10-22 12:52:23 -07001798 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
1799 Color::BLUE, Color::WHITE));
Chia-I Wua56b2042017-11-01 15:16:35 -07001800
1801 // XXX SCALE_CROP is not respected; calling setSize and
1802 // setOverrideScalingMode in separate transactions does not work
1803 // (b/69315456)
1804 Transaction()
1805 .setSize(layer, 64, 16)
1806 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
1807 .apply();
1808 {
1809 SCOPED_TRACE("SCALE_TO_WINDOW");
Alec Mouri80863a62019-01-17 15:19:35 -08001810 getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN,
1811 Color::BLUE, Color::WHITE, true /* filtered */);
Chia-I Wua56b2042017-11-01 15:16:35 -07001812 }
1813}
1814
Dan Stoza000dd012018-08-01 13:31:52 -07001815TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
1816 sp<SurfaceControl> layer;
1817 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
1818
1819 sp<IBinder> handle = layer->getHandle();
1820 ASSERT_TRUE(handle != nullptr);
1821
1822 FrameStats frameStats;
1823 mClient->getLayerFrameStats(handle, &frameStats);
1824
1825 ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
1826}
1827
Alec Mouri80863a62019-01-17 15:19:35 -08001828TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001829 sp<SurfaceControl> layer;
1830 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001831 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001832 const Rect crop(8, 8, 24, 24);
1833
Marissa Wallf58c14b2018-07-24 10:50:43 -07001834 Transaction().setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001835 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001836 shot->expectColor(crop, Color::RED);
1837 shot->expectBorder(crop, Color::BLACK);
1838}
1839
Alec Mouri80863a62019-01-17 15:19:35 -08001840TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001841 sp<SurfaceControl> layer;
1842 ASSERT_NO_FATAL_FAILURE(
1843 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1844 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1845 const Rect crop(8, 8, 24, 24);
1846
1847 Transaction().setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001848 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001849 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
1850 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001851}
1852
Alec Mouri80863a62019-01-17 15:19:35 -08001853TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001854 sp<SurfaceControl> layer;
1855 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001856 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001857
1858 {
1859 SCOPED_TRACE("empty rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001860 Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001861 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07001862 }
1863
1864 {
1865 SCOPED_TRACE("negative rect");
Marissa Wallf58c14b2018-07-24 10:50:43 -07001866 Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001867 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07001868 }
1869}
1870
Alec Mouri80863a62019-01-17 15:19:35 -08001871TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001872 sp<SurfaceControl> layer;
1873 ASSERT_NO_FATAL_FAILURE(
1874 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1875 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1876
1877 {
1878 SCOPED_TRACE("empty rect");
1879 Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
Marissa Wall290ad082019-03-06 13:23:47 -08001880 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07001881 }
1882
1883 {
1884 SCOPED_TRACE("negative rect");
1885 Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
Marissa Wall290ad082019-03-06 13:23:47 -08001886 getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
Marissa Wall61c58622018-07-18 10:12:20 -07001887 }
1888}
1889
Alec Mouri80863a62019-01-17 15:19:35 -08001890TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001891 sp<SurfaceControl> layer;
1892 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001893 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001894
Marissa Wallf58c14b2018-07-24 10:50:43 -07001895 Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001896 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001897 shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
1898 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
1899}
1900
Alec Mouri80863a62019-01-17 15:19:35 -08001901TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferState) {
Valerie Hau0bc09152018-12-20 07:42:47 -08001902 sp<SurfaceControl> layer;
Marissa Wall290ad082019-03-06 13:23:47 -08001903 ASSERT_NO_FATAL_FAILURE(
1904 layer = createLayer("test", 32, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
Valerie Hau0bc09152018-12-20 07:42:47 -08001905 sp<GraphicBuffer> buffer =
Marissa Wall290ad082019-03-06 13:23:47 -08001906 new GraphicBuffer(32, 64, PIXEL_FORMAT_RGBA_8888, 1,
Valerie Hau0bc09152018-12-20 07:42:47 -08001907 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
1908 BufferUsage::COMPOSER_OVERLAY,
1909 "test");
Marissa Wall290ad082019-03-06 13:23:47 -08001910 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 16), Color::BLUE);
1911 fillGraphicBufferColor(buffer, Rect(0, 16, 32, 64), Color::RED);
1912
1913 Transaction().setFrame(layer, Rect(0, 0, 64, 64)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001914
1915 Transaction().setBuffer(layer, buffer).apply();
1916
1917 // Partially out of bounds in the negative (upper left) direction
Marissa Wall290ad082019-03-06 13:23:47 -08001918 Transaction().setCrop(layer, Rect(-128, -128, 32, 16)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001919 {
1920 SCOPED_TRACE("out of bounds, negative (upper left) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08001921 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001922 shot->expectColor(Rect(0, 0, 64, 64), Color::BLUE);
1923 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001924 }
1925
1926 // Partially out of bounds in the positive (lower right) direction
Marissa Wall290ad082019-03-06 13:23:47 -08001927 Transaction().setCrop(layer, Rect(0, 16, 128, 128)).apply();
Valerie Hau0bc09152018-12-20 07:42:47 -08001928 {
1929 SCOPED_TRACE("out of bounds, positive (lower right) direction");
Alec Mouri80863a62019-01-17 15:19:35 -08001930 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001931 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
1932 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001933 }
1934
1935 // Fully out of buffer space bounds
1936 Transaction().setCrop(layer, Rect(-128, -128, -1, -1)).apply();
1937 {
1938 SCOPED_TRACE("Fully out of bounds");
Alec Mouri80863a62019-01-17 15:19:35 -08001939 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08001940 shot->expectColor(Rect(0, 0, 64, 16), Color::BLUE);
1941 shot->expectColor(Rect(0, 16, 64, 64), Color::RED);
1942 shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
Valerie Hau0bc09152018-12-20 07:42:47 -08001943 }
1944}
1945
Alec Mouri80863a62019-01-17 15:19:35 -08001946TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001947 sp<SurfaceControl> layer;
1948 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001949 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001950
1951 const Point position(32, 32);
1952 const Rect crop(8, 8, 24, 24);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001953 Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001954 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001955 shot->expectColor(crop + position, Color::RED);
1956 shot->expectBorder(crop + position, Color::BLACK);
1957}
1958
Alec Mouri80863a62019-01-17 15:19:35 -08001959TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07001960 sp<SurfaceControl> layer;
1961 ASSERT_NO_FATAL_FAILURE(
1962 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
1963 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
1964
Marissa Wall861616d2018-10-22 12:52:23 -07001965 const Rect frame(32, 32, 64, 64);
Marissa Wall61c58622018-07-18 10:12:20 -07001966 const Rect crop(8, 8, 24, 24);
Marissa Wall861616d2018-10-22 12:52:23 -07001967 Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001968 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07001969 shot->expectColor(frame, Color::RED);
1970 shot->expectBorder(frame, Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07001971}
1972
Alec Mouri80863a62019-01-17 15:19:35 -08001973TEST_P(LayerRenderTypeTransactionTest, SetCropWithScale_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001974 sp<SurfaceControl> layer;
1975 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001976 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001977
Marissa Wall861616d2018-10-22 12:52:23 -07001978 // crop_legacy is affected by matrix
Chia-I Wu04dcca82017-11-02 08:30:27 -07001979 Transaction()
1980 .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001981 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
Chia-I Wu04dcca82017-11-02 08:30:27 -07001982 .apply();
Alec Mouri80863a62019-01-17 15:19:35 -08001983 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001984 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
1985 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
1986}
1987
Alec Mouri80863a62019-01-17 15:19:35 -08001988TEST_P(LayerRenderTypeTransactionTest, SetCropWithResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07001989 sp<SurfaceControl> layer;
1990 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07001991 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07001992
Marissa Wallf58c14b2018-07-24 10:50:43 -07001993 // setCrop_legacy is applied immediately by default, with or without resize pending
1994 Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001995 {
1996 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08001997 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07001998 shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
1999 shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
2000 }
2001
Marissa Wall61c58622018-07-18 10:12:20 -07002002 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002003 {
2004 SCOPED_TRACE("resize applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002005 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002006 shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
2007 shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
2008 }
2009}
2010
Alec Mouri80863a62019-01-17 15:19:35 -08002011TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResize_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002012 sp<SurfaceControl> layer;
2013 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002014 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002015
Marissa Wallf58c14b2018-07-24 10:50:43 -07002016 // request setCrop_legacy to be applied with the next resize
2017 Transaction()
2018 .setCrop_legacy(layer, Rect(8, 8, 24, 24))
2019 .setGeometryAppliesWithResize(layer)
2020 .apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002021 {
2022 SCOPED_TRACE("waiting for next resize");
Alec Mouri80863a62019-01-17 15:19:35 -08002023 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002024 }
2025
Marissa Wallf58c14b2018-07-24 10:50:43 -07002026 Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002027 {
2028 SCOPED_TRACE("pending crop modified");
Alec Mouri80863a62019-01-17 15:19:35 -08002029 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002030 }
2031
2032 Transaction().setSize(layer, 16, 16).apply();
2033 {
2034 SCOPED_TRACE("resize pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002035 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
Chia-I Wu04dcca82017-11-02 08:30:27 -07002036 }
2037
2038 // finally resize
Marissa Wall61c58622018-07-18 10:12:20 -07002039 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002040 {
2041 SCOPED_TRACE("new crop applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002042 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002043 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2044 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2045 }
2046}
2047
Alec Mouri80863a62019-01-17 15:19:35 -08002048TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
Chia-I Wu04dcca82017-11-02 08:30:27 -07002049 sp<SurfaceControl> layer;
2050 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Marissa Wall61c58622018-07-18 10:12:20 -07002051 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002052
Marissa Wallf58c14b2018-07-24 10:50:43 -07002053 // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
Chia-I Wu04dcca82017-11-02 08:30:27 -07002054 Transaction()
Marissa Wallf58c14b2018-07-24 10:50:43 -07002055 .setCrop_legacy(layer, Rect(4, 4, 12, 12))
Chia-I Wu04dcca82017-11-02 08:30:27 -07002056 .setSize(layer, 16, 16)
2057 .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
2058 .setGeometryAppliesWithResize(layer)
2059 .apply();
2060 {
2061 SCOPED_TRACE("new crop pending");
Alec Mouri80863a62019-01-17 15:19:35 -08002062 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002063 shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
2064 shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
2065 }
2066
2067 // XXX crop is never latched without other geometry change (b/69315677)
2068 Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002069 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
Chia-I Wu04dcca82017-11-02 08:30:27 -07002070 Transaction().setPosition(layer, 0, 0).apply();
2071 {
2072 SCOPED_TRACE("new crop applied");
Alec Mouri80863a62019-01-17 15:19:35 -08002073 auto shot = getScreenCapture();
Chia-I Wu04dcca82017-11-02 08:30:27 -07002074 shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
2075 shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
2076 }
2077}
2078
Alec Mouri80863a62019-01-17 15:19:35 -08002079TEST_P(LayerRenderTypeTransactionTest, SetFrameBasic_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002080 sp<SurfaceControl> layer;
2081 ASSERT_NO_FATAL_FAILURE(
2082 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2083 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2084 const Rect frame(8, 8, 24, 24);
2085
2086 Transaction().setFrame(layer, frame).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002087 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002088 shot->expectColor(frame, Color::RED);
2089 shot->expectBorder(frame, Color::BLACK);
2090}
2091
Alec Mouri80863a62019-01-17 15:19:35 -08002092TEST_P(LayerRenderTypeTransactionTest, SetFrameEmpty_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002093 sp<SurfaceControl> layer;
2094 ASSERT_NO_FATAL_FAILURE(
2095 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2096 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2097
Marissa Wall61c58622018-07-18 10:12:20 -07002098 {
Marissa Wall861616d2018-10-22 12:52:23 -07002099 SCOPED_TRACE("empty rect");
2100 Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002101 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002102 }
2103
Marissa Wall61c58622018-07-18 10:12:20 -07002104 {
Marissa Wall861616d2018-10-22 12:52:23 -07002105 SCOPED_TRACE("negative rect");
2106 Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
Alec Mouri80863a62019-01-17 15:19:35 -08002107 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002108 }
2109}
2110
Alec Mouri80863a62019-01-17 15:19:35 -08002111TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultParentless_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002112 sp<SurfaceControl> layer;
2113 ASSERT_NO_FATAL_FAILURE(
2114 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2115 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
2116
2117 // A parentless layer will default to a frame with the same size as the buffer
Alec Mouri80863a62019-01-17 15:19:35 -08002118 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002119 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2120 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall861616d2018-10-22 12:52:23 -07002121}
2122
Alec Mouri80863a62019-01-17 15:19:35 -08002123TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBSParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002124 sp<SurfaceControl> parent, child;
2125 ASSERT_NO_FATAL_FAILURE(
2126 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2127 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2128 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2129
2130 ASSERT_NO_FATAL_FAILURE(
2131 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2132 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2133
2134 Transaction().reparent(child, parent->getHandle()).apply();
2135
2136 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002137 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002138 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2139 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2140}
2141
Alec Mouri80863a62019-01-17 15:19:35 -08002142TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBQParent_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002143 sp<SurfaceControl> parent, child;
2144 ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
2145 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
2146
2147 ASSERT_NO_FATAL_FAILURE(
2148 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2149 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2150
2151 Transaction().reparent(child, parent->getHandle()).apply();
2152
2153 // A layer will default to the frame of its parent
Alec Mouri80863a62019-01-17 15:19:35 -08002154 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002155 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2156 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2157}
2158
Alec Mouri80863a62019-01-17 15:19:35 -08002159TEST_P(LayerRenderTypeTransactionTest, SetFrameUpdate_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002160 sp<SurfaceControl> layer;
2161 ASSERT_NO_FATAL_FAILURE(
2162 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2163 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2164 Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
2165
2166 std::this_thread::sleep_for(500ms);
2167
2168 Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
2169
Alec Mouri80863a62019-01-17 15:19:35 -08002170 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002171 shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
2172 shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
2173}
2174
Alec Mouri80863a62019-01-17 15:19:35 -08002175TEST_P(LayerRenderTypeTransactionTest, SetFrameOutsideBounds_BufferState) {
Marissa Wall861616d2018-10-22 12:52:23 -07002176 sp<SurfaceControl> parent, child;
2177 ASSERT_NO_FATAL_FAILURE(
2178 parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2179 ASSERT_NO_FATAL_FAILURE(
2180 child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2181 Transaction().reparent(child, parent->getHandle()).apply();
2182
2183 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
2184 Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
2185
2186 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
2187 Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
2188
Alec Mouri80863a62019-01-17 15:19:35 -08002189 auto shot = getScreenCapture();
Marissa Wall861616d2018-10-22 12:52:23 -07002190 shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
2191 shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
2192 shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
2193}
2194
Alec Mouri80863a62019-01-17 15:19:35 -08002195TEST_P(LayerRenderTypeTransactionTest, SetBufferBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002196 sp<SurfaceControl> layer;
2197 ASSERT_NO_FATAL_FAILURE(
2198 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2199
2200 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2201
Alec Mouri80863a62019-01-17 15:19:35 -08002202 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002203 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2204 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002205}
2206
Alec Mouri80863a62019-01-17 15:19:35 -08002207TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleBuffers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002208 sp<SurfaceControl> layer;
2209 ASSERT_NO_FATAL_FAILURE(
2210 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2211
2212 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2213
2214 {
2215 SCOPED_TRACE("set buffer 1");
Alec Mouri80863a62019-01-17 15:19:35 -08002216 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002217 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2218 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002219 }
2220
2221 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
2222
2223 {
2224 SCOPED_TRACE("set buffer 2");
Alec Mouri80863a62019-01-17 15:19:35 -08002225 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002226 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLUE);
2227 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002228 }
2229
2230 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
2231
2232 {
2233 SCOPED_TRACE("set buffer 3");
Alec Mouri80863a62019-01-17 15:19:35 -08002234 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002235 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2236 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002237 }
2238}
2239
Alec Mouri80863a62019-01-17 15:19:35 -08002240TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleLayers_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002241 sp<SurfaceControl> layer1;
2242 ASSERT_NO_FATAL_FAILURE(
2243 layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
2244
2245 sp<SurfaceControl> layer2;
2246 ASSERT_NO_FATAL_FAILURE(
2247 layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2248
2249 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
2250
Marissa Wall861616d2018-10-22 12:52:23 -07002251 Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002252 {
2253 SCOPED_TRACE("set layer 1 buffer red");
Alec Mouri80863a62019-01-17 15:19:35 -08002254 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002255 shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
2256 }
2257
2258 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
2259
Marissa Wall861616d2018-10-22 12:52:23 -07002260 Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002261 {
2262 SCOPED_TRACE("set layer 2 buffer blue");
Alec Mouri80863a62019-01-17 15:19:35 -08002263 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002264 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2265 shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
2266 shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
2267 }
2268
2269 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
2270 {
2271 SCOPED_TRACE("set layer 1 buffer green");
Alec Mouri80863a62019-01-17 15:19:35 -08002272 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002273 shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
2274 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2275 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2276 }
2277
2278 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
2279
2280 {
2281 SCOPED_TRACE("set layer 2 buffer white");
Alec Mouri80863a62019-01-17 15:19:35 -08002282 auto shot = getScreenCapture();
Marissa Wall61c58622018-07-18 10:12:20 -07002283 shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
2284 shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
2285 shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
2286 }
2287}
2288
Valerie Haua6b15a12019-02-05 14:16:30 -08002289TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002290 sp<SurfaceControl> layer;
2291 ASSERT_NO_FATAL_FAILURE(
2292 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2293
2294 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2295
2296 std::array<sp<GraphicBuffer>, 10> buffers;
2297
2298 size_t idx = 0;
2299 for (auto& buffer : buffers) {
2300 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2301 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2302 BufferUsage::COMPOSER_OVERLAY,
2303 "test");
2304 Color color = colors[idx % colors.size()];
2305 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2306 idx++;
2307 }
2308
2309 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2310 // cache is working.
2311 idx = 0;
2312 for (auto& buffer : buffers) {
2313 for (int i = 0; i < 2; i++) {
2314 Transaction().setBuffer(layer, buffer).apply();
2315
2316 Color color = colors[idx % colors.size()];
2317 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002318 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2319 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002320 }
2321 idx++;
2322 }
2323}
2324
Valerie Haua6b15a12019-02-05 14:16:30 -08002325TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_LeastRecentlyUsed_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002326 sp<SurfaceControl> layer;
2327 ASSERT_NO_FATAL_FAILURE(
2328 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2329
2330 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2331
2332 std::array<sp<GraphicBuffer>, 70> buffers;
2333
2334 size_t idx = 0;
2335 for (auto& buffer : buffers) {
2336 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2337 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2338 BufferUsage::COMPOSER_OVERLAY,
2339 "test");
2340 Color color = colors[idx % colors.size()];
2341 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2342 idx++;
2343 }
2344
2345 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2346 // cache is working.
2347 idx = 0;
2348 for (auto& buffer : buffers) {
2349 for (int i = 0; i < 2; i++) {
2350 Transaction().setBuffer(layer, buffer).apply();
2351
2352 Color color = colors[idx % colors.size()];
2353 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002354 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2355 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002356 }
2357 idx++;
2358 }
2359}
2360
Valerie Haua6b15a12019-02-05 14:16:30 -08002361TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_DestroyedBuffer_BufferState) {
Marissa Wall73411622019-01-25 10:45:41 -08002362 sp<SurfaceControl> layer;
2363 ASSERT_NO_FATAL_FAILURE(
2364 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2365
2366 std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
2367
2368 std::array<sp<GraphicBuffer>, 65> buffers;
2369
2370 size_t idx = 0;
2371 for (auto& buffer : buffers) {
2372 buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2373 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2374 BufferUsage::COMPOSER_OVERLAY,
2375 "test");
2376 Color color = colors[idx % colors.size()];
2377 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
2378 idx++;
2379 }
2380
2381 // Set each buffer twice. The first time adds it to the cache, the second time tests that the
2382 // cache is working.
2383 idx = 0;
2384 for (auto& buffer : buffers) {
2385 for (int i = 0; i < 2; i++) {
2386 Transaction().setBuffer(layer, buffer).apply();
2387
2388 Color color = colors[idx % colors.size()];
2389 auto shot = screenshot();
Marissa Wall290ad082019-03-06 13:23:47 -08002390 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
2391 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall73411622019-01-25 10:45:41 -08002392 }
2393 if (idx == 0) {
2394 buffers[0].clear();
2395 }
2396 idx++;
2397 }
2398}
2399
Alec Mouri80863a62019-01-17 15:19:35 -08002400TEST_P(LayerRenderTypeTransactionTest, SetTransformRotate90_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002401 sp<SurfaceControl> layer;
2402 ASSERT_NO_FATAL_FAILURE(
2403 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2404
2405 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2406 Color::BLUE, Color::WHITE));
2407
Marissa Wall861616d2018-10-22 12:52:23 -07002408 Transaction()
2409 .setFrame(layer, Rect(0, 0, 32, 32))
2410 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
2411 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002412
Alec Mouri80863a62019-01-17 15:19:35 -08002413 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
2414 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002415}
2416
Alec Mouri80863a62019-01-17 15:19:35 -08002417TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipH_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002418 sp<SurfaceControl> layer;
2419 ASSERT_NO_FATAL_FAILURE(
2420 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2421
2422 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2423 Color::BLUE, Color::WHITE));
2424
Marissa Wall861616d2018-10-22 12:52:23 -07002425 Transaction()
2426 .setFrame(layer, Rect(0, 0, 32, 32))
2427 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
2428 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002429
Alec Mouri80863a62019-01-17 15:19:35 -08002430 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
2431 Color::BLUE, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002432}
2433
Alec Mouri80863a62019-01-17 15:19:35 -08002434TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipV_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002435 sp<SurfaceControl> layer;
2436 ASSERT_NO_FATAL_FAILURE(
2437 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2438
2439 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
2440 Color::BLUE, Color::WHITE));
2441
Marissa Wall861616d2018-10-22 12:52:23 -07002442 Transaction()
2443 .setFrame(layer, Rect(0, 0, 32, 32))
2444 .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
2445 .apply();
Marissa Wall61c58622018-07-18 10:12:20 -07002446
Alec Mouri80863a62019-01-17 15:19:35 -08002447 getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
2448 Color::GREEN, true /* filtered */);
Marissa Wall61c58622018-07-18 10:12:20 -07002449}
2450
2451TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
2452 sp<SurfaceControl> layer;
2453 ASSERT_NO_FATAL_FAILURE(
2454 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2455
2456 Transaction().setTransformToDisplayInverse(layer, false).apply();
2457
2458 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
2459
2460 Transaction().setTransformToDisplayInverse(layer, true).apply();
2461}
2462
Alec Mouri80863a62019-01-17 15:19:35 -08002463TEST_P(LayerRenderTypeTransactionTest, SetFenceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002464 sp<SurfaceControl> layer;
Marissa Wall713b63f2018-10-17 15:42:43 -07002465 Transaction transaction;
2466 ASSERT_NO_FATAL_FAILURE(
2467 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2468
2469 sp<GraphicBuffer> buffer =
2470 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2471 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2472 BufferUsage::COMPOSER_OVERLAY,
2473 "test");
2474 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2475
2476 sp<Fence> fence;
2477 if (getBuffer(nullptr, &fence) != NO_ERROR) {
2478 GTEST_SUCCEED() << "test not supported";
2479 return;
2480 }
2481
2482 Transaction().setBuffer(layer, buffer).setAcquireFence(layer, fence).apply();
2483
2484 status_t status = fence->wait(1000);
2485 ASSERT_NE(static_cast<status_t>(Fence::Status::Unsignaled), status);
2486 std::this_thread::sleep_for(200ms);
2487
Alec Mouri80863a62019-01-17 15:19:35 -08002488 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002489 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2490 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall713b63f2018-10-17 15:42:43 -07002491}
2492
Alec Mouri80863a62019-01-17 15:19:35 -08002493TEST_P(LayerRenderTypeTransactionTest, SetFenceNull_BufferState) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002494 sp<SurfaceControl> layer;
Marissa Wall61c58622018-07-18 10:12:20 -07002495 ASSERT_NO_FATAL_FAILURE(
2496 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2497
2498 sp<GraphicBuffer> buffer =
2499 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2500 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2501 BufferUsage::COMPOSER_OVERLAY,
2502 "test");
2503 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2504
Marissa Wallfda30bb2018-10-12 11:34:28 -07002505 sp<Fence> fence = Fence::NO_FENCE;
Marissa Wall61c58622018-07-18 10:12:20 -07002506
2507 Transaction()
2508 .setBuffer(layer, buffer)
2509 .setAcquireFence(layer, fence)
Marissa Wall61c58622018-07-18 10:12:20 -07002510 .apply();
2511
Alec Mouri80863a62019-01-17 15:19:35 -08002512 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002513 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2514 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002515}
2516
Alec Mouri80863a62019-01-17 15:19:35 -08002517TEST_P(LayerRenderTypeTransactionTest, SetDataspaceBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002518 sp<SurfaceControl> layer;
2519 ASSERT_NO_FATAL_FAILURE(
2520 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2521
2522 sp<GraphicBuffer> buffer =
2523 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2524 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2525 BufferUsage::COMPOSER_OVERLAY,
2526 "test");
2527 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2528
2529 Transaction()
2530 .setBuffer(layer, buffer)
2531 .setDataspace(layer, ui::Dataspace::UNKNOWN)
Marissa Wall61c58622018-07-18 10:12:20 -07002532 .apply();
2533
Alec Mouri80863a62019-01-17 15:19:35 -08002534 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002535 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2536 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002537}
2538
Alec Mouri80863a62019-01-17 15:19:35 -08002539TEST_P(LayerRenderTypeTransactionTest, SetHdrMetadataBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002540 sp<SurfaceControl> layer;
2541 ASSERT_NO_FATAL_FAILURE(
2542 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2543
2544 sp<GraphicBuffer> buffer =
2545 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2546 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2547 BufferUsage::COMPOSER_OVERLAY,
2548 "test");
2549 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2550
2551 HdrMetadata hdrMetadata;
2552 hdrMetadata.validTypes = 0;
2553 Transaction()
2554 .setBuffer(layer, buffer)
2555 .setHdrMetadata(layer, hdrMetadata)
Marissa Wall61c58622018-07-18 10:12:20 -07002556 .apply();
2557
Alec Mouri80863a62019-01-17 15:19:35 -08002558 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002559 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2560 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002561}
2562
Alec Mouri80863a62019-01-17 15:19:35 -08002563TEST_P(LayerRenderTypeTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002564 sp<SurfaceControl> layer;
2565 ASSERT_NO_FATAL_FAILURE(
2566 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2567
2568 sp<GraphicBuffer> buffer =
2569 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2570 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2571 BufferUsage::COMPOSER_OVERLAY,
2572 "test");
2573 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2574
2575 Region region;
2576 region.set(32, 32);
2577 Transaction()
2578 .setBuffer(layer, buffer)
2579 .setSurfaceDamageRegion(layer, region)
Marissa Wall61c58622018-07-18 10:12:20 -07002580 .apply();
2581
Alec Mouri80863a62019-01-17 15:19:35 -08002582 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002583 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2584 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002585}
2586
Alec Mouri80863a62019-01-17 15:19:35 -08002587TEST_P(LayerRenderTypeTransactionTest, SetApiBasic_BufferState) {
Marissa Wall61c58622018-07-18 10:12:20 -07002588 sp<SurfaceControl> layer;
2589 ASSERT_NO_FATAL_FAILURE(
2590 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2591
2592 sp<GraphicBuffer> buffer =
2593 new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
2594 BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
2595 BufferUsage::COMPOSER_OVERLAY,
2596 "test");
2597 fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
2598
2599 Transaction()
2600 .setBuffer(layer, buffer)
2601 .setApi(layer, NATIVE_WINDOW_API_CPU)
Marissa Wall61c58622018-07-18 10:12:20 -07002602 .apply();
2603
Alec Mouri80863a62019-01-17 15:19:35 -08002604 auto shot = getScreenCapture();
Marissa Wall290ad082019-03-06 13:23:47 -08002605 shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
2606 shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
Marissa Wall61c58622018-07-18 10:12:20 -07002607}
2608
2609TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
2610 sp<SurfaceControl> layer;
2611 ASSERT_NO_FATAL_FAILURE(
2612 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
2613
2614 // verify this doesn't cause a crash
2615 Transaction().setSidebandStream(layer, nullptr).apply();
2616}
2617
Robert Carr54cf5b12019-01-25 14:02:28 -08002618TEST_F(LayerTransactionTest, ReparentToSelf) {
2619 sp<SurfaceControl> layer;
2620 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
2621 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
2622 Transaction().reparent(layer, layer->getHandle()).apply();
2623
2624 {
2625 // We expect the transaction to be silently dropped, but for SurfaceFlinger
2626 // to still be functioning.
2627 SCOPED_TRACE("after reparent to self");
2628 const Rect rect(0, 0, 32, 32);
2629 auto shot = screenshot();
2630 shot->expectColor(rect, Color::RED);
2631 shot->expectBorder(rect, Color::BLACK);
2632 }
2633}
2634
Alec Mouri80863a62019-01-17 15:19:35 -08002635TEST_P(LayerRenderTypeTransactionTest, SetColorTransformBasic) {
Peiyong Lind3788632018-09-18 16:01:31 -07002636 sp<SurfaceControl> colorLayer;
Vishnu Nair88a11f22018-11-28 18:30:57 -08002637 ASSERT_NO_FATAL_FAILURE(colorLayer =
2638 createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2639 ISurfaceComposerClient::eFXSurfaceColor));
2640 Transaction()
2641 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2642 .setLayer(colorLayer, mLayerZBase + 1)
2643 .apply();
Peiyong Lind3788632018-09-18 16:01:31 -07002644 {
2645 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002646 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
Peiyong Lind3788632018-09-18 16:01:31 -07002647 }
2648
2649 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002650 half3 expected = color;
Peiyong Lind3788632018-09-18 16:01:31 -07002651 mat3 matrix;
2652 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2653 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2654 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
Ady Abraham2a6ab2a2018-10-26 14:25:30 -07002655
2656 // degamma before applying the matrix
2657 if (mColorManagementUsed) {
2658 ColorTransformHelper::DegammaColor(expected);
2659 }
2660
2661 ColorTransformHelper::applyMatrix(expected, matrix);
2662
2663 if (mColorManagementUsed) {
2664 ColorTransformHelper::GammaColor(expected);
2665 }
2666
2667 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2668 uint8_t(expected.b * 255), 255};
2669
2670 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2671 // channel) should be less than one
2672 const uint8_t tolerance = 1;
2673
Peiyong Lind3788632018-09-18 16:01:31 -07002674 Transaction().setColor(colorLayer, color)
2675 .setColorTransform(colorLayer, matrix, vec3()).apply();
2676 {
2677 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002678 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
Peiyong Lind3788632018-09-18 16:01:31 -07002679 }
2680}
2681
Alec Mouri80863a62019-01-17 15:19:35 -08002682TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnParent) {
chaviwf66724d2018-11-28 16:35:21 -08002683 sp<SurfaceControl> parentLayer;
2684 sp<SurfaceControl> colorLayer;
2685 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2686 0 /* buffer height */,
2687 ISurfaceComposerClient::eFXSurfaceContainer));
2688 ASSERT_NO_FATAL_FAILURE(
2689 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2690 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2691
2692 Transaction()
2693 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2694 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2695 .setLayer(parentLayer, mLayerZBase + 1)
2696 .apply();
2697 {
2698 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002699 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08002700 }
2701
2702 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2703 half3 expected = color;
2704 mat3 matrix;
2705 matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
2706 matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
2707 matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
2708
2709 // degamma before applying the matrix
2710 if (mColorManagementUsed) {
2711 ColorTransformHelper::DegammaColor(expected);
2712 }
2713
2714 ColorTransformHelper::applyMatrix(expected, matrix);
2715
2716 if (mColorManagementUsed) {
2717 ColorTransformHelper::GammaColor(expected);
2718 }
2719
2720 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2721 uint8_t(expected.b * 255), 255};
2722
2723 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2724 // channel) should be less than one
2725 const uint8_t tolerance = 1;
2726
2727 Transaction()
2728 .setColor(colorLayer, color)
2729 .setColorTransform(parentLayer, matrix, vec3())
2730 .apply();
2731 {
2732 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002733 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08002734 }
2735}
2736
Alec Mouri80863a62019-01-17 15:19:35 -08002737TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnChildAndParent) {
chaviwf66724d2018-11-28 16:35:21 -08002738 sp<SurfaceControl> parentLayer;
2739 sp<SurfaceControl> colorLayer;
2740 ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
2741 0 /* buffer height */,
2742 ISurfaceComposerClient::eFXSurfaceContainer));
2743 ASSERT_NO_FATAL_FAILURE(
2744 colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
2745 ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
2746
2747 Transaction()
2748 .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
2749 .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
2750 .setLayer(parentLayer, mLayerZBase + 1)
2751 .apply();
2752 {
2753 SCOPED_TRACE("default color");
Alec Mouri80863a62019-01-17 15:19:35 -08002754 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
chaviwf66724d2018-11-28 16:35:21 -08002755 }
2756
2757 const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
2758 half3 expected = color;
2759 mat3 matrixChild;
2760 matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
2761 matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
2762 matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
2763 mat3 matrixParent;
2764 matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
2765 matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
2766 matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
2767
2768 // degamma before applying the matrix
2769 if (mColorManagementUsed) {
2770 ColorTransformHelper::DegammaColor(expected);
2771 }
2772
2773 ColorTransformHelper::applyMatrix(expected, matrixChild);
2774 ColorTransformHelper::applyMatrix(expected, matrixParent);
2775
2776 if (mColorManagementUsed) {
2777 ColorTransformHelper::GammaColor(expected);
2778 }
2779
2780 const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
2781 uint8_t(expected.b * 255), 255};
2782
2783 // this is handwavy, but the precison loss scaled by 255 (8-bit per
2784 // channel) should be less than one
2785 const uint8_t tolerance = 1;
2786
2787 Transaction()
2788 .setColor(colorLayer, color)
2789 .setColorTransform(parentLayer, matrixParent, vec3())
2790 .setColorTransform(colorLayer, matrixChild, vec3())
2791 .apply();
2792 {
2793 SCOPED_TRACE("new color");
Alec Mouri80863a62019-01-17 15:19:35 -08002794 getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
chaviwf66724d2018-11-28 16:35:21 -08002795 }
2796}
2797
Marissa Wallfda30bb2018-10-12 11:34:28 -07002798class LayerCallbackTest : public LayerTransactionTest {
Marissa Wall861616d2018-10-22 12:52:23 -07002799public:
Marissa Wallfda30bb2018-10-12 11:34:28 -07002800 virtual sp<SurfaceControl> createBufferStateLayer() {
Marissa Wall861616d2018-10-22 12:52:23 -07002801 return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002802 }
2803
Marissa Wall713b63f2018-10-17 15:42:43 -07002804 static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
Valerie Hauaa194562019-02-05 16:21:38 -08002805 const sp<SurfaceControl>& layer = nullptr, bool setBuffer = true,
2806 bool setBackgroundColor = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002807 if (layer) {
Marissa Wall713b63f2018-10-17 15:42:43 -07002808 sp<GraphicBuffer> buffer;
2809 sp<Fence> fence;
Valerie Hauaa194562019-02-05 16:21:38 -08002810 if (setBuffer) {
2811 int err = getBuffer(&buffer, &fence);
2812 if (err != NO_ERROR) {
2813 return err;
2814 }
2815
2816 transaction.setBuffer(layer, buffer);
2817 transaction.setAcquireFence(layer, fence);
Marissa Wall713b63f2018-10-17 15:42:43 -07002818 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002819
Valerie Hauaa194562019-02-05 16:21:38 -08002820 if (setBackgroundColor) {
2821 transaction.setBackgroundColor(layer, /*color*/ half3(1.0f, 0, 0), /*alpha*/ 1.0f,
2822 ui::Dataspace::UNKNOWN);
2823 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002824 }
2825
2826 transaction.addTransactionCompletedCallback(callbackHelper->function,
2827 callbackHelper->getContext());
Marissa Wall713b63f2018-10-17 15:42:43 -07002828 return NO_ERROR;
Marissa Wallfda30bb2018-10-12 11:34:28 -07002829 }
2830
Marissa Wall861616d2018-10-22 12:52:23 -07002831 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
2832 bool finalState = false) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08002833 CallbackData callbackData;
2834 ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
2835 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
Marissa Wallfda30bb2018-10-12 11:34:28 -07002836
2837 if (finalState) {
2838 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2839 }
2840 }
2841
Marissa Wall861616d2018-10-22 12:52:23 -07002842 static void waitForCallbacks(CallbackHelper& helper,
2843 const std::vector<ExpectedResult>& expectedResults,
2844 bool finalState = false) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002845 for (const auto& expectedResult : expectedResults) {
2846 waitForCallback(helper, expectedResult);
2847 }
2848 if (finalState) {
2849 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
2850 }
2851 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002852};
2853
Valerie Hauaa194562019-02-05 16:21:38 -08002854TEST_F(LayerCallbackTest, BufferColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002855 sp<SurfaceControl> layer;
2856 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2857
2858 Transaction transaction;
2859 CallbackHelper callback;
Valerie Hauaa194562019-02-05 16:21:38 -08002860 int err = fillTransaction(transaction, &callback, layer, true, true);
Marissa Wall713b63f2018-10-17 15:42:43 -07002861 if (err) {
2862 GTEST_SUCCEED() << "test not supported";
2863 return;
2864 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002865
2866 transaction.apply();
2867
2868 ExpectedResult expected;
2869 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2870 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2871}
2872
Valerie Hauaa194562019-02-05 16:21:38 -08002873TEST_F(LayerCallbackTest, NoBufferNoColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002874 sp<SurfaceControl> layer;
2875 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2876
2877 Transaction transaction;
2878 CallbackHelper callback;
Valerie Hauaa194562019-02-05 16:21:38 -08002879 int err = fillTransaction(transaction, &callback, layer, false, false);
Marissa Wall713b63f2018-10-17 15:42:43 -07002880 if (err) {
2881 GTEST_SUCCEED() << "test not supported";
2882 return;
2883 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002884
Marissa Wall861616d2018-10-22 12:52:23 -07002885 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002886
2887 ExpectedResult expected;
Marissa Wall713b63f2018-10-17 15:42:43 -07002888 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
2889 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07002890 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2891}
2892
Valerie Hauaa194562019-02-05 16:21:38 -08002893TEST_F(LayerCallbackTest, BufferNoColor) {
2894 sp<SurfaceControl> layer;
2895 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2896
2897 Transaction transaction;
2898 CallbackHelper callback;
2899 int err = fillTransaction(transaction, &callback, layer, true, false);
2900 if (err) {
2901 GTEST_SUCCEED() << "test not supported";
2902 return;
2903 }
2904
2905 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
2906
2907 ExpectedResult expected;
2908 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2909 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2910}
2911
2912TEST_F(LayerCallbackTest, NoBufferColor) {
2913 sp<SurfaceControl> layer;
2914 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2915
2916 Transaction transaction;
2917 CallbackHelper callback;
2918 int err = fillTransaction(transaction, &callback, layer, false, true);
2919 if (err) {
2920 GTEST_SUCCEED() << "test not supported";
2921 return;
2922 }
2923
2924 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
2925
2926 ExpectedResult expected;
2927 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
2928 ExpectedResult::Buffer::NOT_ACQUIRED);
2929 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2930}
2931
Marissa Wallfda30bb2018-10-12 11:34:28 -07002932TEST_F(LayerCallbackTest, NoStateChange) {
2933 Transaction transaction;
2934 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002935 int err = fillTransaction(transaction, &callback);
2936 if (err) {
2937 GTEST_SUCCEED() << "test not supported";
2938 return;
2939 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002940
2941 transaction.apply();
2942
2943 ExpectedResult expected;
2944 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2945}
2946
2947TEST_F(LayerCallbackTest, OffScreen) {
2948 sp<SurfaceControl> layer;
2949 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
2950
2951 Transaction transaction;
2952 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07002953 int err = fillTransaction(transaction, &callback, layer);
2954 if (err) {
2955 GTEST_SUCCEED() << "test not supported";
2956 return;
2957 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002958
Marissa Wall861616d2018-10-22 12:52:23 -07002959 transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002960
2961 ExpectedResult expected;
2962 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
2963 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
2964}
2965
Valerie Hauaa194562019-02-05 16:21:38 -08002966TEST_F(LayerCallbackTest, MergeBufferNoColor) {
Marissa Wallfda30bb2018-10-12 11:34:28 -07002967 sp<SurfaceControl> layer1, layer2;
2968 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2969 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2970
2971 Transaction transaction1, transaction2;
2972 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07002973 int err = fillTransaction(transaction1, &callback1, layer1);
2974 if (err) {
2975 GTEST_SUCCEED() << "test not supported";
2976 return;
2977 }
2978 err = fillTransaction(transaction2, &callback2, layer2);
2979 if (err) {
2980 GTEST_SUCCEED() << "test not supported";
2981 return;
2982 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07002983
Marissa Wall861616d2018-10-22 12:52:23 -07002984 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
2985 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07002986
2987 ExpectedResult expected;
2988 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
2989 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
2990 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
2991}
2992
Valerie Hauaa194562019-02-05 16:21:38 -08002993TEST_F(LayerCallbackTest, MergeNoBufferColor) {
2994 sp<SurfaceControl> layer1, layer2;
2995 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
2996 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
2997
2998 Transaction transaction1, transaction2;
2999 CallbackHelper callback1, callback2;
3000 int err = fillTransaction(transaction1, &callback1, layer1, false, true);
3001 if (err) {
3002 GTEST_SUCCEED() << "test not supported";
3003 return;
3004 }
3005 err = fillTransaction(transaction2, &callback2, layer2, false, true);
3006 if (err) {
3007 GTEST_SUCCEED() << "test not supported";
3008 return;
3009 }
3010
3011 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3012 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3013
3014 ExpectedResult expected;
3015 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
3016 ExpectedResult::Buffer::NOT_ACQUIRED);
3017 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3018 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3019}
3020
3021TEST_F(LayerCallbackTest, MergeOneBufferOneColor) {
3022 sp<SurfaceControl> layer1, layer2;
3023 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3024 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3025
3026 Transaction transaction1, transaction2;
3027 CallbackHelper callback1, callback2;
3028 int err = fillTransaction(transaction1, &callback1, layer1);
3029 if (err) {
3030 GTEST_SUCCEED() << "test not supported";
3031 return;
3032 }
3033 err = fillTransaction(transaction2, &callback2, layer2, false, true);
3034 if (err) {
3035 GTEST_SUCCEED() << "test not supported";
3036 return;
3037 }
3038
3039 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3040 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
3041
3042 ExpectedResult expected;
3043 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer1);
3044 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer2,
3045 ExpectedResult::Buffer::NOT_ACQUIRED);
3046 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3047 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3048}
Marissa Wallfda30bb2018-10-12 11:34:28 -07003049TEST_F(LayerCallbackTest, Merge_SameCallback) {
3050 sp<SurfaceControl> layer1, layer2;
3051 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3052 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3053
3054 Transaction transaction1, transaction2;
3055 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003056 int err = fillTransaction(transaction1, &callback, layer1);
3057 if (err) {
3058 GTEST_SUCCEED() << "test not supported";
3059 return;
3060 }
3061 err = fillTransaction(transaction2, &callback, layer2);
3062 if (err) {
3063 GTEST_SUCCEED() << "test not supported";
3064 return;
3065 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003066
3067 transaction2.merge(std::move(transaction1)).apply();
3068
3069 ExpectedResult expected;
3070 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3071 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3072 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3073}
3074
3075TEST_F(LayerCallbackTest, Merge_SameLayer) {
3076 sp<SurfaceControl> layer;
3077 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3078
3079 Transaction transaction1, transaction2;
3080 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003081 int err = fillTransaction(transaction1, &callback1, layer);
3082 if (err) {
3083 GTEST_SUCCEED() << "test not supported";
3084 return;
3085 }
3086 err = fillTransaction(transaction2, &callback2, layer);
3087 if (err) {
3088 GTEST_SUCCEED() << "test not supported";
3089 return;
3090 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003091
3092 transaction2.merge(std::move(transaction1)).apply();
3093
3094 ExpectedResult expected;
3095 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3096 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3097 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3098}
3099
Marissa Wallfda30bb2018-10-12 11:34:28 -07003100TEST_F(LayerCallbackTest, Merge_DifferentClients) {
3101 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3102 client2(new SurfaceComposerClient);
3103
3104 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3105 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3106
3107 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003108 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003109 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003110 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003111 ISurfaceComposerClient::eFXSurfaceBufferState));
3112
3113 Transaction transaction1, transaction2;
3114 CallbackHelper callback1, callback2;
Marissa Wall713b63f2018-10-17 15:42:43 -07003115 int err = fillTransaction(transaction1, &callback1, layer1);
3116 if (err) {
3117 GTEST_SUCCEED() << "test not supported";
3118 return;
3119 }
3120 err = fillTransaction(transaction2, &callback2, layer2);
3121 if (err) {
3122 GTEST_SUCCEED() << "test not supported";
3123 return;
3124 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003125
Marissa Wall861616d2018-10-22 12:52:23 -07003126 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3127 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003128
3129 ExpectedResult expected;
3130 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3131 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3132 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3133}
3134
3135TEST_F(LayerCallbackTest, MultipleTransactions) {
3136 sp<SurfaceControl> layer;
3137 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3138
3139 Transaction transaction;
3140 CallbackHelper callback;
3141 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003142 int err = fillTransaction(transaction, &callback, layer);
3143 if (err) {
3144 GTEST_SUCCEED() << "test not supported";
3145 return;
3146 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003147
3148 transaction.apply();
3149
3150 ExpectedResult expected;
3151 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall713b63f2018-10-17 15:42:43 -07003152 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003153 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3154 : ExpectedResult::PreviousBuffer::RELEASED);
3155 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3156 }
3157 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3158}
3159
3160TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
3161 sp<SurfaceControl> layer;
3162 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3163
3164 Transaction transaction;
3165 CallbackHelper callback;
3166 for (size_t i = 0; i < 10; i++) {
3167 ExpectedResult expected;
3168
3169 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003170 int err = fillTransaction(transaction, &callback, layer);
3171 if (err) {
3172 GTEST_SUCCEED() << "test not supported";
3173 return;
3174 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003175 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3176 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003177 int err = fillTransaction(transaction, &callback);
3178 if (err) {
3179 GTEST_SUCCEED() << "test not supported";
3180 return;
3181 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003182 }
3183
3184 transaction.apply();
3185
3186 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
3187 }
3188 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3189}
3190
3191TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
3192 sp<SurfaceControl> layer;
3193 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3194
3195 Transaction transaction;
3196 CallbackHelper callback;
3197 for (size_t i = 0; i < 10; i++) {
3198 if (i == 0) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003199 int err = fillTransaction(transaction, &callback, layer);
3200 if (err) {
3201 GTEST_SUCCEED() << "test not supported";
3202 return;
3203 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003204 } else {
Marissa Wall713b63f2018-10-17 15:42:43 -07003205 int err = fillTransaction(transaction, &callback);
3206 if (err) {
3207 GTEST_SUCCEED() << "test not supported";
3208 return;
3209 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003210 }
3211
Marissa Wall861616d2018-10-22 12:52:23 -07003212 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003213
3214 ExpectedResult expected;
3215 expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
3216 : ExpectedResult::Transaction::NOT_PRESENTED,
Marissa Wall713b63f2018-10-17 15:42:43 -07003217 layer,
3218 (i == 0) ? ExpectedResult::Buffer::ACQUIRED
3219 : ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003220 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
3221 }
3222 ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
3223}
3224
3225TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
3226 sp<SurfaceControl> layer1, layer2;
3227 ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
3228 ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
3229
3230 Transaction transaction1, transaction2;
3231 CallbackHelper callback1, callback2;
3232 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003233 int err = fillTransaction(transaction1, &callback1, layer1);
3234 if (err) {
3235 GTEST_SUCCEED() << "test not supported";
3236 return;
3237 }
3238 err = fillTransaction(transaction2, &callback2, layer2);
3239 if (err) {
3240 GTEST_SUCCEED() << "test not supported";
3241 return;
3242 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003243
Marissa Wall861616d2018-10-22 12:52:23 -07003244 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3245 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003246
3247 ExpectedResult expected;
3248 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003249 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003250 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3251 : ExpectedResult::PreviousBuffer::RELEASED);
3252 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3253 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3254 }
3255 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3256 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3257}
3258
3259TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
3260 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3261 client2(new SurfaceComposerClient);
3262 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3263 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3264
3265 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003266 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003267 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003268 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003269 ISurfaceComposerClient::eFXSurfaceBufferState));
3270
3271 Transaction transaction1, transaction2;
3272 CallbackHelper callback1, callback2;
3273 for (size_t i = 0; i < 10; i++) {
Marissa Wall713b63f2018-10-17 15:42:43 -07003274 int err = fillTransaction(transaction1, &callback1, layer1);
3275 if (err) {
3276 GTEST_SUCCEED() << "test not supported";
3277 return;
3278 }
3279 err = fillTransaction(transaction2, &callback2, layer2);
3280 if (err) {
3281 GTEST_SUCCEED() << "test not supported";
3282 return;
3283 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003284
Marissa Wall861616d2018-10-22 12:52:23 -07003285 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3286 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003287
3288 ExpectedResult expected;
3289 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
Marissa Wall713b63f2018-10-17 15:42:43 -07003290 ExpectedResult::Buffer::ACQUIRED,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003291 (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
3292 : ExpectedResult::PreviousBuffer::RELEASED);
3293 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
3294 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
3295 }
3296 ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
3297 ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
3298}
3299
3300TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
3301 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3302 client2(new SurfaceComposerClient);
3303 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3304 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3305
3306 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003307 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003308 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003309 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003310 ISurfaceComposerClient::eFXSurfaceBufferState));
3311
3312 Transaction transaction1, transaction2;
3313 CallbackHelper callback1, callback2;
3314
3315 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003316 int err = fillTransaction(transaction1, &callback1, layer1);
3317 if (err) {
3318 GTEST_SUCCEED() << "test not supported";
3319 return;
3320 }
3321 err = fillTransaction(transaction2, &callback2, layer2);
3322 if (err) {
3323 GTEST_SUCCEED() << "test not supported";
3324 return;
3325 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003326
Marissa Wall861616d2018-10-22 12:52:23 -07003327 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3328 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003329
3330 ExpectedResult expected;
3331 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3332 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3333 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3334 expected.reset();
3335
3336 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003337 err = fillTransaction(transaction1, &callback1);
3338 if (err) {
3339 GTEST_SUCCEED() << "test not supported";
3340 return;
3341 }
3342 err = fillTransaction(transaction2, &callback2);
3343 if (err) {
3344 GTEST_SUCCEED() << "test not supported";
3345 return;
3346 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003347
3348 transaction2.merge(std::move(transaction1)).apply();
3349
3350 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3351 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3352}
3353
3354TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
3355 sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
3356 client2(new SurfaceComposerClient);
3357
3358 ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
3359 ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
3360
3361 sp<SurfaceControl> layer1, layer2;
Marissa Wall861616d2018-10-22 12:52:23 -07003362 ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003363 ISurfaceComposerClient::eFXSurfaceBufferState));
Marissa Wall861616d2018-10-22 12:52:23 -07003364 ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
Marissa Wallfda30bb2018-10-12 11:34:28 -07003365 ISurfaceComposerClient::eFXSurfaceBufferState));
3366
3367 Transaction transaction1, transaction2;
3368 CallbackHelper callback1, callback2;
3369
3370 // Normal call to set up test
Marissa Wall713b63f2018-10-17 15:42:43 -07003371 int err = fillTransaction(transaction1, &callback1, layer1);
3372 if (err) {
3373 GTEST_SUCCEED() << "test not supported";
3374 return;
3375 }
3376 err = fillTransaction(transaction2, &callback2, layer2);
3377 if (err) {
3378 GTEST_SUCCEED() << "test not supported";
3379 return;
3380 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003381
Marissa Wall861616d2018-10-22 12:52:23 -07003382 transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
3383 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003384
3385 ExpectedResult expected;
3386 expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
3387 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3388 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3389 expected.reset();
3390
3391 // Test
Marissa Wall713b63f2018-10-17 15:42:43 -07003392 err = fillTransaction(transaction1, &callback1);
3393 if (err) {
3394 GTEST_SUCCEED() << "test not supported";
3395 return;
3396 }
3397 err = fillTransaction(transaction2, &callback2);
3398 if (err) {
3399 GTEST_SUCCEED() << "test not supported";
3400 return;
3401 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003402
Marissa Wall861616d2018-10-22 12:52:23 -07003403 transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003404
Marissa Wall713b63f2018-10-17 15:42:43 -07003405 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
3406 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003407 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
3408 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
3409}
3410
3411TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
3412 sp<SurfaceControl> layer;
3413 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3414
3415 Transaction transaction;
3416 CallbackHelper callback;
3417 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003418 for (auto& expected : expectedResults) {
3419 expected.reset();
3420 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
Marissa Wall5a68a772018-12-22 17:43:42 -08003421 ExpectedResult::Buffer::ACQUIRED,
3422 ExpectedResult::PreviousBuffer::UNKNOWN);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003423
Marissa Wall713b63f2018-10-17 15:42:43 -07003424 int err = fillTransaction(transaction, &callback, layer);
3425 if (err) {
3426 GTEST_SUCCEED() << "test not supported";
3427 return;
3428 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003429
3430 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003431 }
3432 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3433}
3434
3435TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
3436 sp<SurfaceControl> layer;
3437 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3438
Marissa Wall713b63f2018-10-17 15:42:43 -07003439 // Normal call to set up test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003440 Transaction transaction;
3441 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003442 int err = fillTransaction(transaction, &callback, layer);
3443 if (err) {
3444 GTEST_SUCCEED() << "test not supported";
3445 return;
3446 }
3447
3448 transaction.apply();
3449
3450 ExpectedResult expected;
3451 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3452 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3453
3454 // Test
Marissa Wallfda30bb2018-10-12 11:34:28 -07003455 std::vector<ExpectedResult> expectedResults(50);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003456 for (auto& expected : expectedResults) {
3457 expected.reset();
3458
Marissa Wall713b63f2018-10-17 15:42:43 -07003459 err = fillTransaction(transaction, &callback);
3460 if (err) {
3461 GTEST_SUCCEED() << "test not supported";
3462 return;
Marissa Wallfda30bb2018-10-12 11:34:28 -07003463 }
3464
3465 transaction.apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003466 }
3467 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3468}
3469
3470TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
3471 sp<SurfaceControl> layer;
3472 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3473
3474 // Normal call to set up test
3475 Transaction transaction;
3476 CallbackHelper callback;
Marissa Wall713b63f2018-10-17 15:42:43 -07003477 int err = fillTransaction(transaction, &callback, layer);
3478 if (err) {
3479 GTEST_SUCCEED() << "test not supported";
3480 return;
3481 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003482
Marissa Wall861616d2018-10-22 12:52:23 -07003483 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003484
3485 ExpectedResult expectedResult;
3486 expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3487 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
3488
3489 // Test
3490 std::vector<ExpectedResult> expectedResults(50);
3491 for (auto& expected : expectedResults) {
3492 expected.reset();
Marissa Wall713b63f2018-10-17 15:42:43 -07003493 expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
3494 ExpectedResult::Buffer::NOT_ACQUIRED);
Marissa Wallfda30bb2018-10-12 11:34:28 -07003495
Marissa Wall713b63f2018-10-17 15:42:43 -07003496 err = fillTransaction(transaction, &callback);
3497 if (err) {
3498 GTEST_SUCCEED() << "test not supported";
3499 return;
3500 }
Marissa Wallfda30bb2018-10-12 11:34:28 -07003501
Marissa Wall861616d2018-10-22 12:52:23 -07003502 transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
Marissa Wallfda30bb2018-10-12 11:34:28 -07003503 }
3504 EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
3505}
3506
Marissa Wall17b4e452018-12-26 16:32:34 -08003507TEST_F(LayerCallbackTest, DesiredPresentTime) {
3508 sp<SurfaceControl> layer;
3509 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3510
3511 Transaction transaction;
3512 CallbackHelper callback;
3513 int err = fillTransaction(transaction, &callback, layer);
3514 if (err) {
3515 GTEST_SUCCEED() << "test not supported";
3516 return;
3517 }
3518
3519 // Try to present 100ms in the future
3520 nsecs_t time = systemTime() + (100 * 1e6);
3521
3522 transaction.setDesiredPresentTime(time);
3523 transaction.apply();
3524
3525 ExpectedResult expected;
3526 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3527 expected.addExpectedPresentTime(time);
3528 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3529}
3530
3531TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
3532 sp<SurfaceControl> layer;
3533 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3534
3535 Transaction transaction;
3536 CallbackHelper callback1;
3537 int err = fillTransaction(transaction, &callback1, layer);
3538 if (err) {
3539 GTEST_SUCCEED() << "test not supported";
3540 return;
3541 }
3542
3543 // Try to present 100ms in the future
3544 nsecs_t time = systemTime() + (100 * 1e6);
3545
3546 transaction.setDesiredPresentTime(time);
3547 transaction.apply();
3548
3549 ExpectedResult expected1;
3550 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3551 expected1.addExpectedPresentTime(time);
3552
3553 CallbackHelper callback2;
3554 err = fillTransaction(transaction, &callback2, layer);
3555 if (err) {
3556 GTEST_SUCCEED() << "test not supported";
3557 return;
3558 }
3559
3560 // Try to present 33ms after the first frame
3561 time += (33.3 * 1e6);
3562
3563 transaction.setDesiredPresentTime(time);
3564 transaction.apply();
3565
3566 ExpectedResult expected2;
3567 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3568 ExpectedResult::Buffer::ACQUIRED,
3569 ExpectedResult::PreviousBuffer::RELEASED);
3570 expected2.addExpectedPresentTime(time);
3571
3572 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3573 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3574}
3575
3576TEST_F(LayerCallbackTest, DesiredPresentTime_OutOfOrder) {
3577 sp<SurfaceControl> layer;
3578 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3579
3580 Transaction transaction;
3581 CallbackHelper callback1;
3582 int err = fillTransaction(transaction, &callback1, layer);
3583 if (err) {
3584 GTEST_SUCCEED() << "test not supported";
3585 return;
3586 }
3587
3588 // Try to present 100ms in the future
3589 nsecs_t time = systemTime() + (100 * 1e6);
3590
3591 transaction.setDesiredPresentTime(time);
3592 transaction.apply();
3593
3594 ExpectedResult expected1;
3595 expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3596 expected1.addExpectedPresentTime(time);
3597
3598 CallbackHelper callback2;
3599 err = fillTransaction(transaction, &callback2, layer);
3600 if (err) {
3601 GTEST_SUCCEED() << "test not supported";
3602 return;
3603 }
3604
3605 // Try to present 33ms before the previous frame
3606 time -= (33.3 * 1e6);
3607
3608 transaction.setDesiredPresentTime(time);
3609 transaction.apply();
3610
3611 ExpectedResult expected2;
3612 expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
3613 ExpectedResult::Buffer::ACQUIRED,
3614 ExpectedResult::PreviousBuffer::RELEASED);
3615
3616 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
3617 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
3618}
3619
3620TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
3621 sp<SurfaceControl> layer;
3622 ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
3623
3624 Transaction transaction;
3625 CallbackHelper callback;
3626 int err = fillTransaction(transaction, &callback, layer);
3627 if (err) {
3628 GTEST_SUCCEED() << "test not supported";
3629 return;
3630 }
3631
3632 // Try to present 100ms in the past
3633 nsecs_t time = systemTime() - (100 * 1e6);
3634
3635 transaction.setDesiredPresentTime(time);
3636 transaction.apply();
3637
3638 ExpectedResult expected;
3639 expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
3640 expected.addExpectedPresentTime(systemTime());
3641 EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
3642}
3643
Chavi Weingarten40482ff2017-11-30 01:51:40 +00003644class LayerUpdateTest : public LayerTransactionTest {
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003645protected:
3646 virtual void SetUp() {
chaviw0e3479f2018-09-10 16:49:30 -07003647 LayerTransactionTest::SetUp();
3648 ASSERT_EQ(NO_ERROR, mClient->initCheck());
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003649
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08003650 const auto display = SurfaceComposerClient::getInternalDisplayToken();
3651 ASSERT_FALSE(display == nullptr);
3652
Mathias Agopianc666cae2012-07-25 18:56:13 -07003653 DisplayInfo info;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08003654 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
Mathias Agopianc666cae2012-07-25 18:56:13 -07003655
3656 ssize_t displayWidth = info.w;
3657 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003658
3659 // Background surface
chaviw0e3479f2018-09-10 16:49:30 -07003660 mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
3661 displayHeight, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003662 ASSERT_TRUE(mBGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003663 ASSERT_TRUE(mBGSurfaceControl->isValid());
3664 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
3665
3666 // Foreground surface
chaviw0e3479f2018-09-10 16:49:30 -07003667 mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
3668
Peiyong Lin566a3b42018-01-09 18:22:43 -08003669 ASSERT_TRUE(mFGSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003670 ASSERT_TRUE(mFGSurfaceControl->isValid());
3671
3672 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3673
3674 // Synchronization surface
chaviw0e3479f2018-09-10 16:49:30 -07003675 mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08003676 ASSERT_TRUE(mSyncSurfaceControl != nullptr);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003677 ASSERT_TRUE(mSyncSurfaceControl->isValid());
3678
3679 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3680
Robert Carr4cdc58f2017-08-23 14:22:20 -07003681 asTransaction([&](Transaction& t) {
3682 t.setDisplayLayerStack(display, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003683
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003684 t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -07003685
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003686 t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
3687 .setPosition(mFGSurfaceControl, 64, 64)
3688 .show(mFGSurfaceControl);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003689
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003690 t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
3691 .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
3692 .show(mSyncSurfaceControl);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003693 });
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003694 }
3695
3696 virtual void TearDown() {
chaviw0e3479f2018-09-10 16:49:30 -07003697 LayerTransactionTest::TearDown();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003698 mBGSurfaceControl = 0;
3699 mFGSurfaceControl = 0;
3700 mSyncSurfaceControl = 0;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003701 }
3702
3703 void waitForPostedBuffers() {
3704 // Since the sync surface is in synchronous mode (i.e. double buffered)
3705 // posting three buffers to it should ensure that at least two
3706 // SurfaceFlinger::handlePageFlip calls have been made, which should
3707 // guaranteed that a buffer posted to another Surface has been retired.
3708 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3709 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3710 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3711 }
3712
Robert Carr4cdc58f2017-08-23 14:22:20 -07003713
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07003714 sp<SurfaceControl> mBGSurfaceControl;
3715 sp<SurfaceControl> mFGSurfaceControl;
3716
3717 // This surface is used to ensure that the buffers posted to
3718 // mFGSurfaceControl have been picked up by SurfaceFlinger.
3719 sp<SurfaceControl> mSyncSurfaceControl;
3720};
3721
Robert Carr7f619b22017-11-06 12:56:35 -08003722TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
Robert Carr7f619b22017-11-06 12:56:35 -08003723
chaviw0e3479f2018-09-10 16:49:30 -07003724 std::unique_ptr<ScreenCapture> sc;
3725
3726 sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
Robert Carr7f619b22017-11-06 12:56:35 -08003727 fillSurfaceRGBA8(relative, 10, 10, 10);
3728 waitForPostedBuffers();
3729
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003730 Transaction{}
3731 .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
Robert Carr7f619b22017-11-06 12:56:35 -08003732 .setPosition(relative, 64, 64)
3733 .apply();
3734
3735 {
3736 // The relative should be on top of the FG control.
3737 ScreenCapture::captureScreen(&sc);
3738 sc->checkPixel(64, 64, 10, 10, 10);
3739 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003740 Transaction{}.detachChildren(mFGSurfaceControl).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003741
3742 {
3743 // Nothing should change at this point.
3744 ScreenCapture::captureScreen(&sc);
3745 sc->checkPixel(64, 64, 10, 10, 10);
3746 }
3747
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003748 Transaction{}.hide(relative).apply();
Robert Carr7f619b22017-11-06 12:56:35 -08003749
3750 {
3751 // Ensure that the relative was actually hidden, rather than
3752 // being left in the detached but visible state.
3753 ScreenCapture::captureScreen(&sc);
3754 sc->expectFGColor(64, 64);
3755 }
3756}
3757
Robert Carr8d5227b2017-03-16 15:41:03 -07003758class GeometryLatchingTest : public LayerUpdateTest {
3759protected:
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003760 void EXPECT_INITIAL_STATE(const char* trace) {
Robert Carr8d5227b2017-03-16 15:41:03 -07003761 SCOPED_TRACE(trace);
3762 ScreenCapture::captureScreen(&sc);
3763 // We find the leading edge of the FG surface.
3764 sc->expectFGColor(127, 127);
3765 sc->expectBGColor(128, 128);
3766 }
Robert Carr7bf247e2017-05-18 14:02:49 -07003767
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003768 void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
Robert Carr7bf247e2017-05-18 14:02:49 -07003769
3770 void unlockFGBuffer() {
3771 sp<Surface> s = mFGSurfaceControl->getSurface();
3772 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
3773 waitForPostedBuffers();
3774 }
3775
Robert Carr8d5227b2017-03-16 15:41:03 -07003776 void completeFGResize() {
3777 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
3778 waitForPostedBuffers();
3779 }
3780 void restoreInitialState() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003781 asTransaction([&](Transaction& t) {
3782 t.setSize(mFGSurfaceControl, 64, 64);
3783 t.setPosition(mFGSurfaceControl, 64, 64);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003784 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
Robert Carr4cdc58f2017-08-23 14:22:20 -07003785 });
Robert Carr8d5227b2017-03-16 15:41:03 -07003786
3787 EXPECT_INITIAL_STATE("After restoring initial state");
3788 }
chaviw0e3479f2018-09-10 16:49:30 -07003789 std::unique_ptr<ScreenCapture> sc;
Robert Carr8d5227b2017-03-16 15:41:03 -07003790};
3791
Robert Carr8d5227b2017-03-16 15:41:03 -07003792class CropLatchingTest : public GeometryLatchingTest {
3793protected:
3794 void EXPECT_CROPPED_STATE(const char* trace) {
3795 SCOPED_TRACE(trace);
3796 ScreenCapture::captureScreen(&sc);
3797 // The edge should be moved back one pixel by our crop.
3798 sc->expectFGColor(126, 126);
3799 sc->expectBGColor(127, 127);
3800 sc->expectBGColor(128, 128);
3801 }
chaviw59f5c562017-06-28 16:39:06 -07003802
3803 void EXPECT_RESIZE_STATE(const char* trace) {
3804 SCOPED_TRACE(trace);
3805 ScreenCapture::captureScreen(&sc);
3806 // The FG is now resized too 128,128 at 64,64
3807 sc->expectFGColor(64, 64);
3808 sc->expectFGColor(191, 191);
3809 sc->expectBGColor(192, 192);
3810 }
Robert Carr8d5227b2017-03-16 15:41:03 -07003811};
3812
Pablo Ceballos05289c22016-04-14 15:49:55 -07003813TEST_F(LayerUpdateTest, DeferredTransactionTest) {
chaviw0e3479f2018-09-10 16:49:30 -07003814 std::unique_ptr<ScreenCapture> sc;
Pablo Ceballos05289c22016-04-14 15:49:55 -07003815 {
3816 SCOPED_TRACE("before anything");
3817 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003818 sc->expectBGColor(32, 32);
3819 sc->expectFGColor(96, 96);
3820 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003821 }
3822
3823 // set up two deferred transactions on different frames
Robert Carr4cdc58f2017-08-23 14:22:20 -07003824 asTransaction([&](Transaction& t) {
3825 t.setAlpha(mFGSurfaceControl, 0.75);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003826 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3827 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07003828 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003829
Robert Carr4cdc58f2017-08-23 14:22:20 -07003830 asTransaction([&](Transaction& t) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003831 t.setPosition(mFGSurfaceControl, 128, 128);
Marissa Wallf58c14b2018-07-24 10:50:43 -07003832 t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
3833 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
Robert Carr4cdc58f2017-08-23 14:22:20 -07003834 });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003835
3836 {
3837 SCOPED_TRACE("before any trigger");
3838 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003839 sc->expectBGColor(32, 32);
3840 sc->expectFGColor(96, 96);
3841 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003842 }
3843
3844 // should trigger the first deferred transaction, but not the second one
3845 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3846 {
3847 SCOPED_TRACE("after first trigger");
3848 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003849 sc->expectBGColor(32, 32);
3850 sc->checkPixel(96, 96, 162, 63, 96);
3851 sc->expectBGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003852 }
3853
3854 // should show up immediately since it's not deferred
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003855 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
Pablo Ceballos05289c22016-04-14 15:49:55 -07003856
3857 // trigger the second deferred transaction
3858 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
3859 {
3860 SCOPED_TRACE("after second trigger");
3861 ScreenCapture::captureScreen(&sc);
Robert Carr2b91c822017-02-21 19:41:24 -08003862 sc->expectBGColor(32, 32);
3863 sc->expectBGColor(96, 96);
3864 sc->expectFGColor(160, 160);
Pablo Ceballos05289c22016-04-14 15:49:55 -07003865 }
3866}
3867
Robert Carre392b552017-09-19 12:16:05 -07003868TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
chaviw0e3479f2018-09-10 16:49:30 -07003869 std::unique_ptr<ScreenCapture> sc;
Robert Carre392b552017-09-19 12:16:05 -07003870
3871 sp<SurfaceControl> childNoBuffer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08003872 createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
3873 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
3874 sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
3875 PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
Robert Carre392b552017-09-19 12:16:05 -07003876 fillSurfaceRGBA8(childBuffer, 200, 200, 200);
Vishnu Nair60356342018-11-13 13:00:45 -08003877 SurfaceComposerClient::Transaction{}
3878 .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
3879 .show(childNoBuffer)
3880 .show(childBuffer)
3881 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003882 {
3883 ScreenCapture::captureScreen(&sc);
3884 sc->expectChildColor(73, 73);
3885 sc->expectFGColor(74, 74);
3886 }
Vishnu Nair60356342018-11-13 13:00:45 -08003887 SurfaceComposerClient::Transaction{}
3888 .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
3889 .apply(true);
Robert Carre392b552017-09-19 12:16:05 -07003890 {
3891 ScreenCapture::captureScreen(&sc);
3892 sc->expectChildColor(73, 73);
3893 sc->expectChildColor(74, 74);
3894 }
3895}
3896
Robert Carr2c5f6d22017-09-26 12:30:35 -07003897TEST_F(LayerUpdateTest, MergingTransactions) {
chaviw0e3479f2018-09-10 16:49:30 -07003898 std::unique_ptr<ScreenCapture> sc;
Robert Carr2c5f6d22017-09-26 12:30:35 -07003899 {
3900 SCOPED_TRACE("before move");
3901 ScreenCapture::captureScreen(&sc);
3902 sc->expectBGColor(0, 12);
3903 sc->expectFGColor(75, 75);
3904 sc->expectBGColor(145, 145);
3905 }
3906
3907 Transaction t1, t2;
3908 t1.setPosition(mFGSurfaceControl, 128, 128);
3909 t2.setPosition(mFGSurfaceControl, 0, 0);
3910 // We expect that the position update from t2 now
3911 // overwrites the position update from t1.
3912 t1.merge(std::move(t2));
3913 t1.apply();
3914
3915 {
3916 ScreenCapture::captureScreen(&sc);
3917 sc->expectFGColor(1, 1);
3918 }
3919}
3920
Vishnu Nair996bc422019-07-16 14:15:33 -07003921TEST_F(LayerUpdateTest, MergingTransactionFlags) {
3922 Transaction().hide(mFGSurfaceControl).apply();
3923 std::unique_ptr<ScreenCapture> sc;
3924 {
3925 SCOPED_TRACE("before merge");
3926 ScreenCapture::captureScreen(&sc);
3927 sc->expectBGColor(0, 12);
3928 sc->expectBGColor(75, 75);
3929 sc->expectBGColor(145, 145);
3930 }
3931
3932 Transaction t1, t2;
3933 t1.show(mFGSurfaceControl);
3934 t2.setFlags(mFGSurfaceControl, 0 /* flags */, layer_state_t::eLayerSecure /* mask */);
3935 t1.merge(std::move(t2));
3936 t1.apply();
3937
3938 {
3939 SCOPED_TRACE("after merge");
3940 ScreenCapture::captureScreen(&sc);
3941 sc->expectFGColor(75, 75);
3942 }
3943}
3944
Robert Carr1f0a16a2016-10-24 16:27:39 -07003945class ChildLayerTest : public LayerUpdateTest {
3946protected:
3947 void SetUp() override {
3948 LayerUpdateTest::SetUp();
Vishnu Nairc652ff82019-03-15 12:48:54 -07003949 mChild = createSurface(mClient, "Child surface", 10, 15, PIXEL_FORMAT_RGBA_8888, 0,
Vishnu Nair88a11f22018-11-28 18:30:57 -08003950 mFGSurfaceControl.get());
Robert Carr1f0a16a2016-10-24 16:27:39 -07003951 fillSurfaceRGBA8(mChild, 200, 200, 200);
3952
3953 {
3954 SCOPED_TRACE("before anything");
chaviw0e3479f2018-09-10 16:49:30 -07003955 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003956 mCapture->expectChildColor(64, 64);
3957 }
3958 }
3959 void TearDown() override {
3960 LayerUpdateTest::TearDown();
3961 mChild = 0;
3962 }
3963
3964 sp<SurfaceControl> mChild;
chaviw0e3479f2018-09-10 16:49:30 -07003965 std::unique_ptr<ScreenCapture> mCapture;
Robert Carr1f0a16a2016-10-24 16:27:39 -07003966};
3967
3968TEST_F(ChildLayerTest, ChildLayerPositioning) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003969 asTransaction([&](Transaction& t) {
3970 t.show(mChild);
3971 t.setPosition(mChild, 10, 10);
3972 t.setPosition(mFGSurfaceControl, 64, 64);
3973 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003974
3975 {
chaviw0e3479f2018-09-10 16:49:30 -07003976 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003977 // Top left of foreground must now be visible
3978 mCapture->expectFGColor(64, 64);
3979 // But 10 pixels in we should see the child surface
3980 mCapture->expectChildColor(74, 74);
3981 // And 10 more pixels we should be back to the foreground surface
3982 mCapture->expectFGColor(84, 84);
3983 }
3984
Chia-I Wu1078bbb2017-10-20 11:29:02 -07003985 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07003986
3987 {
chaviw0e3479f2018-09-10 16:49:30 -07003988 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07003989 // Top left of foreground should now be at 0, 0
3990 mCapture->expectFGColor(0, 0);
3991 // But 10 pixels in we should see the child surface
3992 mCapture->expectChildColor(10, 10);
3993 // And 10 more pixels we should be back to the foreground surface
3994 mCapture->expectFGColor(20, 20);
3995 }
3996}
3997
Robert Carr41b08b52017-06-01 16:11:34 -07003998TEST_F(ChildLayerTest, ChildLayerCropping) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07003999 asTransaction([&](Transaction& t) {
4000 t.show(mChild);
4001 t.setPosition(mChild, 0, 0);
4002 t.setPosition(mFGSurfaceControl, 0, 0);
Marissa Wallf58c14b2018-07-24 10:50:43 -07004003 t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
Robert Carr4cdc58f2017-08-23 14:22:20 -07004004 });
Robert Carr41b08b52017-06-01 16:11:34 -07004005
4006 {
chaviw0e3479f2018-09-10 16:49:30 -07004007 mCapture = screenshot();
Robert Carr41b08b52017-06-01 16:11:34 -07004008 mCapture->expectChildColor(0, 0);
4009 mCapture->expectChildColor(4, 4);
4010 mCapture->expectBGColor(5, 5);
4011 }
4012}
4013
Robert Carr1f0a16a2016-10-24 16:27:39 -07004014TEST_F(ChildLayerTest, ChildLayerConstraints) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004015 asTransaction([&](Transaction& t) {
4016 t.show(mChild);
4017 t.setPosition(mFGSurfaceControl, 0, 0);
4018 t.setPosition(mChild, 63, 63);
4019 });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004020
4021 {
chaviw0e3479f2018-09-10 16:49:30 -07004022 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004023 mCapture->expectFGColor(0, 0);
4024 // Last pixel in foreground should now be the child.
4025 mCapture->expectChildColor(63, 63);
4026 // But the child should be constrained and the next pixel
4027 // must be the background
4028 mCapture->expectBGColor(64, 64);
4029 }
4030}
4031
4032TEST_F(ChildLayerTest, ChildLayerScaling) {
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004033 asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004034
4035 // Find the boundary between the parent and child
4036 {
chaviw0e3479f2018-09-10 16:49:30 -07004037 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004038 mCapture->expectChildColor(9, 9);
4039 mCapture->expectFGColor(10, 10);
4040 }
4041
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004042 asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
Robert Carr1f0a16a2016-10-24 16:27:39 -07004043
4044 // The boundary should be twice as far from the origin now.
4045 // The pixels from the last test should all be child now
4046 {
chaviw0e3479f2018-09-10 16:49:30 -07004047 mCapture = screenshot();
Robert Carr1f0a16a2016-10-24 16:27:39 -07004048 mCapture->expectChildColor(9, 9);
4049 mCapture->expectChildColor(10, 10);
4050 mCapture->expectChildColor(19, 19);
4051 mCapture->expectFGColor(20, 20);
4052 }
4053}
Robert Carr9524cb32017-02-13 11:32:32 -08004054
Vishnu Nairc652ff82019-03-15 12:48:54 -07004055// A child with a scale transform should be cropped by its parent bounds.
4056TEST_F(ChildLayerTest, ChildLayerScalingCroppedByParent) {
4057 asTransaction([&](Transaction& t) {
4058 t.setPosition(mFGSurfaceControl, 0, 0);
4059 t.setPosition(mChild, 0, 0);
4060 });
4061
4062 // Find the boundary between the parent and child.
4063 {
4064 mCapture = screenshot();
4065 mCapture->expectChildColor(0, 0);
4066 mCapture->expectChildColor(9, 9);
4067 mCapture->expectFGColor(10, 10);
4068 }
4069
4070 asTransaction([&](Transaction& t) { t.setMatrix(mChild, 10.0, 0, 0, 10.0); });
4071
4072 // The child should fill its parent bounds and be cropped by it.
4073 {
4074 mCapture = screenshot();
4075 mCapture->expectChildColor(0, 0);
4076 mCapture->expectChildColor(63, 63);
4077 mCapture->expectBGColor(64, 64);
4078 }
4079}
4080
Robert Carr6452f122017-03-21 10:41:29 -07004081TEST_F(ChildLayerTest, ChildLayerAlpha) {
4082 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
4083 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
4084 fillSurfaceRGBA8(mChild, 0, 254, 0);
4085 waitForPostedBuffers();
4086
Robert Carr4cdc58f2017-08-23 14:22:20 -07004087 asTransaction([&](Transaction& t) {
4088 t.show(mChild);
4089 t.setPosition(mChild, 0, 0);
4090 t.setPosition(mFGSurfaceControl, 0, 0);
4091 });
Robert Carr6452f122017-03-21 10:41:29 -07004092
4093 {
chaviw0e3479f2018-09-10 16:49:30 -07004094 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004095 // Unblended child color
4096 mCapture->checkPixel(0, 0, 0, 254, 0);
4097 }
4098
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004099 asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004100
4101 {
chaviw0e3479f2018-09-10 16:49:30 -07004102 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004103 // Child and BG blended.
4104 mCapture->checkPixel(0, 0, 127, 127, 0);
4105 }
4106
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004107 asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
Robert Carr6452f122017-03-21 10:41:29 -07004108
4109 {
chaviw0e3479f2018-09-10 16:49:30 -07004110 mCapture = screenshot();
Robert Carr6452f122017-03-21 10:41:29 -07004111 // Child and BG blended.
4112 mCapture->checkPixel(0, 0, 95, 64, 95);
4113 }
4114}
4115
Robert Carr9524cb32017-02-13 11:32:32 -08004116TEST_F(ChildLayerTest, ReparentChildren) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004117 asTransaction([&](Transaction& t) {
4118 t.show(mChild);
4119 t.setPosition(mChild, 10, 10);
4120 t.setPosition(mFGSurfaceControl, 64, 64);
4121 });
Robert Carr9524cb32017-02-13 11:32:32 -08004122
4123 {
chaviw0e3479f2018-09-10 16:49:30 -07004124 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004125 // Top left of foreground must now be visible
4126 mCapture->expectFGColor(64, 64);
4127 // But 10 pixels in we should see the child surface
4128 mCapture->expectChildColor(74, 74);
4129 // And 10 more pixels we should be back to the foreground surface
4130 mCapture->expectFGColor(84, 84);
4131 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004132
4133 asTransaction([&](Transaction& t) {
4134 t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
4135 });
4136
Robert Carr9524cb32017-02-13 11:32:32 -08004137 {
chaviw0e3479f2018-09-10 16:49:30 -07004138 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004139 mCapture->expectFGColor(64, 64);
4140 // In reparenting we should have exposed the entire foreground surface.
4141 mCapture->expectFGColor(74, 74);
4142 // And the child layer should now begin at 10, 10 (since the BG
4143 // layer is at (0, 0)).
4144 mCapture->expectBGColor(9, 9);
4145 mCapture->expectChildColor(10, 10);
4146 }
4147}
4148
Robert Carr2e102c92018-10-23 12:11:15 -07004149TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
4150 sp<SurfaceControl> mGrandChild =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004151 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
Robert Carr2e102c92018-10-23 12:11:15 -07004152 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
4153
4154 {
4155 SCOPED_TRACE("Grandchild visible");
4156 ScreenCapture::captureScreen(&mCapture);
4157 mCapture->checkPixel(64, 64, 111, 111, 111);
4158 }
4159
Robert Carr87246532019-02-04 15:20:26 -08004160 mChild.clear();
Robert Carr2e102c92018-10-23 12:11:15 -07004161
4162 {
4163 SCOPED_TRACE("After destroying child");
4164 ScreenCapture::captureScreen(&mCapture);
4165 mCapture->expectFGColor(64, 64);
4166 }
4167
4168 asTransaction([&](Transaction& t) {
4169 t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
4170 });
4171
4172 {
4173 SCOPED_TRACE("After reparenting grandchild");
4174 ScreenCapture::captureScreen(&mCapture);
4175 mCapture->checkPixel(64, 64, 111, 111, 111);
4176 }
4177}
4178
Vishnu Nair14d76922019-08-05 08:41:20 -07004179TEST_F(ChildLayerTest, ChildrenRelativeZSurvivesParentDestruction) {
4180 sp<SurfaceControl> mGrandChild =
4181 createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
4182 fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
4183
4184 // draw grand child behind the foreground surface
4185 asTransaction([&](Transaction& t) {
4186 t.setRelativeLayer(mGrandChild, mFGSurfaceControl->getHandle(), -1);
4187 });
4188
4189 {
4190 SCOPED_TRACE("Child visible");
4191 ScreenCapture::captureScreen(&mCapture);
4192 mCapture->checkPixel(64, 64, 200, 200, 200);
4193 }
4194
4195 asTransaction([&](Transaction& t) {
4196 t.reparent(mChild, nullptr);
4197 t.reparentChildren(mChild, mFGSurfaceControl->getHandle());
4198 });
4199
4200 {
4201 SCOPED_TRACE("foreground visible reparenting grandchild");
4202 ScreenCapture::captureScreen(&mCapture);
4203 mCapture->checkPixel(64, 64, 195, 63, 63);
4204 }
4205}
4206
chaviw161410b02017-07-27 10:46:08 -07004207TEST_F(ChildLayerTest, DetachChildrenSameClient) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004208 asTransaction([&](Transaction& t) {
4209 t.show(mChild);
4210 t.setPosition(mChild, 10, 10);
4211 t.setPosition(mFGSurfaceControl, 64, 64);
4212 });
Robert Carr9524cb32017-02-13 11:32:32 -08004213
4214 {
chaviw0e3479f2018-09-10 16:49:30 -07004215 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004216 // Top left of foreground must now be visible
4217 mCapture->expectFGColor(64, 64);
4218 // But 10 pixels in we should see the child surface
4219 mCapture->expectChildColor(74, 74);
4220 // And 10 more pixels we should be back to the foreground surface
4221 mCapture->expectFGColor(84, 84);
4222 }
4223
chaviw0e3479f2018-09-10 16:49:30 -07004224
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004225 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
Robert Carr9524cb32017-02-13 11:32:32 -08004226
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004227 asTransaction([&](Transaction& t) { t.hide(mChild); });
Robert Carr9524cb32017-02-13 11:32:32 -08004228
chaviw161410b02017-07-27 10:46:08 -07004229 // Since the child has the same client as the parent, it will not get
4230 // detached and will be hidden.
4231 {
chaviw0e3479f2018-09-10 16:49:30 -07004232 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004233 mCapture->expectFGColor(64, 64);
4234 mCapture->expectFGColor(74, 74);
4235 mCapture->expectFGColor(84, 84);
4236 }
4237}
4238
4239TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
4240 sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004241 sp<SurfaceControl> mChildNewClient =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004242 createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
4243 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw161410b02017-07-27 10:46:08 -07004244
chaviw161410b02017-07-27 10:46:08 -07004245 ASSERT_TRUE(mChildNewClient->isValid());
4246
4247 fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
4248
Robert Carr4cdc58f2017-08-23 14:22:20 -07004249 asTransaction([&](Transaction& t) {
4250 t.hide(mChild);
4251 t.show(mChildNewClient);
4252 t.setPosition(mChildNewClient, 10, 10);
4253 t.setPosition(mFGSurfaceControl, 64, 64);
4254 });
chaviw161410b02017-07-27 10:46:08 -07004255
4256 {
chaviw0e3479f2018-09-10 16:49:30 -07004257 mCapture = screenshot();
chaviw161410b02017-07-27 10:46:08 -07004258 // Top left of foreground must now be visible
4259 mCapture->expectFGColor(64, 64);
4260 // But 10 pixels in we should see the child surface
4261 mCapture->expectChildColor(74, 74);
4262 // And 10 more pixels we should be back to the foreground surface
4263 mCapture->expectFGColor(84, 84);
4264 }
4265
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004266 asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
chaviw161410b02017-07-27 10:46:08 -07004267
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004268 asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
chaviw161410b02017-07-27 10:46:08 -07004269
Robert Carr9524cb32017-02-13 11:32:32 -08004270 // Nothing should have changed.
4271 {
chaviw0e3479f2018-09-10 16:49:30 -07004272 mCapture = screenshot();
Robert Carr9524cb32017-02-13 11:32:32 -08004273 mCapture->expectFGColor(64, 64);
4274 mCapture->expectChildColor(74, 74);
4275 mCapture->expectFGColor(84, 84);
4276 }
4277}
4278
chaviw5aedec92018-10-22 10:40:38 -07004279TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
4280 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4281 sp<SurfaceControl> childNewClient =
4282 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4283 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4284
4285 ASSERT_TRUE(childNewClient != nullptr);
4286 ASSERT_TRUE(childNewClient->isValid());
4287
4288 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4289
4290 Transaction()
4291 .hide(mChild)
4292 .show(childNewClient)
4293 .setPosition(childNewClient, 10, 10)
4294 .setPosition(mFGSurfaceControl, 64, 64)
4295 .apply();
4296
4297 {
4298 mCapture = screenshot();
4299 // Top left of foreground must now be visible
4300 mCapture->expectFGColor(64, 64);
4301 // But 10 pixels in we should see the child surface
4302 mCapture->expectChildColor(74, 74);
4303 // And 10 more pixels we should be back to the foreground surface
4304 mCapture->expectFGColor(84, 84);
4305 }
4306
4307 Transaction().detachChildren(mFGSurfaceControl).apply();
4308 Transaction().hide(childNewClient).apply();
4309
4310 // Nothing should have changed.
4311 {
4312 mCapture = screenshot();
4313 mCapture->expectFGColor(64, 64);
4314 mCapture->expectChildColor(74, 74);
4315 mCapture->expectFGColor(84, 84);
4316 }
4317
4318 sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
4319 fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
4320 32);
4321 Transaction()
4322 .setLayer(newParentSurface, INT32_MAX - 1)
4323 .show(newParentSurface)
4324 .setPosition(newParentSurface, 20, 20)
4325 .reparent(childNewClient, newParentSurface->getHandle())
4326 .apply();
4327 {
4328 mCapture = screenshot();
4329 // Child is now hidden.
4330 mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
4331 }
4332}
chaviw43cb3cb2019-05-31 15:23:41 -07004333TEST_F(ChildLayerTest, DetachChildrenWithDeferredTransaction) {
4334 sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
4335 sp<SurfaceControl> childNewClient =
4336 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
4337 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4338
4339 ASSERT_TRUE(childNewClient != nullptr);
4340 ASSERT_TRUE(childNewClient->isValid());
4341
4342 fillSurfaceRGBA8(childNewClient, 200, 200, 200);
4343
4344 Transaction()
4345 .hide(mChild)
4346 .show(childNewClient)
4347 .setPosition(childNewClient, 10, 10)
4348 .setPosition(mFGSurfaceControl, 64, 64)
4349 .apply();
4350
4351 {
4352 mCapture = screenshot();
4353 Rect rect = Rect(74, 74, 84, 84);
4354 mCapture->expectBorder(rect, Color{195, 63, 63, 255});
4355 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4356 }
4357
4358 Transaction()
4359 .deferTransactionUntil_legacy(childNewClient, mFGSurfaceControl->getHandle(),
4360 mFGSurfaceControl->getSurface()->getNextFrameNumber())
4361 .apply();
4362 Transaction().detachChildren(mFGSurfaceControl).apply();
4363 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(mFGSurfaceControl, Color::RED, 32, 32));
4364
4365 // BufferLayer can still dequeue buffers even though there's a detached layer with a
4366 // deferred transaction.
4367 {
4368 SCOPED_TRACE("new buffer");
4369 mCapture = screenshot();
4370 Rect rect = Rect(74, 74, 84, 84);
4371 mCapture->expectBorder(rect, Color::RED);
4372 mCapture->expectColor(rect, Color{200, 200, 200, 255});
4373 }
4374}
chaviw5aedec92018-10-22 10:40:38 -07004375
Robert Carr9b429f42017-04-17 14:56:57 -07004376TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004377 asTransaction([&](Transaction& t) {
4378 t.show(mChild);
4379 t.setPosition(mChild, 0, 0);
4380 t.setPosition(mFGSurfaceControl, 0, 0);
4381 });
Robert Carr9b429f42017-04-17 14:56:57 -07004382
4383 {
chaviw0e3479f2018-09-10 16:49:30 -07004384 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004385 // We've positioned the child in the top left.
4386 mCapture->expectChildColor(0, 0);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004387 // But it's only 10x15.
4388 mCapture->expectFGColor(10, 15);
Robert Carr9b429f42017-04-17 14:56:57 -07004389 }
4390
Robert Carr4cdc58f2017-08-23 14:22:20 -07004391 asTransaction([&](Transaction& t) {
4392 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4393 // We cause scaling by 2.
4394 t.setSize(mFGSurfaceControl, 128, 128);
4395 });
Robert Carr9b429f42017-04-17 14:56:57 -07004396
4397 {
chaviw0e3479f2018-09-10 16:49:30 -07004398 mCapture = screenshot();
Robert Carr9b429f42017-04-17 14:56:57 -07004399 // We've positioned the child in the top left.
4400 mCapture->expectChildColor(0, 0);
4401 mCapture->expectChildColor(10, 10);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004402 mCapture->expectChildColor(19, 29);
4403 // And now it should be scaled all the way to 20x30
4404 mCapture->expectFGColor(20, 30);
Robert Carr9b429f42017-04-17 14:56:57 -07004405 }
4406}
4407
Robert Carr1725eee2017-04-26 18:32:15 -07004408// Regression test for b/37673612
4409TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004410 asTransaction([&](Transaction& t) {
4411 t.show(mChild);
4412 t.setPosition(mChild, 0, 0);
4413 t.setPosition(mFGSurfaceControl, 0, 0);
4414 });
Robert Carr1725eee2017-04-26 18:32:15 -07004415
4416 {
chaviw0e3479f2018-09-10 16:49:30 -07004417 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004418 // We've positioned the child in the top left.
4419 mCapture->expectChildColor(0, 0);
Vishnu Nairc652ff82019-03-15 12:48:54 -07004420 mCapture->expectChildColor(9, 14);
4421 // But it's only 10x15.
4422 mCapture->expectFGColor(10, 15);
Robert Carr1725eee2017-04-26 18:32:15 -07004423 }
Robert Carr1725eee2017-04-26 18:32:15 -07004424 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
4425 // the WM specified state size.
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004426 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
Robert Carr1725eee2017-04-26 18:32:15 -07004427 sp<Surface> s = mFGSurfaceControl->getSurface();
4428 auto anw = static_cast<ANativeWindow*>(s.get());
4429 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4430 native_window_set_buffers_dimensions(anw, 64, 128);
4431 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4432 waitForPostedBuffers();
4433
4434 {
4435 // The child should still be in the same place and not have any strange scaling as in
4436 // b/37673612.
chaviw0e3479f2018-09-10 16:49:30 -07004437 mCapture = screenshot();
Robert Carr1725eee2017-04-26 18:32:15 -07004438 mCapture->expectChildColor(0, 0);
4439 mCapture->expectFGColor(10, 10);
4440 }
4441}
4442
Vishnu Nairc652ff82019-03-15 12:48:54 -07004443// A child with a buffer transform from its parents should be cropped by its parent bounds.
4444TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferTransform) {
4445 asTransaction([&](Transaction& t) {
4446 t.show(mChild);
4447 t.setPosition(mChild, 0, 0);
4448 t.setPosition(mFGSurfaceControl, 0, 0);
4449 t.setSize(mChild, 100, 100);
4450 });
4451 fillSurfaceRGBA8(mChild, 200, 200, 200);
4452
4453 {
4454 mCapture = screenshot();
4455
4456 mCapture->expectChildColor(0, 0);
4457 mCapture->expectChildColor(63, 63);
4458 mCapture->expectBGColor(64, 64);
4459 }
4460
4461 asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
4462 sp<Surface> s = mFGSurfaceControl->getSurface();
4463 auto anw = static_cast<ANativeWindow*>(s.get());
4464 // Apply a 90 transform on the buffer.
4465 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4466 native_window_set_buffers_dimensions(anw, 64, 128);
4467 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4468 waitForPostedBuffers();
4469
4470 // The child should be cropped by the new parent bounds.
4471 {
4472 mCapture = screenshot();
4473 mCapture->expectChildColor(0, 0);
4474 mCapture->expectChildColor(99, 63);
4475 mCapture->expectFGColor(100, 63);
4476 mCapture->expectBGColor(128, 64);
4477 }
4478}
4479
4480// A child with a scale transform from its parents should be cropped by its parent bounds.
4481TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferScale) {
4482 asTransaction([&](Transaction& t) {
4483 t.show(mChild);
4484 t.setPosition(mChild, 0, 0);
4485 t.setPosition(mFGSurfaceControl, 0, 0);
4486 t.setSize(mChild, 200, 200);
4487 });
4488 fillSurfaceRGBA8(mChild, 200, 200, 200);
4489
4490 {
4491 mCapture = screenshot();
4492
4493 mCapture->expectChildColor(0, 0);
4494 mCapture->expectChildColor(63, 63);
4495 mCapture->expectBGColor(64, 64);
4496 }
4497
4498 asTransaction([&](Transaction& t) {
4499 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4500 // Set a scaling by 2.
4501 t.setSize(mFGSurfaceControl, 128, 128);
4502 });
4503
4504 // Child should inherit its parents scale but should be cropped by its parent bounds.
4505 {
4506 mCapture = screenshot();
4507 mCapture->expectChildColor(0, 0);
4508 mCapture->expectChildColor(127, 127);
4509 mCapture->expectBGColor(128, 128);
4510 }
4511}
4512
4513// Regression test for b/127368943
4514// Child should ignore the buffer transform but apply parent scale transform.
4515TEST_F(ChildLayerTest, ChildrenWithParentBufferTransformAndScale) {
4516 asTransaction([&](Transaction& t) {
4517 t.show(mChild);
4518 t.setPosition(mChild, 0, 0);
4519 t.setPosition(mFGSurfaceControl, 0, 0);
4520 });
4521
4522 {
4523 mCapture = screenshot();
4524 mCapture->expectChildColor(0, 0);
4525 mCapture->expectChildColor(9, 14);
4526 mCapture->expectFGColor(10, 15);
4527 }
4528
4529 // Change the size of the foreground to 128 * 64 so we can test rotation as well.
4530 asTransaction([&](Transaction& t) {
4531 t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
4532 t.setSize(mFGSurfaceControl, 128, 64);
4533 });
4534 sp<Surface> s = mFGSurfaceControl->getSurface();
4535 auto anw = static_cast<ANativeWindow*>(s.get());
4536 // Apply a 90 transform on the buffer and submit a buffer half the expected size so that we
4537 // have an effective scale of 2.0 applied to the buffer along with a rotation transform.
4538 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
4539 native_window_set_buffers_dimensions(anw, 32, 64);
4540 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
4541 waitForPostedBuffers();
4542
4543 // The child should ignore the buffer transform but apply the 2.0 scale from parent.
4544 {
4545 mCapture = screenshot();
4546 mCapture->expectChildColor(0, 0);
4547 mCapture->expectChildColor(19, 29);
4548 mCapture->expectFGColor(20, 30);
4549 }
4550}
4551
Dan Stoza412903f2017-04-27 13:42:17 -07004552TEST_F(ChildLayerTest, Bug36858924) {
4553 // Destroy the child layer
4554 mChild.clear();
4555
4556 // Now recreate it as hidden
Vishnu Nair88a11f22018-11-28 18:30:57 -08004557 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
4558 ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
Dan Stoza412903f2017-04-27 13:42:17 -07004559
4560 // Show the child layer in a deferred transaction
Robert Carr4cdc58f2017-08-23 14:22:20 -07004561 asTransaction([&](Transaction& t) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07004562 t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
4563 mFGSurfaceControl->getSurface()->getNextFrameNumber());
Robert Carr4cdc58f2017-08-23 14:22:20 -07004564 t.show(mChild);
4565 });
Dan Stoza412903f2017-04-27 13:42:17 -07004566
4567 // Render the foreground surface a few times
4568 //
4569 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
4570 // frame because SurfaceFlinger would never process the deferred transaction and would therefore
4571 // never acquire/release the first buffer
4572 ALOGI("Filling 1");
4573 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
4574 ALOGI("Filling 2");
4575 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
4576 ALOGI("Filling 3");
4577 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
4578 ALOGI("Filling 4");
4579 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
4580}
4581
chaviwf1961f72017-09-18 16:41:07 -07004582TEST_F(ChildLayerTest, Reparent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004583 asTransaction([&](Transaction& t) {
4584 t.show(mChild);
4585 t.setPosition(mChild, 10, 10);
4586 t.setPosition(mFGSurfaceControl, 64, 64);
4587 });
chaviw06178942017-07-27 10:25:59 -07004588
4589 {
chaviw0e3479f2018-09-10 16:49:30 -07004590 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004591 // Top left of foreground must now be visible
4592 mCapture->expectFGColor(64, 64);
4593 // But 10 pixels in we should see the child surface
4594 mCapture->expectChildColor(74, 74);
4595 // And 10 more pixels we should be back to the foreground surface
4596 mCapture->expectFGColor(84, 84);
4597 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07004598
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004599 asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
Robert Carr4cdc58f2017-08-23 14:22:20 -07004600
chaviw06178942017-07-27 10:25:59 -07004601 {
chaviw0e3479f2018-09-10 16:49:30 -07004602 mCapture = screenshot();
chaviw06178942017-07-27 10:25:59 -07004603 mCapture->expectFGColor(64, 64);
4604 // In reparenting we should have exposed the entire foreground surface.
4605 mCapture->expectFGColor(74, 74);
4606 // And the child layer should now begin at 10, 10 (since the BG
4607 // layer is at (0, 0)).
4608 mCapture->expectBGColor(9, 9);
4609 mCapture->expectChildColor(10, 10);
4610 }
4611}
4612
chaviwf1961f72017-09-18 16:41:07 -07004613TEST_F(ChildLayerTest, ReparentToNoParent) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07004614 asTransaction([&](Transaction& t) {
4615 t.show(mChild);
4616 t.setPosition(mChild, 10, 10);
4617 t.setPosition(mFGSurfaceControl, 64, 64);
4618 });
chaviwf1961f72017-09-18 16:41:07 -07004619
4620 {
chaviw0e3479f2018-09-10 16:49:30 -07004621 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004622 // Top left of foreground must now be visible
4623 mCapture->expectFGColor(64, 64);
4624 // But 10 pixels in we should see the child surface
4625 mCapture->expectChildColor(74, 74);
4626 // And 10 more pixels we should be back to the foreground surface
4627 mCapture->expectFGColor(84, 84);
4628 }
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004629 asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
chaviwf1961f72017-09-18 16:41:07 -07004630 {
chaviw0e3479f2018-09-10 16:49:30 -07004631 mCapture = screenshot();
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004632 // The surface should now be offscreen.
chaviwf1961f72017-09-18 16:41:07 -07004633 mCapture->expectFGColor(64, 64);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08004634 mCapture->expectFGColor(74, 74);
chaviwf1961f72017-09-18 16:41:07 -07004635 mCapture->expectFGColor(84, 84);
4636 }
4637}
4638
4639TEST_F(ChildLayerTest, ReparentFromNoParent) {
chaviw0e3479f2018-09-10 16:49:30 -07004640 sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
Peiyong Lin566a3b42018-01-09 18:22:43 -08004641 ASSERT_TRUE(newSurface != nullptr);
chaviwf1961f72017-09-18 16:41:07 -07004642 ASSERT_TRUE(newSurface->isValid());
4643
4644 fillSurfaceRGBA8(newSurface, 63, 195, 63);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004645 asTransaction([&](Transaction& t) {
4646 t.hide(mChild);
4647 t.show(newSurface);
4648 t.setPosition(newSurface, 10, 10);
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004649 t.setLayer(newSurface, INT32_MAX - 2);
Robert Carr4cdc58f2017-08-23 14:22:20 -07004650 t.setPosition(mFGSurfaceControl, 64, 64);
4651 });
chaviwf1961f72017-09-18 16:41:07 -07004652
4653 {
chaviw0e3479f2018-09-10 16:49:30 -07004654 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004655 // Top left of foreground must now be visible
4656 mCapture->expectFGColor(64, 64);
4657 // At 10, 10 we should see the new surface
4658 mCapture->checkPixel(10, 10, 63, 195, 63);
4659 }
4660
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004661 asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
chaviwf1961f72017-09-18 16:41:07 -07004662
4663 {
chaviw0e3479f2018-09-10 16:49:30 -07004664 mCapture = screenshot();
chaviwf1961f72017-09-18 16:41:07 -07004665 // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
4666 // mFGSurface, putting it at 74, 74.
4667 mCapture->expectFGColor(64, 64);
4668 mCapture->checkPixel(74, 74, 63, 195, 63);
4669 mCapture->expectFGColor(84, 84);
4670 }
4671}
4672
chaviwc9674332017-08-28 12:32:18 -07004673TEST_F(ChildLayerTest, NestedChildren) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004674 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
4675 PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
chaviwc9674332017-08-28 12:32:18 -07004676 fillSurfaceRGBA8(grandchild, 50, 50, 50);
4677
4678 {
chaviw0e3479f2018-09-10 16:49:30 -07004679 mCapture = screenshot();
chaviwc9674332017-08-28 12:32:18 -07004680 // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
4681 // which begins at 64, 64
4682 mCapture->checkPixel(64, 64, 50, 50, 50);
4683 }
4684}
4685
Robert Carr503c7042017-09-27 15:06:08 -07004686TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07004687 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
Robert Carr503c7042017-09-27 15:06:08 -07004688 fillSurfaceRGBA8(relative, 255, 255, 255);
4689
4690 Transaction t;
4691 t.setLayer(relative, INT32_MAX)
4692 .setRelativeLayer(mChild, relative->getHandle(), 1)
4693 .setPosition(mFGSurfaceControl, 0, 0)
4694 .apply(true);
4695
4696 // We expect that the child should have been elevated above our
4697 // INT_MAX layer even though it's not a child of it.
4698 {
chaviw0e3479f2018-09-10 16:49:30 -07004699 mCapture = screenshot();
Robert Carr503c7042017-09-27 15:06:08 -07004700 mCapture->expectChildColor(0, 0);
4701 mCapture->expectChildColor(9, 9);
4702 mCapture->checkPixel(10, 10, 255, 255, 255);
4703 }
4704}
Vishnu Nairc652ff82019-03-15 12:48:54 -07004705
Vishnu Nair60356342018-11-13 13:00:45 -08004706class BoundlessLayerTest : public LayerUpdateTest {
4707protected:
4708 std::unique_ptr<ScreenCapture> mCapture;
4709};
4710
4711// Verify setting a size on a buffer layer has no effect.
4712TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
4713 sp<SurfaceControl> bufferLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004714 createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
4715 mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004716 ASSERT_TRUE(bufferLayer->isValid());
4717 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
4718 asTransaction([&](Transaction& t) { t.show(bufferLayer); });
4719 {
4720 mCapture = screenshot();
4721 // Top left of background must now be visible
4722 mCapture->expectBGColor(0, 0);
4723 // Foreground Surface bounds must be color layer
4724 mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
4725 // Buffer layer should not extend past buffer bounds
4726 mCapture->expectFGColor(95, 95);
4727 }
4728}
4729
4730// Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
4731// which will crop the color layer.
4732TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
4733 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004734 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4735 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004736 ASSERT_TRUE(colorLayer->isValid());
4737 asTransaction([&](Transaction& t) {
4738 t.setColor(colorLayer, half3{0, 0, 0});
4739 t.show(colorLayer);
4740 });
4741 {
4742 mCapture = screenshot();
4743 // Top left of background must now be visible
4744 mCapture->expectBGColor(0, 0);
4745 // Foreground Surface bounds must be color layer
4746 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4747 // Color layer should not extend past foreground bounds
4748 mCapture->expectBGColor(129, 129);
4749 }
4750}
4751
4752// Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
4753// a crop which will be used to crop the color layer.
4754TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004755 sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4756 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004757 ASSERT_TRUE(cropLayer->isValid());
4758 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004759 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4760 ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004761 ASSERT_TRUE(colorLayer->isValid());
4762 asTransaction([&](Transaction& t) {
4763 t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
4764 t.setColor(colorLayer, half3{0, 0, 0});
4765 t.show(cropLayer);
4766 t.show(colorLayer);
4767 });
4768 {
4769 mCapture = screenshot();
4770 // Top left of background must now be visible
4771 mCapture->expectBGColor(0, 0);
4772 // Top left of foreground must now be visible
4773 mCapture->expectFGColor(64, 64);
4774 // 5 pixels from the foreground we should see the child surface
4775 mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
4776 // 10 pixels from the foreground we should be back to the foreground surface
4777 mCapture->expectFGColor(74, 74);
4778 }
4779}
4780
4781// Verify for boundless layer with no children, their transforms have no effect.
4782TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
4783 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004784 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4785 ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004786 ASSERT_TRUE(colorLayer->isValid());
4787 asTransaction([&](Transaction& t) {
4788 t.setPosition(colorLayer, 320, 320);
4789 t.setMatrix(colorLayer, 2, 0, 0, 2);
4790 t.setColor(colorLayer, half3{0, 0, 0});
4791 t.show(colorLayer);
4792 });
4793 {
4794 mCapture = screenshot();
4795 // Top left of background must now be visible
4796 mCapture->expectBGColor(0, 0);
4797 // Foreground Surface bounds must be color layer
4798 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4799 // Color layer should not extend past foreground bounds
4800 mCapture->expectBGColor(129, 129);
4801 }
4802}
4803
4804// Verify for boundless layer with children, their transforms have an effect.
4805TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
4806 sp<SurfaceControl> boundlessLayerRightShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004807 createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4808 0 /* flags */, mFGSurfaceControl.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004809 ASSERT_TRUE(boundlessLayerRightShift->isValid());
4810 sp<SurfaceControl> boundlessLayerDownShift =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004811 createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
4812 0 /* flags */, boundlessLayerRightShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004813 ASSERT_TRUE(boundlessLayerDownShift->isValid());
4814 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004815 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4816 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
Vishnu Nair60356342018-11-13 13:00:45 -08004817 ASSERT_TRUE(colorLayer->isValid());
4818 asTransaction([&](Transaction& t) {
4819 t.setPosition(boundlessLayerRightShift, 32, 0);
4820 t.show(boundlessLayerRightShift);
4821 t.setPosition(boundlessLayerDownShift, 0, 32);
4822 t.show(boundlessLayerDownShift);
4823 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4824 t.setColor(colorLayer, half3{0, 0, 0});
4825 t.show(colorLayer);
4826 });
4827 {
4828 mCapture = screenshot();
4829 // Top left of background must now be visible
4830 mCapture->expectBGColor(0, 0);
4831 // Top left of foreground must now be visible
4832 mCapture->expectFGColor(64, 64);
4833 // Foreground Surface bounds must be color layer
4834 mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
4835 // Color layer should not extend past foreground bounds
4836 mCapture->expectBGColor(129, 129);
4837 }
4838}
4839
4840// Verify child layers do not get clipped if they temporarily move into the negative
4841// coordinate space as the result of an intermediate transformation.
4842TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
4843 sp<SurfaceControl> boundlessLayer =
4844 mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4845 0 /* flags */, mFGSurfaceControl.get());
4846 ASSERT_TRUE(boundlessLayer != nullptr);
4847 ASSERT_TRUE(boundlessLayer->isValid());
4848 sp<SurfaceControl> colorLayer =
4849 mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
4850 ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
4851 ASSERT_TRUE(colorLayer != nullptr);
4852 ASSERT_TRUE(colorLayer->isValid());
4853 asTransaction([&](Transaction& t) {
4854 // shift child layer off bounds. If this layer was not boundless, we will
4855 // expect the child layer to be cropped.
4856 t.setPosition(boundlessLayer, 32, 32);
4857 t.show(boundlessLayer);
4858 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4859 // undo shift by parent
4860 t.setPosition(colorLayer, -32, -32);
4861 t.setColor(colorLayer, half3{0, 0, 0});
4862 t.show(colorLayer);
4863 });
4864 {
4865 mCapture = screenshot();
4866 // Top left of background must now be visible
4867 mCapture->expectBGColor(0, 0);
4868 // Foreground Surface bounds must be color layer
4869 mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
4870 // Color layer should not extend past foreground bounds
4871 mCapture->expectBGColor(129, 129);
4872 }
4873}
4874
4875// Verify for boundless root layers with children, their transforms have an effect.
4876TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08004877 sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
4878 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
Vishnu Nair60356342018-11-13 13:00:45 -08004879 ASSERT_TRUE(rootBoundlessLayer->isValid());
4880 sp<SurfaceControl> colorLayer =
Vishnu Nair88a11f22018-11-28 18:30:57 -08004881 createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
4882 ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
4883
Vishnu Nair60356342018-11-13 13:00:45 -08004884 ASSERT_TRUE(colorLayer->isValid());
4885 asTransaction([&](Transaction& t) {
4886 t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
4887 t.setPosition(rootBoundlessLayer, 32, 32);
4888 t.show(rootBoundlessLayer);
4889 t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
4890 t.setColor(colorLayer, half3{0, 0, 0});
4891 t.show(colorLayer);
4892 t.hide(mFGSurfaceControl);
4893 });
4894 {
4895 mCapture = screenshot();
4896 // Top left of background must now be visible
4897 mCapture->expectBGColor(0, 0);
4898 // Top left of foreground must now be visible
4899 mCapture->expectBGColor(31, 31);
4900 // Foreground Surface bounds must be color layer
4901 mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
4902 // Color layer should not extend past foreground bounds
4903 mCapture->expectBGColor(97, 97);
4904 }
4905}
Robert Carr503c7042017-09-27 15:06:08 -07004906
chaviwa76b2712017-09-20 12:02:26 -07004907class ScreenCaptureTest : public LayerUpdateTest {
4908protected:
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004909 std::unique_ptr<ScreenCapture> mCapture;
chaviwa76b2712017-09-20 12:02:26 -07004910};
4911
4912TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
4913 auto bgHandle = mBGSurfaceControl->getHandle();
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004914 ScreenCapture::captureLayers(&mCapture, bgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004915 mCapture->expectBGColor(0, 0);
4916 // Doesn't capture FG layer which is at 64, 64
4917 mCapture->expectBGColor(64, 64);
4918}
4919
4920TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
4921 auto fgHandle = mFGSurfaceControl->getHandle();
4922
Vishnu Nair88a11f22018-11-28 18:30:57 -08004923 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4924 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07004925 fillSurfaceRGBA8(child, 200, 200, 200);
4926
Chia-I Wu1078bbb2017-10-20 11:29:02 -07004927 SurfaceComposerClient::Transaction().show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07004928
4929 // Captures mFGSurfaceControl layer and its child.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00004930 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07004931 mCapture->expectFGColor(10, 10);
4932 mCapture->expectChildColor(0, 0);
4933}
4934
Robert Carr578038f2018-03-09 12:25:24 -08004935TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
4936 auto fgHandle = mFGSurfaceControl->getHandle();
4937
Vishnu Nair88a11f22018-11-28 18:30:57 -08004938 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4939 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08004940 fillSurfaceRGBA8(child, 200, 200, 200);
4941
4942 SurfaceComposerClient::Transaction().show(child).apply(true);
4943
4944 // Captures mFGSurfaceControl's child
4945 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
4946 mCapture->checkPixel(10, 10, 0, 0, 0);
4947 mCapture->expectChildColor(0, 0);
4948}
4949
Robert Carr866455f2019-04-02 16:28:26 -07004950TEST_F(ScreenCaptureTest, CaptureLayerExclude) {
4951 auto fgHandle = mFGSurfaceControl->getHandle();
4952
4953 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4954 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4955 fillSurfaceRGBA8(child, 200, 200, 200);
4956 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
4957 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4958 fillSurfaceRGBA8(child2, 200, 0, 200);
4959
4960 SurfaceComposerClient::Transaction()
4961 .show(child)
4962 .show(child2)
4963 .setLayer(child, 1)
4964 .setLayer(child2, 2)
4965 .apply(true);
4966
4967 // Child2 would be visible but its excluded, so we should see child1 color instead.
4968 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
4969 mCapture->checkPixel(10, 10, 0, 0, 0);
4970 mCapture->checkPixel(0, 0, 200, 200, 200);
4971}
4972
4973// Like the last test but verifies that children are also exclude.
4974TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) {
4975 auto fgHandle = mFGSurfaceControl->getHandle();
4976
4977 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
4978 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4979 fillSurfaceRGBA8(child, 200, 200, 200);
4980 sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
4981 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
4982 fillSurfaceRGBA8(child2, 200, 0, 200);
4983 sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10,
4984 PIXEL_FORMAT_RGBA_8888, 0, child2.get());
4985 fillSurfaceRGBA8(child2, 200, 0, 200);
4986
4987 SurfaceComposerClient::Transaction()
4988 .show(child)
4989 .show(child2)
4990 .show(child3)
4991 .setLayer(child, 1)
4992 .setLayer(child2, 2)
4993 .apply(true);
4994
4995 // Child2 would be visible but its excluded, so we should see child1 color instead.
4996 ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
4997 mCapture->checkPixel(10, 10, 0, 0, 0);
4998 mCapture->checkPixel(0, 0, 200, 200, 200);
4999}
5000
chaviw50da5042018-04-09 13:49:37 -07005001TEST_F(ScreenCaptureTest, CaptureTransparent) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005002 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5003 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw50da5042018-04-09 13:49:37 -07005004
5005 fillSurfaceRGBA8(child, 200, 200, 200);
5006
5007 SurfaceComposerClient::Transaction().show(child).apply(true);
5008
5009 auto childHandle = child->getHandle();
5010
5011 // Captures child
5012 ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
5013 mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
5014 // Area outside of child's bounds is transparent.
5015 mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
5016}
5017
chaviw4b129c22018-04-09 16:19:43 -07005018TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
5019 auto fgHandle = mFGSurfaceControl->getHandle();
5020
Vishnu Nair88a11f22018-11-28 18:30:57 -08005021 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5022 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5023 ASSERT_NE(nullptr, child.get()) << "failed to create surface";
chaviw0e3479f2018-09-10 16:49:30 -07005024 sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
chaviw4b129c22018-04-09 16:19:43 -07005025 fillSurfaceRGBA8(child, 200, 200, 200);
5026 fillSurfaceRGBA8(relative, 100, 100, 100);
5027
5028 SurfaceComposerClient::Transaction()
5029 .show(child)
5030 // Set relative layer above fg layer so should be shown above when computing all layers.
5031 .setRelativeLayer(relative, fgHandle, 1)
5032 .show(relative)
5033 .apply(true);
5034
5035 // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
5036 ScreenCapture::captureLayers(&mCapture, fgHandle);
5037 mCapture->expectFGColor(10, 10);
5038 mCapture->expectChildColor(0, 0);
5039}
5040
5041TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
5042 auto fgHandle = mFGSurfaceControl->getHandle();
5043
Vishnu Nair88a11f22018-11-28 18:30:57 -08005044 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5045 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
5046 sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
5047 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviw4b129c22018-04-09 16:19:43 -07005048 fillSurfaceRGBA8(child, 200, 200, 200);
5049 fillSurfaceRGBA8(relative, 100, 100, 100);
5050
5051 SurfaceComposerClient::Transaction()
5052 .show(child)
5053 // Set relative layer below fg layer but relative to child layer so it should be shown
5054 // above child layer.
5055 .setLayer(relative, -1)
5056 .setRelativeLayer(relative, child->getHandle(), 1)
5057 .show(relative)
5058 .apply(true);
5059
5060 // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
5061 // relative value should be taken into account, placing it above child layer.
5062 ScreenCapture::captureLayers(&mCapture, fgHandle);
5063 mCapture->expectFGColor(10, 10);
5064 // Relative layer is showing on top of child layer
5065 mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
5066}
Robert Carr578038f2018-03-09 12:25:24 -08005067
5068// In the following tests we verify successful skipping of a parent layer,
5069// so we use the same verification logic and only change how we mutate
5070// the parent layer to verify that various properties are ignored.
5071class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
5072public:
5073 void SetUp() override {
5074 LayerUpdateTest::SetUp();
5075
Vishnu Nair88a11f22018-11-28 18:30:57 -08005076 mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
5077 mFGSurfaceControl.get());
Robert Carr578038f2018-03-09 12:25:24 -08005078 fillSurfaceRGBA8(mChild, 200, 200, 200);
5079
5080 SurfaceComposerClient::Transaction().show(mChild).apply(true);
5081 }
5082
Vishnu Nair333a9572019-02-15 16:05:56 -08005083 void verify(std::function<void()> verifyStartingState) {
5084 // Verify starting state before a screenshot is taken.
5085 verifyStartingState();
5086
5087 // Verify child layer does not inherit any of the properties of its
5088 // parent when its screenshot is captured.
Robert Carr578038f2018-03-09 12:25:24 -08005089 auto fgHandle = mFGSurfaceControl->getHandle();
5090 ScreenCapture::captureChildLayers(&mCapture, fgHandle);
5091 mCapture->checkPixel(10, 10, 0, 0, 0);
5092 mCapture->expectChildColor(0, 0);
Vishnu Nair333a9572019-02-15 16:05:56 -08005093
5094 // Verify all assumptions are still true after the screenshot is taken.
5095 verifyStartingState();
Robert Carr578038f2018-03-09 12:25:24 -08005096 }
5097
5098 std::unique_ptr<ScreenCapture> mCapture;
5099 sp<SurfaceControl> mChild;
5100};
5101
Vishnu Nair333a9572019-02-15 16:05:56 -08005102// Regression test b/76099859
Robert Carr578038f2018-03-09 12:25:24 -08005103TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
5104
5105 SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
5106
5107 // Even though the parent is hidden we should still capture the child.
Vishnu Nair333a9572019-02-15 16:05:56 -08005108
5109 // Before and after reparenting, verify child is properly hidden
5110 // when rendering full-screen.
5111 verify([&] { screenshot()->expectBGColor(64, 64); });
Robert Carr578038f2018-03-09 12:25:24 -08005112}
5113
5114TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07005115 SurfaceComposerClient::Transaction()
5116 .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
5117 .apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005118
5119 // Even though the parent is cropped out we should still capture the child.
Vishnu Nair333a9572019-02-15 16:05:56 -08005120
5121 // Before and after reparenting, verify child is cropped by parent.
5122 verify([&] { screenshot()->expectBGColor(65, 65); });
Robert Carr578038f2018-03-09 12:25:24 -08005123}
5124
Vishnu Nair333a9572019-02-15 16:05:56 -08005125// Regression test b/124372894
Robert Carr578038f2018-03-09 12:25:24 -08005126TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
Vishnu Nair333a9572019-02-15 16:05:56 -08005127 SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
Robert Carr578038f2018-03-09 12:25:24 -08005128
5129 // We should not inherit the parent scaling.
Robert Carr578038f2018-03-09 12:25:24 -08005130
Vishnu Nair333a9572019-02-15 16:05:56 -08005131 // Before and after reparenting, verify child is properly scaled.
5132 verify([&] { screenshot()->expectChildColor(80, 80); });
Robert Carr15eae092018-03-23 13:43:53 -07005133}
5134
5135
chaviwa76b2712017-09-20 12:02:26 -07005136TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
5137 auto fgHandle = mFGSurfaceControl->getHandle();
5138
Vishnu Nair88a11f22018-11-28 18:30:57 -08005139 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5140 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005141 fillSurfaceRGBA8(child, 200, 200, 200);
5142
Vishnu Nair88a11f22018-11-28 18:30:57 -08005143 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5144 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07005145
5146 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5147 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005148 .show(child)
5149 .setPosition(grandchild, 5, 5)
5150 .show(grandchild)
5151 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005152
5153 // Captures mFGSurfaceControl, its child, and the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005154 ScreenCapture::captureLayers(&mCapture, fgHandle);
chaviwa76b2712017-09-20 12:02:26 -07005155 mCapture->expectFGColor(10, 10);
5156 mCapture->expectChildColor(0, 0);
5157 mCapture->checkPixel(5, 5, 50, 50, 50);
5158}
5159
5160TEST_F(ScreenCaptureTest, CaptureChildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005161 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5162 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005163 fillSurfaceRGBA8(child, 200, 200, 200);
5164 auto childHandle = child->getHandle();
5165
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005166 SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005167
5168 // Captures only the child layer, and not the parent.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005169 ScreenCapture::captureLayers(&mCapture, childHandle);
chaviwa76b2712017-09-20 12:02:26 -07005170 mCapture->expectChildColor(0, 0);
5171 mCapture->expectChildColor(9, 9);
5172}
5173
5174TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
Vishnu Nair88a11f22018-11-28 18:30:57 -08005175 sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
5176 PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
chaviwa76b2712017-09-20 12:02:26 -07005177 fillSurfaceRGBA8(child, 200, 200, 200);
5178 auto childHandle = child->getHandle();
5179
Vishnu Nair88a11f22018-11-28 18:30:57 -08005180 sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
5181 PIXEL_FORMAT_RGBA_8888, 0, child.get());
chaviwa76b2712017-09-20 12:02:26 -07005182 fillSurfaceRGBA8(grandchild, 50, 50, 50);
5183
5184 SurfaceComposerClient::Transaction()
Chia-I Wu1078bbb2017-10-20 11:29:02 -07005185 .show(child)
5186 .setPosition(grandchild, 5, 5)
5187 .show(grandchild)
5188 .apply(true);
chaviwa76b2712017-09-20 12:02:26 -07005189
5190 auto grandchildHandle = grandchild->getHandle();
5191
5192 // Captures only the grandchild.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005193 ScreenCapture::captureLayers(&mCapture, grandchildHandle);
chaviwa76b2712017-09-20 12:02:26 -07005194 mCapture->checkPixel(0, 0, 50, 50, 50);
5195 mCapture->checkPixel(4, 4, 50, 50, 50);
5196}
5197
chaviw7206d492017-11-10 16:16:12 -08005198TEST_F(ScreenCaptureTest, CaptureCrop) {
chaviw0e3479f2018-09-10 16:49:30 -07005199 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005200 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5201 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005202
Marissa Wall61c58622018-07-18 10:12:20 -07005203 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5204 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005205
5206 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005207 .setLayer(redLayer, INT32_MAX - 1)
5208 .show(redLayer)
5209 .show(blueLayer)
5210 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005211
5212 auto redLayerHandle = redLayer->getHandle();
5213
5214 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005215 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5216 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5217 // red area below the blue area
5218 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5219 // red area to the right of the blue area
5220 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005221
Marissa Wall861616d2018-10-22 12:52:23 -07005222 const Rect crop = Rect(0, 0, 30, 30);
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005223 ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
chaviw7206d492017-11-10 16:16:12 -08005224 // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
5225 // area visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005226 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
chaviw7206d492017-11-10 16:16:12 -08005227 mCapture->checkPixel(30, 30, 0, 0, 0);
5228}
5229
5230TEST_F(ScreenCaptureTest, CaptureSize) {
chaviw0e3479f2018-09-10 16:49:30 -07005231 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
Vishnu Nair88a11f22018-11-28 18:30:57 -08005232 sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
5233 PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
chaviw7206d492017-11-10 16:16:12 -08005234
Marissa Wall61c58622018-07-18 10:12:20 -07005235 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
5236 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
chaviw7206d492017-11-10 16:16:12 -08005237
5238 SurfaceComposerClient::Transaction()
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005239 .setLayer(redLayer, INT32_MAX - 1)
5240 .show(redLayer)
5241 .show(blueLayer)
5242 .apply(true);
chaviw7206d492017-11-10 16:16:12 -08005243
5244 auto redLayerHandle = redLayer->getHandle();
5245
5246 // Capturing full screen should have both red and blue are visible.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005247 ScreenCapture::captureLayers(&mCapture, redLayerHandle);
5248 mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
5249 // red area below the blue area
5250 mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
5251 // red area to the right of the blue area
5252 mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005253
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005254 ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
chaviw7206d492017-11-10 16:16:12 -08005255 // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005256 mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
5257 // red area below the blue area
5258 mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
5259 // red area to the right of the blue area
5260 mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
chaviw7206d492017-11-10 16:16:12 -08005261 mCapture->checkPixel(30, 30, 0, 0, 0);
5262}
5263
5264TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
chaviw0e3479f2018-09-10 16:49:30 -07005265 sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
chaviw7206d492017-11-10 16:16:12 -08005266
Marissa Wall61c58622018-07-18 10:12:20 -07005267 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
chaviw7206d492017-11-10 16:16:12 -08005268
5269 auto redLayerHandle = redLayer->getHandle();
Robert Carr87246532019-02-04 15:20:26 -08005270 redLayer.clear();
chaviw7206d492017-11-10 16:16:12 -08005271 SurfaceComposerClient::Transaction().apply(true);
5272
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005273 sp<GraphicBuffer> outBuffer;
chaviw7206d492017-11-10 16:16:12 -08005274
5275 // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005276 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
5277 ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
chaviw7206d492017-11-10 16:16:12 -08005278}
5279
chaviw8e3fe5d2018-02-22 10:55:42 -08005280
5281class DereferenceSurfaceControlTest : public LayerTransactionTest {
5282protected:
5283 void SetUp() override {
5284 LayerTransactionTest::SetUp();
5285 bgLayer = createLayer("BG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005286 fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005287 fgLayer = createLayer("FG layer", 20, 20);
Marissa Wall61c58622018-07-18 10:12:20 -07005288 fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
chaviw8e3fe5d2018-02-22 10:55:42 -08005289 Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
5290 {
5291 SCOPED_TRACE("before anything");
5292 auto shot = screenshot();
5293 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5294 }
5295 }
5296 void TearDown() override {
5297 LayerTransactionTest::TearDown();
5298 bgLayer = 0;
5299 fgLayer = 0;
5300 }
5301
5302 sp<SurfaceControl> bgLayer;
5303 sp<SurfaceControl> fgLayer;
5304};
5305
5306TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
5307 fgLayer = nullptr;
5308 {
5309 SCOPED_TRACE("after setting null");
5310 auto shot = screenshot();
5311 shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
5312 }
5313}
5314
5315TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
5316 auto transaction = Transaction().show(fgLayer);
5317 fgLayer = nullptr;
5318 {
5319 SCOPED_TRACE("after setting null");
5320 auto shot = screenshot();
5321 shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
5322 }
5323}
5324
Vishnu Nairb927e1f2019-02-19 13:36:15 -08005325class MultiDisplayLayerBoundsTest : public LayerTransactionTest {
5326protected:
5327 virtual void SetUp() {
5328 LayerTransactionTest::SetUp();
5329 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5330
5331 mMainDisplay = SurfaceComposerClient::getInternalDisplayToken();
5332 SurfaceComposerClient::getDisplayInfo(mMainDisplay, &mMainDisplayInfo);
5333
5334 sp<IGraphicBufferConsumer> consumer;
5335 BufferQueue::createBufferQueue(&mProducer, &consumer);
5336 consumer->setConsumerName(String8("Virtual disp consumer"));
5337 consumer->setDefaultBufferSize(mMainDisplayInfo.w, mMainDisplayInfo.h);
5338 }
5339
5340 virtual void TearDown() {
5341 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
5342 LayerTransactionTest::TearDown();
5343 mColorLayer = 0;
5344 }
5345
5346 void createDisplay(const Rect& layerStackRect, uint32_t layerStack) {
5347 mVirtualDisplay =
5348 SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), false /*secure*/);
5349 asTransaction([&](Transaction& t) {
5350 t.setDisplaySurface(mVirtualDisplay, mProducer);
5351 t.setDisplayLayerStack(mVirtualDisplay, layerStack);
5352 t.setDisplayProjection(mVirtualDisplay, mMainDisplayInfo.orientation, layerStackRect,
5353 Rect(mMainDisplayInfo.w, mMainDisplayInfo.h));
5354 });
5355 }
5356
5357 void createColorLayer(uint32_t layerStack) {
5358 mColorLayer =
5359 createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */,
5360 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
5361 ASSERT_TRUE(mColorLayer != nullptr);
5362 ASSERT_TRUE(mColorLayer->isValid());
5363 asTransaction([&](Transaction& t) {
5364 t.setLayerStack(mColorLayer, layerStack);
5365 t.setCrop_legacy(mColorLayer, Rect(0, 0, 30, 40));
5366 t.setLayer(mColorLayer, INT32_MAX - 2);
5367 t.setColor(mColorLayer,
5368 half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f,
5369 mExpectedColor.b / 255.0f});
5370 t.show(mColorLayer);
5371 });
5372 }
5373
5374 DisplayInfo mMainDisplayInfo;
5375 sp<IBinder> mMainDisplay;
5376 sp<IBinder> mVirtualDisplay;
5377 sp<IGraphicBufferProducer> mProducer;
5378 sp<SurfaceControl> mColorLayer;
5379 Color mExpectedColor = {63, 63, 195, 255};
5380};
5381
5382TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) {
5383 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 1 /* layerStack */);
5384 createColorLayer(1 /* layerStack */);
5385
5386 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5387
5388 // Verify color layer does not render on main display.
5389 std::unique_ptr<ScreenCapture> sc;
5390 ScreenCapture::captureScreen(&sc, mMainDisplay);
5391 sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255});
5392 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5393
5394 // Verify color layer renders correctly on virtual display.
5395 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5396 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5397 sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 0});
5398}
5399
5400TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) {
5401 // Create a display and set its layer stack to the main display's layer stack so
5402 // the contents of the main display are mirrored on to the virtual display.
5403
5404 // Assumption here is that the new mirrored display has the same viewport as the
5405 // primary display that it is mirroring.
5406 createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 0 /* layerStack */);
5407 createColorLayer(0 /* layerStack */);
5408
5409 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
5410
5411 // Verify color layer renders correctly on main display and it is mirrored on the
5412 // virtual display.
5413 std::unique_ptr<ScreenCapture> sc;
5414 ScreenCapture::captureScreen(&sc, mMainDisplay);
5415 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5416 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5417
5418 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
5419 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
5420 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
5421}
5422
Ady Abrahamdf9df4a2019-03-12 17:32:05 -07005423class DisplayActiveConfigTest : public ::testing::Test {
5424protected:
5425 void SetUp() override {
5426 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
5427 SurfaceComposerClient::getDisplayConfigs(mDisplayToken, &mDisplayconfigs);
5428 EXPECT_GT(mDisplayconfigs.size(), 0);
5429
5430 // set display power to on to make sure config can be changed
5431 SurfaceComposerClient::setDisplayPowerMode(mDisplayToken, HWC_POWER_MODE_NORMAL);
5432 }
5433
5434 sp<IBinder> mDisplayToken;
5435 Vector<DisplayInfo> mDisplayconfigs;
5436};
5437
5438TEST_F(DisplayActiveConfigTest, allConfigsAllowed) {
5439 std::vector<int32_t> allowedConfigs;
5440
5441 // Add all configs to the allowed configs
5442 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5443 allowedConfigs.push_back(i);
5444 }
5445
5446 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5447 EXPECT_EQ(res, NO_ERROR);
5448
5449 std::vector<int32_t> outConfigs;
5450 res = SurfaceComposerClient::getAllowedDisplayConfigs(mDisplayToken, &outConfigs);
5451 EXPECT_EQ(res, NO_ERROR);
5452 EXPECT_EQ(allowedConfigs, outConfigs);
5453}
5454
5455TEST_F(DisplayActiveConfigTest, changeAllowedConfig) {
5456 // we need at least 2 configs available for this test
5457 if (mDisplayconfigs.size() <= 1) return;
5458
5459 int activeConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5460
5461 // We want to set the allowed config to everything but the active config
5462 std::vector<int32_t> allowedConfigs;
5463 for (int i = 0; i < mDisplayconfigs.size(); i++) {
5464 if (i != activeConfig) {
5465 allowedConfigs.push_back(i);
5466 }
5467 }
5468
5469 status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
5470 EXPECT_EQ(res, NO_ERROR);
5471
5472 // Allow some time for the config change
5473 std::this_thread::sleep_for(200ms);
5474
5475 int newActiveConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
5476 EXPECT_NE(activeConfig, newActiveConfig);
5477
5478 // Make sure the new config is part of allowed config
5479 EXPECT_TRUE(std::find(allowedConfigs.begin(), allowedConfigs.end(), newActiveConfig) !=
5480 allowedConfigs.end());
5481}
5482
Vishnu Nairda9c85a2019-06-03 17:26:48 -07005483class RelativeZTest : public LayerTransactionTest {
5484protected:
5485 virtual void SetUp() {
5486 LayerTransactionTest::SetUp();
5487 ASSERT_EQ(NO_ERROR, mClient->initCheck());
5488
5489 const auto display = SurfaceComposerClient::getInternalDisplayToken();
5490 ASSERT_FALSE(display == nullptr);
5491
5492 // Back layer
5493 mBackgroundLayer = createColorLayer("Background layer", Color::RED);
5494
5495 // Front layer
5496 mForegroundLayer = createColorLayer("Foreground layer", Color::GREEN);
5497
5498 asTransaction([&](Transaction& t) {
5499 t.setDisplayLayerStack(display, 0);
5500 t.setLayer(mBackgroundLayer, INT32_MAX - 2).show(mBackgroundLayer);
5501 t.setLayer(mForegroundLayer, INT32_MAX - 1).show(mForegroundLayer);
5502 });
5503 }
5504
5505 virtual void TearDown() {
5506 LayerTransactionTest::TearDown();
5507 mBackgroundLayer = 0;
5508 mForegroundLayer = 0;
5509 }
5510
5511 sp<SurfaceControl> mBackgroundLayer;
5512 sp<SurfaceControl> mForegroundLayer;
5513};
5514
5515// When a layer is reparented offscreen, remove relative z order if the relative parent
5516// is still onscreen so that the layer is not drawn.
5517TEST_F(RelativeZTest, LayerRemoved) {
5518 std::unique_ptr<ScreenCapture> sc;
5519
5520 // Background layer (RED)
5521 // Child layer (WHITE) (relative to foregroud layer)
5522 // Foregroud layer (GREEN)
5523 sp<SurfaceControl> childLayer =
5524 createColorLayer("Child layer", Color::BLUE, mBackgroundLayer.get());
5525
5526 Transaction{}
5527 .setRelativeLayer(childLayer, mForegroundLayer->getHandle(), 1)
5528 .show(childLayer)
5529 .apply();
5530
5531 {
5532 // The childLayer should be in front of the FG control.
5533 ScreenCapture::captureScreen(&sc);
5534 sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b);
5535 }
5536
5537 // Background layer (RED)
5538 // Foregroud layer (GREEN)
5539 Transaction{}.reparent(childLayer, nullptr).apply();
5540
5541 // Background layer (RED)
5542 // Child layer (WHITE)
5543 // Foregroud layer (GREEN)
5544 Transaction{}.reparent(childLayer, mBackgroundLayer->getHandle()).apply();
5545
5546 {
5547 // The relative z info for child layer should be reset, leaving FG control on top.
5548 ScreenCapture::captureScreen(&sc);
5549 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5550 }
5551}
5552
5553// When a layer is reparented offscreen, preseve relative z order if the relative parent
5554// is also offscreen. Regression test b/132613412
5555TEST_F(RelativeZTest, LayerRemovedOffscreenRelativeParent) {
5556 std::unique_ptr<ScreenCapture> sc;
5557
5558 // Background layer (RED)
5559 // Foregroud layer (GREEN)
5560 // child level 1 (WHITE)
5561 // child level 2a (BLUE)
5562 // child level 3 (GREEN) (relative to child level 2b)
5563 // child level 2b (BLACK)
5564 sp<SurfaceControl> childLevel1 =
5565 createColorLayer("child level 1", Color::WHITE, mForegroundLayer.get());
5566 sp<SurfaceControl> childLevel2a =
5567 createColorLayer("child level 2a", Color::BLUE, childLevel1.get());
5568 sp<SurfaceControl> childLevel2b =
5569 createColorLayer("child level 2b", Color::BLACK, childLevel1.get());
5570 sp<SurfaceControl> childLevel3 =
5571 createColorLayer("child level 3", Color::GREEN, childLevel2a.get());
5572
5573 Transaction{}
5574 .setRelativeLayer(childLevel3, childLevel2b->getHandle(), 1)
5575 .show(childLevel2a)
5576 .show(childLevel2b)
5577 .show(childLevel3)
5578 .apply();
5579
5580 {
5581 // The childLevel3 should be in front of childLevel2b.
5582 ScreenCapture::captureScreen(&sc);
5583 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5584 }
5585
5586 // Background layer (RED)
5587 // Foregroud layer (GREEN)
5588 Transaction{}.reparent(childLevel1, nullptr).apply();
5589
5590 // Background layer (RED)
5591 // Foregroud layer (GREEN)
5592 // child level 1 (WHITE)
5593 // child level 2 back (BLUE)
5594 // child level 3 (GREEN) (relative to child level 2b)
5595 // child level 2 front (BLACK)
5596 Transaction{}.reparent(childLevel1, mForegroundLayer->getHandle()).apply();
5597
5598 {
5599 // Nothing should change at this point since relative z info was preserved.
5600 ScreenCapture::captureScreen(&sc);
5601 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
5602 }
5603}
5604
Steven Thomas44685cb2019-07-23 16:19:31 -07005605// This test ensures that when we drop an app buffer in SurfaceFlinger, we merge
5606// the dropped buffer's damage region into the next buffer's damage region. If
5607// we don't do this, we'll report an incorrect damage region to hardware
5608// composer, resulting in broken rendering. This test checks the BufferQueue
5609// case.
5610//
5611// Unfortunately, we don't currently have a way to inspect the damage region
5612// SurfaceFlinger sends to hardware composer from a test, so this test requires
5613// the dev to manually watch the device's screen during the test to spot broken
5614// rendering. Because the results can't be automatically verified, this test is
5615// marked disabled.
5616TEST_F(LayerTransactionTest, DISABLED_BufferQueueLayerMergeDamageRegionWhenDroppingBuffers) {
5617 const int width = mDisplayWidth;
5618 const int height = mDisplayHeight;
5619 sp<SurfaceControl> layer;
5620 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
5621 const auto producer = layer->getIGraphicBufferProducer();
5622 const sp<IProducerListener> dummyListener(new DummyProducerListener);
5623 IGraphicBufferProducer::QueueBufferOutput queueBufferOutput;
5624 ASSERT_EQ(OK,
5625 producer->connect(dummyListener, NATIVE_WINDOW_API_CPU, true, &queueBufferOutput));
5626
5627 std::map<int, sp<GraphicBuffer>> slotMap;
5628 auto slotToBuffer = [&](int slot, sp<GraphicBuffer>* buf) {
5629 ASSERT_NE(nullptr, buf);
5630 const auto iter = slotMap.find(slot);
5631 ASSERT_NE(slotMap.end(), iter);
5632 *buf = iter->second;
5633 };
5634
5635 auto dequeue = [&](int* outSlot) {
5636 ASSERT_NE(nullptr, outSlot);
5637 *outSlot = -1;
5638 int slot;
5639 sp<Fence> fence;
5640 uint64_t age;
5641 FrameEventHistoryDelta timestamps;
5642 const status_t dequeueResult =
5643 producer->dequeueBuffer(&slot, &fence, width, height, PIXEL_FORMAT_RGBA_8888,
5644 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
5645 &age, &timestamps);
5646 if (dequeueResult == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
5647 sp<GraphicBuffer> newBuf;
5648 ASSERT_EQ(OK, producer->requestBuffer(slot, &newBuf));
5649 ASSERT_NE(nullptr, newBuf.get());
5650 slotMap[slot] = newBuf;
5651 } else {
5652 ASSERT_EQ(OK, dequeueResult);
5653 }
5654 *outSlot = slot;
5655 };
5656
5657 auto queue = [&](int slot, const Region& damage, nsecs_t displayTime) {
5658 IGraphicBufferProducer::QueueBufferInput input(
5659 /*timestamp=*/displayTime, /*isAutoTimestamp=*/false, HAL_DATASPACE_UNKNOWN,
5660 /*crop=*/Rect::EMPTY_RECT, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
5661 /*transform=*/0, Fence::NO_FENCE);
5662 input.setSurfaceDamage(damage);
5663 IGraphicBufferProducer::QueueBufferOutput output;
5664 ASSERT_EQ(OK, producer->queueBuffer(slot, input, &output));
5665 };
5666
5667 auto fillAndPostBuffers = [&](const Color& color) {
5668 int slot1;
5669 ASSERT_NO_FATAL_FAILURE(dequeue(&slot1));
5670 int slot2;
5671 ASSERT_NO_FATAL_FAILURE(dequeue(&slot2));
5672
5673 sp<GraphicBuffer> buf1;
5674 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot1, &buf1));
5675 sp<GraphicBuffer> buf2;
5676 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot2, &buf2));
5677 fillGraphicBufferColor(buf1, Rect(width, height), color);
5678 fillGraphicBufferColor(buf2, Rect(width, height), color);
5679
5680 const auto displayTime = systemTime() + milliseconds_to_nanoseconds(100);
5681 ASSERT_NO_FATAL_FAILURE(queue(slot1, Region::INVALID_REGION, displayTime));
5682 ASSERT_NO_FATAL_FAILURE(
5683 queue(slot2, Region(Rect(width / 3, height / 3, 2 * width / 3, 2 * height / 3)),
5684 displayTime));
5685 };
5686
5687 const auto startTime = systemTime();
5688 const std::array<Color, 3> colors = {Color::RED, Color::GREEN, Color::BLUE};
5689 int colorIndex = 0;
5690 while (nanoseconds_to_seconds(systemTime() - startTime) < 10) {
5691 ASSERT_NO_FATAL_FAILURE(fillAndPostBuffers(colors[colorIndex++ % colors.size()]));
5692 std::this_thread::sleep_for(1s);
5693 }
5694
5695 ASSERT_EQ(OK, producer->disconnect(NATIVE_WINDOW_API_CPU));
5696}
5697
Chavi Weingarten40482ff2017-11-30 01:51:40 +00005698} // namespace android