xref: /aosp_15_r20/frameworks/native/include/gui/CpuConsumer.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2012 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 #ifndef ANDROID_GUI_CPUCONSUMER_H
18 #define ANDROID_GUI_CPUCONSUMER_H
19 
20 #include <system/window.h>
21 
22 #include <com_android_graphics_libgui_flags.h>
23 #include <gui/BufferQueue.h>
24 #include <gui/ConsumerBase.h>
25 
26 #include <utils/Vector.h>
27 
28 
29 namespace android {
30 
31 class BufferQueue;
32 class GraphicBuffer;
33 class String8;
34 
35 /**
36  * CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU
37  * access to the underlying gralloc buffers provided by BufferQueue. Multiple
38  * buffers may be acquired by it at once, to be used concurrently by the
39  * CpuConsumer owner. Sets gralloc usage flags to be software-read-only.
40  * This queue is synchronous by default.
41  */
42 
43 class CpuConsumer : public ConsumerBase
44 {
45   public:
46     typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
47 
48     struct LockedBuffer {
49         uint8_t    *data;
50         uint32_t    width;
51         uint32_t    height;
52         PixelFormat format;
53         uint32_t    stride;
54         Rect        crop;
55         uint32_t    transform;
56         uint32_t    scalingMode;
57         int64_t     timestamp;
58         android_dataspace dataSpace;
59         uint64_t    frameNumber;
60         // this is the same as format, except for formats that are compatible with
61         // a flexible format (e.g. HAL_PIXEL_FORMAT_YCbCr_420_888). In the latter
62         // case this contains that flexible format
63         PixelFormat flexFormat;
64         // Values below are only valid when using HAL_PIXEL_FORMAT_YCbCr_420_888
65         // or compatible format, in which case LockedBuffer::data
66         // contains the Y channel, and stride is the Y channel stride. For other
67         // formats, these will all be 0.
68         uint8_t    *dataCb;
69         uint8_t    *dataCr;
70         uint32_t    chromaStride;
71         uint32_t    chromaStep;
72 
LockedBufferLockedBuffer73         LockedBuffer() :
74             data(nullptr),
75             width(0),
76             height(0),
77             format(PIXEL_FORMAT_NONE),
78             stride(0),
79             crop(Rect::EMPTY_RECT),
80             transform(0),
81             scalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
82             timestamp(0),
83             dataSpace(HAL_DATASPACE_UNKNOWN),
84             frameNumber(0),
85             flexFormat(PIXEL_FORMAT_NONE),
86             dataCb(nullptr),
87             dataCr(nullptr),
88             chromaStride(0),
89             chromaStep(0)
90         {}
91     };
92 
93     // Create a new CPU consumer. The maxLockedBuffers parameter specifies
94     // how many buffers can be locked for user access at the same time.
95 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
96     CpuConsumer(size_t maxLockedBuffers, bool controlledByApp = false,
97                 bool isConsumerSurfaceFlinger = false);
98 
99     CpuConsumer(const sp<IGraphicBufferConsumer>& bq, size_t maxLockedBuffers,
100                 bool controlledByApp = false)
101             __attribute((deprecated("Prefer ctors that create their own surface and consumer.")));
102 #else
103     CpuConsumer(const sp<IGraphicBufferConsumer>& bq,
104             size_t maxLockedBuffers, bool controlledByApp = false);
105 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
106 
107     // Gets the next graphics buffer from the producer and locks it for CPU use,
108     // filling out the passed-in locked buffer structure with the native pointer
109     // and metadata. Returns BAD_VALUE if no new buffer is available, and
110     // NOT_ENOUGH_DATA if the maximum number of buffers is already locked.
111     //
112     // Only a fixed number of buffers can be locked at a time, determined by the
113     // construction-time maxLockedBuffers parameter. If INVALID_OPERATION is
114     // returned by lockNextBuffer, then old buffers must be returned to the queue
115     // by calling unlockBuffer before more buffers can be acquired.
116     status_t lockNextBuffer(LockedBuffer *nativeBuffer);
117 
118     // Returns a locked buffer to the queue, allowing it to be reused. Since
119     // only a fixed number of buffers may be locked at a time, old buffers must
120     // be released by calling unlockBuffer to ensure new buffers can be acquired by
121     // lockNextBuffer.
122     status_t unlockBuffer(const LockedBuffer &nativeBuffer);
123 
124   private:
125     // Maximum number of buffers that can be locked at a time
126     const size_t mMaxLockedBuffers;
127 
128     // Tracking for buffers acquired by the user
129     struct AcquiredBuffer {
130         static constexpr uintptr_t kUnusedId = 0;
131 
132         // Need to track the original mSlot index and the buffer itself because
133         // the mSlot entry may be freed/reused before the acquired buffer is
134         // released.
135         int mSlot;
136         sp<GraphicBuffer> mGraphicBuffer;
137         uintptr_t mLockedBufferId;
138 
AcquiredBufferAcquiredBuffer139         AcquiredBuffer() :
140                 mSlot(BufferQueue::INVALID_BUFFER_SLOT),
141                 mLockedBufferId(kUnusedId) {
142         }
143 
resetAcquiredBuffer144         void reset() {
145             mSlot = BufferQueue::INVALID_BUFFER_SLOT;
146             mGraphicBuffer.clear();
147             mLockedBufferId = kUnusedId;
148         }
149     };
150 
151     size_t findAcquiredBufferLocked(uintptr_t id) const;
152 
153     status_t lockBufferItem(const BufferItem& item, LockedBuffer* outBuffer) const;
154 
155     Vector<AcquiredBuffer> mAcquiredBuffers;
156 
157     // Count of currently locked buffers
158     size_t mCurrentLockedBuffers;
159 };
160 
161 } // namespace android
162 
163 #endif // ANDROID_GUI_CPUCONSUMER_H
164