SurfaceTexture.java revision 376590d668e22a918439877b55faf075427b13f3
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.graphics; 18 19import java.lang.ref.WeakReference; 20import android.os.Handler; 21import android.os.Looper; 22import android.os.Message; 23 24/** 25 * Captures frames from an image stream as an OpenGL ES texture. 26 * 27 * <p>The image stream may come from either video playback or camera preview. A SurfaceTexture may 28 * be used in place of a SurfaceHolder when specifying the output destination of a MediaPlayer or 29 * Camera object. This will cause all the frames from that image stream to be sent to the 30 * SurfaceTexture object rather than to the device's display. When {@link #updateTexImage} is 31 * called, the contents of the texture object specified when the SurfaceTexture was created is 32 * updated to contain the most recent image from the image stream. This may cause some frames of 33 * the stream to be skipped. 34 * 35 * <p>The texture object uses the GL_TEXTURE_EXTERNAL_OES texture target, which is defined by the 36 * OES_EGL_image_external OpenGL ES extension. This limits how the texture may be used. 37 */ 38public class SurfaceTexture { 39 40 private EventHandler mEventHandler; 41 private OnFrameAvailableListener mOnFrameAvailableListener; 42 43 @SuppressWarnings("unused") 44 private int mSurfaceTexture; 45 46 /** 47 * Callback interface for being notified that a new stream frame is available. 48 */ 49 public interface OnFrameAvailableListener { 50 void onFrameAvailable(SurfaceTexture surfaceTexture); 51 } 52 53 /** 54 * Exception thrown when a surface couldn't be created or resized 55 */ 56 public static class OutOfResourcesException extends Exception { 57 public OutOfResourcesException() { 58 } 59 public OutOfResourcesException(String name) { 60 super(name); 61 } 62 } 63 64 /** 65 * Construct a new SurfaceTexture to stream images to a given OpenGL texture. 66 * 67 * @param texName the OpenGL texture object name (e.g. generated via glGenTextures) 68 */ 69 public SurfaceTexture(int texName) { 70 Looper looper; 71 if ((looper = Looper.myLooper()) != null) { 72 mEventHandler = new EventHandler(looper); 73 } else if ((looper = Looper.getMainLooper()) != null) { 74 mEventHandler = new EventHandler(looper); 75 } else { 76 mEventHandler = null; 77 } 78 nativeInit(texName, new WeakReference<SurfaceTexture>(this)); 79 } 80 81 /** 82 * Register a callback to be invoked when a new image frame becomes available to the 83 * SurfaceTexture. Note that this callback may be called on an arbitrary thread, so it is not 84 * safe to call {@link #updateTexImage} without first binding the OpenGL ES context to the 85 * thread invoking the callback. 86 */ 87 public void setOnFrameAvailableListener(OnFrameAvailableListener l) { 88 mOnFrameAvailableListener = l; 89 } 90 91 /** 92 * Update the texture image to the most recent frame from the image stream. This may only be 93 * called while the OpenGL ES context that owns the texture is bound to the thread. It will 94 * implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target. 95 */ 96 public void updateTexImage() { 97 nativeUpdateTexImage(); 98 } 99 100 /** 101 * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by 102 * the most recent call to updateTexImage. 103 * 104 * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s 105 * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample 106 * that location from the texture. Sampling the texture outside of the range of this transform 107 * is undefined. 108 * 109 * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via 110 * the glLoadMatrixf or glUniformMatrix4fv functions. 111 * 112 * @param mtx the array into which the 4x4 matrix will be stored. The array must have exactly 113 * 16 elements. 114 */ 115 public void getTransformMatrix(float[] mtx) { 116 if (mtx.length != 16) { 117 throw new IllegalArgumentException(); 118 } 119 nativeGetTransformMatrix(mtx); 120 } 121 122 protected void finalize() throws Throwable { 123 try { 124 nativeFinalize(); 125 } finally { 126 super.finalize(); 127 } 128 } 129 130 private class EventHandler extends Handler { 131 public EventHandler(Looper looper) { 132 super(looper); 133 } 134 135 @Override 136 public void handleMessage(Message msg) { 137 if (mOnFrameAvailableListener != null) { 138 mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this); 139 } 140 return; 141 } 142 } 143 144 private static void postEventFromNative(Object selfRef) { 145 WeakReference weakSelf = (WeakReference)selfRef; 146 SurfaceTexture st = (SurfaceTexture)weakSelf.get(); 147 if (st == null) { 148 return; 149 } 150 151 if (st.mEventHandler != null) { 152 Message m = st.mEventHandler.obtainMessage(); 153 st.mEventHandler.sendMessage(m); 154 } 155 } 156 157 private native void nativeInit(int texName, Object weakSelf); 158 private native void nativeFinalize(); 159 private native void nativeGetTransformMatrix(float[] mtx); 160 private native void nativeUpdateTexImage(); 161 162 /* 163 * We use a class initializer to allow the native code to cache some 164 * field offsets. 165 */ 166 private static native void nativeClassInit(); 167 static { nativeClassInit(); } 168} 169