Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 1 | /* |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 2 | * Copyright (C) 2022 The Android Open Source Project |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 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 | */ |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 16 | #define LOG_TAG "AidlBufferPoolCon" |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | |
| 19 | #include <aidlcommonsupport/NativeHandle.h> |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 20 | |
| 21 | #include "Connection.h" |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 22 | #include "Accessor.h" |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 23 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 24 | namespace aidl::android::hardware::media::bufferpool2::implementation { |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 25 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 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 | |
| 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 Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 35 | if (mInitialized && mAccessor) { |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 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++; |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 41 | } |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 42 | } |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 43 | if (failure > 0) { |
| 44 | ALOGD("total fetch %d, failure %d", success + failure, failure); |
| 45 | } |
| 46 | return ::ndk::ScopedAStatus::ok(); |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 47 | } |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 48 | return ::ndk::ScopedAStatus::fromServiceSpecificError(ResultStatus::CRITICAL_ERROR); |
| 49 | } |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 50 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 51 | ::ndk::ScopedAStatus Connection::sync() { |
| 52 | if (mInitialized && mAccessor) { |
| 53 | mAccessor->cleanUp(false); |
| 54 | } |
| 55 | return ::ndk::ScopedAStatus::ok(); |
| 56 | } |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 57 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 58 | |
| 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; |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | Connection::Connection() : mInitialized(false), mConnectionId(-1LL) {} |
| 76 | |
| 77 | Connection::~Connection() { |
| 78 | if (mInitialized && mAccessor) { |
| 79 | mAccessor->close(mConnectionId); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void Connection::initialize( |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 84 | const std::shared_ptr<Accessor>& accessor, ConnectionId connectionId) { |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 85 | if (!mInitialized) { |
| 86 | mAccessor = accessor; |
| 87 | mConnectionId = connectionId; |
| 88 | mInitialized = true; |
| 89 | } |
| 90 | } |
| 91 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 92 | BufferPoolStatus Connection::flush() { |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 93 | if (mInitialized && mAccessor) { |
| 94 | return mAccessor->flush(); |
| 95 | } |
| 96 | return ResultStatus::CRITICAL_ERROR; |
| 97 | } |
| 98 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 99 | BufferPoolStatus Connection::allocate( |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 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 | |
| 108 | void Connection::cleanUp(bool clearCache) { |
| 109 | if (mInitialized && mAccessor) { |
| 110 | mAccessor->cleanUp(clearCache); |
| 111 | } |
| 112 | } |
| 113 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 114 | } // namespace ::aidl::android::hardware::media::bufferpool2::implementation |