blob: 40ea8d387ad1747c57d59affdabe4082b2c08e5e [file] [log] [blame]
Marissa Wall947d34e2019-03-29 14:03:53 -07001/*
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
Dominik Laskowski75788452021-02-09 18:51:25 -080024#include <android-base/stringprintf.h>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000025#include <common/trace.h>
Vishnu Nairdbbe3852022-01-12 20:22:11 -080026#include <renderengine/impl/ExternalTexture.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080027
Marissa Wall947d34e2019-03-29 14:03:53 -070028#include "ClientCache.h"
29
30namespace android {
31
32ANDROID_SINGLETON_STATIC_INSTANCE(ClientCache);
33
Ady Abrahamd11bade2022-08-01 16:18:03 -070034ClientCache::ClientCache() : mDeathRecipient(sp<CacheDeathRecipient>::make()) {}
Marissa Wall947d34e2019-03-29 14:03:53 -070035
36bool ClientCache::getBuffer(const client_cache_t& cacheId,
37 ClientCacheBuffer** outClientCacheBuffer) {
38 auto& [processToken, id] = cacheId;
39 if (processToken == nullptr) {
Patrick Williams640b7292022-10-11 19:25:06 +000040 ALOGE_AND_TRACE("ClientCache::getBuffer - invalid (nullptr) process token");
Marissa Wall947d34e2019-03-29 14:03:53 -070041 return false;
42 }
43 auto it = mBuffers.find(processToken);
44 if (it == mBuffers.end()) {
Patrick Williams640b7292022-10-11 19:25:06 +000045 ALOGE_AND_TRACE("ClientCache::getBuffer - invalid process token");
Marissa Wall947d34e2019-03-29 14:03:53 -070046 return false;
47 }
48
Valerie Hau90fcc942019-11-18 11:18:57 -080049 auto& processBuffers = it->second.second;
Marissa Wall947d34e2019-03-29 14:03:53 -070050
51 auto bufItr = processBuffers.find(id);
52 if (bufItr == processBuffers.end()) {
Patrick Williams640b7292022-10-11 19:25:06 +000053 ALOGE_AND_TRACE("ClientCache::getBuffer - invalid buffer id");
Marissa Wall947d34e2019-03-29 14:03:53 -070054 return false;
55 }
56
57 ClientCacheBuffer& buf = bufItr->second;
58 *outClientCacheBuffer = &buf;
59 return true;
60}
61
Patrick Williamsf1e5df12022-10-17 21:37:42 +000062base::expected<std::shared_ptr<renderengine::ExternalTexture>, ClientCache::AddError>
63ClientCache::add(const client_cache_t& cacheId, const sp<GraphicBuffer>& buffer) {
Marissa Wall947d34e2019-03-29 14:03:53 -070064 auto& [processToken, id] = cacheId;
65 if (processToken == nullptr) {
Patrick Williams640b7292022-10-11 19:25:06 +000066 ALOGE_AND_TRACE("ClientCache::add - invalid (nullptr) process token");
Patrick Williamsf1e5df12022-10-17 21:37:42 +000067 return base::unexpected(AddError::Unspecified);
Marissa Wall947d34e2019-03-29 14:03:53 -070068 }
69
70 if (!buffer) {
Patrick Williams640b7292022-10-11 19:25:06 +000071 ALOGE_AND_TRACE("ClientCache::add - invalid (nullptr) buffer");
Patrick Williamsf1e5df12022-10-17 21:37:42 +000072 return base::unexpected(AddError::Unspecified);
Marissa Wall947d34e2019-03-29 14:03:53 -070073 }
74
75 std::lock_guard lock(mMutex);
76 sp<IBinder> token;
77
78 // If this is a new process token, set a death recipient. If the client process dies, we will
79 // get a callback through binderDied.
80 auto it = mBuffers.find(processToken);
81 if (it == mBuffers.end()) {
82 token = processToken.promote();
83 if (!token) {
Patrick Williams640b7292022-10-11 19:25:06 +000084 ALOGE_AND_TRACE("ClientCache::add - invalid token");
Patrick Williamsf1e5df12022-10-17 21:37:42 +000085 return base::unexpected(AddError::Unspecified);
Marissa Wall947d34e2019-03-29 14:03:53 -070086 }
87
chaviw70aa7572021-09-22 12:27:57 -050088 // Only call linkToDeath if not a local binder
89 if (token->localBinder() == nullptr) {
90 status_t err = token->linkToDeath(mDeathRecipient);
91 if (err != NO_ERROR) {
Patrick Williams640b7292022-10-11 19:25:06 +000092 ALOGE_AND_TRACE("ClientCache::add - could not link to death");
Patrick Williamsf1e5df12022-10-17 21:37:42 +000093 return base::unexpected(AddError::Unspecified);
chaviw70aa7572021-09-22 12:27:57 -050094 }
Marissa Wall947d34e2019-03-29 14:03:53 -070095 }
96 auto [itr, success] =
Valerie Hau90fcc942019-11-18 11:18:57 -080097 mBuffers.emplace(processToken,
98 std::make_pair(token,
99 std::unordered_map<uint64_t, ClientCacheBuffer>()));
Marissa Wall947d34e2019-03-29 14:03:53 -0700100 LOG_ALWAYS_FATAL_IF(!success, "failed to insert new process into client cache");
101 it = itr;
102 }
103
Valerie Hau90fcc942019-11-18 11:18:57 -0800104 auto& processBuffers = it->second.second;
Marissa Wall947d34e2019-03-29 14:03:53 -0700105
106 if (processBuffers.size() > BUFFER_CACHE_MAX_SIZE) {
Patrick Williams640b7292022-10-11 19:25:06 +0000107 ALOGE_AND_TRACE("ClientCache::add - cache is full");
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000108 return base::unexpected(AddError::CacheFull);
Marissa Wall947d34e2019-03-29 14:03:53 -0700109 }
110
Alec Mouria90a5702021-04-16 16:36:21 +0000111 LOG_ALWAYS_FATAL_IF(mRenderEngine == nullptr,
112 "Attempted to build the ClientCache before a RenderEngine instance was "
113 "ready!");
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000114
115 return (processBuffers[id].buffer = std::make_shared<
116 renderengine::impl::ExternalTexture>(buffer, *mRenderEngine,
117 renderengine::impl::ExternalTexture::
118 Usage::READABLE));
Marissa Wall947d34e2019-03-29 14:03:53 -0700119}
120
Brian Lindahl439afad2022-11-14 11:16:55 -0700121sp<GraphicBuffer> ClientCache::erase(const client_cache_t& cacheId) {
122 sp<GraphicBuffer> buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700123 auto& [processToken, id] = cacheId;
124 std::vector<sp<ErasedRecipient>> pendingErase;
125 {
126 std::lock_guard lock(mMutex);
127 ClientCacheBuffer* buf = nullptr;
128 if (!getBuffer(cacheId, &buf)) {
129 ALOGE("failed to erase buffer, could not retrieve buffer");
Brian Lindahl439afad2022-11-14 11:16:55 -0700130 return nullptr;
Marissa Wall947d34e2019-03-29 14:03:53 -0700131 }
132
Brian Lindahl439afad2022-11-14 11:16:55 -0700133 buffer = buf->buffer->getBuffer();
134
Marissa Wall947d34e2019-03-29 14:03:53 -0700135 for (auto& recipient : buf->recipients) {
136 sp<ErasedRecipient> erasedRecipient = recipient.promote();
137 if (erasedRecipient) {
138 pendingErase.push_back(erasedRecipient);
139 }
140 }
141
Valerie Hau90fcc942019-11-18 11:18:57 -0800142 mBuffers[processToken].second.erase(id);
Marissa Wall947d34e2019-03-29 14:03:53 -0700143 }
144
145 for (auto& recipient : pendingErase) {
146 recipient->bufferErased(cacheId);
147 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700148 return buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700149}
150
Alec Mouria90a5702021-04-16 16:36:21 +0000151std::shared_ptr<renderengine::ExternalTexture> ClientCache::get(const client_cache_t& cacheId) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700152 std::lock_guard lock(mMutex);
153
154 ClientCacheBuffer* buf = nullptr;
155 if (!getBuffer(cacheId, &buf)) {
156 ALOGE("failed to get buffer, could not retrieve buffer");
157 return nullptr;
158 }
159
160 return buf->buffer;
161}
162
Alec Mouri4545a8a2019-08-08 20:05:32 -0700163bool ClientCache::registerErasedRecipient(const client_cache_t& cacheId,
Marissa Wall947d34e2019-03-29 14:03:53 -0700164 const wp<ErasedRecipient>& recipient) {
165 std::lock_guard lock(mMutex);
166
167 ClientCacheBuffer* buf = nullptr;
168 if (!getBuffer(cacheId, &buf)) {
Vishnu Nair65eafde2021-03-24 12:17:36 -0700169 ALOGV("failed to register erased recipient, could not retrieve buffer");
Alec Mouri4545a8a2019-08-08 20:05:32 -0700170 return false;
Marissa Wall947d34e2019-03-29 14:03:53 -0700171 }
172 buf->recipients.insert(recipient);
Alec Mouri4545a8a2019-08-08 20:05:32 -0700173 return true;
Marissa Wall947d34e2019-03-29 14:03:53 -0700174}
175
176void ClientCache::unregisterErasedRecipient(const client_cache_t& cacheId,
177 const wp<ErasedRecipient>& recipient) {
178 std::lock_guard lock(mMutex);
179
180 ClientCacheBuffer* buf = nullptr;
181 if (!getBuffer(cacheId, &buf)) {
182 ALOGE("failed to unregister erased recipient");
183 return;
184 }
185
186 buf->recipients.erase(recipient);
187}
188
189void ClientCache::removeProcess(const wp<IBinder>& processToken) {
190 std::vector<std::pair<sp<ErasedRecipient>, client_cache_t>> pendingErase;
191 {
192 if (processToken == nullptr) {
193 ALOGE("failed to remove process, invalid (nullptr) process token");
194 return;
195 }
196 std::lock_guard lock(mMutex);
197 auto itr = mBuffers.find(processToken);
198 if (itr == mBuffers.end()) {
199 ALOGE("failed to remove process, could not find process");
200 return;
201 }
202
Valerie Hau90fcc942019-11-18 11:18:57 -0800203 for (auto& [id, clientCacheBuffer] : itr->second.second) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700204 client_cache_t cacheId = {processToken, id};
205 for (auto& recipient : clientCacheBuffer.recipients) {
206 sp<ErasedRecipient> erasedRecipient = recipient.promote();
207 if (erasedRecipient) {
208 pendingErase.emplace_back(erasedRecipient, cacheId);
209 }
210 }
211 }
212 mBuffers.erase(itr);
213 }
214
215 for (auto& [recipient, cacheId] : pendingErase) {
216 recipient->bufferErased(cacheId);
217 }
218}
219
220void ClientCache::CacheDeathRecipient::binderDied(const wp<IBinder>& who) {
221 ClientCache::getInstance().removeProcess(who);
222}
223
Robert Carrbeba6f02021-02-10 21:06:27 -0800224void ClientCache::dump(std::string& result) {
225 std::lock_guard lock(mMutex);
Dominik Laskowski75788452021-02-09 18:51:25 -0800226 for (const auto& [_, cache] : mBuffers) {
227 base::StringAppendF(&result, " Cache owner: %p\n", cache.first.get());
228
229 for (const auto& [id, entry] : cache.second) {
230 const auto& buffer = entry.buffer->getBuffer();
231 base::StringAppendF(&result, "\tID: %" PRIu64 ", size: %ux%u\n", id, buffer->getWidth(),
232 buffer->getHeight());
Robert Carrbeba6f02021-02-10 21:06:27 -0800233 }
234 }
235}
236
Dominik Laskowski75788452021-02-09 18:51:25 -0800237} // namespace android