TextureView.java revision a8a2f97c100af4ba52c61c3b59c933f44a53dad4
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;
26aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.AttributeSet;
27aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.Log;
28aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
29aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/**
30aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView can be used to display a content stream. Such a content
31aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * stream can for instance be a video or an OpenGL scene. The content stream
32aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can come from the application's process as well as a remote process.</p>
33aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
34aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>TextureView can only be used in a hardware accelerated window. When
35aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * rendered in software, TextureView will draw nothing.</p>
36aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
37aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Unlike {@link SurfaceView}, TextureView does not create a separate
38aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * window but behaves as a regular View. This key difference allows a
39aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView to be moved, transformed, animated, etc. For instance, you
40aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can make a TextureView semi-translucent by calling
41aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <code>myView.setAlpha(0.5f)</code>.</p>
42aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
43aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Using a TextureView is simple: all you need to do is get its
44aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link SurfaceTexture}. The {@link SurfaceTexture} can then be used to
45aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * render content. The following example demonstrates how to render the
46aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * camera preview into a TextureView:</p>
47aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
48aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <pre>
49aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *  public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
50aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      private Camera mCamera;
51aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      private TextureView mTextureView;
52aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
53aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      protected void onCreate(Bundle savedInstanceState) {
54aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          super.onCreate(savedInstanceState);
55aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
56aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mTextureView = new TextureView(this);
57aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mTextureView.setSurfaceTextureListener(this);
58aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
59aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          setContentView(mTextureView);
60aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      }
61aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
62451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
63aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mCamera = Camera.open();
64aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
65aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          try {
66aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              mCamera.setPreviewTexture(surface);
67aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              mCamera.startPreview();
68aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          } catch (IOException ioe) {
69aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              // Something bad happened
70aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          }
71aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      }
72cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *
738f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *      public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
748f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *          // Ignored, Camera does all the work for us
758f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *      }
76cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *
77402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba *      public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
78451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *          mCamera.stopPreview();
79451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *          mCamera.release();
80402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba *          return true;
81451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *      }
82cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *
83cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *      public void onSurfaceTextureUpdated(SurfaceTexture surface) {
8458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy *          // Invoked every time there's a new Camera preview frame
85cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba *      }
86aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *  }
87aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * </pre>
88aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
89aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView's SurfaceTexture can be obtained either by invoking
90aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #getSurfaceTexture()} or by using a {@link SurfaceTextureListener}.
91aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * It is important to know that a SurfaceTexture is available only after the
92aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView is attached to a window (and {@link #onAttachedToWindow()} has
93aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * been invoked.) It is therefore highly recommended you use a listener to
94aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * be notified when the SurfaceTexture becomes available.</p>
95aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
96462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * <p>It is important to note that only one producer can use the TextureView.
97462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * For instance, if you use a TextureView to display the camera preview, you
98462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * cannot use {@link #lockCanvas()} to draw onto the TextureView at the same
99462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * time.</p>
100462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy *
101aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceView
102aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTexture
103aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */
104aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypublic class TextureView extends View {
10577a811610f99e21da7f88dafef60d09f345d0506Romain Guy    private static final String LOG_TAG = "TextureView";
10677a811610f99e21da7f88dafef60d09f345d0506Romain Guy
107aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private HardwareLayer mLayer;
108aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private SurfaceTexture mSurface;
109aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private SurfaceTextureListener mListener;
110aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
111a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    private boolean mOpaque = true;
112c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy
113302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    private final Matrix mMatrix = new Matrix();
114302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    private boolean mMatrixChanged;
115302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
11658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private final Object[] mLock = new Object[0];
11758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private boolean mUpdateLayer;
1182af3524beb75150d347accc925022daa53b4a789Jamie Gennis    private boolean mUpdateSurface;
11958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
1208f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    private SurfaceTexture.OnFrameAvailableListener mUpdateListener;
121aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
1226be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private Canvas mCanvas;
1236be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private int mSaveCount;
1246be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
1256be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private final Object[] mNativeWindowLock = new Object[0];
1266be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    // Used from native code, do not write!
1276be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
1286be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private int mNativeWindow;
1296be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
130aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
131aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
132aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
133aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
134aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
135aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context) {
136aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context);
137aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
138aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
139aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
140aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
141aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
142aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
143aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
144aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
145aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
146aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
147aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs) {
148aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs);
149aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
150aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
151aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
152aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
153aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
154aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
155aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
156aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
157aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param defStyle The default style to apply to this view. If 0, no style
158aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        will be applied (beyond what is included in the theme). This may
159aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        either be an attribute resource, whose value will be retrieved
160aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        from the current theme, or an explicit style resource.
161aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
162aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
163aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs, int defStyle) {
164aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs, defStyle);
165aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
166aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
167aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
168aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private void init() {
169aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mLayerPaint = new Paint();
170aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
171aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
172a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    /**
173a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * {@inheritDoc}
174a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     */
175a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    @Override
176a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    public boolean isOpaque() {
177a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        return mOpaque;
178a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
179a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
180a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    /**
181a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * Indicates whether the content of this TextureView is opaque. The
182a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * content is assumed to be opaque by default.
183a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     *
184a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * @param opaque True if the content of this TextureView is opaque,
185a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     *               false otherwise
186a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     */
187a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    public void setOpaque(boolean opaque) {
188a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        if (opaque != mOpaque) {
189a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            mOpaque = opaque;
190a8a2f97c100af4ba52c61c3b59c933f44a53dad4Romain Guy            if (mLayer != null) {
191a8a2f97c100af4ba52c61c3b59c933f44a53dad4Romain Guy                updateLayer();
192a8a2f97c100af4ba52c61c3b59c933f44a53dad4Romain Guy            }
193a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
194a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
195a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
196aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
197aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected void onAttachedToWindow() {
198aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super.onAttachedToWindow();
199aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
200aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (!isHardwareAccelerated()) {
20177a811610f99e21da7f88dafef60d09f345d0506Romain Guy            Log.w(LOG_TAG, "A TextureView or a subclass can only be "
202aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    + "used with hardware acceleration enabled.");
203aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
204aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
205aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
206451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    @Override
207451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    protected void onDetachedFromWindow() {
208451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        super.onDetachedFromWindow();
2091ac4765e959c79101f64b1279887ed469334c268Romain Guy        if (mLayer != null && mAttachInfo != null && mAttachInfo.mHardwareRenderer != null) {
2101ac4765e959c79101f64b1279887ed469334c268Romain Guy            boolean success = mAttachInfo.mHardwareRenderer.safelyRun(new Runnable() {
2111ac4765e959c79101f64b1279887ed469334c268Romain Guy                @Override
2121ac4765e959c79101f64b1279887ed469334c268Romain Guy                public void run() {
2131ac4765e959c79101f64b1279887ed469334c268Romain Guy                    destroySurface();
2141ac4765e959c79101f64b1279887ed469334c268Romain Guy                }
2151ac4765e959c79101f64b1279887ed469334c268Romain Guy            });
2161ac4765e959c79101f64b1279887ed469334c268Romain Guy
2171ac4765e959c79101f64b1279887ed469334c268Romain Guy            if (!success) {
2181ac4765e959c79101f64b1279887ed469334c268Romain Guy                Log.w(LOG_TAG, "TextureView was not able to destroy its surface: " + this);
2191ac4765e959c79101f64b1279887ed469334c268Romain Guy            }
2201ac4765e959c79101f64b1279887ed469334c268Romain Guy        }
22131f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy    }
222451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
22331f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy    private void destroySurface() {
22480429c458506485904715180d10584092a5cd082Romain Guy        if (mLayer != null) {
2252af3524beb75150d347accc925022daa53b4a789Jamie Gennis            mSurface.detachFromGLContext();
2262af3524beb75150d347accc925022daa53b4a789Jamie Gennis
227402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba            boolean shouldRelease = true;
228451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
229402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba                shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface);
230451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
231451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
2326be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            synchronized (mNativeWindowLock) {
2336be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy                nDestroyNativeWindow();
2346be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            }
2356be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
2366be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            mLayer.destroy();
237402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba            if (shouldRelease) mSurface.release();
238451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mSurface = null;
239451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mLayer = null;
240451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        }
241451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    }
242451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
243aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
244aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * The layer type of a TextureView is ignored since a TextureView is always
245aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * considered to act as a hardware layer. The optional paint supplied to this
246aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * method will however be taken into account when rendering the content of
247aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * this TextureView.
248aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
249aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param layerType The ype of layer to use with this view, must be one of
250aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
251aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_HARDWARE}
252aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param paint The paint used to compose the layer. This argument is optional
253aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        and can be null. It is ignored when the layer type is
254aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}
255aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
256aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
257aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setLayerType(int layerType, Paint paint) {
258aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (paint != mLayerPaint) {
259aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            mLayerPaint = paint;
260aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            invalidate();
261aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
262aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
263aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
264aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
265aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Always returns {@link #LAYER_TYPE_HARDWARE}.
266aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
267aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
268aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public int getLayerType() {
269aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return LAYER_TYPE_HARDWARE;
270aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
271aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
27259c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy    @Override
27359c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy    boolean hasStaticLayer() {
27459c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy        return true;
27559c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy    }
27659c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy
277aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
278aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Calling this method has no effect.
279aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
280aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
281aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void buildLayer() {
282aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
283aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
284aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
285aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
286aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
287aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
288aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
289aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
290aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
291aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public final void draw(Canvas canvas) {
29258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        applyUpdate();
293c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        applyTransformMatrix();
294aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
295aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
296aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
297aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
298aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
299aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
300aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
301aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
302aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
303aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected final void onDraw(Canvas canvas) {
304aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
305aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
306aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
3078f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
3088f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onSizeChanged(w, h, oldw, oldh);
3098f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
310e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
311451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
312451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight());
313451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
3148f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
3158f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
3168f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
3178f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
318a998dff5d49a423aaf7097aa8f96bf5bdc681d25Romain Guy    boolean destroyLayer(boolean valid) {
31916260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy        return false;
32016260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy    }
32116260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy
3221766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy    /**
3231766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy     * @hide
3241766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy     */
32516260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy    @Override
32631f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy    protected void destroyHardwareResources() {
32731f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy        super.destroyHardwareResources();
32831f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy        destroySurface();
32931f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy        invalidateParentCaches();
33031f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy        invalidate(true);
33131f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy    }
33231f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy
33331f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy    @Override
3347e52caf6db5feef2b847cfaa3d13690257122c3aMichael Jurka    HardwareLayer getHardwareLayer() {
335aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (mLayer == null) {
33658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
33758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                return null;
33858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            }
33958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
340a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(mOpaque);
3412af3524beb75150d347accc925022daa53b4a789Jamie Gennis            if (!mUpdateSurface) {
3422af3524beb75150d347accc925022daa53b4a789Jamie Gennis                // We already have a SurfaceTexture to use, and we will pass it
3432af3524beb75150d347accc925022daa53b4a789Jamie Gennis                // to mLayer below.
3442af3524beb75150d347accc925022daa53b4a789Jamie Gennis                mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
3452af3524beb75150d347accc925022daa53b4a789Jamie Gennis            }
346e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
3472af3524beb75150d347accc925022daa53b4a789Jamie Gennis            nCreateNativeWindow(mSurface);
348aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
3498f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() {
350aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                @Override
351aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                public void onFrameAvailable(SurfaceTexture surfaceTexture) {
352aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // Per SurfaceTexture's documentation, the callback may be invoked
353aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // from an arbitrary thread
35458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                    synchronized (mLock) {
35558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                        mUpdateLayer = true;
35658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                    }
3576cb7b46c56449e84434b11eb12f9b8977fcd0398Jeff Brown                    postInvalidate();
358aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                }
3598f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            };
3608f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mSurface.setOnFrameAvailableListener(mUpdateListener);
361aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
362aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            if (mListener != null) {
363451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
364aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            }
365aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
366aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
3672af3524beb75150d347accc925022daa53b4a789Jamie Gennis        if (mUpdateSurface) {
3682af3524beb75150d347accc925022daa53b4a789Jamie Gennis            // Someone has requested that we use a specific SurfaceTexture, so
3692af3524beb75150d347accc925022daa53b4a789Jamie Gennis            // tell mLayer about it and set the SurfaceTexture to use the
3702af3524beb75150d347accc925022daa53b4a789Jamie Gennis            // current view size.
3712af3524beb75150d347accc925022daa53b4a789Jamie Gennis            mUpdateSurface = false;
3722af3524beb75150d347accc925022daa53b4a789Jamie Gennis            mAttachInfo.mHardwareRenderer.setSurfaceTexture(mLayer, mSurface);
3732af3524beb75150d347accc925022daa53b4a789Jamie Gennis            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
3742af3524beb75150d347accc925022daa53b4a789Jamie Gennis        }
3752af3524beb75150d347accc925022daa53b4a789Jamie Gennis
37658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        applyUpdate();
377c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        applyTransformMatrix();
378302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
379aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mLayer;
380aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
381aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
3828f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
3838f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onVisibilityChanged(View changedView, int visibility) {
3848f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onVisibilityChanged(changedView, visibility);
3858f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
3868f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
3878f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // When the view becomes invisible, stop updating it, it's a waste of CPU
3888f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // To cancel updates, the easiest thing to do is simply to remove the
3898f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // updates listener
3908f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            if (visibility == VISIBLE) {
3918f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(mUpdateListener);
392a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy                updateLayer();
3938f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            } else {
3948f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(null);
3958f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            }
3968f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
3978f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
3988f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
399a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    private void updateLayer() {
40058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        mUpdateLayer = true;
40158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        invalidate();
40258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    }
4032af3524beb75150d347accc925022daa53b4a789Jamie Gennis
40458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private void applyUpdate() {
40558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        if (mLayer == null) {
406a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            return;
407a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
408a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
40958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        synchronized (mLock) {
41058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            if (mUpdateLayer) {
41158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                mUpdateLayer = false;
41258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            } else {
41358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                return;
41458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            }
41558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        }
41658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
41702ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy        mLayer.update(getWidth(), getHeight(), mOpaque);
418a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
419cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        if (mListener != null) {
420cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba            mListener.onSurfaceTextureUpdated(mSurface);
421cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        }
422a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
423a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
424aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
425302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * <p>Sets the transform to associate with this texture view.
426302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * The specified transform applies to the underlying surface
427302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * texture and does not affect the size or position of the view
428302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * itself, only of its content.</p>
429302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
430302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * <p>Some transforms might prevent the content from drawing
431302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * all the pixels contained within this view's bounds. In such
432302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * situations, make sure this texture view is not marked opaque.</p>
433302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
434302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @param transform The transform to apply to the content of
435302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *        this view.
436302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
437302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #getTransform(android.graphics.Matrix)
438302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #isOpaque()
439302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #setOpaque(boolean)
440302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     */
441302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    public void setTransform(Matrix transform) {
442302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        mMatrix.set(transform);
443302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        mMatrixChanged = true;
444c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        invalidateParentIfNeeded();
445302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    }
446302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
447302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    /**
448302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * Returns the transform associated with this texture view.
449302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
450302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @param transform The {@link Matrix} in which to copy the current
451302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *        transform. Can be null.
452302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
453302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @return The specified matrix if not null or a new {@link Matrix}
454302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *         instance otherwise.
455302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
456302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #setTransform(android.graphics.Matrix)
457302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     */
458302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    public Matrix getTransform(Matrix transform) {
459302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        if (transform == null) {
460302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy            transform = new Matrix();
461302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        }
462302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
463302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        transform.set(mMatrix);
464302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
465302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        return transform;
466302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    }
467302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
468c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias    private void applyTransformMatrix() {
469c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        if (mMatrixChanged) {
470c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias            mLayer.setTransform(mMatrix);
471c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias            mMatrixChanged = false;
472c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        }
473c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias    }
474c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias
475302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    /**
47677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
47777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
47877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
47977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
48077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
48177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format and its dimensions are the same as this view's.</p>
48277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
48377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
48477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
48577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
486d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
487d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
48877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
48977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or the width &lt;= 0 or the height &lt;= 0
49077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
49177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
49277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
49377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
49477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
49577a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap() {
49677a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return getBitmap(getWidth(), getHeight());
49777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
49877a811610f99e21da7f88dafef60d09f345d0506Romain Guy
49977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
50077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
50177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
50277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
50377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
50477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
50577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format.</p>
50677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
50777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
50877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
50977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
510d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
511d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
51277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param width The width of the bitmap to create
51377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param height The height of the bitmap to create
51477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
51577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
51677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or width is &lt;= 0 or height is &lt;= 0
51777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
51877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
51977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
52077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
52177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
52277a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(int width, int height) {
52377a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (isAvailable() && width > 0 && height > 0) {
52477a811610f99e21da7f88dafef60d09f345d0506Romain Guy            return getBitmap(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
52577a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
52677a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return null;
52777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
52877a811610f99e21da7f88dafef60d09f345d0506Romain Guy
52977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
53077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Copies the content of this view's surface texture into the specified
53177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * bitmap. If the surface texture is not available, the copy is not executed.
53277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * The content of the surface texture will be scaled to fit exactly inside
53377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * the specified bitmap.</p>
53477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
53577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
53677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
53777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
538d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs, the bitmap is left unchanged.</p>
539d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
54077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param bitmap The bitmap to copy the content of the surface texture into,
54177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *               cannot be null, all configurations are supported
54277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
54377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return The bitmap specified as a parameter
54477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
54577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
54677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
54777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
548589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy     *
549589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy     * @throws IllegalStateException if the hardware rendering context cannot be
550589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy     *         acquired to capture the bitmap
55177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
55277a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(Bitmap bitmap) {
55377a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (bitmap != null && isAvailable()) {
554589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy            AttachInfo info = mAttachInfo;
555589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy            if (info != null && info.mHardwareRenderer != null &&
556589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy                    info.mHardwareRenderer.isEnabled()) {
557589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy                if (!info.mHardwareRenderer.validate()) {
558589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy                    throw new IllegalStateException("Could not acquire hardware rendering context");
559589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy                }
560589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy            }
561589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy
562589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy            applyUpdate();
563589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy            applyTransformMatrix();
564589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy
56502ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy            mLayer.copyInto(bitmap);
56677a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
56777a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return bitmap;
56877a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
56977a811610f99e21da7f88dafef60d09f345d0506Romain Guy
57077a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
57177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * Returns true if the {@link SurfaceTexture} associated with this
57277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * TextureView is available for rendering. When this method returns
57377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * true, {@link #getSurfaceTexture()} returns a valid surface texture.
57477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
57577a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public boolean isAvailable() {
57677a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return mSurface != null;
57777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
57877a811610f99e21da7f88dafef60d09f345d0506Romain Guy
57977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
5806be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * <p>Start editing the pixels in the surface.  The returned Canvas can be used
5816be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to draw into the surface's bitmap.  A null is returned if the surface has
5826be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * not been created or otherwise cannot be edited. You will usually need
5836be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to implement
5846be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * {@link SurfaceTextureListener#onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int)}
5856be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to find out when the Surface is available for use.</p>
5866be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
5876be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * <p>The content of the Surface is never preserved between unlockCanvas()
5886be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * and lockCanvas(), for this reason, every pixel within the Surface area
5896be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * must be written. The only exception to this rule is when a dirty
5906be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * rectangle is specified, in which case, non-dirty pixels will be
5916be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * preserved.</p>
592462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy     *
593462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy     * <p>This method can only be used if the underlying surface is not already
594462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy     * owned by another producer. For instance, if the TextureView is being used
595462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy     * to render the camera's preview you cannot invoke this method.</p>
5966be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
5976be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @return A Canvas used to draw into the surface.
5986be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
5996be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas(android.graphics.Rect)
6006be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #unlockCanvasAndPost(android.graphics.Canvas)
6016be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     */
6026be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    public Canvas lockCanvas() {
6036be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        return lockCanvas(null);
6046be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    }
6056be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6066be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    /**
6076be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * Just like {@link #lockCanvas()} but allows specification of a dirty
6086be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * rectangle. Every pixel within that rectangle must be written; however
6096be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * pixels outside the dirty rectangle will be preserved by the next call
6106be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to lockCanvas().
6116be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6126be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @param dirty Area of the surface that will be modified.
6136be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6146be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @return A Canvas used to draw into the surface.
6156be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6166be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas()
6176be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #unlockCanvasAndPost(android.graphics.Canvas)
6186be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     */
6196be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    public Canvas lockCanvas(Rect dirty) {
6206be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        if (!isAvailable()) return null;
6216be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6226be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        if (mCanvas == null) {
6236be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            mCanvas = new Canvas();
6246be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        }
6256be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6266be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        synchronized (mNativeWindowLock) {
6276be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            nLockCanvas(mNativeWindow, mCanvas, dirty);
6286be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        }
6296be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        mSaveCount = mCanvas.save();
6306be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6316be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        return mCanvas;
6326be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    }
6336be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6346be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    /**
6356be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * Finish editing pixels in the surface. After this call, the surface's
6366be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * current pixels will be shown on the screen, but its content is lost,
6376be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * in particular there is no guarantee that the content of the Surface
6386be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * will remain unchanged when lockCanvas() is called again.
6396be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6406be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @param canvas The Canvas previously returned by lockCanvas()
6416be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
6426be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas()
6436be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas(android.graphics.Rect)
6446be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     */
6456be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    public void unlockCanvasAndPost(Canvas canvas) {
6466be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        if (mCanvas != null && canvas == mCanvas) {
6476be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            canvas.restoreToCount(mSaveCount);
6486be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            mSaveCount = 0;
6496be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6506be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            synchronized (mNativeWindowLock) {
6516be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy                nUnlockCanvasAndPost(mNativeWindow, mCanvas);
6526be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            }
6536be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        }
6546be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    }
6556be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6566be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    /**
657aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTexture} used by this view. This method
65877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * may return null if the view is not attached to a window or if the surface
65977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * texture has not been initialized yet.
66077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
66177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
662aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
663aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTexture getSurfaceTexture() {
664aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mSurface;
665aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
666aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
667aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
6682af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * Set the {@link SurfaceTexture} for this view to use. If a {@link
6692af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * SurfaceTexture} is already being used by this view, it is immediately
6702af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * released and not be usable any more.  The {@link
6712af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * SurfaceTextureListener#onSurfaceTextureDestroyed} callback is <b>not</b>
6722af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * called.
6732af3524beb75150d347accc925022daa53b4a789Jamie Gennis     *
6742af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * The {@link SurfaceTexture} object must be detached from all OpenGL ES
6752af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * contexts prior to calling this method.
6762af3524beb75150d347accc925022daa53b4a789Jamie Gennis     *
6772af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * @param surfaceTexture The {@link SurfaceTexture} that the view should use.
6782af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * @see SurfaceTexture#detachFromGLContext()
6792af3524beb75150d347accc925022daa53b4a789Jamie Gennis     * @hide
6802af3524beb75150d347accc925022daa53b4a789Jamie Gennis     */
6812af3524beb75150d347accc925022daa53b4a789Jamie Gennis    public void setSurfaceTexture(SurfaceTexture surfaceTexture) {
6822af3524beb75150d347accc925022daa53b4a789Jamie Gennis        if (surfaceTexture == null) {
6832af3524beb75150d347accc925022daa53b4a789Jamie Gennis            throw new NullPointerException("surfaceTexture must not be null");
6842af3524beb75150d347accc925022daa53b4a789Jamie Gennis        }
6852af3524beb75150d347accc925022daa53b4a789Jamie Gennis        if (mSurface != null) {
6862af3524beb75150d347accc925022daa53b4a789Jamie Gennis            mSurface.release();
6872af3524beb75150d347accc925022daa53b4a789Jamie Gennis        }
6882af3524beb75150d347accc925022daa53b4a789Jamie Gennis        mSurface = surfaceTexture;
6892af3524beb75150d347accc925022daa53b4a789Jamie Gennis        mUpdateSurface = true;
6902af3524beb75150d347accc925022daa53b4a789Jamie Gennis        invalidateParentIfNeeded();
6912af3524beb75150d347accc925022daa53b4a789Jamie Gennis    }
6922af3524beb75150d347accc925022daa53b4a789Jamie Gennis
6932af3524beb75150d347accc925022daa53b4a789Jamie Gennis    /**
694aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTextureListener} currently associated with this
695aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture view.
696aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
697aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener)
698aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
699aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
700aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTextureListener getSurfaceTextureListener() {
701aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mListener;
702aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
703aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
704aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
705aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Sets the {@link SurfaceTextureListener} used to listen to surface
706aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture events.
707aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
708aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #getSurfaceTextureListener()
709aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
710aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
711aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setSurfaceTextureListener(SurfaceTextureListener listener) {
712aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mListener = listener;
713aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
714aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
715aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
716aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * This listener can be used to be notified when the surface texture
717aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * associated with this texture view is available.
718aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
719aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public static interface SurfaceTextureListener {
720aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        /**
721aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use.
722aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *
723aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * @param surface The surface returned by
724aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
725451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param width The width of the surface
726451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param height The height of the surface
727aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         */
728451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height);
7298f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
7308f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        /**
7318f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * Invoked when the {@link SurfaceTexture}'s buffers size changed.
7328f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *
7338f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param surface The surface returned by
7348f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
7358f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param width The new width of the surface
7368f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param height The new height of the surface
7378f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         */
7388f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height);
739451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
740451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        /**
741451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * Invoked when the specified {@link SurfaceTexture} is about to be destroyed.
742402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba         * If returns true, no rendering should happen inside the surface texture after this method
743402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba         * is invoked. If returns false, the client needs to call {@link SurfaceTexture#release()}.
744451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         *
745451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param surface The surface about to be destroyed
746451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         */
747402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface);
748cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba
749cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        /**
750cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * Invoked when the specified {@link SurfaceTexture} is updated through
751cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * {@link SurfaceTexture#updateTexImage()}.
752cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         *
753cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * @param surface The surface just updated
754cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         */
755cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        public void onSurfaceTextureUpdated(SurfaceTexture surface);
756aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
7578f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
7586be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private native void nCreateNativeWindow(SurfaceTexture surface);
7596be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private native void nDestroyNativeWindow();
7606be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
761d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy    private static native void nSetDefaultBufferSize(SurfaceTexture surfaceTexture,
762d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy            int width, int height);
7636be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
7646be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private static native void nLockCanvas(int nativeWindow, Canvas canvas, Rect dirty);
7656be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private static native void nUnlockCanvasAndPost(int nativeWindow, Canvas canvas);
766aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy}
767