Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 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 "BufferStateLayerCache" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 22 | #include <cinttypes> |
| 23 | |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 24 | #include "BufferStateLayerCache.h" |
| 25 | |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 28 | ANDROID_SINGLETON_STATIC_INSTANCE(BufferStateLayerCache); |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 29 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 30 | BufferStateLayerCache::BufferStateLayerCache() : mDeathRecipient(new CacheDeathRecipient) {} |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 31 | |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame^] | 32 | void BufferStateLayerCache::add(const sp<IBinder>& processToken, uint64_t id, |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 33 | const sp<GraphicBuffer>& buffer) { |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 34 | if (!processToken) { |
| 35 | ALOGE("failed to cache buffer: invalid process token"); |
| 36 | return; |
| 37 | } |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 38 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 39 | if (!buffer) { |
| 40 | ALOGE("failed to cache buffer: invalid buffer"); |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 41 | return; |
| 42 | } |
| 43 | |
| 44 | std::lock_guard lock(mMutex); |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 45 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 46 | // If this is a new process token, set a death recipient. If the client process dies, we will |
| 47 | // get a callback through binderDied. |
| 48 | if (mBuffers.find(processToken) == mBuffers.end()) { |
| 49 | status_t err = processToken->linkToDeath(mDeathRecipient); |
| 50 | if (err != NO_ERROR) { |
| 51 | ALOGE("failed to cache buffer: could not link to death"); |
| 52 | return; |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 53 | } |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 56 | auto& processBuffers = mBuffers[processToken]; |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame^] | 57 | |
| 58 | if (processBuffers.size() > BUFFER_CACHE_MAX_SIZE) { |
| 59 | ALOGE("failed to cache buffer: cache is full"); |
| 60 | return; |
| 61 | } |
| 62 | |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 63 | processBuffers[id] = buffer; |
| 64 | } |
| 65 | |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame^] | 66 | void BufferStateLayerCache::erase(const sp<IBinder>& processToken, uint64_t id) { |
| 67 | if (!processToken) { |
| 68 | ALOGE("failed to uncache buffer: invalid process token"); |
| 69 | return; |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame^] | 72 | std::lock_guard lock(mMutex); |
| 73 | |
| 74 | if (mBuffers.find(processToken) == mBuffers.end()) { |
| 75 | ALOGE("failed to uncache buffer: process token not found"); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | auto& processBuffers = mBuffers[processToken]; |
| 80 | processBuffers.erase(id); |
| 81 | } |
| 82 | |
| 83 | sp<GraphicBuffer> BufferStateLayerCache::get(const sp<IBinder>& processToken, uint64_t id) { |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 84 | if (!processToken) { |
| 85 | ALOGE("failed to cache buffer: invalid process token"); |
| 86 | return nullptr; |
| 87 | } |
| 88 | |
| 89 | std::lock_guard lock(mMutex); |
| 90 | auto itr = mBuffers.find(processToken); |
| 91 | if (itr == mBuffers.end()) { |
| 92 | ALOGE("failed to get buffer: process token not found"); |
| 93 | return nullptr; |
| 94 | } |
| 95 | |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame^] | 96 | if (itr->second.find(id) == itr->second.end()) { |
| 97 | ALOGE("failed to get buffer: buffer not found"); |
Marissa Wall | 7341162 | 2019-01-25 10:45:41 -0800 | [diff] [blame] | 98 | return nullptr; |
| 99 | } |
| 100 | |
| 101 | return itr->second[id]; |
| 102 | } |
| 103 | |
| 104 | void BufferStateLayerCache::removeProcess(const wp<IBinder>& processToken) { |
| 105 | std::lock_guard lock(mMutex); |
| 106 | mBuffers.erase(processToken); |
| 107 | } |
| 108 | |
| 109 | void BufferStateLayerCache::CacheDeathRecipient::binderDied(const wp<IBinder>& who) { |
| 110 | BufferStateLayerCache::getInstance().removeProcess(who); |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | }; // namespace android |