blob: 53d350dcf7b594d04c66a6fe57cdd2802974c5fb [file] [log] [blame]
Sungtak Lee97e1dfb2022-12-07 07:45:45 +00001/*
Sungtak Lee8878a132022-12-07 11:42:03 +00002 * Copyright (C) 2022 The Android Open Source Project
Sungtak Lee97e1dfb2022-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 */
Sungtak Lee8878a132022-12-07 11:42:03 +000016#define LOG_TAG "AidlBufferPoolCon"
17//#define LOG_NDEBUG 0
18
19#include <aidlcommonsupport/NativeHandle.h>
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000020
21#include "Connection.h"
Sungtak Lee8878a132022-12-07 11:42:03 +000022#include "Accessor.h"
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000023
Sungtak Lee8878a132022-12-07 11:42:03 +000024namespace aidl::android::hardware::media::bufferpool2::implementation {
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000025
Sungtak Lee8878a132022-12-07 11:42:03 +000026using aidl::android::hardware::media::bufferpool2::ResultStatus;
27using Buffer = aidl::android::hardware::media::bufferpool2::Buffer;
28using FetchInfo = aidl::android::hardware::media::bufferpool2::IConnection::FetchInfo;
29using FetchResult = aidl::android::hardware::media::bufferpool2::IConnection::FetchResult;
30
31::ndk::ScopedAStatus Connection::fetch(const std::vector<FetchInfo>& in_fetchInfos,
32 std::vector<FetchResult>* _aidl_return) {
33 int success = 0;
34 int failure = 0;
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000035 if (mInitialized && mAccessor) {
Sungtak Lee8878a132022-12-07 11:42:03 +000036 for (auto it = in_fetchInfos.begin(); it != in_fetchInfos.end(); ++it) {
37 if (fetch(it->transactionId, it->bufferId, _aidl_return)) {
38 success++;
39 } else {
40 failure++;
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000041 }
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000042 }
Sungtak Lee8878a132022-12-07 11:42:03 +000043 if (failure > 0) {
44 ALOGD("total fetch %d, failure %d", success + failure, failure);
45 }
46 return ::ndk::ScopedAStatus::ok();
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000047 }
Sungtak Lee8878a132022-12-07 11:42:03 +000048 return ::ndk::ScopedAStatus::fromServiceSpecificError(ResultStatus::CRITICAL_ERROR);
49}
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000050
Sungtak Lee8878a132022-12-07 11:42:03 +000051::ndk::ScopedAStatus Connection::sync() {
52 if (mInitialized && mAccessor) {
53 mAccessor->cleanUp(false);
54 }
55 return ::ndk::ScopedAStatus::ok();
56}
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000057
Sungtak Lee8878a132022-12-07 11:42:03 +000058
59bool Connection::fetch(TransactionId transactionId, BufferId bufferId,
60 std::vector<FetchResult> *result) {
61 BufferPoolStatus status = ResultStatus::CRITICAL_ERROR;
62 const native_handle_t *handle = nullptr;
63 status = mAccessor->fetch(
64 mConnectionId, transactionId, bufferId, &handle);
65 if (status == ResultStatus::OK) {
66 result->emplace_back(FetchResult::make<FetchResult::buffer>());
67 result->back().get<FetchResult::buffer>().id = bufferId;
68 result->back().get<FetchResult::buffer>().buffer = ::android::dupToAidl(handle);
69 return true;
70 }
71 result->emplace_back(FetchResult::make<FetchResult::failure>(status));
72 return false;
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000073}
74
75Connection::Connection() : mInitialized(false), mConnectionId(-1LL) {}
76
77Connection::~Connection() {
78 if (mInitialized && mAccessor) {
79 mAccessor->close(mConnectionId);
80 }
81}
82
83void Connection::initialize(
Sungtak Lee8878a132022-12-07 11:42:03 +000084 const std::shared_ptr<Accessor>& accessor, ConnectionId connectionId) {
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000085 if (!mInitialized) {
86 mAccessor = accessor;
87 mConnectionId = connectionId;
88 mInitialized = true;
89 }
90}
91
Sungtak Lee8878a132022-12-07 11:42:03 +000092BufferPoolStatus Connection::flush() {
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000093 if (mInitialized && mAccessor) {
94 return mAccessor->flush();
95 }
96 return ResultStatus::CRITICAL_ERROR;
97}
98
Sungtak Lee8878a132022-12-07 11:42:03 +000099BufferPoolStatus Connection::allocate(
Sungtak Lee97e1dfb2022-12-07 07:45:45 +0000100 const std::vector<uint8_t> &params, BufferId *bufferId,
101 const native_handle_t **handle) {
102 if (mInitialized && mAccessor) {
103 return mAccessor->allocate(mConnectionId, params, bufferId, handle);
104 }
105 return ResultStatus::CRITICAL_ERROR;
106}
107
108void Connection::cleanUp(bool clearCache) {
109 if (mInitialized && mAccessor) {
110 mAccessor->cleanUp(clearCache);
111 }
112}
113
Sungtak Lee8878a132022-12-07 11:42:03 +0000114} // namespace ::aidl::android::hardware::media::bufferpool2::implementation