blob: 8177c7f25da4a90197bdde1d601b87babb7913a7 [file] [log] [blame]
Chia-I Wuaaff73f2017-02-13 12:28:24 -08001/*
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 Pique76ed7032018-12-04 17:24:28 -080017#include <compositionengine/impl/HwcBufferCache.h>
Chia-I Wuaaff73f2017-02-13 12:28:24 -080018#include <gui/BufferQueue.h>
Lloyd Pique76ed7032018-12-04 17:24:28 -080019#include <ui/GraphicBuffer.h>
Chia-I Wuaaff73f2017-02-13 12:28:24 -080020
Lloyd Pique76ed7032018-12-04 17:24:28 -080021namespace android::compositionengine::impl {
Chia-I Wuaaff73f2017-02-13 12:28:24 -080022
Lloyd Pique76ed7032018-12-04 17:24:28 -080023HwcBufferCache::HwcBufferCache() {
Valerie Haub28f0072019-02-20 10:36:29 -080024 std::fill(std::begin(mBuffers), std::end(mBuffers),
25 std::pair<uint64_t, wp<GraphicBuffer>>(0, nullptr));
26}
27int 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 Wuaaff73f2017-02-13 12:28:24 -080037}
38
Valerie Haub28f0072019-02-20 10:36:29 -080039int HwcBufferCache::getLeastRecentlyUsedSlot() {
40 auto iter = std::min_element(std::begin(mBuffers), std::end(mBuffers));
41 return std::distance(std::begin(mBuffers), iter);
42}
43
44void HwcBufferCache::getHwcBuffer(const sp<GraphicBuffer>& buffer, uint32_t* outSlot,
Lloyd Pique76ed7032018-12-04 17:24:28 -080045 sp<GraphicBuffer>* outBuffer) {
Valerie Haub28f0072019-02-20 10:36:29 -080046 *outSlot = getSlot(buffer);
Chia-I Wuaaff73f2017-02-13 12:28:24 -080047
Valerie Haub28f0072019-02-20 10:36:29 -080048 auto& [currentCounter, currentBuffer] = mBuffers[*outSlot];
49 if (currentBuffer == buffer) {
Chia-I Wuaaff73f2017-02-13 12:28:24 -080050 // already cached in HWC, skip sending the buffer
51 *outBuffer = nullptr;
Valerie Haub28f0072019-02-20 10:36:29 -080052 currentCounter = getCounter();
Chia-I Wuaaff73f2017-02-13 12:28:24 -080053 } else {
54 *outBuffer = buffer;
55
56 // update cache
Valerie Haub28f0072019-02-20 10:36:29 -080057 currentBuffer = buffer;
58 currentCounter = getCounter();
Chia-I Wuaaff73f2017-02-13 12:28:24 -080059 }
Chia-I Wuaaff73f2017-02-13 12:28:24 -080060}
61
Valerie Haub28f0072019-02-20 10:36:29 -080062uint64_t HwcBufferCache::getCounter() {
63 return mCounter++;
64}
Lloyd Pique76ed7032018-12-04 17:24:28 -080065} // namespace android::compositionengine::impl