blob: b01932e41380ba7b4085aaf4b7bc01b37ab61963 [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>
Patrick Williams640b7292022-10-11 19:25:06 +000025#include <gui/TraceUtils.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
Alec Mouri4545a8a2019-08-08 20:05:32 -070062bool ClientCache::add(const client_cache_t& cacheId, const sp<GraphicBuffer>& buffer) {
Marissa Wall947d34e2019-03-29 14:03:53 -070063 auto& [processToken, id] = cacheId;
64 if (processToken == nullptr) {
Patrick Williams640b7292022-10-11 19:25:06 +000065 ALOGE_AND_TRACE("ClientCache::add - invalid (nullptr) process token");
Alec Mouri4545a8a2019-08-08 20:05:32 -070066 return false;
Marissa Wall947d34e2019-03-29 14:03:53 -070067 }
68
69 if (!buffer) {
Patrick Williams640b7292022-10-11 19:25:06 +000070 ALOGE_AND_TRACE("ClientCache::add - invalid (nullptr) buffer");
Alec Mouri4545a8a2019-08-08 20:05:32 -070071 return false;
Marissa Wall947d34e2019-03-29 14:03:53 -070072 }
73
74 std::lock_guard lock(mMutex);
75 sp<IBinder> token;
76
77 // If this is a new process token, set a death recipient. If the client process dies, we will
78 // get a callback through binderDied.
79 auto it = mBuffers.find(processToken);
80 if (it == mBuffers.end()) {
81 token = processToken.promote();
82 if (!token) {
Patrick Williams640b7292022-10-11 19:25:06 +000083 ALOGE_AND_TRACE("ClientCache::add - invalid token");
Alec Mouri4545a8a2019-08-08 20:05:32 -070084 return false;
Marissa Wall947d34e2019-03-29 14:03:53 -070085 }
86
chaviw70aa7572021-09-22 12:27:57 -050087 // Only call linkToDeath if not a local binder
88 if (token->localBinder() == nullptr) {
89 status_t err = token->linkToDeath(mDeathRecipient);
90 if (err != NO_ERROR) {
Patrick Williams640b7292022-10-11 19:25:06 +000091 ALOGE_AND_TRACE("ClientCache::add - could not link to death");
chaviw70aa7572021-09-22 12:27:57 -050092 return false;
93 }
Marissa Wall947d34e2019-03-29 14:03:53 -070094 }
95 auto [itr, success] =
Valerie Hau90fcc942019-11-18 11:18:57 -080096 mBuffers.emplace(processToken,
97 std::make_pair(token,
98 std::unordered_map<uint64_t, ClientCacheBuffer>()));
Marissa Wall947d34e2019-03-29 14:03:53 -070099 LOG_ALWAYS_FATAL_IF(!success, "failed to insert new process into client cache");
100 it = itr;
101 }
102
Valerie Hau90fcc942019-11-18 11:18:57 -0800103 auto& processBuffers = it->second.second;
Marissa Wall947d34e2019-03-29 14:03:53 -0700104
105 if (processBuffers.size() > BUFFER_CACHE_MAX_SIZE) {
Patrick Williams640b7292022-10-11 19:25:06 +0000106 ALOGE_AND_TRACE("ClientCache::add - cache is full");
Alec Mouri4545a8a2019-08-08 20:05:32 -0700107 return false;
Marissa Wall947d34e2019-03-29 14:03:53 -0700108 }
109
Alec Mouria90a5702021-04-16 16:36:21 +0000110 LOG_ALWAYS_FATAL_IF(mRenderEngine == nullptr,
111 "Attempted to build the ClientCache before a RenderEngine instance was "
112 "ready!");
113 processBuffers[id].buffer = std::make_shared<
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800114 renderengine::impl::ExternalTexture>(buffer, *mRenderEngine,
115 renderengine::impl::ExternalTexture::Usage::
116 READABLE);
Alec Mouri4545a8a2019-08-08 20:05:32 -0700117 return true;
Marissa Wall947d34e2019-03-29 14:03:53 -0700118}
119
120void ClientCache::erase(const client_cache_t& cacheId) {
121 auto& [processToken, id] = cacheId;
122 std::vector<sp<ErasedRecipient>> pendingErase;
123 {
124 std::lock_guard lock(mMutex);
125 ClientCacheBuffer* buf = nullptr;
126 if (!getBuffer(cacheId, &buf)) {
127 ALOGE("failed to erase buffer, could not retrieve buffer");
128 return;
129 }
130
131 for (auto& recipient : buf->recipients) {
132 sp<ErasedRecipient> erasedRecipient = recipient.promote();
133 if (erasedRecipient) {
134 pendingErase.push_back(erasedRecipient);
135 }
136 }
137
Valerie Hau90fcc942019-11-18 11:18:57 -0800138 mBuffers[processToken].second.erase(id);
Marissa Wall947d34e2019-03-29 14:03:53 -0700139 }
140
141 for (auto& recipient : pendingErase) {
142 recipient->bufferErased(cacheId);
143 }
144}
145
Alec Mouria90a5702021-04-16 16:36:21 +0000146std::shared_ptr<renderengine::ExternalTexture> ClientCache::get(const client_cache_t& cacheId) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700147 std::lock_guard lock(mMutex);
148
149 ClientCacheBuffer* buf = nullptr;
150 if (!getBuffer(cacheId, &buf)) {
151 ALOGE("failed to get buffer, could not retrieve buffer");
152 return nullptr;
153 }
154
155 return buf->buffer;
156}
157
Alec Mouri4545a8a2019-08-08 20:05:32 -0700158bool ClientCache::registerErasedRecipient(const client_cache_t& cacheId,
Marissa Wall947d34e2019-03-29 14:03:53 -0700159 const wp<ErasedRecipient>& recipient) {
160 std::lock_guard lock(mMutex);
161
162 ClientCacheBuffer* buf = nullptr;
163 if (!getBuffer(cacheId, &buf)) {
Vishnu Nair65eafde2021-03-24 12:17:36 -0700164 ALOGV("failed to register erased recipient, could not retrieve buffer");
Alec Mouri4545a8a2019-08-08 20:05:32 -0700165 return false;
Marissa Wall947d34e2019-03-29 14:03:53 -0700166 }
167 buf->recipients.insert(recipient);
Alec Mouri4545a8a2019-08-08 20:05:32 -0700168 return true;
Marissa Wall947d34e2019-03-29 14:03:53 -0700169}
170
171void ClientCache::unregisterErasedRecipient(const client_cache_t& cacheId,
172 const wp<ErasedRecipient>& recipient) {
173 std::lock_guard lock(mMutex);
174
175 ClientCacheBuffer* buf = nullptr;
176 if (!getBuffer(cacheId, &buf)) {
177 ALOGE("failed to unregister erased recipient");
178 return;
179 }
180
181 buf->recipients.erase(recipient);
182}
183
184void ClientCache::removeProcess(const wp<IBinder>& processToken) {
185 std::vector<std::pair<sp<ErasedRecipient>, client_cache_t>> pendingErase;
186 {
187 if (processToken == nullptr) {
188 ALOGE("failed to remove process, invalid (nullptr) process token");
189 return;
190 }
191 std::lock_guard lock(mMutex);
192 auto itr = mBuffers.find(processToken);
193 if (itr == mBuffers.end()) {
194 ALOGE("failed to remove process, could not find process");
195 return;
196 }
197
Valerie Hau90fcc942019-11-18 11:18:57 -0800198 for (auto& [id, clientCacheBuffer] : itr->second.second) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700199 client_cache_t cacheId = {processToken, id};
200 for (auto& recipient : clientCacheBuffer.recipients) {
201 sp<ErasedRecipient> erasedRecipient = recipient.promote();
202 if (erasedRecipient) {
203 pendingErase.emplace_back(erasedRecipient, cacheId);
204 }
205 }
206 }
207 mBuffers.erase(itr);
208 }
209
210 for (auto& [recipient, cacheId] : pendingErase) {
211 recipient->bufferErased(cacheId);
212 }
213}
214
215void ClientCache::CacheDeathRecipient::binderDied(const wp<IBinder>& who) {
216 ClientCache::getInstance().removeProcess(who);
217}
218
Robert Carrbeba6f02021-02-10 21:06:27 -0800219void ClientCache::dump(std::string& result) {
220 std::lock_guard lock(mMutex);
Dominik Laskowski75788452021-02-09 18:51:25 -0800221 for (const auto& [_, cache] : mBuffers) {
222 base::StringAppendF(&result, " Cache owner: %p\n", cache.first.get());
223
224 for (const auto& [id, entry] : cache.second) {
225 const auto& buffer = entry.buffer->getBuffer();
226 base::StringAppendF(&result, "\tID: %" PRIu64 ", size: %ux%u\n", id, buffer->getWidth(),
227 buffer->getHeight());
Robert Carrbeba6f02021-02-10 21:06:27 -0800228 }
229 }
230}
231
Dominik Laskowski75788452021-02-09 18:51:25 -0800232} // namespace android