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