TextureView.java revision c01391fb4eb0eef33d142e89e060eac7e75de39d
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 *
96aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceView
97aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTexture
98aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */
99aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypublic class TextureView extends View {
10077a811610f99e21da7f88dafef60d09f345d0506Romain Guy    private static final String LOG_TAG = "TextureView";
10177a811610f99e21da7f88dafef60d09f345d0506Romain Guy
102aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private HardwareLayer mLayer;
103aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private SurfaceTexture mSurface;
104aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private SurfaceTextureListener mListener;
105aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
106a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    private boolean mOpaque = true;
107c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy
108302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    private final Matrix mMatrix = new Matrix();
109302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    private boolean mMatrixChanged;
110302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
11158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private final Object[] mLock = new Object[0];
11258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private boolean mUpdateLayer;
11358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
1148f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    private SurfaceTexture.OnFrameAvailableListener mUpdateListener;
115aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
1166be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private Canvas mCanvas;
1176be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private int mSaveCount;
1186be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
1196be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private final Object[] mNativeWindowLock = new Object[0];
1206be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    // Used from native code, do not write!
1216be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
1226be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private int mNativeWindow;
1236be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
124aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
125aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
126aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
127aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
128aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
129aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context) {
130aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context);
131aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
132aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
133aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
134aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
135aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
136aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
137aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
138aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
139aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
140aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
141aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs) {
142aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs);
143aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
144aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
145aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
146aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
147aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
148aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
149aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
150aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
151aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param defStyle The default style to apply to this view. If 0, no style
152aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        will be applied (beyond what is included in the theme). This may
153aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        either be an attribute resource, whose value will be retrieved
154aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        from the current theme, or an explicit style resource.
155aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
156aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
157aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs, int defStyle) {
158aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs, defStyle);
159aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
160aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
161aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
162aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private void init() {
163aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mLayerPaint = new Paint();
164aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
165aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
166a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    /**
167a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * {@inheritDoc}
168a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     */
169a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    @Override
170a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    public boolean isOpaque() {
171a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        return mOpaque;
172a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
173a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
174a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    /**
175a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * Indicates whether the content of this TextureView is opaque. The
176a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * content is assumed to be opaque by default.
177a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     *
178a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     * @param opaque True if the content of this TextureView is opaque,
179a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     *               false otherwise
180a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy     */
181a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    public void setOpaque(boolean opaque) {
182a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        if (opaque != mOpaque) {
183a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            mOpaque = opaque;
18458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            updateLayer();
185a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
186a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
187a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
188aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
189aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected void onAttachedToWindow() {
190aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super.onAttachedToWindow();
191aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
192aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (!isHardwareAccelerated()) {
19377a811610f99e21da7f88dafef60d09f345d0506Romain Guy            Log.w(LOG_TAG, "A TextureView or a subclass can only be "
194aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    + "used with hardware acceleration enabled.");
195aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
196aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
197aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
198451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    @Override
199451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    protected void onDetachedFromWindow() {
200451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        super.onDetachedFromWindow();
201451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
20280429c458506485904715180d10584092a5cd082Romain Guy        if (mLayer != null) {
203402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba            boolean shouldRelease = true;
204451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
205402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba                shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface);
206451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
207451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
2086be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            synchronized (mNativeWindowLock) {
2096be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy                nDestroyNativeWindow();
2106be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            }
2116be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
2126be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            mLayer.destroy();
213402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba            if (shouldRelease) mSurface.release();
214451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mSurface = null;
215451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mLayer = null;
216451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        }
217451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    }
218451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
219aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
220aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * The layer type of a TextureView is ignored since a TextureView is always
221aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * considered to act as a hardware layer. The optional paint supplied to this
222aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * method will however be taken into account when rendering the content of
223aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * this TextureView.
224aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
225aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param layerType The ype of layer to use with this view, must be one of
226aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
227aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_HARDWARE}
228aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param paint The paint used to compose the layer. This argument is optional
229aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        and can be null. It is ignored when the layer type is
230aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}
231aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
232aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
233aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setLayerType(int layerType, Paint paint) {
234aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (paint != mLayerPaint) {
235aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            mLayerPaint = paint;
236aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            invalidate();
237aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
238aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
239aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
240aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
241aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Always returns {@link #LAYER_TYPE_HARDWARE}.
242aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
243aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
244aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public int getLayerType() {
245aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return LAYER_TYPE_HARDWARE;
246aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
247aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
248aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
249aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Calling this method has no effect.
250aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
251aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
252aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void buildLayer() {
253aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
254aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
255aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
256aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
257aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
258aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
259aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
260aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
261aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
262aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public final void draw(Canvas canvas) {
26358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        applyUpdate();
264c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        applyTransformMatrix();
265aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
266aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
267aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
268aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
269aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
270aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
271aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
272aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
273aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
274aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected final void onDraw(Canvas canvas) {
275aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
276aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
277aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
2788f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
2798f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onSizeChanged(w, h, oldw, oldh);
2808f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
281e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
282451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
283451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight());
284451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
2858f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
2868f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
2878f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
2888f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
289aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    HardwareLayer getHardwareLayer() {
290aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (mLayer == null) {
29158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
29258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                return null;
29358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            }
29458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
295a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(mOpaque);
296e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
297e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
2986be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            nCreateNativeWindow(mSurface);
299aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
3008f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() {
301aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                @Override
302aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                public void onFrameAvailable(SurfaceTexture surfaceTexture) {
303aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // Per SurfaceTexture's documentation, the callback may be invoked
304aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // from an arbitrary thread
30558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                    synchronized (mLock) {
30658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                        mUpdateLayer = true;
30758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                    }
30858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                    postInvalidateDelayed(0);
309aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                }
3108f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            };
3118f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mSurface.setOnFrameAvailableListener(mUpdateListener);
312aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
313aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            if (mListener != null) {
314451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
315aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            }
316aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
317aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
31858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        applyUpdate();
319c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        applyTransformMatrix();
320302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
321aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mLayer;
322aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
323aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
3248f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
3258f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onVisibilityChanged(View changedView, int visibility) {
3268f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onVisibilityChanged(changedView, visibility);
3278f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
3288f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
3298f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // When the view becomes invisible, stop updating it, it's a waste of CPU
3308f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // To cancel updates, the easiest thing to do is simply to remove the
3318f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // updates listener
3328f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            if (visibility == VISIBLE) {
3338f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(mUpdateListener);
334a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy                updateLayer();
3358f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            } else {
3368f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(null);
3378f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            }
3388f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
3398f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
3408f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
341a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    private void updateLayer() {
34258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        mUpdateLayer = true;
34358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        invalidate();
34458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    }
34558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
34658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy    private void applyUpdate() {
34758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        if (mLayer == null) {
348a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy            return;
349a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        }
350a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
35158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        synchronized (mLock) {
35258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            if (mUpdateLayer) {
35358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                mUpdateLayer = false;
35458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            } else {
35558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy                return;
35658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy            }
35758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy        }
35858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy
35902ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy        mLayer.update(getWidth(), getHeight(), mOpaque);
360a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
361cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        if (mListener != null) {
362cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba            mListener.onSurfaceTextureUpdated(mSurface);
363cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        }
364a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    }
365a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy
366aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
367302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * <p>Sets the transform to associate with this texture view.
368302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * The specified transform applies to the underlying surface
369302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * texture and does not affect the size or position of the view
370302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * itself, only of its content.</p>
371302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
372302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * <p>Some transforms might prevent the content from drawing
373302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * all the pixels contained within this view's bounds. In such
374302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * situations, make sure this texture view is not marked opaque.</p>
375302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
376302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @param transform The transform to apply to the content of
377302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *        this view.
378302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
379302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #getTransform(android.graphics.Matrix)
380302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #isOpaque()
381302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #setOpaque(boolean)
382302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     */
383302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    public void setTransform(Matrix transform) {
384302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        mMatrix.set(transform);
385302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        mMatrixChanged = true;
386c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        invalidateParentIfNeeded();
387302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    }
388302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
389302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    /**
390302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * Returns the transform associated with this texture view.
391302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
392302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @param transform The {@link Matrix} in which to copy the current
393302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *        transform. Can be null.
394302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
395302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @return The specified matrix if not null or a new {@link Matrix}
396302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *         instance otherwise.
397302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
398302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @see #setTransform(android.graphics.Matrix)
399302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     */
400302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    public Matrix getTransform(Matrix transform) {
401302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        if (transform == null) {
402302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy            transform = new Matrix();
403302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        }
404302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
405302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        transform.set(mMatrix);
406302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
407302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy        return transform;
408302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    }
409302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
410c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias    private void applyTransformMatrix() {
411c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        if (mMatrixChanged) {
412c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias            mLayer.setTransform(mMatrix);
413c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias            mMatrixChanged = false;
414c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias        }
415c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias    }
416c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias
417302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    /**
41877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
41977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
42077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
42177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
42277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
42377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format and its dimensions are the same as this view's.</p>
42477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
42577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
42677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
42777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
428d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
429d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
43077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
43177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or the width &lt;= 0 or the height &lt;= 0
43277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
43377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
43477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
43577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
43677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
43777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap() {
43877a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return getBitmap(getWidth(), getHeight());
43977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
44077a811610f99e21da7f88dafef60d09f345d0506Romain Guy
44177a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
44277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
44377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
44477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
44577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
44677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
44777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format.</p>
44877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
44977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
45077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
45177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
452d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
453d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
45477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param width The width of the bitmap to create
45577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param height The height of the bitmap to create
45677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
45777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
45877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or width is &lt;= 0 or height is &lt;= 0
45977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
46077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
46177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
46277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
46377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
46477a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(int width, int height) {
46577a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (isAvailable() && width > 0 && height > 0) {
46677a811610f99e21da7f88dafef60d09f345d0506Romain Guy            return getBitmap(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
46777a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
46877a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return null;
46977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
47077a811610f99e21da7f88dafef60d09f345d0506Romain Guy
47177a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
47277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Copies the content of this view's surface texture into the specified
47377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * bitmap. If the surface texture is not available, the copy is not executed.
47477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * The content of the surface texture will be scaled to fit exactly inside
47577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * the specified bitmap.</p>
47677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
47777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
47877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
47977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
480d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs, the bitmap is left unchanged.</p>
481d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
48277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param bitmap The bitmap to copy the content of the surface texture into,
48377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *               cannot be null, all configurations are supported
48477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
48577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return The bitmap specified as a parameter
48677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
48777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
48877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
48977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
49077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
49177a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(Bitmap bitmap) {
49277a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (bitmap != null && isAvailable()) {
49302ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy            mLayer.copyInto(bitmap);
49477a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
49577a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return bitmap;
49677a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
49777a811610f99e21da7f88dafef60d09f345d0506Romain Guy
49877a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
49977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * Returns true if the {@link SurfaceTexture} associated with this
50077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * TextureView is available for rendering. When this method returns
50177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * true, {@link #getSurfaceTexture()} returns a valid surface texture.
50277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
50377a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public boolean isAvailable() {
50477a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return mSurface != null;
50577a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
50677a811610f99e21da7f88dafef60d09f345d0506Romain Guy
50777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
5086be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * <p>Start editing the pixels in the surface.  The returned Canvas can be used
5096be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to draw into the surface's bitmap.  A null is returned if the surface has
5106be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * not been created or otherwise cannot be edited. You will usually need
5116be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to implement
5126be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * {@link SurfaceTextureListener#onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int)}
5136be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to find out when the Surface is available for use.</p>
5146be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
5156be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * <p>The content of the Surface is never preserved between unlockCanvas()
5166be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * and lockCanvas(), for this reason, every pixel within the Surface area
5176be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * must be written. The only exception to this rule is when a dirty
5186be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * rectangle is specified, in which case, non-dirty pixels will be
5196be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * preserved.</p>
5206be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
5216be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @return A Canvas used to draw into the surface.
5226be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
5236be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas(android.graphics.Rect)
5246be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #unlockCanvasAndPost(android.graphics.Canvas)
5256be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     */
5266be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    public Canvas lockCanvas() {
5276be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        return lockCanvas(null);
5286be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    }
5296be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
5306be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    /**
5316be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * Just like {@link #lockCanvas()} but allows specification of a dirty
5326be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * rectangle. Every pixel within that rectangle must be written; however
5336be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * pixels outside the dirty rectangle will be preserved by the next call
5346be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * to lockCanvas().
5356be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
5366be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @param dirty Area of the surface that will be modified.
5376be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
5386be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @return A Canvas used to draw into the surface.
5396be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
5406be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas()
5416be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #unlockCanvasAndPost(android.graphics.Canvas)
5426be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     */
5436be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    public Canvas lockCanvas(Rect dirty) {
5446be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        if (!isAvailable()) return null;
5456be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
5466be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        if (mCanvas == null) {
5476be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            mCanvas = new Canvas();
5486be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        }
5496be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
5506be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        synchronized (mNativeWindowLock) {
5516be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            nLockCanvas(mNativeWindow, mCanvas, dirty);
5526be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        }
5536be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        mSaveCount = mCanvas.save();
5546be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
5556be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        return mCanvas;
5566be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    }
5576be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
5586be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    /**
5596be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * Finish editing pixels in the surface. After this call, the surface's
5606be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * current pixels will be shown on the screen, but its content is lost,
5616be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * in particular there is no guarantee that the content of the Surface
5626be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * will remain unchanged when lockCanvas() is called again.
5636be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
5646be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @param canvas The Canvas previously returned by lockCanvas()
5656be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     *
5666be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas()
5676be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     * @see #lockCanvas(android.graphics.Rect)
5686be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy     */
5696be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    public void unlockCanvasAndPost(Canvas canvas) {
5706be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        if (mCanvas != null && canvas == mCanvas) {
5716be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            canvas.restoreToCount(mSaveCount);
5726be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            mSaveCount = 0;
5736be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
5746be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            synchronized (mNativeWindowLock) {
5756be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy                nUnlockCanvasAndPost(mNativeWindow, mCanvas);
5766be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy            }
5776be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy        }
5786be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    }
5796be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
5806be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    /**
581aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTexture} used by this view. This method
58277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * may return null if the view is not attached to a window or if the surface
58377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * texture has not been initialized yet.
58477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
58577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
586aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
587aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTexture getSurfaceTexture() {
588aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mSurface;
589aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
590aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
591aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
592aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTextureListener} currently associated with this
593aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture view.
594aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
595aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener)
596aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
597aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
598aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTextureListener getSurfaceTextureListener() {
599aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mListener;
600aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
601aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
602aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
603aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Sets the {@link SurfaceTextureListener} used to listen to surface
604aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture events.
605aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
606aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #getSurfaceTextureListener()
607aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
608aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
609aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setSurfaceTextureListener(SurfaceTextureListener listener) {
610aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mListener = listener;
611aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
612aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
613aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
614aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * This listener can be used to be notified when the surface texture
615aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * associated with this texture view is available.
616aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
617aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public static interface SurfaceTextureListener {
618aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        /**
619aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use.
620aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *
621aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * @param surface The surface returned by
622aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
623451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param width The width of the surface
624451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param height The height of the surface
625aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         */
626451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height);
6278f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
6288f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        /**
6298f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * Invoked when the {@link SurfaceTexture}'s buffers size changed.
6308f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *
6318f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param surface The surface returned by
6328f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
6338f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param width The new width of the surface
6348f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param height The new height of the surface
6358f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         */
6368f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height);
637451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
638451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        /**
639451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * Invoked when the specified {@link SurfaceTexture} is about to be destroyed.
640402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba         * If returns true, no rendering should happen inside the surface texture after this method
641402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba         * is invoked. If returns false, the client needs to call {@link SurfaceTexture#release()}.
642451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         *
643451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param surface The surface about to be destroyed
644451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         */
645402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface);
646cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba
647cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        /**
648cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * Invoked when the specified {@link SurfaceTexture} is updated through
649cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * {@link SurfaceTexture#updateTexImage()}.
650cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         *
651cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         * @param surface The surface just updated
652cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba         */
653cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba        public void onSurfaceTextureUpdated(SurfaceTexture surface);
654aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
6558f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
6566be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private native void nCreateNativeWindow(SurfaceTexture surface);
6576be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private native void nDestroyNativeWindow();
6586be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
659d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy    private static native void nSetDefaultBufferSize(SurfaceTexture surfaceTexture,
660d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy            int width, int height);
6616be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy
6626be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private static native void nLockCanvas(int nativeWindow, Canvas canvas, Rect dirty);
6636be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy    private static native void nUnlockCanvasAndPost(int nativeWindow, Canvas canvas);
664aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy}
665