blob: 6f340b96d0cf9277ac4276f081e1205e2aced8b1 [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() {
Wale Ogunwale5723fbd2019-02-28 15:53:59 +000024 mBuffers.reserve(BufferQueue::NUM_BUFFER_SLOTS);
Valerie Haub28f0072019-02-20 10:36:29 -080025}
Wale Ogunwale5723fbd2019-02-28 15:53:59 +000026
27void HwcBufferCache::getHwcBuffer(int slot, const sp<GraphicBuffer>& buffer, uint32_t* outSlot,
28 sp<GraphicBuffer>* outBuffer) {
29 if (slot == BufferQueue::INVALID_BUFFER_SLOT || slot < 0) {
30 // default to slot 0
31 slot = 0;
Valerie Haub28f0072019-02-20 10:36:29 -080032 }
33
Wale Ogunwale5723fbd2019-02-28 15:53:59 +000034 if (static_cast<size_t>(slot) >= mBuffers.size()) {
35 mBuffers.resize(slot + 1);
36 }
Chia-I Wuaaff73f2017-02-13 12:28:24 -080037
Wale Ogunwale5723fbd2019-02-28 15:53:59 +000038 *outSlot = slot;
Valerie Haub28f0072019-02-20 10:36:29 -080039
Wale Ogunwale5723fbd2019-02-28 15:53:59 +000040 if (mBuffers[slot] == buffer) {
Chia-I Wuaaff73f2017-02-13 12:28:24 -080041 // already cached in HWC, skip sending the buffer
42 *outBuffer = nullptr;
43 } else {
44 *outBuffer = buffer;
45
46 // update cache
Wale Ogunwale5723fbd2019-02-28 15:53:59 +000047 mBuffers[slot] = buffer;
Chia-I Wuaaff73f2017-02-13 12:28:24 -080048 }
Chia-I Wuaaff73f2017-02-13 12:28:24 -080049}
50
Lloyd Pique76ed7032018-12-04 17:24:28 -080051} // namespace android::compositionengine::impl