SurfaceTexture.java revision c99db2bc460cc795947d99076da380e22a21e493
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 /** 69c99db2bc460cc795947d99076da380e22a21e493Igor Murashkin * These fields are used by native code, do not access or modify. 708f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy */ 71e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy private int mSurfaceTexture; 72c99db2bc460cc795947d99076da380e22a21e493Igor Murashkin private int mFrameAvailableListener; 736714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 746714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 756714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Callback interface for being notified that a new stream frame is available. 766714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 776714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public interface OnFrameAvailableListener { 786714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis void onFrameAvailable(SurfaceTexture surfaceTexture); 796714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 806714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 816714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 826714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Exception thrown when a surface couldn't be created or resized 836714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 846714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public static class OutOfResourcesException extends Exception { 856714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public OutOfResourcesException() { 866714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 876714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public OutOfResourcesException(String name) { 886714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis super(name); 896714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 906714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 916714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 926714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 936714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Construct a new SurfaceTexture to stream images to a given OpenGL texture. 946714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * 956714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * @param texName the OpenGL texture object name (e.g. generated via glGenTextures) 966714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 976714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public SurfaceTexture(int texName) { 98554366d158a0ec330a339f4343fb0a3164257f1eJamie Gennis this(texName, false); 990904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba } 1000904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba 1010904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba /** 1020904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba * Construct a new SurfaceTexture to stream images to a given OpenGL texture. 1030904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba * 1040904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba * @param texName the OpenGL texture object name (e.g. generated via glGenTextures) 1050904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba * @param allowSynchronousMode whether the SurfaceTexture can run in the synchronous mode. 1060904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba * When the image stream comes from OpenGL, SurfaceTexture may run in the synchronous 1070904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba * mode where the producer side may be blocked to avoid skipping frames. To avoid the 1080904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba * thread block, set allowSynchronousMode to false. 109554366d158a0ec330a339f4343fb0a3164257f1eJamie Gennis * 110554366d158a0ec330a339f4343fb0a3164257f1eJamie Gennis * @hide 1110904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba */ 1120904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba public SurfaceTexture(int texName, boolean allowSynchronousMode) { 113376590d668e22a918439877b55faf075427b13f3Jamie Gennis Looper looper; 114376590d668e22a918439877b55faf075427b13f3Jamie Gennis if ((looper = Looper.myLooper()) != null) { 115376590d668e22a918439877b55faf075427b13f3Jamie Gennis mEventHandler = new EventHandler(looper); 116376590d668e22a918439877b55faf075427b13f3Jamie Gennis } else if ((looper = Looper.getMainLooper()) != null) { 117376590d668e22a918439877b55faf075427b13f3Jamie Gennis mEventHandler = new EventHandler(looper); 118376590d668e22a918439877b55faf075427b13f3Jamie Gennis } else { 119376590d668e22a918439877b55faf075427b13f3Jamie Gennis mEventHandler = null; 120376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 1210904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba nativeInit(texName, new WeakReference<SurfaceTexture>(this), allowSynchronousMode); 1226714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 1236714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 1246714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 1256714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Register a callback to be invoked when a new image frame becomes available to the 1266714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * SurfaceTexture. Note that this callback may be called on an arbitrary thread, so it is not 1276714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * safe to call {@link #updateTexImage} without first binding the OpenGL ES context to the 1286714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * thread invoking the callback. 1296714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 1306714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public void setOnFrameAvailableListener(OnFrameAvailableListener l) { 131376590d668e22a918439877b55faf075427b13f3Jamie Gennis mOnFrameAvailableListener = l; 1326714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 1336714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 1346714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 1352aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * Set the default size of the image buffers. The image producer may override the buffer size, 1362aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * in which case the producer-set buffer size will be used, not the default size set by this 1372aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * method. Both video and camera based image producers do override the size. This method may 1382aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * be used to set the image size when producing images with {@link android.graphics.Canvas} (via 1392aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * {@link android.view.Surface#lockCanvas}), or OpenGL ES (via an EGLSurface). 140050316184b01c0d1a01c46afae7429b89a27c31btedbo * 1412aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * The new default buffer size will take effect the next time the image producer requests a 1422aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * buffer to fill. For {@link android.graphics.Canvas} this will be the next time {@link 1432aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * android.view.Surface#lockCanvas} is called. For OpenGL ES, the EGLSurface should be 1442aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * destroyed (via eglDestroySurface), made not-current (via eglMakeCurrent), and then recreated 1452aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * (via eglCreateWindowSurface) to ensure that the new default size has taken effect. 146b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian * 147b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian * The width and height parameters must be no greater than the minimum of 148b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian * GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see 149b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian * {@link javax.microedition.khronos.opengles.GL10#glGetIntegerv glGetIntegerv}). 150b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian * An error due to invalid dimensions might not be reported until 151b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian * updateTexImage() is called. 152050316184b01c0d1a01c46afae7429b89a27c31btedbo */ 153050316184b01c0d1a01c46afae7429b89a27c31btedbo public void setDefaultBufferSize(int width, int height) { 154050316184b01c0d1a01c46afae7429b89a27c31btedbo nativeSetDefaultBufferSize(width, height); 155050316184b01c0d1a01c46afae7429b89a27c31btedbo } 156050316184b01c0d1a01c46afae7429b89a27c31btedbo 157050316184b01c0d1a01c46afae7429b89a27c31btedbo /** 1586714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Update the texture image to the most recent frame from the image stream. This may only be 1592b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * called while the OpenGL ES context that owns the texture is current on the calling thread. 1602b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * It will implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target. 1616714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 162376590d668e22a918439877b55faf075427b13f3Jamie Gennis public void updateTexImage() { 16333efb231cb92065c40c019319adae36abc413863Jamie Gennis nativeUpdateTexImage(); 164c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis } 165c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis 166c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis /** 1672b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * Detach the SurfaceTexture from the OpenGL ES context that owns the OpenGL ES texture object. 1682b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * This call must be made with the OpenGL ES context current on the calling thread. The OpenGL 1692b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * ES texture object will be deleted as a result of this call. After calling this method all 1702b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * calls to {@link #updateTexImage} will throw an {@link java.lang.IllegalStateException} until 1712b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * a successful call to {@link #attachToGLContext} is made. 1722b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * 1732b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * This can be used to access the SurfaceTexture image contents from multiple OpenGL ES 1742b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * contexts. Note, however, that the image contents are only accessible from one OpenGL ES 1752b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * context at a time. 176c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis */ 177c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis public void detachFromGLContext() { 178c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis int err = nativeDetachFromGLContext(); 179c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis if (err != 0) { 180c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis throw new RuntimeException("Error during detachFromGLContext (see logcat for details)"); 181c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis } 182c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis } 183c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis 184c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis /** 1852b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * Attach the SurfaceTexture to the OpenGL ES context that is current on the calling thread. A 1862b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * new OpenGL ES texture object is created and populated with the SurfaceTexture image frame 1872b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * that was current at the time of the last call to {@link #detachFromGLContext}. This new 1882b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * texture is bound to the GL_TEXTURE_EXTERNAL_OES texture target. 1892b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * 1902b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * This can be used to access the SurfaceTexture image contents from multiple OpenGL ES 1912b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * contexts. Note, however, that the image contents are only accessible from one OpenGL ES 1922b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * context at a time. 1932b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * 1942b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * @param texName The name of the OpenGL ES texture that will be created. This texture name 1952b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * must be unusued in the OpenGL ES context that is current on the calling thread. 196c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis */ 197c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis public void attachToGLContext(int texName) { 198c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis int err = nativeAttachToGLContext(texName); 199c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis if (err != 0) { 200c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis throw new RuntimeException("Error during detachFromGLContext (see logcat for details)"); 201b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian } 202376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 203b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 204b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis /** 205b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by 206b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * the most recent call to updateTexImage. 207b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 208b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s 209b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample 210b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * that location from the texture. Sampling the texture outside of the range of this transform 211b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * is undefined. 212b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 213b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via 214b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * the glLoadMatrixf or glUniformMatrix4fv functions. 215b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 216b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * @param mtx the array into which the 4x4 matrix will be stored. The array must have exactly 217b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 16 elements. 218b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis */ 219b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis public void getTransformMatrix(float[] mtx) { 220cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten // Note we intentionally don't check mtx for null, so this will result in a 221cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten // NullPointerException. But it's safe because it happens before the call to native. 222b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis if (mtx.length != 16) { 223b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis throw new IllegalArgumentException(); 224b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis } 225376590d668e22a918439877b55faf075427b13f3Jamie Gennis nativeGetTransformMatrix(mtx); 226b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis } 227b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 228c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala /** 229c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * Retrieve the timestamp associated with the texture image set by the most recent call to 230c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * updateTexImage. 231c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * 2325c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * This timestamp is in nanoseconds, and is normally monotonically increasing. The timestamp 2335c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * should be unaffected by time-of-day adjustments, and for a camera should be strictly 2345c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * monotonic but for a MediaPlayer may be reset when the position is set. The 235c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * specific meaning and zero point of the timestamp depends on the source providing images to 236c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * the SurfaceTexture. Unless otherwise specified by the image source, timestamps cannot 237c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * generally be compared across SurfaceTexture instances, or across multiple program 238c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * invocations. It is mostly useful for determining time offsets between subsequent frames. 239c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala */ 2405c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten 241c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala public long getTimestamp() { 242c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala return nativeGetTimestamp(); 243c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala } 244c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala 245ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian /** 246ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * release() frees all the buffers and puts the SurfaceTexture into the 247ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 'abandoned' state. Once put in this state the SurfaceTexture can never 248ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * leave it. When in the 'abandoned' state, all methods of the 249ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * ISurfaceTexture interface will fail with the NO_INIT error. 250ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 251ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * Note that while calling this method causes all the buffers to be freed 252ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * from the perspective of the the SurfaceTexture, if there are additional 253ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * references on the buffers (e.g. if a buffer is referenced by a client or 254ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * by OpenGL ES as a texture) then those buffer will remain allocated. 255ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 256ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * Always call this method when you are done with SurfaceTexture. Failing 257ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * to do so may delay resource deallocation for a significant amount of 258ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * time. 259ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian */ 260ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian public void release() { 261ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian nativeRelease(); 262ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian } 263ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian 264376590d668e22a918439877b55faf075427b13f3Jamie Gennis protected void finalize() throws Throwable { 265376590d668e22a918439877b55faf075427b13f3Jamie Gennis try { 266376590d668e22a918439877b55faf075427b13f3Jamie Gennis nativeFinalize(); 267376590d668e22a918439877b55faf075427b13f3Jamie Gennis } finally { 268376590d668e22a918439877b55faf075427b13f3Jamie Gennis super.finalize(); 269376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 270376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 271376590d668e22a918439877b55faf075427b13f3Jamie Gennis 272376590d668e22a918439877b55faf075427b13f3Jamie Gennis private class EventHandler extends Handler { 273376590d668e22a918439877b55faf075427b13f3Jamie Gennis public EventHandler(Looper looper) { 274376590d668e22a918439877b55faf075427b13f3Jamie Gennis super(looper); 275376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 276376590d668e22a918439877b55faf075427b13f3Jamie Gennis 277376590d668e22a918439877b55faf075427b13f3Jamie Gennis @Override 278376590d668e22a918439877b55faf075427b13f3Jamie Gennis public void handleMessage(Message msg) { 279376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (mOnFrameAvailableListener != null) { 280376590d668e22a918439877b55faf075427b13f3Jamie Gennis mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this); 281376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 282376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 283376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 284376590d668e22a918439877b55faf075427b13f3Jamie Gennis 2858f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy /** 2868f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * This method is invoked from native code only. 2878f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy */ 2888f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @SuppressWarnings({"UnusedDeclaration"}) 289376590d668e22a918439877b55faf075427b13f3Jamie Gennis private static void postEventFromNative(Object selfRef) { 290376590d668e22a918439877b55faf075427b13f3Jamie Gennis WeakReference weakSelf = (WeakReference)selfRef; 291376590d668e22a918439877b55faf075427b13f3Jamie Gennis SurfaceTexture st = (SurfaceTexture)weakSelf.get(); 292376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (st == null) { 293376590d668e22a918439877b55faf075427b13f3Jamie Gennis return; 294376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 295376590d668e22a918439877b55faf075427b13f3Jamie Gennis 296376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (st.mEventHandler != null) { 297376590d668e22a918439877b55faf075427b13f3Jamie Gennis Message m = st.mEventHandler.obtainMessage(); 298376590d668e22a918439877b55faf075427b13f3Jamie Gennis st.mEventHandler.sendMessage(m); 299376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 300376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 301b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 3020904d0af81e8a0a5404d6c03f4dcea02bea8170dGrace Kloba private native void nativeInit(int texName, Object weakSelf, boolean allowSynchronousMode); 303376590d668e22a918439877b55faf075427b13f3Jamie Gennis private native void nativeFinalize(); 304376590d668e22a918439877b55faf075427b13f3Jamie Gennis private native void nativeGetTransformMatrix(float[] mtx); 305c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala private native long nativeGetTimestamp(); 306050316184b01c0d1a01c46afae7429b89a27c31btedbo private native void nativeSetDefaultBufferSize(int width, int height); 3072b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis private native void nativeUpdateTexImage(); 308c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis private native int nativeDetachFromGLContext(); 309c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis private native int nativeAttachToGLContext(int texName); 310925bcaabde5a21687b51caa7ab329310a819f068Grace Kloba private native int nativeGetQueuedCount(); 311ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian private native void nativeRelease(); 3126714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 3136714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /* 3146714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * We use a class initializer to allow the native code to cache some 3156714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * field offsets. 3166714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 3176714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis private static native void nativeClassInit(); 3186714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis static { nativeClassInit(); } 3196714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis} 320