TextureView.java revision d6b2a00dd43257d1498b09175bff63663f6cb861
1aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/*
2aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Copyright (C) 2011 The Android Open Source Project
3aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
4aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * you may not use this file except in compliance with the License.
6aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * You may obtain a copy of the License at
7aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
8aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
10aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Unless required by applicable law or agreed to in writing, software
11aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * See the License for the specific language governing permissions and
14aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * limitations under the License.
15aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */
16aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
17aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypackage android.view;
18aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
19aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.content.Context;
2077a811610f99e21da7f88dafef60d09f345d0506Romain Guyimport android.graphics.Bitmap;
21aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.Canvas;
22aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.Paint;
23aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.SurfaceTexture;
24aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.AttributeSet;
25aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.Log;
26aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
27aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/**
28aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView can be used to display a content stream. Such a content
29aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * stream can for instance be a video or an OpenGL scene. The content stream
30aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can come from the application's process as well as a remote process.</p>
31aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
32aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>TextureView can only be used in a hardware accelerated window. When
33aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * rendered in software, TextureView will draw nothing.</p>
34aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
35aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Unlike {@link SurfaceView}, TextureView does not create a separate
36aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * window but behaves as a regular View. This key difference allows a
37aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView to be moved, transformed, animated, etc. For instance, you
38aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can make a TextureView semi-translucent by calling
39aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <code>myView.setAlpha(0.5f)</code>.</p>
40aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
41aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Using a TextureView is simple: all you need to do is get its
42aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link SurfaceTexture}. The {@link SurfaceTexture} can then be used to
43aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * render content. The following example demonstrates how to render the
44aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * camera preview into a TextureView:</p>
45aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
46aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <pre>
47aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *  public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
48aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      private Camera mCamera;
49aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      private TextureView mTextureView;
50aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
51aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      protected void onCreate(Bundle savedInstanceState) {
52aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          super.onCreate(savedInstanceState);
53aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
54aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mTextureView = new TextureView(this);
55aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mTextureView.setSurfaceTextureListener(this);
56aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
57aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          setContentView(mTextureView);
58aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      }
59aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
60451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
61aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          mCamera = Camera.open();
62aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
63aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          try {
64aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              mCamera.setPreviewTexture(surface);
65aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              mCamera.startPreview();
66aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          } catch (IOException ioe) {
67aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *              // Something bad happened
68aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *          }
69aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *      }
708f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *
718f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *      public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
728f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *          // Ignored, Camera does all the work for us
738f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy *      }
74451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *
75451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *      public void onSurfaceTextureDestroyed(SurfaceTexture surface) {
76451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *          mCamera.stopPreview();
77451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *          mCamera.release();
78451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy *      }
79aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *  }
80aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * </pre>
81aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
82aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView's SurfaceTexture can be obtained either by invoking
83aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #getSurfaceTexture()} or by using a {@link SurfaceTextureListener}.
84aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * It is important to know that a SurfaceTexture is available only after the
85aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView is attached to a window (and {@link #onAttachedToWindow()} has
86aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * been invoked.) It is therefore highly recommended you use a listener to
87aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * be notified when the SurfaceTexture becomes available.</p>
88aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy *
89aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceView
90aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTexture
91aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */
92aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypublic class TextureView extends View {
9377a811610f99e21da7f88dafef60d09f345d0506Romain Guy    private static final String LOG_TAG = "TextureView";
9477a811610f99e21da7f88dafef60d09f345d0506Romain Guy
95aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private HardwareLayer mLayer;
96aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private SurfaceTexture mSurface;
97aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private SurfaceTextureListener mListener;
98aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
99aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private final Runnable mUpdateLayerAction = new Runnable() {
100aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        @Override
101aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        public void run() {
102aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            updateLayer();
103aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
104aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    };
1058f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    private SurfaceTexture.OnFrameAvailableListener mUpdateListener;
106aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
107aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
108aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
109aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
110aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
111aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
112aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context) {
113aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context);
114aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
115aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
116aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
117aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
118aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
119aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
120aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
121aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
122aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
123aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
124aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs) {
125aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs);
126aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
127aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
128aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
129aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
130aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new TextureView.
131aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
132aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param context The context to associate this view with.
133aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param attrs The attributes of the XML tag that is inflating the view.
134aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param defStyle The default style to apply to this view. If 0, no style
135aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        will be applied (beyond what is included in the theme). This may
136aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        either be an attribute resource, whose value will be retrieved
137aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        from the current theme, or an explicit style resource.
138aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
139aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
140aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public TextureView(Context context, AttributeSet attrs, int defStyle) {
141aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super(context, attrs, defStyle);
142aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        init();
143aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
144aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
145aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private void init() {
146aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mLayerPaint = new Paint();
147aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
148aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
149aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
150aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected void onAttachedToWindow() {
151aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super.onAttachedToWindow();
152aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
153aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (!isHardwareAccelerated()) {
15477a811610f99e21da7f88dafef60d09f345d0506Romain Guy            Log.w(LOG_TAG, "A TextureView or a subclass can only be "
155aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    + "used with hardware acceleration enabled.");
156aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
157aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
158aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
159451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    @Override
160451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    protected void onDetachedFromWindow() {
161451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        super.onDetachedFromWindow();
162451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
163451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        if (isHardwareAccelerated() && mLayer != null) {
164451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
165451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureDestroyed(mSurface);
166451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
167451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
168451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mLayer.destroy();
169451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mSurface = null;
170451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            mLayer = null;
171451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        }
172451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy    }
173451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
174aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
175aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * The layer type of a TextureView is ignored since a TextureView is always
176aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * considered to act as a hardware layer. The optional paint supplied to this
177aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * method will however be taken into account when rendering the content of
178aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * this TextureView.
179aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
180aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param layerType The ype of layer to use with this view, must be one of
181aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
182aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_HARDWARE}
183aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param paint The paint used to compose the layer. This argument is optional
184aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        and can be null. It is ignored when the layer type is
185aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *        {@link #LAYER_TYPE_NONE}
186aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
187aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
188aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setLayerType(int layerType, Paint paint) {
189aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (paint != mLayerPaint) {
190aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            mLayerPaint = paint;
191aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            invalidate();
192aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
193aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
194aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
195aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
196aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Always returns {@link #LAYER_TYPE_HARDWARE}.
197aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
198aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
199aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public int getLayerType() {
200aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return LAYER_TYPE_HARDWARE;
201aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
202aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
203aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
204aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Calling this method has no effect.
205aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
206aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
207aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void buildLayer() {
208aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
209aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
210aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
211aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
212aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
213aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
214aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
215aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
216aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
217aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public final void draw(Canvas canvas) {
218aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        super.draw(canvas);
219aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
220aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
221aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
222aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Subclasses of TextureView cannot do their own rendering
223aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * with the {@link Canvas} object.
224aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
225aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @param canvas The Canvas to which the View is rendered.
226aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
227aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
228aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    protected final void onDraw(Canvas canvas) {
229aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
230aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
231aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    @Override
2328f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
2338f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onSizeChanged(w, h, oldw, oldh);
2348f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
235e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
236451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            if (mListener != null) {
237451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight());
238451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy            }
2398f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
2408f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
2418f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
2428f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
243aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    HardwareLayer getHardwareLayer() {
244aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
245aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            return null;
246aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
247aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
248aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (mLayer == null) {
249aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer();
250e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
251e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
252aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
2538f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() {
254aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                @Override
255aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                public void onFrameAvailable(SurfaceTexture surfaceTexture) {
256aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // Per SurfaceTexture's documentation, the callback may be invoked
257aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    // from an arbitrary thread
258aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                    post(mUpdateLayerAction);
259aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy                }
2608f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            };
2618f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            mSurface.setOnFrameAvailableListener(mUpdateListener);
262aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
263aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            if (mListener != null) {
264451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy                mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
265aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            }
266aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
267aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
268aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mLayer;
269aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
270aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
2718f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    @Override
2728f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    protected void onVisibilityChanged(View changedView, int visibility) {
2738f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        super.onVisibilityChanged(changedView, visibility);
2748f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
2758f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        if (mSurface != null) {
2768f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // When the view becomes invisible, stop updating it, it's a waste of CPU
2778f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // To cancel updates, the easiest thing to do is simply to remove the
2788f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            // updates listener
2798f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            if (visibility == VISIBLE) {
2808f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(mUpdateListener);
2818f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                updateLayer();
2828f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            } else {
2838f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy                mSurface.setOnFrameAvailableListener(null);
2848f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy            }
2858f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        }
2868f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy    }
2878f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
288aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    private void updateLayer() {
289aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
290aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy            return;
291aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        }
292aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
293e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy        mAttachInfo.mHardwareRenderer.updateTextureLayer(mLayer, getWidth(), getHeight());
294aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
295aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        invalidate();
296aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
297aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
298aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
29977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
30077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
30177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
30277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
30377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
30477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format and its dimensions are the same as this view's.</p>
30577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
30677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
30777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
30877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
309d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
310d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
31177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
31277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or the width &lt;= 0 or the height &lt;= 0
31377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
31477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
31577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
31677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
31777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
31877a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap() {
31977a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return getBitmap(getWidth(), getHeight());
32077a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
32177a811610f99e21da7f88dafef60d09f345d0506Romain Guy
32277a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
32377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
32477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * of the associated surface texture. If the surface texture is not available,
32577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * this method returns null.</p>
32677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
32777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
32877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * pixel format.</p>
32977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
33077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
33177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
33277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
333d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
334d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
33577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param width The width of the bitmap to create
33677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param height The height of the bitmap to create
33777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
33877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
33977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *         texture is not available or width is &lt;= 0 or height is &lt;= 0
34077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
34177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
34277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(android.graphics.Bitmap)
34377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
34477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
34577a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(int width, int height) {
34677a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (isAvailable() && width > 0 && height > 0) {
34777a811610f99e21da7f88dafef60d09f345d0506Romain Guy            return getBitmap(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
34877a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
34977a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return null;
35077a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
35177a811610f99e21da7f88dafef60d09f345d0506Romain Guy
35277a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
35377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p>Copies the content of this view's surface texture into the specified
35477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * bitmap. If the surface texture is not available, the copy is not executed.
35577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * The content of the surface texture will be scaled to fit exactly inside
35677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * the specified bitmap.</p>
35777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
35877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * <p><strong>Do not</strong> invoke this method from a drawing method
35977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
36077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
361d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     * <p>If an error occurs, the bitmap is left unchanged.</p>
362d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy     *
36377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @param bitmap The bitmap to copy the content of the surface texture into,
36477a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *               cannot be null, all configurations are supported
36577a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
36677a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @return The bitmap specified as a parameter
36777a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
36877a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
36977a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap(int, int)
37077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #getBitmap()
37177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
37277a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public Bitmap getBitmap(Bitmap bitmap) {
37377a811610f99e21da7f88dafef60d09f345d0506Romain Guy        if (bitmap != null && isAvailable()) {
37477a811610f99e21da7f88dafef60d09f345d0506Romain Guy            mAttachInfo.mHardwareRenderer.copyLayer(mLayer, bitmap);
37577a811610f99e21da7f88dafef60d09f345d0506Romain Guy        }
37677a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return bitmap;
37777a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
37877a811610f99e21da7f88dafef60d09f345d0506Romain Guy
37977a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
38077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * Returns true if the {@link SurfaceTexture} associated with this
38177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * TextureView is available for rendering. When this method returns
38277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * true, {@link #getSurfaceTexture()} returns a valid surface texture.
38377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     */
38477a811610f99e21da7f88dafef60d09f345d0506Romain Guy    public boolean isAvailable() {
38577a811610f99e21da7f88dafef60d09f345d0506Romain Guy        return mSurface != null;
38677a811610f99e21da7f88dafef60d09f345d0506Romain Guy    }
38777a811610f99e21da7f88dafef60d09f345d0506Romain Guy
38877a811610f99e21da7f88dafef60d09f345d0506Romain Guy    /**
389aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTexture} used by this view. This method
39077a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * may return null if the view is not attached to a window or if the surface
39177a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * texture has not been initialized yet.
39277a811610f99e21da7f88dafef60d09f345d0506Romain Guy     *
39377a811610f99e21da7f88dafef60d09f345d0506Romain Guy     * @see #isAvailable()
394aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
395aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTexture getSurfaceTexture() {
396aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mSurface;
397aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
398aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
399aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
400aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Returns the {@link SurfaceTextureListener} currently associated with this
401aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture view.
402aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
403aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener)
404aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
405aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
406aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public SurfaceTextureListener getSurfaceTextureListener() {
407aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        return mListener;
408aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
409aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
410aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
411aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Sets the {@link SurfaceTextureListener} used to listen to surface
412aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * texture events.
413aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     *
414aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see #getSurfaceTextureListener()
415aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * @see SurfaceTextureListener
416aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
417aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public void setSurfaceTextureListener(SurfaceTextureListener listener) {
418aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        mListener = listener;
419aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
420aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
421aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
422aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * This listener can be used to be notified when the surface texture
423aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * associated with this texture view is available.
424aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
425aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    public static interface SurfaceTextureListener {
426aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        /**
427aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use.
428aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *
429aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         * @param surface The surface returned by
430aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
431451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param width The width of the surface
432451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param height The height of the surface
433aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy         */
434451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height);
4358f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
4368f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        /**
4378f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * Invoked when the {@link SurfaceTexture}'s buffers size changed.
4388f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *
4398f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param surface The surface returned by
4408f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         *                {@link android.view.TextureView#getSurfaceTexture()}
4418f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param width The new width of the surface
4428f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         * @param height The new height of the surface
4438f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy         */
4448f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height);
445451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy
446451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        /**
447451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * Invoked when the specified {@link SurfaceTexture} is about to be destroyed.
448451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * After this method is invoked, no rendering should happen inside the surface
449451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * texture.
450451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         *
451451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         * @param surface The surface about to be destroyed
452451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy         */
453451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy        public void onSurfaceTextureDestroyed(SurfaceTexture surface);
454aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
4558f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
456d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy    private static native void nSetDefaultBufferSize(SurfaceTexture surfaceTexture,
457d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy            int width, int height);
458aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy}
459