| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 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_CONSUMERBASE_H | 
|  | 18 | #define ANDROID_GUI_CONSUMERBASE_H | 
|  | 19 |  | 
| Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 20 | #include <gui/BufferQueueDefs.h> | 
|  | 21 | #include <gui/IConsumerListener.h> | 
|  | 22 | #include <gui/IGraphicBufferConsumer.h> | 
|  | 23 | #include <gui/OccupancyTracker.h> | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 24 |  | 
| Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 25 | #include <ui/PixelFormat.h> | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | #include <utils/String8.h> | 
|  | 28 | #include <utils/Vector.h> | 
|  | 29 | #include <utils/threads.h> | 
| Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 30 |  | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | namespace android { | 
|  | 33 | // ---------------------------------------------------------------------------- | 
|  | 34 |  | 
|  | 35 | class String8; | 
| Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 36 | class GraphicBuffer; | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | // ConsumerBase is a base class for BufferQueue consumer end-points. It | 
|  | 39 | // handles common tasks like management of the connection to the BufferQueue | 
|  | 40 | // and the buffer pool. | 
|  | 41 | class ConsumerBase : public virtual RefBase, | 
| Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 42 | protected ConsumerListener { | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 43 | public: | 
|  | 44 | struct FrameAvailableListener : public virtual RefBase { | 
| Dan Stoza | dc13c5b | 2015-05-11 15:33:01 -0700 | [diff] [blame] | 45 | // See IConsumerListener::onFrame{Available,Replaced} | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 46 | virtual void onFrameAvailable(const BufferItem& item) = 0; | 
| Dan Stoza | dc13c5b | 2015-05-11 15:33:01 -0700 | [diff] [blame] | 47 | virtual void onFrameReplaced(const BufferItem& /* item */) {} | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 48 | }; | 
|  | 49 |  | 
| Yi Kong | ca03851 | 2017-05-02 16:55:24 -0700 | [diff] [blame] | 50 | ~ConsumerBase() override; | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | // abandon frees all the buffers and puts the ConsumerBase into the | 
|  | 53 | // 'abandoned' state.  Once put in this state the ConsumerBase can never | 
|  | 54 | // leave it.  When in the 'abandoned' state, all methods of the | 
| Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 55 | // IGraphicBufferProducer interface will fail with the NO_INIT error. | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 56 | // | 
|  | 57 | // Note that while calling this method causes all the buffers to be freed | 
|  | 58 | // from the perspective of the the ConsumerBase, if there are additional | 
|  | 59 | // references on the buffers (e.g. if a buffer is referenced by a client | 
|  | 60 | // or by OpenGL ES as a texture) then those buffer will remain allocated. | 
|  | 61 | void abandon(); | 
|  | 62 |  | 
| John Reck | e478305 | 2015-05-14 15:55:11 -0700 | [diff] [blame] | 63 | // Returns true if the ConsumerBase is in the 'abandoned' state | 
|  | 64 | bool isAbandoned(); | 
|  | 65 |  | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 66 | // set the name of the ConsumerBase that will be used to identify it in | 
|  | 67 | // log messages. | 
|  | 68 | void setName(const String8& name); | 
|  | 69 |  | 
| Colin Cross | 3d1d280 | 2016-09-26 18:10:16 -0700 | [diff] [blame] | 70 | // dumpState writes the current state to a string. Child classes should add | 
| Jesse Hall | 7adb0f8 | 2013-03-06 16:13:49 -0800 | [diff] [blame] | 71 | // their state to the dump by overriding the dumpLocked method, which is | 
|  | 72 | // called by these methods after locking the mutex. | 
| Colin Cross | 3d1d280 | 2016-09-26 18:10:16 -0700 | [diff] [blame] | 73 | void dumpState(String8& result) const; | 
|  | 74 | void dumpState(String8& result, const char* prefix) const; | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 75 |  | 
|  | 76 | // setFrameAvailableListener sets the listener object that will be notified | 
|  | 77 | // when a new frame becomes available. | 
| Igor Murashkin | a4a3149 | 2012-10-29 13:36:11 -0700 | [diff] [blame] | 78 | void setFrameAvailableListener(const wp<FrameAvailableListener>& listener); | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 79 |  | 
| Dan Stoza | 634f5ee | 2015-04-03 14:22:05 -0700 | [diff] [blame] | 80 | // See IGraphicBufferConsumer::detachBuffer | 
|  | 81 | status_t detachBuffer(int slot); | 
|  | 82 |  | 
| Michael Lentine | 847f11e | 2015-05-18 13:41:23 -0700 | [diff] [blame] | 83 | // See IGraphicBufferConsumer::setDefaultBufferSize | 
|  | 84 | status_t setDefaultBufferSize(uint32_t width, uint32_t height); | 
|  | 85 |  | 
|  | 86 | // See IGraphicBufferConsumer::setDefaultBufferFormat | 
|  | 87 | status_t setDefaultBufferFormat(PixelFormat defaultFormat); | 
|  | 88 |  | 
|  | 89 | // See IGraphicBufferConsumer::setDefaultBufferDataSpace | 
|  | 90 | status_t setDefaultBufferDataSpace(android_dataspace defaultDataSpace); | 
|  | 91 |  | 
| Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 92 | // See IGraphicBufferConsumer::getOccupancyHistory | 
|  | 93 | status_t getOccupancyHistory(bool forceFlush, | 
|  | 94 | std::vector<OccupancyTracker::Segment>* outHistory); | 
|  | 95 |  | 
| Eino-Ville Talvala | bc2df65 | 2016-07-21 17:06:58 -0700 | [diff] [blame] | 96 | // See IGraphicBufferConsumer::discardFreeBuffers | 
|  | 97 | status_t discardFreeBuffers(); | 
|  | 98 |  | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 99 | private: | 
|  | 100 | ConsumerBase(const ConsumerBase&); | 
|  | 101 | void operator=(const ConsumerBase&); | 
|  | 102 |  | 
|  | 103 | protected: | 
| Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 104 | // ConsumerBase constructs a new ConsumerBase object to consume image | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 105 | // buffers from the given IGraphicBufferConsumer. | 
| Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 106 | // The controlledByApp flag indicates that this consumer is under the application's | 
|  | 107 | // control. | 
| Chih-Hung Hsieh | 65d4787 | 2016-09-01 11:39:25 -0700 | [diff] [blame] | 108 | explicit ConsumerBase(const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp = false); | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 109 |  | 
| Jamie Gennis | ad669b0 | 2013-04-05 16:41:27 -0700 | [diff] [blame] | 110 | // onLastStrongRef gets called by RefBase just before the dtor of the most | 
|  | 111 | // derived class.  It is used to clean up the buffers so that ConsumerBase | 
|  | 112 | // can coordinate the clean-up by calling into virtual methods implemented | 
|  | 113 | // by the derived classes.  This would not be possible from the | 
|  | 114 | // ConsuemrBase dtor because by the time that gets called the derived | 
|  | 115 | // classes have already been destructed. | 
|  | 116 | // | 
|  | 117 | // This methods should not need to be overridden by derived classes, but | 
|  | 118 | // if they are overridden the ConsumerBase implementation must be called | 
|  | 119 | // from the derived class. | 
|  | 120 | virtual void onLastStrongRef(const void* id); | 
|  | 121 |  | 
| Pablo Ceballos | e07e3e5 | 2016-03-15 15:07:54 -0700 | [diff] [blame] | 122 | // Implementation of the IConsumerListener interface.  These | 
|  | 123 | // calls are used to notify the ConsumerBase of asynchronous events in the | 
|  | 124 | // BufferQueue.  The onFrameAvailable, onFrameReplaced, and | 
|  | 125 | // onBuffersReleased methods should not need to be overridden by derived | 
|  | 126 | // classes, but if they are overridden the ConsumerBase implementation must | 
|  | 127 | // be called from the derived class. The ConsumerBase version of | 
|  | 128 | // onSidebandStreamChanged does nothing and can be overriden by derived | 
|  | 129 | // classes if they want the notification. | 
|  | 130 | virtual void onFrameAvailable(const BufferItem& item) override; | 
|  | 131 | virtual void onFrameReplaced(const BufferItem& item) override; | 
|  | 132 | virtual void onBuffersReleased() override; | 
|  | 133 | virtual void onSidebandStreamChanged() override; | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 134 |  | 
|  | 135 | // freeBufferLocked frees up the given buffer slot.  If the slot has been | 
|  | 136 | // initialized this will release the reference to the GraphicBuffer in that | 
| Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 137 | // slot.  Otherwise it has no effect. | 
|  | 138 | // | 
|  | 139 | // Derived classes should override this method to clean up any state they | 
|  | 140 | // keep per slot.  If it is overridden, the derived class's implementation | 
|  | 141 | // must call ConsumerBase::freeBufferLocked. | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 142 | // | 
|  | 143 | // This method must be called with mMutex locked. | 
|  | 144 | virtual void freeBufferLocked(int slotIndex); | 
|  | 145 |  | 
|  | 146 | // abandonLocked puts the BufferQueue into the abandoned state, causing | 
|  | 147 | // all future operations on it to fail. This method rather than the public | 
|  | 148 | // abandon method should be overridden by child classes to add abandon- | 
|  | 149 | // time behavior. | 
|  | 150 | // | 
| Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 151 | // Derived classes should override this method to clean up any object | 
|  | 152 | // state they keep (as opposed to per-slot state).  If it is overridden, | 
|  | 153 | // the derived class's implementation must call ConsumerBase::abandonLocked. | 
|  | 154 | // | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 155 | // This method must be called with mMutex locked. | 
|  | 156 | virtual void abandonLocked(); | 
|  | 157 |  | 
| Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 158 | // dumpLocked dumps the current state of the ConsumerBase object to the | 
|  | 159 | // result string.  Each line is prefixed with the string pointed to by the | 
|  | 160 | // prefix argument.  The buffer argument points to a buffer that may be | 
|  | 161 | // used for intermediate formatting data, and the size of that buffer is | 
|  | 162 | // indicated by the size argument. | 
|  | 163 | // | 
|  | 164 | // Derived classes should override this method to dump their internal | 
|  | 165 | // state.  If this method is overridden the derived class's implementation | 
|  | 166 | // should call ConsumerBase::dumpLocked. | 
|  | 167 | // | 
|  | 168 | // This method must be called with mMutex locked. | 
| Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 169 | virtual void dumpLocked(String8& result, const char* prefix) const; | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 170 |  | 
|  | 171 | // acquireBufferLocked fetches the next buffer from the BufferQueue and | 
|  | 172 | // updates the buffer slot for the buffer returned. | 
| Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 173 | // | 
|  | 174 | // Derived classes should override this method to perform any | 
|  | 175 | // initialization that must take place the first time a buffer is assigned | 
|  | 176 | // to a slot.  If it is overridden the derived class's implementation must | 
|  | 177 | // call ConsumerBase::acquireBufferLocked. | 
| Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 178 | virtual status_t acquireBufferLocked(BufferItem *item, nsecs_t presentWhen, | 
|  | 179 | uint64_t maxFrameNumber = 0); | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 180 |  | 
|  | 181 | // releaseBufferLocked relinquishes control over a buffer, returning that | 
|  | 182 | // control to the BufferQueue. | 
| Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 183 | // | 
|  | 184 | // Derived classes should override this method to perform any cleanup that | 
|  | 185 | // must take place when a buffer is released back to the BufferQueue.  If | 
|  | 186 | // it is overridden the derived class's implementation must call | 
| Brian Anderson | 3546a3f | 2016-07-14 11:51:14 -0700 | [diff] [blame] | 187 | // ConsumerBase::releaseBufferLocked. | 
| Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 188 | virtual status_t releaseBufferLocked(int slot, | 
|  | 189 | const sp<GraphicBuffer> graphicBuffer, | 
|  | 190 | EGLDisplay display, EGLSyncKHR eglFence); | 
|  | 191 |  | 
|  | 192 | // returns true iff the slot still has the graphicBuffer in it. | 
|  | 193 | bool stillTracking(int slot, const sp<GraphicBuffer> graphicBuffer); | 
| Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 194 |  | 
| Jesse Hall | 9504eb9 | 2012-10-05 14:34:21 -0700 | [diff] [blame] | 195 | // addReleaseFence* adds the sync points associated with a fence to the set | 
| Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 196 | // of sync points that must be reached before the buffer in the given slot | 
|  | 197 | // may be used after the slot has been released.  This should be called by | 
|  | 198 | // derived classes each time some asynchronous work is kicked off that | 
|  | 199 | // references the buffer. | 
| Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 200 | status_t addReleaseFence(int slot, | 
|  | 201 | const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence); | 
|  | 202 | status_t addReleaseFenceLocked(int slot, | 
|  | 203 | const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence); | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 204 |  | 
|  | 205 | // Slot contains the information and object references that | 
|  | 206 | // ConsumerBase maintains about a BufferQueue buffer slot. | 
|  | 207 | struct Slot { | 
|  | 208 | // mGraphicBuffer is the Gralloc buffer store in the slot or NULL if | 
|  | 209 | // no Gralloc buffer is in the slot. | 
|  | 210 | sp<GraphicBuffer> mGraphicBuffer; | 
|  | 211 |  | 
|  | 212 | // mFence is a fence which will signal when the buffer associated with | 
|  | 213 | // this buffer slot is no longer being used by the consumer and can be | 
|  | 214 | // overwritten. The buffer can be dequeued before the fence signals; | 
|  | 215 | // the producer is responsible for delaying writes until it signals. | 
|  | 216 | sp<Fence> mFence; | 
| Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 217 |  | 
|  | 218 | // the frame number of the last acquired frame for this slot | 
|  | 219 | uint64_t mFrameNumber; | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 220 | }; | 
|  | 221 |  | 
|  | 222 | // mSlots stores the buffers that have been allocated by the BufferQueue | 
|  | 223 | // for each buffer slot.  It is initialized to null pointers, and gets | 
|  | 224 | // filled in with the result of BufferQueue::acquire when the | 
|  | 225 | // client dequeues a buffer from a | 
|  | 226 | // slot that has not yet been used. The buffer allocated to a slot will also | 
|  | 227 | // be replaced if the requested buffer usage or geometry differs from that | 
|  | 228 | // of the buffer allocated to a slot. | 
| Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 229 | Slot mSlots[BufferQueueDefs::NUM_BUFFER_SLOTS]; | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 230 |  | 
|  | 231 | // mAbandoned indicates that the BufferQueue will no longer be used to | 
| Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 232 | // consume images buffers pushed to it using the IGraphicBufferProducer | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 233 | // interface. It is initialized to false, and set to true in the abandon | 
|  | 234 | // method.  A BufferQueue that has been abandoned will return the NO_INIT | 
|  | 235 | // error from all IConsumerBase methods capable of returning an error. | 
|  | 236 | bool mAbandoned; | 
|  | 237 |  | 
|  | 238 | // mName is a string used to identify the ConsumerBase in log messages. | 
|  | 239 | // It can be set by the setName method. | 
|  | 240 | String8 mName; | 
|  | 241 |  | 
|  | 242 | // mFrameAvailableListener is the listener object that will be called when a | 
|  | 243 | // new frame becomes available. If it is not NULL it will be called from | 
|  | 244 | // queueBuffer. | 
| Igor Murashkin | a4a3149 | 2012-10-29 13:36:11 -0700 | [diff] [blame] | 245 | wp<FrameAvailableListener> mFrameAvailableListener; | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 246 |  | 
|  | 247 | // The ConsumerBase has-a BufferQueue and is responsible for creating this object | 
|  | 248 | // if none is supplied | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 249 | sp<IGraphicBufferConsumer> mConsumer; | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 250 |  | 
| Brian Anderson | 3546a3f | 2016-07-14 11:51:14 -0700 | [diff] [blame] | 251 | // The final release fence of the most recent buffer released by | 
|  | 252 | // releaseBufferLocked. | 
|  | 253 | sp<Fence> mPrevFinalReleaseFence; | 
|  | 254 |  | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 255 | // mMutex is the mutex used to prevent concurrent access to the member | 
|  | 256 | // variables of ConsumerBase objects. It must be locked whenever the | 
| Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 257 | // member variables are accessed or when any of the *Locked methods are | 
|  | 258 | // called. | 
|  | 259 | // | 
|  | 260 | // This mutex is intended to be locked by derived classes. | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 261 | mutable Mutex mMutex; | 
| Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 262 | }; | 
|  | 263 |  | 
|  | 264 | // ---------------------------------------------------------------------------- | 
|  | 265 | }; // namespace android | 
|  | 266 |  | 
|  | 267 | #endif // ANDROID_GUI_CONSUMERBASE_H |