Sungtak Lee | 9ec3356 | 2018-09-04 15:22:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "buffferpool_unit_test" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | #include <binder/ProcessState.h> |
| 23 | #include <bufferpool/ClientManager.h> |
| 24 | #include <hidl/HidlSupport.h> |
| 25 | #include <hidl/HidlTransportSupport.h> |
| 26 | #include <hidl/LegacySupport.h> |
| 27 | #include <hidl/Status.h> |
| 28 | #include <unistd.h> |
| 29 | #include <iostream> |
| 30 | #include <memory> |
| 31 | #include <vector> |
| 32 | #include "allocator.h" |
| 33 | |
| 34 | using android::hardware::hidl_handle; |
| 35 | using android::hardware::media::bufferpool::V2_0::ResultStatus; |
| 36 | using android::hardware::media::bufferpool::V2_0::implementation::BufferId; |
| 37 | using android::hardware::media::bufferpool::V2_0::implementation::ClientManager; |
| 38 | using android::hardware::media::bufferpool::V2_0::implementation::ConnectionId; |
| 39 | using android::hardware::media::bufferpool::V2_0::implementation::TransactionId; |
| 40 | using android::hardware::media::bufferpool::BufferPoolData; |
| 41 | |
| 42 | namespace { |
| 43 | |
| 44 | // Number of iteration for buffer allocation test. |
| 45 | constexpr static int kNumAllocationTest = 3; |
| 46 | |
| 47 | // Number of iteration for buffer recycling test. |
| 48 | constexpr static int kNumRecycleTest = 3; |
| 49 | |
| 50 | // media.bufferpool test setup |
| 51 | class BufferpoolSingleTest : public ::testing::Test { |
| 52 | public: |
| 53 | virtual void SetUp() override { |
| 54 | ResultStatus status; |
| 55 | mConnectionValid = false; |
| 56 | |
| 57 | mManager = ClientManager::getInstance(); |
| 58 | ASSERT_NE(mManager, nullptr); |
| 59 | |
| 60 | mAllocator = std::make_shared<TestBufferPoolAllocator>(); |
| 61 | ASSERT_TRUE((bool)mAllocator); |
| 62 | |
| 63 | status = mManager->create(mAllocator, &mConnectionId); |
| 64 | ASSERT_TRUE(status == ResultStatus::OK); |
| 65 | |
| 66 | mConnectionValid = true; |
| 67 | |
| 68 | status = mManager->registerSender(mManager, mConnectionId, &mReceiverId); |
| 69 | ASSERT_TRUE(status == ResultStatus::ALREADY_EXISTS && |
| 70 | mReceiverId == mConnectionId); |
| 71 | } |
| 72 | |
| 73 | virtual void TearDown() override { |
| 74 | if (mConnectionValid) { |
| 75 | mManager->close(mConnectionId); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | protected: |
| 80 | static void description(const std::string& description) { |
| 81 | RecordProperty("description", description); |
| 82 | } |
| 83 | |
| 84 | android::sp<ClientManager> mManager; |
| 85 | std::shared_ptr<BufferPoolAllocator> mAllocator; |
| 86 | bool mConnectionValid; |
| 87 | ConnectionId mConnectionId; |
| 88 | ConnectionId mReceiverId; |
| 89 | |
| 90 | }; |
| 91 | |
| 92 | // Buffer allocation test. |
| 93 | // Check whether each buffer allocation is done successfully with |
| 94 | // unique buffer id. |
| 95 | TEST_F(BufferpoolSingleTest, AllocateBuffer) { |
| 96 | ResultStatus status; |
| 97 | std::vector<uint8_t> vecParams; |
| 98 | getTestAllocatorParams(&vecParams); |
| 99 | |
| 100 | std::shared_ptr<BufferPoolData> buffer[kNumAllocationTest]; |
| 101 | native_handle_t *allocHandle = nullptr; |
| 102 | for (int i = 0; i < kNumAllocationTest; ++i) { |
| 103 | status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &buffer[i]); |
| 104 | ASSERT_TRUE(status == ResultStatus::OK); |
Sungtak Lee | ec638f5 | 2020-10-26 23:54:43 -0700 | [diff] [blame] | 105 | if (allocHandle) { |
| 106 | native_handle_close(allocHandle); |
| 107 | native_handle_delete(allocHandle); |
| 108 | } |
Sungtak Lee | 9ec3356 | 2018-09-04 15:22:46 -0700 | [diff] [blame] | 109 | } |
| 110 | for (int i = 0; i < kNumAllocationTest; ++i) { |
| 111 | for (int j = i + 1; j < kNumAllocationTest; ++j) { |
| 112 | ASSERT_TRUE(buffer[i]->mId != buffer[j]->mId); |
| 113 | } |
| 114 | } |
| 115 | EXPECT_TRUE(kNumAllocationTest > 1); |
| 116 | } |
| 117 | |
| 118 | // Buffer recycle test. |
| 119 | // Check whether de-allocated buffers are recycled. |
| 120 | TEST_F(BufferpoolSingleTest, RecycleBuffer) { |
| 121 | ResultStatus status; |
| 122 | std::vector<uint8_t> vecParams; |
| 123 | getTestAllocatorParams(&vecParams); |
| 124 | |
| 125 | BufferId bid[kNumRecycleTest]; |
| 126 | for (int i = 0; i < kNumRecycleTest; ++i) { |
| 127 | std::shared_ptr<BufferPoolData> buffer; |
| 128 | native_handle_t *allocHandle = nullptr; |
| 129 | status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &buffer); |
| 130 | ASSERT_TRUE(status == ResultStatus::OK); |
| 131 | bid[i] = buffer->mId; |
Sungtak Lee | ec638f5 | 2020-10-26 23:54:43 -0700 | [diff] [blame] | 132 | if (allocHandle) { |
| 133 | native_handle_close(allocHandle); |
| 134 | native_handle_delete(allocHandle); |
| 135 | } |
Sungtak Lee | 9ec3356 | 2018-09-04 15:22:46 -0700 | [diff] [blame] | 136 | } |
| 137 | for (int i = 1; i < kNumRecycleTest; ++i) { |
| 138 | ASSERT_TRUE(bid[i - 1] == bid[i]); |
| 139 | } |
| 140 | EXPECT_TRUE(kNumRecycleTest > 1); |
| 141 | } |
| 142 | |
| 143 | // Buffer transfer test. |
| 144 | // Check whether buffer is transferred to another client successfully. |
| 145 | TEST_F(BufferpoolSingleTest, TransferBuffer) { |
| 146 | ResultStatus status; |
| 147 | std::vector<uint8_t> vecParams; |
| 148 | getTestAllocatorParams(&vecParams); |
| 149 | std::shared_ptr<BufferPoolData> sbuffer, rbuffer; |
| 150 | native_handle_t *allocHandle = nullptr; |
| 151 | native_handle_t *recvHandle = nullptr; |
| 152 | |
| 153 | TransactionId transactionId; |
| 154 | int64_t postUs; |
| 155 | |
| 156 | status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &sbuffer); |
| 157 | ASSERT_TRUE(status == ResultStatus::OK); |
Sungtak Lee | c34b9b7 | 2018-09-06 14:48:21 -0700 | [diff] [blame] | 158 | ASSERT_TRUE(TestBufferPoolAllocator::Fill(allocHandle, 0x77)); |
Sungtak Lee | 9ec3356 | 2018-09-04 15:22:46 -0700 | [diff] [blame] | 159 | status = mManager->postSend(mReceiverId, sbuffer, &transactionId, &postUs); |
| 160 | ASSERT_TRUE(status == ResultStatus::OK); |
| 161 | status = mManager->receive(mReceiverId, transactionId, sbuffer->mId, postUs, |
| 162 | &recvHandle, &rbuffer); |
| 163 | EXPECT_TRUE(status == ResultStatus::OK); |
Sungtak Lee | c34b9b7 | 2018-09-06 14:48:21 -0700 | [diff] [blame] | 164 | ASSERT_TRUE(TestBufferPoolAllocator::Verify(recvHandle, 0x77)); |
Sungtak Lee | ec638f5 | 2020-10-26 23:54:43 -0700 | [diff] [blame] | 165 | |
| 166 | if (allocHandle) { |
| 167 | native_handle_close(allocHandle); |
| 168 | native_handle_delete(allocHandle); |
| 169 | } |
| 170 | if (recvHandle) { |
| 171 | native_handle_close(recvHandle); |
| 172 | native_handle_delete(recvHandle); |
| 173 | } |
Sungtak Lee | 9ec3356 | 2018-09-04 15:22:46 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | } // anonymous namespace |
| 177 | |
| 178 | int main(int argc, char** argv) { |
| 179 | ::testing::InitGoogleTest(&argc, argv); |
| 180 | int status = RUN_ALL_TESTS(); |
| 181 | LOG(INFO) << "Test result = " << status; |
| 182 | return status; |
| 183 | } |