blob: 5a1b6d7d9d18bd8f0ba81b573dfa368c94b3c868 [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#pragma once
18
19#include <functional>
20#include <mutex>
21#include <stack>
22#include <unordered_map>
23
24#include "ClientCache.h"
25
26namespace android {
27
28class HwcSlotGenerator : public ClientCache::ErasedRecipient {
29public:
30 HwcSlotGenerator();
31 void bufferErased(const client_cache_t& clientCacheId);
32 int getHwcCacheSlot(const client_cache_t& clientCacheId);
33
34private:
35 friend class SlotGenerationTest;
36 int addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
37 int getFreeHwcCacheSlot() REQUIRES(mMutex);
38 void evictLeastRecentlyUsed() REQUIRES(mMutex);
39 void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
40
41 struct CachedBufferHash {
42 std::size_t operator()(const client_cache_t& clientCacheId) const {
43 return std::hash<uint64_t>{}(clientCacheId.id);
44 }
45 };
46
47 std::mutex mMutex;
48
49 std::unordered_map<client_cache_t, std::pair<int /*HwcCacheSlot*/, uint64_t /*counter*/>,
50 CachedBufferHash>
51 mCachedBuffers GUARDED_BY(mMutex);
52 std::stack<int /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
53
54 // The cache increments this counter value when a slot is updated or used.
55 // Used to track the least recently-used buffer
56 uint64_t mCounter = 0;
57};
58} // namespace android