xref: /aosp_15_r20/frameworks/native/include/gui/BufferQueueConsumer.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2014 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #ifndef ANDROID_GUI_BUFFERQUEUECONSUMER_H
18*38e8c45fSAndroid Build Coastguard Worker #define ANDROID_GUI_BUFFERQUEUECONSUMER_H
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include <EGL/egl.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <EGL/eglext.h>
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #include <gui/BufferQueueDefs.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <gui/IGraphicBufferConsumer.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <utils/String8.h>
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker namespace android {
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker class BufferQueueCore;
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker class BufferQueueConsumer : public BnGraphicBufferConsumer {
32*38e8c45fSAndroid Build Coastguard Worker 
33*38e8c45fSAndroid Build Coastguard Worker public:
34*38e8c45fSAndroid Build Coastguard Worker     explicit BufferQueueConsumer(const sp<BufferQueueCore>& core);
35*38e8c45fSAndroid Build Coastguard Worker     ~BufferQueueConsumer() override;
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker     // acquireBuffer attempts to acquire ownership of the next pending buffer in
38*38e8c45fSAndroid Build Coastguard Worker     // the BufferQueue. If no buffer is pending then it returns
39*38e8c45fSAndroid Build Coastguard Worker     // NO_BUFFER_AVAILABLE. If a buffer is successfully acquired, the
40*38e8c45fSAndroid Build Coastguard Worker     // information about the buffer is returned in BufferItem.  If the buffer
41*38e8c45fSAndroid Build Coastguard Worker     // returned had previously been acquired then the BufferItem::mGraphicBuffer
42*38e8c45fSAndroid Build Coastguard Worker     // field of buffer is set to NULL and it is assumed that the consumer still
43*38e8c45fSAndroid Build Coastguard Worker     // holds a reference to the buffer.
44*38e8c45fSAndroid Build Coastguard Worker     //
45*38e8c45fSAndroid Build Coastguard Worker     // If expectedPresent is nonzero, it indicates the time when the buffer
46*38e8c45fSAndroid Build Coastguard Worker     // will be displayed on screen. If the buffer's timestamp is farther in the
47*38e8c45fSAndroid Build Coastguard Worker     // future, the buffer won't be acquired, and PRESENT_LATER will be
48*38e8c45fSAndroid Build Coastguard Worker     // returned.  The presentation time is in nanoseconds, and the time base
49*38e8c45fSAndroid Build Coastguard Worker     // is CLOCK_MONOTONIC.
50*38e8c45fSAndroid Build Coastguard Worker     virtual status_t acquireBuffer(BufferItem* outBuffer,
51*38e8c45fSAndroid Build Coastguard Worker             nsecs_t expectedPresent, uint64_t maxFrameNumber = 0) override;
52*38e8c45fSAndroid Build Coastguard Worker 
53*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::detachBuffer
54*38e8c45fSAndroid Build Coastguard Worker     virtual status_t detachBuffer(int slot);
55*38e8c45fSAndroid Build Coastguard Worker 
56*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::attachBuffer
57*38e8c45fSAndroid Build Coastguard Worker     virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer);
58*38e8c45fSAndroid Build Coastguard Worker 
59*38e8c45fSAndroid Build Coastguard Worker     // releaseBuffer releases a buffer slot from the consumer back to the
60*38e8c45fSAndroid Build Coastguard Worker     // BufferQueue.  This may be done while the buffer's contents are still
61*38e8c45fSAndroid Build Coastguard Worker     // being accessed.  The fence will signal when the buffer is no longer
62*38e8c45fSAndroid Build Coastguard Worker     // in use. frameNumber is used to indentify the exact buffer returned.
63*38e8c45fSAndroid Build Coastguard Worker     //
64*38e8c45fSAndroid Build Coastguard Worker     // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
65*38e8c45fSAndroid Build Coastguard Worker     // any references to the just-released buffer that it might have, as if it
66*38e8c45fSAndroid Build Coastguard Worker     // had received a onBuffersReleased() call with a mask set for the released
67*38e8c45fSAndroid Build Coastguard Worker     // buffer.
68*38e8c45fSAndroid Build Coastguard Worker     //
69*38e8c45fSAndroid Build Coastguard Worker     // Note that the dependencies on EGL will be removed once we switch to using
70*38e8c45fSAndroid Build Coastguard Worker     // the Android HW Sync HAL.
71*38e8c45fSAndroid Build Coastguard Worker     virtual status_t releaseBuffer(int slot, uint64_t frameNumber,
72*38e8c45fSAndroid Build Coastguard Worker             const sp<Fence>& releaseFence, EGLDisplay display,
73*38e8c45fSAndroid Build Coastguard Worker             EGLSyncKHR fence);
74*38e8c45fSAndroid Build Coastguard Worker 
75*38e8c45fSAndroid Build Coastguard Worker     // connect connects a consumer to the BufferQueue.  Only one
76*38e8c45fSAndroid Build Coastguard Worker     // consumer may be connected, and when that consumer disconnects the
77*38e8c45fSAndroid Build Coastguard Worker     // BufferQueue is placed into the "abandoned" state, causing most
78*38e8c45fSAndroid Build Coastguard Worker     // interactions with the BufferQueue by the producer to fail.
79*38e8c45fSAndroid Build Coastguard Worker     // controlledByApp indicates whether the consumer is controlled by
80*38e8c45fSAndroid Build Coastguard Worker     // the application.
81*38e8c45fSAndroid Build Coastguard Worker     //
82*38e8c45fSAndroid Build Coastguard Worker     // consumerListener may not be NULL.
83*38e8c45fSAndroid Build Coastguard Worker     virtual status_t connect(const sp<IConsumerListener>& consumerListener,
84*38e8c45fSAndroid Build Coastguard Worker             bool controlledByApp);
85*38e8c45fSAndroid Build Coastguard Worker 
86*38e8c45fSAndroid Build Coastguard Worker     // disconnect disconnects a consumer from the BufferQueue. All
87*38e8c45fSAndroid Build Coastguard Worker     // buffers will be freed and the BufferQueue is placed in the "abandoned"
88*38e8c45fSAndroid Build Coastguard Worker     // state, causing most interactions with the BufferQueue by the producer to
89*38e8c45fSAndroid Build Coastguard Worker     // fail.
90*38e8c45fSAndroid Build Coastguard Worker     virtual status_t disconnect();
91*38e8c45fSAndroid Build Coastguard Worker 
92*38e8c45fSAndroid Build Coastguard Worker     // getReleasedBuffers sets the value pointed to by outSlotMask to a bit mask
93*38e8c45fSAndroid Build Coastguard Worker     // indicating which buffer slots have been released by the BufferQueue
94*38e8c45fSAndroid Build Coastguard Worker     // but have not yet been released by the consumer.
95*38e8c45fSAndroid Build Coastguard Worker     //
96*38e8c45fSAndroid Build Coastguard Worker     // This should be called from the onBuffersReleased() callback.
97*38e8c45fSAndroid Build Coastguard Worker     virtual status_t getReleasedBuffers(uint64_t* outSlotMask);
98*38e8c45fSAndroid Build Coastguard Worker 
99*38e8c45fSAndroid Build Coastguard Worker     // setDefaultBufferSize is used to set the size of buffers returned by
100*38e8c45fSAndroid Build Coastguard Worker     // dequeueBuffer when a width and height of zero is requested.  Default
101*38e8c45fSAndroid Build Coastguard Worker     // is 1x1.
102*38e8c45fSAndroid Build Coastguard Worker     virtual status_t setDefaultBufferSize(uint32_t width, uint32_t height);
103*38e8c45fSAndroid Build Coastguard Worker 
104*38e8c45fSAndroid Build Coastguard Worker     // see IGraphicBufferConsumer::setMaxBufferCount
105*38e8c45fSAndroid Build Coastguard Worker     virtual status_t setMaxBufferCount(int bufferCount);
106*38e8c45fSAndroid Build Coastguard Worker 
107*38e8c45fSAndroid Build Coastguard Worker     // setMaxAcquiredBufferCount sets the maximum number of buffers that can
108*38e8c45fSAndroid Build Coastguard Worker     // be acquired by the consumer at one time (default 1).  This call will
109*38e8c45fSAndroid Build Coastguard Worker     // fail if a producer is connected to the BufferQueue.
110*38e8c45fSAndroid Build Coastguard Worker     virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
111*38e8c45fSAndroid Build Coastguard Worker 
112*38e8c45fSAndroid Build Coastguard Worker     // setConsumerName sets the name used in logging
113*38e8c45fSAndroid Build Coastguard Worker     status_t setConsumerName(const String8& name) override;
114*38e8c45fSAndroid Build Coastguard Worker 
115*38e8c45fSAndroid Build Coastguard Worker     // setDefaultBufferFormat allows the BufferQueue to create
116*38e8c45fSAndroid Build Coastguard Worker     // GraphicBuffers of a defaultFormat if no format is specified
117*38e8c45fSAndroid Build Coastguard Worker     // in dequeueBuffer. The initial default is HAL_PIXEL_FORMAT_RGBA_8888.
118*38e8c45fSAndroid Build Coastguard Worker     virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat);
119*38e8c45fSAndroid Build Coastguard Worker 
120*38e8c45fSAndroid Build Coastguard Worker     // setDefaultBufferDataSpace allows the BufferQueue to create
121*38e8c45fSAndroid Build Coastguard Worker     // GraphicBuffers of a defaultDataSpace if no data space is specified
122*38e8c45fSAndroid Build Coastguard Worker     // in queueBuffer.
123*38e8c45fSAndroid Build Coastguard Worker     // The initial default is HAL_DATASPACE_UNKNOWN
124*38e8c45fSAndroid Build Coastguard Worker     virtual status_t setDefaultBufferDataSpace(android_dataspace defaultDataSpace);
125*38e8c45fSAndroid Build Coastguard Worker 
126*38e8c45fSAndroid Build Coastguard Worker     // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
127*38e8c45fSAndroid Build Coastguard Worker     // These are merged with the bits passed to dequeueBuffer.  The values are
128*38e8c45fSAndroid Build Coastguard Worker     // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
129*38e8c45fSAndroid Build Coastguard Worker     virtual status_t setConsumerUsageBits(uint64_t usage) override;
130*38e8c45fSAndroid Build Coastguard Worker 
131*38e8c45fSAndroid Build Coastguard Worker     // setConsumerIsProtected will turn on an internal bit that indicates whether
132*38e8c45fSAndroid Build Coastguard Worker     // the consumer can handle protected gralloc buffers (i.e. with
133*38e8c45fSAndroid Build Coastguard Worker     // GRALLOC_USAGE_PROTECTED set). IGraphicBufferProducer can query this
134*38e8c45fSAndroid Build Coastguard Worker     // capability using NATIVE_WINDOW_CONSUMER_IS_PROTECTED.
135*38e8c45fSAndroid Build Coastguard Worker     virtual status_t setConsumerIsProtected(bool isProtected);
136*38e8c45fSAndroid Build Coastguard Worker 
137*38e8c45fSAndroid Build Coastguard Worker     // setTransformHint bakes in rotation to buffers so overlays can be used.
138*38e8c45fSAndroid Build Coastguard Worker     // The values are enumerated in window.h, e.g.
139*38e8c45fSAndroid Build Coastguard Worker     // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform).
140*38e8c45fSAndroid Build Coastguard Worker     virtual status_t setTransformHint(uint32_t hint);
141*38e8c45fSAndroid Build Coastguard Worker 
142*38e8c45fSAndroid Build Coastguard Worker     // Retrieve the sideband buffer stream, if any.
143*38e8c45fSAndroid Build Coastguard Worker     status_t getSidebandStream(sp<NativeHandle>* outStream) const override;
144*38e8c45fSAndroid Build Coastguard Worker 
145*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::getOccupancyHistory
146*38e8c45fSAndroid Build Coastguard Worker     virtual status_t getOccupancyHistory(bool forceFlush,
147*38e8c45fSAndroid Build Coastguard Worker             std::vector<OccupancyTracker::Segment>* outHistory) override;
148*38e8c45fSAndroid Build Coastguard Worker 
149*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::discardFreeBuffers
150*38e8c45fSAndroid Build Coastguard Worker     virtual status_t discardFreeBuffers() override;
151*38e8c45fSAndroid Build Coastguard Worker 
152*38e8c45fSAndroid Build Coastguard Worker     // dump our state in a String
153*38e8c45fSAndroid Build Coastguard Worker     status_t dumpState(const String8& prefix, String8* outResult) const override;
154*38e8c45fSAndroid Build Coastguard Worker 
155*38e8c45fSAndroid Build Coastguard Worker     // Functions required for backwards compatibility.
156*38e8c45fSAndroid Build Coastguard Worker     // These will be modified/renamed in IGraphicBufferConsumer and will be
157*38e8c45fSAndroid Build Coastguard Worker     // removed from this class at that time. See b/13306289.
158*38e8c45fSAndroid Build Coastguard Worker 
releaseBuffer(int buf,uint64_t frameNumber,EGLDisplay display,EGLSyncKHR fence,const sp<Fence> & releaseFence)159*38e8c45fSAndroid Build Coastguard Worker     virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
160*38e8c45fSAndroid Build Coastguard Worker             EGLDisplay display, EGLSyncKHR fence,
161*38e8c45fSAndroid Build Coastguard Worker             const sp<Fence>& releaseFence) {
162*38e8c45fSAndroid Build Coastguard Worker         return releaseBuffer(buf, frameNumber, releaseFence, display, fence);
163*38e8c45fSAndroid Build Coastguard Worker     }
164*38e8c45fSAndroid Build Coastguard Worker 
consumerConnect(const sp<IConsumerListener> & consumer,bool controlledByApp)165*38e8c45fSAndroid Build Coastguard Worker     virtual status_t consumerConnect(const sp<IConsumerListener>& consumer,
166*38e8c45fSAndroid Build Coastguard Worker             bool controlledByApp) {
167*38e8c45fSAndroid Build Coastguard Worker         return connect(consumer, controlledByApp);
168*38e8c45fSAndroid Build Coastguard Worker     }
169*38e8c45fSAndroid Build Coastguard Worker 
consumerDisconnect()170*38e8c45fSAndroid Build Coastguard Worker     virtual status_t consumerDisconnect() { return disconnect(); }
171*38e8c45fSAndroid Build Coastguard Worker 
172*38e8c45fSAndroid Build Coastguard Worker     // End functions required for backwards compatibility
173*38e8c45fSAndroid Build Coastguard Worker 
174*38e8c45fSAndroid Build Coastguard Worker     // Value used to determine if present time is valid.
175*38e8c45fSAndroid Build Coastguard Worker     constexpr static int MAX_REASONABLE_NSEC = 1'000'000'000ULL; // 1 second
176*38e8c45fSAndroid Build Coastguard Worker 
177*38e8c45fSAndroid Build Coastguard Worker     // This allows the consumer to acquire an additional buffer if that buffer is not droppable and
178*38e8c45fSAndroid Build Coastguard Worker     // will eventually be released or acquired by the consumer.
179*38e8c45fSAndroid Build Coastguard Worker     void setAllowExtraAcquire(bool /* allow */);
180*38e8c45fSAndroid Build Coastguard Worker 
181*38e8c45fSAndroid Build Coastguard Worker private:
182*38e8c45fSAndroid Build Coastguard Worker     sp<BufferQueueCore> mCore;
183*38e8c45fSAndroid Build Coastguard Worker 
184*38e8c45fSAndroid Build Coastguard Worker     // This references mCore->mSlots. Lock mCore->mMutex while accessing.
185*38e8c45fSAndroid Build Coastguard Worker     BufferQueueDefs::SlotsType& mSlots;
186*38e8c45fSAndroid Build Coastguard Worker 
187*38e8c45fSAndroid Build Coastguard Worker     // This is a cached copy of the name stored in the BufferQueueCore.
188*38e8c45fSAndroid Build Coastguard Worker     // It's updated during setConsumerName.
189*38e8c45fSAndroid Build Coastguard Worker     String8 mConsumerName;
190*38e8c45fSAndroid Build Coastguard Worker 
191*38e8c45fSAndroid Build Coastguard Worker }; // class BufferQueueConsumer
192*38e8c45fSAndroid Build Coastguard Worker 
193*38e8c45fSAndroid Build Coastguard Worker } // namespace android
194*38e8c45fSAndroid Build Coastguard Worker 
195*38e8c45fSAndroid Build Coastguard Worker #endif
196