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