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