TextureView.java revision cf559377b750271472aa0a717bf3b7d34abc0b39
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) {
81cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *          // Ignored
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
105a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    private final Runnable mUpdateLayerAction = new Runnable() {
106a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        @Override
107a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        public void run() {
108a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            updateLayer();
109a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
110a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    };
1118f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    private SurfaceTexture.OnFrameAvailableListener mUpdateListener;
112aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
113aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
114aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
115aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
116aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
117aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
118aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context) {
119aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context);
120aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
121aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
122aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
123aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
124aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
125aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
126aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
127aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
128aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
129aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
130aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs) {
131aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs);
132aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
133aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
134aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
135aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
136aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
137aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
138aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
139aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
140aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param defStyle The default style to apply to this view. If 0, no style
141aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        will be applied (beyond what is included in the theme). This may
142aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        either be an attribute resource, whose value will be retrieved
143aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        from the current theme, or an explicit style resource.
144aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
145aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
146aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs, int defStyle) {
147aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs, defStyle);
148aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
149aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
150aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
151aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private void init() {
152aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mLayerPaint = new Paint();
153aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
154aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
155a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    /**
156a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * {@inheritDoc}
157a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     */
158a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    @Override
159a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    public boolean isOpaque() {
160a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        return mOpaque;
161a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
162a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
163a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    /**
164a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * Indicates whether the content of this TextureView is opaque. The
165a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * content is assumed to be opaque by default.
166a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     *
167a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * @param opaque True if the content of this TextureView is opaque,
168a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     *               false otherwise
169a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     */
170a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    public void setOpaque(boolean opaque) {
171a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        if (opaque != mOpaque) {
172a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            mOpaque = opaque;
1731ec3a58bcdd0d5fa82cf878d974d062811933ae2Romain Guy            if (mLayer != null) updateLayer();
174a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
175a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
176a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
177aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
178aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected void onAttachedToWindow() {
179aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super.onAttachedToWindow();
180aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
181aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (!isHardwareAccelerated()) {
18277a811610f99e21da7f88dafef60d09f345d0506Romain Guy            Log.w(LOG_TAG, "A TextureView or a subclass can only be "
183aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    + "used with hardware acceleration enabled.");
184aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
185aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
186aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
187451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    @Override
188451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    protected void onDetachedFromWindow() {
189451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        super.onDetachedFromWindow();
190451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
191451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        if (isHardwareAccelerated() && mLayer != null) {
192451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
193451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureDestroyed(mSurface);
194451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
195451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
196451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mLayer.destroy();
197451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mSurface = null;
198451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mLayer = null;
199451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        }
200451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    }
201451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
202aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
203aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * The layer type of a TextureView is ignored since a TextureView is always
204aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * considered to act as a hardware layer. The optional paint supplied to this
205aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * method will however be taken into account when rendering the content of
206aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * this TextureView.
207aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
208aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param layerType The ype of layer to use with this view, must be one of
209aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
210aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_HARDWARE}
211aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param paint The paint used to compose the layer. This argument is optional
212aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        and can be null. It is ignored when the layer type is
213aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}
214aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
215aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
216aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setLayerType(int layerType, Paint paint) {
217aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (paint != mLayerPaint) {
218aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            mLayerPaint = paint;
219aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            invalidate();
220aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
221aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
222aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
223aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
224aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Always returns {@link #LAYER_TYPE_HARDWARE}.
225aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
226aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
227aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public int getLayerType() {
228aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return LAYER_TYPE_HARDWARE;
229aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
230aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
231aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
232aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Calling this method has no effect.
233aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
234aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
235aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void buildLayer() {
236aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
237aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
238aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
239aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
240aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
241aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
242aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
243aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
244aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
245aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public final void draw(Canvas canvas) {
246aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
247aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
248aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
249aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
250aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
251aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
252aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
253aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
254aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
255aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected final void onDraw(Canvas canvas) {
256aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
257aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
258aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
2598f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
2608f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onSizeChanged(w, h, oldw, oldh);
2618f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
262e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
263451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
264451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight());
265451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
2668f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
2678f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
2688f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
2698f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
270aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    HardwareLayer getHardwareLayer() {
271aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
272aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            return null;
273aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
274aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
275aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (mLayer == null) {
276a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(mOpaque);
277e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
278e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
279aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
2808f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() {
281aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                @Override
282aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                public void onFrameAvailable(SurfaceTexture surfaceTexture) {
283aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // Per SurfaceTexture's documentation, the callback may be invoked
284aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // from an arbitrary thread
285a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy                    post(mUpdateLayerAction);
286aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                }
2878f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            };
2888f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mSurface.setOnFrameAvailableListener(mUpdateListener);
289aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
290aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            if (mListener != null) {
291451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
292aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            }
293aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
294aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
295aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mLayer;
296aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
297aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
2988f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
2998f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onVisibilityChanged(View changedView, int visibility) {
3008f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onVisibilityChanged(changedView, visibility);
3018f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
3028f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
3038f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // When the view becomes invisible, stop updating it, it's a waste of CPU
3048f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // To cancel updates, the easiest thing to do is simply to remove the
3058f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // updates listener
3068f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            if (visibility == VISIBLE) {
3078f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(mUpdateListener);
308a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy                updateLayer();
3098f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            } else {
3108f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(null);
3118f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            }
3128f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
3138f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
3148f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
315a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    private void updateLayer() {
316a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
317a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            return;
318a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
319a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
320a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        mAttachInfo.mHardwareRenderer.updateTextureLayer(mLayer, getWidth(), getHeight(), mOpaque);
321a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
322cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        if (mListener != null) {
323cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba            mListener.onSurfaceTextureUpdated(mSurface);
324cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        }
325cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba
326a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        invalidate();
327a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
328a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
329aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
33077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
33177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
33277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
33377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
33477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
33577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format and its dimensions are the same as this view's.</p>
33677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
33777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
33877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
33977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
340d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
341d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
34277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
34377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or the width &lt;= 0 or the height &lt;= 0
34477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
34577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
34677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
34777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
34877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
34977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap() {
35077a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return getBitmap(getWidth(), getHeight());
35177a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
35277a811610f99e21da7f88dafef60d09f345d0506Romain Guy
35377a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
35477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
35577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
35677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
35777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
35877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
35977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format.</p>
36077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
36177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
36277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
36377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
364d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
365d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
36677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param width The width of the bitmap to create
36777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param height The height of the bitmap to create
36877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
36977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
37077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or width is &lt;= 0 or height is &lt;= 0
37177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
37277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
37377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
37477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
37577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
37677a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(int width, int height) {
37777a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (isAvailable() && width > 0 && height > 0) {
37877a811610f99e21da7f88dafef60d09f345d0506Romain Guy            return getBitmap(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
37977a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
38077a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return null;
38177a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
38277a811610f99e21da7f88dafef60d09f345d0506Romain Guy
38377a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
38477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Copies the content of this view's surface texture into the specified
38577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * bitmap. If the surface texture is not available, the copy is not executed.
38677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * The content of the surface texture will be scaled to fit exactly inside
38777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * the specified bitmap.</p>
38877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
38977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
39077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
39177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
392d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs, the bitmap is left unchanged.</p>
393d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
39477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param bitmap The bitmap to copy the content of the surface texture into,
39577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *               cannot be null, all configurations are supported
39677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
39777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return The bitmap specified as a parameter
39877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
39977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
40077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
40177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
40277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
40377a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(Bitmap bitmap) {
40477a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (bitmap != null && isAvailable()) {
40577a811610f99e21da7f88dafef60d09f345d0506Romain Guy            mAttachInfo.mHardwareRenderer.copyLayer(mLayer, bitmap);
40677a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
40777a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return bitmap;
40877a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
40977a811610f99e21da7f88dafef60d09f345d0506Romain Guy
41077a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
41177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * Returns true if the {@link SurfaceTexture} associated with this
41277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * TextureView is available for rendering. When this method returns
41377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * true, {@link #getSurfaceTexture()} returns a valid surface texture.
41477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
41577a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public boolean isAvailable() {
41677a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return mSurface != null;
41777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
41877a811610f99e21da7f88dafef60d09f345d0506Romain Guy
41977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
420aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTexture} used by this view. This method
42177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * may return null if the view is not attached to a window or if the surface
42277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * texture has not been initialized yet.
42377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
42477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
425aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
426aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTexture getSurfaceTexture() {
427aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mSurface;
428aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
429aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
430aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
431aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTextureListener} currently associated with this
432aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture view.
433aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
434aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener)
435aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
436aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
437aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTextureListener getSurfaceTextureListener() {
438aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mListener;
439aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
440aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
441aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
442aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Sets the {@link SurfaceTextureListener} used to listen to surface
443aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture events.
444aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
445aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #getSurfaceTextureListener()
446aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
447aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
448aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setSurfaceTextureListener(SurfaceTextureListener listener) {
449aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mListener = listener;
450aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
451aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
452aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
453aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * This listener can be used to be notified when the surface texture
454aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * associated with this texture view is available.
455aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
456aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public static interface SurfaceTextureListener {
457aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        /**
458aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use.
459aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *
460aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * @param surface The surface returned by
461aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
462451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param width The width of the surface
463451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param height The height of the surface
464aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         */
465451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height);
4668f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
4678f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        /**
4688f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * Invoked when the {@link SurfaceTexture}'s buffers size changed.
4698f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *
4708f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param surface The surface returned by
4718f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
4728f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param width The new width of the surface
4738f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param height The new height of the surface
4748f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         */
4758f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height);
476451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
477451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        /**
478451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * Invoked when the specified {@link SurfaceTexture} is about to be destroyed.
479451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * After this method is invoked, no rendering should happen inside the surface
480451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * texture.
481451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         *
482451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param surface The surface about to be destroyed
483451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         */
484451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        public void onSurfaceTextureDestroyed(SurfaceTexture surface);
485cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba
486cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        /**
487cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * Invoked when the specified {@link SurfaceTexture} is updated through
488cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * {@link SurfaceTexture#updateTexImage()}.
489cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         *
490cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * @param surface The surface just updated
491cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         */
492cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        public void onSurfaceTextureUpdated(SurfaceTexture surface);
493aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
4948f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
495d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy    private static native void nSetDefaultBufferSize(SurfaceTexture surfaceTexture,
496d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy            int width, int height);
497aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy}
498