blob: c82ad7bc493e4cb505edd380b90cb7b7b2849157 [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
22#include "BufferStateLayerCache.h"
23
24#define MAX_CACHE_SIZE 64
25
26namespace android {
27
28int32_t BufferStateLayerCache::add(const sp<IBinder>& processToken,
29 const sp<GraphicBuffer>& buffer) {
30 std::lock_guard lock(mMutex);
31
32 auto& processCache = getProccessCache(processToken);
33
34 int32_t slot = findSlot(processCache);
35 if (slot < 0) {
36 return slot;
37 }
38
39 processCache[slot] = buffer;
40
41 return slot;
42}
43
44void BufferStateLayerCache::release(const sp<IBinder>& processToken, int32_t id) {
45 if (id < 0) {
46 ALOGE("invalid buffer id");
47 return;
48 }
49
50 std::lock_guard lock(mMutex);
51 auto& processCache = getProccessCache(processToken);
52
53 if (id >= processCache.size()) {
54 ALOGE("invalid buffer id");
55 return;
56 }
57 processCache[id] = nullptr;
58}
59
60sp<GraphicBuffer> BufferStateLayerCache::get(const sp<IBinder>& processToken, int32_t id) {
61 if (id < 0) {
62 ALOGE("invalid buffer id");
63 return nullptr;
64 }
65
66 std::lock_guard lock(mMutex);
67 auto& processCache = getProccessCache(processToken);
68
69 if (id >= processCache.size()) {
70 ALOGE("invalid buffer id");
71 return nullptr;
72 }
73 return processCache[id];
74}
75
76std::vector<sp<GraphicBuffer>>& BufferStateLayerCache::getProccessCache(
77 const sp<IBinder>& processToken) {
78 return mBuffers[processToken];
79}
80
81int32_t BufferStateLayerCache::findSlot(std::vector<sp<GraphicBuffer>>& processCache) {
82 int32_t slot = 0;
83
84 for (const sp<GraphicBuffer> buffer : processCache) {
85 if (!buffer) {
86 return slot;
87 }
88 slot++;
89 }
90
91 if (processCache.size() < MAX_CACHE_SIZE) {
92 processCache.push_back(nullptr);
93 return slot;
94 }
95
96 return -1;
97}
98
99}; // namespace android