SurfaceTexture.java revision c6d993077761fc737bbb0f4db44b961a4e7b6bbb
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;
20b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian
21376590d668e22a918439877b55faf075427b13f3Jamie Gennisimport android.os.Handler;
22376590d668e22a918439877b55faf075427b13f3Jamie Gennisimport android.os.Looper;
23376590d668e22a918439877b55faf075427b13f3Jamie Gennisimport android.os.Message;
24376590d668e22a918439877b55faf075427b13f3Jamie Gennis
256714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis/**
266714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Captures frames from an image stream as an OpenGL ES texture.
276714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *
28cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * <p>The image stream may come from either camera preview or video decode.  A SurfaceTexture
29cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * may be used in place of a SurfaceHolder when specifying the output destination of a
30cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * {@link android.hardware.Camera} or {@link android.media.MediaPlayer}
3137cec0fc50760fe89614863eded14011f9412534Jamie Gennis * object.  Doing so will cause all the frames from the image stream to be sent to the
326714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * SurfaceTexture object rather than to the device's display.  When {@link #updateTexImage} is
33cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * called, the contents of the texture object specified when the SurfaceTexture was created are
346714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * updated to contain the most recent image from the image stream.  This may cause some frames of
356714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * the stream to be skipped.
366714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *
375f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * <p>When sampling from the texture one should first transform the texture coordinates using the
388f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * matrix queried via {@link #getTransformMatrix(float[])}.  The transform matrix may change each
398f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * time {@link #updateTexImage} is called, so it should be re-queried each time the texture image
408f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * is updated.
415f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * This matrix transforms traditional 2D OpenGL ES texture coordinate column vectors of the form (s,
425f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * t, 0, 1) where s and t are on the inclusive interval [0, 1] to the proper sampling location in
435f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * the streamed texture.  This transform compensates for any properties of the image stream source
445f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * that cause it to appear different from a traditional OpenGL ES texture.  For example, sampling
455f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * from the bottom left corner of the image can be accomplished by transforming the column vector
465f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * (0, 0, 0, 1) using the queried matrix, while sampling from the top right corner of the image can
475f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * be done by transforming (1, 1, 0, 1).
485f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis *
496714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * <p>The texture object uses the GL_TEXTURE_EXTERNAL_OES texture target, which is defined by the
50858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * <a href="http://www.khronos.org/registry/gles/extensions/OES/OES_EGL_image_external.txt">
51858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * GL_OES_EGL_image_external</a> OpenGL ES extension.  This limits how the texture may be used.
52858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * Each time the texture is bound it must be bound to the GL_TEXTURE_EXTERNAL_OES target rather than
53858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * the GL_TEXTURE_2D target.  Additionally, any OpenGL ES 2.0 shader that samples from the texture
54858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * must declare its use of this extension using, for example, an "#extension
55858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * GL_OES_EGL_image_external : require" directive.  Such shaders must also access the texture using
56858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * the samplerExternalOES GLSL sampler type.
5737cec0fc50760fe89614863eded14011f9412534Jamie Gennis *
5837cec0fc50760fe89614863eded14011f9412534Jamie Gennis * <p>SurfaceTexture objects may be created on any thread.  {@link #updateTexImage} may only be
5937cec0fc50760fe89614863eded14011f9412534Jamie Gennis * called on the thread with the OpenGL ES context that contains the texture object.  The
6037cec0fc50760fe89614863eded14011f9412534Jamie Gennis * frame-available callback is called on an arbitrary thread, so unless special care is taken {@link
6137cec0fc50760fe89614863eded14011f9412534Jamie Gennis * #updateTexImage} should not be called directly from the callback.
626714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */
636714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennispublic class SurfaceTexture {
646714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
65376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private EventHandler mEventHandler;
66376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private OnFrameAvailableListener mOnFrameAvailableListener;
67376590d668e22a918439877b55faf075427b13f3Jamie Gennis
688f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    /**
698f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy     * This field is used by native code, do not access or modify.
708f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy     */
71e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy    private int mSurfaceTexture;
726714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
736714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
746714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Callback interface for being notified that a new stream frame is available.
756714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
766714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public interface OnFrameAvailableListener {
776714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        void onFrameAvailable(SurfaceTexture surfaceTexture);
786714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
796714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
806714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
816714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Exception thrown when a surface couldn't be created or resized
826714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
836714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public static class OutOfResourcesException extends Exception {
846714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        public OutOfResourcesException() {
856714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        }
866714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        public OutOfResourcesException(String name) {
876714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis            super(name);
886714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        }
896714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
906714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
916714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
926714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Construct a new SurfaceTexture to stream images to a given OpenGL texture.
936714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     *
946714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * @param texName the OpenGL texture object name (e.g. generated via glGenTextures)
956714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
966714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public SurfaceTexture(int texName) {
97554366d158a0ec330a339f4343fb0a3164257f1eJamie Gennis        this(texName, false);
980904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba    }
990904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba
1000904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba    /**
1010904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     * Construct a new SurfaceTexture to stream images to a given OpenGL texture.
1020904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     *
1030904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     * @param texName the OpenGL texture object name (e.g. generated via glGenTextures)
1040904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     * @param allowSynchronousMode whether the SurfaceTexture can run in the synchronous mode.
1050904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     *      When the image stream comes from OpenGL, SurfaceTexture may run in the synchronous
1060904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     *      mode where the producer side may be blocked to avoid skipping frames. To avoid the
1070904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     *      thread block, set allowSynchronousMode to false.
108554366d158a0ec330a339f4343fb0a3164257f1eJamie Gennis     *
109554366d158a0ec330a339f4343fb0a3164257f1eJamie Gennis     * @hide
1100904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba     */
1110904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba    public SurfaceTexture(int texName, boolean allowSynchronousMode) {
112376590d668e22a918439877b55faf075427b13f3Jamie Gennis        Looper looper;
113376590d668e22a918439877b55faf075427b13f3Jamie Gennis        if ((looper = Looper.myLooper()) != null) {
114376590d668e22a918439877b55faf075427b13f3Jamie Gennis            mEventHandler = new EventHandler(looper);
115376590d668e22a918439877b55faf075427b13f3Jamie Gennis        } else if ((looper = Looper.getMainLooper()) != null) {
116376590d668e22a918439877b55faf075427b13f3Jamie Gennis            mEventHandler = new EventHandler(looper);
117376590d668e22a918439877b55faf075427b13f3Jamie Gennis        } else {
118376590d668e22a918439877b55faf075427b13f3Jamie Gennis            mEventHandler = null;
119376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
1200904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba        nativeInit(texName, new WeakReference<SurfaceTexture>(this), allowSynchronousMode);
1216714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
1226714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
1236714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
1246714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Register a callback to be invoked when a new image frame becomes available to the
1256714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * SurfaceTexture.  Note that this callback may be called on an arbitrary thread, so it is not
1266714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * safe to call {@link #updateTexImage} without first binding the OpenGL ES context to the
1276714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * thread invoking the callback.
1286714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
1296714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public void setOnFrameAvailableListener(OnFrameAvailableListener l) {
130376590d668e22a918439877b55faf075427b13f3Jamie Gennis        mOnFrameAvailableListener = l;
1316714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
1326714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
1336714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
1342aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * Set the default size of the image buffers.  The image producer may override the buffer size,
1352aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * in which case the producer-set buffer size will be used, not the default size set by this
1362aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * method.  Both video and camera based image producers do override the size.  This method may
1372aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * be used to set the image size when producing images with {@link android.graphics.Canvas} (via
1382aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * {@link android.view.Surface#lockCanvas}), or OpenGL ES (via an EGLSurface).
139050316184b01c0d1a01c46afae7429b89a27c31btedbo     *
1402aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * The new default buffer size will take effect the next time the image producer requests a
1412aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * buffer to fill.  For {@link android.graphics.Canvas} this will be the next time {@link
1422aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * android.view.Surface#lockCanvas} is called.  For OpenGL ES, the EGLSurface should be
1432aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * destroyed (via eglDestroySurface), made not-current (via eglMakeCurrent), and then recreated
1442aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * (via eglCreateWindowSurface) to ensure that the new default size has taken effect.
145b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian     *
146b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian     * The width and height parameters must be no greater than the minimum of
147b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian     * GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see
148b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian     * {@link javax.microedition.khronos.opengles.GL10#glGetIntegerv glGetIntegerv}).
149b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian     * An error due to invalid dimensions might not be reported until
150b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian     * updateTexImage() is called.
151050316184b01c0d1a01c46afae7429b89a27c31btedbo     */
152050316184b01c0d1a01c46afae7429b89a27c31btedbo    public void setDefaultBufferSize(int width, int height) {
153050316184b01c0d1a01c46afae7429b89a27c31btedbo        nativeSetDefaultBufferSize(width, height);
154050316184b01c0d1a01c46afae7429b89a27c31btedbo    }
155050316184b01c0d1a01c46afae7429b89a27c31btedbo
156050316184b01c0d1a01c46afae7429b89a27c31btedbo    /**
1576714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Update the texture image to the most recent frame from the image stream.  This may only be
1586714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * called while the OpenGL ES context that owns the texture is bound to the thread.  It will
1596714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target.
1606714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
161376590d668e22a918439877b55faf075427b13f3Jamie Gennis    public void updateTexImage() {
162b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian        int err = nativeUpdateTexImage();
163b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian        if (err != 0) {
164c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis            throw new RuntimeException("Error during updateTexImage (see logcat for details)");
165c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        }
166c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    }
167c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis
168c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    /**
169c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis     * Detach the SurfaceTexture from the OpenGL ES context with which it is currently associated.
170c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis     * This can be used to change from one OpenGL ES context to another.
171c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis     *
172c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis     * @hide
173c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis     */
174c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    public void detachFromGLContext() {
175c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        int err = nativeDetachFromGLContext();
176c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        if (err != 0) {
177c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis            throw new RuntimeException("Error during detachFromGLContext (see logcat for details)");
178c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        }
179c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    }
180c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis
181c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    /**
182c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis     *
183c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis     * @hide
184c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis     */
185c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    public void attachToGLContext(int texName) {
186c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        int err = nativeAttachToGLContext(texName);
187c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        if (err != 0) {
188c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis            throw new RuntimeException("Error during detachFromGLContext (see logcat for details)");
189b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian        }
190376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
191b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
192b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    /**
193b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by
194b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * the most recent call to updateTexImage.
195b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
196b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s
197b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample
198b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * that location from the texture.  Sampling the texture outside of the range of this transform
199b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * is undefined.
200b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
201b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via
202b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * the glLoadMatrixf or glUniformMatrix4fv functions.
203b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
204b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * @param mtx the array into which the 4x4 matrix will be stored.  The array must have exactly
205b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *     16 elements.
206b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     */
207b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    public void getTransformMatrix(float[] mtx) {
208cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten        // Note we intentionally don't check mtx for null, so this will result in a
209cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten        // NullPointerException. But it's safe because it happens before the call to native.
210b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis        if (mtx.length != 16) {
211b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis            throw new IllegalArgumentException();
212b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis        }
213376590d668e22a918439877b55faf075427b13f3Jamie Gennis        nativeGetTransformMatrix(mtx);
214b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    }
215b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
216c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    /**
217c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * Retrieve the timestamp associated with the texture image set by the most recent call to
218c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * updateTexImage.
219c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     *
2205c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten     * This timestamp is in nanoseconds, and is normally monotonically increasing. The timestamp
2215c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten     * should be unaffected by time-of-day adjustments, and for a camera should be strictly
2225c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten     * monotonic but for a MediaPlayer may be reset when the position is set.  The
223c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * specific meaning and zero point of the timestamp depends on the source providing images to
224c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * the SurfaceTexture. Unless otherwise specified by the image source, timestamps cannot
225c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * generally be compared across SurfaceTexture instances, or across multiple program
226c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * invocations. It is mostly useful for determining time offsets between subsequent frames.
227c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     */
2285c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten
229c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    public long getTimestamp() {
230c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala        return nativeGetTimestamp();
231c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    }
232c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala
233ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    /**
234ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * release() frees all the buffers and puts the SurfaceTexture into the
235ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * 'abandoned' state. Once put in this state the SurfaceTexture can never
236ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * leave it. When in the 'abandoned' state, all methods of the
237ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * ISurfaceTexture interface will fail with the NO_INIT error.
238ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     *
239ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * Note that while calling this method causes all the buffers to be freed
240ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * from the perspective of the the SurfaceTexture, if there are additional
241ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * references on the buffers (e.g. if a buffer is referenced by a client or
242ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * by OpenGL ES as a texture) then those buffer will remain allocated.
243ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     *
244ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * Always call this method when you are done with SurfaceTexture. Failing
245ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * to do so may delay resource deallocation for a significant amount of
246ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * time.
247ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     */
248ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    public void release() {
249ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian        nativeRelease();
250ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    }
251ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian
252376590d668e22a918439877b55faf075427b13f3Jamie Gennis    protected void finalize() throws Throwable {
253376590d668e22a918439877b55faf075427b13f3Jamie Gennis        try {
254376590d668e22a918439877b55faf075427b13f3Jamie Gennis            nativeFinalize();
255376590d668e22a918439877b55faf075427b13f3Jamie Gennis        } finally {
256376590d668e22a918439877b55faf075427b13f3Jamie Gennis            super.finalize();
257376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
258376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
259376590d668e22a918439877b55faf075427b13f3Jamie Gennis
260376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private class EventHandler extends Handler {
261376590d668e22a918439877b55faf075427b13f3Jamie Gennis        public EventHandler(Looper looper) {
262376590d668e22a918439877b55faf075427b13f3Jamie Gennis            super(looper);
263376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
264376590d668e22a918439877b55faf075427b13f3Jamie Gennis
265376590d668e22a918439877b55faf075427b13f3Jamie Gennis        @Override
266376590d668e22a918439877b55faf075427b13f3Jamie Gennis        public void handleMessage(Message msg) {
267376590d668e22a918439877b55faf075427b13f3Jamie Gennis            if (mOnFrameAvailableListener != null) {
268376590d668e22a918439877b55faf075427b13f3Jamie Gennis                mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this);
269376590d668e22a918439877b55faf075427b13f3Jamie Gennis            }
270376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
271376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
272376590d668e22a918439877b55faf075427b13f3Jamie Gennis
2738f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    /**
2748f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy     * This method is invoked from native code only.
2758f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy     */
2768f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @SuppressWarnings({"UnusedDeclaration"})
277376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private static void postEventFromNative(Object selfRef) {
278376590d668e22a918439877b55faf075427b13f3Jamie Gennis        WeakReference weakSelf = (WeakReference)selfRef;
279376590d668e22a918439877b55faf075427b13f3Jamie Gennis        SurfaceTexture st = (SurfaceTexture)weakSelf.get();
280376590d668e22a918439877b55faf075427b13f3Jamie Gennis        if (st == null) {
281376590d668e22a918439877b55faf075427b13f3Jamie Gennis            return;
282376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
283376590d668e22a918439877b55faf075427b13f3Jamie Gennis
284376590d668e22a918439877b55faf075427b13f3Jamie Gennis        if (st.mEventHandler != null) {
285376590d668e22a918439877b55faf075427b13f3Jamie Gennis            Message m = st.mEventHandler.obtainMessage();
286376590d668e22a918439877b55faf075427b13f3Jamie Gennis            st.mEventHandler.sendMessage(m);
287376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
288376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
289b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
2900904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba    private native void nativeInit(int texName, Object weakSelf, boolean allowSynchronousMode);
291376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeFinalize();
292376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeGetTransformMatrix(float[] mtx);
293c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    private native long nativeGetTimestamp();
294050316184b01c0d1a01c46afae7429b89a27c31btedbo    private native void nativeSetDefaultBufferSize(int width, int height);
295b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian    private native int nativeUpdateTexImage();
296c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    private native int nativeDetachFromGLContext();
297c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    private native int nativeAttachToGLContext(int texName);
298925bcaabde5a21687b51caa7ab329310a819f068Grace Kloba    private native int nativeGetQueuedCount();
299ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    private native void nativeRelease();
3006714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
3016714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /*
3026714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * We use a class initializer to allow the native code to cache some
3036714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * field offsets.
3046714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
3056714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    private static native void nativeClassInit();
3066714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    static { nativeClassInit(); }
3076714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis}
308