blob: 03de8d0b6d3ab7cb0d1db33ac80cecea643ce8e4 [file] [log] [blame]
Valerie Hau9cfc6d82019-09-23 13:54:07 -07001/*
2 * Copyright (C) 2019 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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Valerie Hau9cfc6d82019-09-23 13:54:07 -070021#include <thread>
22#include "LayerTransactionTest.h"
23
24namespace android {
25
26using android::hardware::graphics::common::V1_1::BufferUsage;
27
Valerie Hau9cfc6d82019-09-23 13:54:07 -070028TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
29 sp<SurfaceControl> layer;
30 ASSERT_NO_FATAL_FAILURE(
31 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
32
33 Transaction().setTransformToDisplayInverse(layer, false).apply();
34
Patrick Williams83f36b22022-09-14 17:57:35 +000035 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::GREEN, 32, 32));
Valerie Hau9cfc6d82019-09-23 13:54:07 -070036
37 Transaction().setTransformToDisplayInverse(layer, true).apply();
38}
39
40TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
41 sp<SurfaceControl> layer;
42 ASSERT_NO_FATAL_FAILURE(
43 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
44
45 // verify this doesn't cause a crash
46 Transaction().setSidebandStream(layer, nullptr).apply();
47}
48
49TEST_F(LayerTransactionTest, ReparentToSelf) {
50 sp<SurfaceControl> layer;
51 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
52 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
Pablo Gamito11dcc222020-09-12 15:49:39 +000053 Transaction().reparent(layer, layer).apply();
Valerie Hau9cfc6d82019-09-23 13:54:07 -070054
55 {
56 // We expect the transaction to be silently dropped, but for SurfaceFlinger
57 // to still be functioning.
58 SCOPED_TRACE("after reparent to self");
59 const Rect rect(0, 0, 32, 32);
60 auto shot = screenshot();
61 shot->expectColor(rect, Color::RED);
62 shot->expectBorder(rect, Color::BLACK);
63 }
64}
65
66// This test ensures that when we drop an app buffer in SurfaceFlinger, we merge
67// the dropped buffer's damage region into the next buffer's damage region. If
68// we don't do this, we'll report an incorrect damage region to hardware
69// composer, resulting in broken rendering. This test checks the BufferQueue
70// case.
71//
72// Unfortunately, we don't currently have a way to inspect the damage region
73// SurfaceFlinger sends to hardware composer from a test, so this test requires
74// the dev to manually watch the device's screen during the test to spot broken
75// rendering. Because the results can't be automatically verified, this test is
76// marked disabled.
77TEST_F(LayerTransactionTest, DISABLED_BufferQueueLayerMergeDamageRegionWhenDroppingBuffers) {
78 const int width = mDisplayWidth;
79 const int height = mDisplayHeight;
80 sp<SurfaceControl> layer;
81 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
82 const auto producer = layer->getIGraphicBufferProducer();
Ady Abrahamd11bade2022-08-01 16:18:03 -070083 const sp<IProducerListener> stubListener(sp<StubProducerListener>::make());
Valerie Hau9cfc6d82019-09-23 13:54:07 -070084 IGraphicBufferProducer::QueueBufferOutput queueBufferOutput;
Peiyong Lind8460c82020-07-28 16:04:22 -070085 ASSERT_EQ(OK, producer->connect(stubListener, NATIVE_WINDOW_API_CPU, true, &queueBufferOutput));
Valerie Hau9cfc6d82019-09-23 13:54:07 -070086
87 std::map<int, sp<GraphicBuffer>> slotMap;
88 auto slotToBuffer = [&](int slot, sp<GraphicBuffer>* buf) {
89 ASSERT_NE(nullptr, buf);
90 const auto iter = slotMap.find(slot);
91 ASSERT_NE(slotMap.end(), iter);
92 *buf = iter->second;
93 };
94
95 auto dequeue = [&](int* outSlot) {
96 ASSERT_NE(nullptr, outSlot);
97 *outSlot = -1;
98 int slot;
99 sp<Fence> fence;
100 uint64_t age;
101 FrameEventHistoryDelta timestamps;
102 const status_t dequeueResult =
103 producer->dequeueBuffer(&slot, &fence, width, height, PIXEL_FORMAT_RGBA_8888,
104 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
105 &age, &timestamps);
106 if (dequeueResult == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
107 sp<GraphicBuffer> newBuf;
108 ASSERT_EQ(OK, producer->requestBuffer(slot, &newBuf));
109 ASSERT_NE(nullptr, newBuf.get());
110 slotMap[slot] = newBuf;
111 } else {
112 ASSERT_EQ(OK, dequeueResult);
113 }
114 *outSlot = slot;
115 };
116
117 auto queue = [&](int slot, const Region& damage, nsecs_t displayTime) {
118 IGraphicBufferProducer::QueueBufferInput input(
119 /*timestamp=*/displayTime, /*isAutoTimestamp=*/false, HAL_DATASPACE_UNKNOWN,
120 /*crop=*/Rect::EMPTY_RECT, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
121 /*transform=*/0, Fence::NO_FENCE);
122 input.setSurfaceDamage(damage);
123 IGraphicBufferProducer::QueueBufferOutput output;
124 ASSERT_EQ(OK, producer->queueBuffer(slot, input, &output));
125 };
126
127 auto fillAndPostBuffers = [&](const Color& color) {
128 int slot1;
129 ASSERT_NO_FATAL_FAILURE(dequeue(&slot1));
130 int slot2;
131 ASSERT_NO_FATAL_FAILURE(dequeue(&slot2));
132
133 sp<GraphicBuffer> buf1;
134 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot1, &buf1));
135 sp<GraphicBuffer> buf2;
136 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot2, &buf2));
137 TransactionUtils::fillGraphicBufferColor(buf1, Rect(width, height), color);
138 TransactionUtils::fillGraphicBufferColor(buf2, Rect(width, height), color);
139
140 const auto displayTime = systemTime() + milliseconds_to_nanoseconds(100);
141 ASSERT_NO_FATAL_FAILURE(queue(slot1, Region::INVALID_REGION, displayTime));
142 ASSERT_NO_FATAL_FAILURE(
143 queue(slot2, Region(Rect(width / 3, height / 3, 2 * width / 3, 2 * height / 3)),
144 displayTime));
145 };
146
147 const auto startTime = systemTime();
148 const std::array<Color, 3> colors = {Color::RED, Color::GREEN, Color::BLUE};
149 int colorIndex = 0;
150 while (nanoseconds_to_seconds(systemTime() - startTime) < 10) {
151 ASSERT_NO_FATAL_FAILURE(fillAndPostBuffers(colors[colorIndex++ % colors.size()]));
152 std::this_thread::sleep_for(1s);
153 }
154
155 ASSERT_EQ(OK, producer->disconnect(NATIVE_WINDOW_API_CPU));
156}
Vishnu Nair5e3befd2022-09-07 18:23:52 +0000157
158// b/245052266 - we possible could support blur and a buffer at the same layer but
159// might break existing assumptions at higher level. This test captures the current
160// expectations. A layer drawing a buffer will not support blur.
161TEST_F(LayerTransactionTest, BufferTakesPriorityOverBlur) {
162 sp<SurfaceControl> layer;
163 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Patrick Williams83f36b22022-09-14 17:57:35 +0000164 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::RED, 32, 32));
Vishnu Nair5e3befd2022-09-07 18:23:52 +0000165 Transaction().setBackgroundBlurRadius(layer, 5).apply();
166 {
167 SCOPED_TRACE("BufferTakesPriorityOverBlur");
168 const Rect rect(0, 0, 32, 32);
169 auto shot = screenshot();
170 shot->expectColor(rect, Color::RED);
171 }
172}
173
174TEST_F(LayerTransactionTest, BufferTakesPriorityOverColor) {
175 sp<SurfaceControl> layer;
176 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
Patrick Williams83f36b22022-09-14 17:57:35 +0000177 ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::RED, 32, 32));
Vishnu Nair5e3befd2022-09-07 18:23:52 +0000178 Transaction().setColor(layer, {Color::GREEN.r, Color::GREEN.g, Color::GREEN.b}).apply();
179 {
180 SCOPED_TRACE("BufferTakesPriorityOverColor");
181 const Rect rect(0, 0, 32, 32);
182 auto shot = screenshot();
183 shot->expectColor(rect, Color::RED);
184 }
185}
186
Patrick Williams30da3672023-07-25 16:52:01 -0500187TEST_F(LayerTransactionTest, CommitCallbackCalledOnce) {
188 auto callCount = 0;
189 auto commitCallback =
190 [&callCount](void* /* context */, nsecs_t /* latchTime */,
191 const sp<Fence>& /* presentFence */,
192 const std::vector<SurfaceControlStats>& /* stats */) mutable {
193 callCount++;
194 };
195
196 // Create two transactions that both contain the same callback id.
197 Transaction t1;
198 t1.addTransactionCommittedCallback(commitCallback, nullptr);
199 Parcel parcel;
200 t1.writeToParcel(&parcel);
201 parcel.setDataPosition(0);
202 Transaction t2;
203 t2.readFromParcel(&parcel);
204
205 // Apply the two transactions. There is a race here as we can't guarantee that the two
206 // transactions will be applied within the same SurfaceFlinger commit. If the transactions are
207 // applied within the same commit then we verify that callback ids are deduplicated within a
208 // single commit. Otherwise, we verify that commit callbacks are deduplicated across separate
209 // commits.
210 t1.apply();
211 t2.apply(/*synchronous=*/true);
212
213 ASSERT_EQ(callCount, 1);
214}
215
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700216} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800217
218// TODO(b/129481165): remove the #pragma below and fix conversion issues
219#pragma clang diagnostic pop // ignored "-Wconversion"