blob: 1b63552fc1aec58d4fcb6562c94364cb047e5fc2 [file] [log] [blame]
Jamie Gennis1a4d8832012-08-02 20:11:05 -07001/*
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>
Mathias Agopiana4e19522013-07-31 20:09:53 -070027#include <gui/IConsumerListener.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070028
Pablo Ceballos22b57022016-02-19 17:41:54 -080029#include <queue>
30
Jamie Gennis1a4d8832012-08-02 20:11:05 -070031namespace android {
32// ----------------------------------------------------------------------------
33
34class String8;
35
36// ConsumerBase is a base class for BufferQueue consumer end-points. It
37// handles common tasks like management of the connection to the BufferQueue
38// and the buffer pool.
39class ConsumerBase : public virtual RefBase,
Mathias Agopiana4e19522013-07-31 20:09:53 -070040 protected ConsumerListener {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041public:
42 struct FrameAvailableListener : public virtual RefBase {
Dan Stozadc13c5b2015-05-11 15:33:01 -070043 // See IConsumerListener::onFrame{Available,Replaced}
Dan Stoza8dc55392014-11-04 11:37:46 -080044 virtual void onFrameAvailable(const BufferItem& item) = 0;
Dan Stozadc13c5b2015-05-11 15:33:01 -070045 virtual void onFrameReplaced(const BufferItem& /* item */) {}
Jamie Gennis1a4d8832012-08-02 20:11:05 -070046 };
47
48 virtual ~ConsumerBase();
49
50 // abandon frees all the buffers and puts the ConsumerBase into the
51 // 'abandoned' state. Once put in this state the ConsumerBase can never
52 // leave it. When in the 'abandoned' state, all methods of the
Andy McFadden2adaf042012-12-18 09:49:45 -080053 // IGraphicBufferProducer interface will fail with the NO_INIT error.
Jamie Gennis1a4d8832012-08-02 20:11:05 -070054 //
55 // Note that while calling this method causes all the buffers to be freed
56 // from the perspective of the the ConsumerBase, if there are additional
57 // references on the buffers (e.g. if a buffer is referenced by a client
58 // or by OpenGL ES as a texture) then those buffer will remain allocated.
59 void abandon();
60
John Recke4783052015-05-14 15:55:11 -070061 // Returns true if the ConsumerBase is in the 'abandoned' state
62 bool isAbandoned();
63
Jamie Gennis1a4d8832012-08-02 20:11:05 -070064 // set the name of the ConsumerBase that will be used to identify it in
65 // log messages.
66 void setName(const String8& name);
67
Jesse Hall7adb0f82013-03-06 16:13:49 -080068 // dump writes the current state to a string. Child classes should add
69 // their state to the dump by overriding the dumpLocked method, which is
70 // called by these methods after locking the mutex.
Jamie Gennis1a4d8832012-08-02 20:11:05 -070071 void dump(String8& result) const;
Mathias Agopian74d211a2013-04-22 16:55:35 +020072 void dump(String8& result, const char* prefix) const;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070073
74 // setFrameAvailableListener sets the listener object that will be notified
75 // when a new frame becomes available.
Igor Murashkina4a31492012-10-29 13:36:11 -070076 void setFrameAvailableListener(const wp<FrameAvailableListener>& listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070077
Dan Stoza634f5ee2015-04-03 14:22:05 -070078 // See IGraphicBufferConsumer::detachBuffer
79 status_t detachBuffer(int slot);
80
Michael Lentine847f11e2015-05-18 13:41:23 -070081 // See IGraphicBufferConsumer::setDefaultBufferSize
82 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
83
84 // See IGraphicBufferConsumer::setDefaultBufferFormat
85 status_t setDefaultBufferFormat(PixelFormat defaultFormat);
86
87 // See IGraphicBufferConsumer::setDefaultBufferDataSpace
88 status_t setDefaultBufferDataSpace(android_dataspace defaultDataSpace);
89
Jamie Gennis1a4d8832012-08-02 20:11:05 -070090private:
91 ConsumerBase(const ConsumerBase&);
92 void operator=(const ConsumerBase&);
93
94protected:
Jamie Gennis9fea3422012-08-07 18:03:04 -070095 // ConsumerBase constructs a new ConsumerBase object to consume image
Mathias Agopiandb89edc2013-08-02 01:40:18 -070096 // buffers from the given IGraphicBufferConsumer.
Mathias Agopian595264f2013-07-16 22:56:09 -070097 // The controlledByApp flag indicates that this consumer is under the application's
98 // control.
Mathias Agopiandb89edc2013-08-02 01:40:18 -070099 ConsumerBase(const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp = false);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700100
Jamie Gennisad669b02013-04-05 16:41:27 -0700101 // onLastStrongRef gets called by RefBase just before the dtor of the most
102 // derived class. It is used to clean up the buffers so that ConsumerBase
103 // can coordinate the clean-up by calling into virtual methods implemented
104 // by the derived classes. This would not be possible from the
105 // ConsuemrBase dtor because by the time that gets called the derived
106 // classes have already been destructed.
107 //
108 // This methods should not need to be overridden by derived classes, but
109 // if they are overridden the ConsumerBase implementation must be called
110 // from the derived class.
111 virtual void onLastStrongRef(const void* id);
112
Pablo Ceballos22b57022016-02-19 17:41:54 -0800113 // Handlers for the IConsumerListener interface, these will be called from
114 // the message queue thread. These calls are used to notify the ConsumerBase
115 // of asynchronous events in the BufferQueue. The onFrameAvailableHandler,
116 // onFrameReplacedHandler, and onBuffersReleasedHandler methods should not
117 // need to be overridden by derived classes, but if they are overridden the
118 // ConsumerBase implementation must be called from the derived class. The
119 // ConsumerBase version of onSidebandStreamChangedHandler does nothing and
120 // can be overriden by derived classes if they want the notification.
121 virtual void onFrameAvailableHandler(const BufferItem& item);
122 virtual void onFrameReplacedHandler(const BufferItem& item);
123 virtual void onBuffersReleasedHandler();
124 virtual void onSidebandStreamChangedHandler();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700125
126 // freeBufferLocked frees up the given buffer slot. If the slot has been
127 // initialized this will release the reference to the GraphicBuffer in that
Jamie Gennis9fea3422012-08-07 18:03:04 -0700128 // slot. Otherwise it has no effect.
129 //
130 // Derived classes should override this method to clean up any state they
131 // keep per slot. If it is overridden, the derived class's implementation
132 // must call ConsumerBase::freeBufferLocked.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700133 //
134 // This method must be called with mMutex locked.
135 virtual void freeBufferLocked(int slotIndex);
136
137 // abandonLocked puts the BufferQueue into the abandoned state, causing
138 // all future operations on it to fail. This method rather than the public
139 // abandon method should be overridden by child classes to add abandon-
140 // time behavior.
141 //
Jamie Gennis9fea3422012-08-07 18:03:04 -0700142 // Derived classes should override this method to clean up any object
143 // state they keep (as opposed to per-slot state). If it is overridden,
144 // the derived class's implementation must call ConsumerBase::abandonLocked.
145 //
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700146 // This method must be called with mMutex locked.
147 virtual void abandonLocked();
148
Jamie Gennis9fea3422012-08-07 18:03:04 -0700149 // dumpLocked dumps the current state of the ConsumerBase object to the
150 // result string. Each line is prefixed with the string pointed to by the
151 // prefix argument. The buffer argument points to a buffer that may be
152 // used for intermediate formatting data, and the size of that buffer is
153 // indicated by the size argument.
154 //
155 // Derived classes should override this method to dump their internal
156 // state. If this method is overridden the derived class's implementation
157 // should call ConsumerBase::dumpLocked.
158 //
159 // This method must be called with mMutex locked.
Mathias Agopian74d211a2013-04-22 16:55:35 +0200160 virtual void dumpLocked(String8& result, const char* prefix) const;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700161
162 // acquireBufferLocked fetches the next buffer from the BufferQueue and
163 // updates the buffer slot for the buffer returned.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700164 //
165 // Derived classes should override this method to perform any
166 // initialization that must take place the first time a buffer is assigned
167 // to a slot. If it is overridden the derived class's implementation must
168 // call ConsumerBase::acquireBufferLocked.
Dan Stozaa4650a52015-05-12 12:56:16 -0700169 virtual status_t acquireBufferLocked(BufferItem *item, nsecs_t presentWhen,
170 uint64_t maxFrameNumber = 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700171
172 // releaseBufferLocked relinquishes control over a buffer, returning that
173 // control to the BufferQueue.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700174 //
175 // Derived classes should override this method to perform any cleanup that
176 // must take place when a buffer is released back to the BufferQueue. If
177 // it is overridden the derived class's implementation must call
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700178 // ConsumerBase::releaseBufferLocked.e
179 virtual status_t releaseBufferLocked(int slot,
180 const sp<GraphicBuffer> graphicBuffer,
181 EGLDisplay display, EGLSyncKHR eglFence);
182
183 // returns true iff the slot still has the graphicBuffer in it.
184 bool stillTracking(int slot, const sp<GraphicBuffer> graphicBuffer);
Jamie Gennisb2725412012-09-05 20:09:05 -0700185
Jesse Hall9504eb92012-10-05 14:34:21 -0700186 // addReleaseFence* adds the sync points associated with a fence to the set
Jamie Gennisb2725412012-09-05 20:09:05 -0700187 // of sync points that must be reached before the buffer in the given slot
188 // may be used after the slot has been released. This should be called by
189 // derived classes each time some asynchronous work is kicked off that
190 // references the buffer.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700191 status_t addReleaseFence(int slot,
192 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence);
193 status_t addReleaseFenceLocked(int slot,
194 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700195
196 // Slot contains the information and object references that
197 // ConsumerBase maintains about a BufferQueue buffer slot.
198 struct Slot {
199 // mGraphicBuffer is the Gralloc buffer store in the slot or NULL if
200 // no Gralloc buffer is in the slot.
201 sp<GraphicBuffer> mGraphicBuffer;
202
203 // mFence is a fence which will signal when the buffer associated with
204 // this buffer slot is no longer being used by the consumer and can be
205 // overwritten. The buffer can be dequeued before the fence signals;
206 // the producer is responsible for delaying writes until it signals.
207 sp<Fence> mFence;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700208
209 // the frame number of the last acquired frame for this slot
210 uint64_t mFrameNumber;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700211 };
212
213 // mSlots stores the buffers that have been allocated by the BufferQueue
214 // for each buffer slot. It is initialized to null pointers, and gets
215 // filled in with the result of BufferQueue::acquire when the
216 // client dequeues a buffer from a
217 // slot that has not yet been used. The buffer allocated to a slot will also
218 // be replaced if the requested buffer usage or geometry differs from that
219 // of the buffer allocated to a slot.
220 Slot mSlots[BufferQueue::NUM_BUFFER_SLOTS];
221
222 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden2adaf042012-12-18 09:49:45 -0800223 // consume images buffers pushed to it using the IGraphicBufferProducer
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700224 // interface. It is initialized to false, and set to true in the abandon
225 // method. A BufferQueue that has been abandoned will return the NO_INIT
226 // error from all IConsumerBase methods capable of returning an error.
227 bool mAbandoned;
228
229 // mName is a string used to identify the ConsumerBase in log messages.
230 // It can be set by the setName method.
231 String8 mName;
232
233 // mFrameAvailableListener is the listener object that will be called when a
234 // new frame becomes available. If it is not NULL it will be called from
235 // queueBuffer.
Igor Murashkina4a31492012-10-29 13:36:11 -0700236 wp<FrameAvailableListener> mFrameAvailableListener;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700237
238 // The ConsumerBase has-a BufferQueue and is responsible for creating this object
239 // if none is supplied
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700240 sp<IGraphicBufferConsumer> mConsumer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700241
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700242 // mMutex is the mutex used to prevent concurrent access to the member
243 // variables of ConsumerBase objects. It must be locked whenever the
Jamie Gennis9fea3422012-08-07 18:03:04 -0700244 // member variables are accessed or when any of the *Locked methods are
245 // called.
246 //
247 // This mutex is intended to be locked by derived classes.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700248 mutable Mutex mMutex;
Pablo Ceballos22b57022016-02-19 17:41:54 -0800249
250 // Implements the ConsumerListener interface
251 virtual void onFrameAvailable(const BufferItem& item) override;
252 virtual void onFrameReplaced(const BufferItem& item) override;
253 virtual void onBuffersReleased() override;
254 virtual void onSidebandStreamChanged() override;
255
256 enum MessageType {
257 ON_FRAME_AVAILABLE,
258 ON_FRAME_REPLACED,
259 ON_BUFFERS_RELEASED,
260 ON_SIDEBAND_STREAM_CHANGED,
261 EXIT,
262 };
263
264 mutable Mutex mMessageQueueLock;
265 Condition mMessageAvailable;
266 std::queue<std::pair<MessageType, BufferItem>> mMessageQueue;
267
268 class MessageThread : public Thread {
269 public:
270 MessageThread(ConsumerBase* consumerBase) :
271 mConsumerBase(consumerBase) {};
272 protected:
273 virtual bool threadLoop() override;
274 ConsumerBase* mConsumerBase;
275 };
276
277 sp<MessageThread> mMessageThread;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700278};
279
280// ----------------------------------------------------------------------------
281}; // namespace android
282
283#endif // ANDROID_GUI_CONSUMERBASE_H