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> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 28 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 29 | #include <ui/PixelFormat.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 30 | |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 31 | #include <gui/OccupancyTracker.h> |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 32 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 33 | #include <EGL/egl.h> |
| 34 | #include <EGL/eglext.h> |
| 35 | |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 36 | namespace android { |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
Dan Stoza | de7100a | 2015-03-11 16:38:47 -0700 | [diff] [blame] | 39 | class BufferItem; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 40 | class Fence; |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 41 | class GraphicBuffer; |
| 42 | class IConsumerListener; |
| 43 | class NativeHandle; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 44 | |
| 45 | class IGraphicBufferConsumer : public IInterface { |
| 46 | |
| 47 | public: |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 48 | enum { |
| 49 | // Returned by releaseBuffer, after which the consumer must |
| 50 | // free any references to the just-released buffer that it might have. |
| 51 | STALE_BUFFER_SLOT = 1, |
| 52 | // Returned by dequeueBuffer if there are no pending buffers available. |
| 53 | NO_BUFFER_AVAILABLE, |
| 54 | // Returned by dequeueBuffer if it's too early for the buffer to be acquired. |
| 55 | PRESENT_LATER, |
| 56 | }; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 57 | |
| 58 | // acquireBuffer attempts to acquire ownership of the next pending buffer in |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 59 | // the BufferQueue. If no buffer is pending then it returns |
| 60 | // NO_BUFFER_AVAILABLE. If a buffer is successfully acquired, the |
| 61 | // information about the buffer is returned in BufferItem. |
| 62 | // |
| 63 | // If the buffer returned had previously been |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 64 | // acquired then the BufferItem::mGraphicBuffer field of buffer is set to |
| 65 | // NULL and it is assumed that the consumer still holds a reference to the |
| 66 | // buffer. |
| 67 | // |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 68 | // 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] | 69 | // be displayed on screen. If the buffer's timestamp is farther in the |
| 70 | // future, the buffer won't be acquired, and PRESENT_LATER will be |
| 71 | // returned. The presentation time is in nanoseconds, and the time base |
| 72 | // is CLOCK_MONOTONIC. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 73 | // |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 74 | // If maxFrameNumber is non-zero, it indicates that acquireBuffer should |
| 75 | // only return a buffer with a frame number less than or equal to |
| 76 | // maxFrameNumber. If no such frame is available (such as when a buffer has |
| 77 | // been replaced but the consumer has not received the onFrameReplaced |
| 78 | // callback), then PRESENT_LATER will be returned. |
| 79 | // |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 80 | // Return of NO_ERROR means the operation completed as normal. |
| 81 | // |
| 82 | // Return of a positive value means the operation could not be completed |
| 83 | // at this time, but the user should try again later: |
| 84 | // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer) |
| 85 | // * PRESENT_LATER - the buffer's timestamp is farther in the future |
| 86 | // |
| 87 | // Return of a negative value means an error has occurred: |
| 88 | // * INVALID_OPERATION - too many buffers have been acquired |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 89 | virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen, |
| 90 | uint64_t maxFrameNumber = 0) = 0; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 91 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 92 | // detachBuffer attempts to remove all ownership of the buffer in the given |
| 93 | // slot from the buffer queue. If this call succeeds, the slot will be |
| 94 | // freed, and there will be no way to obtain the buffer from this interface. |
| 95 | // The freed slot will remain unallocated until either it is selected to |
| 96 | // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached |
| 97 | // to the slot. The buffer must have already been acquired. |
| 98 | // |
| 99 | // Return of a value other than NO_ERROR means an error has occurred: |
| 100 | // * BAD_VALUE - the given slot number is invalid, either because it is |
| 101 | // out of the range [0, NUM_BUFFER_SLOTS) or because the slot |
| 102 | // it refers to is not currently acquired. |
| 103 | virtual status_t detachBuffer(int slot) = 0; |
| 104 | |
| 105 | // attachBuffer attempts to transfer ownership of a buffer to the buffer |
| 106 | // queue. If this call succeeds, it will be as if this buffer was acquired |
| 107 | // from the returned slot number. As such, this call will fail if attaching |
| 108 | // this buffer would cause too many buffers to be simultaneously acquired. |
| 109 | // |
| 110 | // If the buffer is successfully attached, its frameNumber is initialized |
| 111 | // to 0. This must be passed into the releaseBuffer call or else the buffer |
| 112 | // will be deallocated as stale. |
| 113 | // |
| 114 | // 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] | 115 | // * BAD_VALUE - outSlot or buffer were NULL, or the generation number of |
| 116 | // the buffer did not match the buffer queue. |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 117 | // * INVALID_OPERATION - cannot attach the buffer because it would cause too |
| 118 | // many buffers to be acquired. |
| 119 | // * NO_MEMORY - no free slots available |
| 120 | virtual status_t attachBuffer(int *outSlot, |
| 121 | const sp<GraphicBuffer>& buffer) = 0; |
| 122 | |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 123 | // releaseBuffer releases a buffer slot from the consumer back to the |
| 124 | // BufferQueue. This may be done while the buffer's contents are still |
| 125 | // being accessed. The fence will signal when the buffer is no longer |
| 126 | // in use. frameNumber is used to indentify the exact buffer returned. |
| 127 | // |
| 128 | // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free |
| 129 | // any references to the just-released buffer that it might have, as if it |
| 130 | // had received a onBuffersReleased() call with a mask set for the released |
| 131 | // buffer. |
| 132 | // |
| 133 | // Note that the dependencies on EGL will be removed once we switch to using |
| 134 | // the Android HW Sync HAL. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 135 | // |
| 136 | // Return of NO_ERROR means the operation completed as normal. |
| 137 | // |
| 138 | // Return of a positive value means the operation could not be completed |
| 139 | // at this time, but the user should try again later: |
| 140 | // * STALE_BUFFER_SLOT - see above (second paragraph) |
| 141 | // |
| 142 | // Return of a negative value means an error has occurred: |
| 143 | // * BAD_VALUE - one of the following could've happened: |
| 144 | // * the buffer slot was invalid |
| 145 | // * the fence was NULL |
| 146 | // * the buffer slot specified is not in the acquired state |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 147 | virtual status_t releaseBuffer(int buf, uint64_t frameNumber, |
| 148 | EGLDisplay display, EGLSyncKHR fence, |
| 149 | const sp<Fence>& releaseFence) = 0; |
| 150 | |
| 151 | // consumerConnect connects a consumer to the BufferQueue. Only one |
| 152 | // consumer may be connected, and when that consumer disconnects the |
| 153 | // BufferQueue is placed into the "abandoned" state, causing most |
| 154 | // interactions with the BufferQueue by the producer to fail. |
| 155 | // controlledByApp indicates whether the consumer is controlled by |
| 156 | // the application. |
| 157 | // |
| 158 | // consumer may not be NULL. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 159 | // |
| 160 | // Return of a value other than NO_ERROR means an error has occurred: |
| 161 | // * NO_INIT - the buffer queue has been abandoned |
| 162 | // * BAD_VALUE - a NULL consumer was provided |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 163 | virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0; |
| 164 | |
| 165 | // consumerDisconnect disconnects a consumer from the BufferQueue. All |
| 166 | // buffers will be freed and the BufferQueue is placed in the "abandoned" |
| 167 | // state, causing most interactions with the BufferQueue by the producer to |
| 168 | // fail. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 169 | // |
| 170 | // Return of a value other than NO_ERROR means an error has occurred: |
| 171 | // * BAD_VALUE - no consumer is currently connected |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 172 | virtual status_t consumerDisconnect() = 0; |
| 173 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 174 | // getReleasedBuffers sets the value pointed to by slotMask to a bit set. |
| 175 | // Each bit index with a 1 corresponds to a released buffer slot with that |
| 176 | // index value. In particular, a released buffer is one that has |
| 177 | // 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] | 178 | // |
| 179 | // This should be called from the onBuffersReleased() callback. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 180 | // |
| 181 | // Return of a value other than NO_ERROR means an error has occurred: |
| 182 | // * NO_INIT - the buffer queue has been abandoned. |
Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 183 | virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 184 | |
| 185 | // setDefaultBufferSize is used to set the size of buffers returned by |
| 186 | // dequeueBuffer when a width and height of zero is requested. Default |
| 187 | // is 1x1. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 188 | // |
| 189 | // Return of a value other than NO_ERROR means an error has occurred: |
| 190 | // * BAD_VALUE - either w or h was zero |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 191 | virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0; |
| 192 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 193 | // setMaxBufferCount sets the maximum value for the number of buffers used |
| 194 | // in the buffer queue (the initial default is NUM_BUFFER_SLOTS). If a call |
| 195 | // to setMaxAcquiredBufferCount (by the consumer), or a call to setAsyncMode |
| 196 | // or setMaxDequeuedBufferCount (by the producer), would cause this value to |
| 197 | // be exceeded then that call will fail. This call will fail if a producer |
| 198 | // is connected to the BufferQueue. |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 199 | // |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 200 | // The count must be between 1 and NUM_BUFFER_SLOTS, inclusive. The count |
| 201 | // cannot be less than maxAcquiredBufferCount. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 202 | // |
| 203 | // 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] | 204 | // * BAD_VALUE - one of the below conditions occurred: |
| 205 | // * bufferCount was out of range (see above). |
| 206 | // * failure to adjust the number of available slots. |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 207 | // * INVALID_OPERATION - attempting to call this after a producer connected. |
| 208 | virtual status_t setMaxBufferCount(int bufferCount) = 0; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 209 | |
| 210 | // setMaxAcquiredBufferCount sets the maximum number of buffers that can |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 211 | // be acquired by the consumer at one time (default 1). If this method |
| 212 | // succeeds, any new buffer slots will be both unallocated and owned by the |
| 213 | // BufferQueue object (i.e. they are not owned by the producer or consumer). |
| 214 | // Calling this may also cause some buffer slots to be emptied. |
| 215 | // |
| 216 | // This function should not be called with a value of maxAcquiredBuffers |
| 217 | // that is less than the number of currently acquired buffer slots. Doing so |
| 218 | // will result in a BAD_VALUE error. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 219 | // |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 220 | // maxAcquiredBuffers must be (inclusive) between 1 and |
| 221 | // MAX_MAX_ACQUIRED_BUFFERS. It also cannot cause the maxBufferCount value |
| 222 | // to be exceeded. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 223 | // |
| 224 | // 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] | 225 | // * NO_INIT - the buffer queue has been abandoned |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 226 | // * BAD_VALUE - one of the below conditions occurred: |
| 227 | // * maxAcquiredBuffers was out of range (see above). |
| 228 | // * failure to adjust the number of available slots. |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 229 | // * client would have more than the requested number of |
| 230 | // acquired buffers after this call |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 231 | // * INVALID_OPERATION - attempting to call this after a producer connected. |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 232 | virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0; |
| 233 | |
| 234 | // setConsumerName sets the name used in logging |
| 235 | virtual void setConsumerName(const String8& name) = 0; |
| 236 | |
| 237 | // setDefaultBufferFormat allows the BufferQueue to create |
| 238 | // GraphicBuffers of a defaultFormat if no format is specified |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 239 | // in dequeueBuffer. |
| 240 | // The initial default is PIXEL_FORMAT_RGBA_8888. |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 241 | // |
| 242 | // 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] | 243 | virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 244 | |
Eino-Ville Talvala | 5b75a51 | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 245 | // setDefaultBufferDataSpace is a request to the producer to provide buffers |
| 246 | // of the indicated dataSpace. The producer may ignore this request. |
| 247 | // The initial default is HAL_DATASPACE_UNKNOWN. |
| 248 | // |
| 249 | // Return of a value other than NO_ERROR means an unknown error has occurred. |
| 250 | virtual status_t setDefaultBufferDataSpace( |
| 251 | android_dataspace defaultDataSpace) = 0; |
| 252 | |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 253 | // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer. |
| 254 | // These are merged with the bits passed to dequeueBuffer. The values are |
| 255 | // 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] | 256 | // |
| 257 | // 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] | 258 | virtual status_t setConsumerUsageBits(uint32_t usage) = 0; |
| 259 | |
| 260 | // setTransformHint bakes in rotation to buffers so overlays can be used. |
| 261 | // The values are enumerated in window.h, e.g. |
| 262 | // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform). |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 263 | // |
| 264 | // 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] | 265 | virtual status_t setTransformHint(uint32_t hint) = 0; |
| 266 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 267 | // Retrieve the sideband buffer stream, if any. |
| 268 | virtual sp<NativeHandle> getSidebandStream() const = 0; |
| 269 | |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 270 | // Retrieves any stored segments of the occupancy history of this |
| 271 | // BufferQueue and clears them. Optionally closes out the pending segment if |
| 272 | // forceFlush is true. |
| 273 | virtual status_t getOccupancyHistory(bool forceFlush, |
| 274 | std::vector<OccupancyTracker::Segment>* outHistory) = 0; |
| 275 | |
Eino-Ville Talvala | bc2df65 | 2016-07-21 17:06:58 -0700 | [diff] [blame] | 276 | // discardFreeBuffers releases all currently-free buffers held by the queue, |
| 277 | // in order to reduce the memory consumption of the queue to the minimum |
| 278 | // possible without discarding data. |
| 279 | virtual status_t discardFreeBuffers() = 0; |
| 280 | |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 281 | // dump state into a string |
Colin Cross | 3d1d280 | 2016-09-26 18:10:16 -0700 | [diff] [blame] | 282 | virtual void dumpState(String8& result, const char* prefix) const = 0; |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 283 | |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 284 | public: |
Colin Cross | 17576de | 2016-09-26 13:07:06 -0700 | [diff] [blame] | 285 | DECLARE_META_INTERFACE(GraphicBufferConsumer) |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | // ---------------------------------------------------------------------------- |
| 289 | |
| 290 | class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer> |
| 291 | { |
| 292 | public: |
| 293 | virtual status_t onTransact( uint32_t code, |
| 294 | const Parcel& data, |
| 295 | Parcel* reply, |
| 296 | uint32_t flags = 0); |
| 297 | }; |
| 298 | |
| 299 | // ---------------------------------------------------------------------------- |
| 300 | }; // namespace android |
| 301 | |
| 302 | #endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H |