single.cpp revision 878e45a7f16a9a68b322f7bdb369bf653a0f66cb
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
37using android::C2AllocatorIon;
38using android::C2PlatformAllocatorStore;
39using android::hardware::hidl_handle;
40using android::hardware::media::bufferpool::V1_0::IAccessor;
41using android::hardware::media::bufferpool::V1_0::ResultStatus;
42using android::hardware::media::bufferpool::V1_0::implementation::BufferId;
43using android::hardware::media::bufferpool::V1_0::implementation::ClientManager;
44using android::hardware::media::bufferpool::V1_0::implementation::ConnectionId;
45using android::hardware::media::bufferpool::V1_0::implementation::TransactionId;
46using android::hardware::media::bufferpool::BufferPoolData;
47
48namespace {
49
50// Number of iteration for buffer allocation test.
51constexpr static int kNumAllocationTest = 3;
52
53// Number of iteration for buffer recycling test.
54constexpr static int kNumRecycleTest = 3;
55
56// media.bufferpool test setup
57class BufferpoolSingleTest : public ::testing::Test {
58 public:
59  virtual void SetUp() override {
60    ResultStatus status;
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    status = mManager->getAccessor(mConnectionId, &mAccessor);
76    ASSERT_TRUE(status == ResultStatus::OK && (bool)mAccessor);
77
78    ConnectionId& receiverId = mReceiverId;
79    mManager->registerSender(
80        mAccessor,
81        [&status, &receiverId](ResultStatus hidlStatus, ConnectionId hidlId) {
82          status = hidlStatus;
83          receiverId = hidlId;
84        });
85    ASSERT_TRUE(status == ResultStatus::ALREADY_EXISTS &&
86                receiverId == mConnectionId);
87  }
88
89 protected:
90  static void description(const std::string& description) {
91    RecordProperty("description", description);
92  }
93
94  android::sp<ClientManager> mManager;
95  android::sp<IAccessor> mAccessor;
96  std::shared_ptr<BufferPoolAllocator> mAllocator;
97  ConnectionId mConnectionId;
98  ConnectionId mReceiverId;
99
100};
101
102// Buffer allocation test.
103// Check whether each buffer allocation is done successfully with
104// unique buffer id.
105TEST_F(BufferpoolSingleTest, AllocateBuffer) {
106  ResultStatus status;
107  std::vector<uint8_t> vecParams;
108  getVtsAllocatorParams(&vecParams);
109
110  std::shared_ptr<BufferPoolData> buffer[kNumAllocationTest];
111  for (int i = 0; i < kNumAllocationTest; ++i) {
112    status = mManager->allocate(mConnectionId, vecParams, &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.
125TEST_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    status = mManager->allocate(mConnectionId, vecParams, &buffer);
134    ASSERT_TRUE(status == ResultStatus::OK);
135    bid[i] = buffer->mId;
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.
145TEST_F(BufferpoolSingleTest, TransferBuffer) {
146  ResultStatus status;
147  std::vector<uint8_t> vecParams;
148  getVtsAllocatorParams(&vecParams);
149  std::shared_ptr<BufferPoolData> sbuffer, rbuffer;
150
151  TransactionId transactionId;
152  int64_t postUs;
153
154  status = mManager->allocate(mConnectionId, vecParams, &sbuffer);
155  ASSERT_TRUE(status == ResultStatus::OK);
156  status = mManager->postSend(mReceiverId, sbuffer, &transactionId, &postUs);
157  ASSERT_TRUE(status == ResultStatus::OK);
158  status = mManager->receive(mReceiverId, transactionId, sbuffer->mId, postUs,
159                             &rbuffer);
160  EXPECT_TRUE(status == ResultStatus::OK);
161}
162
163}  // anonymous namespace
164
165int main(int argc, char** argv) {
166  ::testing::InitGoogleTest(&argc, argv);
167  int status = RUN_ALL_TESTS();
168  LOG(INFO) << "Test result = " << status;
169  return status;
170}
171