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
28 ANDROID_SINGLETON_STATIC_INSTANCE(ClientCache);
29
ClientCache()30 ClientCache::ClientCache() : mDeathRecipient(new CacheDeathRecipient) {}
31
getBuffer(const client_cache_t & cacheId,ClientCacheBuffer ** outClientCacheBuffer)32 bool ClientCache::getBuffer(const client_cache_t& cacheId,
33 ClientCacheBuffer** outClientCacheBuffer) {
34 auto& [processToken, id] = cacheId;
35 if (processToken == nullptr) {
36 ALOGE("failed to get buffer, invalid (nullptr) process token");
37 return false;
38 }
39 auto it = mBuffers.find(processToken);
40 if (it == mBuffers.end()) {
41 ALOGE("failed to get buffer, invalid process token");
42 return false;
43 }
44
45 auto& processBuffers = it->second;
46
47 auto bufItr = processBuffers.find(id);
48 if (bufItr == processBuffers.end()) {
49 ALOGE("failed to get buffer, invalid buffer id");
50 return false;
51 }
52
53 ClientCacheBuffer& buf = bufItr->second;
54 *outClientCacheBuffer = &buf;
55 return true;
56 }
57
add(const client_cache_t & cacheId,const sp<GraphicBuffer> & buffer)58 void ClientCache::add(const client_cache_t& cacheId, const sp<GraphicBuffer>& buffer) {
59 auto& [processToken, id] = cacheId;
60 if (processToken == nullptr) {
61 ALOGE("failed to cache buffer: invalid process token");
62 return;
63 }
64
65 if (!buffer) {
66 ALOGE("failed to cache buffer: invalid buffer");
67 return;
68 }
69
70 std::lock_guard lock(mMutex);
71 sp<IBinder> token;
72
73 // If this is a new process token, set a death recipient. If the client process dies, we will
74 // get a callback through binderDied.
75 auto it = mBuffers.find(processToken);
76 if (it == mBuffers.end()) {
77 token = processToken.promote();
78 if (!token) {
79 ALOGE("failed to cache buffer: invalid token");
80 return;
81 }
82
83 status_t err = token->linkToDeath(mDeathRecipient);
84 if (err != NO_ERROR) {
85 ALOGE("failed to cache buffer: could not link to death");
86 return;
87 }
88 auto [itr, success] =
89 mBuffers.emplace(processToken, std::unordered_map<uint64_t, ClientCacheBuffer>());
90 LOG_ALWAYS_FATAL_IF(!success, "failed to insert new process into client cache");
91 it = itr;
92 }
93
94 auto& processBuffers = it->second;
95
96 if (processBuffers.size() > BUFFER_CACHE_MAX_SIZE) {
97 ALOGE("failed to cache buffer: cache is full");
98 return;
99 }
100
101 processBuffers[id].buffer = buffer;
102 }
103
erase(const client_cache_t & cacheId)104 void ClientCache::erase(const client_cache_t& cacheId) {
105 auto& [processToken, id] = cacheId;
106 std::vector<sp<ErasedRecipient>> pendingErase;
107 {
108 std::lock_guard lock(mMutex);
109 ClientCacheBuffer* buf = nullptr;
110 if (!getBuffer(cacheId, &buf)) {
111 ALOGE("failed to erase buffer, could not retrieve buffer");
112 return;
113 }
114
115 for (auto& recipient : buf->recipients) {
116 sp<ErasedRecipient> erasedRecipient = recipient.promote();
117 if (erasedRecipient) {
118 pendingErase.push_back(erasedRecipient);
119 }
120 }
121
122 mBuffers[processToken].erase(id);
123 }
124
125 for (auto& recipient : pendingErase) {
126 recipient->bufferErased(cacheId);
127 }
128 }
129
get(const client_cache_t & cacheId)130 sp<GraphicBuffer> ClientCache::get(const client_cache_t& cacheId) {
131 std::lock_guard lock(mMutex);
132
133 ClientCacheBuffer* buf = nullptr;
134 if (!getBuffer(cacheId, &buf)) {
135 ALOGE("failed to get buffer, could not retrieve buffer");
136 return nullptr;
137 }
138
139 return buf->buffer;
140 }
141
registerErasedRecipient(const client_cache_t & cacheId,const wp<ErasedRecipient> & recipient)142 void ClientCache::registerErasedRecipient(const client_cache_t& cacheId,
143 const wp<ErasedRecipient>& recipient) {
144 std::lock_guard lock(mMutex);
145
146 ClientCacheBuffer* buf = nullptr;
147 if (!getBuffer(cacheId, &buf)) {
148 ALOGE("failed to register erased recipient, could not retrieve buffer");
149 return;
150 }
151 buf->recipients.insert(recipient);
152 }
153
unregisterErasedRecipient(const client_cache_t & cacheId,const wp<ErasedRecipient> & recipient)154 void ClientCache::unregisterErasedRecipient(const client_cache_t& cacheId,
155 const wp<ErasedRecipient>& recipient) {
156 std::lock_guard lock(mMutex);
157
158 ClientCacheBuffer* buf = nullptr;
159 if (!getBuffer(cacheId, &buf)) {
160 ALOGE("failed to unregister erased recipient");
161 return;
162 }
163
164 buf->recipients.erase(recipient);
165 }
166
removeProcess(const wp<IBinder> & processToken)167 void ClientCache::removeProcess(const wp<IBinder>& processToken) {
168 std::vector<std::pair<sp<ErasedRecipient>, client_cache_t>> pendingErase;
169 {
170 if (processToken == nullptr) {
171 ALOGE("failed to remove process, invalid (nullptr) process token");
172 return;
173 }
174 std::lock_guard lock(mMutex);
175 auto itr = mBuffers.find(processToken);
176 if (itr == mBuffers.end()) {
177 ALOGE("failed to remove process, could not find process");
178 return;
179 }
180
181 for (auto& [id, clientCacheBuffer] : itr->second) {
182 client_cache_t cacheId = {processToken, id};
183 for (auto& recipient : clientCacheBuffer.recipients) {
184 sp<ErasedRecipient> erasedRecipient = recipient.promote();
185 if (erasedRecipient) {
186 pendingErase.emplace_back(erasedRecipient, cacheId);
187 }
188 }
189 }
190 mBuffers.erase(itr);
191 }
192
193 for (auto& [recipient, cacheId] : pendingErase) {
194 recipient->bufferErased(cacheId);
195 }
196 }
197
binderDied(const wp<IBinder> & who)198 void ClientCache::CacheDeathRecipient::binderDied(const wp<IBinder>& who) {
199 ClientCache::getInstance().removeProcess(who);
200 }
201
202 }; // namespace android
203