blob: 0be09a283ecdaf544f53392270b302f9901ce5fa [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 //
72 // Return of NO_ERROR means the operation completed as normal.
73 //
74 // Return of a positive value means the operation could not be completed
75 // at this time, but the user should try again later:
76 // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
77 // * PRESENT_LATER - the buffer's timestamp is farther in the future
78 //
79 // Return of a negative value means an error has occurred:
80 // * INVALID_OPERATION - too many buffers have been acquired
81 virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -070082
Dan Stoza9f3053d2014-03-06 15:14:33 -080083 // detachBuffer attempts to remove all ownership of the buffer in the given
84 // slot from the buffer queue. If this call succeeds, the slot will be
85 // freed, and there will be no way to obtain the buffer from this interface.
86 // The freed slot will remain unallocated until either it is selected to
87 // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
88 // to the slot. The buffer must have already been acquired.
89 //
90 // Return of a value other than NO_ERROR means an error has occurred:
91 // * BAD_VALUE - the given slot number is invalid, either because it is
92 // out of the range [0, NUM_BUFFER_SLOTS) or because the slot
93 // it refers to is not currently acquired.
94 virtual status_t detachBuffer(int slot) = 0;
95
96 // attachBuffer attempts to transfer ownership of a buffer to the buffer
97 // queue. If this call succeeds, it will be as if this buffer was acquired
98 // from the returned slot number. As such, this call will fail if attaching
99 // this buffer would cause too many buffers to be simultaneously acquired.
100 //
101 // If the buffer is successfully attached, its frameNumber is initialized
102 // to 0. This must be passed into the releaseBuffer call or else the buffer
103 // 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
107 // * INVALID_OPERATION - cannot attach the buffer because it would cause too
108 // many buffers to be acquired.
109 // * NO_MEMORY - no free slots available
110 virtual status_t attachBuffer(int *outSlot,
111 const sp<GraphicBuffer>& buffer) = 0;
112
Mathias Agopiana4e19522013-07-31 20:09:53 -0700113 // releaseBuffer releases a buffer slot from the consumer back to the
114 // BufferQueue. This may be done while the buffer's contents are still
115 // being accessed. The fence will signal when the buffer is no longer
116 // in use. frameNumber is used to indentify the exact buffer returned.
117 //
118 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
119 // any references to the just-released buffer that it might have, as if it
120 // had received a onBuffersReleased() call with a mask set for the released
121 // buffer.
122 //
123 // Note that the dependencies on EGL will be removed once we switch to using
124 // the Android HW Sync HAL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800125 //
126 // Return of NO_ERROR means the operation completed as normal.
127 //
128 // Return of a positive value means the operation could not be completed
129 // at this time, but the user should try again later:
130 // * STALE_BUFFER_SLOT - see above (second paragraph)
131 //
132 // Return of a negative value means an error has occurred:
133 // * BAD_VALUE - one of the following could've happened:
134 // * the buffer slot was invalid
135 // * the fence was NULL
136 // * the buffer slot specified is not in the acquired state
Mathias Agopiana4e19522013-07-31 20:09:53 -0700137 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
138 EGLDisplay display, EGLSyncKHR fence,
139 const sp<Fence>& releaseFence) = 0;
140
141 // consumerConnect connects a consumer to the BufferQueue. Only one
142 // consumer may be connected, and when that consumer disconnects the
143 // BufferQueue is placed into the "abandoned" state, causing most
144 // interactions with the BufferQueue by the producer to fail.
145 // controlledByApp indicates whether the consumer is controlled by
146 // the application.
147 //
148 // consumer may not be NULL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800149 //
150 // Return of a value other than NO_ERROR means an error has occurred:
151 // * NO_INIT - the buffer queue has been abandoned
152 // * BAD_VALUE - a NULL consumer was provided
Mathias Agopiana4e19522013-07-31 20:09:53 -0700153 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
154
155 // consumerDisconnect disconnects a consumer from the BufferQueue. All
156 // buffers will be freed and the BufferQueue is placed in the "abandoned"
157 // state, causing most interactions with the BufferQueue by the producer to
158 // fail.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800159 //
160 // Return of a value other than NO_ERROR means an error has occurred:
161 // * BAD_VALUE - no consumer is currently connected
Mathias Agopiana4e19522013-07-31 20:09:53 -0700162 virtual status_t consumerDisconnect() = 0;
163
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800164 // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
165 // Each bit index with a 1 corresponds to a released buffer slot with that
166 // index value. In particular, a released buffer is one that has
167 // been released by the BufferQueue but have not yet been released by the consumer.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700168 //
169 // This should be called from the onBuffersReleased() callback.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800170 //
171 // Return of a value other than NO_ERROR means an error has occurred:
172 // * NO_INIT - the buffer queue has been abandoned.
Dan Stozafebd4f42014-04-09 16:14:51 -0700173 virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700174
175 // setDefaultBufferSize is used to set the size of buffers returned by
176 // dequeueBuffer when a width and height of zero is requested. Default
177 // is 1x1.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800178 //
179 // Return of a value other than NO_ERROR means an error has occurred:
180 // * BAD_VALUE - either w or h was zero
Mathias Agopiana4e19522013-07-31 20:09:53 -0700181 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
182
183 // setDefaultMaxBufferCount sets the default value for the maximum buffer
184 // count (the initial default is 2). If the producer has requested a
185 // buffer count using setBufferCount, the default buffer count will only
186 // take effect if the producer sets the count back to zero.
187 //
188 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800189 //
190 // Return of a value other than NO_ERROR means an error has occurred:
191 // * BAD_VALUE - bufferCount was out of range (see above).
Mathias Agopiana4e19522013-07-31 20:09:53 -0700192 virtual status_t setDefaultMaxBufferCount(int bufferCount) = 0;
193
194 // disableAsyncBuffer disables the extra buffer used in async mode
195 // (when both producer and consumer have set their "isControlledByApp"
196 // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
197 //
198 // This can only be called before consumerConnect().
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800199 //
200 // Return of a value other than NO_ERROR means an error has occurred:
201 // * INVALID_OPERATION - attempting to call this after consumerConnect.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700202 virtual status_t disableAsyncBuffer() = 0;
203
204 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
205 // be acquired by the consumer at one time (default 1). This call will
206 // fail if a producer is connected to the BufferQueue.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800207 //
208 // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS.
209 //
210 // Return of a value other than NO_ERROR means an error has occurred:
211 // * BAD_VALUE - maxAcquiredBuffers was out of range (see above).
212 // * INVALID_OPERATION - attempting to call this after a producer connected.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700213 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
214
215 // setConsumerName sets the name used in logging
216 virtual void setConsumerName(const String8& name) = 0;
217
218 // setDefaultBufferFormat allows the BufferQueue to create
219 // GraphicBuffers of a defaultFormat if no format is specified
Dan Stozad723bd72014-11-18 10:24:03 -0800220 // in dequeueBuffer.
221 // The initial default is PIXEL_FORMAT_RGBA_8888.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800222 //
223 // Return of a value other than NO_ERROR means an unknown error has occurred.
Dan Stozad723bd72014-11-18 10:24:03 -0800224 virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700225
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800226 // setDefaultBufferDataSpace is a request to the producer to provide buffers
227 // of the indicated dataSpace. The producer may ignore this request.
228 // The initial default is HAL_DATASPACE_UNKNOWN.
229 //
230 // Return of a value other than NO_ERROR means an unknown error has occurred.
231 virtual status_t setDefaultBufferDataSpace(
232 android_dataspace defaultDataSpace) = 0;
233
Mathias Agopiana4e19522013-07-31 20:09:53 -0700234 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
235 // These are merged with the bits passed to dequeueBuffer. The values are
236 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800237 //
238 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700239 virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
240
241 // setTransformHint bakes in rotation to buffers so overlays can be used.
242 // The values are enumerated in window.h, e.g.
243 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
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 setTransformHint(uint32_t hint) = 0;
247
Jesse Hall399184a2014-03-03 15:42:54 -0800248 // Retrieve the sideband buffer stream, if any.
249 virtual sp<NativeHandle> getSidebandStream() const = 0;
250
Dan Stozaecc50402015-04-28 14:42:06 -0700251 // setShadowQueueSize notifies the BufferQueue that the consumer is
252 // shadowing its queue and allows it to limit the number of buffers it is
253 // permitted to drop during acquire so as to not get out of sync.
254 virtual void setShadowQueueSize(size_t size) = 0;
255
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700256 // dump state into a string
257 virtual void dump(String8& result, const char* prefix) const = 0;
258
Mathias Agopiana4e19522013-07-31 20:09:53 -0700259public:
260 DECLARE_META_INTERFACE(GraphicBufferConsumer);
261};
262
263// ----------------------------------------------------------------------------
264
265class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
266{
267public:
268 virtual status_t onTransact( uint32_t code,
269 const Parcel& data,
270 Parcel* reply,
271 uint32_t flags = 0);
272};
273
274// ----------------------------------------------------------------------------
275}; // namespace android
276
277#endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H