blob: b7e9607c4b7a45e6cbf50e1ad0a9b037c9deccaf [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
59 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
60
61 int hwcCacheSlot = getFreeHwcCacheSlot();
62 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
63 return hwcCacheSlot;
64}
65
66int HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
67 if (mFreeHwcCacheSlots.empty()) {
68 evictLeastRecentlyUsed();
69 }
70
71 int hwcCacheSlot = mFreeHwcCacheSlots.top();
72 mFreeHwcCacheSlots.pop();
73 return hwcCacheSlot;
74}
75
76void HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
77 uint64_t minCounter = UINT_MAX;
78 client_cache_t minClientCacheId = {};
79 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
80 const auto& [hwcCacheSlot, counter] = slotCounter;
81 if (counter < minCounter) {
82 minCounter = counter;
83 minClientCacheId = clientCacheId;
84 }
85 }
86 eraseBufferLocked(minClientCacheId);
87
88 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
89}
90
91void HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex) {
92 auto itr = mCachedBuffers.find(clientCacheId);
93 if (itr == mCachedBuffers.end()) {
94 return;
95 }
96 auto& [hwcCacheSlot, counter] = itr->second;
97
98 // TODO send to hwc cache and resources
99
100 mFreeHwcCacheSlots.push(hwcCacheSlot);
101 mCachedBuffers.erase(clientCacheId);
102}
103
104} // namespace android