TextureView.java revision c989d867f2580a99cde25fab0e49e445aea33f2f
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 99c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy private final Object[] mLock = new Object[0]; 100c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy private boolean mUpdateLayer; 101c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy 1028f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy private SurfaceTexture.OnFrameAvailableListener mUpdateListener; 103aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 104aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 105aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 106aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 107aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 108aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 109aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public TextureView(Context context) { 110aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super(context); 111aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 112aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 113aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 114aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 115aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 116aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 117aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 118aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param attrs The attributes of the XML tag that is inflating the view. 119aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 120aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @SuppressWarnings({"UnusedDeclaration"}) 121aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public TextureView(Context context, AttributeSet attrs) { 122aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super(context, attrs); 123aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 124aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 125aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 126aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 127aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 128aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 129aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 130aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param attrs The attributes of the XML tag that is inflating the view. 131aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param defStyle The default style to apply to this view. If 0, no style 132aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * will be applied (beyond what is included in the theme). This may 133aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * either be an attribute resource, whose value will be retrieved 134aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * from the current theme, or an explicit style resource. 135aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 136aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @SuppressWarnings({"UnusedDeclaration"}) 137aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public TextureView(Context context, AttributeSet attrs, int defStyle) { 138aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super(context, attrs, defStyle); 139aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 140aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 141aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 142aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private void init() { 143aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mLayerPaint = new Paint(); 144aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 145aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 146aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 147aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy protected void onAttachedToWindow() { 148aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super.onAttachedToWindow(); 149aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 150aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (!isHardwareAccelerated()) { 15177a811610f99e21da7f88dafef60d09f345d0506Romain Guy Log.w(LOG_TAG, "A TextureView or a subclass can only be " 152aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy + "used with hardware acceleration enabled."); 153aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 154aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 155aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 156451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy @Override 157451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy protected void onDetachedFromWindow() { 158451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy super.onDetachedFromWindow(); 159451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 160451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy if (isHardwareAccelerated() && mLayer != null) { 161451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy if (mListener != null) { 162451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mListener.onSurfaceTextureDestroyed(mSurface); 163451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 164451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 165451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mLayer.destroy(); 166451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mSurface = null; 167451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mLayer = null; 168451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 169451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 170451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 171aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 172aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * The layer type of a TextureView is ignored since a TextureView is always 173aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * considered to act as a hardware layer. The optional paint supplied to this 174aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * method will however be taken into account when rendering the content of 175aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * this TextureView. 176aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 177aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param layerType The ype of layer to use with this view, must be one of 178aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or 179aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_HARDWARE} 180aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param paint The paint used to compose the layer. This argument is optional 181aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * and can be null. It is ignored when the layer type is 182aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_NONE} 183aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 184aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 185aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void setLayerType(int layerType, Paint paint) { 186aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (paint != mLayerPaint) { 187aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mLayerPaint = paint; 188aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy invalidate(); 189aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 190aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 191aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 192aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 193aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Always returns {@link #LAYER_TYPE_HARDWARE}. 194aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 195aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 196aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public int getLayerType() { 197aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return LAYER_TYPE_HARDWARE; 198aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 199aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 200aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 201aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Calling this method has no effect. 202aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 203aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 204aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void buildLayer() { 205aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 206aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 207aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 208aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Subclasses of TextureView cannot do their own rendering 209aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * with the {@link Canvas} object. 210aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 211aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param canvas The Canvas to which the View is rendered. 212aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 213aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 214aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public final void draw(Canvas canvas) { 215aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super.draw(canvas); 216aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 217aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 218aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 219aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Subclasses of TextureView cannot do their own rendering 220aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * with the {@link Canvas} object. 221aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 222aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param canvas The Canvas to which the View is rendered. 223aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 224aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 225aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy protected final void onDraw(Canvas canvas) { 226aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 227aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 228aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 2298f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy protected void onSizeChanged(int w, int h, int oldw, int oldh) { 2308f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy super.onSizeChanged(w, h, oldw, oldh); 2318f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (mSurface != null) { 232c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy // No need to synchronize here, we set update layer to false only on the UI thread 233c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy mUpdateLayer = true; 234e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy nSetDefaultBufferSize(mSurface, getWidth(), getHeight()); 235451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy if (mListener != null) { 236451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight()); 237451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 2388f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 2398f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 2408f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 2418f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @Override 242aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy HardwareLayer getHardwareLayer() { 243aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) { 244aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return null; 245aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 246aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 247aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (mLayer == null) { 248aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(); 249e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer); 250e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy nSetDefaultBufferSize(mSurface, getWidth(), getHeight()); 251aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 2528f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() { 253aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 254aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void onFrameAvailable(SurfaceTexture surfaceTexture) { 255aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy // Per SurfaceTexture's documentation, the callback may be invoked 256aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy // from an arbitrary thread 257c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy synchronized (mLock) { 258c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy mUpdateLayer = true; 259c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy postInvalidate(); 260c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy } 261aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 2628f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy }; 2638f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mSurface.setOnFrameAvailableListener(mUpdateListener); 264aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 265aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (mListener != null) { 266451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight()); 267aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 268aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 269aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 270c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy synchronized (mLock) { 271c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy if (mUpdateLayer) { 272c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy mAttachInfo.mHardwareRenderer.updateTextureLayer(mLayer, getWidth(), getHeight()); 273c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy mUpdateLayer = false; 274c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy } 275c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy } 276c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy 277aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mLayer; 278aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 279aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 2808f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @Override 2818f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy protected void onVisibilityChanged(View changedView, int visibility) { 2828f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy super.onVisibilityChanged(changedView, visibility); 2838f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 2848f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (mSurface != null) { 2858f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // When the view becomes invisible, stop updating it, it's a waste of CPU 2868f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // To cancel updates, the easiest thing to do is simply to remove the 2878f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // updates listener 2888f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (visibility == VISIBLE) { 2898f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mSurface.setOnFrameAvailableListener(mUpdateListener); 290c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy // No need to synchronize here, we set update layer to false only on the UI thread 291c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy mUpdateLayer = true; 292c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy invalidate(); 2938f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } else { 2948f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mSurface.setOnFrameAvailableListener(null); 2958f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 2968f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 2978f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 2988f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 299aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 30077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Returns a {@link android.graphics.Bitmap} representation of the content 30177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * of the associated surface texture. If the surface texture is not available, 30277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * this method returns null.</p> 30377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 30477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888} 30577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * pixel format and its dimensions are the same as this view's.</p> 30677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 30777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 30877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 30977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 310d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs during the copy, an empty bitmap will be returned.</p> 311d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 31277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface 31377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture is not available or the width <= 0 or the height <= 0 31477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 31577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 31677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(android.graphics.Bitmap) 31777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(int, int) 31877a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 31977a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap() { 32077a811610f99e21da7f88dafef60d09f345d0506Romain Guy return getBitmap(getWidth(), getHeight()); 32177a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 32277a811610f99e21da7f88dafef60d09f345d0506Romain Guy 32377a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 32477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Returns a {@link android.graphics.Bitmap} representation of the content 32577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * of the associated surface texture. If the surface texture is not available, 32677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * this method returns null.</p> 32777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 32877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888} 32977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * pixel format.</p> 33077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 33177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 33277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 33377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 334d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs during the copy, an empty bitmap will be returned.</p> 335d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 33677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param width The width of the bitmap to create 33777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param height The height of the bitmap to create 33877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 33977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface 34077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture is not available or width is <= 0 or height is <= 0 34177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 34277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 34377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(android.graphics.Bitmap) 34477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap() 34577a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 34677a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap(int width, int height) { 34777a811610f99e21da7f88dafef60d09f345d0506Romain Guy if (isAvailable() && width > 0 && height > 0) { 34877a811610f99e21da7f88dafef60d09f345d0506Romain Guy return getBitmap(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)); 34977a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 35077a811610f99e21da7f88dafef60d09f345d0506Romain Guy return null; 35177a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 35277a811610f99e21da7f88dafef60d09f345d0506Romain Guy 35377a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 35477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Copies the content of this view's surface texture into the specified 35577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * bitmap. If the surface texture is not available, the copy is not executed. 35677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * The content of the surface texture will be scaled to fit exactly inside 35777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * the specified bitmap.</p> 35877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 35977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 36077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 36177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 362d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs, the bitmap is left unchanged.</p> 363d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 36477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param bitmap The bitmap to copy the content of the surface texture into, 36577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * cannot be null, all configurations are supported 36677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 36777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return The bitmap specified as a parameter 36877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 36977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 37077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(int, int) 37177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap() 37277a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 37377a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap(Bitmap bitmap) { 37477a811610f99e21da7f88dafef60d09f345d0506Romain Guy if (bitmap != null && isAvailable()) { 37577a811610f99e21da7f88dafef60d09f345d0506Romain Guy mAttachInfo.mHardwareRenderer.copyLayer(mLayer, bitmap); 37677a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 37777a811610f99e21da7f88dafef60d09f345d0506Romain Guy return bitmap; 37877a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 37977a811610f99e21da7f88dafef60d09f345d0506Romain Guy 38077a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 38177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * Returns true if the {@link SurfaceTexture} associated with this 38277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * TextureView is available for rendering. When this method returns 38377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * true, {@link #getSurfaceTexture()} returns a valid surface texture. 38477a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 38577a811610f99e21da7f88dafef60d09f345d0506Romain Guy public boolean isAvailable() { 38677a811610f99e21da7f88dafef60d09f345d0506Romain Guy return mSurface != null; 38777a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 38877a811610f99e21da7f88dafef60d09f345d0506Romain Guy 38977a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 390aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Returns the {@link SurfaceTexture} used by this view. This method 39177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * may return null if the view is not attached to a window or if the surface 39277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture has not been initialized yet. 39377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 39477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 395aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 396aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public SurfaceTexture getSurfaceTexture() { 397aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mSurface; 398aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 399aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 400aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 401aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Returns the {@link SurfaceTextureListener} currently associated with this 402aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * texture view. 403aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 404aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener) 405aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTextureListener 406aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 407aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public SurfaceTextureListener getSurfaceTextureListener() { 408aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mListener; 409aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 410aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 411aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 412aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Sets the {@link SurfaceTextureListener} used to listen to surface 413aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * texture events. 414aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 415aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see #getSurfaceTextureListener() 416aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTextureListener 417aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 418aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void setSurfaceTextureListener(SurfaceTextureListener listener) { 419aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mListener = listener; 420aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 421aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 422aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 423aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * This listener can be used to be notified when the surface texture 424aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * associated with this texture view is available. 425aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 426aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public static interface SurfaceTextureListener { 427aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 428aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use. 429aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 430aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param surface The surface returned by 431aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link android.view.TextureView#getSurfaceTexture()} 432451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param width The width of the surface 433451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param height The height of the surface 434aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 435451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height); 4368f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 4378f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy /** 4388f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * Invoked when the {@link SurfaceTexture}'s buffers size changed. 4398f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * 4408f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param surface The surface returned by 4418f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * {@link android.view.TextureView#getSurfaceTexture()} 4428f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param width The new width of the surface 4438f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param height The new height of the surface 4448f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy */ 4458f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height); 446451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 447451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy /** 448451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * Invoked when the specified {@link SurfaceTexture} is about to be destroyed. 449451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * After this method is invoked, no rendering should happen inside the surface 450451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * texture. 451451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * 452451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param surface The surface about to be destroyed 453451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy */ 454451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy public void onSurfaceTextureDestroyed(SurfaceTexture surface); 455aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 4568f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 457d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy private static native void nSetDefaultBufferSize(SurfaceTexture surfaceTexture, 458d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy int width, int height); 459aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy} 460