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