SurfaceTexture.java revision b89d88f531ee39927f8f554baaae5ecc9101ba9d
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) { 164b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian throw new RuntimeException("Error during updateTexImage (see logs)"); 165b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian } 166376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 167b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 168b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis /** 169b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by 170b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * the most recent call to updateTexImage. 171b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 172b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s 173b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample 174b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * that location from the texture. Sampling the texture outside of the range of this transform 175b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * is undefined. 176b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 177b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via 178b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * the glLoadMatrixf or glUniformMatrix4fv functions. 179b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 180b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * @param mtx the array into which the 4x4 matrix will be stored. The array must have exactly 181b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 16 elements. 182b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis */ 183b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis public void getTransformMatrix(float[] mtx) { 184cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten // Note we intentionally don't check mtx for null, so this will result in a 185cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten // NullPointerException. But it's safe because it happens before the call to native. 186b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis if (mtx.length != 16) { 187b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis throw new IllegalArgumentException(); 188b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis } 189376590d668e22a918439877b55faf075427b13f3Jamie Gennis nativeGetTransformMatrix(mtx); 190b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis } 191b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 192c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala /** 193c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * Retrieve the timestamp associated with the texture image set by the most recent call to 194c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * updateTexImage. 195c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * 1965c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * This timestamp is in nanoseconds, and is normally monotonically increasing. The timestamp 1975c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * should be unaffected by time-of-day adjustments, and for a camera should be strictly 1985c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * monotonic but for a MediaPlayer may be reset when the position is set. The 199c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * specific meaning and zero point of the timestamp depends on the source providing images to 200c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * the SurfaceTexture. Unless otherwise specified by the image source, timestamps cannot 201c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * generally be compared across SurfaceTexture instances, or across multiple program 202c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * invocations. It is mostly useful for determining time offsets between subsequent frames. 203c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala */ 2045c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten 205c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala public long getTimestamp() { 206c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala return nativeGetTimestamp(); 207c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala } 208c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala 209ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian /** 210ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * release() frees all the buffers and puts the SurfaceTexture into the 211ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 'abandoned' state. Once put in this state the SurfaceTexture can never 212ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * leave it. When in the 'abandoned' state, all methods of the 213ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * ISurfaceTexture interface will fail with the NO_INIT error. 214ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 215ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * Note that while calling this method causes all the buffers to be freed 216ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * from the perspective of the the SurfaceTexture, if there are additional 217ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * references on the buffers (e.g. if a buffer is referenced by a client or 218ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * by OpenGL ES as a texture) then those buffer will remain allocated. 219ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 220ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * Always call this method when you are done with SurfaceTexture. Failing 221ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * to do so may delay resource deallocation for a significant amount of 222ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * time. 223ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian */ 224ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian public void release() { 225ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian nativeRelease(); 226ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian } 227ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian 228376590d668e22a918439877b55faf075427b13f3Jamie Gennis protected void finalize() throws Throwable { 229376590d668e22a918439877b55faf075427b13f3Jamie Gennis try { 230376590d668e22a918439877b55faf075427b13f3Jamie Gennis nativeFinalize(); 231376590d668e22a918439877b55faf075427b13f3Jamie Gennis } finally { 232376590d668e22a918439877b55faf075427b13f3Jamie Gennis super.finalize(); 233376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 234376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 235376590d668e22a918439877b55faf075427b13f3Jamie Gennis 236376590d668e22a918439877b55faf075427b13f3Jamie Gennis private class EventHandler extends Handler { 237376590d668e22a918439877b55faf075427b13f3Jamie Gennis public EventHandler(Looper looper) { 238376590d668e22a918439877b55faf075427b13f3Jamie Gennis super(looper); 239376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 240376590d668e22a918439877b55faf075427b13f3Jamie Gennis 241376590d668e22a918439877b55faf075427b13f3Jamie Gennis @Override 242376590d668e22a918439877b55faf075427b13f3Jamie Gennis public void handleMessage(Message msg) { 243376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (mOnFrameAvailableListener != null) { 244376590d668e22a918439877b55faf075427b13f3Jamie Gennis mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this); 245376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 246376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 247376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 248376590d668e22a918439877b55faf075427b13f3Jamie Gennis 2498f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy /** 2508f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * This method is invoked from native code only. 2518f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy */ 2528f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @SuppressWarnings({"UnusedDeclaration"}) 253376590d668e22a918439877b55faf075427b13f3Jamie Gennis private static void postEventFromNative(Object selfRef) { 254376590d668e22a918439877b55faf075427b13f3Jamie Gennis WeakReference weakSelf = (WeakReference)selfRef; 255376590d668e22a918439877b55faf075427b13f3Jamie Gennis SurfaceTexture st = (SurfaceTexture)weakSelf.get(); 256376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (st == null) { 257376590d668e22a918439877b55faf075427b13f3Jamie Gennis return; 258376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 259376590d668e22a918439877b55faf075427b13f3Jamie Gennis 260376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (st.mEventHandler != null) { 261376590d668e22a918439877b55faf075427b13f3Jamie Gennis Message m = st.mEventHandler.obtainMessage(); 262376590d668e22a918439877b55faf075427b13f3Jamie Gennis st.mEventHandler.sendMessage(m); 263376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 264376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 265b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 2660904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba private native void nativeInit(int texName, Object weakSelf, boolean allowSynchronousMode); 267376590d668e22a918439877b55faf075427b13f3Jamie Gennis private native void nativeFinalize(); 268376590d668e22a918439877b55faf075427b13f3Jamie Gennis private native void nativeGetTransformMatrix(float[] mtx); 269c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala private native long nativeGetTimestamp(); 270050316184b01c0d1a01c46afae7429b89a27c31btedbo private native void nativeSetDefaultBufferSize(int width, int height); 271b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian private native int nativeUpdateTexImage(); 272925bcaabde5a21687b51caa7ab329310a819f068Grace Kloba private native int nativeGetQueuedCount(); 273ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian private native void nativeRelease(); 2746714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 2756714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /* 2766714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * We use a class initializer to allow the native code to cache some 2776714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * field offsets. 2786714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 2796714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis private static native void nativeClassInit(); 2806714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis static { nativeClassInit(); } 2816714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis} 282