blob: 6a7809ab05e7c9ef10c64772a6d3e1cd7d8dd55a [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
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_SURFACETEXTURE_H
18#define ANDROID_GUI_SURFACETEXTURE_H
19
20#include <EGL/egl.h>
21#include <EGL/eglext.h>
22#include <GLES2/gl2.h>
23
24#include <gui/ISurfaceTexture.h>
25
26#include <ui/GraphicBuffer.h>
27
28#include <utils/threads.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080029#include <utils/Vector.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080030
31#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
32
33namespace android {
34// ----------------------------------------------------------------------------
35
Jamie Gennis9a78c902011-01-12 18:30:40 -080036class IGraphicBufferAlloc;
37
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080038class SurfaceTexture : public BnSurfaceTexture {
39public:
Jamie Gennis9d4d6c12011-02-27 14:10:20 -080040 enum { MIN_UNDEQUEUED_BUFFERS = 2 };
41 enum { MIN_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS + 1 };
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080042 enum { NUM_BUFFER_SLOTS = 32 };
43
Jamie Gennisc4d4aea2011-01-13 14:43:36 -080044 struct FrameAvailableListener : public virtual RefBase {
45 virtual void onFrameAvailable() = 0;
46 };
47
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080048 // tex indicates the name OpenGL texture to which images are to be streamed.
49 // This texture name cannot be changed once the SurfaceTexture is created.
50 SurfaceTexture(GLuint tex);
51
52 virtual ~SurfaceTexture();
53
54 // setBufferCount updates the number of available buffer slots. After
55 // calling this all buffer slots are both unallocated and owned by the
56 // SurfaceTexture object (i.e. they are not owned by the client).
57 virtual status_t setBufferCount(int bufferCount);
58
59 virtual sp<GraphicBuffer> requestBuffer(int buf, uint32_t w, uint32_t h,
60 uint32_t format, uint32_t usage);
61
62 // dequeueBuffer gets the next buffer slot index for the client to use. If a
63 // buffer slot is available then that slot index is written to the location
64 // pointed to by the buf argument and a status of OK is returned. If no
65 // slot is available then a status of -EBUSY is returned and buf is
66 // unmodified.
67 virtual status_t dequeueBuffer(int *buf);
68
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080069 // queueBuffer returns a filled buffer to the SurfaceTexture. In addition, a
70 // timestamp must be provided for the buffer. The timestamp is in
71 // nanoseconds, and must be monotonically increasing. Its other semantics
72 // (zero point, etc) are client-dependent and should be documented by the
73 // client.
74 virtual status_t queueBuffer(int buf, int64_t timestamp);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080075 virtual void cancelBuffer(int buf);
76 virtual status_t setCrop(const Rect& reg);
77 virtual status_t setTransform(uint32_t transform);
78
79 // updateTexImage sets the image contents of the target texture to that of
80 // the most recently queued buffer.
81 //
82 // This call may only be made while the OpenGL ES context to which the
83 // target texture belongs is bound to the calling thread.
84 status_t updateTexImage();
85
Jamie Gennisf238e282011-01-09 16:33:17 -080086 // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix
87 // associated with the texture image set by the most recent call to
88 // updateTexImage.
89 //
90 // This transform matrix maps 2D homogeneous texture coordinates of the form
91 // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture
92 // coordinate that should be used to sample that location from the texture.
93 // Sampling the texture outside of the range of this transform is undefined.
94 //
95 // This transform is necessary to compensate for transforms that the stream
96 // content producer may implicitly apply to the content. By forcing users of
97 // a SurfaceTexture to apply this transform we avoid performing an extra
98 // copy of the data that would be needed to hide the transform from the
99 // user.
100 //
101 // The matrix is stored in column-major order so that it may be passed
102 // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv
103 // functions.
104 void getTransformMatrix(float mtx[16]);
105
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800106 // getTimestamp retrieves the timestamp associated with the texture image
107 // set by the most recent call to updateTexImage.
108 //
109 // The timestamp is in nanoseconds, and is monotonically increasing. Its
110 // other semantics (zero point, etc) are source-dependent and should be
111 // documented by the source.
112 int64_t getTimestamp();
113
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800114 // setFrameAvailableListener sets the listener object that will be notified
115 // when a new frame becomes available.
116 void setFrameAvailableListener(const sp<FrameAvailableListener>& l);
117
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800118 // getAllocator retrieves the binder object that must be referenced as long
119 // as the GraphicBuffers dequeued from this SurfaceTexture are referenced.
120 // Holding this binder reference prevents SurfaceFlinger from freeing the
121 // buffers before the client is done with them.
122 sp<IBinder> getAllocator();
123
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700124 // setDefaultBufferSize is used to set the size of buffers returned by
125 // requestBuffers when a with and height of zero is requested.
126 // A call to setDefaultBufferSize() may trigger requestBuffers() to
127 // be called from the client.
128 status_t setDefaultBufferSize(uint32_t w, uint32_t h);
129
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700130 // getCurrentBuffer returns the buffer associated with the current image.
131 sp<GraphicBuffer> getCurrentBuffer() const;
132
133 // getCurrentTextureTarget returns the texture target of the current
134 // texture as returned by updateTexImage().
135 GLenum getCurrentTextureTarget() const;
136
137 // getCurrentCrop returns the cropping rectangle of the current buffer
138 Rect getCurrentCrop() const;
139
140 // getCurrentTransform returns the transform of the current buffer
141 uint32_t getCurrentTransform() const;
142
143protected:
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800144
145 // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for
146 // all slots.
147 void freeAllBuffers();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700148 static bool isExternalFormat(uint32_t format);
149 static GLenum getTextureTarget(uint32_t format);
150
151private:
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800152
153 // createImage creates a new EGLImage from a GraphicBuffer.
154 EGLImageKHR createImage(EGLDisplay dpy,
155 const sp<GraphicBuffer>& graphicBuffer);
156
157 enum { INVALID_BUFFER_SLOT = -1 };
158
159 struct BufferSlot {
160 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
161 // if no buffer has been allocated.
162 sp<GraphicBuffer> mGraphicBuffer;
163
164 // mEglImage is the EGLImage created from mGraphicBuffer.
165 EGLImageKHR mEglImage;
166
167 // mEglDisplay is the EGLDisplay used to create mEglImage.
168 EGLDisplay mEglDisplay;
169
170 // mOwnedByClient indicates whether the slot is currently accessible to a
171 // client and should not be used by the SurfaceTexture object. It gets
172 // set to true when dequeueBuffer returns the slot and is reset to false
173 // when the client calls either queueBuffer or cancelBuffer on the slot.
174 bool mOwnedByClient;
175 };
176
177 // mSlots is the array of buffer slots that must be mirrored on the client
178 // side. This allows buffer ownership to be transferred between the client
179 // and server without sending a GraphicBuffer over binder. The entire array
180 // is initialized to NULL at construction time, and buffers are allocated
181 // for a slot when requestBuffer is called with that slot's index.
182 BufferSlot mSlots[NUM_BUFFER_SLOTS];
183
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700184 // mDefaultWidth holds the default width of allocated buffers. It is used
185 // in requestBuffers() if a width and height of zero is specified.
186 uint32_t mDefaultWidth;
187
188 // mDefaultHeight holds the default height of allocated buffers. It is used
189 // in requestBuffers() if a width and height of zero is specified.
190 uint32_t mDefaultHeight;
191
192 // mPixelFormat holds the pixel format of allocated buffers. It is used
193 // in requestBuffers() if a format of zero is specified.
194 uint32_t mPixelFormat;
195
196 // mUseDefaultSize indicates whether or not the default size should be used
197 // that is, if the last requestBuffer has been called with both width
198 // and height null.
199 bool mUseDefaultSize;
200
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800201 // mBufferCount is the number of buffer slots that the client and server
202 // must maintain. It defaults to MIN_BUFFER_SLOTS and can be changed by
203 // calling setBufferCount.
204 int mBufferCount;
205
206 // mCurrentTexture is the buffer slot index of the buffer that is currently
Jamie Gennis67eedd72011-01-09 13:25:39 -0800207 // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
208 // indicating that no buffer slot is currently bound to the texture. Note,
209 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
210 // that no buffer is bound to the texture. A call to setBufferCount will
211 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800212 int mCurrentTexture;
213
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700214 // mCurrentTextureTarget is the GLES texture target to be used with the
215 // current texture.
216 GLenum mCurrentTextureTarget;
217
Jamie Gennis9a78c902011-01-12 18:30:40 -0800218 // mCurrentTextureBuf is the graphic buffer of the current texture. It's
219 // possible that this buffer is not associated with any buffer slot, so we
220 // must track it separately in order to properly use
221 // IGraphicBufferAlloc::freeAllGraphicBuffersExcept.
222 sp<GraphicBuffer> mCurrentTextureBuf;
223
Jamie Gennisf238e282011-01-09 16:33:17 -0800224 // mCurrentCrop is the crop rectangle that applies to the current texture.
225 // It gets set to mLastQueuedCrop each time updateTexImage is called.
226 Rect mCurrentCrop;
227
228 // mCurrentTransform is the transform identifier for the current texture. It
229 // gets set to mLastQueuedTransform each time updateTexImage is called.
230 uint32_t mCurrentTransform;
231
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800232 // mCurrentTimestamp is the timestamp for the current texture. It
233 // gets set to mLastQueuedTimestamp each time updateTexImage is called.
234 int64_t mCurrentTimestamp;
235
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800236 // mLastQueued is the buffer slot index of the most recently enqueued buffer.
237 // At construction time it is initialized to INVALID_BUFFER_SLOT, and is
238 // updated each time queueBuffer is called.
239 int mLastQueued;
240
Jamie Gennisf238e282011-01-09 16:33:17 -0800241 // mLastQueuedCrop is the crop rectangle for the buffer that was most
242 // recently queued. This gets set to mNextCrop each time queueBuffer gets
243 // called.
244 Rect mLastQueuedCrop;
245
246 // mLastQueuedTransform is the transform identifier for the buffer that was
247 // most recently queued. This gets set to mNextTransform each time
248 // queueBuffer gets called.
249 uint32_t mLastQueuedTransform;
250
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800251 // mLastQueuedTimestamp is the timestamp for the buffer that was most
252 // recently queued. This gets set by queueBuffer.
253 int64_t mLastQueuedTimestamp;
254
Jamie Gennisf238e282011-01-09 16:33:17 -0800255 // mNextCrop is the crop rectangle that will be used for the next buffer
256 // that gets queued. It is set by calling setCrop.
257 Rect mNextCrop;
258
259 // mNextTransform is the transform identifier that will be used for the next
260 // buffer that gets queued. It is set by calling setTransform.
261 uint32_t mNextTransform;
262
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800263 // mTexName is the name of the OpenGL texture to which streamed images will
264 // be bound when updateTexImage is called. It is set at construction time
265 // changed with a call to setTexName.
266 const GLuint mTexName;
267
Jamie Gennis9a78c902011-01-12 18:30:40 -0800268 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
269 // allocate new GraphicBuffer objects.
270 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
271
272 // mAllocdBuffers is mirror of the list of buffers that SurfaceFlinger is
273 // referencing. This is kept so that gralloc implementations do not need to
274 // properly handle the case where SurfaceFlinger no longer holds a reference
275 // to a buffer, but other processes do.
276 Vector<sp<GraphicBuffer> > mAllocdBuffers;
277
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800278 // mFrameAvailableListener is the listener object that will be called when a
279 // new frame becomes available. If it is not NULL it will be called from
280 // queueBuffer.
281 sp<FrameAvailableListener> mFrameAvailableListener;
282
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800283 // mMutex is the mutex used to prevent concurrent access to the member
284 // variables of SurfaceTexture objects. It must be locked whenever the
285 // member variables are accessed.
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700286 mutable Mutex mMutex;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800287};
288
289// ----------------------------------------------------------------------------
290}; // namespace android
291
292#endif // ANDROID_GUI_SURFACETEXTURE_H