blob: e983c160a5fb8df9bdee9001569cf83f4dc3f8b8 [file] [log] [blame]
Mathias Agopiana4e19522013-07-31 20:09:53 -07001/*
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#ifndef ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H
18#define ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/Errors.h>
24#include <utils/RefBase.h>
25#include <utils/Timers.h>
26
27#include <binder/IInterface.h>
Dan Stozad723bd72014-11-18 10:24:03 -080028#include <ui/PixelFormat.h>
Mathias Agopiana4e19522013-07-31 20:09:53 -070029#include <ui/Rect.h>
30
Dan Stoza99b18b42014-03-28 15:34:33 -070031#include <EGL/egl.h>
32#include <EGL/eglext.h>
33
Mathias Agopiana4e19522013-07-31 20:09:53 -070034namespace android {
35// ----------------------------------------------------------------------------
36
Dan Stozade7100a2015-03-11 16:38:47 -070037class BufferItem;
Mathias Agopiana4e19522013-07-31 20:09:53 -070038class Fence;
Jesse Hall399184a2014-03-03 15:42:54 -080039class GraphicBuffer;
40class IConsumerListener;
41class NativeHandle;
Mathias Agopiana4e19522013-07-31 20:09:53 -070042
43class IGraphicBufferConsumer : public IInterface {
44
45public:
Igor Murashkin7d2d1602013-11-12 18:02:20 -080046 enum {
47 // Returned by releaseBuffer, after which the consumer must
48 // free any references to the just-released buffer that it might have.
49 STALE_BUFFER_SLOT = 1,
50 // Returned by dequeueBuffer if there are no pending buffers available.
51 NO_BUFFER_AVAILABLE,
52 // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
53 PRESENT_LATER,
54 };
Mathias Agopiana4e19522013-07-31 20:09:53 -070055
56 // acquireBuffer attempts to acquire ownership of the next pending buffer in
Igor Murashkin7d2d1602013-11-12 18:02:20 -080057 // the BufferQueue. If no buffer is pending then it returns
58 // NO_BUFFER_AVAILABLE. If a buffer is successfully acquired, the
59 // information about the buffer is returned in BufferItem.
60 //
61 // If the buffer returned had previously been
Mathias Agopiana4e19522013-07-31 20:09:53 -070062 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
63 // NULL and it is assumed that the consumer still holds a reference to the
64 // buffer.
65 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -080066 // If presentWhen is non-zero, it indicates the time when the buffer will
Mathias Agopiana4e19522013-07-31 20:09:53 -070067 // be displayed on screen. If the buffer's timestamp is farther in the
68 // future, the buffer won't be acquired, and PRESENT_LATER will be
69 // returned. The presentation time is in nanoseconds, and the time base
70 // is CLOCK_MONOTONIC.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080071 //
Dan Stozaa4650a52015-05-12 12:56:16 -070072 // If maxFrameNumber is non-zero, it indicates that acquireBuffer should
73 // only return a buffer with a frame number less than or equal to
74 // maxFrameNumber. If no such frame is available (such as when a buffer has
75 // been replaced but the consumer has not received the onFrameReplaced
76 // callback), then PRESENT_LATER will be returned.
77 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -080078 // Return of NO_ERROR means the operation completed as normal.
79 //
80 // Return of a positive value means the operation could not be completed
81 // at this time, but the user should try again later:
82 // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
83 // * PRESENT_LATER - the buffer's timestamp is farther in the future
84 //
85 // Return of a negative value means an error has occurred:
86 // * INVALID_OPERATION - too many buffers have been acquired
Dan Stozaa4650a52015-05-12 12:56:16 -070087 virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen,
88 uint64_t maxFrameNumber = 0) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -070089
Dan Stoza9f3053d2014-03-06 15:14:33 -080090 // detachBuffer attempts to remove all ownership of the buffer in the given
91 // slot from the buffer queue. If this call succeeds, the slot will be
92 // freed, and there will be no way to obtain the buffer from this interface.
93 // The freed slot will remain unallocated until either it is selected to
94 // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
95 // to the slot. The buffer must have already been acquired.
96 //
97 // Return of a value other than NO_ERROR means an error has occurred:
98 // * BAD_VALUE - the given slot number is invalid, either because it is
99 // out of the range [0, NUM_BUFFER_SLOTS) or because the slot
100 // it refers to is not currently acquired.
101 virtual status_t detachBuffer(int slot) = 0;
102
103 // attachBuffer attempts to transfer ownership of a buffer to the buffer
104 // queue. If this call succeeds, it will be as if this buffer was acquired
105 // from the returned slot number. As such, this call will fail if attaching
106 // this buffer would cause too many buffers to be simultaneously acquired.
107 //
108 // If the buffer is successfully attached, its frameNumber is initialized
109 // to 0. This must be passed into the releaseBuffer call or else the buffer
110 // will be deallocated as stale.
111 //
112 // Return of a value other than NO_ERROR means an error has occurred:
Dan Stoza812ed062015-06-02 15:45:22 -0700113 // * BAD_VALUE - outSlot or buffer were NULL, or the generation number of
114 // the buffer did not match the buffer queue.
Dan Stoza9f3053d2014-03-06 15:14:33 -0800115 // * INVALID_OPERATION - cannot attach the buffer because it would cause too
116 // many buffers to be acquired.
117 // * NO_MEMORY - no free slots available
118 virtual status_t attachBuffer(int *outSlot,
119 const sp<GraphicBuffer>& buffer) = 0;
120
Mathias Agopiana4e19522013-07-31 20:09:53 -0700121 // releaseBuffer releases a buffer slot from the consumer back to the
122 // BufferQueue. This may be done while the buffer's contents are still
123 // being accessed. The fence will signal when the buffer is no longer
124 // in use. frameNumber is used to indentify the exact buffer returned.
125 //
126 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
127 // any references to the just-released buffer that it might have, as if it
128 // had received a onBuffersReleased() call with a mask set for the released
129 // buffer.
130 //
131 // Note that the dependencies on EGL will be removed once we switch to using
132 // the Android HW Sync HAL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800133 //
134 // Return of NO_ERROR means the operation completed as normal.
135 //
136 // Return of a positive value means the operation could not be completed
137 // at this time, but the user should try again later:
138 // * STALE_BUFFER_SLOT - see above (second paragraph)
139 //
140 // Return of a negative value means an error has occurred:
141 // * BAD_VALUE - one of the following could've happened:
142 // * the buffer slot was invalid
143 // * the fence was NULL
144 // * the buffer slot specified is not in the acquired state
Mathias Agopiana4e19522013-07-31 20:09:53 -0700145 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
146 EGLDisplay display, EGLSyncKHR fence,
147 const sp<Fence>& releaseFence) = 0;
148
149 // consumerConnect connects a consumer to the BufferQueue. Only one
150 // consumer may be connected, and when that consumer disconnects the
151 // BufferQueue is placed into the "abandoned" state, causing most
152 // interactions with the BufferQueue by the producer to fail.
153 // controlledByApp indicates whether the consumer is controlled by
154 // the application.
155 //
156 // consumer may not be NULL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800157 //
158 // Return of a value other than NO_ERROR means an error has occurred:
159 // * NO_INIT - the buffer queue has been abandoned
160 // * BAD_VALUE - a NULL consumer was provided
Mathias Agopiana4e19522013-07-31 20:09:53 -0700161 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
162
163 // consumerDisconnect disconnects a consumer from the BufferQueue. All
164 // buffers will be freed and the BufferQueue is placed in the "abandoned"
165 // state, causing most interactions with the BufferQueue by the producer to
166 // fail.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800167 //
168 // Return of a value other than NO_ERROR means an error has occurred:
169 // * BAD_VALUE - no consumer is currently connected
Mathias Agopiana4e19522013-07-31 20:09:53 -0700170 virtual status_t consumerDisconnect() = 0;
171
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800172 // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
173 // Each bit index with a 1 corresponds to a released buffer slot with that
174 // index value. In particular, a released buffer is one that has
175 // been released by the BufferQueue but have not yet been released by the consumer.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700176 //
177 // This should be called from the onBuffersReleased() callback.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800178 //
179 // Return of a value other than NO_ERROR means an error has occurred:
180 // * NO_INIT - the buffer queue has been abandoned.
Dan Stozafebd4f42014-04-09 16:14:51 -0700181 virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700182
183 // setDefaultBufferSize is used to set the size of buffers returned by
184 // dequeueBuffer when a width and height of zero is requested. Default
185 // is 1x1.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800186 //
187 // Return of a value other than NO_ERROR means an error has occurred:
188 // * BAD_VALUE - either w or h was zero
Mathias Agopiana4e19522013-07-31 20:09:53 -0700189 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
190
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700191 // setMaxBufferCount sets the maximum value for the number of buffers used
192 // in the buffer queue (the initial default is NUM_BUFFER_SLOTS). If a call
193 // to setMaxAcquiredBufferCount (by the consumer), or a call to setAsyncMode
194 // or setMaxDequeuedBufferCount (by the producer), would cause this value to
195 // be exceeded then that call will fail. This call will fail if a producer
196 // is connected to the BufferQueue.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700197 //
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700198 // The count must be between 1 and NUM_BUFFER_SLOTS, inclusive. The count
199 // cannot be less than maxAcquiredBufferCount.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800200 //
201 // Return of a value other than NO_ERROR means an error has occurred:
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800202 // * BAD_VALUE - one of the below conditions occurred:
203 // * bufferCount was out of range (see above).
204 // * failure to adjust the number of available slots.
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700205 // * INVALID_OPERATION - attempting to call this after a producer connected.
206 virtual status_t setMaxBufferCount(int bufferCount) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700207
208 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
Pablo Ceballos72daab62015-12-07 16:38:43 -0800209 // be acquired by the consumer at one time (default 1). If this method
210 // succeeds, any new buffer slots will be both unallocated and owned by the
211 // BufferQueue object (i.e. they are not owned by the producer or consumer).
212 // Calling this may also cause some buffer slots to be emptied.
213 //
214 // This function should not be called with a value of maxAcquiredBuffers
215 // that is less than the number of currently acquired buffer slots. Doing so
216 // will result in a BAD_VALUE error.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800217 //
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700218 // maxAcquiredBuffers must be (inclusive) between 1 and
219 // MAX_MAX_ACQUIRED_BUFFERS. It also cannot cause the maxBufferCount value
220 // to be exceeded.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800221 //
222 // Return of a value other than NO_ERROR means an error has occurred:
Pablo Ceballos72daab62015-12-07 16:38:43 -0800223 // * NO_INIT - the buffer queue has been abandoned
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800224 // * BAD_VALUE - one of the below conditions occurred:
225 // * maxAcquiredBuffers was out of range (see above).
226 // * failure to adjust the number of available slots.
Pablo Ceballos72daab62015-12-07 16:38:43 -0800227 // * client would have more than the requested number of
228 // acquired buffers after this call
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800229 // * INVALID_OPERATION - attempting to call this after a producer connected.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700230 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
231
232 // setConsumerName sets the name used in logging
233 virtual void setConsumerName(const String8& name) = 0;
234
235 // setDefaultBufferFormat allows the BufferQueue to create
236 // GraphicBuffers of a defaultFormat if no format is specified
Dan Stozad723bd72014-11-18 10:24:03 -0800237 // in dequeueBuffer.
238 // The initial default is PIXEL_FORMAT_RGBA_8888.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800239 //
240 // Return of a value other than NO_ERROR means an unknown error has occurred.
Dan Stozad723bd72014-11-18 10:24:03 -0800241 virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700242
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800243 // setDefaultBufferDataSpace is a request to the producer to provide buffers
244 // of the indicated dataSpace. The producer may ignore this request.
245 // The initial default is HAL_DATASPACE_UNKNOWN.
246 //
247 // Return of a value other than NO_ERROR means an unknown error has occurred.
248 virtual status_t setDefaultBufferDataSpace(
249 android_dataspace defaultDataSpace) = 0;
250
Mathias Agopiana4e19522013-07-31 20:09:53 -0700251 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
252 // These are merged with the bits passed to dequeueBuffer. The values are
253 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800254 //
255 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700256 virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
257
258 // setTransformHint bakes in rotation to buffers so overlays can be used.
259 // The values are enumerated in window.h, e.g.
260 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800261 //
262 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700263 virtual status_t setTransformHint(uint32_t hint) = 0;
264
Jesse Hall399184a2014-03-03 15:42:54 -0800265 // Retrieve the sideband buffer stream, if any.
266 virtual sp<NativeHandle> getSidebandStream() const = 0;
267
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700268 // dump state into a string
269 virtual void dump(String8& result, const char* prefix) const = 0;
270
Mathias Agopiana4e19522013-07-31 20:09:53 -0700271public:
272 DECLARE_META_INTERFACE(GraphicBufferConsumer);
273};
274
275// ----------------------------------------------------------------------------
276
277class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
278{
279public:
280 virtual status_t onTransact( uint32_t code,
281 const Parcel& data,
282 Parcel* reply,
283 uint32_t flags = 0);
284};
285
286// ----------------------------------------------------------------------------
287}; // namespace android
288
289#endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H