blob: 5be3ffb8d32a74ce063387ab91100729c47a0706 [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
29namespace android {
30// ----------------------------------------------------------------------------
31
32class 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.
37class ConsumerBase : public virtual RefBase,
Mathias Agopiana4e19522013-07-31 20:09:53 -070038 protected ConsumerListener {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039public:
40 struct FrameAvailableListener : public virtual RefBase {
Dan Stozadc13c5b2015-05-11 15:33:01 -070041 // See IConsumerListener::onFrame{Available,Replaced}
Dan Stoza8dc55392014-11-04 11:37:46 -080042 virtual void onFrameAvailable(const BufferItem& item) = 0;
Dan Stozadc13c5b2015-05-11 15:33:01 -070043 virtual void onFrameReplaced(const BufferItem& /* item */) {}
Jamie Gennis1a4d8832012-08-02 20:11:05 -070044 };
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
Andy McFadden2adaf042012-12-18 09:49:45 -080051 // IGraphicBufferProducer interface will fail with the NO_INIT error.
Jamie Gennis1a4d8832012-08-02 20:11:05 -070052 //
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 // set the name of the ConsumerBase that will be used to identify it in
60 // log messages.
61 void setName(const String8& name);
62
Jesse Hall7adb0f82013-03-06 16:13:49 -080063 // dump writes the current state to a string. Child classes should add
64 // their state to the dump by overriding the dumpLocked method, which is
65 // called by these methods after locking the mutex.
Jamie Gennis1a4d8832012-08-02 20:11:05 -070066 void dump(String8& result) const;
Mathias Agopian74d211a2013-04-22 16:55:35 +020067 void dump(String8& result, const char* prefix) const;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070068
69 // setFrameAvailableListener sets the listener object that will be notified
70 // when a new frame becomes available.
Igor Murashkina4a31492012-10-29 13:36:11 -070071 void setFrameAvailableListener(const wp<FrameAvailableListener>& listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070072
Dan Stoza634f5ee2015-04-03 14:22:05 -070073 // See IGraphicBufferConsumer::detachBuffer
74 status_t detachBuffer(int slot);
75
Jamie Gennis1a4d8832012-08-02 20:11:05 -070076private:
77 ConsumerBase(const ConsumerBase&);
78 void operator=(const ConsumerBase&);
79
80protected:
Jamie Gennis9fea3422012-08-07 18:03:04 -070081 // ConsumerBase constructs a new ConsumerBase object to consume image
Mathias Agopiandb89edc2013-08-02 01:40:18 -070082 // buffers from the given IGraphicBufferConsumer.
Mathias Agopian595264f2013-07-16 22:56:09 -070083 // The controlledByApp flag indicates that this consumer is under the application's
84 // control.
Mathias Agopiandb89edc2013-08-02 01:40:18 -070085 ConsumerBase(const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp = false);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070086
Jamie Gennisad669b02013-04-05 16:41:27 -070087 // onLastStrongRef gets called by RefBase just before the dtor of the most
88 // derived class. It is used to clean up the buffers so that ConsumerBase
89 // can coordinate the clean-up by calling into virtual methods implemented
90 // by the derived classes. This would not be possible from the
91 // ConsuemrBase dtor because by the time that gets called the derived
92 // classes have already been destructed.
93 //
94 // This methods should not need to be overridden by derived classes, but
95 // if they are overridden the ConsumerBase implementation must be called
96 // from the derived class.
97 virtual void onLastStrongRef(const void* id);
98
Mathias Agopiandb89edc2013-08-02 01:40:18 -070099 // Implementation of the IConsumerListener interface. These
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700100 // calls are used to notify the ConsumerBase of asynchronous events in the
Dan Stozadc13c5b2015-05-11 15:33:01 -0700101 // BufferQueue. The onFrameAvailable, onFrameReplaced, and
102 // onBuffersReleased methods should not need to be overridden by derived
103 // classes, but if they are overridden the ConsumerBase implementation must
104 // be called from the derived class. The ConsumerBase version of
105 // onSidebandStreamChanged does nothing and can be overriden by derived
106 // classes if they want the notification.
107 virtual void onFrameAvailable(const BufferItem& item) override;
108 virtual void onFrameReplaced(const BufferItem& item) override;
109 virtual void onBuffersReleased() override;
110 virtual void onSidebandStreamChanged() override;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700111
112 // freeBufferLocked frees up the given buffer slot. If the slot has been
113 // initialized this will release the reference to the GraphicBuffer in that
Jamie Gennis9fea3422012-08-07 18:03:04 -0700114 // slot. Otherwise it has no effect.
115 //
116 // Derived classes should override this method to clean up any state they
117 // keep per slot. If it is overridden, the derived class's implementation
118 // must call ConsumerBase::freeBufferLocked.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700119 //
120 // This method must be called with mMutex locked.
121 virtual void freeBufferLocked(int slotIndex);
122
123 // abandonLocked puts the BufferQueue into the abandoned state, causing
124 // all future operations on it to fail. This method rather than the public
125 // abandon method should be overridden by child classes to add abandon-
126 // time behavior.
127 //
Jamie Gennis9fea3422012-08-07 18:03:04 -0700128 // Derived classes should override this method to clean up any object
129 // state they keep (as opposed to per-slot state). If it is overridden,
130 // the derived class's implementation must call ConsumerBase::abandonLocked.
131 //
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700132 // This method must be called with mMutex locked.
133 virtual void abandonLocked();
134
Jamie Gennis9fea3422012-08-07 18:03:04 -0700135 // dumpLocked dumps the current state of the ConsumerBase object to the
136 // result string. Each line is prefixed with the string pointed to by the
137 // prefix argument. The buffer argument points to a buffer that may be
138 // used for intermediate formatting data, and the size of that buffer is
139 // indicated by the size argument.
140 //
141 // Derived classes should override this method to dump their internal
142 // state. If this method is overridden the derived class's implementation
143 // should call ConsumerBase::dumpLocked.
144 //
145 // This method must be called with mMutex locked.
Mathias Agopian74d211a2013-04-22 16:55:35 +0200146 virtual void dumpLocked(String8& result, const char* prefix) const;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700147
148 // acquireBufferLocked fetches the next buffer from the BufferQueue and
149 // updates the buffer slot for the buffer returned.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700150 //
151 // Derived classes should override this method to perform any
152 // initialization that must take place the first time a buffer is assigned
153 // to a slot. If it is overridden the derived class's implementation must
154 // call ConsumerBase::acquireBufferLocked.
Dan Stozacf3834d2015-03-11 14:04:22 -0700155 virtual status_t acquireBufferLocked(BufferItem *item, nsecs_t presentWhen);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700156
157 // releaseBufferLocked relinquishes control over a buffer, returning that
158 // control to the BufferQueue.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700159 //
160 // Derived classes should override this method to perform any cleanup that
161 // must take place when a buffer is released back to the BufferQueue. If
162 // it is overridden the derived class's implementation must call
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700163 // ConsumerBase::releaseBufferLocked.e
164 virtual status_t releaseBufferLocked(int slot,
165 const sp<GraphicBuffer> graphicBuffer,
166 EGLDisplay display, EGLSyncKHR eglFence);
167
168 // returns true iff the slot still has the graphicBuffer in it.
169 bool stillTracking(int slot, const sp<GraphicBuffer> graphicBuffer);
Jamie Gennisb2725412012-09-05 20:09:05 -0700170
Jesse Hall9504eb92012-10-05 14:34:21 -0700171 // addReleaseFence* adds the sync points associated with a fence to the set
Jamie Gennisb2725412012-09-05 20:09:05 -0700172 // of sync points that must be reached before the buffer in the given slot
173 // may be used after the slot has been released. This should be called by
174 // derived classes each time some asynchronous work is kicked off that
175 // references the buffer.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700176 status_t addReleaseFence(int slot,
177 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence);
178 status_t addReleaseFenceLocked(int slot,
179 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700180
181 // Slot contains the information and object references that
182 // ConsumerBase maintains about a BufferQueue buffer slot.
183 struct Slot {
184 // mGraphicBuffer is the Gralloc buffer store in the slot or NULL if
185 // no Gralloc buffer is in the slot.
186 sp<GraphicBuffer> mGraphicBuffer;
187
188 // mFence is a fence which will signal when the buffer associated with
189 // this buffer slot is no longer being used by the consumer and can be
190 // overwritten. The buffer can be dequeued before the fence signals;
191 // the producer is responsible for delaying writes until it signals.
192 sp<Fence> mFence;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700193
194 // the frame number of the last acquired frame for this slot
195 uint64_t mFrameNumber;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700196 };
197
198 // mSlots stores the buffers that have been allocated by the BufferQueue
199 // for each buffer slot. It is initialized to null pointers, and gets
200 // filled in with the result of BufferQueue::acquire when the
201 // client dequeues a buffer from a
202 // slot that has not yet been used. The buffer allocated to a slot will also
203 // be replaced if the requested buffer usage or geometry differs from that
204 // of the buffer allocated to a slot.
205 Slot mSlots[BufferQueue::NUM_BUFFER_SLOTS];
206
207 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden2adaf042012-12-18 09:49:45 -0800208 // consume images buffers pushed to it using the IGraphicBufferProducer
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700209 // interface. It is initialized to false, and set to true in the abandon
210 // method. A BufferQueue that has been abandoned will return the NO_INIT
211 // error from all IConsumerBase methods capable of returning an error.
212 bool mAbandoned;
213
214 // mName is a string used to identify the ConsumerBase in log messages.
215 // It can be set by the setName method.
216 String8 mName;
217
218 // mFrameAvailableListener is the listener object that will be called when a
219 // new frame becomes available. If it is not NULL it will be called from
220 // queueBuffer.
Igor Murashkina4a31492012-10-29 13:36:11 -0700221 wp<FrameAvailableListener> mFrameAvailableListener;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700222
223 // The ConsumerBase has-a BufferQueue and is responsible for creating this object
224 // if none is supplied
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700225 sp<IGraphicBufferConsumer> mConsumer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700226
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700227 // mMutex is the mutex used to prevent concurrent access to the member
228 // variables of ConsumerBase objects. It must be locked whenever the
Jamie Gennis9fea3422012-08-07 18:03:04 -0700229 // member variables are accessed or when any of the *Locked methods are
230 // called.
231 //
232 // This mutex is intended to be locked by derived classes.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700233 mutable Mutex mMutex;
234};
235
236// ----------------------------------------------------------------------------
237}; // namespace android
238
239#endif // ANDROID_GUI_CONSUMERBASE_H