Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 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 | #include <private/android_filesystem_config.h> |
| 18 | #include <thread> |
| 19 | #include "LayerTransactionTest.h" |
| 20 | |
| 21 | namespace android { |
| 22 | |
| 23 | using android::hardware::graphics::common::V1_1::BufferUsage; |
| 24 | |
| 25 | TEST_F(LayerTransactionTest, SetFlagsSecureEUidSystem) { |
| 26 | sp<SurfaceControl> layer; |
| 27 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 28 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32)); |
| 29 | |
| 30 | sp<ISurfaceComposer> composer = ComposerService::getComposerService(); |
| 31 | sp<GraphicBuffer> outBuffer; |
| 32 | Transaction() |
| 33 | .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure) |
| 34 | .apply(true); |
| 35 | ASSERT_EQ(PERMISSION_DENIED, |
| 36 | composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false)); |
| 37 | |
| 38 | UIDFaker f(AID_SYSTEM); |
| 39 | |
| 40 | // By default the system can capture screenshots with secure layers but they |
| 41 | // will be blacked out |
| 42 | ASSERT_EQ(NO_ERROR, composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false)); |
| 43 | |
| 44 | { |
| 45 | SCOPED_TRACE("as system"); |
| 46 | auto shot = screenshot(); |
| 47 | shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK); |
| 48 | } |
| 49 | |
| 50 | // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able |
| 51 | // to receive them...we are expected to take care with the results. |
| 52 | bool outCapturedSecureLayers; |
| 53 | ASSERT_EQ(NO_ERROR, |
| 54 | composer->captureScreen(mDisplay, &outBuffer, outCapturedSecureLayers, |
| 55 | ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(), 0, |
| 56 | 0, false, ISurfaceComposer::eRotateNone, true)); |
| 57 | ASSERT_EQ(true, outCapturedSecureLayers); |
| 58 | ScreenCapture sc(outBuffer); |
| 59 | sc.expectColor(Rect(0, 0, 32, 32), Color::RED); |
| 60 | } |
| 61 | |
| 62 | TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) { |
| 63 | sp<SurfaceControl> layer; |
| 64 | ASSERT_NO_FATAL_FAILURE( |
| 65 | layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState)); |
| 66 | |
| 67 | Transaction().setTransformToDisplayInverse(layer, false).apply(); |
| 68 | |
| 69 | ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32)); |
| 70 | |
| 71 | Transaction().setTransformToDisplayInverse(layer, true).apply(); |
| 72 | } |
| 73 | |
| 74 | TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) { |
| 75 | sp<SurfaceControl> layer; |
| 76 | ASSERT_NO_FATAL_FAILURE( |
| 77 | layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState)); |
| 78 | |
| 79 | // verify this doesn't cause a crash |
| 80 | Transaction().setSidebandStream(layer, nullptr).apply(); |
| 81 | } |
| 82 | |
| 83 | TEST_F(LayerTransactionTest, ReparentToSelf) { |
| 84 | sp<SurfaceControl> layer; |
| 85 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32)); |
| 86 | ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32)); |
| 87 | Transaction().reparent(layer, layer->getHandle()).apply(); |
| 88 | |
| 89 | { |
| 90 | // We expect the transaction to be silently dropped, but for SurfaceFlinger |
| 91 | // to still be functioning. |
| 92 | SCOPED_TRACE("after reparent to self"); |
| 93 | const Rect rect(0, 0, 32, 32); |
| 94 | auto shot = screenshot(); |
| 95 | shot->expectColor(rect, Color::RED); |
| 96 | shot->expectBorder(rect, Color::BLACK); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // This test ensures that when we drop an app buffer in SurfaceFlinger, we merge |
| 101 | // the dropped buffer's damage region into the next buffer's damage region. If |
| 102 | // we don't do this, we'll report an incorrect damage region to hardware |
| 103 | // composer, resulting in broken rendering. This test checks the BufferQueue |
| 104 | // case. |
| 105 | // |
| 106 | // Unfortunately, we don't currently have a way to inspect the damage region |
| 107 | // SurfaceFlinger sends to hardware composer from a test, so this test requires |
| 108 | // the dev to manually watch the device's screen during the test to spot broken |
| 109 | // rendering. Because the results can't be automatically verified, this test is |
| 110 | // marked disabled. |
| 111 | TEST_F(LayerTransactionTest, DISABLED_BufferQueueLayerMergeDamageRegionWhenDroppingBuffers) { |
| 112 | const int width = mDisplayWidth; |
| 113 | const int height = mDisplayHeight; |
| 114 | sp<SurfaceControl> layer; |
| 115 | ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height)); |
| 116 | const auto producer = layer->getIGraphicBufferProducer(); |
| 117 | const sp<IProducerListener> dummyListener(new DummyProducerListener); |
| 118 | IGraphicBufferProducer::QueueBufferOutput queueBufferOutput; |
| 119 | ASSERT_EQ(OK, |
| 120 | producer->connect(dummyListener, NATIVE_WINDOW_API_CPU, true, &queueBufferOutput)); |
| 121 | |
| 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, ×tamps); |
| 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 |