Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 28 | #include <ui/PixelFormat.h> |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 29 | #include <ui/Rect.h> |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 30 | #include <gui/OccupancyTracker.h> |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 31 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 32 | #include <EGL/egl.h> |
| 33 | #include <EGL/eglext.h> |
| 34 | |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
Dan Stoza | de7100a | 2015-03-11 16:38:47 -0700 | [diff] [blame] | 38 | class BufferItem; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 39 | class Fence; |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 40 | class GraphicBuffer; |
| 41 | class IConsumerListener; |
| 42 | class NativeHandle; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 43 | |
| 44 | class IGraphicBufferConsumer : public IInterface { |
| 45 | |
| 46 | public: |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 47 | 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 Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 56 | |
| 57 | // acquireBuffer attempts to acquire ownership of the next pending buffer in |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 58 | // 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 Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 63 | // 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 Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 67 | // If presentWhen is non-zero, it indicates the time when the buffer will |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 68 | // 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 Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 72 | // |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 73 | // 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 Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 79 | // 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 Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 88 | virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen, |
| 89 | uint64_t maxFrameNumber = 0) = 0; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 90 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 91 | // 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 Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 114 | // * BAD_VALUE - outSlot or buffer were NULL, or the generation number of |
| 115 | // the buffer did not match the buffer queue. |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 116 | // * 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 Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 122 | // 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 Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 134 | // |
| 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 Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 146 | 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 Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 158 | // |
| 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 Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 162 | 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 Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 168 | // |
| 169 | // Return of a value other than NO_ERROR means an error has occurred: |
| 170 | // * BAD_VALUE - no consumer is currently connected |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 171 | virtual status_t consumerDisconnect() = 0; |
| 172 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 173 | // 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 Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 177 | // |
| 178 | // This should be called from the onBuffersReleased() callback. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 179 | // |
| 180 | // Return of a value other than NO_ERROR means an error has occurred: |
| 181 | // * NO_INIT - the buffer queue has been abandoned. |
Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 182 | virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 183 | |
| 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 Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 187 | // |
| 188 | // Return of a value other than NO_ERROR means an error has occurred: |
| 189 | // * BAD_VALUE - either w or h was zero |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 190 | virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0; |
| 191 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 192 | // 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 Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 198 | // |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 199 | // The count must be between 1 and NUM_BUFFER_SLOTS, inclusive. The count |
| 200 | // cannot be less than maxAcquiredBufferCount. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 201 | // |
| 202 | // Return of a value other than NO_ERROR means an error has occurred: |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 203 | // * 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 Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 206 | // * INVALID_OPERATION - attempting to call this after a producer connected. |
| 207 | virtual status_t setMaxBufferCount(int bufferCount) = 0; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 208 | |
| 209 | // setMaxAcquiredBufferCount sets the maximum number of buffers that can |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 210 | // 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 Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 218 | // |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 219 | // maxAcquiredBuffers must be (inclusive) between 1 and |
| 220 | // MAX_MAX_ACQUIRED_BUFFERS. It also cannot cause the maxBufferCount value |
| 221 | // to be exceeded. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 222 | // |
| 223 | // Return of a value other than NO_ERROR means an error has occurred: |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 224 | // * NO_INIT - the buffer queue has been abandoned |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 225 | // * 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 Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 228 | // * client would have more than the requested number of |
| 229 | // acquired buffers after this call |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 230 | // * INVALID_OPERATION - attempting to call this after a producer connected. |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 231 | 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 Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 238 | // in dequeueBuffer. |
| 239 | // The initial default is PIXEL_FORMAT_RGBA_8888. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 240 | // |
| 241 | // Return of a value other than NO_ERROR means an unknown error has occurred. |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 242 | virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 243 | |
Eino-Ville Talvala | 5b75a51 | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 244 | // 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 Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 252 | // 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 Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 255 | // |
| 256 | // Return of a value other than NO_ERROR means an unknown error has occurred. |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 257 | 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 Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 262 | // |
| 263 | // Return of a value other than NO_ERROR means an unknown error has occurred. |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 264 | virtual status_t setTransformHint(uint32_t hint) = 0; |
| 265 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 266 | // Retrieve the sideband buffer stream, if any. |
| 267 | virtual sp<NativeHandle> getSidebandStream() const = 0; |
| 268 | |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 269 | // 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 Talvala | bc2df65 | 2016-07-21 17:06:58 -0700 | [diff] [blame] | 275 | // 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 Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 280 | // dump state into a string |
| 281 | virtual void dump(String8& result, const char* prefix) const = 0; |
| 282 | |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 283 | public: |
Colin Cross | 17576de | 2016-09-26 13:07:06 -0700 | [diff] [blame] | 284 | DECLARE_META_INTERFACE(GraphicBufferConsumer) |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | // ---------------------------------------------------------------------------- |
| 288 | |
| 289 | class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer> |
| 290 | { |
| 291 | public: |
| 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 |