Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #define LOG_TAG "BufferItemConsumer_test" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 20 | #include <gmock/gmock.h> |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | #include <gui/BufferItemConsumer.h> |
| 23 | #include <gui/IProducerListener.h> |
| 24 | #include <gui/Surface.h> |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 25 | #include <ui/GraphicBuffer.h> |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | static constexpr int kWidth = 100; |
| 30 | static constexpr int kHeight = 100; |
| 31 | static constexpr int kMaxLockedBuffers = 3; |
| 32 | static constexpr int kFormat = HAL_PIXEL_FORMAT_RGBA_8888; |
| 33 | static constexpr int kFrameSleepUs = 30 * 1000; |
| 34 | |
| 35 | class BufferItemConsumerTest : public ::testing::Test { |
| 36 | protected: |
| 37 | struct BufferFreedListener |
| 38 | : public BufferItemConsumer::BufferFreedListener { |
| 39 | explicit BufferFreedListener(BufferItemConsumerTest* test) |
| 40 | : mTest(test) {} |
| 41 | void onBufferFreed(const wp<GraphicBuffer>& /* gBuffer */) override { |
| 42 | mTest->HandleBufferFreed(); |
| 43 | } |
| 44 | BufferItemConsumerTest* mTest; |
| 45 | }; |
| 46 | |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 47 | struct TrackingProducerListener : public BnProducerListener { |
| 48 | TrackingProducerListener(BufferItemConsumerTest* test) : mTest(test) {} |
| 49 | |
| 50 | virtual void onBufferReleased() override {} |
| 51 | virtual bool needsReleaseNotify() override { return true; } |
| 52 | virtual void onBuffersDiscarded(const std::vector<int32_t>&) override {} |
| 53 | virtual void onBufferDetached(int slot) override { mTest->HandleBufferDetached(slot); } |
| 54 | |
| 55 | BufferItemConsumerTest* mTest; |
| 56 | }; |
| 57 | |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 58 | void SetUp() override { |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 59 | mBIC = new BufferItemConsumer(kFormat, kMaxLockedBuffers, true); |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 60 | String8 name("BufferItemConsumer_Under_Test"); |
| 61 | mBIC->setName(name); |
| 62 | mBFL = new BufferFreedListener(this); |
| 63 | mBIC->setBufferFreedListener(mBFL); |
| 64 | |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 65 | sp<IProducerListener> producerListener = new TrackingProducerListener(this); |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 66 | mProducer = mBIC->getSurface()->getIGraphicBufferProducer(); |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 67 | IGraphicBufferProducer::QueueBufferOutput bufferOutput; |
| 68 | ASSERT_EQ(NO_ERROR, |
| 69 | mProducer->connect(producerListener, NATIVE_WINDOW_API_CPU, |
| 70 | true, &bufferOutput)); |
| 71 | ASSERT_EQ(NO_ERROR, |
| 72 | mProducer->setMaxDequeuedBufferCount(kMaxLockedBuffers)); |
| 73 | } |
| 74 | |
| 75 | int GetFreedBufferCount() { |
| 76 | std::lock_guard<std::mutex> lock(mMutex); |
| 77 | return mFreedBufferCount; |
| 78 | } |
| 79 | |
| 80 | void HandleBufferFreed() { |
| 81 | std::lock_guard<std::mutex> lock(mMutex); |
| 82 | mFreedBufferCount++; |
Nolan Scobie | f50aebc | 2023-04-13 17:49:18 +0000 | [diff] [blame] | 83 | ALOGD("HandleBufferFreed, mFreedBufferCount=%d", mFreedBufferCount); |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 86 | void HandleBufferDetached(int slot) { |
| 87 | std::lock_guard<std::mutex> lock(mMutex); |
| 88 | mDetachedBufferSlots.push_back(slot); |
| 89 | ALOGD("HandleBufferDetached, slot=%d mDetachedBufferSlots-count=%zu", slot, |
| 90 | mDetachedBufferSlots.size()); |
| 91 | } |
| 92 | |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 93 | void DequeueBuffer(int* outSlot) { |
| 94 | ASSERT_NE(outSlot, nullptr); |
| 95 | |
| 96 | int slot; |
| 97 | sp<Fence> outFence; |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 98 | status_t ret = mProducer->dequeueBuffer(&slot, &outFence, kWidth, kHeight, 0, 0, |
| 99 | nullptr, nullptr); |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 100 | ASSERT_GE(ret, 0); |
| 101 | |
Nolan Scobie | f50aebc | 2023-04-13 17:49:18 +0000 | [diff] [blame] | 102 | ALOGD("dequeueBuffer: slot=%d", slot); |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 103 | if (ret & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) { |
| 104 | ret = mProducer->requestBuffer(slot, &mBuffers[slot]); |
| 105 | ASSERT_EQ(NO_ERROR, ret); |
| 106 | } |
| 107 | *outSlot = slot; |
| 108 | } |
| 109 | |
| 110 | void QueueBuffer(int slot) { |
Nolan Scobie | f50aebc | 2023-04-13 17:49:18 +0000 | [diff] [blame] | 111 | ALOGD("enqueueBuffer: slot=%d", slot); |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 112 | IGraphicBufferProducer::QueueBufferInput bufferInput( |
| 113 | 0ULL, true, HAL_DATASPACE_UNKNOWN, Rect::INVALID_RECT, |
| 114 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
| 115 | IGraphicBufferProducer::QueueBufferOutput bufferOutput; |
| 116 | status_t ret = mProducer->queueBuffer(slot, bufferInput, &bufferOutput); |
| 117 | ASSERT_EQ(NO_ERROR, ret); |
| 118 | } |
| 119 | |
| 120 | void AcquireBuffer(int* outSlot) { |
| 121 | ASSERT_NE(outSlot, nullptr); |
| 122 | BufferItem buffer; |
| 123 | status_t ret = mBIC->acquireBuffer(&buffer, 0, false); |
| 124 | ASSERT_EQ(NO_ERROR, ret); |
| 125 | |
Nolan Scobie | f50aebc | 2023-04-13 17:49:18 +0000 | [diff] [blame] | 126 | ALOGD("acquireBuffer: slot=%d", buffer.mSlot); |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 127 | *outSlot = buffer.mSlot; |
| 128 | } |
| 129 | |
| 130 | void ReleaseBuffer(int slot) { |
Nolan Scobie | f50aebc | 2023-04-13 17:49:18 +0000 | [diff] [blame] | 131 | ALOGD("releaseBuffer: slot=%d", slot); |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 132 | BufferItem buffer; |
| 133 | buffer.mSlot = slot; |
| 134 | buffer.mGraphicBuffer = mBuffers[slot]; |
| 135 | status_t ret = mBIC->releaseBuffer(buffer, Fence::NO_FENCE); |
| 136 | ASSERT_EQ(NO_ERROR, ret); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | std::mutex mMutex; |
| 141 | int mFreedBufferCount{0}; |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 142 | std::vector<int> mDetachedBufferSlots = {}; |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 143 | |
| 144 | sp<BufferItemConsumer> mBIC; |
| 145 | sp<BufferFreedListener> mBFL; |
| 146 | sp<IGraphicBufferProducer> mProducer; |
| 147 | sp<IGraphicBufferConsumer> mConsumer; |
| 148 | sp<GraphicBuffer> mBuffers[BufferQueueDefs::NUM_BUFFER_SLOTS]; |
| 149 | }; |
| 150 | |
| 151 | // Test that detaching buffer from consumer side triggers onBufferFreed. |
| 152 | TEST_F(BufferItemConsumerTest, TriggerBufferFreed_DetachBufferFromConsumer) { |
| 153 | int slot; |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 154 | // Producer: generate a placeholder buffer. |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 155 | DequeueBuffer(&slot); |
| 156 | QueueBuffer(slot); |
| 157 | |
| 158 | ASSERT_EQ(0, GetFreedBufferCount()); |
| 159 | // Consumer: acquire the buffer and then detach it. |
| 160 | AcquireBuffer(&slot); |
| 161 | status_t ret = mBIC->detachBuffer(slot); |
| 162 | ASSERT_EQ(NO_ERROR, ret); |
| 163 | |
| 164 | // Sleep to give some time for callbacks to happen. |
| 165 | usleep(kFrameSleepUs); |
| 166 | ASSERT_EQ(1, GetFreedBufferCount()); |
| 167 | } |
| 168 | |
| 169 | // Test that detaching buffer from producer side triggers onBufferFreed. |
| 170 | TEST_F(BufferItemConsumerTest, TriggerBufferFreed_DetachBufferFromProducer) { |
| 171 | int slot; |
| 172 | // Let buffer go through the cycle at least once. |
| 173 | DequeueBuffer(&slot); |
| 174 | QueueBuffer(slot); |
| 175 | AcquireBuffer(&slot); |
| 176 | ReleaseBuffer(slot); |
| 177 | |
| 178 | ASSERT_EQ(0, GetFreedBufferCount()); |
| 179 | |
| 180 | // Producer: generate the buffer again. |
| 181 | DequeueBuffer(&slot); |
| 182 | |
| 183 | // Producer: detach the buffer. |
| 184 | status_t ret = mProducer->detachBuffer(slot); |
| 185 | ASSERT_EQ(NO_ERROR, ret); |
| 186 | |
| 187 | // Sleep to give some time for callbacks to happen. |
| 188 | usleep(kFrameSleepUs); |
| 189 | ASSERT_EQ(1, GetFreedBufferCount()); |
| 190 | } |
| 191 | |
| 192 | // Test that abandoning BufferItemConsumer triggers onBufferFreed. |
| 193 | TEST_F(BufferItemConsumerTest, TriggerBufferFreed_AbandonBufferItemConsumer) { |
| 194 | int slot; |
| 195 | // Let buffer go through the cycle at least once. |
| 196 | DequeueBuffer(&slot); |
| 197 | QueueBuffer(slot); |
| 198 | AcquireBuffer(&slot); |
| 199 | ReleaseBuffer(slot); |
| 200 | |
| 201 | // Abandon the BufferItemConsumer. |
| 202 | mBIC->abandon(); |
| 203 | |
| 204 | // Sleep to give some time for callbacks to happen. |
| 205 | usleep(kFrameSleepUs); |
| 206 | ASSERT_EQ(1, GetFreedBufferCount()); |
| 207 | } |
| 208 | |
| 209 | // Test that delete BufferItemConsumer triggers onBufferFreed. |
| 210 | TEST_F(BufferItemConsumerTest, TriggerBufferFreed_DeleteBufferItemConsumer) { |
| 211 | int slot; |
| 212 | // Let buffer go through the cycle at least once. |
| 213 | DequeueBuffer(&slot); |
| 214 | QueueBuffer(slot); |
| 215 | AcquireBuffer(&slot); |
| 216 | ReleaseBuffer(slot); |
| 217 | |
| 218 | // Delete the BufferItemConsumer. |
| 219 | mBIC.clear(); |
| 220 | |
| 221 | // Sleep to give some time for callbacks to happen. |
| 222 | usleep(kFrameSleepUs); |
| 223 | ASSERT_EQ(1, GetFreedBufferCount()); |
| 224 | } |
| 225 | |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 226 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS) |
| 227 | // Test that delete BufferItemConsumer triggers onBufferFreed. |
| 228 | TEST_F(BufferItemConsumerTest, DetachBufferWithBuffer) { |
| 229 | int slot; |
| 230 | // Let buffer go through the cycle at least once. |
| 231 | DequeueBuffer(&slot); |
| 232 | QueueBuffer(slot); |
| 233 | AcquireBuffer(&slot); |
| 234 | |
| 235 | sp<GraphicBuffer> buffer = mBuffers[slot]; |
| 236 | EXPECT_EQ(OK, mBIC->detachBuffer(buffer)); |
| 237 | EXPECT_THAT(mDetachedBufferSlots, testing::ElementsAre(slot)); |
| 238 | } |
| 239 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS) |
| 240 | |
Jiwen 'Steve' Cai | af7b2a1 | 2017-03-19 12:26:10 -0700 | [diff] [blame] | 241 | } // namespace android |