blob: 51ca45c53a87cf0a17e5e665d1b1ad919dd5d6dc [file] [log] [blame]
Marissa Wallebc2c052019-01-16 19:16:55 -08001/*
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 Wall73411622019-01-25 10:45:41 -080022#include <cinttypes>
23
Marissa Wallebc2c052019-01-16 19:16:55 -080024#include "BufferStateLayerCache.h"
25
Marissa Wallebc2c052019-01-16 19:16:55 -080026namespace android {
27
Marissa Wall73411622019-01-25 10:45:41 -080028ANDROID_SINGLETON_STATIC_INSTANCE(BufferStateLayerCache);
Marissa Wallebc2c052019-01-16 19:16:55 -080029
Marissa Wall73411622019-01-25 10:45:41 -080030BufferStateLayerCache::BufferStateLayerCache() : mDeathRecipient(new CacheDeathRecipient) {}
Marissa Wallebc2c052019-01-16 19:16:55 -080031
Marissa Wall78b72202019-03-15 14:58:34 -070032void BufferStateLayerCache::add(const sp<IBinder>& processToken, uint64_t id,
Marissa Wall73411622019-01-25 10:45:41 -080033 const sp<GraphicBuffer>& buffer) {
Marissa Wall73411622019-01-25 10:45:41 -080034 if (!processToken) {
35 ALOGE("failed to cache buffer: invalid process token");
36 return;
37 }
Marissa Wallebc2c052019-01-16 19:16:55 -080038
Marissa Wall73411622019-01-25 10:45:41 -080039 if (!buffer) {
40 ALOGE("failed to cache buffer: invalid buffer");
Marissa Wallebc2c052019-01-16 19:16:55 -080041 return;
42 }
43
44 std::lock_guard lock(mMutex);
Marissa Wallebc2c052019-01-16 19:16:55 -080045
Marissa Wall73411622019-01-25 10:45:41 -080046 // 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 Wallebc2c052019-01-16 19:16:55 -080053 }
Marissa Wallebc2c052019-01-16 19:16:55 -080054 }
55
Marissa Wall73411622019-01-25 10:45:41 -080056 auto& processBuffers = mBuffers[processToken];
Marissa Wall78b72202019-03-15 14:58:34 -070057
58 if (processBuffers.size() > BUFFER_CACHE_MAX_SIZE) {
59 ALOGE("failed to cache buffer: cache is full");
60 return;
61 }
62
Marissa Wall73411622019-01-25 10:45:41 -080063 processBuffers[id] = buffer;
64}
65
Marissa Wall78b72202019-03-15 14:58:34 -070066void BufferStateLayerCache::erase(const sp<IBinder>& processToken, uint64_t id) {
67 if (!processToken) {
68 ALOGE("failed to uncache buffer: invalid process token");
69 return;
Marissa Wallebc2c052019-01-16 19:16:55 -080070 }
71
Marissa Wall78b72202019-03-15 14:58:34 -070072 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
83sp<GraphicBuffer> BufferStateLayerCache::get(const sp<IBinder>& processToken, uint64_t id) {
Marissa Wall73411622019-01-25 10:45:41 -080084 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 Wall78b72202019-03-15 14:58:34 -070096 if (itr->second.find(id) == itr->second.end()) {
97 ALOGE("failed to get buffer: buffer not found");
Marissa Wall73411622019-01-25 10:45:41 -080098 return nullptr;
99 }
100
101 return itr->second[id];
102}
103
104void BufferStateLayerCache::removeProcess(const wp<IBinder>& processToken) {
105 std::lock_guard lock(mMutex);
106 mBuffers.erase(processToken);
107}
108
109void BufferStateLayerCache::CacheDeathRecipient::binderDied(const wp<IBinder>& who) {
110 BufferStateLayerCache::getInstance().removeProcess(who);
Marissa Wallebc2c052019-01-16 19:16:55 -0800111}
112
113}; // namespace android