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 
19 // TODO(b/129481165): remove the #pragma below and fix conversion issues
20 #pragma clang diagnostic push
21 #pragma clang diagnostic ignored "-Wconversion"
22 
23 #include <gui/BufferQueue.h>
24 #include <ui/GraphicBuffer.h>
25 
26 // TODO(b/129481165): remove the #pragma below and fix conversion issues
27 #pragma clang diagnostic pop // ignored "-Wconversion"
28 
29 namespace android::compositionengine::impl {
30 
HwcBufferCache()31 HwcBufferCache::HwcBufferCache() {
32     std::fill(std::begin(mBuffers), std::end(mBuffers), wp<GraphicBuffer>(nullptr));
33 }
34 
getHwcBuffer(int slot,const sp<GraphicBuffer> & buffer,uint32_t * outSlot,sp<GraphicBuffer> * outBuffer)35 void HwcBufferCache::getHwcBuffer(int slot, const sp<GraphicBuffer>& buffer, uint32_t* outSlot,
36                                   sp<GraphicBuffer>* outBuffer) {
37     // default is 0
38     if (slot == BufferQueue::INVALID_BUFFER_SLOT || slot < 0 ||
39         slot >= BufferQueue::NUM_BUFFER_SLOTS) {
40         *outSlot = 0;
41     } else {
42         *outSlot = static_cast<uint32_t>(slot);
43     }
44 
45     auto& currentBuffer = mBuffers[*outSlot];
46     wp<GraphicBuffer> weakCopy(buffer);
47     if (currentBuffer == weakCopy) {
48         // already cached in HWC, skip sending the buffer
49         *outBuffer = nullptr;
50     } else {
51         *outBuffer = buffer;
52 
53         // update cache
54         currentBuffer = buffer;
55     }
56 }
57 
58 } // namespace android::compositionengine::impl
59