TextureView.java revision 88801b270f693ffd4125534724f204135f592f72
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;
22302a9df1d50373c82923bb84ff665dfce584fb22Romain Guyimport android.graphics.Matrix;
23aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.Paint;
246be3d5561cbeccf0a8257a4acb155657f868e548Romain Guyimport android.graphics.Rect;
25aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.SurfaceTexture;
2652c145ff5ce34980d866155bb154913a847d955bRomain Guyimport android.os.Looper;
27aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.AttributeSet;
28aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.Log;
29aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
30aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/**
31aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView can be used to display a content stream. Such a content
32aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * stream can for instance be a video or an OpenGL scene. The content stream
33aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can come from the application's process as well as a remote process.</p>
34aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
35aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>TextureView can only be used in a hardware accelerated window. When
36aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * rendered in software, TextureView will draw nothing.</p>
37aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
38aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Unlike {@link SurfaceView}, TextureView does not create a separate
39aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * window but behaves as a regular View. This key difference allows a
40aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView to be moved, transformed, animated, etc. For instance, you
41aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can make a TextureView semi-translucent by calling
42aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <code>myView.setAlpha(0.5f)</code>.</p>
43aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
44aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Using a TextureView is simple: all you need to do is get its
45aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link SurfaceTexture}. The {@link SurfaceTexture} can then be used to
46aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * render content. The following example demonstrates how to render the
47aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * camera preview into a TextureView:</p>
48aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
49aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <pre>
50aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *  public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
51aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      private Camera mCamera;
52aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      private TextureView mTextureView;
53aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
54aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      protected void onCreate(Bundle savedInstanceState) {
55aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          super.onCreate(savedInstanceState);
56aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
57aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mTextureView = new TextureView(this);
58aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mTextureView.setSurfaceTextureListener(this);
59aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
60aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          setContentView(mTextureView);
61aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      }
62aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
63451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
64aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mCamera = Camera.open();
65aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
66aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          try {
67aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              mCamera.setPreviewTexture(surface);
68aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              mCamera.startPreview();
69aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          } catch (IOException ioe) {
70aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              // Something bad happened
71aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          }
72aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      }
73cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *
748f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *      public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
758f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *          // Ignored, Camera does all the work for us
768f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *      }
77cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *
78402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba *      public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
79451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *          mCamera.stopPreview();
80451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *          mCamera.release();
81402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba *          return true;
82451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *      }
83cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *
84cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *      public void onSurfaceTextureUpdated(SurfaceTexture surface) {
8558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy *          // Invoked every time there's a new Camera preview frame
86cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *      }
87aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *  }
88aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * </pre>
89aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
90aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView's SurfaceTexture can be obtained either by invoking
91aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #getSurfaceTexture()} or by using a {@link SurfaceTextureListener}.
92aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * It is important to know that a SurfaceTexture is available only after the
93aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView is attached to a window (and {@link #onAttachedToWindow()} has
94aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * been invoked.) It is therefore highly recommended you use a listener to
95aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * be notified when the SurfaceTexture becomes available.</p>
96aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
97462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * <p>It is important to note that only one producer can use the TextureView.
98462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * For instance, if you use a TextureView to display the camera preview, you
99462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * cannot use {@link #lockCanvas()} to draw onto the TextureView at the same
100462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * time.</p>
101462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy *
102aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceView
103aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTexture
104aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */
105aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypublic class TextureView extends View {
10677a811610f99e21da7f88dafef60d09f345d0506Romain Guy    private static final String LOG_TAG = "TextureView";
10777a811610f99e21da7f88dafef60d09f345d0506Romain Guy
108aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private HardwareLayer mLayer;
109aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private SurfaceTexture mSurface;
110aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private SurfaceTextureListener mListener;
111aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
112a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    private boolean mOpaque = true;
113c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy
114302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    private final Matrix mMatrix = new Matrix();
115302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    private boolean mMatrixChanged;
116302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
11758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private final Object[] mLock = new Object[0];
11858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private boolean mUpdateLayer;
1192af3524beb75150d347accc925022daa53b4a789Jamie Gennis    private boolean mUpdateSurface;
12058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
1218f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    private SurfaceTexture.OnFrameAvailableListener mUpdateListener;
122aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
1236be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private Canvas mCanvas;
1246be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private int mSaveCount;
1256be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
1266be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private final Object[] mNativeWindowLock = new Object[0];
1276be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    // Used from native code, do not write!
1286be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
1296be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private int mNativeWindow;
1306be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
131aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
132aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
133aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
134aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
135aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
136aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context) {
137aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context);
138aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
139aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
140aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
141aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
142aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
143aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
144aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
145aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
146aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
147aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
148aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs) {
149aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs);
150aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
151aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
152aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
153aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
154aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
155aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
156aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
157aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
158aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param defStyle The default style to apply to this view. If 0, no style
159aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        will be applied (beyond what is included in the theme). This may
160aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        either be an attribute resource, whose value will be retrieved
161aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        from the current theme, or an explicit style resource.
162aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
163aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
164aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs, int defStyle) {
165aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs, defStyle);
166aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
167aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
168aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
169aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private void init() {
170aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mLayerPaint = new Paint();
171aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
172aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
173a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    /**
174a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * {@inheritDoc}
175a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     */
176a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    @Override
177a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    public boolean isOpaque() {
178a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        return mOpaque;
179a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
180a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
181a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    /**
182a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * Indicates whether the content of this TextureView is opaque. The
183a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * content is assumed to be opaque by default.
184a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     *
185a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * @param opaque True if the content of this TextureView is opaque,
186a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     *               false otherwise
187a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     */
188a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    public void setOpaque(boolean opaque) {
189a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        if (opaque != mOpaque) {
190a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            mOpaque = opaque;
191a8a2f97c100af4ba52c61c3b59c933f44a53dad4Romain Guy            if (mLayer != null) {
19288801b270f693ffd4125534724f204135f592f72Romain Guy                updateLayerAndInvalidate();
193a8a2f97c100af4ba52c61c3b59c933f44a53dad4Romain Guy            }
194a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
195a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
196a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
197aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
198aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected void onAttachedToWindow() {
199aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super.onAttachedToWindow();
200aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
201aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (!isHardwareAccelerated()) {
20277a811610f99e21da7f88dafef60d09f345d0506Romain Guy            Log.w(LOG_TAG, "A TextureView or a subclass can only be "
203aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    + "used with hardware acceleration enabled.");
204aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
205aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
206aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
207451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    @Override
208451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    protected void onDetachedFromWindow() {
209451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        super.onDetachedFromWindow();
2101ac4765e959c79101f64b1279887ed469334c268Romain Guy        if (mLayer != null && mAttachInfo != null && mAttachInfo.mHardwareRenderer != null) {
2111ac4765e959c79101f64b1279887ed469334c268Romain Guy            boolean success = mAttachInfo.mHardwareRenderer.safelyRun(new Runnable() {
2121ac4765e959c79101f64b1279887ed469334c268Romain Guy                @Override
2131ac4765e959c79101f64b1279887ed469334c268Romain Guy                public void run() {
2141ac4765e959c79101f64b1279887ed469334c268Romain Guy                    destroySurface();
2151ac4765e959c79101f64b1279887ed469334c268Romain Guy                }
2161ac4765e959c79101f64b1279887ed469334c268Romain Guy            });
2171ac4765e959c79101f64b1279887ed469334c268Romain Guy
2181ac4765e959c79101f64b1279887ed469334c268Romain Guy            if (!success) {
2191ac4765e959c79101f64b1279887ed469334c268Romain Guy                Log.w(LOG_TAG, "TextureView was not able to destroy its surface: " + this);
2201ac4765e959c79101f64b1279887ed469334c268Romain Guy            }
2211ac4765e959c79101f64b1279887ed469334c268Romain Guy        }
22231f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy    }
223451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
22431f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy    private void destroySurface() {
22580429c458506485904715180d10584092a5cd082Romain Guy        if (mLayer != null) {
2262af3524beb75150d347accc925022daa53b4a789Jamie Gennis            mSurface.detachFromGLContext();
227ef09a210dd6ea481158b7028ec2424a7f5769ed2Romain Guy            mLayer.clearStorage();
2282af3524beb75150d347accc925022daa53b4a789Jamie Gennis
229402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba            boolean shouldRelease = true;
230451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
231402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba                shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface);
232451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
233451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
2346be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            synchronized (mNativeWindowLock) {
2356be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy                nDestroyNativeWindow();
2366be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            }
2376be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
2386be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            mLayer.destroy();
239402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba            if (shouldRelease) mSurface.release();
240451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mSurface = null;
241451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mLayer = null;
242451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        }
243451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    }
244451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
245aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
246aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * The layer type of a TextureView is ignored since a TextureView is always
247aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * considered to act as a hardware layer. The optional paint supplied to this
248aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * method will however be taken into account when rendering the content of
249aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * this TextureView.
250aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
251aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param layerType The ype of layer to use with this view, must be one of
252aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
253aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_HARDWARE}
254aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param paint The paint used to compose the layer. This argument is optional
255aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        and can be null. It is ignored when the layer type is
256aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}
257aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
258aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
259aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setLayerType(int layerType, Paint paint) {
260aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (paint != mLayerPaint) {
261aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            mLayerPaint = paint;
262aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            invalidate();
263aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
264aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
265aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
266aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
267aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Always returns {@link #LAYER_TYPE_HARDWARE}.
268aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
269aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
270aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public int getLayerType() {
271aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return LAYER_TYPE_HARDWARE;
272aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
273aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
27459c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy    @Override
27559c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy    boolean hasStaticLayer() {
27659c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy        return true;
27759c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy    }
27859c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy
279aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
280aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Calling this method has no effect.
281aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
282aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
283aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void buildLayer() {
284aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
285aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
286aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
287aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
288aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
289aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
290aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
291aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
292aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
293aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public final void draw(Canvas canvas) {
29458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        applyUpdate();
295c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        applyTransformMatrix();
296aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
297aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
298aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
299aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
300aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
301aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
302aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
303aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
304aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
305aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected final void onDraw(Canvas canvas) {
306aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
307aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
308aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
3098f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
3108f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onSizeChanged(w, h, oldw, oldh);
3118f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
312e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
31388801b270f693ffd4125534724f204135f592f72Romain Guy            updateLayer();
314451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
315451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight());
316451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
3178f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
3188f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
3198f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
3208f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
321a998dff5d49a423aaf7097aa8f96bf5bdc681d25Romain Guy    boolean destroyLayer(boolean valid) {
32216260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy        return false;
32316260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy    }
32416260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy
3251766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy    /**
3261766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy     * @hide
3271766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy     */
32816260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy    @Override
32931f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy    protected void destroyHardwareResources() {
33031f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy        super.destroyHardwareResources();
33131f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy        destroySurface();
33231f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy        invalidateParentCaches();
33331f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy        invalidate(true);
33431f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy    }
33531f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy
33631f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy    @Override
3377e52caf6db5feef2b847cfaa3d13690257122c3aMichael Jurka    HardwareLayer getHardwareLayer() {
338aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (mLayer == null) {
33958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
34058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                return null;
34158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            }
34258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
343a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(mOpaque);
3442af3524beb75150d347accc925022daa53b4a789Jamie Gennis            if (!mUpdateSurface) {
3458a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis                // Create a new SurfaceTexture for the layer.
3462af3524beb75150d347accc925022daa53b4a789Jamie Gennis                mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
3472af3524beb75150d347accc925022daa53b4a789Jamie Gennis            }
348e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
3492af3524beb75150d347accc925022daa53b4a789Jamie Gennis            nCreateNativeWindow(mSurface);
350aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
3518f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() {
352aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                @Override
353aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                public void onFrameAvailable(SurfaceTexture surfaceTexture) {
354aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // Per SurfaceTexture's documentation, the callback may be invoked
355aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // from an arbitrary thread
35688801b270f693ffd4125534724f204135f592f72Romain Guy                    updateLayer();
35752c145ff5ce34980d866155bb154913a847d955bRomain Guy
35852c145ff5ce34980d866155bb154913a847d955bRomain Guy                    if (Looper.myLooper() == Looper.getMainLooper()) {
35952c145ff5ce34980d866155bb154913a847d955bRomain Guy                        invalidate();
36052c145ff5ce34980d866155bb154913a847d955bRomain Guy                    } else {
36152c145ff5ce34980d866155bb154913a847d955bRomain Guy                        postInvalidate();
36252c145ff5ce34980d866155bb154913a847d955bRomain Guy                    }
363aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                }
3648f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            };
3658f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mSurface.setOnFrameAvailableListener(mUpdateListener);
366aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
3678a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis            if (mListener != null && !mUpdateSurface) {
368451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
369aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            }
370d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase            mLayer.setLayerPaint(mLayerPaint);
371aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
372aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
3732af3524beb75150d347accc925022daa53b4a789Jamie Gennis        if (mUpdateSurface) {
3742af3524beb75150d347accc925022daa53b4a789Jamie Gennis            // Someone has requested that we use a specific SurfaceTexture, so
3752af3524beb75150d347accc925022daa53b4a789Jamie Gennis            // tell mLayer about it and set the SurfaceTexture to use the
3762af3524beb75150d347accc925022daa53b4a789Jamie Gennis            // current view size.
3772af3524beb75150d347accc925022daa53b4a789Jamie Gennis            mUpdateSurface = false;
37851f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy
37951f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy            // Since we are updating the layer, force an update to ensure its
38051f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy            // parameters are correct (width, height, transform, etc.)
38188801b270f693ffd4125534724f204135f592f72Romain Guy            updateLayer();
38251f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy            mMatrixChanged = true;
38351f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy
3842af3524beb75150d347accc925022daa53b4a789Jamie Gennis            mAttachInfo.mHardwareRenderer.setSurfaceTexture(mLayer, mSurface);
3852af3524beb75150d347accc925022daa53b4a789Jamie Gennis            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
3862af3524beb75150d347accc925022daa53b4a789Jamie Gennis        }
3872af3524beb75150d347accc925022daa53b4a789Jamie Gennis
38858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        applyUpdate();
389c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        applyTransformMatrix();
390302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
391aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mLayer;
392aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
393aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
3948f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
3958f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onVisibilityChanged(View changedView, int visibility) {
3968f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onVisibilityChanged(changedView, visibility);
3978f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
3988f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
3998f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // When the view becomes invisible, stop updating it, it's a waste of CPU
4008f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // To cancel updates, the easiest thing to do is simply to remove the
4018f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // updates listener
4028f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            if (visibility == VISIBLE) {
4038f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(mUpdateListener);
40488801b270f693ffd4125534724f204135f592f72Romain Guy                updateLayerAndInvalidate();
4058f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            } else {
4068f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(null);
4078f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            }
4088f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
4098f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
4108f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
411a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    private void updateLayer() {
41288801b270f693ffd4125534724f204135f592f72Romain Guy        synchronized (mLock) {
41388801b270f693ffd4125534724f204135f592f72Romain Guy            mUpdateLayer = true;
41488801b270f693ffd4125534724f204135f592f72Romain Guy        }
41588801b270f693ffd4125534724f204135f592f72Romain Guy    }
41688801b270f693ffd4125534724f204135f592f72Romain Guy
41788801b270f693ffd4125534724f204135f592f72Romain Guy    private void updateLayerAndInvalidate() {
41888801b270f693ffd4125534724f204135f592f72Romain Guy        synchronized (mLock) {
41988801b270f693ffd4125534724f204135f592f72Romain Guy            mUpdateLayer = true;
42088801b270f693ffd4125534724f204135f592f72Romain Guy        }
42158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        invalidate();
42258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    }
4232af3524beb75150d347accc925022daa53b4a789Jamie Gennis
42458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private void applyUpdate() {
42558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        if (mLayer == null) {
426a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            return;
427a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
428a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
42958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        synchronized (mLock) {
43058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            if (mUpdateLayer) {
43158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                mUpdateLayer = false;
43258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            } else {
43358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                return;
43458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            }
43558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        }
43658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
43702ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy        mLayer.update(getWidth(), getHeight(), mOpaque);
438a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
439cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        if (mListener != null) {
440cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba            mListener.onSurfaceTextureUpdated(mSurface);
441cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        }
442a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
443a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
444aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
445302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * <p>Sets the transform to associate with this texture view.
446302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * The specified transform applies to the underlying surface
447302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * texture and does not affect the size or position of the view
448302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * itself, only of its content.</p>
449302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
450302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * <p>Some transforms might prevent the content from drawing
451302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * all the pixels contained within this view's bounds. In such
452302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * situations, make sure this texture view is not marked opaque.</p>
453302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
454302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @param transform The transform to apply to the content of
455302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *        this view.
456302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
457302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #getTransform(android.graphics.Matrix)
458302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #isOpaque()
459302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #setOpaque(boolean)
460302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     */
461302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    public void setTransform(Matrix transform) {
462302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        mMatrix.set(transform);
463302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        mMatrixChanged = true;
464c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        invalidateParentIfNeeded();
465302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    }
466302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
467302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    /**
468302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * Returns the transform associated with this texture view.
469302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
470302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @param transform The {@link Matrix} in which to copy the current
471302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *        transform. Can be null.
472302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
473302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @return The specified matrix if not null or a new {@link Matrix}
474302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *         instance otherwise.
475302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
476302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #setTransform(android.graphics.Matrix)
477302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     */
478302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    public Matrix getTransform(Matrix transform) {
479302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        if (transform == null) {
480302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy            transform = new Matrix();
481302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        }
482302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
483302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        transform.set(mMatrix);
484302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
485302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        return transform;
486302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    }
487302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
488c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias    private void applyTransformMatrix() {
48951f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy        if (mMatrixChanged && mLayer != null) {
490c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias            mLayer.setTransform(mMatrix);
491c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias            mMatrixChanged = false;
492c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        }
493c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias    }
494c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias
495302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    /**
49677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
49777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
49877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
49977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
50077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
50177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format and its dimensions are the same as this view's.</p>
50277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
50377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
50477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
50577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
506d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
507d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
50877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
50977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or the width &lt;= 0 or the height &lt;= 0
51077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
51177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
51277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
51377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
51477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
51577a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap() {
51677a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return getBitmap(getWidth(), getHeight());
51777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
51877a811610f99e21da7f88dafef60d09f345d0506Romain Guy
51977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
52077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
52177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
52277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
52377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
52477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
52577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format.</p>
52677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
52777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
52877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
52977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
530d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
531d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
53277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param width The width of the bitmap to create
53377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param height The height of the bitmap to create
53477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
53577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
53677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or width is &lt;= 0 or height is &lt;= 0
53777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
53877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
53977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
54077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
54177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
54277a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(int width, int height) {
54377a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (isAvailable() && width > 0 && height > 0) {
544dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            return getBitmap(Bitmap.createBitmap(getResources().getDisplayMetrics(),
545dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn                    width, height, Bitmap.Config.ARGB_8888));
54677a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
54777a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return null;
54877a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
54977a811610f99e21da7f88dafef60d09f345d0506Romain Guy
55077a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
55177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Copies the content of this view's surface texture into the specified
55277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * bitmap. If the surface texture is not available, the copy is not executed.
55377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * The content of the surface texture will be scaled to fit exactly inside
55477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * the specified bitmap.</p>
55577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
55677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
55777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
55877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
559d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs, the bitmap is left unchanged.</p>
560d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
56177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param bitmap The bitmap to copy the content of the surface texture into,
56277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *               cannot be null, all configurations are supported
56377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
56477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return The bitmap specified as a parameter
56577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
56677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
56777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
56877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
569589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy     *
570589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy     * @throws IllegalStateException if the hardware rendering context cannot be
571589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy     *         acquired to capture the bitmap
57277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
57377a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(Bitmap bitmap) {
57477a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (bitmap != null && isAvailable()) {
575589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy            AttachInfo info = mAttachInfo;
576589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy            if (info != null && info.mHardwareRenderer != null &&
577589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy                    info.mHardwareRenderer.isEnabled()) {
578589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy                if (!info.mHardwareRenderer.validate()) {
579589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy                    throw new IllegalStateException("Could not acquire hardware rendering context");
580589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy                }
581589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy            }
582589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy
583589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy            applyUpdate();
584589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy            applyTransformMatrix();
585589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy
58678245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy            // This case can happen if the app invokes setSurfaceTexture() before
58778245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy            // we are able to create the hardware layer. We can safely initialize
58878245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy            // the layer here thanks to the validate() call at the beginning of
58978245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy            // this method
59078245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy            if (mLayer == null && mUpdateSurface) {
59178245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy                getHardwareLayer();
59278245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy            }
59378245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy
59478245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy            if (mLayer != null) {
59578245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy                mLayer.copyInto(bitmap);
59678245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy            }
59777a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
59877a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return bitmap;
59977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
60077a811610f99e21da7f88dafef60d09f345d0506Romain Guy
60177a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
60277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * Returns true if the {@link SurfaceTexture} associated with this
60377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * TextureView is available for rendering. When this method returns
60477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * true, {@link #getSurfaceTexture()} returns a valid surface texture.
60577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
60677a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public boolean isAvailable() {
60777a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return mSurface != null;
60877a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
60977a811610f99e21da7f88dafef60d09f345d0506Romain Guy
61077a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
6116be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * <p>Start editing the pixels in the surface.  The returned Canvas can be used
6126be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to draw into the surface's bitmap.  A null is returned if the surface has
6136be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * not been created or otherwise cannot be edited. You will usually need
6146be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to implement
6156be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * {@link SurfaceTextureListener#onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int)}
6166be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to find out when the Surface is available for use.</p>
6176be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6186be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * <p>The content of the Surface is never preserved between unlockCanvas()
6196be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * and lockCanvas(), for this reason, every pixel within the Surface area
6206be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * must be written. The only exception to this rule is when a dirty
6216be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * rectangle is specified, in which case, non-dirty pixels will be
6226be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * preserved.</p>
623462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy     *
624462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy     * <p>This method can only be used if the underlying surface is not already
625462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy     * owned by another producer. For instance, if the TextureView is being used
626462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy     * to render the camera's preview you cannot invoke this method.</p>
6276be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6286be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @return A Canvas used to draw into the surface.
6296be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6306be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas(android.graphics.Rect)
6316be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #unlockCanvasAndPost(android.graphics.Canvas)
6326be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     */
6336be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    public Canvas lockCanvas() {
6346be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        return lockCanvas(null);
6356be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    }
6366be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6376be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    /**
6386be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * Just like {@link #lockCanvas()} but allows specification of a dirty
6396be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * rectangle. Every pixel within that rectangle must be written; however
6406be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * pixels outside the dirty rectangle will be preserved by the next call
6416be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to lockCanvas().
6426be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6436be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @param dirty Area of the surface that will be modified.
6446be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6456be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @return A Canvas used to draw into the surface.
6466be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6476be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas()
6486be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #unlockCanvasAndPost(android.graphics.Canvas)
6496be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     */
6506be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    public Canvas lockCanvas(Rect dirty) {
6516be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        if (!isAvailable()) return null;
6526be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6536be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        if (mCanvas == null) {
6546be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            mCanvas = new Canvas();
6556be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        }
6566be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6576be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        synchronized (mNativeWindowLock) {
6586be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            nLockCanvas(mNativeWindow, mCanvas, dirty);
6596be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        }
6606be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        mSaveCount = mCanvas.save();
6616be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6626be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        return mCanvas;
6636be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    }
6646be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6656be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    /**
6666be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * Finish editing pixels in the surface. After this call, the surface's
6676be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * current pixels will be shown on the screen, but its content is lost,
6686be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * in particular there is no guarantee that the content of the Surface
6696be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * will remain unchanged when lockCanvas() is called again.
6706be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6716be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @param canvas The Canvas previously returned by lockCanvas()
6726be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6736be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas()
6746be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas(android.graphics.Rect)
6756be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     */
6766be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    public void unlockCanvasAndPost(Canvas canvas) {
6776be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        if (mCanvas != null && canvas == mCanvas) {
6786be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            canvas.restoreToCount(mSaveCount);
6796be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            mSaveCount = 0;
6806be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6816be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            synchronized (mNativeWindowLock) {
6826be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy                nUnlockCanvasAndPost(mNativeWindow, mCanvas);
6836be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            }
6846be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        }
6856be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    }
6866be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6876be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    /**
688aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTexture} used by this view. This method
68977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * may return null if the view is not attached to a window or if the surface
69077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * texture has not been initialized yet.
69177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
69277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
693aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
694aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTexture getSurfaceTexture() {
695aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mSurface;
696aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
697aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
698aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
6992af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * Set the {@link SurfaceTexture} for this view to use. If a {@link
7002af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * SurfaceTexture} is already being used by this view, it is immediately
7012af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * released and not be usable any more.  The {@link
7022af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * SurfaceTextureListener#onSurfaceTextureDestroyed} callback is <b>not</b>
7038a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis     * called for the previous {@link SurfaceTexture}.  Similarly, the {@link
7048a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis     * SurfaceTextureListener#onSurfaceTextureAvailable} callback is <b>not</b>
7058a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis     * called for the {@link SurfaceTexture} passed to setSurfaceTexture.
7062af3524beb75150d347accc925022daa53b4a789Jamie Gennis     *
7072af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * The {@link SurfaceTexture} object must be detached from all OpenGL ES
7082af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * contexts prior to calling this method.
7092af3524beb75150d347accc925022daa53b4a789Jamie Gennis     *
7102af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * @param surfaceTexture The {@link SurfaceTexture} that the view should use.
7112af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * @see SurfaceTexture#detachFromGLContext()
7122af3524beb75150d347accc925022daa53b4a789Jamie Gennis     */
7132af3524beb75150d347accc925022daa53b4a789Jamie Gennis    public void setSurfaceTexture(SurfaceTexture surfaceTexture) {
7142af3524beb75150d347accc925022daa53b4a789Jamie Gennis        if (surfaceTexture == null) {
7152af3524beb75150d347accc925022daa53b4a789Jamie Gennis            throw new NullPointerException("surfaceTexture must not be null");
7162af3524beb75150d347accc925022daa53b4a789Jamie Gennis        }
7172af3524beb75150d347accc925022daa53b4a789Jamie Gennis        if (mSurface != null) {
7182af3524beb75150d347accc925022daa53b4a789Jamie Gennis            mSurface.release();
7192af3524beb75150d347accc925022daa53b4a789Jamie Gennis        }
7202af3524beb75150d347accc925022daa53b4a789Jamie Gennis        mSurface = surfaceTexture;
7212af3524beb75150d347accc925022daa53b4a789Jamie Gennis        mUpdateSurface = true;
7222af3524beb75150d347accc925022daa53b4a789Jamie Gennis        invalidateParentIfNeeded();
7232af3524beb75150d347accc925022daa53b4a789Jamie Gennis    }
7242af3524beb75150d347accc925022daa53b4a789Jamie Gennis
7252af3524beb75150d347accc925022daa53b4a789Jamie Gennis    /**
726aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTextureListener} currently associated with this
727aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture view.
728aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
729aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener)
730aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
731aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
732aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTextureListener getSurfaceTextureListener() {
733aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mListener;
734aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
735aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
736aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
737aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Sets the {@link SurfaceTextureListener} used to listen to surface
738aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture events.
739aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
740aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #getSurfaceTextureListener()
741aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
742aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
743aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setSurfaceTextureListener(SurfaceTextureListener listener) {
744aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mListener = listener;
745aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
746aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
747aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
748aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * This listener can be used to be notified when the surface texture
749aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * associated with this texture view is available.
750aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
751aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public static interface SurfaceTextureListener {
752aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        /**
753aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use.
754aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *
755aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * @param surface The surface returned by
756aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
757451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param width The width of the surface
758451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param height The height of the surface
759aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         */
760451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height);
7618f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
7628f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        /**
7638f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * Invoked when the {@link SurfaceTexture}'s buffers size changed.
7648f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *
7658f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param surface The surface returned by
7668f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
7678f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param width The new width of the surface
7688f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param height The new height of the surface
7698f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         */
7708f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height);
771451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
772451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        /**
773451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * Invoked when the specified {@link SurfaceTexture} is about to be destroyed.
774402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba         * If returns true, no rendering should happen inside the surface texture after this method
775402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba         * is invoked. If returns false, the client needs to call {@link SurfaceTexture#release()}.
776451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         *
777451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param surface The surface about to be destroyed
778451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         */
779402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface);
780cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba
781cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        /**
782cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * Invoked when the specified {@link SurfaceTexture} is updated through
783cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * {@link SurfaceTexture#updateTexImage()}.
784cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         *
785cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * @param surface The surface just updated
786cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         */
787cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        public void onSurfaceTextureUpdated(SurfaceTexture surface);
788aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
7898f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
7906be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private native void nCreateNativeWindow(SurfaceTexture surface);
7916be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private native void nDestroyNativeWindow();
7926be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
793d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy    private static native void nSetDefaultBufferSize(SurfaceTexture surfaceTexture,
794d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy            int width, int height);
7956be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
7966be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private static native void nLockCanvas(int nativeWindow, Canvas canvas, Rect dirty);
7976be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private static native void nUnlockCanvasAndPost(int nativeWindow, Canvas canvas);
798aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy}
799