blob: 6363a3a4f6561d599fbbb40cb429ec4580cfd46c [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:
113 // * BAD_VALUE - outSlot or buffer were NULL
114 // * INVALID_OPERATION - cannot attach the buffer because it would cause too
115 // many buffers to be acquired.
116 // * NO_MEMORY - no free slots available
117 virtual status_t attachBuffer(int *outSlot,
118 const sp<GraphicBuffer>& buffer) = 0;
119
Mathias Agopiana4e19522013-07-31 20:09:53 -0700120 // releaseBuffer releases a buffer slot from the consumer back to the
121 // BufferQueue. This may be done while the buffer's contents are still
122 // being accessed. The fence will signal when the buffer is no longer
123 // in use. frameNumber is used to indentify the exact buffer returned.
124 //
125 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
126 // any references to the just-released buffer that it might have, as if it
127 // had received a onBuffersReleased() call with a mask set for the released
128 // buffer.
129 //
130 // Note that the dependencies on EGL will be removed once we switch to using
131 // the Android HW Sync HAL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800132 //
133 // Return of NO_ERROR means the operation completed as normal.
134 //
135 // Return of a positive value means the operation could not be completed
136 // at this time, but the user should try again later:
137 // * STALE_BUFFER_SLOT - see above (second paragraph)
138 //
139 // Return of a negative value means an error has occurred:
140 // * BAD_VALUE - one of the following could've happened:
141 // * the buffer slot was invalid
142 // * the fence was NULL
143 // * the buffer slot specified is not in the acquired state
Mathias Agopiana4e19522013-07-31 20:09:53 -0700144 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
145 EGLDisplay display, EGLSyncKHR fence,
146 const sp<Fence>& releaseFence) = 0;
147
148 // consumerConnect connects a consumer to the BufferQueue. Only one
149 // consumer may be connected, and when that consumer disconnects the
150 // BufferQueue is placed into the "abandoned" state, causing most
151 // interactions with the BufferQueue by the producer to fail.
152 // controlledByApp indicates whether the consumer is controlled by
153 // the application.
154 //
155 // consumer may not be NULL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800156 //
157 // Return of a value other than NO_ERROR means an error has occurred:
158 // * NO_INIT - the buffer queue has been abandoned
159 // * BAD_VALUE - a NULL consumer was provided
Mathias Agopiana4e19522013-07-31 20:09:53 -0700160 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
161
162 // consumerDisconnect disconnects a consumer from the BufferQueue. All
163 // buffers will be freed and the BufferQueue is placed in the "abandoned"
164 // state, causing most interactions with the BufferQueue by the producer to
165 // fail.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800166 //
167 // Return of a value other than NO_ERROR means an error has occurred:
168 // * BAD_VALUE - no consumer is currently connected
Mathias Agopiana4e19522013-07-31 20:09:53 -0700169 virtual status_t consumerDisconnect() = 0;
170
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800171 // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
172 // Each bit index with a 1 corresponds to a released buffer slot with that
173 // index value. In particular, a released buffer is one that has
174 // been released by the BufferQueue but have not yet been released by the consumer.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700175 //
176 // This should be called from the onBuffersReleased() callback.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800177 //
178 // Return of a value other than NO_ERROR means an error has occurred:
179 // * NO_INIT - the buffer queue has been abandoned.
Dan Stozafebd4f42014-04-09 16:14:51 -0700180 virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700181
182 // setDefaultBufferSize is used to set the size of buffers returned by
183 // dequeueBuffer when a width and height of zero is requested. Default
184 // is 1x1.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800185 //
186 // Return of a value other than NO_ERROR means an error has occurred:
187 // * BAD_VALUE - either w or h was zero
Mathias Agopiana4e19522013-07-31 20:09:53 -0700188 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
189
190 // setDefaultMaxBufferCount sets the default value for the maximum buffer
191 // count (the initial default is 2). If the producer has requested a
192 // buffer count using setBufferCount, the default buffer count will only
193 // take effect if the producer sets the count back to zero.
194 //
195 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800196 //
197 // Return of a value other than NO_ERROR means an error has occurred:
198 // * BAD_VALUE - bufferCount was out of range (see above).
Mathias Agopiana4e19522013-07-31 20:09:53 -0700199 virtual status_t setDefaultMaxBufferCount(int bufferCount) = 0;
200
201 // disableAsyncBuffer disables the extra buffer used in async mode
202 // (when both producer and consumer have set their "isControlledByApp"
203 // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
204 //
205 // This can only be called before consumerConnect().
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800206 //
207 // Return of a value other than NO_ERROR means an error has occurred:
208 // * INVALID_OPERATION - attempting to call this after consumerConnect.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700209 virtual status_t disableAsyncBuffer() = 0;
210
211 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
212 // be acquired by the consumer at one time (default 1). This call will
213 // fail if a producer is connected to the BufferQueue.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800214 //
215 // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS.
216 //
217 // Return of a value other than NO_ERROR means an error has occurred:
218 // * BAD_VALUE - maxAcquiredBuffers was out of range (see above).
219 // * INVALID_OPERATION - attempting to call this after a producer connected.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700220 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
221
222 // setConsumerName sets the name used in logging
223 virtual void setConsumerName(const String8& name) = 0;
224
225 // setDefaultBufferFormat allows the BufferQueue to create
226 // GraphicBuffers of a defaultFormat if no format is specified
Dan Stozad723bd72014-11-18 10:24:03 -0800227 // in dequeueBuffer.
228 // The initial default is PIXEL_FORMAT_RGBA_8888.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800229 //
230 // Return of a value other than NO_ERROR means an unknown error has occurred.
Dan Stozad723bd72014-11-18 10:24:03 -0800231 virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700232
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800233 // setDefaultBufferDataSpace is a request to the producer to provide buffers
234 // of the indicated dataSpace. The producer may ignore this request.
235 // The initial default is HAL_DATASPACE_UNKNOWN.
236 //
237 // Return of a value other than NO_ERROR means an unknown error has occurred.
238 virtual status_t setDefaultBufferDataSpace(
239 android_dataspace defaultDataSpace) = 0;
240
Mathias Agopiana4e19522013-07-31 20:09:53 -0700241 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
242 // These are merged with the bits passed to dequeueBuffer. The values are
243 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800244 //
245 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700246 virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
247
248 // setTransformHint bakes in rotation to buffers so overlays can be used.
249 // The values are enumerated in window.h, e.g.
250 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800251 //
252 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700253 virtual status_t setTransformHint(uint32_t hint) = 0;
254
Jesse Hall399184a2014-03-03 15:42:54 -0800255 // Retrieve the sideband buffer stream, if any.
256 virtual sp<NativeHandle> getSidebandStream() const = 0;
257
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700258 // dump state into a string
259 virtual void dump(String8& result, const char* prefix) const = 0;
260
Mathias Agopiana4e19522013-07-31 20:09:53 -0700261public:
262 DECLARE_META_INTERFACE(GraphicBufferConsumer);
263};
264
265// ----------------------------------------------------------------------------
266
267class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
268{
269public:
270 virtual status_t onTransact( uint32_t code,
271 const Parcel& data,
272 Parcel* reply,
273 uint32_t flags = 0);
274};
275
276// ----------------------------------------------------------------------------
277}; // namespace android
278
279#endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H