blob: 0ef4150bfe086864f94b805153fe3a4bd8ed8fba [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 <private/android_filesystem_config.h>
22#include <thread>
23#include "LayerTransactionTest.h"
24
25namespace android {
26
27using android::hardware::graphics::common::V1_1::BufferUsage;
28
29TEST_F(LayerTransactionTest, SetFlagsSecureEUidSystem) {
30 sp<SurfaceControl> layer;
31 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
32 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
33
34 sp<ISurfaceComposer> composer = ComposerService::getComposerService();
Valerie Hau9cfc6d82019-09-23 13:54:07 -070035 Transaction()
36 .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
37 .apply(true);
chaviwd2432892020-07-24 17:42:39 -070038 ASSERT_EQ(PERMISSION_DENIED, composer->captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau9cfc6d82019-09-23 13:54:07 -070039
40 UIDFaker f(AID_SYSTEM);
41
42 // By default the system can capture screenshots with secure layers but they
43 // will be blacked out
chaviwd2432892020-07-24 17:42:39 -070044 ASSERT_EQ(NO_ERROR, composer->captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau9cfc6d82019-09-23 13:54:07 -070045
46 {
47 SCOPED_TRACE("as system");
48 auto shot = screenshot();
49 shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
50 }
51
52 // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
53 // to receive them...we are expected to take care with the results.
chaviwd2432892020-07-24 17:42:39 -070054 DisplayCaptureArgs args;
55 args.displayToken = mDisplay;
56 args.captureSecureLayers = true;
57 ASSERT_EQ(NO_ERROR, composer->captureDisplay(args, mCaptureResults));
58 ASSERT_EQ(true, mCaptureResults.capturedSecureLayers);
59 ScreenCapture sc(mCaptureResults.buffer);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070060 sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
61}
62
63TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
64 sp<SurfaceControl> layer;
65 ASSERT_NO_FATAL_FAILURE(
66 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
67
68 Transaction().setTransformToDisplayInverse(layer, false).apply();
69
70 ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
71
72 Transaction().setTransformToDisplayInverse(layer, true).apply();
73}
74
75TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
76 sp<SurfaceControl> layer;
77 ASSERT_NO_FATAL_FAILURE(
78 layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
79
80 // verify this doesn't cause a crash
81 Transaction().setSidebandStream(layer, nullptr).apply();
82}
83
84TEST_F(LayerTransactionTest, ReparentToSelf) {
85 sp<SurfaceControl> layer;
86 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
87 ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
88 Transaction().reparent(layer, layer->getHandle()).apply();
89
90 {
91 // We expect the transaction to be silently dropped, but for SurfaceFlinger
92 // to still be functioning.
93 SCOPED_TRACE("after reparent to self");
94 const Rect rect(0, 0, 32, 32);
95 auto shot = screenshot();
96 shot->expectColor(rect, Color::RED);
97 shot->expectBorder(rect, Color::BLACK);
98 }
99}
100
101// This test ensures that when we drop an app buffer in SurfaceFlinger, we merge
102// the dropped buffer's damage region into the next buffer's damage region. If
103// we don't do this, we'll report an incorrect damage region to hardware
104// composer, resulting in broken rendering. This test checks the BufferQueue
105// case.
106//
107// Unfortunately, we don't currently have a way to inspect the damage region
108// SurfaceFlinger sends to hardware composer from a test, so this test requires
109// the dev to manually watch the device's screen during the test to spot broken
110// rendering. Because the results can't be automatically verified, this test is
111// marked disabled.
112TEST_F(LayerTransactionTest, DISABLED_BufferQueueLayerMergeDamageRegionWhenDroppingBuffers) {
113 const int width = mDisplayWidth;
114 const int height = mDisplayHeight;
115 sp<SurfaceControl> layer;
116 ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
117 const auto producer = layer->getIGraphicBufferProducer();
Peiyong Lind8460c82020-07-28 16:04:22 -0700118 const sp<IProducerListener> stubListener(new StubProducerListener);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700119 IGraphicBufferProducer::QueueBufferOutput queueBufferOutput;
Peiyong Lind8460c82020-07-28 16:04:22 -0700120 ASSERT_EQ(OK, producer->connect(stubListener, NATIVE_WINDOW_API_CPU, true, &queueBufferOutput));
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700121
122 std::map<int, sp<GraphicBuffer>> slotMap;
123 auto slotToBuffer = [&](int slot, sp<GraphicBuffer>* buf) {
124 ASSERT_NE(nullptr, buf);
125 const auto iter = slotMap.find(slot);
126 ASSERT_NE(slotMap.end(), iter);
127 *buf = iter->second;
128 };
129
130 auto dequeue = [&](int* outSlot) {
131 ASSERT_NE(nullptr, outSlot);
132 *outSlot = -1;
133 int slot;
134 sp<Fence> fence;
135 uint64_t age;
136 FrameEventHistoryDelta timestamps;
137 const status_t dequeueResult =
138 producer->dequeueBuffer(&slot, &fence, width, height, PIXEL_FORMAT_RGBA_8888,
139 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
140 &age, &timestamps);
141 if (dequeueResult == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
142 sp<GraphicBuffer> newBuf;
143 ASSERT_EQ(OK, producer->requestBuffer(slot, &newBuf));
144 ASSERT_NE(nullptr, newBuf.get());
145 slotMap[slot] = newBuf;
146 } else {
147 ASSERT_EQ(OK, dequeueResult);
148 }
149 *outSlot = slot;
150 };
151
152 auto queue = [&](int slot, const Region& damage, nsecs_t displayTime) {
153 IGraphicBufferProducer::QueueBufferInput input(
154 /*timestamp=*/displayTime, /*isAutoTimestamp=*/false, HAL_DATASPACE_UNKNOWN,
155 /*crop=*/Rect::EMPTY_RECT, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
156 /*transform=*/0, Fence::NO_FENCE);
157 input.setSurfaceDamage(damage);
158 IGraphicBufferProducer::QueueBufferOutput output;
159 ASSERT_EQ(OK, producer->queueBuffer(slot, input, &output));
160 };
161
162 auto fillAndPostBuffers = [&](const Color& color) {
163 int slot1;
164 ASSERT_NO_FATAL_FAILURE(dequeue(&slot1));
165 int slot2;
166 ASSERT_NO_FATAL_FAILURE(dequeue(&slot2));
167
168 sp<GraphicBuffer> buf1;
169 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot1, &buf1));
170 sp<GraphicBuffer> buf2;
171 ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot2, &buf2));
172 TransactionUtils::fillGraphicBufferColor(buf1, Rect(width, height), color);
173 TransactionUtils::fillGraphicBufferColor(buf2, Rect(width, height), color);
174
175 const auto displayTime = systemTime() + milliseconds_to_nanoseconds(100);
176 ASSERT_NO_FATAL_FAILURE(queue(slot1, Region::INVALID_REGION, displayTime));
177 ASSERT_NO_FATAL_FAILURE(
178 queue(slot2, Region(Rect(width / 3, height / 3, 2 * width / 3, 2 * height / 3)),
179 displayTime));
180 };
181
182 const auto startTime = systemTime();
183 const std::array<Color, 3> colors = {Color::RED, Color::GREEN, Color::BLUE};
184 int colorIndex = 0;
185 while (nanoseconds_to_seconds(systemTime() - startTime) < 10) {
186 ASSERT_NO_FATAL_FAILURE(fillAndPostBuffers(colors[colorIndex++ % colors.size()]));
187 std::this_thread::sleep_for(1s);
188 }
189
190 ASSERT_EQ(OK, producer->disconnect(NATIVE_WINDOW_API_CPU));
191}
192} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800193
194// TODO(b/129481165): remove the #pragma below and fix conversion issues
195#pragma clang diagnostic pop // ignored "-Wconversion"