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 
17 #include "HWComposerBufferCache.h"
18 
19 #include <gui/BufferQueue.h>
20 
21 namespace android {
22 
HWComposerBufferCache()23 HWComposerBufferCache::HWComposerBufferCache()
24 {
25     mBuffers.reserve(BufferQueue::NUM_BUFFER_SLOTS);
26 }
27 
getHwcBuffer(int slot,const sp<GraphicBuffer> & buffer,uint32_t * outSlot,sp<GraphicBuffer> * outBuffer)28 void HWComposerBufferCache::getHwcBuffer(int slot,
29         const sp<GraphicBuffer>& buffer,
30         uint32_t* outSlot, sp<GraphicBuffer>* outBuffer)
31 {
32     if (slot == BufferQueue::INVALID_BUFFER_SLOT || slot < 0) {
33         // default to slot 0
34         slot = 0;
35     }
36 
37     if (static_cast<size_t>(slot) >= mBuffers.size()) {
38         mBuffers.resize(slot + 1);
39     }
40 
41     *outSlot = slot;
42 
43     if (mBuffers[slot] == buffer) {
44         // already cached in HWC, skip sending the buffer
45         *outBuffer = nullptr;
46     } else {
47         *outBuffer = buffer;
48 
49         // update cache
50         mBuffers[slot] = buffer;
51     }
52 }
53 
54 } // namespace android
55