Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "ClientCache" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
| 22 | #include <cinttypes> |
| 23 | |
| 24 | #include "ClientCache.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
Robert Carr | beba6f0 | 2021-02-10 21:06:27 -0800 | [diff] [blame] | 28 | using base::StringAppendF; |
| 29 | |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 30 | ANDROID_SINGLETON_STATIC_INSTANCE(ClientCache); |
| 31 | |
| 32 | ClientCache::ClientCache() : mDeathRecipient(new CacheDeathRecipient) {} |
| 33 | |
| 34 | bool ClientCache::getBuffer(const client_cache_t& cacheId, |
| 35 | ClientCacheBuffer** outClientCacheBuffer) { |
| 36 | auto& [processToken, id] = cacheId; |
| 37 | if (processToken == nullptr) { |
| 38 | ALOGE("failed to get buffer, invalid (nullptr) process token"); |
| 39 | return false; |
| 40 | } |
| 41 | auto it = mBuffers.find(processToken); |
| 42 | if (it == mBuffers.end()) { |
| 43 | ALOGE("failed to get buffer, invalid process token"); |
| 44 | return false; |
| 45 | } |
| 46 | |
Valerie Hau | 90fcc94 | 2019-11-18 11:18:57 -0800 | [diff] [blame] | 47 | auto& processBuffers = it->second.second; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 48 | |
| 49 | auto bufItr = processBuffers.find(id); |
| 50 | if (bufItr == processBuffers.end()) { |
Vishnu Nair | 65eafde | 2021-03-24 12:17:36 -0700 | [diff] [blame^] | 51 | ALOGV("failed to get buffer, invalid buffer id"); |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
| 55 | ClientCacheBuffer& buf = bufItr->second; |
| 56 | *outClientCacheBuffer = &buf; |
| 57 | return true; |
| 58 | } |
| 59 | |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 60 | bool ClientCache::add(const client_cache_t& cacheId, const sp<GraphicBuffer>& buffer) { |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 61 | auto& [processToken, id] = cacheId; |
| 62 | if (processToken == nullptr) { |
| 63 | ALOGE("failed to cache buffer: invalid process token"); |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 64 | return false; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (!buffer) { |
| 68 | ALOGE("failed to cache buffer: invalid buffer"); |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 69 | return false; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | std::lock_guard lock(mMutex); |
| 73 | sp<IBinder> token; |
| 74 | |
| 75 | // If this is a new process token, set a death recipient. If the client process dies, we will |
| 76 | // get a callback through binderDied. |
| 77 | auto it = mBuffers.find(processToken); |
| 78 | if (it == mBuffers.end()) { |
| 79 | token = processToken.promote(); |
| 80 | if (!token) { |
| 81 | ALOGE("failed to cache buffer: invalid token"); |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 82 | return false; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | status_t err = token->linkToDeath(mDeathRecipient); |
| 86 | if (err != NO_ERROR) { |
| 87 | ALOGE("failed to cache buffer: could not link to death"); |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 88 | return false; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 89 | } |
| 90 | auto [itr, success] = |
Valerie Hau | 90fcc94 | 2019-11-18 11:18:57 -0800 | [diff] [blame] | 91 | mBuffers.emplace(processToken, |
| 92 | std::make_pair(token, |
| 93 | std::unordered_map<uint64_t, ClientCacheBuffer>())); |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 94 | LOG_ALWAYS_FATAL_IF(!success, "failed to insert new process into client cache"); |
| 95 | it = itr; |
| 96 | } |
| 97 | |
Valerie Hau | 90fcc94 | 2019-11-18 11:18:57 -0800 | [diff] [blame] | 98 | auto& processBuffers = it->second.second; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 99 | |
| 100 | if (processBuffers.size() > BUFFER_CACHE_MAX_SIZE) { |
| 101 | ALOGE("failed to cache buffer: cache is full"); |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 102 | return false; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | processBuffers[id].buffer = buffer; |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 106 | return true; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void ClientCache::erase(const client_cache_t& cacheId) { |
| 110 | auto& [processToken, id] = cacheId; |
| 111 | std::vector<sp<ErasedRecipient>> pendingErase; |
| 112 | { |
| 113 | std::lock_guard lock(mMutex); |
| 114 | ClientCacheBuffer* buf = nullptr; |
| 115 | if (!getBuffer(cacheId, &buf)) { |
| 116 | ALOGE("failed to erase buffer, could not retrieve buffer"); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | for (auto& recipient : buf->recipients) { |
| 121 | sp<ErasedRecipient> erasedRecipient = recipient.promote(); |
| 122 | if (erasedRecipient) { |
| 123 | pendingErase.push_back(erasedRecipient); |
| 124 | } |
| 125 | } |
| 126 | |
Valerie Hau | 90fcc94 | 2019-11-18 11:18:57 -0800 | [diff] [blame] | 127 | mBuffers[processToken].second.erase(id); |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | for (auto& recipient : pendingErase) { |
| 131 | recipient->bufferErased(cacheId); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | sp<GraphicBuffer> ClientCache::get(const client_cache_t& cacheId) { |
| 136 | std::lock_guard lock(mMutex); |
| 137 | |
| 138 | ClientCacheBuffer* buf = nullptr; |
| 139 | if (!getBuffer(cacheId, &buf)) { |
| 140 | ALOGE("failed to get buffer, could not retrieve buffer"); |
| 141 | return nullptr; |
| 142 | } |
| 143 | |
| 144 | return buf->buffer; |
| 145 | } |
| 146 | |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 147 | bool ClientCache::registerErasedRecipient(const client_cache_t& cacheId, |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 148 | const wp<ErasedRecipient>& recipient) { |
| 149 | std::lock_guard lock(mMutex); |
| 150 | |
| 151 | ClientCacheBuffer* buf = nullptr; |
| 152 | if (!getBuffer(cacheId, &buf)) { |
Vishnu Nair | 65eafde | 2021-03-24 12:17:36 -0700 | [diff] [blame^] | 153 | ALOGV("failed to register erased recipient, could not retrieve buffer"); |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 154 | return false; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 155 | } |
| 156 | buf->recipients.insert(recipient); |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 157 | return true; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void ClientCache::unregisterErasedRecipient(const client_cache_t& cacheId, |
| 161 | const wp<ErasedRecipient>& recipient) { |
| 162 | std::lock_guard lock(mMutex); |
| 163 | |
| 164 | ClientCacheBuffer* buf = nullptr; |
| 165 | if (!getBuffer(cacheId, &buf)) { |
| 166 | ALOGE("failed to unregister erased recipient"); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | buf->recipients.erase(recipient); |
| 171 | } |
| 172 | |
| 173 | void ClientCache::removeProcess(const wp<IBinder>& processToken) { |
| 174 | std::vector<std::pair<sp<ErasedRecipient>, client_cache_t>> pendingErase; |
| 175 | { |
| 176 | if (processToken == nullptr) { |
| 177 | ALOGE("failed to remove process, invalid (nullptr) process token"); |
| 178 | return; |
| 179 | } |
| 180 | std::lock_guard lock(mMutex); |
| 181 | auto itr = mBuffers.find(processToken); |
| 182 | if (itr == mBuffers.end()) { |
| 183 | ALOGE("failed to remove process, could not find process"); |
| 184 | return; |
| 185 | } |
| 186 | |
Valerie Hau | 90fcc94 | 2019-11-18 11:18:57 -0800 | [diff] [blame] | 187 | for (auto& [id, clientCacheBuffer] : itr->second.second) { |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 188 | client_cache_t cacheId = {processToken, id}; |
| 189 | for (auto& recipient : clientCacheBuffer.recipients) { |
| 190 | sp<ErasedRecipient> erasedRecipient = recipient.promote(); |
| 191 | if (erasedRecipient) { |
| 192 | pendingErase.emplace_back(erasedRecipient, cacheId); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | mBuffers.erase(itr); |
| 197 | } |
| 198 | |
| 199 | for (auto& [recipient, cacheId] : pendingErase) { |
| 200 | recipient->bufferErased(cacheId); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void ClientCache::CacheDeathRecipient::binderDied(const wp<IBinder>& who) { |
| 205 | ClientCache::getInstance().removeProcess(who); |
| 206 | } |
| 207 | |
Robert Carr | beba6f0 | 2021-02-10 21:06:27 -0800 | [diff] [blame] | 208 | void ClientCache::dump(std::string& result) { |
| 209 | std::lock_guard lock(mMutex); |
| 210 | for (auto i : mBuffers) { |
| 211 | const sp<IBinder>& cacheOwner = i.second.first; |
| 212 | StringAppendF(&result," Cache owner: %p\n", cacheOwner.get()); |
| 213 | auto &buffers = i.second.second; |
| 214 | for (auto& [id, clientCacheBuffer] : buffers) { |
| 215 | StringAppendF(&result, "\t ID: %d, Width/Height: %d,%d\n", (int)id, |
| 216 | (int)clientCacheBuffer.buffer->getWidth(), |
| 217 | (int)clientCacheBuffer.buffer->getHeight()); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 222 | }; // namespace android |