SurfaceTexture.java revision ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08f
16714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis/*
26714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Copyright (C) 2010 The Android Open Source Project
36714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *
46714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Licensed under the Apache License, Version 2.0 (the "License");
56714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * you may not use this file except in compliance with the License.
66714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * You may obtain a copy of the License at
76714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *
86714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *      http://www.apache.org/licenses/LICENSE-2.0
96714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *
106714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Unless required by applicable law or agreed to in writing, software
116714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * distributed under the License is distributed on an "AS IS" BASIS,
126714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * See the License for the specific language governing permissions and
146714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * limitations under the License.
156714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */
166714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
176714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennispackage android.graphics;
186714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
19376590d668e22a918439877b55faf075427b13f3Jamie Gennisimport java.lang.ref.WeakReference;
20376590d668e22a918439877b55faf075427b13f3Jamie Gennisimport android.os.Handler;
21376590d668e22a918439877b55faf075427b13f3Jamie Gennisimport android.os.Looper;
22376590d668e22a918439877b55faf075427b13f3Jamie Gennisimport android.os.Message;
23376590d668e22a918439877b55faf075427b13f3Jamie Gennis
246714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis/**
256714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Captures frames from an image stream as an OpenGL ES texture.
266714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *
27cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * <p>The image stream may come from either camera preview or video decode.  A SurfaceTexture
28cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * may be used in place of a SurfaceHolder when specifying the output destination of a
29cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * {@link android.hardware.Camera} or {@link android.media.MediaPlayer}
3037cec0fc50760fe89614863eded14011f9412534Jamie Gennis * object.  Doing so will cause all the frames from the image stream to be sent to the
316714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * SurfaceTexture object rather than to the device's display.  When {@link #updateTexImage} is
32cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * called, the contents of the texture object specified when the SurfaceTexture was created are
336714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * updated to contain the most recent image from the image stream.  This may cause some frames of
346714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * the stream to be skipped.
356714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *
365f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * <p>When sampling from the texture one should first transform the texture coordinates using the
378f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * matrix queried via {@link #getTransformMatrix(float[])}.  The transform matrix may change each
388f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * time {@link #updateTexImage} is called, so it should be re-queried each time the texture image
398f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * is updated.
405f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * This matrix transforms traditional 2D OpenGL ES texture coordinate column vectors of the form (s,
415f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * t, 0, 1) where s and t are on the inclusive interval [0, 1] to the proper sampling location in
425f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * the streamed texture.  This transform compensates for any properties of the image stream source
435f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * that cause it to appear different from a traditional OpenGL ES texture.  For example, sampling
445f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * from the bottom left corner of the image can be accomplished by transforming the column vector
455f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * (0, 0, 0, 1) using the queried matrix, while sampling from the top right corner of the image can
465f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * be done by transforming (1, 1, 0, 1).
475f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis *
486714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * <p>The texture object uses the GL_TEXTURE_EXTERNAL_OES texture target, which is defined by the
49858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * <a href="http://www.khronos.org/registry/gles/extensions/OES/OES_EGL_image_external.txt">
50858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * GL_OES_EGL_image_external</a> OpenGL ES extension.  This limits how the texture may be used.
51858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * Each time the texture is bound it must be bound to the GL_TEXTURE_EXTERNAL_OES target rather than
52858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * the GL_TEXTURE_2D target.  Additionally, any OpenGL ES 2.0 shader that samples from the texture
53858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * must declare its use of this extension using, for example, an "#extension
54858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * GL_OES_EGL_image_external : require" directive.  Such shaders must also access the texture using
55858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * the samplerExternalOES GLSL sampler type.
5637cec0fc50760fe89614863eded14011f9412534Jamie Gennis *
5737cec0fc50760fe89614863eded14011f9412534Jamie Gennis * <p>SurfaceTexture objects may be created on any thread.  {@link #updateTexImage} may only be
5837cec0fc50760fe89614863eded14011f9412534Jamie Gennis * called on the thread with the OpenGL ES context that contains the texture object.  The
5937cec0fc50760fe89614863eded14011f9412534Jamie Gennis * frame-available callback is called on an arbitrary thread, so unless special care is taken {@link
6037cec0fc50760fe89614863eded14011f9412534Jamie Gennis * #updateTexImage} should not be called directly from the callback.
616714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */
626714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennispublic class SurfaceTexture {
636714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
64376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private EventHandler mEventHandler;
65376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private OnFrameAvailableListener mOnFrameAvailableListener;
66376590d668e22a918439877b55faf075427b13f3Jamie Gennis
678f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    /**
688f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy     * This field is used by native code, do not access or modify.
698f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy     */
70e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy    private int mSurfaceTexture;
716714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
726714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
736714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Callback interface for being notified that a new stream frame is available.
746714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
756714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public interface OnFrameAvailableListener {
766714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        void onFrameAvailable(SurfaceTexture surfaceTexture);
776714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
786714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
796714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
806714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Exception thrown when a surface couldn't be created or resized
816714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
826714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public static class OutOfResourcesException extends Exception {
836714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        public OutOfResourcesException() {
846714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        }
856714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        public OutOfResourcesException(String name) {
866714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis            super(name);
876714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        }
886714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
896714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
906714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
916714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Construct a new SurfaceTexture to stream images to a given OpenGL texture.
926714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     *
936714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * @param texName the OpenGL texture object name (e.g. generated via glGenTextures)
946714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
956714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public SurfaceTexture(int texName) {
960904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba        this(texName, true);
970904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba    }
980904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba
990904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba    /**
1000904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     * Construct a new SurfaceTexture to stream images to a given OpenGL texture.
1010904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     *
1020904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     * @param texName the OpenGL texture object name (e.g. generated via glGenTextures)
1030904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     * @param allowSynchronousMode whether the SurfaceTexture can run in the synchronous mode.
1040904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     *      When the image stream comes from OpenGL, SurfaceTexture may run in the synchronous
1050904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     *      mode where the producer side may be blocked to avoid skipping frames. To avoid the
1060904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     *      thread block, set allowSynchronousMode to false.
1070904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     */
1080904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba    public SurfaceTexture(int texName, boolean allowSynchronousMode) {
109376590d668e22a918439877b55faf075427b13f3Jamie Gennis        Looper looper;
110376590d668e22a918439877b55faf075427b13f3Jamie Gennis        if ((looper = Looper.myLooper()) != null) {
111376590d668e22a918439877b55faf075427b13f3Jamie Gennis            mEventHandler = new EventHandler(looper);
112376590d668e22a918439877b55faf075427b13f3Jamie Gennis        } else if ((looper = Looper.getMainLooper()) != null) {
113376590d668e22a918439877b55faf075427b13f3Jamie Gennis            mEventHandler = new EventHandler(looper);
114376590d668e22a918439877b55faf075427b13f3Jamie Gennis        } else {
115376590d668e22a918439877b55faf075427b13f3Jamie Gennis            mEventHandler = null;
116376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
1170904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba        nativeInit(texName, new WeakReference<SurfaceTexture>(this), allowSynchronousMode);
1186714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
1196714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
1206714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
1216714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Register a callback to be invoked when a new image frame becomes available to the
1226714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * SurfaceTexture.  Note that this callback may be called on an arbitrary thread, so it is not
1236714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * safe to call {@link #updateTexImage} without first binding the OpenGL ES context to the
1246714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * thread invoking the callback.
1256714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
1266714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public void setOnFrameAvailableListener(OnFrameAvailableListener l) {
127376590d668e22a918439877b55faf075427b13f3Jamie Gennis        mOnFrameAvailableListener = l;
1286714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
1296714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
1306714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
131050316184b01c0d1a01c46afae7429b89a27c31btedbo     * Set the size of buffers returned by requestBuffers when a width and height
132050316184b01c0d1a01c46afae7429b89a27c31btedbo     * of zero is requested.
133050316184b01c0d1a01c46afae7429b89a27c31btedbo     *
134050316184b01c0d1a01c46afae7429b89a27c31btedbo     * @hide Pending approval by API council.
135050316184b01c0d1a01c46afae7429b89a27c31btedbo     */
136050316184b01c0d1a01c46afae7429b89a27c31btedbo    public void setDefaultBufferSize(int width, int height) {
137050316184b01c0d1a01c46afae7429b89a27c31btedbo        nativeSetDefaultBufferSize(width, height);
138050316184b01c0d1a01c46afae7429b89a27c31btedbo    }
139050316184b01c0d1a01c46afae7429b89a27c31btedbo
140050316184b01c0d1a01c46afae7429b89a27c31btedbo    /**
1416714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Update the texture image to the most recent frame from the image stream.  This may only be
1426714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * called while the OpenGL ES context that owns the texture is bound to the thread.  It will
1436714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target.
1446714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
145376590d668e22a918439877b55faf075427b13f3Jamie Gennis    public void updateTexImage() {
146376590d668e22a918439877b55faf075427b13f3Jamie Gennis        nativeUpdateTexImage();
147376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
148b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
149b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    /**
150b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by
151b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * the most recent call to updateTexImage.
152b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
153b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s
154b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample
155b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * that location from the texture.  Sampling the texture outside of the range of this transform
156b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * is undefined.
157b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
158b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via
159b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * the glLoadMatrixf or glUniformMatrix4fv functions.
160b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
161b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * @param mtx the array into which the 4x4 matrix will be stored.  The array must have exactly
162b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *     16 elements.
163b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     */
164b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    public void getTransformMatrix(float[] mtx) {
165cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten        // Note we intentionally don't check mtx for null, so this will result in a
166cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten        // NullPointerException. But it's safe because it happens before the call to native.
167b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis        if (mtx.length != 16) {
168b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis            throw new IllegalArgumentException();
169b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis        }
170376590d668e22a918439877b55faf075427b13f3Jamie Gennis        nativeGetTransformMatrix(mtx);
171b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    }
172b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
173c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    /**
174c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * Retrieve the timestamp associated with the texture image set by the most recent call to
175c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * updateTexImage.
176c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     *
1775c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten     * This timestamp is in nanoseconds, and is normally monotonically increasing. The timestamp
1785c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten     * should be unaffected by time-of-day adjustments, and for a camera should be strictly
1795c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten     * monotonic but for a MediaPlayer may be reset when the position is set.  The
180c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * specific meaning and zero point of the timestamp depends on the source providing images to
181c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * the SurfaceTexture. Unless otherwise specified by the image source, timestamps cannot
182c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * generally be compared across SurfaceTexture instances, or across multiple program
183c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * invocations. It is mostly useful for determining time offsets between subsequent frames.
184c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     */
1855c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten
186c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    public long getTimestamp() {
187c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala        return nativeGetTimestamp();
188c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    }
189c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala
190ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    /**
191ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * release() frees all the buffers and puts the SurfaceTexture into the
192ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * 'abandoned' state. Once put in this state the SurfaceTexture can never
193ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * leave it. When in the 'abandoned' state, all methods of the
194ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * ISurfaceTexture interface will fail with the NO_INIT error.
195ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     *
196ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * Note that while calling this method causes all the buffers to be freed
197ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * from the perspective of the the SurfaceTexture, if there are additional
198ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * references on the buffers (e.g. if a buffer is referenced by a client or
199ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * by OpenGL ES as a texture) then those buffer will remain allocated.
200ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     *
201ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * Always call this method when you are done with SurfaceTexture. Failing
202ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * to do so may delay resource deallocation for a significant amount of
203ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * time.
204ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     */
205ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    public void release() {
206ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian        nativeRelease();
207ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    }
208ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian
209376590d668e22a918439877b55faf075427b13f3Jamie Gennis    protected void finalize() throws Throwable {
210376590d668e22a918439877b55faf075427b13f3Jamie Gennis        try {
211376590d668e22a918439877b55faf075427b13f3Jamie Gennis            nativeFinalize();
212376590d668e22a918439877b55faf075427b13f3Jamie Gennis        } finally {
213376590d668e22a918439877b55faf075427b13f3Jamie Gennis            super.finalize();
214376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
215376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
216376590d668e22a918439877b55faf075427b13f3Jamie Gennis
217376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private class EventHandler extends Handler {
218376590d668e22a918439877b55faf075427b13f3Jamie Gennis        public EventHandler(Looper looper) {
219376590d668e22a918439877b55faf075427b13f3Jamie Gennis            super(looper);
220376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
221376590d668e22a918439877b55faf075427b13f3Jamie Gennis
222376590d668e22a918439877b55faf075427b13f3Jamie Gennis        @Override
223376590d668e22a918439877b55faf075427b13f3Jamie Gennis        public void handleMessage(Message msg) {
224376590d668e22a918439877b55faf075427b13f3Jamie Gennis            if (mOnFrameAvailableListener != null) {
225376590d668e22a918439877b55faf075427b13f3Jamie Gennis                mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this);
226376590d668e22a918439877b55faf075427b13f3Jamie Gennis            }
227376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
228376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
229376590d668e22a918439877b55faf075427b13f3Jamie Gennis
2308f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    /**
2318f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy     * This method is invoked from native code only.
2328f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy     */
2338f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @SuppressWarnings({"UnusedDeclaration"})
234376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private static void postEventFromNative(Object selfRef) {
235376590d668e22a918439877b55faf075427b13f3Jamie Gennis        WeakReference weakSelf = (WeakReference)selfRef;
236376590d668e22a918439877b55faf075427b13f3Jamie Gennis        SurfaceTexture st = (SurfaceTexture)weakSelf.get();
237376590d668e22a918439877b55faf075427b13f3Jamie Gennis        if (st == null) {
238376590d668e22a918439877b55faf075427b13f3Jamie Gennis            return;
239376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
240376590d668e22a918439877b55faf075427b13f3Jamie Gennis
241376590d668e22a918439877b55faf075427b13f3Jamie Gennis        if (st.mEventHandler != null) {
242376590d668e22a918439877b55faf075427b13f3Jamie Gennis            Message m = st.mEventHandler.obtainMessage();
243376590d668e22a918439877b55faf075427b13f3Jamie Gennis            st.mEventHandler.sendMessage(m);
244376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
245376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
246b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
2470904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba    private native void nativeInit(int texName, Object weakSelf, boolean allowSynchronousMode);
248376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeFinalize();
249376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeGetTransformMatrix(float[] mtx);
250c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    private native long nativeGetTimestamp();
251050316184b01c0d1a01c46afae7429b89a27c31btedbo    private native void nativeSetDefaultBufferSize(int width, int height);
252376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeUpdateTexImage();
253925bcaabde5a21687b51caa7ab329310a819f068Grace Kloba    private native int nativeGetQueuedCount();
254ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    private native void nativeRelease();
2556714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
2566714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /*
2576714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * We use a class initializer to allow the native code to cache some
2586714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * field offsets.
2596714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
2606714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    private static native void nativeClassInit();
2616714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    static { nativeClassInit(); }
2626714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis}
263