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