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 #pragma once
18 
19 #include <cstdint>
20 #include <vector>
21 
22 // TODO(b/129481165): remove the #pragma below and fix conversion issues
23 #pragma clang diagnostic push
24 #pragma clang diagnostic ignored "-Wconversion"
25 
26 #include <gui/BufferQueue.h>
27 
28 // TODO(b/129481165): remove the #pragma below and fix conversion issues
29 #pragma clang diagnostic pop // ignored "-Wconversion"
30 
31 #include <utils/StrongPointer.h>
32 
33 namespace android {
34 
35 class GraphicBuffer;
36 
37 namespace compositionengine::impl {
38 
39 // With HIDLized hwcomposer HAL, the HAL can maintain a buffer cache for each
40 // HWC display and layer.  When updating a display target or a layer buffer,
41 // we have the option to send the buffer handle over or to request the HAL to
42 // retrieve it from its cache.  The latter is cheaper since it eliminates the
43 // overhead to transfer the handle over the trasport layer, and the overhead
44 // for the HAL to clone and retain the handle.
45 //
46 // To be able to find out whether a buffer is already in the HAL's cache, we
47 // use HWComposerBufferCache to mirror the cache in SF.
48 class HwcBufferCache {
49 public:
50     HwcBufferCache();
51     // Given a buffer, return the HWC cache slot and
52     // buffer to be sent to HWC.
53     //
54     // outBuffer is set to buffer when buffer is not in the HWC cache;
55     // otherwise, outBuffer is set to nullptr.
56     void getHwcBuffer(int slot, const sp<GraphicBuffer>& buffer, uint32_t* outSlot,
57                       sp<GraphicBuffer>* outBuffer);
58 
59 private:
60     // an array where the index corresponds to a slot and the value corresponds to a (counter,
61     // buffer) pair. "counter" is a unique value that indicates the last time this slot was updated
62     // or used and allows us to keep track of the least-recently used buffer.
63     wp<GraphicBuffer> mBuffers[BufferQueue::NUM_BUFFER_SLOTS];
64 };
65 
66 } // namespace compositionengine::impl
67 } // namespace android
68