blob: ae85bd501958d3830c8ca5d414657bf247d90b25 [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:
46
47 // public facing structure for BufferSlot
48 class BufferItem : public Flattenable<BufferItem> {
49 friend class Flattenable<BufferItem>;
50 size_t getPodSize() const;
51 size_t getFlattenedSize() const;
52 size_t getFdCount() const;
53 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
54 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
55
56 public:
Igor Murashkin7d2d1602013-11-12 18:02:20 -080057 // The default value of mBuf, used to indicate this doesn't correspond to a slot.
Mathias Agopiana4e19522013-07-31 20:09:53 -070058 enum { INVALID_BUFFER_SLOT = -1 };
59 BufferItem();
60
61 // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
62 // if the buffer in this slot has been acquired in the past (see
63 // BufferSlot.mAcquireCalled).
64 sp<GraphicBuffer> mGraphicBuffer;
65
66 // mFence is a fence that will signal when the buffer is idle.
67 sp<Fence> mFence;
68
69 // mCrop is the current crop rectangle for this buffer slot.
70 Rect mCrop;
71
72 // mTransform is the current transform flags for this buffer slot.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080073 // refer to NATIVE_WINDOW_TRANSFORM_* in <window.h>
Mathias Agopiana4e19522013-07-31 20:09:53 -070074 uint32_t mTransform;
75
76 // mScalingMode is the current scaling mode for this buffer slot.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080077 // refer to NATIVE_WINDOW_SCALING_* in <window.h>
Mathias Agopiana4e19522013-07-31 20:09:53 -070078 uint32_t mScalingMode;
79
80 // mTimestamp is the current timestamp for this buffer slot. This gets
Igor Murashkin7d2d1602013-11-12 18:02:20 -080081 // to set by queueBuffer each time this slot is queued. This value
82 // is guaranteed to be monotonically increasing for each newly
83 // acquired buffer.
Mathias Agopiana4e19522013-07-31 20:09:53 -070084 int64_t mTimestamp;
85
Andy McFadden3c256212013-08-16 14:55:39 -070086 // mIsAutoTimestamp indicates whether mTimestamp was generated
87 // automatically when the buffer was queued.
88 bool mIsAutoTimestamp;
89
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -080090 // mDataSpace is the current dataSpace for this buffer slot. This gets
91 // set by queueBuffer each time this slot is queued.
92 android_dataspace mDataSpace;
93
Mathias Agopiana4e19522013-07-31 20:09:53 -070094 // mFrameNumber is the number of the queued frame for this slot.
95 uint64_t mFrameNumber;
96
Igor Murashkin7d2d1602013-11-12 18:02:20 -080097 // mBuf is the slot index of this buffer (default INVALID_BUFFER_SLOT).
Mathias Agopiana4e19522013-07-31 20:09:53 -070098 int mBuf;
99
100 // mIsDroppable whether this buffer was queued with the
101 // property that it can be replaced by a new buffer for the purpose of
102 // making sure dequeueBuffer() won't block.
103 // i.e.: was the BufferQueue in "mDequeueBufferCannotBlock" when this buffer
104 // was queued.
105 bool mIsDroppable;
106
107 // Indicates whether this buffer has been seen by a consumer yet
108 bool mAcquireCalled;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700109
110 // Indicates this buffer must be transformed by the inverse transform of the screen
111 // it is displayed onto. This is applied after mTransform.
112 bool mTransformToDisplayInverse;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700113 };
114
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800115 enum {
116 // Returned by releaseBuffer, after which the consumer must
117 // free any references to the just-released buffer that it might have.
118 STALE_BUFFER_SLOT = 1,
119 // Returned by dequeueBuffer if there are no pending buffers available.
120 NO_BUFFER_AVAILABLE,
121 // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
122 PRESENT_LATER,
123 };
Mathias Agopiana4e19522013-07-31 20:09:53 -0700124
125 // acquireBuffer attempts to acquire ownership of the next pending buffer in
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800126 // the BufferQueue. If no buffer is pending then it returns
127 // NO_BUFFER_AVAILABLE. If a buffer is successfully acquired, the
128 // information about the buffer is returned in BufferItem.
129 //
130 // If the buffer returned had previously been
Mathias Agopiana4e19522013-07-31 20:09:53 -0700131 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
132 // NULL and it is assumed that the consumer still holds a reference to the
133 // buffer.
134 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800135 // If presentWhen is non-zero, it indicates the time when the buffer will
Mathias Agopiana4e19522013-07-31 20:09:53 -0700136 // be displayed on screen. If the buffer's timestamp is farther in the
137 // future, the buffer won't be acquired, and PRESENT_LATER will be
138 // returned. The presentation time is in nanoseconds, and the time base
139 // is CLOCK_MONOTONIC.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800140 //
141 // Return of NO_ERROR means the operation completed as normal.
142 //
143 // Return of a positive value means the operation could not be completed
144 // at this time, but the user should try again later:
145 // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
146 // * PRESENT_LATER - the buffer's timestamp is farther in the future
147 //
148 // Return of a negative value means an error has occurred:
149 // * INVALID_OPERATION - too many buffers have been acquired
150 virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen) = 0;
Dan Stozade7100a2015-03-11 16:38:47 -0700151 virtual status_t acquireBuffer(android::BufferItem* buffer, nsecs_t presentWhen) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700152
Dan Stoza9f3053d2014-03-06 15:14:33 -0800153 // detachBuffer attempts to remove all ownership of the buffer in the given
154 // slot from the buffer queue. If this call succeeds, the slot will be
155 // freed, and there will be no way to obtain the buffer from this interface.
156 // The freed slot will remain unallocated until either it is selected to
157 // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
158 // to the slot. The buffer must have already been acquired.
159 //
160 // Return of a value other than NO_ERROR means an error has occurred:
161 // * BAD_VALUE - the given slot number is invalid, either because it is
162 // out of the range [0, NUM_BUFFER_SLOTS) or because the slot
163 // it refers to is not currently acquired.
164 virtual status_t detachBuffer(int slot) = 0;
165
166 // attachBuffer attempts to transfer ownership of a buffer to the buffer
167 // queue. If this call succeeds, it will be as if this buffer was acquired
168 // from the returned slot number. As such, this call will fail if attaching
169 // this buffer would cause too many buffers to be simultaneously acquired.
170 //
171 // If the buffer is successfully attached, its frameNumber is initialized
172 // to 0. This must be passed into the releaseBuffer call or else the buffer
173 // will be deallocated as stale.
174 //
175 // Return of a value other than NO_ERROR means an error has occurred:
176 // * BAD_VALUE - outSlot or buffer were NULL
177 // * INVALID_OPERATION - cannot attach the buffer because it would cause too
178 // many buffers to be acquired.
179 // * NO_MEMORY - no free slots available
180 virtual status_t attachBuffer(int *outSlot,
181 const sp<GraphicBuffer>& buffer) = 0;
182
Mathias Agopiana4e19522013-07-31 20:09:53 -0700183 // releaseBuffer releases a buffer slot from the consumer back to the
184 // BufferQueue. This may be done while the buffer's contents are still
185 // being accessed. The fence will signal when the buffer is no longer
186 // in use. frameNumber is used to indentify the exact buffer returned.
187 //
188 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
189 // any references to the just-released buffer that it might have, as if it
190 // had received a onBuffersReleased() call with a mask set for the released
191 // buffer.
192 //
193 // Note that the dependencies on EGL will be removed once we switch to using
194 // the Android HW Sync HAL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800195 //
196 // Return of NO_ERROR means the operation completed as normal.
197 //
198 // Return of a positive value means the operation could not be completed
199 // at this time, but the user should try again later:
200 // * STALE_BUFFER_SLOT - see above (second paragraph)
201 //
202 // Return of a negative value means an error has occurred:
203 // * BAD_VALUE - one of the following could've happened:
204 // * the buffer slot was invalid
205 // * the fence was NULL
206 // * the buffer slot specified is not in the acquired state
Mathias Agopiana4e19522013-07-31 20:09:53 -0700207 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
208 EGLDisplay display, EGLSyncKHR fence,
209 const sp<Fence>& releaseFence) = 0;
210
211 // consumerConnect connects a consumer to the BufferQueue. Only one
212 // consumer may be connected, and when that consumer disconnects the
213 // BufferQueue is placed into the "abandoned" state, causing most
214 // interactions with the BufferQueue by the producer to fail.
215 // controlledByApp indicates whether the consumer is controlled by
216 // the application.
217 //
218 // consumer may not be NULL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800219 //
220 // Return of a value other than NO_ERROR means an error has occurred:
221 // * NO_INIT - the buffer queue has been abandoned
222 // * BAD_VALUE - a NULL consumer was provided
Mathias Agopiana4e19522013-07-31 20:09:53 -0700223 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
224
225 // consumerDisconnect disconnects a consumer from the BufferQueue. All
226 // buffers will be freed and the BufferQueue is placed in the "abandoned"
227 // state, causing most interactions with the BufferQueue by the producer to
228 // fail.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800229 //
230 // Return of a value other than NO_ERROR means an error has occurred:
231 // * BAD_VALUE - no consumer is currently connected
Mathias Agopiana4e19522013-07-31 20:09:53 -0700232 virtual status_t consumerDisconnect() = 0;
233
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800234 // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
235 // Each bit index with a 1 corresponds to a released buffer slot with that
236 // index value. In particular, a released buffer is one that has
237 // been released by the BufferQueue but have not yet been released by the consumer.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700238 //
239 // This should be called from the onBuffersReleased() callback.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800240 //
241 // Return of a value other than NO_ERROR means an error has occurred:
242 // * NO_INIT - the buffer queue has been abandoned.
Dan Stozafebd4f42014-04-09 16:14:51 -0700243 virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700244
245 // setDefaultBufferSize is used to set the size of buffers returned by
246 // dequeueBuffer when a width and height of zero is requested. Default
247 // is 1x1.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800248 //
249 // Return of a value other than NO_ERROR means an error has occurred:
250 // * BAD_VALUE - either w or h was zero
Mathias Agopiana4e19522013-07-31 20:09:53 -0700251 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
252
253 // setDefaultMaxBufferCount sets the default value for the maximum buffer
254 // count (the initial default is 2). If the producer has requested a
255 // buffer count using setBufferCount, the default buffer count will only
256 // take effect if the producer sets the count back to zero.
257 //
258 // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800259 //
260 // Return of a value other than NO_ERROR means an error has occurred:
261 // * BAD_VALUE - bufferCount was out of range (see above).
Mathias Agopiana4e19522013-07-31 20:09:53 -0700262 virtual status_t setDefaultMaxBufferCount(int bufferCount) = 0;
263
264 // disableAsyncBuffer disables the extra buffer used in async mode
265 // (when both producer and consumer have set their "isControlledByApp"
266 // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
267 //
268 // This can only be called before consumerConnect().
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800269 //
270 // Return of a value other than NO_ERROR means an error has occurred:
271 // * INVALID_OPERATION - attempting to call this after consumerConnect.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700272 virtual status_t disableAsyncBuffer() = 0;
273
274 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
275 // be acquired by the consumer at one time (default 1). This call will
276 // fail if a producer is connected to the BufferQueue.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800277 //
278 // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS.
279 //
280 // Return of a value other than NO_ERROR means an error has occurred:
281 // * BAD_VALUE - maxAcquiredBuffers was out of range (see above).
282 // * INVALID_OPERATION - attempting to call this after a producer connected.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700283 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
284
285 // setConsumerName sets the name used in logging
286 virtual void setConsumerName(const String8& name) = 0;
287
288 // setDefaultBufferFormat allows the BufferQueue to create
289 // GraphicBuffers of a defaultFormat if no format is specified
Dan Stozad723bd72014-11-18 10:24:03 -0800290 // in dequeueBuffer.
291 // The initial default is PIXEL_FORMAT_RGBA_8888.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800292 //
293 // Return of a value other than NO_ERROR means an unknown error has occurred.
Dan Stozad723bd72014-11-18 10:24:03 -0800294 virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700295
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800296 // setDefaultBufferDataSpace is a request to the producer to provide buffers
297 // of the indicated dataSpace. The producer may ignore this request.
298 // The initial default is HAL_DATASPACE_UNKNOWN.
299 //
300 // Return of a value other than NO_ERROR means an unknown error has occurred.
301 virtual status_t setDefaultBufferDataSpace(
302 android_dataspace defaultDataSpace) = 0;
303
Mathias Agopiana4e19522013-07-31 20:09:53 -0700304 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
305 // These are merged with the bits passed to dequeueBuffer. The values are
306 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800307 //
308 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700309 virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
310
311 // setTransformHint bakes in rotation to buffers so overlays can be used.
312 // The values are enumerated in window.h, e.g.
313 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800314 //
315 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700316 virtual status_t setTransformHint(uint32_t hint) = 0;
317
Jesse Hall399184a2014-03-03 15:42:54 -0800318 // Retrieve the sideband buffer stream, if any.
319 virtual sp<NativeHandle> getSidebandStream() const = 0;
320
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700321 // dump state into a string
322 virtual void dump(String8& result, const char* prefix) const = 0;
323
Mathias Agopiana4e19522013-07-31 20:09:53 -0700324public:
325 DECLARE_META_INTERFACE(GraphicBufferConsumer);
326};
327
328// ----------------------------------------------------------------------------
329
330class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
331{
332public:
333 virtual status_t onTransact( uint32_t code,
334 const Parcel& data,
335 Parcel* reply,
336 uint32_t flags = 0);
337};
338
339// ----------------------------------------------------------------------------
340}; // namespace android
341
342#endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H