• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 <com_android_graphics_libgui_flags.h>
20 #include <gui/OccupancyTracker.h>
21 
22 #include <binder/IInterface.h>
23 #include <binder/SafeInterface.h>
24 
25 #include <EGL/egl.h>
26 #include <EGL/eglext.h>
27 
28 #include <ui/PixelFormat.h>
29 
30 #include <utils/Errors.h>
31 
32 namespace android {
33 
34 class BufferItem;
35 class Fence;
36 class GraphicBuffer;
37 class IConsumerListener;
38 class NativeHandle;
39 
40 /*
41  * See IGraphicBufferProducer for details on SLOT_COUNT.
42  */
43 class IGraphicBufferConsumer : public RefBase {
44 public:
45     enum {
46         // Returned by releaseBuffer, after which the consumer must free any references to the
47         // just-released buffer that it might have.
48         STALE_BUFFER_SLOT = 1,
49         // Returned by dequeueBuffer if there are no pending buffers available.
50         NO_BUFFER_AVAILABLE,
51         // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
52         PRESENT_LATER,
53     };
54 
55     // acquireBuffer attempts to acquire ownership of the next pending buffer in the BufferQueue.
56     // If no buffer is pending then it returns NO_BUFFER_AVAILABLE. If a buffer is successfully
57     // acquired, the information about the buffer is returned in BufferItem.
58     //
59     // If the buffer returned had previously been acquired then the BufferItem::mGraphicBuffer field
60     // of buffer is set to NULL and it is assumed that the consumer still holds a reference to the
61     // buffer.
62     //
63     // If presentWhen is non-zero, it indicates the time when the buffer will be displayed on
64     // screen. If the buffer's timestamp is farther in the future, the buffer won't be acquired, and
65     // PRESENT_LATER will be returned. The presentation time is in nanoseconds, and the time base
66     // is CLOCK_MONOTONIC.
67     //
68     // If maxFrameNumber is non-zero, it indicates that acquireBuffer should only return a buffer
69     // with a frame number less than or equal to maxFrameNumber. If no such frame is available
70     // (such as when a buffer has been replaced but the consumer has not received the
71     // onFrameReplaced callback), then PRESENT_LATER will be returned.
72     //
73     // Return of NO_ERROR means the operation completed as normal.
74     //
75     // Return of a positive value means the operation could not be completed at this time, but the
76     // user should try again later:
77     // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
78     // * PRESENT_LATER - the buffer's timestamp is farther in the future
79     //
80     // Return of a negative value means an error has occurred:
81     // * INVALID_OPERATION - too many buffers have been acquired
82     virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen,
83                                    uint64_t maxFrameNumber = 0) = 0;
84 
85     // detachBuffer attempts to remove all ownership of the buffer in the given slot from the buffer
86     // queue. If this call succeeds, the slot will be freed, and there will be no way to obtain the
87     // buffer from this interface. The freed slot will remain unallocated until either it is
88     // selected to hold a freshly allocated buffer in dequeueBuffer or a buffer is attached to the
89     // slot. The buffer must have already been acquired.
90     //
91     // Return of a value other than NO_ERROR means an error has occurred:
92     // * BAD_VALUE - the given slot number is invalid, either because it is out of the range
93     //               [0, SLOT_COUNT) or because the slot it refers to is not
94     //               currently acquired.
95     virtual status_t detachBuffer(int slot) = 0;
96 
97     // attachBuffer attempts to transfer ownership of a buffer to the BufferQueue. If this call
98     // succeeds, it will be as if this buffer was acquired from the returned slot number. As such,
99     // this call will fail if attaching this buffer would cause too many buffers to be
100     // simultaneously acquired.
101     //
102     // If the buffer is successfully attached, its frameNumber is initialized to 0. This must be
103     // passed into the releaseBuffer call or else the buffer will be deallocated as stale.
104     //
105     // Return of a value other than NO_ERROR means an error has occurred:
106     // * BAD_VALUE - outSlot or buffer were NULL, or the generation number of the buffer did not
107     //               match the BufferQueue.
108     // * INVALID_OPERATION - cannot attach the buffer because it would cause too many buffers
109     //                       to be acquired.
110     // * NO_MEMORY - no free slots available
111     virtual status_t attachBuffer(int* outSlot, const sp<GraphicBuffer>& buffer) = 0;
112 
113     // releaseBuffer releases a buffer slot from the consumer back to the BufferQueue. This may be
114     // done while the buffer's contents are still being accessed. The fence will signal when the
115     // buffer is no longer in use. frameNumber is used to identify the exact buffer returned.
116     //
117     // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free any references to the
118     // just-released buffer that it might have, as if it had received a onBuffersReleased() call
119     // with a mask set for the released buffer.
120     //
121     // Note that the dependencies on EGL will be removed once we switch to using the Android HW
122     // Sync HAL.
123     //
124     // Return of NO_ERROR means the operation completed as normal.
125     //
126     // Return of a positive value means the operation could not be completed at this time, but the
127     // user should try again later:
128     // * STALE_BUFFER_SLOT - see above (second paragraph)
129     //
130     // Return of a negative value means an error has occurred:
131     // * BAD_VALUE - one of the following could've happened:
132     //               * the buffer slot was invalid
133     //               * the fence was NULL
134     //               * the buffer slot specified is not in the acquired state
135 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
136     virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
137                                    const sp<Fence>& releaseFence) = 0;
138 #else
139     virtual status_t releaseBuffer(int buf, uint64_t frameNumber, EGLDisplay display,
140                                    EGLSyncKHR fence, const sp<Fence>& releaseFence) = 0;
141 
releaseBuffer(int buf,uint64_t frameNumber,const sp<Fence> & releaseFence)142     status_t releaseBuffer(int buf, uint64_t frameNumber, const sp<Fence>& releaseFence) {
143         return releaseBuffer(buf, frameNumber, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, releaseFence);
144     }
145 #endif
146 
147     // consumerConnect connects a consumer to the BufferQueue. Only one consumer may be connected,
148     // and when that consumer disconnects the BufferQueue is placed into the "abandoned" state,
149     // causing most interactions with the BufferQueue by the producer to fail. controlledByApp
150     // indicates whether the consumer is controlled by the application.
151     //
152     // consumer may not be NULL.
153     //
154     // Return of a value other than NO_ERROR means an error has occurred:
155     // * NO_INIT - the BufferQueue has been abandoned
156     // * BAD_VALUE - a NULL consumer was provided
157     virtual status_t consumerConnect(const sp<IConsumerListener>& consumer,
158                                      bool controlledByApp) = 0;
159 
160     // consumerDisconnect disconnects a consumer from the BufferQueue. All buffers will be freed and
161     // the BufferQueue is placed in the "abandoned" state, causing most interactions with the
162     // BufferQueue by the producer to fail.
163     //
164     // Return of a value other than NO_ERROR means an error has occurred:
165     // * BAD_VALUE - no consumer is currently connected
166     virtual status_t consumerDisconnect() = 0;
167 
168     // getReleasedBuffers sets the value pointed to by slotMask to a bit set. Each bit index with a
169     // 1 corresponds to a released buffer slot with that index value. In particular, a released
170     // buffer is one that has been released by the BufferQueue but has not yet been released by
171     // the consumer.
172     //
173     // This should be called from the onBuffersReleased() callback.
174     //
175     // Return of a value other than NO_ERROR means an error has occurred:
176     // * NO_INIT - the BufferQueue has been abandoned.
177     virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
178 
179 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
180     // getReleasedBuffersExtended for each slot, sets slotMask[slot] to 1 if it
181     // corresponds to a released buffer slot. In particular, a released buffer
182     // is one that has been released by the BufferQueue but has not yet been
183     // released by the consumer.
184     //
185     // This should be called from the onBuffersReleased() callback.
186     //
187     // Return of a value other than NO_ERROR means an error has occurred:
188     // * NO_INIT - the BufferQueue has been abandoned.
189     virtual status_t getReleasedBuffersExtended(std::vector<bool>* slotMask) = 0;
190 #endif
191 
192     // setDefaultBufferSize is used to set the size of buffers returned by dequeueBuffer when a
193     // width and height of zero is requested. Default is 1x1.
194     //
195     // Return of a value other than NO_ERROR means an error has occurred:
196     // * BAD_VALUE - either w or h was zero
197     virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
198 
199 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
200     // allowUnlimitedSlots allows the producer to set the upper bound on slots.
201     //
202     // Must be called before the producer is connected. If the producer
203     // increases the slot count, an IConsumerListener::onSlotCountChanged
204     // update is sent.
205     //
206     // This can not be used with setMaxBufferCount. Calls after
207     // setMaxBufferCount will fail and calls to setMaxBufferCount after setting
208     // this to true will fail.
209     //
210     // Return of a value other than NO_ERROR means an error has occurred:
211     // * NO_INIT - the BufferQueue has been abandoned
212     // * INVALID_OPERATION - one of the following errors has occurred:
213     //                       * Producer has been connected
214     //                       * setMaxBufferCount has been called and shrunk the
215     //                         BufferQueue.
216     virtual status_t allowUnlimitedSlots(bool allowUnlimitedSlots) = 0;
217 #endif
218 
219     // setMaxBufferCount sets the maximum value for the number of buffers used in the BufferQueue
220     // (the initial default is NUM_BUFFER_SLOTS). If a call to setMaxAcquiredBufferCount (by the
221     // consumer), or a call to setAsyncMode or setMaxDequeuedBufferCount (by the producer), would
222     // cause this value to be exceeded then that call will fail. This call will fail if a producer
223     // is connected to the BufferQueue.
224     //
225     // The count must be between 1 and NUM_BUFFER_SLOTS, inclusive. The count cannot be less than
226     // maxAcquiredBufferCount.
227     //
228     // Return of a value other than NO_ERROR means an error has occurred:
229     // * BAD_VALUE - one of the below conditions occurred:
230     //               * bufferCount was out of range (see above).
231     //               * failure to adjust the number of available slots.
232     // * INVALID_OPERATION - attempting to call this after a producer connected.
233     virtual status_t setMaxBufferCount(int bufferCount) = 0;
234 
235     // setMaxAcquiredBufferCount sets the maximum number of buffers that can be acquired by the
236     // consumer at one time (default 1). If this method succeeds, any new buffer slots will be both
237     // unallocated and owned by the BufferQueue object (i.e. they are not owned by the producer or
238     // consumer). Calling this may also cause some buffer slots to be emptied.
239     //
240     // This function should not be called with a value of maxAcquiredBuffers that is less than the
241     // number of currently acquired buffer slots. Doing so will result in a BAD_VALUE error.
242     //
243     // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS. It also cannot
244     // cause the maxBufferCount value to be exceeded.
245     //
246     // If called with onBuffersReleasedCallback, that call back will be called in lieu of
247     // IConsumerListener::onBuffersReleased.
248     //
249     // Return of a value other than NO_ERROR means an error has occurred:
250     // * NO_INIT - the BufferQueue has been abandoned
251     // * BAD_VALUE - one of the below conditions occurred:
252     //               * maxAcquiredBuffers was out of range (see above).
253     //               * failure to adjust the number of available slots.
254     //               * client would have more than the requested number of acquired buffers after
255     //                 this call
256     // * INVALID_OPERATION - attempting to call this after a producer connected.
257     virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
258 
259     using OnBufferReleasedCallback = std::function<void(void)>;
260     virtual status_t setMaxAcquiredBufferCount(
261             int maxAcquiredBuffers,
262             std::optional<OnBufferReleasedCallback> onBuffersReleasedCallback) = 0;
263 
264     // setConsumerName sets the name used in logging
265     virtual status_t setConsumerName(const String8& name) = 0;
266 
267     // setDefaultBufferFormat allows the BufferQueue to create GraphicBuffers of a defaultFormat if
268     // no format is specified in dequeueBuffer. The initial default is PIXEL_FORMAT_RGBA_8888.
269     //
270     // Return of a value other than NO_ERROR means an unknown error has occurred.
271     virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
272 
273     // setDefaultBufferDataSpace is a request to the producer to provide buffers of the indicated
274     // dataSpace. The producer may ignore this request. The initial default is
275     // HAL_DATASPACE_UNKNOWN.
276     //
277     // Return of a value other than NO_ERROR means an unknown error has occurred.
278     virtual status_t setDefaultBufferDataSpace(android_dataspace defaultDataSpace) = 0;
279 
280     // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer. These are merged
281     // with the bits passed to dequeueBuffer. The values are enumerated in gralloc.h,
282     // e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
283     //
284     // Return of a value other than NO_ERROR means an unknown error has occurred.
285     virtual status_t setConsumerUsageBits(uint64_t usage) = 0;
286 
287     // setConsumerIsProtected will turn on an internal bit that indicates whether
288     // the consumer can handle protected gralloc buffers (i.e. with
289     // GRALLOC_USAGE_PROTECTED set). IGraphicBufferProducer can query this
290     // capability using NATIVE_WINDOW_CONSUMER_IS_PROTECTED.
291     virtual status_t setConsumerIsProtected(bool isProtected) = 0;
292 
293     // setTransformHint bakes in rotation to buffers so overlays can be used. The values are
294     // enumerated in window.h, e.g. NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0
295     // (no transform).
296     //
297     // Return of a value other than NO_ERROR means an unknown error has occurred.
298     virtual status_t setTransformHint(uint32_t hint) = 0;
299 
300     // Retrieve the sideband buffer stream, if any.
301     virtual status_t getSidebandStream(sp<NativeHandle>* outStream) const = 0;
302 
303     // Retrieves any stored segments of the occupancy history of this BufferQueue and clears them.
304     // Optionally closes out the pending segment if forceFlush is true.
305     virtual status_t getOccupancyHistory(bool forceFlush,
306                                          std::vector<OccupancyTracker::Segment>* outHistory) = 0;
307 
308     // discardFreeBuffers releases all currently-free buffers held by the BufferQueue, in order to
309     // reduce the memory consumption of the BufferQueue to the minimum possible without
310     // discarding data.
311     // The consumer invoking this method is responsible for calling getReleasedBuffers() after this
312     // call to free up any of its locally cached buffers.
313     virtual status_t discardFreeBuffers() = 0;
314 
315     // dump state into a string
316     virtual status_t dumpState(const String8& prefix, String8* outResult) const = 0;
317 
318     // Provide backwards source compatibility
dumpState(String8 & result,const char * prefix)319     void dumpState(String8& result, const char* prefix) {
320         String8 returned;
321         dumpState(String8(prefix), &returned);
322         result.append(returned);
323     }
324 };
325 
326 } // namespace android
327