Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 17 | #include <compositionengine/impl/HwcBufferCache.h> |
| 18 | |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 19 | #include <gui/BufferQueue.h> |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 20 | #include <ui/GraphicBuffer.h> |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 21 | |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 22 | namespace android::compositionengine::impl { |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 23 | |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 24 | HwcBufferCache::HwcBufferCache() { |
Brian Lindahl | 90553da | 2022-12-06 13:36:30 -0700 | [diff] [blame] | 25 | for (uint32_t i = kMaxLayerBufferCount; i-- > 0;) { |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 26 | mFreeSlots.push(i); |
| 27 | } |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 30 | HwcSlotAndBuffer HwcBufferCache::getHwcSlotAndBuffer(const sp<GraphicBuffer>& buffer) { |
| 31 | // TODO(b/261930578): This is for unit tests which don't mock GraphicBuffers but instead send |
| 32 | // in nullptrs. |
| 33 | if (buffer == nullptr) { |
| 34 | return {0, nullptr}; |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame] | 35 | } |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 36 | if (auto i = mCacheByBufferId.find(buffer->getId()); i != mCacheByBufferId.end()) { |
| 37 | Cache& cache = i->second; |
| 38 | // mark this cache slot as more recently used so it won't get evicted anytime soon |
| 39 | cache.lruCounter = mLeastRecentlyUsedCounter++; |
| 40 | return {cache.slot, nullptr}; |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 41 | } |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 42 | return {cache(buffer), buffer}; |
| 43 | } |
| 44 | |
Brian Lindahl | b158a5c | 2022-12-15 15:21:13 -0700 | [diff] [blame^] | 45 | HwcSlotAndBuffer HwcBufferCache::getOverrideHwcSlotAndBuffer(const sp<GraphicBuffer>& buffer) { |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 46 | if (buffer == mLastOverrideBuffer) { |
| 47 | return {kOverrideBufferSlot, nullptr}; |
| 48 | } |
| 49 | mLastOverrideBuffer = buffer; |
| 50 | return {kOverrideBufferSlot, buffer}; |
| 51 | } |
| 52 | |
| 53 | uint32_t HwcBufferCache::uncache(uint64_t bufferId) { |
| 54 | if (auto i = mCacheByBufferId.find(bufferId); i != mCacheByBufferId.end()) { |
| 55 | uint32_t slot = i->second.slot; |
| 56 | mCacheByBufferId.erase(i); |
| 57 | mFreeSlots.push(slot); |
| 58 | return slot; |
| 59 | } |
| 60 | if (mLastOverrideBuffer && bufferId == mLastOverrideBuffer->getId()) { |
| 61 | mLastOverrideBuffer = nullptr; |
| 62 | return kOverrideBufferSlot; |
| 63 | } |
| 64 | return UINT32_MAX; |
| 65 | } |
| 66 | |
| 67 | uint32_t HwcBufferCache::cache(const sp<GraphicBuffer>& buffer) { |
| 68 | Cache cache; |
| 69 | cache.slot = getLeastRecentlyUsedSlot(); |
| 70 | cache.lruCounter = mLeastRecentlyUsedCounter++; |
| 71 | cache.buffer = buffer; |
| 72 | mCacheByBufferId.emplace(buffer->getId(), cache); |
| 73 | return cache.slot; |
| 74 | } |
| 75 | |
| 76 | uint32_t HwcBufferCache::getLeastRecentlyUsedSlot() { |
| 77 | if (mFreeSlots.empty()) { |
| 78 | assert(!mCacheByBufferId.empty()); |
| 79 | // evict the least recently used cache entry |
| 80 | auto cacheToErase = mCacheByBufferId.begin(); |
| 81 | for (auto i = cacheToErase; i != mCacheByBufferId.end(); ++i) { |
| 82 | if (i->second.lruCounter < cacheToErase->second.lruCounter) { |
| 83 | cacheToErase = i; |
| 84 | } |
| 85 | } |
| 86 | uint32_t slot = cacheToErase->second.slot; |
| 87 | mCacheByBufferId.erase(cacheToErase); |
| 88 | mFreeSlots.push(slot); |
| 89 | } |
| 90 | uint32_t slot = mFreeSlots.top(); |
| 91 | mFreeSlots.pop(); |
| 92 | return slot; |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 95 | } // namespace android::compositionengine::impl |