| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2014 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_BUFFERQUEUECONSUMER_H | 
|  | 18 | #define ANDROID_GUI_BUFFERQUEUECONSUMER_H | 
|  | 19 |  | 
|  | 20 | #include <EGL/egl.h> | 
|  | 21 | #include <EGL/eglext.h> | 
|  | 22 |  | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 23 | #include <gui/BufferQueueDefs.h> | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 24 | #include <gui/IGraphicBufferConsumer.h> | 
|  | 25 |  | 
|  | 26 | namespace android { | 
|  | 27 |  | 
|  | 28 | class BufferQueueCore; | 
|  | 29 |  | 
|  | 30 | class BufferQueueConsumer : public BnGraphicBufferConsumer { | 
|  | 31 |  | 
|  | 32 | public: | 
|  | 33 | BufferQueueConsumer(const sp<BufferQueueCore>& core); | 
|  | 34 | virtual ~BufferQueueConsumer(); | 
|  | 35 |  | 
|  | 36 | // acquireBuffer attempts to acquire ownership of the next pending buffer in | 
|  | 37 | // the BufferQueue. If no buffer is pending then it returns | 
|  | 38 | // NO_BUFFER_AVAILABLE. If a buffer is successfully acquired, the | 
|  | 39 | // information about the buffer is returned in BufferItem.  If the buffer | 
|  | 40 | // returned had previously been acquired then the BufferItem::mGraphicBuffer | 
|  | 41 | // field of buffer is set to NULL and it is assumed that the consumer still | 
|  | 42 | // holds a reference to the buffer. | 
|  | 43 | // | 
|  | 44 | // If expectedPresent is nonzero, it indicates the time when the buffer | 
|  | 45 | // will be displayed on screen. If the buffer's timestamp is farther in the | 
|  | 46 | // future, the buffer won't be acquired, and PRESENT_LATER will be | 
|  | 47 | // returned.  The presentation time is in nanoseconds, and the time base | 
|  | 48 | // is CLOCK_MONOTONIC. | 
|  | 49 | virtual status_t acquireBuffer(BufferItem* outBuffer, | 
|  | 50 | nsecs_t expectedPresent); | 
|  | 51 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 52 | // See IGraphicBufferConsumer::detachBuffer | 
|  | 53 | virtual status_t detachBuffer(int slot); | 
|  | 54 |  | 
|  | 55 | // See IGraphicBufferConsumer::attachBuffer | 
|  | 56 | virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer); | 
|  | 57 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 58 | // releaseBuffer releases a buffer slot from the consumer back to the | 
|  | 59 | // BufferQueue.  This may be done while the buffer's contents are still | 
|  | 60 | // being accessed.  The fence will signal when the buffer is no longer | 
|  | 61 | // in use. frameNumber is used to indentify the exact buffer returned. | 
|  | 62 | // | 
|  | 63 | // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free | 
|  | 64 | // any references to the just-released buffer that it might have, as if it | 
|  | 65 | // had received a onBuffersReleased() call with a mask set for the released | 
|  | 66 | // buffer. | 
|  | 67 | // | 
|  | 68 | // Note that the dependencies on EGL will be removed once we switch to using | 
|  | 69 | // the Android HW Sync HAL. | 
|  | 70 | virtual status_t releaseBuffer(int slot, uint64_t frameNumber, | 
|  | 71 | const sp<Fence>& releaseFence, EGLDisplay display, | 
|  | 72 | EGLSyncKHR fence); | 
|  | 73 |  | 
|  | 74 | // connect connects a consumer to the BufferQueue.  Only one | 
|  | 75 | // consumer may be connected, and when that consumer disconnects the | 
|  | 76 | // BufferQueue is placed into the "abandoned" state, causing most | 
|  | 77 | // interactions with the BufferQueue by the producer to fail. | 
|  | 78 | // controlledByApp indicates whether the consumer is controlled by | 
|  | 79 | // the application. | 
|  | 80 | // | 
|  | 81 | // consumerListener may not be NULL. | 
|  | 82 | virtual status_t connect(const sp<IConsumerListener>& consumerListener, | 
|  | 83 | bool controlledByApp); | 
|  | 84 |  | 
|  | 85 | // disconnect disconnects a consumer from the BufferQueue. All | 
|  | 86 | // buffers will be freed and the BufferQueue is placed in the "abandoned" | 
|  | 87 | // state, causing most interactions with the BufferQueue by the producer to | 
|  | 88 | // fail. | 
|  | 89 | virtual status_t disconnect(); | 
|  | 90 |  | 
|  | 91 | // getReleasedBuffers sets the value pointed to by outSlotMask to a bit mask | 
|  | 92 | // indicating which buffer slots have been released by the BufferQueue | 
|  | 93 | // but have not yet been released by the consumer. | 
|  | 94 | // | 
|  | 95 | // This should be called from the onBuffersReleased() callback. | 
| Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 96 | virtual status_t getReleasedBuffers(uint64_t* outSlotMask); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 97 |  | 
|  | 98 | // setDefaultBufferSize is used to set the size of buffers returned by | 
|  | 99 | // dequeueBuffer when a width and height of zero is requested.  Default | 
|  | 100 | // is 1x1. | 
|  | 101 | virtual status_t setDefaultBufferSize(uint32_t width, uint32_t height); | 
|  | 102 |  | 
|  | 103 | // setDefaultMaxBufferCount sets the default value for the maximum buffer | 
|  | 104 | // count (the initial default is 2). If the producer has requested a | 
|  | 105 | // buffer count using setBufferCount, the default buffer count will only | 
|  | 106 | // take effect if the producer sets the count back to zero. | 
|  | 107 | // | 
|  | 108 | // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. | 
|  | 109 | virtual status_t setDefaultMaxBufferCount(int bufferCount); | 
|  | 110 |  | 
|  | 111 | // disableAsyncBuffer disables the extra buffer used in async mode | 
|  | 112 | // (when both producer and consumer have set their "isControlledByApp" | 
|  | 113 | // flag) and has dequeueBuffer() return WOULD_BLOCK instead. | 
|  | 114 | // | 
|  | 115 | // This can only be called before connect(). | 
|  | 116 | virtual status_t disableAsyncBuffer(); | 
|  | 117 |  | 
|  | 118 | // setMaxAcquiredBufferCount sets the maximum number of buffers that can | 
|  | 119 | // be acquired by the consumer at one time (default 1).  This call will | 
|  | 120 | // fail if a producer is connected to the BufferQueue. | 
|  | 121 | virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers); | 
|  | 122 |  | 
|  | 123 | // setConsumerName sets the name used in logging | 
|  | 124 | virtual void setConsumerName(const String8& name); | 
|  | 125 |  | 
|  | 126 | // setDefaultBufferFormat allows the BufferQueue to create | 
|  | 127 | // GraphicBuffers of a defaultFormat if no format is specified | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 128 | // in dequeueBuffer. The initial default is HAL_PIXEL_FORMAT_RGBA_8888. | 
|  | 129 | virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 130 |  | 
| Eino-Ville Talvala | 5b75a51 | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 131 | // setDefaultBufferDataSpace allows the BufferQueue to create | 
|  | 132 | // GraphicBuffers of a defaultDataSpace if no data space is specified | 
|  | 133 | // in queueBuffer. | 
|  | 134 | // The initial default is HAL_DATASPACE_UNKNOWN | 
|  | 135 | virtual status_t setDefaultBufferDataSpace( | 
|  | 136 | android_dataspace defaultDataSpace); | 
|  | 137 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 138 | // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer. | 
|  | 139 | // These are merged with the bits passed to dequeueBuffer.  The values are | 
|  | 140 | // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0. | 
|  | 141 | virtual status_t setConsumerUsageBits(uint32_t usage); | 
|  | 142 |  | 
|  | 143 | // setTransformHint bakes in rotation to buffers so overlays can be used. | 
|  | 144 | // The values are enumerated in window.h, e.g. | 
|  | 145 | // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform). | 
|  | 146 | virtual status_t setTransformHint(uint32_t hint); | 
|  | 147 |  | 
| Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 148 | // Retrieve the sideband buffer stream, if any. | 
|  | 149 | virtual sp<NativeHandle> getSidebandStream() const; | 
|  | 150 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 151 | // dump our state in a String | 
|  | 152 | virtual void dump(String8& result, const char* prefix) const; | 
|  | 153 |  | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 154 | // Functions required for backwards compatibility. | 
|  | 155 | // These will be modified/renamed in IGraphicBufferConsumer and will be | 
|  | 156 | // removed from this class at that time. See b/13306289. | 
|  | 157 |  | 
|  | 158 | virtual status_t releaseBuffer(int buf, uint64_t frameNumber, | 
|  | 159 | EGLDisplay display, EGLSyncKHR fence, | 
|  | 160 | const sp<Fence>& releaseFence) { | 
|  | 161 | return releaseBuffer(buf, frameNumber, releaseFence, display, fence); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, | 
|  | 165 | bool controlledByApp) { | 
|  | 166 | return connect(consumer, controlledByApp); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | virtual status_t consumerDisconnect() { return disconnect(); } | 
|  | 170 |  | 
|  | 171 | // End functions required for backwards compatibility | 
|  | 172 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 173 | private: | 
|  | 174 | sp<BufferQueueCore> mCore; | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 175 |  | 
|  | 176 | // This references mCore->mSlots. Lock mCore->mMutex while accessing. | 
|  | 177 | BufferQueueDefs::SlotsType& mSlots; | 
|  | 178 |  | 
|  | 179 | // This is a cached copy of the name stored in the BufferQueueCore. | 
|  | 180 | // It's updated during setConsumerName. | 
|  | 181 | String8 mConsumerName; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 182 |  | 
|  | 183 | }; // class BufferQueueConsumer | 
|  | 184 |  | 
|  | 185 | } // namespace android | 
|  | 186 |  | 
|  | 187 | #endif |