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 <compositionengine/impl/HwcBufferCache.h>
18 #include <gui/BufferQueue.h>
19 #include <ui/GraphicBuffer.h>
20 
21 namespace android::compositionengine::impl {
22 
HwcBufferCache()23 HwcBufferCache::HwcBufferCache() {
24     std::fill(std::begin(mBuffers), std::end(mBuffers), wp<GraphicBuffer>(nullptr));
25 }
26 
getHwcBuffer(int slot,const sp<GraphicBuffer> & buffer,uint32_t * outSlot,sp<GraphicBuffer> * outBuffer)27 void HwcBufferCache::getHwcBuffer(int slot, const sp<GraphicBuffer>& buffer, uint32_t* outSlot,
28                                   sp<GraphicBuffer>* outBuffer) {
29     // default is 0
30     if (slot == BufferQueue::INVALID_BUFFER_SLOT || slot < 0 ||
31         slot >= BufferQueue::NUM_BUFFER_SLOTS) {
32         *outSlot = 0;
33     } else {
34         *outSlot = slot;
35     }
36 
37     auto& currentBuffer = mBuffers[*outSlot];
38     wp<GraphicBuffer> weakCopy(buffer);
39     if (currentBuffer == weakCopy) {
40         // already cached in HWC, skip sending the buffer
41         *outBuffer = nullptr;
42     } else {
43         *outBuffer = buffer;
44 
45         // update cache
46         currentBuffer = buffer;
47     }
48 }
49 
50 } // namespace android::compositionengine::impl
51