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