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