blob: 53b4e758b5ec4f8d528b23aad6e5055dbceae8f4 [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
Mathias Agopiana4e19522013-07-31 20:09:53 -070037class Fence;
Jesse Hall399184a2014-03-03 15:42:54 -080038class GraphicBuffer;
39class IConsumerListener;
40class NativeHandle;
Mathias Agopiana4e19522013-07-31 20:09:53 -070041
42class IGraphicBufferConsumer : public IInterface {
43
44public:
45
46 // public facing structure for BufferSlot
47 class BufferItem : public Flattenable<BufferItem> {
48 friend class Flattenable<BufferItem>;
49 size_t getPodSize() const;
50 size_t getFlattenedSize() const;
51 size_t getFdCount() const;
52 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
53 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
54
55 public:
Igor Murashkin7d2d1602013-11-12 18:02:20 -080056 // The default value of mBuf, used to indicate this doesn't correspond to a slot.
Mathias Agopiana4e19522013-07-31 20:09:53 -070057 enum { INVALID_BUFFER_SLOT = -1 };
58 BufferItem();
59
60 // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
61 // if the buffer in this slot has been acquired in the past (see
62 // BufferSlot.mAcquireCalled).
63 sp<GraphicBuffer> mGraphicBuffer;
64
65 // mFence is a fence that will signal when the buffer is idle.
66 sp<Fence> mFence;
67
68 // mCrop is the current crop rectangle for this buffer slot.
69 Rect mCrop;
70
71 // mTransform is the current transform flags for this buffer slot.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080072 // refer to NATIVE_WINDOW_TRANSFORM_* in <window.h>
Mathias Agopiana4e19522013-07-31 20:09:53 -070073 uint32_t mTransform;
74
75 // mScalingMode is the current scaling mode for this buffer slot.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080076 // refer to NATIVE_WINDOW_SCALING_* in <window.h>
Mathias Agopiana4e19522013-07-31 20:09:53 -070077 uint32_t mScalingMode;
78
79 // mTimestamp is the current timestamp for this buffer slot. This gets
Igor Murashkin7d2d1602013-11-12 18:02:20 -080080 // to set by queueBuffer each time this slot is queued. This value
81 // is guaranteed to be monotonically increasing for each newly
82 // acquired buffer.
Mathias Agopiana4e19522013-07-31 20:09:53 -070083 int64_t mTimestamp;
84
Andy McFadden3c256212013-08-16 14:55:39 -070085 // mIsAutoTimestamp indicates whether mTimestamp was generated
86 // automatically when the buffer was queued.
87 bool mIsAutoTimestamp;
88
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -080089 // mDataSpace is the current dataSpace for this buffer slot. This gets
90 // set by queueBuffer each time this slot is queued.
91 android_dataspace mDataSpace;
92
Mathias Agopiana4e19522013-07-31 20:09:53 -070093 // mFrameNumber is the number of the queued frame for this slot.
94 uint64_t mFrameNumber;
95
Igor Murashkin7d2d1602013-11-12 18:02:20 -080096 // mBuf is the slot index of this buffer (default INVALID_BUFFER_SLOT).
Mathias Agopiana4e19522013-07-31 20:09:53 -070097 int mBuf;
98
99 // mIsDroppable whether this buffer was queued with the
100 // property that it can be replaced by a new buffer for the purpose of
101 // making sure dequeueBuffer() won't block.
102 // i.e.: was the BufferQueue in "mDequeueBufferCannotBlock" when this buffer
103 // was queued.
104 bool mIsDroppable;
105
106 // Indicates whether this buffer has been seen by a consumer yet
107 bool mAcquireCalled;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700108
109 // Indicates this buffer must be transformed by the inverse transform of the screen
110 // it is displayed onto. This is applied after mTransform.
111 bool mTransformToDisplayInverse;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700112 };
113
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800114 enum {
115 // Returned by releaseBuffer, after which the consumer must
116 // free any references to the just-released buffer that it might have.
117 STALE_BUFFER_SLOT = 1,
118 // Returned by dequeueBuffer if there are no pending buffers available.
119 NO_BUFFER_AVAILABLE,
120 // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
121 PRESENT_LATER,
122 };
Mathias Agopiana4e19522013-07-31 20:09:53 -0700123
124 // acquireBuffer attempts to acquire ownership of the next pending buffer in
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800125 // the BufferQueue. If no buffer is pending then it returns
126 // NO_BUFFER_AVAILABLE. If a buffer is successfully acquired, the
127 // information about the buffer is returned in BufferItem.
128 //
129 // If the buffer returned had previously been
Mathias Agopiana4e19522013-07-31 20:09:53 -0700130 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
131 // NULL and it is assumed that the consumer still holds a reference to the
132 // buffer.
133 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800134 // If presentWhen is non-zero, it indicates the time when the buffer will
Mathias Agopiana4e19522013-07-31 20:09:53 -0700135 // be displayed on screen. If the buffer's timestamp is farther in the
136 // future, the buffer won't be acquired, and PRESENT_LATER will be
137 // returned. The presentation time is in nanoseconds, and the time base
138 // is CLOCK_MONOTONIC.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800139 //
140 // Return of NO_ERROR means the operation completed as normal.
141 //
142 // Return of a positive value means the operation could not be completed
143 // at this time, but the user should try again later:
144 // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
145 // * PRESENT_LATER - the buffer's timestamp is farther in the future
146 //
147 // Return of a negative value means an error has occurred:
148 // * INVALID_OPERATION - too many buffers have been acquired
149 virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700150
Dan Stoza9f3053d2014-03-06 15:14:33 -0800151 // detachBuffer attempts to remove all ownership of the buffer in the given
152 // slot from the buffer queue. If this call succeeds, the slot will be
153 // freed, and there will be no way to obtain the buffer from this interface.
154 // The freed slot will remain unallocated until either it is selected to
155 // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
156 // to the slot. The buffer must have already been acquired.
157 //
158 // Return of a value other than NO_ERROR means an error has occurred:
159 // * BAD_VALUE - the given slot number is invalid, either because it is
160 // out of the range [0, NUM_BUFFER_SLOTS) or because the slot
161 // it refers to is not currently acquired.
162 virtual status_t detachBuffer(int slot) = 0;
163
164 // attachBuffer attempts to transfer ownership of a buffer to the buffer
165 // queue. If this call succeeds, it will be as if this buffer was acquired
166 // from the returned slot number. As such, this call will fail if attaching
167 // this buffer would cause too many buffers to be simultaneously acquired.
168 //
169 // If the buffer is successfully attached, its frameNumber is initialized
170 // to 0. This must be passed into the releaseBuffer call or else the buffer
171 // will be deallocated as stale.
172 //
173 // Return of a value other than NO_ERROR means an error has occurred:
174 // * BAD_VALUE - outSlot or buffer were NULL
175 // * INVALID_OPERATION - cannot attach the buffer because it would cause too
176 // many buffers to be acquired.
177 // * NO_MEMORY - no free slots available
178 virtual status_t attachBuffer(int *outSlot,
179 const sp<GraphicBuffer>& buffer) = 0;
180
Mathias Agopiana4e19522013-07-31 20:09:53 -0700181 // releaseBuffer releases a buffer slot from the consumer back to the
182 // BufferQueue. This may be done while the buffer's contents are still
183 // being accessed. The fence will signal when the buffer is no longer
184 // in use. frameNumber is used to indentify the exact buffer returned.
185 //
186 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
187 // any references to the just-released buffer that it might have, as if it
188 // had received a onBuffersReleased() call with a mask set for the released
189 // buffer.
190 //
191 // Note that the dependencies on EGL will be removed once we switch to using
192 // the Android HW Sync HAL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800193 //
194 // Return of NO_ERROR means the operation completed as normal.
195 //
196 // Return of a positive value means the operation could not be completed
197 // at this time, but the user should try again later:
198 // * STALE_BUFFER_SLOT - see above (second paragraph)
199 //
200 // Return of a negative value means an error has occurred:
201 // * BAD_VALUE - one of the following could've happened:
202 // * the buffer slot was invalid
203 // * the fence was NULL
204 // * the buffer slot specified is not in the acquired state
Mathias Agopiana4e19522013-07-31 20:09:53 -0700205 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
206 EGLDisplay display, EGLSyncKHR fence,
207 const sp<Fence>& releaseFence) = 0;
208
209 // consumerConnect connects a consumer to the BufferQueue. Only one
210 // consumer may be connected, and when that consumer disconnects the
211 // BufferQueue is placed into the "abandoned" state, causing most
212 // interactions with the BufferQueue by the producer to fail.
213 // controlledByApp indicates whether the consumer is controlled by
214 // the application.
215 //
216 // consumer may not be NULL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800217 //
218 // Return of a value other than NO_ERROR means an error has occurred:
219 // * NO_INIT - the buffer queue has been abandoned
220 // * BAD_VALUE - a NULL consumer was provided
Mathias Agopiana4e19522013-07-31 20:09:53 -0700221 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
222
223 // consumerDisconnect disconnects a consumer from the BufferQueue. All
224 // buffers will be freed and the BufferQueue is placed in the "abandoned"
225 // state, causing most interactions with the BufferQueue by the producer to
226 // fail.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800227 //
228 // Return of a value other than NO_ERROR means an error has occurred:
229 // * BAD_VALUE - no consumer is currently connected
Mathias Agopiana4e19522013-07-31 20:09:53 -0700230 virtual status_t consumerDisconnect() = 0;
231
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800232 // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
233 // Each bit index with a 1 corresponds to a released buffer slot with that
234 // index value. In particular, a released buffer is one that has
235 // been released by the BufferQueue but have not yet been released by the consumer.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700236 //
237 // This should be called from the onBuffersReleased() callback.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800238 //
239 // Return of a value other than NO_ERROR means an error has occurred:
240 // * NO_INIT - the buffer queue has been abandoned.
Dan Stozafebd4f42014-04-09 16:14:51 -0700241 virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700242
243 // setDefaultBufferSize is used to set the size of buffers returned by
244 // dequeueBuffer when a width and height of zero is requested. Default
245 // is 1x1.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800246 //
247 // Return of a value other than NO_ERROR means an error has occurred:
248 // * BAD_VALUE - either w or h was zero
Mathias Agopiana4e19522013-07-31 20:09:53 -0700249 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
250
251 // setDefaultMaxBufferCount sets the default value for the maximum buffer
252 // count (the initial default is 2). If the producer has requested a
253 // buffer count using setBufferCount, the default buffer count will only
254 // take effect if the producer sets the count back to zero.
255 //
256 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800257 //
258 // Return of a value other than NO_ERROR means an error has occurred:
259 // * BAD_VALUE - bufferCount was out of range (see above).
Mathias Agopiana4e19522013-07-31 20:09:53 -0700260 virtual status_t setDefaultMaxBufferCount(int bufferCount) = 0;
261
262 // disableAsyncBuffer disables the extra buffer used in async mode
263 // (when both producer and consumer have set their "isControlledByApp"
264 // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
265 //
266 // This can only be called before consumerConnect().
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800267 //
268 // Return of a value other than NO_ERROR means an error has occurred:
269 // * INVALID_OPERATION - attempting to call this after consumerConnect.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700270 virtual status_t disableAsyncBuffer() = 0;
271
272 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
273 // be acquired by the consumer at one time (default 1). This call will
274 // fail if a producer is connected to the BufferQueue.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800275 //
276 // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS.
277 //
278 // Return of a value other than NO_ERROR means an error has occurred:
279 // * BAD_VALUE - maxAcquiredBuffers was out of range (see above).
280 // * INVALID_OPERATION - attempting to call this after a producer connected.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700281 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
282
283 // setConsumerName sets the name used in logging
284 virtual void setConsumerName(const String8& name) = 0;
285
286 // setDefaultBufferFormat allows the BufferQueue to create
287 // GraphicBuffers of a defaultFormat if no format is specified
Dan Stozad723bd72014-11-18 10:24:03 -0800288 // in dequeueBuffer.
289 // The initial default is PIXEL_FORMAT_RGBA_8888.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800290 //
291 // Return of a value other than NO_ERROR means an unknown error has occurred.
Dan Stozad723bd72014-11-18 10:24:03 -0800292 virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700293
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800294 // setDefaultBufferDataSpace is a request to the producer to provide buffers
295 // of the indicated dataSpace. The producer may ignore this request.
296 // The initial default is HAL_DATASPACE_UNKNOWN.
297 //
298 // Return of a value other than NO_ERROR means an unknown error has occurred.
299 virtual status_t setDefaultBufferDataSpace(
300 android_dataspace defaultDataSpace) = 0;
301
Mathias Agopiana4e19522013-07-31 20:09:53 -0700302 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
303 // These are merged with the bits passed to dequeueBuffer. The values are
304 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800305 //
306 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700307 virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
308
309 // setTransformHint bakes in rotation to buffers so overlays can be used.
310 // The values are enumerated in window.h, e.g.
311 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800312 //
313 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700314 virtual status_t setTransformHint(uint32_t hint) = 0;
315
Jesse Hall399184a2014-03-03 15:42:54 -0800316 // Retrieve the sideband buffer stream, if any.
317 virtual sp<NativeHandle> getSidebandStream() const = 0;
318
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700319 // dump state into a string
320 virtual void dump(String8& result, const char* prefix) const = 0;
321
Mathias Agopiana4e19522013-07-31 20:09:53 -0700322public:
323 DECLARE_META_INTERFACE(GraphicBufferConsumer);
324};
325
326// ----------------------------------------------------------------------------
327
328class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
329{
330public:
331 virtual status_t onTransact( uint32_t code,
332 const Parcel& data,
333 Parcel* reply,
334 uint32_t flags = 0);
335};
336
337// ----------------------------------------------------------------------------
338}; // namespace android
339
340#endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H