Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 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 | #define LOG_TAG "BufferPoolConnection" |
| 17 | |
| 18 | #include "Accessor.h" |
| 19 | #include "AccessorImpl.h" |
| 20 | #include "Connection.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace hardware { |
| 24 | namespace media { |
| 25 | namespace bufferpool { |
| 26 | namespace V2_0 { |
| 27 | namespace implementation { |
| 28 | |
| 29 | void ConnectionDeathRecipient::add( |
| 30 | int64_t connectionId, |
| 31 | const sp<Accessor> &accessor) { |
| 32 | std::lock_guard<std::mutex> lock(mLock); |
| 33 | if (mAccessors.find(connectionId) == mAccessors.end()) { |
| 34 | mAccessors.insert(std::make_pair(connectionId, accessor)); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | void ConnectionDeathRecipient::remove(int64_t connectionId) { |
| 39 | std::lock_guard<std::mutex> lock(mLock); |
| 40 | mAccessors.erase(connectionId); |
| 41 | auto it = mConnectionToCookie.find(connectionId); |
| 42 | if (it != mConnectionToCookie.end()) { |
| 43 | uint64_t cookie = it->second; |
| 44 | mConnectionToCookie.erase(it); |
| 45 | auto cit = mCookieToConnections.find(cookie); |
| 46 | if (cit != mCookieToConnections.end()) { |
| 47 | cit->second.erase(connectionId); |
| 48 | if (cit->second.size() == 0) { |
| 49 | mCookieToConnections.erase(cit); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void ConnectionDeathRecipient::addCookieToConnection( |
| 56 | uint64_t cookie, |
| 57 | int64_t connectionId) { |
| 58 | std::lock_guard<std::mutex> lock(mLock); |
| 59 | if (mAccessors.find(connectionId) == mAccessors.end()) { |
| 60 | return; |
| 61 | } |
| 62 | mConnectionToCookie.insert(std::make_pair(connectionId, cookie)); |
| 63 | auto it = mCookieToConnections.find(cookie); |
| 64 | if (it != mCookieToConnections.end()) { |
| 65 | it->second.insert(connectionId); |
| 66 | } else { |
| 67 | mCookieToConnections.insert(std::make_pair( |
| 68 | cookie, std::set<int64_t>{connectionId})); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void ConnectionDeathRecipient::serviceDied( |
| 73 | uint64_t cookie, |
| 74 | const wp<::android::hidl::base::V1_0::IBase>& /* who */ |
| 75 | ) { |
| 76 | std::map<int64_t, const wp<Accessor>> connectionsToClose; |
| 77 | { |
| 78 | std::lock_guard<std::mutex> lock(mLock); |
| 79 | |
| 80 | auto it = mCookieToConnections.find(cookie); |
| 81 | if (it != mCookieToConnections.end()) { |
| 82 | for (auto conIt = it->second.begin(); conIt != it->second.end(); ++conIt) { |
| 83 | auto accessorIt = mAccessors.find(*conIt); |
| 84 | if (accessorIt != mAccessors.end()) { |
| 85 | connectionsToClose.insert(std::make_pair(*conIt, accessorIt->second)); |
| 86 | mAccessors.erase(accessorIt); |
| 87 | } |
| 88 | mConnectionToCookie.erase(*conIt); |
| 89 | } |
| 90 | mCookieToConnections.erase(it); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (connectionsToClose.size() > 0) { |
| 95 | sp<Accessor> accessor; |
| 96 | for (auto it = connectionsToClose.begin(); it != connectionsToClose.end(); ++it) { |
| 97 | accessor = it->second.promote(); |
| 98 | |
| 99 | if (accessor) { |
| 100 | accessor->close(it->first); |
| 101 | ALOGD("connection %lld closed on death", (long long)it->first); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | namespace { |
| 108 | static sp<ConnectionDeathRecipient> sConnectionDeathRecipient = |
| 109 | new ConnectionDeathRecipient(); |
| 110 | } |
| 111 | |
| 112 | sp<ConnectionDeathRecipient> Accessor::getConnectionDeathRecipient() { |
| 113 | return sConnectionDeathRecipient; |
| 114 | } |
| 115 | |
Sungtak Lee | d312838 | 2018-11-07 17:30:37 -0800 | [diff] [blame] | 116 | void Accessor::createInvalidator() { |
| 117 | Accessor::Impl::createInvalidator(); |
| 118 | } |
| 119 | |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 120 | // Methods from ::android::hardware::media::bufferpool::V2_0::IAccessor follow. |
Sungtak Lee | d491f1f | 2018-10-05 15:56:56 -0700 | [diff] [blame] | 121 | Return<void> Accessor::connect( |
| 122 | const sp<::android::hardware::media::bufferpool::V2_0::IObserver>& observer, |
| 123 | connect_cb _hidl_cb) { |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 124 | sp<Connection> connection; |
| 125 | ConnectionId connectionId; |
Sungtak Lee | c7f9e2c | 2018-09-14 16:23:40 -0700 | [diff] [blame] | 126 | uint32_t msgId; |
Sungtak Lee | 1cb0ccb | 2018-09-05 14:47:36 -0700 | [diff] [blame] | 127 | const StatusDescriptor* fmqDesc; |
Sungtak Lee | c7f9e2c | 2018-09-14 16:23:40 -0700 | [diff] [blame] | 128 | const InvalidationDescriptor* invDesc; |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 129 | |
Sungtak Lee | c7f9e2c | 2018-09-14 16:23:40 -0700 | [diff] [blame] | 130 | ResultStatus status = connect( |
| 131 | observer, false, &connection, &connectionId, &msgId, &fmqDesc, &invDesc); |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 132 | if (status == ResultStatus::OK) { |
Sungtak Lee | c7f9e2c | 2018-09-14 16:23:40 -0700 | [diff] [blame] | 133 | _hidl_cb(status, connection, connectionId, msgId, *fmqDesc, *invDesc); |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 134 | } else { |
Sungtak Lee | c7f9e2c | 2018-09-14 16:23:40 -0700 | [diff] [blame] | 135 | _hidl_cb(status, nullptr, -1LL, 0, |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 136 | android::hardware::MQDescriptorSync<BufferStatusMessage>( |
| 137 | std::vector<android::hardware::GrantorDescriptor>(), |
Sungtak Lee | 1cb0ccb | 2018-09-05 14:47:36 -0700 | [diff] [blame] | 138 | nullptr /* nhandle */, 0 /* size */), |
Sungtak Lee | ae72946 | 2018-09-28 13:15:48 -0700 | [diff] [blame] | 139 | android::hardware::MQDescriptorUnsync<BufferInvalidationMessage>( |
Sungtak Lee | 1cb0ccb | 2018-09-05 14:47:36 -0700 | [diff] [blame] | 140 | std::vector<android::hardware::GrantorDescriptor>(), |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 141 | nullptr /* nhandle */, 0 /* size */)); |
| 142 | } |
| 143 | return Void(); |
| 144 | } |
| 145 | |
| 146 | Accessor::Accessor(const std::shared_ptr<BufferPoolAllocator> &allocator) |
| 147 | : mImpl(new Impl(allocator)) {} |
| 148 | |
| 149 | Accessor::~Accessor() { |
| 150 | } |
| 151 | |
| 152 | bool Accessor::isValid() { |
Sungtak Lee | c7f9e2c | 2018-09-14 16:23:40 -0700 | [diff] [blame] | 153 | return (bool)mImpl && mImpl->isValid(); |
| 154 | } |
| 155 | |
| 156 | ResultStatus Accessor::flush() { |
| 157 | if (mImpl) { |
| 158 | mImpl->flush(); |
| 159 | return ResultStatus::OK; |
| 160 | } |
| 161 | return ResultStatus::CRITICAL_ERROR; |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | ResultStatus Accessor::allocate( |
| 165 | ConnectionId connectionId, |
| 166 | const std::vector<uint8_t> ¶ms, |
| 167 | BufferId *bufferId, const native_handle_t** handle) { |
| 168 | if (mImpl) { |
| 169 | return mImpl->allocate(connectionId, params, bufferId, handle); |
| 170 | } |
| 171 | return ResultStatus::CRITICAL_ERROR; |
| 172 | } |
| 173 | |
| 174 | ResultStatus Accessor::fetch( |
| 175 | ConnectionId connectionId, TransactionId transactionId, |
| 176 | BufferId bufferId, const native_handle_t** handle) { |
| 177 | if (mImpl) { |
| 178 | return mImpl->fetch(connectionId, transactionId, bufferId, handle); |
| 179 | } |
| 180 | return ResultStatus::CRITICAL_ERROR; |
| 181 | } |
| 182 | |
| 183 | ResultStatus Accessor::connect( |
Sungtak Lee | c7f9e2c | 2018-09-14 16:23:40 -0700 | [diff] [blame] | 184 | const sp<IObserver> &observer, bool local, |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 185 | sp<Connection> *connection, ConnectionId *pConnectionId, |
Sungtak Lee | c7f9e2c | 2018-09-14 16:23:40 -0700 | [diff] [blame] | 186 | uint32_t *pMsgId, |
| 187 | const StatusDescriptor** statusDescPtr, |
| 188 | const InvalidationDescriptor** invDescPtr) { |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 189 | if (mImpl) { |
Sungtak Lee | c7f9e2c | 2018-09-14 16:23:40 -0700 | [diff] [blame] | 190 | ResultStatus status = mImpl->connect( |
| 191 | this, observer, connection, pConnectionId, pMsgId, |
| 192 | statusDescPtr, invDescPtr); |
Sungtak Lee | bbe37b6 | 2018-08-29 15:15:48 -0700 | [diff] [blame] | 193 | if (!local && status == ResultStatus::OK) { |
| 194 | sp<Accessor> accessor(this); |
| 195 | sConnectionDeathRecipient->add(*pConnectionId, accessor); |
| 196 | } |
| 197 | return status; |
| 198 | } |
| 199 | return ResultStatus::CRITICAL_ERROR; |
| 200 | } |
| 201 | |
| 202 | ResultStatus Accessor::close(ConnectionId connectionId) { |
| 203 | if (mImpl) { |
| 204 | ResultStatus status = mImpl->close(connectionId); |
| 205 | sConnectionDeathRecipient->remove(connectionId); |
| 206 | return status; |
| 207 | } |
| 208 | return ResultStatus::CRITICAL_ERROR; |
| 209 | } |
| 210 | |
| 211 | void Accessor::cleanUp(bool clearCache) { |
| 212 | if (mImpl) { |
| 213 | mImpl->cleanUp(clearCache); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | //IAccessor* HIDL_FETCH_IAccessor(const char* /* name */) { |
| 218 | // return new Accessor(); |
| 219 | //} |
| 220 | |
| 221 | } // namespace implementation |
| 222 | } // namespace V2_0 |
| 223 | } // namespace bufferpool |
| 224 | } // namespace media |
| 225 | } // namespace hardware |
| 226 | } // namespace android |