blob: dcddca46e81b57a10832613e03de869b5678b158 [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>
Dan Stozae77c7662016-05-13 11:37:28 -070030#include <gui/OccupancyTracker.h>
Mathias Agopiana4e19522013-07-31 20:09:53 -070031
Dan Stoza99b18b42014-03-28 15:34:33 -070032#include <EGL/egl.h>
33#include <EGL/eglext.h>
34
Mathias Agopiana4e19522013-07-31 20:09:53 -070035namespace android {
36// ----------------------------------------------------------------------------
37
Dan Stozade7100a2015-03-11 16:38:47 -070038class BufferItem;
Mathias Agopiana4e19522013-07-31 20:09:53 -070039class Fence;
Jesse Hall399184a2014-03-03 15:42:54 -080040class GraphicBuffer;
41class IConsumerListener;
42class NativeHandle;
Mathias Agopiana4e19522013-07-31 20:09:53 -070043
44class IGraphicBufferConsumer : public IInterface {
45
46public:
Igor Murashkin7d2d1602013-11-12 18:02:20 -080047 enum {
48 // Returned by releaseBuffer, after which the consumer must
49 // free any references to the just-released buffer that it might have.
50 STALE_BUFFER_SLOT = 1,
51 // Returned by dequeueBuffer if there are no pending buffers available.
52 NO_BUFFER_AVAILABLE,
53 // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
54 PRESENT_LATER,
55 };
Mathias Agopiana4e19522013-07-31 20:09:53 -070056
57 // acquireBuffer attempts to acquire ownership of the next pending buffer in
Igor Murashkin7d2d1602013-11-12 18:02:20 -080058 // the BufferQueue. If no buffer is pending then it returns
59 // NO_BUFFER_AVAILABLE. If a buffer is successfully acquired, the
60 // information about the buffer is returned in BufferItem.
61 //
62 // If the buffer returned had previously been
Mathias Agopiana4e19522013-07-31 20:09:53 -070063 // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
64 // NULL and it is assumed that the consumer still holds a reference to the
65 // buffer.
66 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -080067 // If presentWhen is non-zero, it indicates the time when the buffer will
Mathias Agopiana4e19522013-07-31 20:09:53 -070068 // be displayed on screen. If the buffer's timestamp is farther in the
69 // future, the buffer won't be acquired, and PRESENT_LATER will be
70 // returned. The presentation time is in nanoseconds, and the time base
71 // is CLOCK_MONOTONIC.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080072 //
Dan Stozaa4650a52015-05-12 12:56:16 -070073 // If maxFrameNumber is non-zero, it indicates that acquireBuffer should
74 // only return a buffer with a frame number less than or equal to
75 // maxFrameNumber. If no such frame is available (such as when a buffer has
76 // been replaced but the consumer has not received the onFrameReplaced
77 // callback), then PRESENT_LATER will be returned.
78 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -080079 // Return of NO_ERROR means the operation completed as normal.
80 //
81 // Return of a positive value means the operation could not be completed
82 // at this time, but the user should try again later:
83 // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
84 // * PRESENT_LATER - the buffer's timestamp is farther in the future
85 //
86 // Return of a negative value means an error has occurred:
87 // * INVALID_OPERATION - too many buffers have been acquired
Dan Stozaa4650a52015-05-12 12:56:16 -070088 virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen,
89 uint64_t maxFrameNumber = 0) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -070090
Dan Stoza9f3053d2014-03-06 15:14:33 -080091 // detachBuffer attempts to remove all ownership of the buffer in the given
92 // slot from the buffer queue. If this call succeeds, the slot will be
93 // freed, and there will be no way to obtain the buffer from this interface.
94 // The freed slot will remain unallocated until either it is selected to
95 // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
96 // to the slot. The buffer must have already been acquired.
97 //
98 // Return of a value other than NO_ERROR means an error has occurred:
99 // * BAD_VALUE - the given slot number is invalid, either because it is
100 // out of the range [0, NUM_BUFFER_SLOTS) or because the slot
101 // it refers to is not currently acquired.
102 virtual status_t detachBuffer(int slot) = 0;
103
104 // attachBuffer attempts to transfer ownership of a buffer to the buffer
105 // queue. If this call succeeds, it will be as if this buffer was acquired
106 // from the returned slot number. As such, this call will fail if attaching
107 // this buffer would cause too many buffers to be simultaneously acquired.
108 //
109 // If the buffer is successfully attached, its frameNumber is initialized
110 // to 0. This must be passed into the releaseBuffer call or else the buffer
111 // will be deallocated as stale.
112 //
113 // Return of a value other than NO_ERROR means an error has occurred:
Dan Stoza812ed062015-06-02 15:45:22 -0700114 // * BAD_VALUE - outSlot or buffer were NULL, or the generation number of
115 // the buffer did not match the buffer queue.
Dan Stoza9f3053d2014-03-06 15:14:33 -0800116 // * INVALID_OPERATION - cannot attach the buffer because it would cause too
117 // many buffers to be acquired.
118 // * NO_MEMORY - no free slots available
119 virtual status_t attachBuffer(int *outSlot,
120 const sp<GraphicBuffer>& buffer) = 0;
121
Mathias Agopiana4e19522013-07-31 20:09:53 -0700122 // releaseBuffer releases a buffer slot from the consumer back to the
123 // BufferQueue. This may be done while the buffer's contents are still
124 // being accessed. The fence will signal when the buffer is no longer
125 // in use. frameNumber is used to indentify the exact buffer returned.
126 //
127 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
128 // any references to the just-released buffer that it might have, as if it
129 // had received a onBuffersReleased() call with a mask set for the released
130 // buffer.
131 //
132 // Note that the dependencies on EGL will be removed once we switch to using
133 // the Android HW Sync HAL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800134 //
135 // Return of NO_ERROR means the operation completed as normal.
136 //
137 // Return of a positive value means the operation could not be completed
138 // at this time, but the user should try again later:
139 // * STALE_BUFFER_SLOT - see above (second paragraph)
140 //
141 // Return of a negative value means an error has occurred:
142 // * BAD_VALUE - one of the following could've happened:
143 // * the buffer slot was invalid
144 // * the fence was NULL
145 // * the buffer slot specified is not in the acquired state
Mathias Agopiana4e19522013-07-31 20:09:53 -0700146 virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
147 EGLDisplay display, EGLSyncKHR fence,
148 const sp<Fence>& releaseFence) = 0;
149
150 // consumerConnect connects a consumer to the BufferQueue. Only one
151 // consumer may be connected, and when that consumer disconnects the
152 // BufferQueue is placed into the "abandoned" state, causing most
153 // interactions with the BufferQueue by the producer to fail.
154 // controlledByApp indicates whether the consumer is controlled by
155 // the application.
156 //
157 // consumer may not be NULL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800158 //
159 // Return of a value other than NO_ERROR means an error has occurred:
160 // * NO_INIT - the buffer queue has been abandoned
161 // * BAD_VALUE - a NULL consumer was provided
Mathias Agopiana4e19522013-07-31 20:09:53 -0700162 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
163
164 // consumerDisconnect disconnects a consumer from the BufferQueue. All
165 // buffers will be freed and the BufferQueue is placed in the "abandoned"
166 // state, causing most interactions with the BufferQueue by the producer to
167 // fail.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800168 //
169 // Return of a value other than NO_ERROR means an error has occurred:
170 // * BAD_VALUE - no consumer is currently connected
Mathias Agopiana4e19522013-07-31 20:09:53 -0700171 virtual status_t consumerDisconnect() = 0;
172
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800173 // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
174 // Each bit index with a 1 corresponds to a released buffer slot with that
175 // index value. In particular, a released buffer is one that has
176 // been released by the BufferQueue but have not yet been released by the consumer.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700177 //
178 // This should be called from the onBuffersReleased() callback.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800179 //
180 // Return of a value other than NO_ERROR means an error has occurred:
181 // * NO_INIT - the buffer queue has been abandoned.
Dan Stozafebd4f42014-04-09 16:14:51 -0700182 virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700183
184 // setDefaultBufferSize is used to set the size of buffers returned by
185 // dequeueBuffer when a width and height of zero is requested. Default
186 // is 1x1.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800187 //
188 // Return of a value other than NO_ERROR means an error has occurred:
189 // * BAD_VALUE - either w or h was zero
Mathias Agopiana4e19522013-07-31 20:09:53 -0700190 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
191
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700192 // setMaxBufferCount sets the maximum value for the number of buffers used
193 // in the buffer queue (the initial default is NUM_BUFFER_SLOTS). If a call
194 // to setMaxAcquiredBufferCount (by the consumer), or a call to setAsyncMode
195 // or setMaxDequeuedBufferCount (by the producer), would cause this value to
196 // be exceeded then that call will fail. This call will fail if a producer
197 // is connected to the BufferQueue.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700198 //
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700199 // The count must be between 1 and NUM_BUFFER_SLOTS, inclusive. The count
200 // cannot be less than maxAcquiredBufferCount.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800201 //
202 // Return of a value other than NO_ERROR means an error has occurred:
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800203 // * BAD_VALUE - one of the below conditions occurred:
204 // * bufferCount was out of range (see above).
205 // * failure to adjust the number of available slots.
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700206 // * INVALID_OPERATION - attempting to call this after a producer connected.
207 virtual status_t setMaxBufferCount(int bufferCount) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700208
209 // setMaxAcquiredBufferCount sets the maximum number of buffers that can
Pablo Ceballos72daab62015-12-07 16:38:43 -0800210 // be acquired by the consumer at one time (default 1). If this method
211 // succeeds, any new buffer slots will be both unallocated and owned by the
212 // BufferQueue object (i.e. they are not owned by the producer or consumer).
213 // Calling this may also cause some buffer slots to be emptied.
214 //
215 // This function should not be called with a value of maxAcquiredBuffers
216 // that is less than the number of currently acquired buffer slots. Doing so
217 // will result in a BAD_VALUE error.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800218 //
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700219 // maxAcquiredBuffers must be (inclusive) between 1 and
220 // MAX_MAX_ACQUIRED_BUFFERS. It also cannot cause the maxBufferCount value
221 // to be exceeded.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800222 //
223 // Return of a value other than NO_ERROR means an error has occurred:
Pablo Ceballos72daab62015-12-07 16:38:43 -0800224 // * NO_INIT - the buffer queue has been abandoned
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800225 // * BAD_VALUE - one of the below conditions occurred:
226 // * maxAcquiredBuffers was out of range (see above).
227 // * failure to adjust the number of available slots.
Pablo Ceballos72daab62015-12-07 16:38:43 -0800228 // * client would have more than the requested number of
229 // acquired buffers after this call
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800230 // * INVALID_OPERATION - attempting to call this after a producer connected.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700231 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
232
233 // setConsumerName sets the name used in logging
234 virtual void setConsumerName(const String8& name) = 0;
235
236 // setDefaultBufferFormat allows the BufferQueue to create
237 // GraphicBuffers of a defaultFormat if no format is specified
Dan Stozad723bd72014-11-18 10:24:03 -0800238 // in dequeueBuffer.
239 // The initial default is PIXEL_FORMAT_RGBA_8888.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800240 //
241 // Return of a value other than NO_ERROR means an unknown error has occurred.
Dan Stozad723bd72014-11-18 10:24:03 -0800242 virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700243
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800244 // setDefaultBufferDataSpace is a request to the producer to provide buffers
245 // of the indicated dataSpace. The producer may ignore this request.
246 // The initial default is HAL_DATASPACE_UNKNOWN.
247 //
248 // Return of a value other than NO_ERROR means an unknown error has occurred.
249 virtual status_t setDefaultBufferDataSpace(
250 android_dataspace defaultDataSpace) = 0;
251
Mathias Agopiana4e19522013-07-31 20:09:53 -0700252 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
253 // These are merged with the bits passed to dequeueBuffer. The values are
254 // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800255 //
256 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700257 virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
258
259 // setTransformHint bakes in rotation to buffers so overlays can be used.
260 // The values are enumerated in window.h, e.g.
261 // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800262 //
263 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700264 virtual status_t setTransformHint(uint32_t hint) = 0;
265
Jesse Hall399184a2014-03-03 15:42:54 -0800266 // Retrieve the sideband buffer stream, if any.
267 virtual sp<NativeHandle> getSidebandStream() const = 0;
268
Dan Stozae77c7662016-05-13 11:37:28 -0700269 // Retrieves any stored segments of the occupancy history of this
270 // BufferQueue and clears them. Optionally closes out the pending segment if
271 // forceFlush is true.
272 virtual status_t getOccupancyHistory(bool forceFlush,
273 std::vector<OccupancyTracker::Segment>* outHistory) = 0;
274
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700275 // discardFreeBuffers releases all currently-free buffers held by the queue,
276 // in order to reduce the memory consumption of the queue to the minimum
277 // possible without discarding data.
278 virtual status_t discardFreeBuffers() = 0;
279
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700280 // dump state into a string
Colin Cross3d1d2802016-09-26 18:10:16 -0700281 virtual void dumpState(String8& result, const char* prefix) const = 0;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700282
Mathias Agopiana4e19522013-07-31 20:09:53 -0700283public:
Colin Cross17576de2016-09-26 13:07:06 -0700284 DECLARE_META_INTERFACE(GraphicBufferConsumer)
Mathias Agopiana4e19522013-07-31 20:09:53 -0700285};
286
287// ----------------------------------------------------------------------------
288
289class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
290{
291public:
292 virtual status_t onTransact( uint32_t code,
293 const Parcel& data,
294 Parcel* reply,
295 uint32_t flags = 0);
296};
297
298// ----------------------------------------------------------------------------
299}; // namespace android
300
301#endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H