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 | b28f007 | 2019-02-20 10:36:29 -0800 | [diff] [blame^] | 24 | std::fill(std::begin(mBuffers), std::end(mBuffers), |
| 25 | std::pair<uint64_t, wp<GraphicBuffer>>(0, nullptr)); |
| 26 | } |
| 27 | int HwcBufferCache::getSlot(const sp<GraphicBuffer>& buffer) { |
| 28 | // search for cached buffer first |
| 29 | for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 30 | if (mBuffers[i].second == buffer) { |
| 31 | return i; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // use the least-recently used slot |
| 36 | return getLeastRecentlyUsedSlot(); |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Valerie Hau | b28f007 | 2019-02-20 10:36:29 -0800 | [diff] [blame^] | 39 | int HwcBufferCache::getLeastRecentlyUsedSlot() { |
| 40 | auto iter = std::min_element(std::begin(mBuffers), std::end(mBuffers)); |
| 41 | return std::distance(std::begin(mBuffers), iter); |
| 42 | } |
| 43 | |
| 44 | void HwcBufferCache::getHwcBuffer(const sp<GraphicBuffer>& buffer, uint32_t* outSlot, |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 45 | sp<GraphicBuffer>* outBuffer) { |
Valerie Hau | b28f007 | 2019-02-20 10:36:29 -0800 | [diff] [blame^] | 46 | *outSlot = getSlot(buffer); |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 47 | |
Valerie Hau | b28f007 | 2019-02-20 10:36:29 -0800 | [diff] [blame^] | 48 | auto& [currentCounter, currentBuffer] = mBuffers[*outSlot]; |
| 49 | if (currentBuffer == buffer) { |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 50 | // already cached in HWC, skip sending the buffer |
| 51 | *outBuffer = nullptr; |
Valerie Hau | b28f007 | 2019-02-20 10:36:29 -0800 | [diff] [blame^] | 52 | currentCounter = getCounter(); |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 53 | } else { |
| 54 | *outBuffer = buffer; |
| 55 | |
| 56 | // update cache |
Valerie Hau | b28f007 | 2019-02-20 10:36:29 -0800 | [diff] [blame^] | 57 | currentBuffer = buffer; |
| 58 | currentCounter = getCounter(); |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 59 | } |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Valerie Hau | b28f007 | 2019-02-20 10:36:29 -0800 | [diff] [blame^] | 62 | uint64_t HwcBufferCache::getCounter() { |
| 63 | return mCounter++; |
| 64 | } |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 65 | } // namespace android::compositionengine::impl |