Skip to content

Commit a631399

Browse files
author
Daniel Lam
committed
Removed dependecies between BufferQueue and SurfaceTexture
Refactored SurfaceTexture and BufferQueue such that share no protected members. Created an consumer facing interface for BufferQueue in preparation of connecting SurfaceTexture and BufferQueue through a binder. Change-Id: Iff55e740e36a7f70c9f7a17ee7a5af38e3d21f0f
1 parent 8181201 commit a631399

File tree

4 files changed

+438
-168
lines changed

4 files changed

+438
-168
lines changed

include/gui/BufferQueue.h

Lines changed: 100 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class BufferQueue : public BnSurfaceTexture {
4040
};
4141
enum { NUM_BUFFER_SLOTS = 32 };
4242
enum { NO_CONNECTED_API = 0 };
43+
enum { INVALID_BUFFER_SLOT = -1 };
4344

4445
struct FrameAvailableListener : public virtual RefBase {
4546
// onFrameAvailable() is called from queueBuffer() each time an
@@ -119,8 +120,91 @@ class BufferQueue : public BnSurfaceTexture {
119120
// connected to the specified client API.
120121
virtual status_t disconnect(int api);
121122

122-
protected:
123+
// dump our state in a String
124+
virtual void dump(String8& result) const;
125+
virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
126+
127+
// public facing structure for BufferSlot
128+
struct BufferItem {
129+
130+
BufferItem()
131+
:
132+
mTransform(0),
133+
mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
134+
mTimestamp(0),
135+
mFrameNumber(0),
136+
mBuf(INVALID_BUFFER_SLOT) {
137+
mCrop.makeInvalid();
138+
}
139+
// mGraphicBuffer points to the buffer allocated for this slot or is NULL
140+
// if no buffer has been allocated.
141+
sp<GraphicBuffer> mGraphicBuffer;
142+
143+
// mCrop is the current crop rectangle for this buffer slot. This gets
144+
// set to mNextCrop each time queueBuffer gets called for this buffer.
145+
Rect mCrop;
146+
147+
// mTransform is the current transform flags for this buffer slot. This
148+
// gets set to mNextTransform each time queueBuffer gets called for this
149+
// slot.
150+
uint32_t mTransform;
151+
152+
// mScalingMode is the current scaling mode for this buffer slot. This
153+
// gets set to mNextScalingMode each time queueBuffer gets called for
154+
// this slot.
155+
uint32_t mScalingMode;
156+
157+
// mTimestamp is the current timestamp for this buffer slot. This gets
158+
// to set by queueBuffer each time this slot is queued.
159+
int64_t mTimestamp;
123160

161+
// mFrameNumber is the number of the queued frame for this slot.
162+
uint64_t mFrameNumber;
163+
164+
// buf is the slot index of this buffer
165+
int mBuf;
166+
167+
};
168+
169+
// The following public functions is the consumer facing interface
170+
171+
// acquire consumes a buffer by transferring its ownership to a consumer.
172+
// buffer contains the GraphicBuffer and its corresponding information.
173+
// buffer.mGraphicsBuffer will be NULL when the buffer has been already
174+
// acquired by the consumer.
175+
176+
status_t acquire(BufferItem *buffer);
177+
178+
// releaseBuffer releases a buffer slot from the consumer back to the
179+
// BufferQueue pending a fence sync.
180+
status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence);
181+
182+
// consumerDisconnect disconnects a consumer from the BufferQueue. All
183+
// buffers will be freed.
184+
status_t consumerDisconnect();
185+
186+
// setDefaultBufferSize is used to set the size of buffers returned by
187+
// requestBuffers when a with and height of zero is requested.
188+
status_t setDefaultBufferSize(uint32_t w, uint32_t h);
189+
190+
// setBufferCountServer set the buffer count. If the client has requested
191+
// a buffer count using setBufferCount, the server-buffer count will
192+
// take effect once the client sets the count back to zero.
193+
status_t setBufferCountServer(int bufferCount);
194+
195+
// isSynchronousMode returns whether the SurfaceTexture is currently in
196+
// synchronous mode.
197+
bool isSynchronousMode() const;
198+
199+
// setConsumerName sets the name used in logging
200+
void setConsumerName(const String8& name);
201+
202+
// setFrameAvailableListener sets the listener object that will be notified
203+
// when a new frame becomes available.
204+
void setFrameAvailableListener(const sp<FrameAvailableListener>& listener);
205+
206+
207+
private:
124208
// freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
125209
// for the given slot.
126210
void freeBufferLocked(int index);
@@ -145,30 +229,25 @@ class BufferQueue : public BnSurfaceTexture {
145229

146230
status_t setBufferCountServerLocked(int bufferCount);
147231

148-
enum { INVALID_BUFFER_SLOT = -1 };
149-
150232
struct BufferSlot {
151233

152234
BufferSlot()
153-
: mEglImage(EGL_NO_IMAGE_KHR),
154-
mEglDisplay(EGL_NO_DISPLAY),
235+
: mEglDisplay(EGL_NO_DISPLAY),
155236
mBufferState(BufferSlot::FREE),
156237
mRequestBufferCalled(false),
157238
mTransform(0),
158239
mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
159240
mTimestamp(0),
160241
mFrameNumber(0),
161-
mFence(EGL_NO_SYNC_KHR) {
242+
mFence(EGL_NO_SYNC_KHR),
243+
mAcquireCalled(false) {
162244
mCrop.makeInvalid();
163245
}
164246

165247
// mGraphicBuffer points to the buffer allocated for this slot or is NULL
166248
// if no buffer has been allocated.
167249
sp<GraphicBuffer> mGraphicBuffer;
168250

169-
// mEglImage is the EGLImage created from mGraphicBuffer.
170-
EGLImageKHR mEglImage;
171-
172251
// mEglDisplay is the EGLDisplay used to create mEglImage.
173252
EGLDisplay mEglDisplay;
174253

@@ -178,6 +257,7 @@ class BufferQueue : public BnSurfaceTexture {
178257
// FREE indicates that the buffer is not currently being used and
179258
// will not be used in the future until it gets dequeued and
180259
// subsequently queued by the client.
260+
// aka "owned by BufferQueue, ready to be dequeued"
181261
FREE = 0,
182262

183263
// DEQUEUED indicates that the buffer has been dequeued by the
@@ -190,6 +270,7 @@ class BufferQueue : public BnSurfaceTexture {
190270
// dequeued by the client. That means that the current buffer can
191271
// be in either the DEQUEUED or QUEUED state. In asynchronous mode,
192272
// however, the current buffer is always in the QUEUED state.
273+
// aka "owned by producer, ready to be queued"
193274
DEQUEUED = 1,
194275

195276
// QUEUED indicates that the buffer has been queued by the client,
@@ -199,7 +280,11 @@ class BufferQueue : public BnSurfaceTexture {
199280
// the current buffer may be dequeued by the client under some
200281
// circumstances. See the note about the current buffer in the
201282
// documentation for DEQUEUED.
283+
// aka "owned by BufferQueue, ready to be acquired"
202284
QUEUED = 2,
285+
286+
// aka "owned by consumer, ready to be released"
287+
ACQUIRED = 3
203288
};
204289

205290
// mBufferState is the current state of this buffer slot.
@@ -236,6 +321,9 @@ class BufferQueue : public BnSurfaceTexture {
236321
// to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
237322
// on a compile-time option) set to a new sync object in updateTexImage.
238323
EGLSyncKHR mFence;
324+
325+
// Indicates whether this buffer has been seen by a consumer yet
326+
bool mAcquireCalled;
239327
};
240328

241329
// mSlots is the array of buffer slots that must be mirrored on the client
@@ -245,7 +333,6 @@ class BufferQueue : public BnSurfaceTexture {
245333
// for a slot when requestBuffer is called with that slot's index.
246334
BufferSlot mSlots[NUM_BUFFER_SLOTS];
247335

248-
249336
// mDefaultWidth holds the default width of allocated buffers. It is used
250337
// in requestBuffers() if a width and height of zero is specified.
251338
uint32_t mDefaultWidth;
@@ -271,14 +358,6 @@ class BufferQueue : public BnSurfaceTexture {
271358
// mServerBufferCount buffer count requested by the server-side
272359
int mServerBufferCount;
273360

274-
// mCurrentTexture is the buffer slot index of the buffer that is currently
275-
// bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
276-
// indicating that no buffer slot is currently bound to the texture. Note,
277-
// however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
278-
// that no buffer is bound to the texture. A call to setBufferCount will
279-
// reset mCurrentTexture to INVALID_BUFFER_SLOT.
280-
int mCurrentTexture;
281-
282361
// mNextCrop is the crop rectangle that will be used for the next buffer
283362
// that gets queued. It is set by calling setCrop.
284363
Rect mNextCrop;
@@ -327,7 +406,7 @@ class BufferQueue : public BnSurfaceTexture {
327406

328407
// mName is a string used to identify the BufferQueue in log messages.
329408
// It is set by the setName method.
330-
String8 mName;
409+
String8 mConsumerName;
331410

332411
// mMutex is the mutex used to prevent concurrent access to the member
333412
// variables of BufferQueue objects. It must be locked whenever the
@@ -337,6 +416,8 @@ class BufferQueue : public BnSurfaceTexture {
337416
// mFrameCounter is the free running counter, incremented for every buffer queued
338417
// with the surface Texture.
339418
uint64_t mFrameCounter;
419+
420+
bool mBufferHasBeenQueued;
340421
};
341422

342423
// ----------------------------------------------------------------------------

include/gui/SurfaceTexture.h

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ class SurfaceTexture : public BufferQueue {
153153
void setName(const String8& name);
154154

155155
// dump our state in a String
156-
void dump(String8& result) const;
157-
void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
156+
virtual void dump(String8& result) const;
157+
virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
158158

159159
protected:
160160

@@ -217,6 +217,56 @@ class SurfaceTexture : public BufferQueue {
217217
// browser's tile cache exceeds.
218218
const GLenum mTexTarget;
219219

220+
// SurfaceTexture maintains EGL information about GraphicBuffers that corresponds
221+
// directly with BufferQueue's buffers
222+
struct EGLSlot {
223+
EGLSlot()
224+
: mEglImage(EGL_NO_IMAGE_KHR),
225+
mEglDisplay(EGL_NO_DISPLAY),
226+
mFence(EGL_NO_SYNC_KHR) {
227+
}
228+
229+
sp<GraphicBuffer> mGraphicBuffer;
230+
231+
// mEglImage is the EGLImage created from mGraphicBuffer.
232+
EGLImageKHR mEglImage;
233+
234+
// mEglDisplay is the EGLDisplay used to create mEglImage.
235+
EGLDisplay mEglDisplay;
236+
237+
// mFence is the EGL sync object that must signal before the buffer
238+
// associated with this buffer slot may be dequeued. It is initialized
239+
// to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
240+
// on a compile-time option) set to a new sync object in updateTexImage.
241+
EGLSyncKHR mFence;
242+
};
243+
244+
EGLSlot mEGLSlots[NUM_BUFFER_SLOTS];
245+
246+
// mAbandoned indicates that the BufferQueue will no longer be used to
247+
// consume images buffers pushed to it using the ISurfaceTexture interface.
248+
// It is initialized to false, and set to true in the abandon method. A
249+
// BufferQueue that has been abandoned will return the NO_INIT error from
250+
// all ISurfaceTexture methods capable of returning an error.
251+
bool mAbandoned;
252+
253+
// mName is a string used to identify the SurfaceTexture in log messages.
254+
// It can be set by the setName method.
255+
String8 mName;
256+
257+
// mMutex is the mutex used to prevent concurrent access to the member
258+
// variables of SurfaceTexture objects. It must be locked whenever the
259+
// member variables are accessed.
260+
mutable Mutex mMutex;
261+
262+
// mCurrentTexture is the buffer slot index of the buffer that is currently
263+
// bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
264+
// indicating that no buffer slot is currently bound to the texture. Note,
265+
// however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
266+
// that no buffer is bound to the texture. A call to setBufferCount will
267+
// reset mCurrentTexture to INVALID_BUFFER_SLOT.
268+
int mCurrentTexture;
269+
220270
};
221271

222272
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)