SurfaceTexture.java revision 493f2e11909e2d5839ca81ddc66d48d538192478
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
21ba873d21718b0c0bbeabbb3b4796e54073739700Jeff Brownimport android.annotation.Nullable;
22376590d668e22a918439877b55faf075427b13f3Jamie Gennisimport android.os.Handler;
23376590d668e22a918439877b55faf075427b13f3Jamie Gennisimport android.os.Looper;
24376590d668e22a918439877b55faf075427b13f3Jamie Gennisimport android.os.Message;
25a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkinimport android.view.Surface;
26376590d668e22a918439877b55faf075427b13f3Jamie Gennis
276714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis/**
286714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * Captures frames from an image stream as an OpenGL ES texture.
296714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *
30cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * <p>The image stream may come from either camera preview or video decode.  A SurfaceTexture
31cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * may be used in place of a SurfaceHolder when specifying the output destination of a
32cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * {@link android.hardware.Camera} or {@link android.media.MediaPlayer}
3337cec0fc50760fe89614863eded14011f9412534Jamie Gennis * object.  Doing so will cause all the frames from the image stream to be sent to the
346714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * SurfaceTexture object rather than to the device's display.  When {@link #updateTexImage} is
35cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten * called, the contents of the texture object specified when the SurfaceTexture was created are
366714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * updated to contain the most recent image from the image stream.  This may cause some frames of
376714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * the stream to be skipped.
386714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis *
395f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * <p>When sampling from the texture one should first transform the texture coordinates using the
408f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * matrix queried via {@link #getTransformMatrix(float[])}.  The transform matrix may change each
418f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * time {@link #updateTexImage} is called, so it should be re-queried each time the texture image
428f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * is updated.
435f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * This matrix transforms traditional 2D OpenGL ES texture coordinate column vectors of the form (s,
445f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * t, 0, 1) where s and t are on the inclusive interval [0, 1] to the proper sampling location in
455f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * the streamed texture.  This transform compensates for any properties of the image stream source
465f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * that cause it to appear different from a traditional OpenGL ES texture.  For example, sampling
475f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * from the bottom left corner of the image can be accomplished by transforming the column vector
485f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * (0, 0, 0, 1) using the queried matrix, while sampling from the top right corner of the image can
495f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis * be done by transforming (1, 1, 0, 1).
505f8b6653e0b5b99097db7d8f41d5251f7b398704Jamie Gennis *
516714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis * <p>The texture object uses the GL_TEXTURE_EXTERNAL_OES texture target, which is defined by the
52858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * <a href="http://www.khronos.org/registry/gles/extensions/OES/OES_EGL_image_external.txt">
53858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * GL_OES_EGL_image_external</a> OpenGL ES extension.  This limits how the texture may be used.
54858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * Each time the texture is bound it must be bound to the GL_TEXTURE_EXTERNAL_OES target rather than
55858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * the GL_TEXTURE_2D target.  Additionally, any OpenGL ES 2.0 shader that samples from the texture
56858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * must declare its use of this extension using, for example, an "#extension
57858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * GL_OES_EGL_image_external : require" directive.  Such shaders must also access the texture using
58858873271250e80a704c29c71ff0578a87bd9d31Jamie Gennis * the samplerExternalOES GLSL sampler type.
5937cec0fc50760fe89614863eded14011f9412534Jamie Gennis *
6037cec0fc50760fe89614863eded14011f9412534Jamie Gennis * <p>SurfaceTexture objects may be created on any thread.  {@link #updateTexImage} may only be
6137cec0fc50760fe89614863eded14011f9412534Jamie Gennis * called on the thread with the OpenGL ES context that contains the texture object.  The
6237cec0fc50760fe89614863eded14011f9412534Jamie Gennis * frame-available callback is called on an arbitrary thread, so unless special care is taken {@link
6337cec0fc50760fe89614863eded14011f9412534Jamie Gennis * #updateTexImage} should not be called directly from the callback.
646714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis */
656714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennispublic class SurfaceTexture {
66c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown    private final Looper mCreatorLooper;
67c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown    private Handler mOnFrameAvailableHandler;
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;
730dc146be5a6cd0c33910d5b18885df46873a93cbDan Stoza    private long mProducer;
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     *
86c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * @deprecated No longer thrown. {@link android.view.Surface.OutOfResourcesException}
87c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * is used instead.
886714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
89a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin    @SuppressWarnings("serial")
90a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin    @Deprecated
916714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public static class OutOfResourcesException extends Exception {
926714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        public OutOfResourcesException() {
936714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        }
946714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        public OutOfResourcesException(String name) {
956714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis            super(name);
966714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis        }
976714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
986714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
996714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
1006714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Construct a new SurfaceTexture to stream images to a given OpenGL texture.
1016714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     *
1026714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * @param texName the OpenGL texture object name (e.g. generated via glGenTextures)
103a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin     *
104c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * @throws Surface.OutOfResourcesException If the SurfaceTexture cannot be created.
1056714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
1066714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    public SurfaceTexture(int texName) {
107c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown        this(texName, false);
108e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian    }
109e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian
110e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian    /**
111e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * Construct a new SurfaceTexture to stream images to a given OpenGL texture.
112e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     *
113e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * In single buffered mode the application is responsible for serializing access to the image
114e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * content buffer. Each time the image content is to be updated, the
115e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * {@link #releaseTexImage()} method must be called before the image content producer takes
116e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * ownership of the buffer. For example, when producing image content with the NDK
117e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * ANativeWindow_lock and ANativeWindow_unlockAndPost functions, {@link #releaseTexImage()}
118e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * must be called before each ANativeWindow_lock, or that call will fail. When producing
119e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * image content with OpenGL ES, {@link #releaseTexImage()} must be called before the first
120e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * OpenGL ES function call each frame.
121e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     *
122e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * @param texName the OpenGL texture object name (e.g. generated via glGenTextures)
123e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * @param singleBufferMode whether the SurfaceTexture will be in single buffered mode.
124a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin     *
125c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * @throws Surface.OutOfResourcesException If the SurfaceTexture cannot be created.
126e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     */
127e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian    public SurfaceTexture(int texName, boolean singleBufferMode) {
128c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown        mCreatorLooper = Looper.myLooper();
129493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza        nativeInit(false, texName, singleBufferMode, new WeakReference<SurfaceTexture>(this));
130493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza    }
131493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza
132493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza    /**
133493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * Construct a new SurfaceTexture to stream images to a given OpenGL texture.
134493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     *
135493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * In single buffered mode the application is responsible for serializing access to the image
136493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * content buffer. Each time the image content is to be updated, the
137493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * {@link #releaseTexImage()} method must be called before the image content producer takes
138493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * ownership of the buffer. For example, when producing image content with the NDK
139493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * ANativeWindow_lock and ANativeWindow_unlockAndPost functions, {@link #releaseTexImage()}
140493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * must be called before each ANativeWindow_lock, or that call will fail. When producing
141493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * image content with OpenGL ES, {@link #releaseTexImage()} must be called before the first
142493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * OpenGL ES function call each frame.
143493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     *
144493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * Unlike {@link #SurfaceTexture(int, boolean)}, which takes an OpenGL texture object name,
145493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * this constructor creates the SurfaceTexture in detached mode. A texture name must be passed
146493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * in using {@link #attachToGLContext} before calling {@link #releaseTexImage()} and producing
147493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * image content using OpenGL ES.
148493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     *
149493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * @param singleBufferMode whether the SurfaceTexture will be in single buffered mode.
150493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     *
151493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * @throws Surface.OutOfResourcesException If the SurfaceTexture cannot be created.
152493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     * @hide
153493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza     */
154493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza    public SurfaceTexture(boolean singleBufferMode) {
155493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza        mCreatorLooper = Looper.myLooper();
156493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza        nativeInit(true, 0, singleBufferMode, new WeakReference<SurfaceTexture>(this));
1576714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
1586714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
1596714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
1606714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Register a callback to be invoked when a new image frame becomes available to the
161c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * SurfaceTexture.
162c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * <p>
163ba873d21718b0c0bbeabbb3b4796e54073739700Jeff Brown     * The callback may be called on an arbitrary thread, so it is not
1646714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * safe to call {@link #updateTexImage} without first binding the OpenGL ES context to the
1656714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * thread invoking the callback.
166c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * </p>
167c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     *
168ba873d21718b0c0bbeabbb3b4796e54073739700Jeff Brown     * @param listener The listener to use, or null to remove the listener.
1696714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
170ba873d21718b0c0bbeabbb3b4796e54073739700Jeff Brown    public void setOnFrameAvailableListener(@Nullable OnFrameAvailableListener listener) {
171c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown        setOnFrameAvailableListener(listener, null);
172c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown    }
173c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown
174c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown    /**
175c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * Register a callback to be invoked when a new image frame becomes available to the
176c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * SurfaceTexture.
177c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * <p>
178ba873d21718b0c0bbeabbb3b4796e54073739700Jeff Brown     * If a handler is specified, the callback will be invoked on that handler's thread.
179ba873d21718b0c0bbeabbb3b4796e54073739700Jeff Brown     * If no handler is specified, then the callback may be called on an arbitrary thread,
180c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * so it is not safe to call {@link #updateTexImage} without first binding the OpenGL ES
181c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * context to the thread invoking the callback.
182c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * </p>
183c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     *
184ba873d21718b0c0bbeabbb3b4796e54073739700Jeff Brown     * @param listener The listener to use, or null to remove the listener.
185c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * @param handler The handler on which the listener should be invoked, or null
186c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     * to use an arbitrary thread.
187c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown     */
188ba873d21718b0c0bbeabbb3b4796e54073739700Jeff Brown    public void setOnFrameAvailableListener(@Nullable final OnFrameAvailableListener listener,
189ba873d21718b0c0bbeabbb3b4796e54073739700Jeff Brown            @Nullable Handler handler) {
190c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown        if (listener != null) {
191c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown            // Although we claim the thread is arbitrary, earlier implementation would
192c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown            // prefer to send the callback on the creating looper or the main looper
193c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown            // so we preserve this behavior here.
194c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown            Looper looper = handler != null ? handler.getLooper() :
195c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown                    mCreatorLooper != null ? mCreatorLooper : Looper.getMainLooper();
196c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown            mOnFrameAvailableHandler = new Handler(looper, null, true /*async*/) {
197c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown                @Override
198c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown                public void handleMessage(Message msg) {
199c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown                    listener.onFrameAvailable(SurfaceTexture.this);
200c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown                }
201c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown            };
202c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown        } else {
203c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown            mOnFrameAvailableHandler = null;
204c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown        }
2056714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    }
2066714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
2076714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /**
2082aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * Set the default size of the image buffers.  The image producer may override the buffer size,
2092aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * in which case the producer-set buffer size will be used, not the default size set by this
2102aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * method.  Both video and camera based image producers do override the size.  This method may
2112aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * be used to set the image size when producing images with {@link android.graphics.Canvas} (via
2122aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * {@link android.view.Surface#lockCanvas}), or OpenGL ES (via an EGLSurface).
213050316184b01c0d1a01c46afae7429b89a27c31btedbo     *
2142aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * The new default buffer size will take effect the next time the image producer requests a
2152aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * buffer to fill.  For {@link android.graphics.Canvas} this will be the next time {@link
2162aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * android.view.Surface#lockCanvas} is called.  For OpenGL ES, the EGLSurface should be
2172aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * destroyed (via eglDestroySurface), made not-current (via eglMakeCurrent), and then recreated
2182aafe742e5d2d63d77c49df032ec580966661597Jamie Gennis     * (via eglCreateWindowSurface) to ensure that the new default size has taken effect.
219a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin     *
220b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian     * The width and height parameters must be no greater than the minimum of
221a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin     * GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see
222b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian     * {@link javax.microedition.khronos.opengles.GL10#glGetIntegerv glGetIntegerv}).
223b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian     * An error due to invalid dimensions might not be reported until
224b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian     * updateTexImage() is called.
225050316184b01c0d1a01c46afae7429b89a27c31btedbo     */
226050316184b01c0d1a01c46afae7429b89a27c31btedbo    public void setDefaultBufferSize(int width, int height) {
227050316184b01c0d1a01c46afae7429b89a27c31btedbo        nativeSetDefaultBufferSize(width, height);
228050316184b01c0d1a01c46afae7429b89a27c31btedbo    }
229050316184b01c0d1a01c46afae7429b89a27c31btedbo
230050316184b01c0d1a01c46afae7429b89a27c31btedbo    /**
2316714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * Update the texture image to the most recent frame from the image stream.  This may only be
2322b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * called while the OpenGL ES context that owns the texture is current on the calling thread.
2332b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * It will implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target.
2346714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
235376590d668e22a918439877b55faf075427b13f3Jamie Gennis    public void updateTexImage() {
23633efb231cb92065c40c019319adae36abc413863Jamie Gennis        nativeUpdateTexImage();
237c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    }
238c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis
239c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    /**
240e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * Releases the the texture content. This is needed in single buffered mode to allow the image
241e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     * content producer to take ownership of the image buffer.
242cb92a8d9428c2e4a9f038cd5f10e9d61bf265cd4Mathias Agopian     * For more information see {@link #SurfaceTexture(int, boolean)}.
243e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian     */
244e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian    public void releaseTexImage() {
245e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian        nativeReleaseTexImage();
246e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian    }
247e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian
248e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian    /**
2492b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * Detach the SurfaceTexture from the OpenGL ES context that owns the OpenGL ES texture object.
2502b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * This call must be made with the OpenGL ES context current on the calling thread.  The OpenGL
2512b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * ES texture object will be deleted as a result of this call.  After calling this method all
2522b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * calls to {@link #updateTexImage} will throw an {@link java.lang.IllegalStateException} until
2532b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * a successful call to {@link #attachToGLContext} is made.
2542b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     *
2552b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * This can be used to access the SurfaceTexture image contents from multiple OpenGL ES
2562b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * contexts.  Note, however, that the image contents are only accessible from one OpenGL ES
2572b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * context at a time.
258c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis     */
259c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    public void detachFromGLContext() {
260c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        int err = nativeDetachFromGLContext();
261c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        if (err != 0) {
262c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis            throw new RuntimeException("Error during detachFromGLContext (see logcat for details)");
263c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        }
264c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    }
265c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis
266c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    /**
2672b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * Attach the SurfaceTexture to the OpenGL ES context that is current on the calling thread.  A
2682b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * new OpenGL ES texture object is created and populated with the SurfaceTexture image frame
2692b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * that was current at the time of the last call to {@link #detachFromGLContext}.  This new
2702b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * texture is bound to the GL_TEXTURE_EXTERNAL_OES texture target.
2712b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     *
2722b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * This can be used to access the SurfaceTexture image contents from multiple OpenGL ES
2732b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * contexts.  Note, however, that the image contents are only accessible from one OpenGL ES
2742b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * context at a time.
2752b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     *
2762b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * @param texName The name of the OpenGL ES texture that will be created.  This texture name
2772b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis     * must be unusued in the OpenGL ES context that is current on the calling thread.
278c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis     */
279c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    public void attachToGLContext(int texName) {
280c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        int err = nativeAttachToGLContext(texName);
281c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis        if (err != 0) {
282425c3da61ad39f5dd4aaba9fd0235f46ff079245Jonathan Dixon            throw new RuntimeException("Error during attachToGLContext (see logcat for details)");
283b89d88f531ee39927f8f554baaae5ecc9101ba9dMathias Agopian        }
284376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
285b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
286b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    /**
287b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by
288b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * the most recent call to updateTexImage.
289b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
290b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s
291b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample
292b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * that location from the texture.  Sampling the texture outside of the range of this transform
293b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * is undefined.
294b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
295b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via
296b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * the glLoadMatrixf or glUniformMatrix4fv functions.
297b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *
298b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     * @param mtx the array into which the 4x4 matrix will be stored.  The array must have exactly
299b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     *     16 elements.
300b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis     */
301b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    public void getTransformMatrix(float[] mtx) {
302cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten        // Note we intentionally don't check mtx for null, so this will result in a
303cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten        // NullPointerException. But it's safe because it happens before the call to native.
304b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis        if (mtx.length != 16) {
305b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis            throw new IllegalArgumentException();
306b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis        }
307376590d668e22a918439877b55faf075427b13f3Jamie Gennis        nativeGetTransformMatrix(mtx);
308b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis    }
309b0ba48c95ea8768a051100c5adb4c906caa1e080Jamie Gennis
310c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    /**
311c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * Retrieve the timestamp associated with the texture image set by the most recent call to
312c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * updateTexImage.
313c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     *
3145c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten     * This timestamp is in nanoseconds, and is normally monotonically increasing. The timestamp
3155c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten     * should be unaffected by time-of-day adjustments, and for a camera should be strictly
3165c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten     * monotonic but for a MediaPlayer may be reset when the position is set.  The
317c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * specific meaning and zero point of the timestamp depends on the source providing images to
318c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * the SurfaceTexture. Unless otherwise specified by the image source, timestamps cannot
319c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * generally be compared across SurfaceTexture instances, or across multiple program
320c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     * invocations. It is mostly useful for determining time offsets between subsequent frames.
321c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala     */
3225c2faf3dc310b100707eb9e32e1e5ae8ceffd0c6Glenn Kasten
323c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    public long getTimestamp() {
324c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala        return nativeGetTimestamp();
325c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    }
326c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala
327ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    /**
328ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * release() frees all the buffers and puts the SurfaceTexture into the
329ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * 'abandoned' state. Once put in this state the SurfaceTexture can never
330ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * leave it. When in the 'abandoned' state, all methods of the
331d47f7d8b5fe3a3861d7cbdc5f912235407823c8eAndy McFadden     * IGraphicBufferProducer interface will fail with the NO_INIT error.
332ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     *
333ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * Note that while calling this method causes all the buffers to be freed
334ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * from the perspective of the the SurfaceTexture, if there are additional
335ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * references on the buffers (e.g. if a buffer is referenced by a client or
336ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * by OpenGL ES as a texture) then those buffer will remain allocated.
337ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     *
338ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * Always call this method when you are done with SurfaceTexture. Failing
339ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * to do so may delay resource deallocation for a significant amount of
340ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     * time.
341ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian     */
342ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    public void release() {
343ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian        nativeRelease();
344ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    }
345ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian
346a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin    @Override
347376590d668e22a918439877b55faf075427b13f3Jamie Gennis    protected void finalize() throws Throwable {
348376590d668e22a918439877b55faf075427b13f3Jamie Gennis        try {
349376590d668e22a918439877b55faf075427b13f3Jamie Gennis            nativeFinalize();
350376590d668e22a918439877b55faf075427b13f3Jamie Gennis        } finally {
351376590d668e22a918439877b55faf075427b13f3Jamie Gennis            super.finalize();
352376590d668e22a918439877b55faf075427b13f3Jamie Gennis        }
353376590d668e22a918439877b55faf075427b13f3Jamie Gennis    }
354376590d668e22a918439877b55faf075427b13f3Jamie Gennis
3558f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    /**
3568f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy     * This method is invoked from native code only.
3578f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy     */
3588f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @SuppressWarnings({"UnusedDeclaration"})
359c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown    private static void postEventFromNative(WeakReference<SurfaceTexture> weakSelf) {
360c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown        SurfaceTexture st = weakSelf.get();
361c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown        if (st != null) {
362c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown            Handler handler = st.mOnFrameAvailableHandler;
363c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown            if (handler != null) {
364c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown                handler.sendEmptyMessage(0);
365c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown            }
366e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian        }
367e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian    }
368e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian
369493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza    private native void nativeInit(boolean isDetached, int texName,
370493f2e11909e2d5839ca81ddc66d48d538192478Dan Stoza            boolean singleBufferMode, WeakReference<SurfaceTexture> weakSelf)
371a86ab640f7bb0bf3cb4eaed80473ca8c5d131903Igor Murashkin            throws Surface.OutOfResourcesException;
372376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeFinalize();
373376590d668e22a918439877b55faf075427b13f3Jamie Gennis    private native void nativeGetTransformMatrix(float[] mtx);
374c5f94d8a4779050125145396ca83fbc862c7ed6bEino-Ville Talvala    private native long nativeGetTimestamp();
375050316184b01c0d1a01c46afae7429b89a27c31btedbo    private native void nativeSetDefaultBufferSize(int width, int height);
3762b4bfa5efec7df408b4db127961cfc9aca9e57cfJamie Gennis    private native void nativeUpdateTexImage();
377e591b49de038a9942cbcc77540c03e85c96e3dcbMathias Agopian    private native void nativeReleaseTexImage();
378c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    private native int nativeDetachFromGLContext();
379c6d993077761fc737bbb0f4db44b961a4e7b6bbbJamie Gennis    private native int nativeAttachToGLContext(int texName);
380925bcaabde5a21687b51caa7ab329310a819f068Grace Kloba    private native int nativeGetQueuedCount();
381ec46b4e1ca89d7c3a9ad70ded58da08b5e19f08fMathias Agopian    private native void nativeRelease();
3826714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis
3836714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    /*
3846714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * We use a class initializer to allow the native code to cache some
3856714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     * field offsets.
3866714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis     */
3876714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    private static native void nativeClassInit();
3886714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis    static { nativeClassInit(); }
3896714efc5e0c52953b65e774de0003e22377e7d39Jamie Gennis}
390