• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 
21 #include <thread>
22 #include "LayerTransactionTest.h"
23 
24 namespace android {
25 
26 using android::hardware::graphics::common::V1_1::BufferUsage;
27 
TEST_F(LayerTransactionTest,SetTransformToDisplayInverse_BufferState)28 TEST_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 
35     ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::GREEN, 32, 32));
36 
37     Transaction().setTransformToDisplayInverse(layer, true).apply();
38 }
39 
TEST_F(LayerTransactionTest,SetSidebandStreamNull_BufferState)40 TEST_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 
TEST_F(LayerTransactionTest,ReparentToSelf)49 TEST_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));
53     Transaction().reparent(layer, layer).apply();
54 
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.
TEST_F(LayerTransactionTest,DISABLED_BufferQueueLayerMergeDamageRegionWhenDroppingBuffers)77 TEST_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();
83     const sp<IProducerListener> stubListener(sp<StubProducerListener>::make());
84     IGraphicBufferProducer::QueueBufferOutput queueBufferOutput;
85     ASSERT_EQ(OK, producer->connect(stubListener, NATIVE_WINDOW_API_CPU, true, &queueBufferOutput));
86 
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 }
157 
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.
TEST_F(LayerTransactionTest,BufferTakesPriorityOverBlur)161 TEST_F(LayerTransactionTest, BufferTakesPriorityOverBlur) {
162     sp<SurfaceControl> layer;
163     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
164     ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::RED, 32, 32));
165     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 
TEST_F(LayerTransactionTest,BufferTakesPriorityOverColor)174 TEST_F(LayerTransactionTest, BufferTakesPriorityOverColor) {
175     sp<SurfaceControl> layer;
176     ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
177     ASSERT_NO_FATAL_FAILURE(fillBufferLayerColor(layer, Color::RED, 32, 32));
178     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 
TEST_F(LayerTransactionTest,CommitCallbackCalledOnce)187 TEST_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 
TEST_F(LayerTransactionTest,AddRemoveLayers)216 TEST_F(LayerTransactionTest, AddRemoveLayers) {
217     for (int i = 0; i < 100; i++) {
218         sp<SurfaceControl> layer;
219         ASSERT_NO_FATAL_FAILURE(
220                 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
221         layer.clear();
222     }
223 }
224 
225 } // namespace android
226 
227 // TODO(b/129481165): remove the #pragma below and fix conversion issues
228 #pragma clang diagnostic pop // ignored "-Wconversion"
229