blob: cb02d16007f27e868267b55bdb01dcb845d46c56 [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 Wall73411622019-01-25 10:45:41 -080026#define VALID_CACHE_ID(id) ((id) >= 0 || (id) < (BUFFER_CACHE_MAX_SIZE))
Marissa Wallebc2c052019-01-16 19:16:55 -080027
28namespace android {
29
Marissa Wall73411622019-01-25 10:45:41 -080030ANDROID_SINGLETON_STATIC_INSTANCE(BufferStateLayerCache);
Marissa Wallebc2c052019-01-16 19:16:55 -080031
Marissa Wall73411622019-01-25 10:45:41 -080032BufferStateLayerCache::BufferStateLayerCache() : mDeathRecipient(new CacheDeathRecipient) {}
Marissa Wallebc2c052019-01-16 19:16:55 -080033
Marissa Wall73411622019-01-25 10:45:41 -080034void 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 Wallebc2c052019-01-16 19:16:55 -080039 }
40
Marissa Wall73411622019-01-25 10:45:41 -080041 if (!processToken) {
42 ALOGE("failed to cache buffer: invalid process token");
43 return;
44 }
Marissa Wallebc2c052019-01-16 19:16:55 -080045
Marissa Wall73411622019-01-25 10:45:41 -080046 if (!buffer) {
47 ALOGE("failed to cache buffer: invalid buffer");
Marissa Wallebc2c052019-01-16 19:16:55 -080048 return;
49 }
50
51 std::lock_guard lock(mMutex);
Marissa Wallebc2c052019-01-16 19:16:55 -080052
Marissa Wall73411622019-01-25 10:45:41 -080053 // 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 Wallebc2c052019-01-16 19:16:55 -080060 }
Marissa Wallebc2c052019-01-16 19:16:55 -080061 }
62
Marissa Wall73411622019-01-25 10:45:41 -080063 auto& processBuffers = mBuffers[processToken];
64 processBuffers[id] = buffer;
65}
66
67sp<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 Wallebc2c052019-01-16 19:16:55 -080071 }
72
Marissa Wall73411622019-01-25 10:45:41 -080073 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
93void BufferStateLayerCache::removeProcess(const wp<IBinder>& processToken) {
94 std::lock_guard lock(mMutex);
95 mBuffers.erase(processToken);
96}
97
98void BufferStateLayerCache::CacheDeathRecipient::binderDied(const wp<IBinder>& who) {
99 BufferStateLayerCache::getInstance().removeProcess(who);
Marissa Wallebc2c052019-01-16 19:16:55 -0800100}
101
102}; // namespace android