blob: 8613210cfc90833324bac8164a57b36a440ce971 [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 Hau6f89c372019-03-02 00:13:33 +000024 std::fill(std::begin(mBuffers), std::end(mBuffers),
25 std::pair<uint64_t, wp<GraphicBuffer>>(0, nullptr));
26}
27bool 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 Haub28f0072019-02-20 10:36:29 -080044}
Wale Ogunwale5723fbd2019-02-28 15:53:59 +000045
Valerie Hau6f89c372019-03-02 00:13:33 +000046uint32_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
51void HwcBufferCache::getHwcBuffer(const sp<GraphicBuffer>& buffer, uint32_t* outSlot,
Wale Ogunwale5723fbd2019-02-28 15:53:59 +000052 sp<GraphicBuffer>* outBuffer) {
Valerie Hau6f89c372019-03-02 00:13:33 +000053 bool cached = getSlot(buffer, outSlot);
Valerie Haub28f0072019-02-20 10:36:29 -080054
Valerie Hau6f89c372019-03-02 00:13:33 +000055 auto& [currentCounter, currentBuffer] = mBuffers[*outSlot];
56 if (cached) {
Chia-I Wuaaff73f2017-02-13 12:28:24 -080057 // already cached in HWC, skip sending the buffer
58 *outBuffer = nullptr;
Valerie Hau6f89c372019-03-02 00:13:33 +000059 currentCounter = getCounter();
Chia-I Wuaaff73f2017-02-13 12:28:24 -080060 } else {
61 *outBuffer = buffer;
62
63 // update cache
Valerie Hau6f89c372019-03-02 00:13:33 +000064 currentBuffer = buffer;
65 currentCounter = getCounter();
Chia-I Wuaaff73f2017-02-13 12:28:24 -080066 }
Chia-I Wuaaff73f2017-02-13 12:28:24 -080067}
68
Valerie Hau6f89c372019-03-02 00:13:33 +000069uint64_t HwcBufferCache::getCounter() {
70 return mCounter++;
71}
Lloyd Pique76ed7032018-12-04 17:24:28 -080072} // namespace android::compositionengine::impl