blob: 002e48b6d925efebe81d72319f41f4d3894aed3e [file] [log] [blame]
Jamie Gennis68e4a7a2010-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 Gennisf7acf162011-01-12 18:30:40 -080029#include <utils/Vector.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080030
31#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
32
33namespace android {
34// ----------------------------------------------------------------------------
35
Jamie Gennisf7acf162011-01-12 18:30:40 -080036class IGraphicBufferAlloc;
37
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080038class SurfaceTexture : public BnSurfaceTexture {
39public:
40 enum { MIN_BUFFER_SLOTS = 3 };
41 enum { NUM_BUFFER_SLOTS = 32 };
42
43 // tex indicates the name OpenGL texture to which images are to be streamed.
44 // This texture name cannot be changed once the SurfaceTexture is created.
45 SurfaceTexture(GLuint tex);
46
47 virtual ~SurfaceTexture();
48
49 // setBufferCount updates the number of available buffer slots. After
50 // calling this all buffer slots are both unallocated and owned by the
51 // SurfaceTexture object (i.e. they are not owned by the client).
52 virtual status_t setBufferCount(int bufferCount);
53
54 virtual sp<GraphicBuffer> requestBuffer(int buf, uint32_t w, uint32_t h,
55 uint32_t format, uint32_t usage);
56
57 // dequeueBuffer gets the next buffer slot index for the client to use. If a
58 // buffer slot is available then that slot index is written to the location
59 // pointed to by the buf argument and a status of OK is returned. If no
60 // slot is available then a status of -EBUSY is returned and buf is
61 // unmodified.
62 virtual status_t dequeueBuffer(int *buf);
63
64 virtual status_t queueBuffer(int buf);
65 virtual void cancelBuffer(int buf);
66 virtual status_t setCrop(const Rect& reg);
67 virtual status_t setTransform(uint32_t transform);
68
69 // updateTexImage sets the image contents of the target texture to that of
70 // the most recently queued buffer.
71 //
72 // This call may only be made while the OpenGL ES context to which the
73 // target texture belongs is bound to the calling thread.
74 status_t updateTexImage();
75
Jamie Gennisb598fb92011-01-09 16:33:17 -080076 // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix
77 // associated with the texture image set by the most recent call to
78 // updateTexImage.
79 //
80 // This transform matrix maps 2D homogeneous texture coordinates of the form
81 // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture
82 // coordinate that should be used to sample that location from the texture.
83 // Sampling the texture outside of the range of this transform is undefined.
84 //
85 // This transform is necessary to compensate for transforms that the stream
86 // content producer may implicitly apply to the content. By forcing users of
87 // a SurfaceTexture to apply this transform we avoid performing an extra
88 // copy of the data that would be needed to hide the transform from the
89 // user.
90 //
91 // The matrix is stored in column-major order so that it may be passed
92 // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv
93 // functions.
94 void getTransformMatrix(float mtx[16]);
95
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080096private:
97
98 // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for
99 // all slots.
100 void freeAllBuffers();
101
102 // createImage creates a new EGLImage from a GraphicBuffer.
103 EGLImageKHR createImage(EGLDisplay dpy,
104 const sp<GraphicBuffer>& graphicBuffer);
105
106 enum { INVALID_BUFFER_SLOT = -1 };
107
108 struct BufferSlot {
109 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
110 // if no buffer has been allocated.
111 sp<GraphicBuffer> mGraphicBuffer;
112
113 // mEglImage is the EGLImage created from mGraphicBuffer.
114 EGLImageKHR mEglImage;
115
116 // mEglDisplay is the EGLDisplay used to create mEglImage.
117 EGLDisplay mEglDisplay;
118
119 // mOwnedByClient indicates whether the slot is currently accessible to a
120 // client and should not be used by the SurfaceTexture object. It gets
121 // set to true when dequeueBuffer returns the slot and is reset to false
122 // when the client calls either queueBuffer or cancelBuffer on the slot.
123 bool mOwnedByClient;
124 };
125
126 // mSlots is the array of buffer slots that must be mirrored on the client
127 // side. This allows buffer ownership to be transferred between the client
128 // and server without sending a GraphicBuffer over binder. The entire array
129 // is initialized to NULL at construction time, and buffers are allocated
130 // for a slot when requestBuffer is called with that slot's index.
131 BufferSlot mSlots[NUM_BUFFER_SLOTS];
132
133 // mBufferCount is the number of buffer slots that the client and server
134 // must maintain. It defaults to MIN_BUFFER_SLOTS and can be changed by
135 // calling setBufferCount.
136 int mBufferCount;
137
138 // mCurrentTexture is the buffer slot index of the buffer that is currently
Jamie Gennisd369dc42011-01-09 13:25:39 -0800139 // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
140 // indicating that no buffer slot is currently bound to the texture. Note,
141 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
142 // that no buffer is bound to the texture. A call to setBufferCount will
143 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800144 int mCurrentTexture;
145
Jamie Gennisf7acf162011-01-12 18:30:40 -0800146 // mCurrentTextureBuf is the graphic buffer of the current texture. It's
147 // possible that this buffer is not associated with any buffer slot, so we
148 // must track it separately in order to properly use
149 // IGraphicBufferAlloc::freeAllGraphicBuffersExcept.
150 sp<GraphicBuffer> mCurrentTextureBuf;
151
Jamie Gennisb598fb92011-01-09 16:33:17 -0800152 // mCurrentCrop is the crop rectangle that applies to the current texture.
153 // It gets set to mLastQueuedCrop each time updateTexImage is called.
154 Rect mCurrentCrop;
155
156 // mCurrentTransform is the transform identifier for the current texture. It
157 // gets set to mLastQueuedTransform each time updateTexImage is called.
158 uint32_t mCurrentTransform;
159
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800160 // mLastQueued is the buffer slot index of the most recently enqueued buffer.
161 // At construction time it is initialized to INVALID_BUFFER_SLOT, and is
162 // updated each time queueBuffer is called.
163 int mLastQueued;
164
Jamie Gennisb598fb92011-01-09 16:33:17 -0800165 // mLastQueuedCrop is the crop rectangle for the buffer that was most
166 // recently queued. This gets set to mNextCrop each time queueBuffer gets
167 // called.
168 Rect mLastQueuedCrop;
169
170 // mLastQueuedTransform is the transform identifier for the buffer that was
171 // most recently queued. This gets set to mNextTransform each time
172 // queueBuffer gets called.
173 uint32_t mLastQueuedTransform;
174
175 // mNextCrop is the crop rectangle that will be used for the next buffer
176 // that gets queued. It is set by calling setCrop.
177 Rect mNextCrop;
178
179 // mNextTransform is the transform identifier that will be used for the next
180 // buffer that gets queued. It is set by calling setTransform.
181 uint32_t mNextTransform;
182
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800183 // mTexName is the name of the OpenGL texture to which streamed images will
184 // be bound when updateTexImage is called. It is set at construction time
185 // changed with a call to setTexName.
186 const GLuint mTexName;
187
Jamie Gennisf7acf162011-01-12 18:30:40 -0800188 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
189 // allocate new GraphicBuffer objects.
190 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
191
192 // mAllocdBuffers is mirror of the list of buffers that SurfaceFlinger is
193 // referencing. This is kept so that gralloc implementations do not need to
194 // properly handle the case where SurfaceFlinger no longer holds a reference
195 // to a buffer, but other processes do.
196 Vector<sp<GraphicBuffer> > mAllocdBuffers;
197
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800198 // mMutex is the mutex used to prevent concurrent access to the member
199 // variables of SurfaceTexture objects. It must be locked whenever the
200 // member variables are accessed.
201 Mutex mMutex;
202};
203
204// ----------------------------------------------------------------------------
205}; // namespace android
206
207#endif // ANDROID_GUI_SURFACETEXTURE_H