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.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 bool 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 false;
63     }
64 
65     if (!buffer) {
66         ALOGE("failed to cache buffer: invalid buffer");
67         return false;
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 false;
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 false;
87         }
88         auto [itr, success] =
89                 mBuffers.emplace(processToken,
90                                  std::make_pair(token,
91                                                 std::unordered_map<uint64_t, ClientCacheBuffer>()));
92         LOG_ALWAYS_FATAL_IF(!success, "failed to insert new process into client cache");
93         it = itr;
94     }
95 
96     auto& processBuffers = it->second.second;
97 
98     if (processBuffers.size() > BUFFER_CACHE_MAX_SIZE) {
99         ALOGE("failed to cache buffer: cache is full");
100         return false;
101     }
102 
103     processBuffers[id].buffer = buffer;
104     return true;
105 }
106 
erase(const client_cache_t & cacheId)107 void ClientCache::erase(const client_cache_t& cacheId) {
108     auto& [processToken, id] = cacheId;
109     std::vector<sp<ErasedRecipient>> pendingErase;
110     {
111         std::lock_guard lock(mMutex);
112         ClientCacheBuffer* buf = nullptr;
113         if (!getBuffer(cacheId, &buf)) {
114             ALOGE("failed to erase buffer, could not retrieve buffer");
115             return;
116         }
117 
118         for (auto& recipient : buf->recipients) {
119             sp<ErasedRecipient> erasedRecipient = recipient.promote();
120             if (erasedRecipient) {
121                 pendingErase.push_back(erasedRecipient);
122             }
123         }
124 
125         mBuffers[processToken].second.erase(id);
126     }
127 
128     for (auto& recipient : pendingErase) {
129         recipient->bufferErased(cacheId);
130     }
131 }
132 
get(const client_cache_t & cacheId)133 sp<GraphicBuffer> ClientCache::get(const client_cache_t& cacheId) {
134     std::lock_guard lock(mMutex);
135 
136     ClientCacheBuffer* buf = nullptr;
137     if (!getBuffer(cacheId, &buf)) {
138         ALOGE("failed to get buffer, could not retrieve buffer");
139         return nullptr;
140     }
141 
142     return buf->buffer;
143 }
144 
registerErasedRecipient(const client_cache_t & cacheId,const wp<ErasedRecipient> & recipient)145 bool ClientCache::registerErasedRecipient(const client_cache_t& cacheId,
146                                           const wp<ErasedRecipient>& recipient) {
147     std::lock_guard lock(mMutex);
148 
149     ClientCacheBuffer* buf = nullptr;
150     if (!getBuffer(cacheId, &buf)) {
151         ALOGE("failed to register erased recipient, could not retrieve buffer");
152         return false;
153     }
154     buf->recipients.insert(recipient);
155     return true;
156 }
157 
unregisterErasedRecipient(const client_cache_t & cacheId,const wp<ErasedRecipient> & recipient)158 void ClientCache::unregisterErasedRecipient(const client_cache_t& cacheId,
159                                             const wp<ErasedRecipient>& recipient) {
160     std::lock_guard lock(mMutex);
161 
162     ClientCacheBuffer* buf = nullptr;
163     if (!getBuffer(cacheId, &buf)) {
164         ALOGE("failed to unregister erased recipient");
165         return;
166     }
167 
168     buf->recipients.erase(recipient);
169 }
170 
removeProcess(const wp<IBinder> & processToken)171 void ClientCache::removeProcess(const wp<IBinder>& processToken) {
172     std::vector<std::pair<sp<ErasedRecipient>, client_cache_t>> pendingErase;
173     {
174         if (processToken == nullptr) {
175             ALOGE("failed to remove process, invalid (nullptr) process token");
176             return;
177         }
178         std::lock_guard lock(mMutex);
179         auto itr = mBuffers.find(processToken);
180         if (itr == mBuffers.end()) {
181             ALOGE("failed to remove process, could not find process");
182             return;
183         }
184 
185         for (auto& [id, clientCacheBuffer] : itr->second.second) {
186             client_cache_t cacheId = {processToken, id};
187             for (auto& recipient : clientCacheBuffer.recipients) {
188                 sp<ErasedRecipient> erasedRecipient = recipient.promote();
189                 if (erasedRecipient) {
190                     pendingErase.emplace_back(erasedRecipient, cacheId);
191                 }
192             }
193         }
194         mBuffers.erase(itr);
195     }
196 
197     for (auto& [recipient, cacheId] : pendingErase) {
198         recipient->bufferErased(cacheId);
199     }
200 }
201 
binderDied(const wp<IBinder> & who)202 void ClientCache::CacheDeathRecipient::binderDied(const wp<IBinder>& who) {
203     ClientCache::getInstance().removeProcess(who);
204 }
205 
206 }; // namespace android
207