SurfaceTexture.java revision 5f8b6653e0b5b99097db7d8f41d5251f7b398704
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 *
2737cec0fc50760fe89614863eded14011f9412534Jamie Gennis * <p>The image stream may come from either camera preview.  A SurfaceTexture may be used in place
2837cec0fc50760fe89614863eded14011f9412534Jamie Gennis * of a SurfaceHolder when specifying the output destination of a {@link android.hardware.Camera}
2937cec0fc50760fe89614863eded14011f9412534Jamie Gennis * object.  Doing so will cause all the frames from the image stream to be sent to the
306714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * SurfaceTexture object rather than to the device's display.  When {@link #updateTexImage} is
316714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * called, the contents of the texture object specified when the SurfaceTexture was created is
326714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * updated to contain the most recent image from the image stream.  This may cause some frames of
336714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * the stream to be skipped.
346714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *
355f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * <p>When sampling from the texture one should first transform the texture coordinates using the
365f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * matrix queried via {@link #getTransformMatrix}.  The transform matrix may change each time {@link
375f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * #updateTexImage} is called, so it should be re-queried each time the texture image is updated.
385f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * This matrix transforms traditional 2D OpenGL ES texture coordinate column vectors of the form (s,
395f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * t, 0, 1) where s and t are on the inclusive interval [0, 1] to the proper sampling location in
405f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * the streamed texture.  This transform compensates for any properties of the image stream source
415f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * that cause it to appear different from a traditional OpenGL ES texture.  For example, sampling
425f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * from the bottom left corner of the image can be accomplished by transforming the column vector
435f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * (0, 0, 0, 1) using the queried matrix, while sampling from the top right corner of the image can
445f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * be done by transforming (1, 1, 0, 1).
455f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis *
466714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * <p>The texture object uses the GL_TEXTURE_EXTERNAL_OES texture target, which is defined by the
476714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * OES_EGL_image_external OpenGL ES extension.  This limits how the texture may be used.
4837cec0fc50760fe89614863eded14011f9412534Jamie Gennis *
4937cec0fc50760fe89614863eded14011f9412534Jamie Gennis * <p>SurfaceTexture objects may be created on any thread.  {@link #updateTexImage} may only be
5037cec0fc50760fe89614863eded14011f9412534Jamie Gennis * called on the thread with the OpenGL ES context that contains the texture object.  The
5137cec0fc50760fe89614863eded14011f9412534Jamie Gennis * frame-available callback is called on an arbitrary thread, so unless special care is taken {@link
5237cec0fc50760fe89614863eded14011f9412534Jamie Gennis * #updateTexImage} should not be called directly from the callback.
536714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */
546714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennispublic class SurfaceTexture {
556714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
56376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private EventHandler mEventHandler;
57376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private OnFrameAvailableListener mOnFrameAvailableListener;
58376590d668e22a918439877b55faf075427b13f3Jamie Gennis
596714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    @SuppressWarnings("unused")
606714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    private int mSurfaceTexture;
616714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
626714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
636714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Callback interface for being notified that a new stream frame is available.
646714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
656714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public interface OnFrameAvailableListener {
666714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        void onFrameAvailable(SurfaceTexture surfaceTexture);
676714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
686714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
696714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
706714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Exception thrown when a surface couldn't be created or resized
716714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
726714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public static class OutOfResourcesException extends Exception {
736714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        public OutOfResourcesException() {
746714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        }
756714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        public OutOfResourcesException(String name) {
766714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis            super(name);
776714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        }
786714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
796714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
806714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
816714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Construct a new SurfaceTexture to stream images to a given OpenGL texture.
826714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     *
836714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * @param texName the OpenGL texture object name (e.g. generated via glGenTextures)
846714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
856714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public SurfaceTexture(int texName) {
86376590d668e22a918439877b55faf075427b13f3Jamie Gennis        Looper looper;
87376590d668e22a918439877b55faf075427b13f3Jamie Gennis        if ((looper = Looper.myLooper()) != null) {
88376590d668e22a918439877b55faf075427b13f3Jamie Gennis            mEventHandler = new EventHandler(looper);
89376590d668e22a918439877b55faf075427b13f3Jamie Gennis        } else if ((looper = Looper.getMainLooper()) != null) {
90376590d668e22a918439877b55faf075427b13f3Jamie Gennis            mEventHandler = new EventHandler(looper);
91376590d668e22a918439877b55faf075427b13f3Jamie Gennis        } else {
92376590d668e22a918439877b55faf075427b13f3Jamie Gennis            mEventHandler = null;
93376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
94376590d668e22a918439877b55faf075427b13f3Jamie Gennis        nativeInit(texName, new WeakReference<SurfaceTexture>(this));
956714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
966714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
976714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
986714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Register a callback to be invoked when a new image frame becomes available to the
996714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * SurfaceTexture.  Note that this callback may be called on an arbitrary thread, so it is not
1006714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * safe to call {@link #updateTexImage} without first binding the OpenGL ES context to the
1016714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * thread invoking the callback.
1026714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
1036714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public void setOnFrameAvailableListener(OnFrameAvailableListener l) {
104376590d668e22a918439877b55faf075427b13f3Jamie Gennis        mOnFrameAvailableListener = l;
1056714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
1066714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
1076714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
1086714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Update the texture image to the most recent frame from the image stream.  This may only be
1096714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * called while the OpenGL ES context that owns the texture is bound to the thread.  It will
1106714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target.
1116714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
112376590d668e22a918439877b55faf075427b13f3Jamie Gennis    public void updateTexImage() {
113376590d668e22a918439877b55faf075427b13f3Jamie Gennis        nativeUpdateTexImage();
114376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
115b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
116b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    /**
117b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by
118b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * the most recent call to updateTexImage.
119b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
120b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s
121b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample
122b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * that location from the texture.  Sampling the texture outside of the range of this transform
123b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * is undefined.
124b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
125b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via
126b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * the glLoadMatrixf or glUniformMatrix4fv functions.
127b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
128b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * @param mtx the array into which the 4x4 matrix will be stored.  The array must have exactly
129b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *     16 elements.
130b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     */
131b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    public void getTransformMatrix(float[] mtx) {
132b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis        if (mtx.length != 16) {
133b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis            throw new IllegalArgumentException();
134b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis        }
135376590d668e22a918439877b55faf075427b13f3Jamie Gennis        nativeGetTransformMatrix(mtx);
136b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    }
137b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
138376590d668e22a918439877b55faf075427b13f3Jamie Gennis    protected void finalize() throws Throwable {
139376590d668e22a918439877b55faf075427b13f3Jamie Gennis        try {
140376590d668e22a918439877b55faf075427b13f3Jamie Gennis            nativeFinalize();
141376590d668e22a918439877b55faf075427b13f3Jamie Gennis        } finally {
142376590d668e22a918439877b55faf075427b13f3Jamie Gennis            super.finalize();
143376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
144376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
145376590d668e22a918439877b55faf075427b13f3Jamie Gennis
146376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private class EventHandler extends Handler {
147376590d668e22a918439877b55faf075427b13f3Jamie Gennis        public EventHandler(Looper looper) {
148376590d668e22a918439877b55faf075427b13f3Jamie Gennis            super(looper);
149376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
150376590d668e22a918439877b55faf075427b13f3Jamie Gennis
151376590d668e22a918439877b55faf075427b13f3Jamie Gennis        @Override
152376590d668e22a918439877b55faf075427b13f3Jamie Gennis        public void handleMessage(Message msg) {
153376590d668e22a918439877b55faf075427b13f3Jamie Gennis            if (mOnFrameAvailableListener != null) {
154376590d668e22a918439877b55faf075427b13f3Jamie Gennis                mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this);
155376590d668e22a918439877b55faf075427b13f3Jamie Gennis            }
156376590d668e22a918439877b55faf075427b13f3Jamie Gennis            return;
157376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
158376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
159376590d668e22a918439877b55faf075427b13f3Jamie Gennis
160376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private static void postEventFromNative(Object selfRef) {
161376590d668e22a918439877b55faf075427b13f3Jamie Gennis        WeakReference weakSelf = (WeakReference)selfRef;
162376590d668e22a918439877b55faf075427b13f3Jamie Gennis        SurfaceTexture st = (SurfaceTexture)weakSelf.get();
163376590d668e22a918439877b55faf075427b13f3Jamie Gennis        if (st == null) {
164376590d668e22a918439877b55faf075427b13f3Jamie Gennis            return;
165376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
166376590d668e22a918439877b55faf075427b13f3Jamie Gennis
167376590d668e22a918439877b55faf075427b13f3Jamie Gennis        if (st.mEventHandler != null) {
168376590d668e22a918439877b55faf075427b13f3Jamie Gennis            Message m = st.mEventHandler.obtainMessage();
169376590d668e22a918439877b55faf075427b13f3Jamie Gennis            st.mEventHandler.sendMessage(m);
170376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
171376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
172b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
173376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeInit(int texName, Object weakSelf);
174376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeFinalize();
175376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeGetTransformMatrix(float[] mtx);
176376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeUpdateTexImage();
1776714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
1786714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /*
1796714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * We use a class initializer to allow the native code to cache some
1806714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * field offsets.
1816714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
1826714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    private static native void nativeClassInit();
1836714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    static { nativeClassInit(); }
1846714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis}
185