ConsumerBase.h revision a4a3149a36bc69a06e4824aeae909ab910661070
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
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
56    // ISurfaceTexture interface will fail with the NO_INIT error.
57    //
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
72    // dump writes the current state to a string.  These methods should NOT be
73    // overridden by child classes.  Instead they should override the
74    // dumpLocked method, which is called by these methods after locking the
75    // mutex.
76    void dump(String8& result) const;
77    void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
78
79    // setFrameAvailableListener sets the listener object that will be notified
80    // when a new frame becomes available.
81    void setFrameAvailableListener(const wp<FrameAvailableListener>& listener);
82
83private:
84    ConsumerBase(const ConsumerBase&);
85    void operator=(const ConsumerBase&);
86
87protected:
88
89    // ConsumerBase constructs a new ConsumerBase object to consume image
90    // buffers from the given BufferQueue.
91    ConsumerBase(const sp<BufferQueue> &bufferQueue);
92
93    // Implementation of the BufferQueue::ConsumerListener interface.  These
94    // calls are used to notify the ConsumerBase of asynchronous events in the
95    // BufferQueue.  These methods should not need to be overridden by derived
96    // classes, but if they are overridden the ConsumerBase implementation
97    // must be called from the derived class.
98    virtual void onFrameAvailable();
99    virtual void onBuffersReleased();
100
101    // freeBufferLocked frees up the given buffer slot.  If the slot has been
102    // initialized this will release the reference to the GraphicBuffer in that
103    // slot.  Otherwise it has no effect.
104    //
105    // Derived classes should override this method to clean up any state they
106    // keep per slot.  If it is overridden, the derived class's implementation
107    // must call ConsumerBase::freeBufferLocked.
108    //
109    // This method must be called with mMutex locked.
110    virtual void freeBufferLocked(int slotIndex);
111
112    // abandonLocked puts the BufferQueue into the abandoned state, causing
113    // all future operations on it to fail. This method rather than the public
114    // abandon method should be overridden by child classes to add abandon-
115    // time behavior.
116    //
117    // Derived classes should override this method to clean up any object
118    // state they keep (as opposed to per-slot state).  If it is overridden,
119    // the derived class's implementation must call ConsumerBase::abandonLocked.
120    //
121    // This method must be called with mMutex locked.
122    virtual void abandonLocked();
123
124    // dumpLocked dumps the current state of the ConsumerBase object to the
125    // result string.  Each line is prefixed with the string pointed to by the
126    // prefix argument.  The buffer argument points to a buffer that may be
127    // used for intermediate formatting data, and the size of that buffer is
128    // indicated by the size argument.
129    //
130    // Derived classes should override this method to dump their internal
131    // state.  If this method is overridden the derived class's implementation
132    // should call ConsumerBase::dumpLocked.
133    //
134    // This method must be called with mMutex locked.
135    virtual void dumpLocked(String8& result, const char* prefix, char* buffer,
136            size_t size) const;
137
138    // acquireBufferLocked fetches the next buffer from the BufferQueue and
139    // updates the buffer slot for the buffer returned.
140    //
141    // Derived classes should override this method to perform any
142    // initialization that must take place the first time a buffer is assigned
143    // to a slot.  If it is overridden the derived class's implementation must
144    // call ConsumerBase::acquireBufferLocked.
145    virtual status_t acquireBufferLocked(BufferQueue::BufferItem *item);
146
147    // releaseBufferLocked relinquishes control over a buffer, returning that
148    // control to the BufferQueue.
149    //
150    // Derived classes should override this method to perform any cleanup that
151    // must take place when a buffer is released back to the BufferQueue.  If
152    // it is overridden the derived class's implementation must call
153    // ConsumerBase::releaseBufferLocked.
154    virtual status_t releaseBufferLocked(int buf, EGLDisplay display,
155           EGLSyncKHR eglFence);
156
157    // addReleaseFence* adds the sync points associated with a fence to the set
158    // of sync points that must be reached before the buffer in the given slot
159    // may be used after the slot has been released.  This should be called by
160    // derived classes each time some asynchronous work is kicked off that
161    // references the buffer.
162    status_t addReleaseFence(int slot, const sp<Fence>& fence);
163    status_t addReleaseFenceLocked(int slot, const sp<Fence>& fence);
164
165    // Slot contains the information and object references that
166    // ConsumerBase maintains about a BufferQueue buffer slot.
167    struct Slot {
168        // mGraphicBuffer is the Gralloc buffer store in the slot or NULL if
169        // no Gralloc buffer is in the slot.
170        sp<GraphicBuffer> mGraphicBuffer;
171
172        // mFence is a fence which will signal when the buffer associated with
173        // this buffer slot is no longer being used by the consumer and can be
174        // overwritten. The buffer can be dequeued before the fence signals;
175        // the producer is responsible for delaying writes until it signals.
176        sp<Fence> mFence;
177    };
178
179    // mSlots stores the buffers that have been allocated by the BufferQueue
180    // for each buffer slot.  It is initialized to null pointers, and gets
181    // filled in with the result of BufferQueue::acquire when the
182    // client dequeues a buffer from a
183    // slot that has not yet been used. The buffer allocated to a slot will also
184    // be replaced if the requested buffer usage or geometry differs from that
185    // of the buffer allocated to a slot.
186    Slot mSlots[BufferQueue::NUM_BUFFER_SLOTS];
187
188    // mAbandoned indicates that the BufferQueue will no longer be used to
189    // consume images buffers pushed to it using the ISurfaceTexture
190    // interface. It is initialized to false, and set to true in the abandon
191    // method.  A BufferQueue that has been abandoned will return the NO_INIT
192    // error from all IConsumerBase methods capable of returning an error.
193    bool mAbandoned;
194
195    // mName is a string used to identify the ConsumerBase in log messages.
196    // It can be set by the setName method.
197    String8 mName;
198
199    // mFrameAvailableListener is the listener object that will be called when a
200    // new frame becomes available. If it is not NULL it will be called from
201    // queueBuffer.
202    wp<FrameAvailableListener> mFrameAvailableListener;
203
204    // The ConsumerBase has-a BufferQueue and is responsible for creating this object
205    // if none is supplied
206    sp<BufferQueue> mBufferQueue;
207
208    // mMutex is the mutex used to prevent concurrent access to the member
209    // variables of ConsumerBase objects. It must be locked whenever the
210    // member variables are accessed or when any of the *Locked methods are
211    // called.
212    //
213    // This mutex is intended to be locked by derived classes.
214    mutable Mutex mMutex;
215};
216
217// ----------------------------------------------------------------------------
218}; // namespace android
219
220#endif // ANDROID_GUI_CONSUMERBASE_H
221