SurfaceTexture.java revision 72aa313ff4c91e7b2aae3d37067f9201b2b0fdbe
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; 24a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkinimport android.view.Surface; 25376590d668e22a918439877b55faf075427b13f3Jamie Gennis 266714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis/** 276714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Captures frames from an image stream as an OpenGL ES texture. 286714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * 29cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * <p>The image stream may come from either camera preview or video decode. A SurfaceTexture 30cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * may be used in place of a SurfaceHolder when specifying the output destination of a 31cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * {@link android.hardware.Camera} or {@link android.media.MediaPlayer} 3237cec0fc50760fe89614863eded14011f9412534Jamie Gennis * object. Doing so will cause all the frames from the image stream to be sent to the 336714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * SurfaceTexture object rather than to the device's display. When {@link #updateTexImage} is 34cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * called, the contents of the texture object specified when the SurfaceTexture was created are 356714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * updated to contain the most recent image from the image stream. This may cause some frames of 366714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * the stream to be skipped. 376714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * 385f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * <p>When sampling from the texture one should first transform the texture coordinates using the 398f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * matrix queried via {@link #getTransformMatrix(float[])}. The transform matrix may change each 408f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * time {@link #updateTexImage} is called, so it should be re-queried each time the texture image 418f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * is updated. 425f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * This matrix transforms traditional 2D OpenGL ES texture coordinate column vectors of the form (s, 435f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * t, 0, 1) where s and t are on the inclusive interval [0, 1] to the proper sampling location in 445f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * the streamed texture. This transform compensates for any properties of the image stream source 455f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * that cause it to appear different from a traditional OpenGL ES texture. For example, sampling 465f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * from the bottom left corner of the image can be accomplished by transforming the column vector 475f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * (0, 0, 0, 1) using the queried matrix, while sampling from the top right corner of the image can 485f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * be done by transforming (1, 1, 0, 1). 495f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * 506714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * <p>The texture object uses the GL_TEXTURE_EXTERNAL_OES texture target, which is defined by the 51858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * <a href="http://www.khronos.org/registry/gles/extensions/OES/OES_EGL_image_external.txt"> 52858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * GL_OES_EGL_image_external</a> OpenGL ES extension. This limits how the texture may be used. 53858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * Each time the texture is bound it must be bound to the GL_TEXTURE_EXTERNAL_OES target rather than 54858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * the GL_TEXTURE_2D target. Additionally, any OpenGL ES 2.0 shader that samples from the texture 55858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * must declare its use of this extension using, for example, an "#extension 56858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * GL_OES_EGL_image_external : require" directive. Such shaders must also access the texture using 57858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * the samplerExternalOES GLSL sampler type. 5837cec0fc50760fe89614863eded14011f9412534Jamie Gennis * 5937cec0fc50760fe89614863eded14011f9412534Jamie Gennis * <p>SurfaceTexture objects may be created on any thread. {@link #updateTexImage} may only be 6037cec0fc50760fe89614863eded14011f9412534Jamie Gennis * called on the thread with the OpenGL ES context that contains the texture object. The 6137cec0fc50760fe89614863eded14011f9412534Jamie Gennis * frame-available callback is called on an arbitrary thread, so unless special care is taken {@link 6237cec0fc50760fe89614863eded14011f9412534Jamie Gennis * #updateTexImage} should not be called directly from the callback. 636714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 646714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennispublic class SurfaceTexture { 656714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 66376590d668e22a918439877b55faf075427b13f3Jamie Gennis private EventHandler mEventHandler; 67376590d668e22a918439877b55faf075427b13f3Jamie Gennis private OnFrameAvailableListener mOnFrameAvailableListener; 68376590d668e22a918439877b55faf075427b13f3Jamie Gennis 698f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy /** 70c99db2bc460cc795947d99076da380e22a21e493Igor Murashkin * These fields are used by native code, do not access or modify. 718f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy */ 7272aa313ff4c91e7b2aae3d37067f9201b2b0fdbeAshok Bhat private long mSurfaceTexture; 7372aa313ff4c91e7b2aae3d37067f9201b2b0fdbeAshok Bhat private long mBufferQueue; 7472aa313ff4c91e7b2aae3d37067f9201b2b0fdbeAshok Bhat private long mFrameAvailableListener; 756714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 766714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 776714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Callback interface for being notified that a new stream frame is available. 786714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 796714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public interface OnFrameAvailableListener { 806714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis void onFrameAvailable(SurfaceTexture surfaceTexture); 816714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 826714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 836714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 84a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin * Exception thrown when a SurfaceTexture couldn't be created or resized. 85a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin * 86a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin * @deprecated No longer thrown. {@link Surface.OutOfResourcesException} is used instead. 876714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 88a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin @SuppressWarnings("serial") 89a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin @Deprecated 906714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public static class OutOfResourcesException extends Exception { 916714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public OutOfResourcesException() { 926714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 936714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public OutOfResourcesException(String name) { 946714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis super(name); 956714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 966714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 976714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 986714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 996714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Construct a new SurfaceTexture to stream images to a given OpenGL texture. 1006714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * 1016714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * @param texName the OpenGL texture object name (e.g. generated via glGenTextures) 102a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin * 103a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin * @throws OutOfResourcesException If the SurfaceTexture cannot be created. 1046714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 1056714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public SurfaceTexture(int texName) { 106e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian init(texName, false); 107e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian } 108e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian 109e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian /** 110e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * Construct a new SurfaceTexture to stream images to a given OpenGL texture. 111e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * 112e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * In single buffered mode the application is responsible for serializing access to the image 113e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * content buffer. Each time the image content is to be updated, the 114e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * {@link #releaseTexImage()} method must be called before the image content producer takes 115e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * ownership of the buffer. For example, when producing image content with the NDK 116e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * ANativeWindow_lock and ANativeWindow_unlockAndPost functions, {@link #releaseTexImage()} 117e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * must be called before each ANativeWindow_lock, or that call will fail. When producing 118e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * image content with OpenGL ES, {@link #releaseTexImage()} must be called before the first 119e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * OpenGL ES function call each frame. 120e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * 121e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * @param texName the OpenGL texture object name (e.g. generated via glGenTextures) 122e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * @param singleBufferMode whether the SurfaceTexture will be in single buffered mode. 123a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin * 124a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin * @throws throws OutOfResourcesException If the SurfaceTexture cannot be created. 125e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian */ 126e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian public SurfaceTexture(int texName, boolean singleBufferMode) { 127e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian init(texName, singleBufferMode); 1286714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 1296714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 1306714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 1316714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Register a callback to be invoked when a new image frame becomes available to the 1326714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * SurfaceTexture. Note that this callback may be called on an arbitrary thread, so it is not 1336714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * safe to call {@link #updateTexImage} without first binding the OpenGL ES context to the 1346714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * thread invoking the callback. 1356714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 1366714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis public void setOnFrameAvailableListener(OnFrameAvailableListener l) { 137376590d668e22a918439877b55faf075427b13f3Jamie Gennis mOnFrameAvailableListener = l; 1386714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis } 1396714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 1406714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /** 1412aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * Set the default size of the image buffers. The image producer may override the buffer size, 1422aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * in which case the producer-set buffer size will be used, not the default size set by this 1432aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * method. Both video and camera based image producers do override the size. This method may 1442aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * be used to set the image size when producing images with {@link android.graphics.Canvas} (via 1452aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * {@link android.view.Surface#lockCanvas}), or OpenGL ES (via an EGLSurface). 146050316184b01c0d1a01c46afae7429b89a27c31btedbo * 1472aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * The new default buffer size will take effect the next time the image producer requests a 1482aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * buffer to fill. For {@link android.graphics.Canvas} this will be the next time {@link 1492aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * android.view.Surface#lockCanvas} is called. For OpenGL ES, the EGLSurface should be 1502aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * destroyed (via eglDestroySurface), made not-current (via eglMakeCurrent), and then recreated 1512aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis * (via eglCreateWindowSurface) to ensure that the new default size has taken effect. 152a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin * 153b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian * The width and height parameters must be no greater than the minimum of 154a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin * GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see 155b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian * {@link javax.microedition.khronos.opengles.GL10#glGetIntegerv glGetIntegerv}). 156b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian * An error due to invalid dimensions might not be reported until 157b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian * updateTexImage() is called. 158050316184b01c0d1a01c46afae7429b89a27c31btedbo */ 159050316184b01c0d1a01c46afae7429b89a27c31btedbo public void setDefaultBufferSize(int width, int height) { 160050316184b01c0d1a01c46afae7429b89a27c31btedbo nativeSetDefaultBufferSize(width, height); 161050316184b01c0d1a01c46afae7429b89a27c31btedbo } 162050316184b01c0d1a01c46afae7429b89a27c31btedbo 163050316184b01c0d1a01c46afae7429b89a27c31btedbo /** 1646714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Update the texture image to the most recent frame from the image stream. This may only be 1652b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * called while the OpenGL ES context that owns the texture is current on the calling thread. 1662b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * It will implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target. 1676714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 168376590d668e22a918439877b55faf075427b13f3Jamie Gennis public void updateTexImage() { 16933efb231cb92065c40c019319adae36abc413863Jamie Gennis nativeUpdateTexImage(); 170c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis } 171c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis 172c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis /** 173e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * Releases the the texture content. This is needed in single buffered mode to allow the image 174e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian * content producer to take ownership of the image buffer. 175cb92a8d9428c2e4a9f038cd5f10e9d61bf265cd4Mathias Agopian * For more information see {@link #SurfaceTexture(int, boolean)}. 176e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian */ 177e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian public void releaseTexImage() { 178e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian nativeReleaseTexImage(); 179e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian } 180e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian 181e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian /** 1822b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * Detach the SurfaceTexture from the OpenGL ES context that owns the OpenGL ES texture object. 1832b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * This call must be made with the OpenGL ES context current on the calling thread. The OpenGL 1842b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * ES texture object will be deleted as a result of this call. After calling this method all 1852b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * calls to {@link #updateTexImage} will throw an {@link java.lang.IllegalStateException} until 1862b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * a successful call to {@link #attachToGLContext} is made. 1872b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * 1882b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * This can be used to access the SurfaceTexture image contents from multiple OpenGL ES 1892b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * contexts. Note, however, that the image contents are only accessible from one OpenGL ES 1902b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * context at a time. 191c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis */ 192c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis public void detachFromGLContext() { 193c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis int err = nativeDetachFromGLContext(); 194c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis if (err != 0) { 195c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis throw new RuntimeException("Error during detachFromGLContext (see logcat for details)"); 196c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis } 197c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis } 198c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis 199c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis /** 2002b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * Attach the SurfaceTexture to the OpenGL ES context that is current on the calling thread. A 2012b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * new OpenGL ES texture object is created and populated with the SurfaceTexture image frame 2022b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * that was current at the time of the last call to {@link #detachFromGLContext}. This new 2032b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * texture is bound to the GL_TEXTURE_EXTERNAL_OES texture target. 2042b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * 2052b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * This can be used to access the SurfaceTexture image contents from multiple OpenGL ES 2062b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * contexts. Note, however, that the image contents are only accessible from one OpenGL ES 2072b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * context at a time. 2082b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * 2092b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * @param texName The name of the OpenGL ES texture that will be created. This texture name 2102b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis * must be unusued in the OpenGL ES context that is current on the calling thread. 211c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis */ 212c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis public void attachToGLContext(int texName) { 213c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis int err = nativeAttachToGLContext(texName); 214c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis if (err != 0) { 215425c3da61ad39f5dd4aaba9fd0235f46ff079245Jonathan Dixon throw new RuntimeException("Error during attachToGLContext (see logcat for details)"); 216b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian } 217376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 218b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 219b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis /** 220b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by 221b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * the most recent call to updateTexImage. 222b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 223b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s 224b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample 225b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * that location from the texture. Sampling the texture outside of the range of this transform 226b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * is undefined. 227b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 228b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via 229b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * the glLoadMatrixf or glUniformMatrix4fv functions. 230b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 231b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * @param mtx the array into which the 4x4 matrix will be stored. The array must have exactly 232b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis * 16 elements. 233b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis */ 234b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis public void getTransformMatrix(float[] mtx) { 235cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten // Note we intentionally don't check mtx for null, so this will result in a 236cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten // NullPointerException. But it's safe because it happens before the call to native. 237b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis if (mtx.length != 16) { 238b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis throw new IllegalArgumentException(); 239b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis } 240376590d668e22a918439877b55faf075427b13f3Jamie Gennis nativeGetTransformMatrix(mtx); 241b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis } 242b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 243c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala /** 244c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * Retrieve the timestamp associated with the texture image set by the most recent call to 245c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * updateTexImage. 246c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * 2475c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * This timestamp is in nanoseconds, and is normally monotonically increasing. The timestamp 2485c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * should be unaffected by time-of-day adjustments, and for a camera should be strictly 2495c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten * monotonic but for a MediaPlayer may be reset when the position is set. The 250c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * specific meaning and zero point of the timestamp depends on the source providing images to 251c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * the SurfaceTexture. Unless otherwise specified by the image source, timestamps cannot 252c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * generally be compared across SurfaceTexture instances, or across multiple program 253c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala * invocations. It is mostly useful for determining time offsets between subsequent frames. 254c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala */ 2555c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten 256c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala public long getTimestamp() { 257c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala return nativeGetTimestamp(); 258c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala } 259c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala 260ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian /** 261ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * release() frees all the buffers and puts the SurfaceTexture into the 262ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 'abandoned' state. Once put in this state the SurfaceTexture can never 263ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * leave it. When in the 'abandoned' state, all methods of the 264d47f7d8b5fe3a3861d7cbdc5f912235407823c8eAndy McFadden * IGraphicBufferProducer interface will fail with the NO_INIT error. 265ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 266ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * Note that while calling this method causes all the buffers to be freed 267ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * from the perspective of the the SurfaceTexture, if there are additional 268ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * references on the buffers (e.g. if a buffer is referenced by a client or 269ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * by OpenGL ES as a texture) then those buffer will remain allocated. 270ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * 271ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * Always call this method when you are done with SurfaceTexture. Failing 272ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * to do so may delay resource deallocation for a significant amount of 273ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian * time. 274ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian */ 275ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian public void release() { 276ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian nativeRelease(); 277ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian } 278ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian 279a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin @Override 280376590d668e22a918439877b55faf075427b13f3Jamie Gennis protected void finalize() throws Throwable { 281376590d668e22a918439877b55faf075427b13f3Jamie Gennis try { 282376590d668e22a918439877b55faf075427b13f3Jamie Gennis nativeFinalize(); 283376590d668e22a918439877b55faf075427b13f3Jamie Gennis } finally { 284376590d668e22a918439877b55faf075427b13f3Jamie Gennis super.finalize(); 285376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 286376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 287376590d668e22a918439877b55faf075427b13f3Jamie Gennis 288376590d668e22a918439877b55faf075427b13f3Jamie Gennis private class EventHandler extends Handler { 289376590d668e22a918439877b55faf075427b13f3Jamie Gennis public EventHandler(Looper looper) { 290376590d668e22a918439877b55faf075427b13f3Jamie Gennis super(looper); 291376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 292376590d668e22a918439877b55faf075427b13f3Jamie Gennis 293376590d668e22a918439877b55faf075427b13f3Jamie Gennis @Override 294376590d668e22a918439877b55faf075427b13f3Jamie Gennis public void handleMessage(Message msg) { 295376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (mOnFrameAvailableListener != null) { 296376590d668e22a918439877b55faf075427b13f3Jamie Gennis mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this); 297376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 298376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 299376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 300376590d668e22a918439877b55faf075427b13f3Jamie Gennis 3018f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy /** 3028f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * This method is invoked from native code only. 3038f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy */ 3048f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @SuppressWarnings({"UnusedDeclaration"}) 305376590d668e22a918439877b55faf075427b13f3Jamie Gennis private static void postEventFromNative(Object selfRef) { 306376590d668e22a918439877b55faf075427b13f3Jamie Gennis WeakReference weakSelf = (WeakReference)selfRef; 307376590d668e22a918439877b55faf075427b13f3Jamie Gennis SurfaceTexture st = (SurfaceTexture)weakSelf.get(); 308376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (st == null) { 309376590d668e22a918439877b55faf075427b13f3Jamie Gennis return; 310376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 311376590d668e22a918439877b55faf075427b13f3Jamie Gennis 312376590d668e22a918439877b55faf075427b13f3Jamie Gennis if (st.mEventHandler != null) { 313376590d668e22a918439877b55faf075427b13f3Jamie Gennis Message m = st.mEventHandler.obtainMessage(); 314376590d668e22a918439877b55faf075427b13f3Jamie Gennis st.mEventHandler.sendMessage(m); 315376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 316376590d668e22a918439877b55faf075427b13f3Jamie Gennis } 317b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis 318a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin private void init(int texName, boolean singleBufferMode) throws Surface.OutOfResourcesException { 319e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian Looper looper; 320e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian if ((looper = Looper.myLooper()) != null) { 321e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian mEventHandler = new EventHandler(looper); 322e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian } else if ((looper = Looper.getMainLooper()) != null) { 323e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian mEventHandler = new EventHandler(looper); 324e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian } else { 325e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian mEventHandler = null; 326e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian } 327e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian nativeInit(texName, singleBufferMode, new WeakReference<SurfaceTexture>(this)); 328e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian } 329e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian 330a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin private native void nativeInit(int texName, boolean singleBufferMode, Object weakSelf) 331a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin throws Surface.OutOfResourcesException; 332376590d668e22a918439877b55faf075427b13f3Jamie Gennis private native void nativeFinalize(); 333376590d668e22a918439877b55faf075427b13f3Jamie Gennis private native void nativeGetTransformMatrix(float[] mtx); 334c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala private native long nativeGetTimestamp(); 335050316184b01c0d1a01c46afae7429b89a27c31btedbo private native void nativeSetDefaultBufferSize(int width, int height); 3362b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis private native void nativeUpdateTexImage(); 337e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian private native void nativeReleaseTexImage(); 338c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis private native int nativeDetachFromGLContext(); 339c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis private native int nativeAttachToGLContext(int texName); 340925bcaabde5a21687b51caa7ab329310a819f068Grace Kloba private native int nativeGetQueuedCount(); 341ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian private native void nativeRelease(); 3426714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis 3436714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis /* 3446714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * We use a class initializer to allow the native code to cache some 3456714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * field offsets. 3466714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */ 3476714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis private static native void nativeClassInit(); 3486714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis static { nativeClassInit(); } 3496714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis} 350