SurfaceTexture.java revision 2aafe742e5d2d63d77c49df032ec580966661597
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) { 96554366d158a0ec330a339f4343fb0a3164257f1eJamie Gennis this(texName, false); 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. 107554366d158a0ec330a339f4343fb0a3164257f1eJamie Gennis * 108554366d158a0ec330a339f4343fb0a3164257f1eJamie Gennis * @hide 1090904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba */ 1100904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba public SurfaceTexture(int texName, boolean allowSynchronousMode) { 111376590d668e22a918439877b55faf075427b13f3Jamie Gennis Looper looper; 112376590d668e22a918439877b55faf075427b13f3Jamie Gennis if ((looper = Looper.myLooper()) != null) { 113376590d668e22a918439877b55faf075427b13f3Jamie Gennis mEventHandler = new EventHandler(looper); 114376590d668e22a918439877b55faf075427b13f3Jamie Gennis } else if ((looper = Looper.getMainLooper()) != null) { 115376590d668e22a918439877b55faf075427b13f3Jamie Gennis mEventHandler = new EventHandler(looper); 116376590d668e22a918439877b55faf075427b13f3Jamie Gennis } else { 117376590d668e22a918439877b55faf075427b13f3Jamie Gennis mEventHandler = null; 118376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 1190904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba nativeInit(texName, new WeakReference<SurfaceTexture>(this), allowSynchronousMode); 1206714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 1216714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 1226714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 1236714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Register a callback to be invoked when a new image frame becomes available to the 1246714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * SurfaceTexture. Note that this callback may be called on an arbitrary thread, so it is not 1256714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * safe to call {@link #updateTexImage} without first binding the OpenGL ES context to the 1266714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * thread invoking the callback. 1276714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 1286714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public void setOnFrameAvailableListener(OnFrameAvailableListener l) { 129376590d668e22a918439877b55faf075427b13f3Jamie Gennis mOnFrameAvailableListener = l; 1306714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 1316714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 1326714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 1332aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * Set the default size of the image buffers. The image producer may override the buffer size, 1342aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * in which case the producer-set buffer size will be used, not the default size set by this 1352aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * method. Both video and camera based image producers do override the size. This method may 1362aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * be used to set the image size when producing images with {@link android.graphics.Canvas} (via 1372aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * {@link android.view.Surface#lockCanvas}), or OpenGL ES (via an EGLSurface). 138050316184b01c0d1a01c46afae7429b89a27c31btedbo * 1392aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * The new default buffer size will take effect the next time the image producer requests a 1402aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * buffer to fill. For {@link android.graphics.Canvas} this will be the next time {@link 1412aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * android.view.Surface#lockCanvas} is called. For OpenGL ES, the EGLSurface should be 1422aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * destroyed (via eglDestroySurface), made not-current (via eglMakeCurrent), and then recreated 1432aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * (via eglCreateWindowSurface) to ensure that the new default size has taken effect. 144050316184b01c0d1a01c46afae7429b89a27c31btedbo */ 145050316184b01c0d1a01c46afae7429b89a27c31btedbo public void setDefaultBufferSize(int width, int height) { 146050316184b01c0d1a01c46afae7429b89a27c31btedbo nativeSetDefaultBufferSize(width, height); 147050316184b01c0d1a01c46afae7429b89a27c31btedbo } 148050316184b01c0d1a01c46afae7429b89a27c31btedbo 149050316184b01c0d1a01c46afae7429b89a27c31btedbo /** 1506714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Update the texture image to the most recent frame from the image stream. This may only be 1516714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * called while the OpenGL ES context that owns the texture is bound to the thread. It will 1526714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target. 1536714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 154376590d668e22a918439877b55faf075427b13f3Jamie Gennis public void updateTexImage() { 155376590d668e22a918439877b55faf075427b13f3Jamie Gennis nativeUpdateTexImage(); 156376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 157b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 158b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis /** 159b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by 160b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * the most recent call to updateTexImage. 161b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 162b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s 163b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample 164b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * that location from the texture. Sampling the texture outside of the range of this transform 165b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * is undefined. 166b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 167b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via 168b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * the glLoadMatrixf or glUniformMatrix4fv functions. 169b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 170b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * @param mtx the array into which the 4x4 matrix will be stored. The array must have exactly 171b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 16 elements. 172b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis */ 173b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis public void getTransformMatrix(float[] mtx) { 174cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten // Note we intentionally don't check mtx for null, so this will result in a 175cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten // NullPointerException. But it's safe because it happens before the call to native. 176b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis if (mtx.length != 16) { 177b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis throw new IllegalArgumentException(); 178b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis } 179376590d668e22a918439877b55faf075427b13f3Jamie Gennis nativeGetTransformMatrix(mtx); 180b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis } 181b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 182c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala /** 183c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * Retrieve the timestamp associated with the texture image set by the most recent call to 184c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * updateTexImage. 185c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * 1865c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * This timestamp is in nanoseconds, and is normally monotonically increasing. The timestamp 1875c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * should be unaffected by time-of-day adjustments, and for a camera should be strictly 1885c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * monotonic but for a MediaPlayer may be reset when the position is set. The 189c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * specific meaning and zero point of the timestamp depends on the source providing images to 190c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * the SurfaceTexture. Unless otherwise specified by the image source, timestamps cannot 191c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * generally be compared across SurfaceTexture instances, or across multiple program 192c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * invocations. It is mostly useful for determining time offsets between subsequent frames. 193c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala */ 1945c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten 195c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala public long getTimestamp() { 196c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala return nativeGetTimestamp(); 197c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala } 198c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala 199ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian /** 200ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * release() frees all the buffers and puts the SurfaceTexture into the 201ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 'abandoned' state. Once put in this state the SurfaceTexture can never 202ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * leave it. When in the 'abandoned' state, all methods of the 203ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * ISurfaceTexture interface will fail with the NO_INIT error. 204ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 205ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * Note that while calling this method causes all the buffers to be freed 206ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * from the perspective of the the SurfaceTexture, if there are additional 207ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * references on the buffers (e.g. if a buffer is referenced by a client or 208ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * by OpenGL ES as a texture) then those buffer will remain allocated. 209ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 210ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * Always call this method when you are done with SurfaceTexture. Failing 211ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * to do so may delay resource deallocation for a significant amount of 212ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * time. 213ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian */ 214ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian public void release() { 215ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian nativeRelease(); 216ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian } 217ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian 218376590d668e22a918439877b55faf075427b13f3Jamie Gennis protected void finalize() throws Throwable { 219376590d668e22a918439877b55faf075427b13f3Jamie Gennis try { 220376590d668e22a918439877b55faf075427b13f3Jamie Gennis nativeFinalize(); 221376590d668e22a918439877b55faf075427b13f3Jamie Gennis } finally { 222376590d668e22a918439877b55faf075427b13f3Jamie Gennis super.finalize(); 223376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 224376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 225376590d668e22a918439877b55faf075427b13f3Jamie Gennis 226376590d668e22a918439877b55faf075427b13f3Jamie Gennis private class EventHandler extends Handler { 227376590d668e22a918439877b55faf075427b13f3Jamie Gennis public EventHandler(Looper looper) { 228376590d668e22a918439877b55faf075427b13f3Jamie Gennis super(looper); 229376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 230376590d668e22a918439877b55faf075427b13f3Jamie Gennis 231376590d668e22a918439877b55faf075427b13f3Jamie Gennis @Override 232376590d668e22a918439877b55faf075427b13f3Jamie Gennis public void handleMessage(Message msg) { 233376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (mOnFrameAvailableListener != null) { 234376590d668e22a918439877b55faf075427b13f3Jamie Gennis mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this); 235376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 236376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 237376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 238376590d668e22a918439877b55faf075427b13f3Jamie Gennis 2398f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy /** 2408f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * This method is invoked from native code only. 2418f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy */ 2428f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @SuppressWarnings({"UnusedDeclaration"}) 243376590d668e22a918439877b55faf075427b13f3Jamie Gennis private static void postEventFromNative(Object selfRef) { 244376590d668e22a918439877b55faf075427b13f3Jamie Gennis WeakReference weakSelf = (WeakReference)selfRef; 245376590d668e22a918439877b55faf075427b13f3Jamie Gennis SurfaceTexture st = (SurfaceTexture)weakSelf.get(); 246376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (st == null) { 247376590d668e22a918439877b55faf075427b13f3Jamie Gennis return; 248376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 249376590d668e22a918439877b55faf075427b13f3Jamie Gennis 250376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (st.mEventHandler != null) { 251376590d668e22a918439877b55faf075427b13f3Jamie Gennis Message m = st.mEventHandler.obtainMessage(); 252376590d668e22a918439877b55faf075427b13f3Jamie Gennis st.mEventHandler.sendMessage(m); 253376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 254376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 255b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 2560904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba private native void nativeInit(int texName, Object weakSelf, boolean allowSynchronousMode); 257376590d668e22a918439877b55faf075427b13f3Jamie Gennis private native void nativeFinalize(); 258376590d668e22a918439877b55faf075427b13f3Jamie Gennis private native void nativeGetTransformMatrix(float[] mtx); 259c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala private native long nativeGetTimestamp(); 260050316184b01c0d1a01c46afae7429b89a27c31btedbo private native void nativeSetDefaultBufferSize(int width, int height); 261376590d668e22a918439877b55faf075427b13f3Jamie Gennis private native void nativeUpdateTexImage(); 262925bcaabde5a21687b51caa7ab329310a819f068Grace Kloba private native int nativeGetQueuedCount(); 263ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian private native void nativeRelease(); 2646714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 2656714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /* 2666714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * We use a class initializer to allow the native code to cache some 2676714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * field offsets. 2686714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 2696714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis private static native void nativeClassInit(); 2706714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis static { nativeClassInit(); } 2716714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis} 272