TextureView.java revision 58f4edb7701bf20925468fa5fd1a06a461ff085b
1aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/*
2aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Copyright (C) 2011 The Android Open Source Project
3aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
4aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * you may not use this file except in compliance with the License.
6aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * You may obtain a copy of the License at
7aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
8aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
10aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Unless required by applicable law or agreed to in writing, software
11aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * See the License for the specific language governing permissions and
14aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * limitations under the License.
15aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */
16aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
17aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypackage android.view;
18aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
19aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.content.Context;
2077a811610f99e21da7f88dafef60d09f345d0506Romain Guyimport android.graphics.Bitmap;
21aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.Canvas;
22aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.Paint;
23aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.SurfaceTexture;
24aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.AttributeSet;
25aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.Log;
26aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
27aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/**
28aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView can be used to display a content stream. Such a content
29aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * stream can for instance be a video or an OpenGL scene. The content stream
30aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can come from the application's process as well as a remote process.</p>
31aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
32aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>TextureView can only be used in a hardware accelerated window. When
33aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * rendered in software, TextureView will draw nothing.</p>
34aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
35aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Unlike {@link SurfaceView}, TextureView does not create a separate
36aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * window but behaves as a regular View. This key difference allows a
37aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView to be moved, transformed, animated, etc. For instance, you
38aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can make a TextureView semi-translucent by calling
39aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <code>myView.setAlpha(0.5f)</code>.</p>
40aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
41aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Using a TextureView is simple: all you need to do is get its
42aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link SurfaceTexture}. The {@link SurfaceTexture} can then be used to
43aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * render content. The following example demonstrates how to render the
44aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * camera preview into a TextureView:</p>
45aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
46aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <pre>
47aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *  public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
48aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      private Camera mCamera;
49aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      private TextureView mTextureView;
50aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
51aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      protected void onCreate(Bundle savedInstanceState) {
52aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          super.onCreate(savedInstanceState);
53aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
54aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mTextureView = new TextureView(this);
55aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mTextureView.setSurfaceTextureListener(this);
56aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
57aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          setContentView(mTextureView);
58aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      }
59aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
60451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
61aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mCamera = Camera.open();
62aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
63aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          try {
64aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              mCamera.setPreviewTexture(surface);
65aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              mCamera.startPreview();
66aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          } catch (IOException ioe) {
67aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              // Something bad happened
68aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          }
69aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      }
70cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *
718f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *      public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
728f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *          // Ignored, Camera does all the work for us
738f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *      }
74cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *
75451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *      public void onSurfaceTextureDestroyed(SurfaceTexture surface) {
76451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *          mCamera.stopPreview();
77451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *          mCamera.release();
78451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *      }
79cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *
80cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *      public void onSurfaceTextureUpdated(SurfaceTexture surface) {
8158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy *          // Invoked every time there's a new Camera preview frame
82cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *      }
83aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *  }
84aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * </pre>
85aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
86aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView's SurfaceTexture can be obtained either by invoking
87aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #getSurfaceTexture()} or by using a {@link SurfaceTextureListener}.
88aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * It is important to know that a SurfaceTexture is available only after the
89aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView is attached to a window (and {@link #onAttachedToWindow()} has
90aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * been invoked.) It is therefore highly recommended you use a listener to
91aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * be notified when the SurfaceTexture becomes available.</p>
92aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
93aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceView
94aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTexture
95aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */
96aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypublic class TextureView extends View {
9777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    private static final String LOG_TAG = "TextureView";
9877a811610f99e21da7f88dafef60d09f345d0506Romain Guy
99aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private HardwareLayer mLayer;
100aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private SurfaceTexture mSurface;
101aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private SurfaceTextureListener mListener;
102aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
103a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    private boolean mOpaque = true;
104c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy
10558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private final Object[] mLock = new Object[0];
10658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private boolean mUpdateLayer;
10758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
1088f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    private SurfaceTexture.OnFrameAvailableListener mUpdateListener;
109aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
110aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
111aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
112aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
113aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
114aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
115aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context) {
116aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context);
117aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
118aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
119aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
120aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
121aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
122aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
123aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
124aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
125aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
126aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
127aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs) {
128aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs);
129aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
130aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
131aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
132aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
133aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
134aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
135aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
136aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
137aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param defStyle The default style to apply to this view. If 0, no style
138aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        will be applied (beyond what is included in the theme). This may
139aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        either be an attribute resource, whose value will be retrieved
140aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        from the current theme, or an explicit style resource.
141aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
142aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
143aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs, int defStyle) {
144aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs, defStyle);
145aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
146aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
147aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
148aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private void init() {
149aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mLayerPaint = new Paint();
150aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
151aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
152a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    /**
153a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * {@inheritDoc}
154a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     */
155a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    @Override
156a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    public boolean isOpaque() {
157a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        return mOpaque;
158a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
159a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
160a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    /**
161a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * Indicates whether the content of this TextureView is opaque. The
162a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * content is assumed to be opaque by default.
163a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     *
164a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * @param opaque True if the content of this TextureView is opaque,
165a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     *               false otherwise
166a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     */
167a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    public void setOpaque(boolean opaque) {
168a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        if (opaque != mOpaque) {
169a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            mOpaque = opaque;
17058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            updateLayer();
171a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
172a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
173a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
174aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
175aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected void onAttachedToWindow() {
176aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super.onAttachedToWindow();
177aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
178aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (!isHardwareAccelerated()) {
17977a811610f99e21da7f88dafef60d09f345d0506Romain Guy            Log.w(LOG_TAG, "A TextureView or a subclass can only be "
180aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    + "used with hardware acceleration enabled.");
181aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
182aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
183aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
184451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    @Override
185451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    protected void onDetachedFromWindow() {
186451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        super.onDetachedFromWindow();
187451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
188451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        if (isHardwareAccelerated() && mLayer != null) {
189451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
190451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureDestroyed(mSurface);
191451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
192451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
193451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mLayer.destroy();
194451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mSurface = null;
195451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mLayer = null;
196451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        }
197451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    }
198451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
199aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
200aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * The layer type of a TextureView is ignored since a TextureView is always
201aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * considered to act as a hardware layer. The optional paint supplied to this
202aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * method will however be taken into account when rendering the content of
203aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * this TextureView.
204aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
205aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param layerType The ype of layer to use with this view, must be one of
206aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
207aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_HARDWARE}
208aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param paint The paint used to compose the layer. This argument is optional
209aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        and can be null. It is ignored when the layer type is
210aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}
211aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
212aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
213aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setLayerType(int layerType, Paint paint) {
214aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (paint != mLayerPaint) {
215aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            mLayerPaint = paint;
216aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            invalidate();
217aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
218aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
219aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
220aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
221aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Always returns {@link #LAYER_TYPE_HARDWARE}.
222aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
223aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
224aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public int getLayerType() {
225aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return LAYER_TYPE_HARDWARE;
226aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
227aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
228aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
229aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Calling this method has no effect.
230aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
231aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
232aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void buildLayer() {
233aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
234aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
235aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
236aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
237aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
238aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
239aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
240aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
241aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
242aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public final void draw(Canvas canvas) {
24358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        applyUpdate();
244aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
245aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
246aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
247aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
248aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
249aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
250aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
251aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
252aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
253aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected final void onDraw(Canvas canvas) {
254aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
255aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
256aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
2578f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
2588f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onSizeChanged(w, h, oldw, oldh);
2598f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
260e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
261451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
262451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight());
263451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
2648f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
2658f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
2668f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
2678f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
268aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    HardwareLayer getHardwareLayer() {
269aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (mLayer == null) {
27058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
27158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                return null;
27258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            }
27358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
274a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(mOpaque);
275e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
276e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
277aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
2788f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() {
279aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                @Override
280aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                public void onFrameAvailable(SurfaceTexture surfaceTexture) {
281aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // Per SurfaceTexture's documentation, the callback may be invoked
282aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // from an arbitrary thread
28358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                    synchronized (mLock) {
28458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                        mUpdateLayer = true;
28558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                    }
28658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                    postInvalidateDelayed(0);
287aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                }
2888f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            };
2898f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mSurface.setOnFrameAvailableListener(mUpdateListener);
290aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
291aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            if (mListener != null) {
292451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
293aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            }
294aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
295aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
29658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        applyUpdate();
29758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
298aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mLayer;
299aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
300aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
3018f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
3028f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onVisibilityChanged(View changedView, int visibility) {
3038f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onVisibilityChanged(changedView, visibility);
3048f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
3058f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
3068f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // When the view becomes invisible, stop updating it, it's a waste of CPU
3078f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // To cancel updates, the easiest thing to do is simply to remove the
3088f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // updates listener
3098f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            if (visibility == VISIBLE) {
3108f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(mUpdateListener);
311a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy                updateLayer();
3128f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            } else {
3138f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(null);
3148f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            }
3158f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
3168f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
3178f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
318a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    private void updateLayer() {
31958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        mUpdateLayer = true;
32058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        invalidate();
32158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    }
32258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
32358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private void applyUpdate() {
32458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        if (mLayer == null) {
325a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            return;
326a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
327a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
32858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        synchronized (mLock) {
32958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            if (mUpdateLayer) {
33058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                mUpdateLayer = false;
33158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            } else {
33258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                return;
33358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            }
33458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        }
33558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
33602ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy        mLayer.update(getWidth(), getHeight(), mOpaque);
337a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
338cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        if (mListener != null) {
339cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba            mListener.onSurfaceTextureUpdated(mSurface);
340cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        }
341a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
342a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
343aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
34477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
34577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
34677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
34777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
34877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
34977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format and its dimensions are the same as this view's.</p>
35077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
35177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
35277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
35377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
354d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
355d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
35677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
35777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or the width &lt;= 0 or the height &lt;= 0
35877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
35977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
36077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
36177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
36277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
36377a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap() {
36477a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return getBitmap(getWidth(), getHeight());
36577a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
36677a811610f99e21da7f88dafef60d09f345d0506Romain Guy
36777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
36877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
36977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
37077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
37177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
37277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
37377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format.</p>
37477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
37577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
37677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
37777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
378d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
379d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
38077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param width The width of the bitmap to create
38177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param height The height of the bitmap to create
38277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
38377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
38477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or width is &lt;= 0 or height is &lt;= 0
38577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
38677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
38777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
38877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
38977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
39077a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(int width, int height) {
39177a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (isAvailable() && width > 0 && height > 0) {
39277a811610f99e21da7f88dafef60d09f345d0506Romain Guy            return getBitmap(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
39377a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
39477a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return null;
39577a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
39677a811610f99e21da7f88dafef60d09f345d0506Romain Guy
39777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
39877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Copies the content of this view's surface texture into the specified
39977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * bitmap. If the surface texture is not available, the copy is not executed.
40077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * The content of the surface texture will be scaled to fit exactly inside
40177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * the specified bitmap.</p>
40277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
40377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
40477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
40577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
406d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs, the bitmap is left unchanged.</p>
407d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
40877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param bitmap The bitmap to copy the content of the surface texture into,
40977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *               cannot be null, all configurations are supported
41077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
41177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return The bitmap specified as a parameter
41277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
41377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
41477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
41577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
41677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
41777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(Bitmap bitmap) {
41877a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (bitmap != null && isAvailable()) {
41902ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy            mLayer.copyInto(bitmap);
42077a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
42177a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return bitmap;
42277a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
42377a811610f99e21da7f88dafef60d09f345d0506Romain Guy
42477a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
42577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * Returns true if the {@link SurfaceTexture} associated with this
42677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * TextureView is available for rendering. When this method returns
42777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * true, {@link #getSurfaceTexture()} returns a valid surface texture.
42877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
42977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public boolean isAvailable() {
43077a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return mSurface != null;
43177a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
43277a811610f99e21da7f88dafef60d09f345d0506Romain Guy
43377a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
434aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTexture} used by this view. This method
43577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * may return null if the view is not attached to a window or if the surface
43677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * texture has not been initialized yet.
43777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
43877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
439aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
440aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTexture getSurfaceTexture() {
441aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mSurface;
442aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
443aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
444aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
445aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTextureListener} currently associated with this
446aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture view.
447aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
448aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener)
449aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
450aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
451aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTextureListener getSurfaceTextureListener() {
452aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mListener;
453aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
454aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
455aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
456aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Sets the {@link SurfaceTextureListener} used to listen to surface
457aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture events.
458aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
459aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #getSurfaceTextureListener()
460aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
461aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
462aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setSurfaceTextureListener(SurfaceTextureListener listener) {
463aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mListener = listener;
464aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
465aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
466aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
467aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * This listener can be used to be notified when the surface texture
468aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * associated with this texture view is available.
469aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
470aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public static interface SurfaceTextureListener {
471aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        /**
472aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use.
473aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *
474aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * @param surface The surface returned by
475aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
476451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param width The width of the surface
477451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param height The height of the surface
478aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         */
479451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height);
4808f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
4818f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        /**
4828f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * Invoked when the {@link SurfaceTexture}'s buffers size changed.
4838f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *
4848f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param surface The surface returned by
4858f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
4868f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param width The new width of the surface
4878f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param height The new height of the surface
4888f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         */
4898f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height);
490451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
491451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        /**
492451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * Invoked when the specified {@link SurfaceTexture} is about to be destroyed.
493451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * After this method is invoked, no rendering should happen inside the surface
494451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * texture.
495451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         *
496451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param surface The surface about to be destroyed
497451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         */
498451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        public void onSurfaceTextureDestroyed(SurfaceTexture surface);
499cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba
500cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        /**
501cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * Invoked when the specified {@link SurfaceTexture} is updated through
502cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * {@link SurfaceTexture#updateTexImage()}.
503cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         *
504cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * @param surface The surface just updated
505cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         */
506cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        public void onSurfaceTextureUpdated(SurfaceTexture surface);
507aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
5088f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
509d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy    private static native void nSetDefaultBufferSize(SurfaceTexture surfaceTexture,
510d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy            int width, int height);
511aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy}
512