blob: 939c35b8b30a0170ac5466810955472730b05577 [file] [log] [blame]
Vishnu Nair397a0e32022-07-26 00:01:48 +00001/*
2 * Copyright 2022 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#undef LOG_TAG
18#define LOG_TAG "HwcSlotGenerator"
19#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
21#include <gui/BufferQueue.h>
22
23#include "HwcSlotGenerator.h"
24
25namespace android {
26
27HwcSlotGenerator::HwcSlotGenerator() {
28 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
29 mFreeHwcCacheSlots.push(i);
30 }
31}
32
33void HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
34 std::lock_guard lock(mMutex);
35 if (!clientCacheId.isValid()) {
36 ALOGE("invalid process, failed to erase buffer");
37 return;
38 }
39 eraseBufferLocked(clientCacheId);
40}
41
42int HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
43 std::lock_guard<std::mutex> lock(mMutex);
44 auto itr = mCachedBuffers.find(clientCacheId);
45 if (itr == mCachedBuffers.end()) {
46 return addCachedBuffer(clientCacheId);
47 }
48 auto& [hwcCacheSlot, counter] = itr->second;
49 counter = mCounter++;
50 return hwcCacheSlot;
51}
52
53int HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex) {
54 if (!clientCacheId.isValid()) {
55 ALOGE("invalid process, returning invalid slot");
56 return BufferQueue::INVALID_BUFFER_SLOT;
57 }
58
Ady Abrahamd11bade2022-08-01 16:18:03 -070059 ClientCache::getInstance().registerErasedRecipient(clientCacheId,
60 wp<ErasedRecipient>::fromExisting(this));
Vishnu Nair397a0e32022-07-26 00:01:48 +000061
62 int hwcCacheSlot = getFreeHwcCacheSlot();
63 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
64 return hwcCacheSlot;
65}
66
67int HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
68 if (mFreeHwcCacheSlots.empty()) {
69 evictLeastRecentlyUsed();
70 }
71
72 int hwcCacheSlot = mFreeHwcCacheSlots.top();
73 mFreeHwcCacheSlots.pop();
74 return hwcCacheSlot;
75}
76
77void HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
78 uint64_t minCounter = UINT_MAX;
79 client_cache_t minClientCacheId = {};
80 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
81 const auto& [hwcCacheSlot, counter] = slotCounter;
82 if (counter < minCounter) {
83 minCounter = counter;
84 minClientCacheId = clientCacheId;
85 }
86 }
87 eraseBufferLocked(minClientCacheId);
88
Ady Abrahamd11bade2022-08-01 16:18:03 -070089 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId,
90 wp<ErasedRecipient>::fromExisting(this));
Vishnu Nair397a0e32022-07-26 00:01:48 +000091}
92
93void HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex) {
94 auto itr = mCachedBuffers.find(clientCacheId);
95 if (itr == mCachedBuffers.end()) {
96 return;
97 }
98 auto& [hwcCacheSlot, counter] = itr->second;
99
100 // TODO send to hwc cache and resources
101
102 mFreeHwcCacheSlots.push(hwcCacheSlot);
103 mCachedBuffers.erase(clientCacheId);
104}
105
106} // namespace android