blob: 78a36089e08dd7feeedcf6ac37351d328f4cb526 [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>
27
28namespace android {
29// ----------------------------------------------------------------------------
30
31class String8;
32
33// ConsumerBase is a base class for BufferQueue consumer end-points. It
34// handles common tasks like management of the connection to the BufferQueue
35// and the buffer pool.
36class ConsumerBase : public virtual RefBase,
37 protected BufferQueue::ConsumerListener {
38public:
39 struct FrameAvailableListener : public virtual RefBase {
40 // onFrameAvailable() is called each time an additional frame becomes
41 // available for consumption. This means that frames that are queued
42 // while in asynchronous mode only trigger the callback if no previous
43 // frames are pending. Frames queued while in synchronous mode always
44 // trigger the callback.
45 //
46 // This is called without any lock held and can be called concurrently
47 // by multiple threads.
48 virtual void onFrameAvailable() = 0;
49 };
50
51 virtual ~ConsumerBase();
52
53 // abandon frees all the buffers and puts the ConsumerBase into the
54 // 'abandoned' state. Once put in this state the ConsumerBase can never
55 // leave it. When in the 'abandoned' state, all methods of the
Andy McFadden2adaf042012-12-18 09:49:45 -080056 // IGraphicBufferProducer interface will fail with the NO_INIT error.
Jamie Gennis1a4d8832012-08-02 20:11:05 -070057 //
58 // Note that while calling this method causes all the buffers to be freed
59 // from the perspective of the the ConsumerBase, if there are additional
60 // references on the buffers (e.g. if a buffer is referenced by a client
61 // or by OpenGL ES as a texture) then those buffer will remain allocated.
62 void abandon();
63
64 // set the name of the ConsumerBase that will be used to identify it in
65 // log messages.
66 void setName(const String8& name);
67
68 // getBufferQueue returns the BufferQueue object to which this
69 // ConsumerBase is connected.
70 sp<BufferQueue> getBufferQueue() const;
71
Jesse Hall7adb0f82013-03-06 16:13:49 -080072 // dump writes the current state to a string. Child classes should add
73 // their state to the dump by overriding the dumpLocked method, which is
74 // called by these methods after locking the mutex.
Jamie Gennis1a4d8832012-08-02 20:11:05 -070075 void dump(String8& result) const;
76 void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
77
78 // setFrameAvailableListener sets the listener object that will be notified
79 // when a new frame becomes available.
Igor Murashkina4a31492012-10-29 13:36:11 -070080 void setFrameAvailableListener(const wp<FrameAvailableListener>& listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070081
82private:
83 ConsumerBase(const ConsumerBase&);
84 void operator=(const ConsumerBase&);
85
86protected:
87
Jamie Gennis9fea3422012-08-07 18:03:04 -070088 // ConsumerBase constructs a new ConsumerBase object to consume image
89 // buffers from the given BufferQueue.
Jamie Gennis1a4d8832012-08-02 20:11:05 -070090 ConsumerBase(const sp<BufferQueue> &bufferQueue);
91
92 // Implementation of the BufferQueue::ConsumerListener interface. These
93 // calls are used to notify the ConsumerBase of asynchronous events in the
Jamie Gennis9fea3422012-08-07 18:03:04 -070094 // BufferQueue. These methods should not need to be overridden by derived
95 // classes, but if they are overridden the ConsumerBase implementation
96 // must be called from the derived class.
Jamie Gennis1a4d8832012-08-02 20:11:05 -070097 virtual void onFrameAvailable();
98 virtual void onBuffersReleased();
99
100 // freeBufferLocked frees up the given buffer slot. If the slot has been
101 // initialized this will release the reference to the GraphicBuffer in that
Jamie Gennis9fea3422012-08-07 18:03:04 -0700102 // slot. Otherwise it has no effect.
103 //
104 // Derived classes should override this method to clean up any state they
105 // keep per slot. If it is overridden, the derived class's implementation
106 // must call ConsumerBase::freeBufferLocked.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700107 //
108 // This method must be called with mMutex locked.
109 virtual void freeBufferLocked(int slotIndex);
110
111 // abandonLocked puts the BufferQueue into the abandoned state, causing
112 // all future operations on it to fail. This method rather than the public
113 // abandon method should be overridden by child classes to add abandon-
114 // time behavior.
115 //
Jamie Gennis9fea3422012-08-07 18:03:04 -0700116 // Derived classes should override this method to clean up any object
117 // state they keep (as opposed to per-slot state). If it is overridden,
118 // the derived class's implementation must call ConsumerBase::abandonLocked.
119 //
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700120 // This method must be called with mMutex locked.
121 virtual void abandonLocked();
122
Jamie Gennis9fea3422012-08-07 18:03:04 -0700123 // dumpLocked dumps the current state of the ConsumerBase object to the
124 // result string. Each line is prefixed with the string pointed to by the
125 // prefix argument. The buffer argument points to a buffer that may be
126 // used for intermediate formatting data, and the size of that buffer is
127 // indicated by the size argument.
128 //
129 // Derived classes should override this method to dump their internal
130 // state. If this method is overridden the derived class's implementation
131 // should call ConsumerBase::dumpLocked.
132 //
133 // This method must be called with mMutex locked.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700134 virtual void dumpLocked(String8& result, const char* prefix, char* buffer,
Jamie Gennis9fea3422012-08-07 18:03:04 -0700135 size_t size) const;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700136
137 // acquireBufferLocked fetches the next buffer from the BufferQueue and
138 // updates the buffer slot for the buffer returned.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700139 //
140 // Derived classes should override this method to perform any
141 // initialization that must take place the first time a buffer is assigned
142 // to a slot. If it is overridden the derived class's implementation must
143 // call ConsumerBase::acquireBufferLocked.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700144 virtual status_t acquireBufferLocked(BufferQueue::BufferItem *item);
145
146 // releaseBufferLocked relinquishes control over a buffer, returning that
147 // control to the BufferQueue.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700148 //
149 // Derived classes should override this method to perform any cleanup that
150 // must take place when a buffer is released back to the BufferQueue. If
151 // it is overridden the derived class's implementation must call
Jesse Hall9504eb92012-10-05 14:34:21 -0700152 // ConsumerBase::releaseBufferLocked.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700153 virtual status_t releaseBufferLocked(int buf, EGLDisplay display,
Jamie Gennisb2725412012-09-05 20:09:05 -0700154 EGLSyncKHR eglFence);
155
Jesse Hall9504eb92012-10-05 14:34:21 -0700156 // addReleaseFence* adds the sync points associated with a fence to the set
Jamie Gennisb2725412012-09-05 20:09:05 -0700157 // of sync points that must be reached before the buffer in the given slot
158 // may be used after the slot has been released. This should be called by
159 // derived classes each time some asynchronous work is kicked off that
160 // references the buffer.
161 status_t addReleaseFence(int slot, const sp<Fence>& fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700162 status_t addReleaseFenceLocked(int slot, const sp<Fence>& fence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700163
164 // Slot contains the information and object references that
165 // ConsumerBase maintains about a BufferQueue buffer slot.
166 struct Slot {
167 // mGraphicBuffer is the Gralloc buffer store in the slot or NULL if
168 // no Gralloc buffer is in the slot.
169 sp<GraphicBuffer> mGraphicBuffer;
170
171 // mFence is a fence which will signal when the buffer associated with
172 // this buffer slot is no longer being used by the consumer and can be
173 // overwritten. The buffer can be dequeued before the fence signals;
174 // the producer is responsible for delaying writes until it signals.
175 sp<Fence> mFence;
176 };
177
178 // mSlots stores the buffers that have been allocated by the BufferQueue
179 // for each buffer slot. It is initialized to null pointers, and gets
180 // filled in with the result of BufferQueue::acquire when the
181 // client dequeues a buffer from a
182 // slot that has not yet been used. The buffer allocated to a slot will also
183 // be replaced if the requested buffer usage or geometry differs from that
184 // of the buffer allocated to a slot.
185 Slot mSlots[BufferQueue::NUM_BUFFER_SLOTS];
186
187 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden2adaf042012-12-18 09:49:45 -0800188 // consume images buffers pushed to it using the IGraphicBufferProducer
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700189 // interface. It is initialized to false, and set to true in the abandon
190 // method. A BufferQueue that has been abandoned will return the NO_INIT
191 // error from all IConsumerBase methods capable of returning an error.
192 bool mAbandoned;
193
194 // mName is a string used to identify the ConsumerBase in log messages.
195 // It can be set by the setName method.
196 String8 mName;
197
198 // mFrameAvailableListener is the listener object that will be called when a
199 // new frame becomes available. If it is not NULL it will be called from
200 // queueBuffer.
Igor Murashkina4a31492012-10-29 13:36:11 -0700201 wp<FrameAvailableListener> mFrameAvailableListener;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700202
203 // The ConsumerBase has-a BufferQueue and is responsible for creating this object
204 // if none is supplied
205 sp<BufferQueue> mBufferQueue;
206
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700207 // mMutex is the mutex used to prevent concurrent access to the member
208 // variables of ConsumerBase objects. It must be locked whenever the
Jamie Gennis9fea3422012-08-07 18:03:04 -0700209 // member variables are accessed or when any of the *Locked methods are
210 // called.
211 //
212 // This mutex is intended to be locked by derived classes.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700213 mutable Mutex mMutex;
214};
215
216// ----------------------------------------------------------------------------
217}; // namespace android
218
219#endif // ANDROID_GUI_CONSUMERBASE_H