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 | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 17 | #include <compositionengine/impl/HwcBufferCache.h> |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 18 | #include <gui/BufferQueue.h> |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 19 | #include <ui/GraphicBuffer.h> |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 20 | |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 21 | namespace android::compositionengine::impl { |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 22 | |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 23 | HwcBufferCache::HwcBufferCache() { |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 24 | std::fill(std::begin(mBuffers), std::end(mBuffers), |
| 25 | std::pair<uint64_t, wp<GraphicBuffer>>(0, nullptr)); |
| 26 | } |
| 27 | bool HwcBufferCache::getSlot(const sp<GraphicBuffer>& buffer, uint32_t* outSlot) { |
| 28 | // search for cached buffer first |
| 29 | for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 30 | // Weak pointers in the cache may have had their object destroyed. |
| 31 | // Comparisons between weak pointers will accurately reflect this case, |
| 32 | // but comparisons between weak and strong may not. Thus, we create a weak |
| 33 | // pointer from strong pointer buffer |
| 34 | wp<GraphicBuffer> weakCopy(buffer); |
| 35 | if (mBuffers[i].second == weakCopy) { |
| 36 | *outSlot = i; |
| 37 | return true; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // use the least-recently used slot |
| 42 | *outSlot = getLeastRecentlyUsedSlot(); |
| 43 | return false; |
Valerie Hau | b28f007 | 2019-02-20 10:36:29 -0800 | [diff] [blame] | 44 | } |
Wale Ogunwale | 5723fbd | 2019-02-28 15:53:59 +0000 | [diff] [blame] | 45 | |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 46 | uint32_t HwcBufferCache::getLeastRecentlyUsedSlot() { |
| 47 | auto iter = std::min_element(std::begin(mBuffers), std::end(mBuffers)); |
| 48 | return std::distance(std::begin(mBuffers), iter); |
| 49 | } |
| 50 | |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame^] | 51 | void HwcBufferCache::getHwcBuffer(int slot, const sp<GraphicBuffer>& buffer, uint32_t* outSlot, |
Wale Ogunwale | 5723fbd | 2019-02-28 15:53:59 +0000 | [diff] [blame] | 52 | sp<GraphicBuffer>* outBuffer) { |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame^] | 53 | // if this slot corresponds to a BufferStateLayer, generate the slot |
| 54 | if (slot == BufferQueue::INVALID_BUFFER_SLOT) { |
| 55 | getSlot(buffer, outSlot); |
| 56 | } else if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) { |
| 57 | *outSlot = 0; |
| 58 | } else { |
| 59 | *outSlot = slot; |
| 60 | } |
Valerie Hau | b28f007 | 2019-02-20 10:36:29 -0800 | [diff] [blame] | 61 | |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 62 | auto& [currentCounter, currentBuffer] = mBuffers[*outSlot]; |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame^] | 63 | wp<GraphicBuffer> weakCopy(buffer); |
| 64 | if (currentBuffer == weakCopy) { |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 65 | // already cached in HWC, skip sending the buffer |
| 66 | *outBuffer = nullptr; |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 67 | currentCounter = getCounter(); |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 68 | } else { |
| 69 | *outBuffer = buffer; |
| 70 | |
| 71 | // update cache |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 72 | currentBuffer = buffer; |
| 73 | currentCounter = getCounter(); |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 74 | } |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 77 | uint64_t HwcBufferCache::getCounter() { |
| 78 | return mCounter++; |
| 79 | } |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 80 | } // namespace android::compositionengine::impl |