1aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/* 2aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Copyright (C) 2011 The Android Open Source Project 3aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 4aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Licensed under the Apache License, Version 2.0 (the "License"); 5aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * you may not use this file except in compliance with the License. 6aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * You may obtain a copy of the License at 7aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 8aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * http://www.apache.org/licenses/LICENSE-2.0 9aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 10aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Unless required by applicable law or agreed to in writing, software 11aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * distributed under the License is distributed on an "AS IS" BASIS, 12aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * See the License for the specific language governing permissions and 14aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * limitations under the License. 15aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 16aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 17aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypackage android.view; 18aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 19aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.content.Context; 2077a811610f99e21da7f88dafef60d09f345d0506Romain Guyimport android.graphics.Bitmap; 21aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.Canvas; 22302a9df1d50373c82923bb84ff665dfce584fb22Romain Guyimport android.graphics.Matrix; 23aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.Paint; 246be3d5561cbeccf0a8257a4acb155657f868e548Romain Guyimport android.graphics.Rect; 25aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.SurfaceTexture; 26aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.AttributeSet; 27aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.Log; 28aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 29aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/** 30aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView can be used to display a content stream. Such a content 31aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * stream can for instance be a video or an OpenGL scene. The content stream 32aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can come from the application's process as well as a remote process.</p> 33aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 34aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>TextureView can only be used in a hardware accelerated window. When 35aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * rendered in software, TextureView will draw nothing.</p> 36aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 37aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Unlike {@link SurfaceView}, TextureView does not create a separate 38aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * window but behaves as a regular View. This key difference allows a 39aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView to be moved, transformed, animated, etc. For instance, you 40aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can make a TextureView semi-translucent by calling 41aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <code>myView.setAlpha(0.5f)</code>.</p> 42aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 43aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Using a TextureView is simple: all you need to do is get its 44aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link SurfaceTexture}. The {@link SurfaceTexture} can then be used to 45aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * render content. The following example demonstrates how to render the 46aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * camera preview into a TextureView:</p> 47aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 48aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <pre> 49aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener { 50aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * private Camera mCamera; 51aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * private TextureView mTextureView; 52aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 53aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * protected void onCreate(Bundle savedInstanceState) { 54aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * super.onCreate(savedInstanceState); 55aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 56aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mTextureView = new TextureView(this); 57aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mTextureView.setSurfaceTextureListener(this); 58aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 59aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * setContentView(mTextureView); 60aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 61aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 62451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 63aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mCamera = Camera.open(); 64aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 65aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * try { 66aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mCamera.setPreviewTexture(surface); 67aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mCamera.startPreview(); 68aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } catch (IOException ioe) { 69aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * // Something bad happened 70aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 71aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 72cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 738f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { 748f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * // Ignored, Camera does all the work for us 758f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * } 76cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 77402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba * public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 78451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * mCamera.stopPreview(); 79451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * mCamera.release(); 80402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba * return true; 81451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * } 82cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 83cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * public void onSurfaceTextureUpdated(SurfaceTexture surface) { 8458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy * // Invoked every time there's a new Camera preview frame 85cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * } 86aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 87aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * </pre> 88aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 89aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView's SurfaceTexture can be obtained either by invoking 90aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #getSurfaceTexture()} or by using a {@link SurfaceTextureListener}. 91aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * It is important to know that a SurfaceTexture is available only after the 92aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView is attached to a window (and {@link #onAttachedToWindow()} has 93aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * been invoked.) It is therefore highly recommended you use a listener to 94aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * be notified when the SurfaceTexture becomes available.</p> 95aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 96462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * <p>It is important to note that only one producer can use the TextureView. 97462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * For instance, if you use a TextureView to display the camera preview, you 98462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * cannot use {@link #lockCanvas()} to draw onto the TextureView at the same 99462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * time.</p> 100462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * 101aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceView 102aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTexture 103aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 104aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypublic class TextureView extends View { 10577a811610f99e21da7f88dafef60d09f345d0506Romain Guy private static final String LOG_TAG = "TextureView"; 10677a811610f99e21da7f88dafef60d09f345d0506Romain Guy 107aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private HardwareLayer mLayer; 108aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private SurfaceTexture mSurface; 109aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private SurfaceTextureListener mListener; 11067603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy private boolean mHadSurface; 111aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 112a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy private boolean mOpaque = true; 113c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy 114302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy private final Matrix mMatrix = new Matrix(); 115302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy private boolean mMatrixChanged; 116302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 11758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy private final Object[] mLock = new Object[0]; 11858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy private boolean mUpdateLayer; 1192af3524beb75150d347accc925022daa53b4a789Jamie Gennis private boolean mUpdateSurface; 12058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 1216be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private Canvas mCanvas; 1226be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private int mSaveCount; 1236be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 1246be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private final Object[] mNativeWindowLock = new Object[0]; 125918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck // Set by native code, do not write! 12636bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private long mNativeWindow; 1276be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 128aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 129aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 130aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 131aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 132aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 133aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public TextureView(Context context) { 134aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super(context); 135aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 136aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 137aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 138aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 139aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 140aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 141aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 142aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param attrs The attributes of the XML tag that is inflating the view. 143aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 144aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public TextureView(Context context, AttributeSet attrs) { 145aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super(context, attrs); 146aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 147aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 148aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 149aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 150aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 151aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 152aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 153aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param attrs The attributes of the XML tag that is inflating the view. 154617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleAttr An attribute in the current theme that contains a 155617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * reference to a style resource that supplies default values for 156617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * the view. Can be 0 to not look for defaults. 157aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 158617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public TextureView(Context context, AttributeSet attrs, int defStyleAttr) { 159617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette super(context, attrs, defStyleAttr); 160617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette init(); 161617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette } 162617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette 163617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette /** 164617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * Creates a new TextureView. 165617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * 166617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param context The context to associate this view with. 167617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param attrs The attributes of the XML tag that is inflating the view. 168617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleAttr An attribute in the current theme that contains a 169617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * reference to a style resource that supplies default values for 170617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * the view. Can be 0 to not look for defaults. 171617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleRes A resource identifier of a style resource that 172617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * supplies default values for the view, used only if 173617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * defStyleAttr is 0 or can not be found in the theme. Can be 0 174617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * to not look for defaults. 175617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette */ 176617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public TextureView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 177617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette super(context, attrs, defStyleAttr, defStyleRes); 178aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 179aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 180aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 181aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private void init() { 182aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mLayerPaint = new Paint(); 183aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 184aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 185a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy /** 186a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * {@inheritDoc} 187a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy */ 188a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy @Override 189a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy public boolean isOpaque() { 190a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy return mOpaque; 191a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 192a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 193a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy /** 194a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * Indicates whether the content of this TextureView is opaque. The 195a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * content is assumed to be opaque by default. 196a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * 197a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * @param opaque True if the content of this TextureView is opaque, 198a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * false otherwise 199a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy */ 200a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy public void setOpaque(boolean opaque) { 201a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy if (opaque != mOpaque) { 202a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy mOpaque = opaque; 203a8a2f97c100af4ba52c61c3b59c933f44a53dad4Romain Guy if (mLayer != null) { 20488801b270f693ffd4125534724f204135f592f72Romain Guy updateLayerAndInvalidate(); 205a8a2f97c100af4ba52c61c3b59c933f44a53dad4Romain Guy } 206a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 207a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 208a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 209aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 210aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy protected void onAttachedToWindow() { 211aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super.onAttachedToWindow(); 212aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 213aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (!isHardwareAccelerated()) { 21477a811610f99e21da7f88dafef60d09f345d0506Romain Guy Log.w(LOG_TAG, "A TextureView or a subclass can only be " 215aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy + "used with hardware acceleration enabled."); 216aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 21767603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy 21867603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy if (mHadSurface) { 21967603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy invalidate(true); 22067603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy mHadSurface = false; 22167603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy } 222aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 223aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 224b14dfe20ef300c47cc5cdfbd844c21f7fd302f0cJohn Reck /** @hide */ 225451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy @Override 226b14dfe20ef300c47cc5cdfbd844c21f7fd302f0cJohn Reck protected void onDetachedFromWindowInternal() { 22719b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck destroySurface(); 228b14dfe20ef300c47cc5cdfbd844c21f7fd302f0cJohn Reck super.onDetachedFromWindowInternal(); 22931f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy } 230451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 23131f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy private void destroySurface() { 23280429c458506485904715180d10584092a5cd082Romain Guy if (mLayer != null) { 233918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck mLayer.detachSurfaceTexture(); 2342af3524beb75150d347accc925022daa53b4a789Jamie Gennis 235402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba boolean shouldRelease = true; 236451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy if (mListener != null) { 237402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface); 238451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 239451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 2406be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy synchronized (mNativeWindowLock) { 2416be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy nDestroyNativeWindow(); 2426be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 2436be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 2446be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mLayer.destroy(); 245402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba if (shouldRelease) mSurface.release(); 246451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mSurface = null; 247451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mLayer = null; 24867603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy 24967603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy mHadSurface = true; 250451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 251451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 252451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 253aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 254aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * The layer type of a TextureView is ignored since a TextureView is always 255aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * considered to act as a hardware layer. The optional paint supplied to this 256aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * method will however be taken into account when rendering the content of 257aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * this TextureView. 258aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 259aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param layerType The ype of layer to use with this view, must be one of 260aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or 261aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_HARDWARE} 262aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param paint The paint used to compose the layer. This argument is optional 263aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * and can be null. It is ignored when the layer type is 264aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_NONE} 265aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 266aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 267aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void setLayerType(int layerType, Paint paint) { 268aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (paint != mLayerPaint) { 269c2079c968dc0a1da455be5be1c44a35028b00c70Romain Guy mLayerPaint = paint == null ? new Paint() : paint; 270aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy invalidate(); 271aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 272aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 273aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 27425fbb3fa1138675379102a44405852555cefccbdJohn Reck @Override 27525fbb3fa1138675379102a44405852555cefccbdJohn Reck public void setLayerPaint(Paint paint) { 27625fbb3fa1138675379102a44405852555cefccbdJohn Reck setLayerType(/* ignored */ 0, paint); 27725fbb3fa1138675379102a44405852555cefccbdJohn Reck } 27825fbb3fa1138675379102a44405852555cefccbdJohn Reck 279aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 280aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Always returns {@link #LAYER_TYPE_HARDWARE}. 281aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 282aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 283aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public int getLayerType() { 284aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return LAYER_TYPE_HARDWARE; 285aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 286aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 28759c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy @Override 28859c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy boolean hasStaticLayer() { 28959c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy return true; 29059c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy } 29159c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy 292aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 293aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Calling this method has no effect. 294aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 295aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 296aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void buildLayer() { 297aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 298aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 299aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 300aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Subclasses of TextureView cannot do their own rendering 301aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * with the {@link Canvas} object. 302aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 303aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param canvas The Canvas to which the View is rendered. 304aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 305aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 306aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public final void draw(Canvas canvas) { 30752b307ebc86e12e368694442eb8751e7e0de239eRomain Guy // NOTE: Maintain this carefully (see View.java) 30852b307ebc86e12e368694442eb8751e7e0de239eRomain Guy mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN; 30952b307ebc86e12e368694442eb8751e7e0de239eRomain Guy 31058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy applyUpdate(); 311c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias applyTransformMatrix(); 312aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 313aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 314aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 315aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Subclasses of TextureView cannot do their own rendering 316aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * with the {@link Canvas} object. 317aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 318aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param canvas The Canvas to which the View is rendered. 319aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 320aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 321aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy protected final void onDraw(Canvas canvas) { 322aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 323aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 324aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 3258f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy protected void onSizeChanged(int w, int h, int oldw, int oldh) { 3268f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy super.onSizeChanged(w, h, oldw, oldh); 3278f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (mSurface != null) { 32852a9a10b6b8c7b7a9f97777541841b94d4fd9754Mathias Agopian mSurface.setDefaultBufferSize(getWidth(), getHeight()); 32988801b270f693ffd4125534724f204135f592f72Romain Guy updateLayer(); 330451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy if (mListener != null) { 331451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight()); 332451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 3338f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 3348f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 3358f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 3361766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy /** 3371766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy * @hide 3381766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy */ 33916260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy @Override 34031f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy protected void destroyHardwareResources() { 34131f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy super.destroyHardwareResources(); 34231f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy destroySurface(); 34331f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy invalidateParentCaches(); 34431f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy invalidate(true); 34531f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy } 34631f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy 34731f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy @Override 3487e52caf6db5feef2b847cfaa3d13690257122c3aMichael Jurka HardwareLayer getHardwareLayer() { 34952b307ebc86e12e368694442eb8751e7e0de239eRomain Guy // NOTE: Maintain these two lines very carefully (see View.java) 35052b307ebc86e12e368694442eb8751e7e0de239eRomain Guy mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID; 35152b307ebc86e12e368694442eb8751e7e0de239eRomain Guy mPrivateFlags &= ~PFLAG_DIRTY_MASK; 35252b307ebc86e12e368694442eb8751e7e0de239eRomain Guy 353aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (mLayer == null) { 35458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) { 35558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy return null; 35658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 35758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 35804fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck mLayer = mAttachInfo.mHardwareRenderer.createTextureLayer(); 3592af3524beb75150d347accc925022daa53b4a789Jamie Gennis if (!mUpdateSurface) { 3608a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis // Create a new SurfaceTexture for the layer. 361918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck mSurface = new SurfaceTexture(false); 362918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck mLayer.setSurfaceTexture(mSurface); 3632af3524beb75150d347accc925022daa53b4a789Jamie Gennis } 36452a9a10b6b8c7b7a9f97777541841b94d4fd9754Mathias Agopian mSurface.setDefaultBufferSize(getWidth(), getHeight()); 3652af3524beb75150d347accc925022daa53b4a789Jamie Gennis nCreateNativeWindow(mSurface); 366aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 367c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler); 368aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 3698a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis if (mListener != null && !mUpdateSurface) { 370451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight()); 371aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 372d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase mLayer.setLayerPaint(mLayerPaint); 373aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 374aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 3752af3524beb75150d347accc925022daa53b4a789Jamie Gennis if (mUpdateSurface) { 3762af3524beb75150d347accc925022daa53b4a789Jamie Gennis // Someone has requested that we use a specific SurfaceTexture, so 3772af3524beb75150d347accc925022daa53b4a789Jamie Gennis // tell mLayer about it and set the SurfaceTexture to use the 3782af3524beb75150d347accc925022daa53b4a789Jamie Gennis // current view size. 3792af3524beb75150d347accc925022daa53b4a789Jamie Gennis mUpdateSurface = false; 38051f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy 38151f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy // Since we are updating the layer, force an update to ensure its 38251f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy // parameters are correct (width, height, transform, etc.) 38388801b270f693ffd4125534724f204135f592f72Romain Guy updateLayer(); 38451f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy mMatrixChanged = true; 38551f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy 38604fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck mLayer.setSurfaceTexture(mSurface); 38752a9a10b6b8c7b7a9f97777541841b94d4fd9754Mathias Agopian mSurface.setDefaultBufferSize(getWidth(), getHeight()); 3882af3524beb75150d347accc925022daa53b4a789Jamie Gennis } 3892af3524beb75150d347accc925022daa53b4a789Jamie Gennis 39058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy applyUpdate(); 391c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias applyTransformMatrix(); 392302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 393aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mLayer; 394aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 395aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 3968f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @Override 3978f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy protected void onVisibilityChanged(View changedView, int visibility) { 3988f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy super.onVisibilityChanged(changedView, visibility); 3998f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 4008f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (mSurface != null) { 4018f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // When the view becomes invisible, stop updating it, it's a waste of CPU 4028f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // To cancel updates, the easiest thing to do is simply to remove the 4038f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // updates listener 4048f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (visibility == VISIBLE) { 4052dedafb48f85e34a2f48262f12908866fc9de132John Reck if (mLayer != null) { 4062dedafb48f85e34a2f48262f12908866fc9de132John Reck mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler); 4072dedafb48f85e34a2f48262f12908866fc9de132John Reck } 40888801b270f693ffd4125534724f204135f592f72Romain Guy updateLayerAndInvalidate(); 4098f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } else { 4108f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mSurface.setOnFrameAvailableListener(null); 4118f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 4128f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 4138f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 4148f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 415a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy private void updateLayer() { 41688801b270f693ffd4125534724f204135f592f72Romain Guy synchronized (mLock) { 41788801b270f693ffd4125534724f204135f592f72Romain Guy mUpdateLayer = true; 41888801b270f693ffd4125534724f204135f592f72Romain Guy } 41988801b270f693ffd4125534724f204135f592f72Romain Guy } 42088801b270f693ffd4125534724f204135f592f72Romain Guy 42188801b270f693ffd4125534724f204135f592f72Romain Guy private void updateLayerAndInvalidate() { 42288801b270f693ffd4125534724f204135f592f72Romain Guy synchronized (mLock) { 42388801b270f693ffd4125534724f204135f592f72Romain Guy mUpdateLayer = true; 42488801b270f693ffd4125534724f204135f592f72Romain Guy } 42558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy invalidate(); 42658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 4272af3524beb75150d347accc925022daa53b4a789Jamie Gennis 42858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy private void applyUpdate() { 42958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy if (mLayer == null) { 430a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy return; 431a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 432a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 43358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy synchronized (mLock) { 43458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy if (mUpdateLayer) { 43558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy mUpdateLayer = false; 43658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } else { 43758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy return; 43858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 43958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 44058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 44104fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck mLayer.prepare(getWidth(), getHeight(), mOpaque); 44204fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck mLayer.updateSurfaceTexture(); 443a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 444cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba if (mListener != null) { 445cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba mListener.onSurfaceTextureUpdated(mSurface); 446cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba } 447a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 448a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 449aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 450302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * <p>Sets the transform to associate with this texture view. 451302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * The specified transform applies to the underlying surface 452302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * texture and does not affect the size or position of the view 453302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * itself, only of its content.</p> 454302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 455302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * <p>Some transforms might prevent the content from drawing 456302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * all the pixels contained within this view's bounds. In such 457302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * situations, make sure this texture view is not marked opaque.</p> 458302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 459302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @param transform The transform to apply to the content of 460302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * this view. 461302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 462302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @see #getTransform(android.graphics.Matrix) 463302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @see #isOpaque() 464302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @see #setOpaque(boolean) 465302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy */ 466302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy public void setTransform(Matrix transform) { 467302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy mMatrix.set(transform); 468302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy mMatrixChanged = true; 469c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias invalidateParentIfNeeded(); 470302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy } 471302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 472302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy /** 473302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * Returns the transform associated with this texture view. 474302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 475302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @param transform The {@link Matrix} in which to copy the current 476302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * transform. Can be null. 477302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 478302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @return The specified matrix if not null or a new {@link Matrix} 479302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * instance otherwise. 480302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 481302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @see #setTransform(android.graphics.Matrix) 482302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy */ 483302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy public Matrix getTransform(Matrix transform) { 484302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy if (transform == null) { 485302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy transform = new Matrix(); 486302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy } 487302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 488302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy transform.set(mMatrix); 489302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 490302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy return transform; 491302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy } 492302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 493c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias private void applyTransformMatrix() { 49451f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy if (mMatrixChanged && mLayer != null) { 495c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias mLayer.setTransform(mMatrix); 496c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias mMatrixChanged = false; 497c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias } 498c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias } 499c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias 500302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy /** 50177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Returns a {@link android.graphics.Bitmap} representation of the content 50277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * of the associated surface texture. If the surface texture is not available, 50377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * this method returns null.</p> 50477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 50577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888} 50677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * pixel format and its dimensions are the same as this view's.</p> 50777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 50877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 50977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 51077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 511d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs during the copy, an empty bitmap will be returned.</p> 512d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 51377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface 51477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture is not available or the width <= 0 or the height <= 0 51577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 51677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 51777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(android.graphics.Bitmap) 51877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(int, int) 51977a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 52077a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap() { 52177a811610f99e21da7f88dafef60d09f345d0506Romain Guy return getBitmap(getWidth(), getHeight()); 52277a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 52377a811610f99e21da7f88dafef60d09f345d0506Romain Guy 52477a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 52577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Returns a {@link android.graphics.Bitmap} representation of the content 52677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * of the associated surface texture. If the surface texture is not available, 52777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * this method returns null.</p> 52877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 52977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888} 53077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * pixel format.</p> 53177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 53277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 53377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 53477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 535d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs during the copy, an empty bitmap will be returned.</p> 536d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 53777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param width The width of the bitmap to create 53877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param height The height of the bitmap to create 53977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 54077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface 54177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture is not available or width is <= 0 or height is <= 0 54277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 54377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 54477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(android.graphics.Bitmap) 54577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap() 54677a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 54777a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap(int width, int height) { 54877a811610f99e21da7f88dafef60d09f345d0506Romain Guy if (isAvailable() && width > 0 && height > 0) { 549dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn return getBitmap(Bitmap.createBitmap(getResources().getDisplayMetrics(), 550dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn width, height, Bitmap.Config.ARGB_8888)); 55177a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 55277a811610f99e21da7f88dafef60d09f345d0506Romain Guy return null; 55377a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 55477a811610f99e21da7f88dafef60d09f345d0506Romain Guy 55577a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 55677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Copies the content of this view's surface texture into the specified 55777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * bitmap. If the surface texture is not available, the copy is not executed. 55877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * The content of the surface texture will be scaled to fit exactly inside 55977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * the specified bitmap.</p> 56077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 56177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 56277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 56377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 564d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs, the bitmap is left unchanged.</p> 565d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 56677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param bitmap The bitmap to copy the content of the surface texture into, 56777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * cannot be null, all configurations are supported 56877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 56977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return The bitmap specified as a parameter 57077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 57177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 57277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(int, int) 57377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap() 574589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy * 575589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy * @throws IllegalStateException if the hardware rendering context cannot be 576589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy * acquired to capture the bitmap 57777a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 57877a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap(Bitmap bitmap) { 57977a811610f99e21da7f88dafef60d09f345d0506Romain Guy if (bitmap != null && isAvailable()) { 580589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy applyUpdate(); 581589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy applyTransformMatrix(); 582589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy 58378245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy // This case can happen if the app invokes setSurfaceTexture() before 58478245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy // we are able to create the hardware layer. We can safely initialize 58578245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy // the layer here thanks to the validate() call at the beginning of 58678245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy // this method 58778245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy if (mLayer == null && mUpdateSurface) { 58878245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy getHardwareLayer(); 58978245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy } 59078245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy 59178245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy if (mLayer != null) { 59278245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy mLayer.copyInto(bitmap); 59378245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy } 59477a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 59577a811610f99e21da7f88dafef60d09f345d0506Romain Guy return bitmap; 59677a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 59777a811610f99e21da7f88dafef60d09f345d0506Romain Guy 59877a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 59977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * Returns true if the {@link SurfaceTexture} associated with this 60077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * TextureView is available for rendering. When this method returns 60177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * true, {@link #getSurfaceTexture()} returns a valid surface texture. 60277a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 60377a811610f99e21da7f88dafef60d09f345d0506Romain Guy public boolean isAvailable() { 60477a811610f99e21da7f88dafef60d09f345d0506Romain Guy return mSurface != null; 60577a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 60677a811610f99e21da7f88dafef60d09f345d0506Romain Guy 60777a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 6086be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * <p>Start editing the pixels in the surface. The returned Canvas can be used 6096be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to draw into the surface's bitmap. A null is returned if the surface has 6106be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * not been created or otherwise cannot be edited. You will usually need 6116be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to implement 6126be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * {@link SurfaceTextureListener#onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int)} 6136be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to find out when the Surface is available for use.</p> 6146be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6156be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * <p>The content of the Surface is never preserved between unlockCanvas() 6166be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * and lockCanvas(), for this reason, every pixel within the Surface area 6176be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * must be written. The only exception to this rule is when a dirty 6186be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * rectangle is specified, in which case, non-dirty pixels will be 6196be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * preserved.</p> 620462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * 621462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * <p>This method can only be used if the underlying surface is not already 622462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * owned by another producer. For instance, if the TextureView is being used 623462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * to render the camera's preview you cannot invoke this method.</p> 6246be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6256be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @return A Canvas used to draw into the surface. 6266be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6276be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas(android.graphics.Rect) 6286be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #unlockCanvasAndPost(android.graphics.Canvas) 6296be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy */ 6306be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy public Canvas lockCanvas() { 6316be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy return lockCanvas(null); 6326be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6336be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6346be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy /** 6356be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * Just like {@link #lockCanvas()} but allows specification of a dirty 6366be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * rectangle. Every pixel within that rectangle must be written; however 6376be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * pixels outside the dirty rectangle will be preserved by the next call 6386be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to lockCanvas(). 63953bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * 64053bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * This method can return null if the underlying surface texture is not 64153bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * available (see {@link #isAvailable()} or if the surface texture is 64253bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * already connected to an image producer (for instance: the camera, 64353bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * OpenGL, a media player, etc.) 6446be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6456be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @param dirty Area of the surface that will be modified. 6466be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6476be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @return A Canvas used to draw into the surface. 6486be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6496be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas() 65053bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * @see #unlockCanvasAndPost(android.graphics.Canvas) 65153bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * @see #isAvailable() 6526be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy */ 6536be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy public Canvas lockCanvas(Rect dirty) { 6546be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy if (!isAvailable()) return null; 6556be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6566be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy if (mCanvas == null) { 6576be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mCanvas = new Canvas(); 6586be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6596be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6606be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy synchronized (mNativeWindowLock) { 66153bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy if (!nLockCanvas(mNativeWindow, mCanvas, dirty)) { 66253bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy return null; 66353bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy } 6646be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6656be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mSaveCount = mCanvas.save(); 6666be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6676be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy return mCanvas; 6686be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6696be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6706be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy /** 6716be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * Finish editing pixels in the surface. After this call, the surface's 6726be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * current pixels will be shown on the screen, but its content is lost, 6736be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * in particular there is no guarantee that the content of the Surface 6746be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * will remain unchanged when lockCanvas() is called again. 6756be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6766be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @param canvas The Canvas previously returned by lockCanvas() 6776be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6786be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas() 6796be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas(android.graphics.Rect) 6806be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy */ 6816be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy public void unlockCanvasAndPost(Canvas canvas) { 6826be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy if (mCanvas != null && canvas == mCanvas) { 6836be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy canvas.restoreToCount(mSaveCount); 6846be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mSaveCount = 0; 6856be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6866be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy synchronized (mNativeWindowLock) { 6876be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy nUnlockCanvasAndPost(mNativeWindow, mCanvas); 6886be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6896be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6906be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6916be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6926be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy /** 693aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Returns the {@link SurfaceTexture} used by this view. This method 69477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * may return null if the view is not attached to a window or if the surface 69577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture has not been initialized yet. 69677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 69777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 698aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 699aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public SurfaceTexture getSurfaceTexture() { 700aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mSurface; 701aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 702aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 703aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 7042af3524beb75150d347accc925022daa53b4a789Jamie Gennis * Set the {@link SurfaceTexture} for this view to use. If a {@link 7052af3524beb75150d347accc925022daa53b4a789Jamie Gennis * SurfaceTexture} is already being used by this view, it is immediately 7062af3524beb75150d347accc925022daa53b4a789Jamie Gennis * released and not be usable any more. The {@link 7072af3524beb75150d347accc925022daa53b4a789Jamie Gennis * SurfaceTextureListener#onSurfaceTextureDestroyed} callback is <b>not</b> 7088a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis * called for the previous {@link SurfaceTexture}. Similarly, the {@link 7098a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis * SurfaceTextureListener#onSurfaceTextureAvailable} callback is <b>not</b> 7108a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis * called for the {@link SurfaceTexture} passed to setSurfaceTexture. 7112af3524beb75150d347accc925022daa53b4a789Jamie Gennis * 7122af3524beb75150d347accc925022daa53b4a789Jamie Gennis * The {@link SurfaceTexture} object must be detached from all OpenGL ES 7132af3524beb75150d347accc925022daa53b4a789Jamie Gennis * contexts prior to calling this method. 7142af3524beb75150d347accc925022daa53b4a789Jamie Gennis * 7152af3524beb75150d347accc925022daa53b4a789Jamie Gennis * @param surfaceTexture The {@link SurfaceTexture} that the view should use. 7162af3524beb75150d347accc925022daa53b4a789Jamie Gennis * @see SurfaceTexture#detachFromGLContext() 7172af3524beb75150d347accc925022daa53b4a789Jamie Gennis */ 7182af3524beb75150d347accc925022daa53b4a789Jamie Gennis public void setSurfaceTexture(SurfaceTexture surfaceTexture) { 7192af3524beb75150d347accc925022daa53b4a789Jamie Gennis if (surfaceTexture == null) { 7202af3524beb75150d347accc925022daa53b4a789Jamie Gennis throw new NullPointerException("surfaceTexture must not be null"); 7212af3524beb75150d347accc925022daa53b4a789Jamie Gennis } 7222af3524beb75150d347accc925022daa53b4a789Jamie Gennis if (mSurface != null) { 7232af3524beb75150d347accc925022daa53b4a789Jamie Gennis mSurface.release(); 7242af3524beb75150d347accc925022daa53b4a789Jamie Gennis } 7252af3524beb75150d347accc925022daa53b4a789Jamie Gennis mSurface = surfaceTexture; 7262af3524beb75150d347accc925022daa53b4a789Jamie Gennis mUpdateSurface = true; 7272af3524beb75150d347accc925022daa53b4a789Jamie Gennis invalidateParentIfNeeded(); 7282af3524beb75150d347accc925022daa53b4a789Jamie Gennis } 7292af3524beb75150d347accc925022daa53b4a789Jamie Gennis 7302af3524beb75150d347accc925022daa53b4a789Jamie Gennis /** 731aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Returns the {@link SurfaceTextureListener} currently associated with this 732aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * texture view. 733aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 734aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener) 735aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTextureListener 736aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 737aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public SurfaceTextureListener getSurfaceTextureListener() { 738aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mListener; 739aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 740aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 741aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 742aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Sets the {@link SurfaceTextureListener} used to listen to surface 743aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * texture events. 744aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 745aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see #getSurfaceTextureListener() 746aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTextureListener 747aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 748aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void setSurfaceTextureListener(SurfaceTextureListener listener) { 749aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mListener = listener; 750aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 751aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 752c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown private final SurfaceTexture.OnFrameAvailableListener mUpdateListener = 753c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown new SurfaceTexture.OnFrameAvailableListener() { 754c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown @Override 755c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown public void onFrameAvailable(SurfaceTexture surfaceTexture) { 756c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown updateLayer(); 757c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown invalidate(); 758c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown } 759c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown }; 760c7282e57cd01f1576baac04356bf99bee34e4c18Jeff Brown 761aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 762aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * This listener can be used to be notified when the surface texture 763aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * associated with this texture view is available. 764aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 765aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public static interface SurfaceTextureListener { 766aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 767aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use. 768aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 769aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param surface The surface returned by 770aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link android.view.TextureView#getSurfaceTexture()} 771451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param width The width of the surface 772451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param height The height of the surface 773aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 774451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height); 7758f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 7768f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy /** 7778f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * Invoked when the {@link SurfaceTexture}'s buffers size changed. 7788f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * 7798f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param surface The surface returned by 7808f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * {@link android.view.TextureView#getSurfaceTexture()} 7818f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param width The new width of the surface 7828f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param height The new height of the surface 7838f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy */ 7848f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height); 785451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 786451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy /** 787451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * Invoked when the specified {@link SurfaceTexture} is about to be destroyed. 788402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba * If returns true, no rendering should happen inside the surface texture after this method 789402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba * is invoked. If returns false, the client needs to call {@link SurfaceTexture#release()}. 79052b307ebc86e12e368694442eb8751e7e0de239eRomain Guy * Most applications should return true. 791451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * 792451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param surface The surface about to be destroyed 793451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy */ 794402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba public boolean onSurfaceTextureDestroyed(SurfaceTexture surface); 795cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba 796cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba /** 797cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * Invoked when the specified {@link SurfaceTexture} is updated through 798cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * {@link SurfaceTexture#updateTexImage()}. 799cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 800cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * @param surface The surface just updated 801cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba */ 802cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba public void onSurfaceTextureUpdated(SurfaceTexture surface); 803aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 8048f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 8056be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private native void nCreateNativeWindow(SurfaceTexture surface); 8066be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private native void nDestroyNativeWindow(); 8076be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 80836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native boolean nLockCanvas(long nativeWindow, Canvas canvas, Rect dirty); 80936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native void nUnlockCanvasAndPost(long nativeWindow, Canvas canvas); 810aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy} 811