1 /*
2 * Copyright (C) 2022 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 #define LOG_TAG "AidlBufferPoolCon"
17 //#define LOG_NDEBUG 0
18
19 #include <aidlcommonsupport/NativeHandle.h>
20
21 #include "Connection.h"
22 #include "Accessor.h"
23
24 namespace aidl::android::hardware::media::bufferpool2::implementation {
25
26 using aidl::android::hardware::media::bufferpool2::ResultStatus;
27 using Buffer = aidl::android::hardware::media::bufferpool2::Buffer;
28 using FetchInfo = aidl::android::hardware::media::bufferpool2::IConnection::FetchInfo;
29 using FetchResult = aidl::android::hardware::media::bufferpool2::IConnection::FetchResult;
30
fetch(const std::vector<FetchInfo> & in_fetchInfos,std::vector<FetchResult> * _aidl_return)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;
35 if (mInitialized && mAccessor) {
36 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++;
41 }
42 }
43 if (failure > 0) {
44 ALOGD("total fetch %d, failure %d", success + failure, failure);
45 }
46 return ::ndk::ScopedAStatus::ok();
47 }
48 return ::ndk::ScopedAStatus::fromServiceSpecificError(ResultStatus::CRITICAL_ERROR);
49 }
50
sync()51 ::ndk::ScopedAStatus Connection::sync() {
52 if (mInitialized && mAccessor) {
53 mAccessor->cleanUp(false);
54 }
55 return ::ndk::ScopedAStatus::ok();
56 }
57
58
fetch(TransactionId transactionId,BufferId bufferId,std::vector<FetchResult> * result)59 bool 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;
73 }
74
Connection()75 Connection::Connection() : mInitialized(false), mConnectionId(-1LL) {}
76
~Connection()77 Connection::~Connection() {
78 if (mInitialized && mAccessor) {
79 mAccessor->close(mConnectionId);
80 }
81 }
82
initialize(const std::shared_ptr<Accessor> & accessor,ConnectionId connectionId)83 void Connection::initialize(
84 const std::shared_ptr<Accessor>& accessor, ConnectionId connectionId) {
85 if (!mInitialized) {
86 mAccessor = accessor;
87 mConnectionId = connectionId;
88 mInitialized = true;
89 }
90 }
91
flush()92 BufferPoolStatus Connection::flush() {
93 if (mInitialized && mAccessor) {
94 return mAccessor->flush();
95 }
96 return ResultStatus::CRITICAL_ERROR;
97 }
98
allocate(const std::vector<uint8_t> & params,BufferId * bufferId,const native_handle_t ** handle)99 BufferPoolStatus Connection::allocate(
100 const std::vector<uint8_t> ¶ms, 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
cleanUp(bool clearCache)108 void Connection::cleanUp(bool clearCache) {
109 if (mInitialized && mAccessor) {
110 mAccessor->cleanUp(clearCache);
111 }
112 }
113
114 } // namespace ::aidl::android::hardware::media::bufferpool2::implementation
115