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