blob: 66aa5e931cf8678a1861d32548db22a8ceb309df [file] [log] [blame]
Sungtak Lee8fc3ca42022-12-07 07:45:45 +00001/*
Sungtak Lee76937c62022-12-07 11:42:03 +00002 * Copyright (C) 2022 The Android Open Source Project
Sungtak Lee8fc3ca42022-12-07 07:45:45 +00003 *
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>
Sungtak Lee76937c62022-12-07 11:42:03 +000023#include <bufferpool2/ClientManager.h>
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000024#include <unistd.h>
25#include <iostream>
26#include <memory>
27#include <vector>
28#include "allocator.h"
29
Sungtak Lee76937c62022-12-07 11:42:03 +000030using aidl::android::hardware::media::bufferpool2::implementation::BufferId;
31using aidl::android::hardware::media::bufferpool2::implementation::BufferPoolStatus;
32using aidl::android::hardware::media::bufferpool2::implementation::ClientManager;
33using aidl::android::hardware::media::bufferpool2::implementation::ConnectionId;
34using aidl::android::hardware::media::bufferpool2::implementation::TransactionId;
35using aidl::android::hardware::media::bufferpool2::BufferPoolData;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000036
37namespace {
38
39// Number of iteration for buffer allocation test.
40constexpr static int kNumAllocationTest = 3;
41
42// Number of iteration for buffer recycling test.
43constexpr static int kNumRecycleTest = 3;
44
45// media.bufferpool test setup
46class BufferpoolSingleTest : public ::testing::Test {
47 public:
48 virtual void SetUp() override {
Sungtak Lee76937c62022-12-07 11:42:03 +000049 BufferPoolStatus status;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000050 mConnectionValid = false;
51
52 mManager = ClientManager::getInstance();
53 ASSERT_NE(mManager, nullptr);
54
55 mAllocator = std::make_shared<TestBufferPoolAllocator>();
56 ASSERT_TRUE((bool)mAllocator);
57
58 status = mManager->create(mAllocator, &mConnectionId);
59 ASSERT_TRUE(status == ResultStatus::OK);
60
61 mConnectionValid = true;
62
Sungtak Lee76937c62022-12-07 11:42:03 +000063 bool isNew = true;
64 status = mManager->registerSender(mManager, mConnectionId, &mReceiverId, &isNew);
65 ASSERT_TRUE(status == ResultStatus::OK && isNew == false &&
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000066 mReceiverId == mConnectionId);
67 }
68
69 virtual void TearDown() override {
70 if (mConnectionValid) {
71 mManager->close(mConnectionId);
72 }
73 }
74
75 protected:
76 static void description(const std::string& description) {
77 RecordProperty("description", description);
78 }
79
Sungtak Lee76937c62022-12-07 11:42:03 +000080 std::shared_ptr<ClientManager> mManager;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000081 std::shared_ptr<BufferPoolAllocator> mAllocator;
82 bool mConnectionValid;
83 ConnectionId mConnectionId;
84 ConnectionId mReceiverId;
85
86};
87
88// Buffer allocation test.
89// Check whether each buffer allocation is done successfully with
90// unique buffer id.
91TEST_F(BufferpoolSingleTest, AllocateBuffer) {
Sungtak Lee76937c62022-12-07 11:42:03 +000092 BufferPoolStatus status;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000093 std::vector<uint8_t> vecParams;
94 getTestAllocatorParams(&vecParams);
95
96 std::shared_ptr<BufferPoolData> buffer[kNumAllocationTest];
97 native_handle_t *allocHandle = nullptr;
98 for (int i = 0; i < kNumAllocationTest; ++i) {
99 status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &buffer[i]);
100 ASSERT_TRUE(status == ResultStatus::OK);
101 if (allocHandle) {
102 native_handle_close(allocHandle);
103 native_handle_delete(allocHandle);
104 }
105 }
106 for (int i = 0; i < kNumAllocationTest; ++i) {
107 for (int j = i + 1; j < kNumAllocationTest; ++j) {
108 ASSERT_TRUE(buffer[i]->mId != buffer[j]->mId);
109 }
110 }
111 EXPECT_TRUE(kNumAllocationTest > 1);
112}
113
114// Buffer recycle test.
115// Check whether de-allocated buffers are recycled.
116TEST_F(BufferpoolSingleTest, RecycleBuffer) {
Sungtak Lee76937c62022-12-07 11:42:03 +0000117 BufferPoolStatus status;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000118 std::vector<uint8_t> vecParams;
119 getTestAllocatorParams(&vecParams);
120
121 BufferId bid[kNumRecycleTest];
122 for (int i = 0; i < kNumRecycleTest; ++i) {
123 std::shared_ptr<BufferPoolData> buffer;
124 native_handle_t *allocHandle = nullptr;
125 status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &buffer);
126 ASSERT_TRUE(status == ResultStatus::OK);
127 bid[i] = buffer->mId;
128 if (allocHandle) {
129 native_handle_close(allocHandle);
130 native_handle_delete(allocHandle);
131 }
132 }
133 for (int i = 1; i < kNumRecycleTest; ++i) {
134 ASSERT_TRUE(bid[i - 1] == bid[i]);
135 }
136 EXPECT_TRUE(kNumRecycleTest > 1);
137}
138
139// Buffer transfer test.
140// Check whether buffer is transferred to another client successfully.
141TEST_F(BufferpoolSingleTest, TransferBuffer) {
Sungtak Lee76937c62022-12-07 11:42:03 +0000142 BufferPoolStatus status;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000143 std::vector<uint8_t> vecParams;
144 getTestAllocatorParams(&vecParams);
145 std::shared_ptr<BufferPoolData> sbuffer, rbuffer;
146 native_handle_t *allocHandle = nullptr;
147 native_handle_t *recvHandle = nullptr;
148
149 TransactionId transactionId;
Sungtak Lee76937c62022-12-07 11:42:03 +0000150 int64_t postMs;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000151
152 status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &sbuffer);
153 ASSERT_TRUE(status == ResultStatus::OK);
154 ASSERT_TRUE(TestBufferPoolAllocator::Fill(allocHandle, 0x77));
Sungtak Lee76937c62022-12-07 11:42:03 +0000155 status = mManager->postSend(mReceiverId, sbuffer, &transactionId, &postMs);
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000156 ASSERT_TRUE(status == ResultStatus::OK);
Sungtak Lee76937c62022-12-07 11:42:03 +0000157 status = mManager->receive(mReceiverId, transactionId, sbuffer->mId, postMs,
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000158 &recvHandle, &rbuffer);
159 EXPECT_TRUE(status == ResultStatus::OK);
160 ASSERT_TRUE(TestBufferPoolAllocator::Verify(recvHandle, 0x77));
161
162 if (allocHandle) {
163 native_handle_close(allocHandle);
164 native_handle_delete(allocHandle);
165 }
166 if (recvHandle) {
167 native_handle_close(recvHandle);
168 native_handle_delete(recvHandle);
169 }
170}
171
172} // anonymous namespace
173
174int main(int argc, char** argv) {
175 ::testing::InitGoogleTest(&argc, argv);
176 int status = RUN_ALL_TESTS();
177 LOG(INFO) << "Test result = " << status;
178 return status;
179}