SurfaceTexture.java revision 2aafe742e5d2d63d77c49df032ec580966661597
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
17package android.graphics;
18
19import java.lang.ref.WeakReference;
20import android.os.Handler;
21import android.os.Looper;
22import android.os.Message;
23
24/**
25 * Captures frames from an image stream as an OpenGL ES texture.
26 *
27 * <p>The image stream may come from either camera preview or video decode.  A SurfaceTexture
28 * may be used in place of a SurfaceHolder when specifying the output destination of a
29 * {@link android.hardware.Camera} or {@link android.media.MediaPlayer}
30 * object.  Doing so will cause all the frames from the image stream to be sent to the
31 * SurfaceTexture object rather than to the device's display.  When {@link #updateTexImage} is
32 * called, the contents of the texture object specified when the SurfaceTexture was created are
33 * updated to contain the most recent image from the image stream.  This may cause some frames of
34 * the stream to be skipped.
35 *
36 * <p>When sampling from the texture one should first transform the texture coordinates using the
37 * matrix queried via {@link #getTransformMatrix(float[])}.  The transform matrix may change each
38 * time {@link #updateTexImage} is called, so it should be re-queried each time the texture image
39 * is updated.
40 * This matrix transforms traditional 2D OpenGL ES texture coordinate column vectors of the form (s,
41 * t, 0, 1) where s and t are on the inclusive interval [0, 1] to the proper sampling location in
42 * the streamed texture.  This transform compensates for any properties of the image stream source
43 * that cause it to appear different from a traditional OpenGL ES texture.  For example, sampling
44 * from the bottom left corner of the image can be accomplished by transforming the column vector
45 * (0, 0, 0, 1) using the queried matrix, while sampling from the top right corner of the image can
46 * be done by transforming (1, 1, 0, 1).
47 *
48 * <p>The texture object uses the GL_TEXTURE_EXTERNAL_OES texture target, which is defined by the
49 * <a href="http://www.khronos.org/registry/gles/extensions/OES/OES_EGL_image_external.txt">
50 * GL_OES_EGL_image_external</a> OpenGL ES extension.  This limits how the texture may be used.
51 * Each time the texture is bound it must be bound to the GL_TEXTURE_EXTERNAL_OES target rather than
52 * the GL_TEXTURE_2D target.  Additionally, any OpenGL ES 2.0 shader that samples from the texture
53 * must declare its use of this extension using, for example, an "#extension
54 * GL_OES_EGL_image_external : require" directive.  Such shaders must also access the texture using
55 * the samplerExternalOES GLSL sampler type.
56 *
57 * <p>SurfaceTexture objects may be created on any thread.  {@link #updateTexImage} may only be
58 * called on the thread with the OpenGL ES context that contains the texture object.  The
59 * frame-available callback is called on an arbitrary thread, so unless special care is taken {@link
60 * #updateTexImage} should not be called directly from the callback.
61 */
62public class SurfaceTexture {
63
64    private EventHandler mEventHandler;
65    private OnFrameAvailableListener mOnFrameAvailableListener;
66
67    /**
68     * This field is used by native code, do not access or modify.
69     */
70    private int mSurfaceTexture;
71
72    /**
73     * Callback interface for being notified that a new stream frame is available.
74     */
75    public interface OnFrameAvailableListener {
76        void onFrameAvailable(SurfaceTexture surfaceTexture);
77    }
78
79    /**
80     * Exception thrown when a surface couldn't be created or resized
81     */
82    public static class OutOfResourcesException extends Exception {
83        public OutOfResourcesException() {
84        }
85        public OutOfResourcesException(String name) {
86            super(name);
87        }
88    }
89
90    /**
91     * Construct a new SurfaceTexture to stream images to a given OpenGL texture.
92     *
93     * @param texName the OpenGL texture object name (e.g. generated via glGenTextures)
94     */
95    public SurfaceTexture(int texName) {
96        this(texName, false);
97    }
98
99    /**
100     * Construct a new SurfaceTexture to stream images to a given OpenGL texture.
101     *
102     * @param texName the OpenGL texture object name (e.g. generated via glGenTextures)
103     * @param allowSynchronousMode whether the SurfaceTexture can run in the synchronous mode.
104     *      When the image stream comes from OpenGL, SurfaceTexture may run in the synchronous
105     *      mode where the producer side may be blocked to avoid skipping frames. To avoid the
106     *      thread block, set allowSynchronousMode to false.
107     *
108     * @hide
109     */
110    public SurfaceTexture(int texName, boolean allowSynchronousMode) {
111        Looper looper;
112        if ((looper = Looper.myLooper()) != null) {
113            mEventHandler = new EventHandler(looper);
114        } else if ((looper = Looper.getMainLooper()) != null) {
115            mEventHandler = new EventHandler(looper);
116        } else {
117            mEventHandler = null;
118        }
119        nativeInit(texName, new WeakReference<SurfaceTexture>(this), allowSynchronousMode);
120    }
121
122    /**
123     * Register a callback to be invoked when a new image frame becomes available to the
124     * SurfaceTexture.  Note that this callback may be called on an arbitrary thread, so it is not
125     * safe to call {@link #updateTexImage} without first binding the OpenGL ES context to the
126     * thread invoking the callback.
127     */
128    public void setOnFrameAvailableListener(OnFrameAvailableListener l) {
129        mOnFrameAvailableListener = l;
130    }
131
132    /**
133     * Set the default size of the image buffers.  The image producer may override the buffer size,
134     * in which case the producer-set buffer size will be used, not the default size set by this
135     * method.  Both video and camera based image producers do override the size.  This method may
136     * be used to set the image size when producing images with {@link android.graphics.Canvas} (via
137     * {@link android.view.Surface#lockCanvas}), or OpenGL ES (via an EGLSurface).
138     *
139     * The new default buffer size will take effect the next time the image producer requests a
140     * buffer to fill.  For {@link android.graphics.Canvas} this will be the next time {@link
141     * android.view.Surface#lockCanvas} is called.  For OpenGL ES, the EGLSurface should be
142     * destroyed (via eglDestroySurface), made not-current (via eglMakeCurrent), and then recreated
143     * (via eglCreateWindowSurface) to ensure that the new default size has taken effect.
144     */
145    public void setDefaultBufferSize(int width, int height) {
146        nativeSetDefaultBufferSize(width, height);
147    }
148
149    /**
150     * Update the texture image to the most recent frame from the image stream.  This may only be
151     * called while the OpenGL ES context that owns the texture is bound to the thread.  It will
152     * implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target.
153     */
154    public void updateTexImage() {
155        nativeUpdateTexImage();
156    }
157
158    /**
159     * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by
160     * the most recent call to updateTexImage.
161     *
162     * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s
163     * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample
164     * that location from the texture.  Sampling the texture outside of the range of this transform
165     * is undefined.
166     *
167     * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via
168     * the glLoadMatrixf or glUniformMatrix4fv functions.
169     *
170     * @param mtx the array into which the 4x4 matrix will be stored.  The array must have exactly
171     *     16 elements.
172     */
173    public void getTransformMatrix(float[] mtx) {
174        // Note we intentionally don't check mtx for null, so this will result in a
175        // NullPointerException. But it's safe because it happens before the call to native.
176        if (mtx.length != 16) {
177            throw new IllegalArgumentException();
178        }
179        nativeGetTransformMatrix(mtx);
180    }
181
182    /**
183     * Retrieve the timestamp associated with the texture image set by the most recent call to
184     * updateTexImage.
185     *
186     * This timestamp is in nanoseconds, and is normally monotonically increasing. The timestamp
187     * should be unaffected by time-of-day adjustments, and for a camera should be strictly
188     * monotonic but for a MediaPlayer may be reset when the position is set.  The
189     * specific meaning and zero point of the timestamp depends on the source providing images to
190     * the SurfaceTexture. Unless otherwise specified by the image source, timestamps cannot
191     * generally be compared across SurfaceTexture instances, or across multiple program
192     * invocations. It is mostly useful for determining time offsets between subsequent frames.
193     */
194
195    public long getTimestamp() {
196        return nativeGetTimestamp();
197    }
198
199    /**
200     * release() frees all the buffers and puts the SurfaceTexture into the
201     * 'abandoned' state. Once put in this state the SurfaceTexture can never
202     * leave it. When in the 'abandoned' state, all methods of the
203     * ISurfaceTexture interface will fail with the NO_INIT error.
204     *
205     * Note that while calling this method causes all the buffers to be freed
206     * from the perspective of the the SurfaceTexture, if there are additional
207     * references on the buffers (e.g. if a buffer is referenced by a client or
208     * by OpenGL ES as a texture) then those buffer will remain allocated.
209     *
210     * Always call this method when you are done with SurfaceTexture. Failing
211     * to do so may delay resource deallocation for a significant amount of
212     * time.
213     */
214    public void release() {
215        nativeRelease();
216    }
217
218    protected void finalize() throws Throwable {
219        try {
220            nativeFinalize();
221        } finally {
222            super.finalize();
223        }
224    }
225
226    private class EventHandler extends Handler {
227        public EventHandler(Looper looper) {
228            super(looper);
229        }
230
231        @Override
232        public void handleMessage(Message msg) {
233            if (mOnFrameAvailableListener != null) {
234                mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this);
235            }
236        }
237    }
238
239    /**
240     * This method is invoked from native code only.
241     */
242    @SuppressWarnings({"UnusedDeclaration"})
243    private static void postEventFromNative(Object selfRef) {
244        WeakReference weakSelf = (WeakReference)selfRef;
245        SurfaceTexture st = (SurfaceTexture)weakSelf.get();
246        if (st == null) {
247            return;
248        }
249
250        if (st.mEventHandler != null) {
251            Message m = st.mEventHandler.obtainMessage();
252            st.mEventHandler.sendMessage(m);
253        }
254    }
255
256    private native void nativeInit(int texName, Object weakSelf, boolean allowSynchronousMode);
257    private native void nativeFinalize();
258    private native void nativeGetTransformMatrix(float[] mtx);
259    private native long nativeGetTimestamp();
260    private native void nativeSetDefaultBufferSize(int width, int height);
261    private native void nativeUpdateTexImage();
262    private native int nativeGetQueuedCount();
263    private native void nativeRelease();
264
265    /*
266     * We use a class initializer to allow the native code to cache some
267     * field offsets.
268     */
269    private static native void nativeClassInit();
270    static { nativeClassInit(); }
271}
272