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