TextureView.java revision b14dfe20ef300c47cc5cdfbd844c21f7fd302f0c
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; 2652c145ff5ce34980d866155bb154913a847d955bRomain Guyimport android.os.Looper; 27aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.AttributeSet; 28aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.Log; 29aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 30aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/** 31aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView can be used to display a content stream. Such a content 32aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * stream can for instance be a video or an OpenGL scene. The content stream 33aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can come from the application's process as well as a remote process.</p> 34aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 35aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>TextureView can only be used in a hardware accelerated window. When 36aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * rendered in software, TextureView will draw nothing.</p> 37aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 38aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Unlike {@link SurfaceView}, TextureView does not create a separate 39aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * window but behaves as a regular View. This key difference allows a 40aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView to be moved, transformed, animated, etc. For instance, you 41aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can make a TextureView semi-translucent by calling 42aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <code>myView.setAlpha(0.5f)</code>.</p> 43aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 44aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Using a TextureView is simple: all you need to do is get its 45aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link SurfaceTexture}. The {@link SurfaceTexture} can then be used to 46aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * render content. The following example demonstrates how to render the 47aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * camera preview into a TextureView:</p> 48aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 49aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <pre> 50aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener { 51aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * private Camera mCamera; 52aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * private TextureView mTextureView; 53aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 54aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * protected void onCreate(Bundle savedInstanceState) { 55aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * super.onCreate(savedInstanceState); 56aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 57aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mTextureView = new TextureView(this); 58aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mTextureView.setSurfaceTextureListener(this); 59aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 60aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * setContentView(mTextureView); 61aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 62aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 63451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 64aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mCamera = Camera.open(); 65aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 66aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * try { 67aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mCamera.setPreviewTexture(surface); 68aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mCamera.startPreview(); 69aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } catch (IOException ioe) { 70aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * // Something bad happened 71aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 72aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 73cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 748f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { 758f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * // Ignored, Camera does all the work for us 768f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * } 77cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 78402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba * public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 79451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * mCamera.stopPreview(); 80451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * mCamera.release(); 81402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba * return true; 82451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * } 83cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 84cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * public void onSurfaceTextureUpdated(SurfaceTexture surface) { 8558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy * // Invoked every time there's a new Camera preview frame 86cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * } 87aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 88aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * </pre> 89aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 90aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView's SurfaceTexture can be obtained either by invoking 91aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #getSurfaceTexture()} or by using a {@link SurfaceTextureListener}. 92aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * It is important to know that a SurfaceTexture is available only after the 93aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView is attached to a window (and {@link #onAttachedToWindow()} has 94aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * been invoked.) It is therefore highly recommended you use a listener to 95aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * be notified when the SurfaceTexture becomes available.</p> 96aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 97462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * <p>It is important to note that only one producer can use the TextureView. 98462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * For instance, if you use a TextureView to display the camera preview, you 99462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * cannot use {@link #lockCanvas()} to draw onto the TextureView at the same 100462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * time.</p> 101462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * 102aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceView 103aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTexture 104aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 105aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypublic class TextureView extends View { 10677a811610f99e21da7f88dafef60d09f345d0506Romain Guy private static final String LOG_TAG = "TextureView"; 10777a811610f99e21da7f88dafef60d09f345d0506Romain Guy 108aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private HardwareLayer mLayer; 109aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private SurfaceTexture mSurface; 110aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private SurfaceTextureListener mListener; 11167603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy private boolean mHadSurface; 112aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 113a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy private boolean mOpaque = true; 114c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy 115302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy private final Matrix mMatrix = new Matrix(); 116302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy private boolean mMatrixChanged; 117302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 11858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy private final Object[] mLock = new Object[0]; 11958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy private boolean mUpdateLayer; 1202af3524beb75150d347accc925022daa53b4a789Jamie Gennis private boolean mUpdateSurface; 12158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 1228f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy private SurfaceTexture.OnFrameAvailableListener mUpdateListener; 123aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 1246be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private Canvas mCanvas; 1256be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private int mSaveCount; 1266be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 1276be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private final Object[] mNativeWindowLock = new Object[0]; 1286be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy // Used from native code, do not write! 1296be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy @SuppressWarnings({"UnusedDeclaration"}) 13036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private long mNativeWindow; 1316be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 132aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 133aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 134aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 135aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 136aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 137aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public TextureView(Context context) { 138aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super(context); 139aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 140aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 141aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 142aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 143aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 144aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 145aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 146aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param attrs The attributes of the XML tag that is inflating the view. 147aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 148aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @SuppressWarnings({"UnusedDeclaration"}) 149aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public TextureView(Context context, AttributeSet attrs) { 150aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super(context, attrs); 151aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 152aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 153aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 154aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 155aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 156aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 157aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 158aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param attrs The attributes of the XML tag that is inflating the view. 159617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleAttr An attribute in the current theme that contains a 160617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * reference to a style resource that supplies default values for 161617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * the view. Can be 0 to not look for defaults. 162aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 163aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @SuppressWarnings({"UnusedDeclaration"}) 164617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public TextureView(Context context, AttributeSet attrs, int defStyleAttr) { 165617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette super(context, attrs, defStyleAttr); 166617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette init(); 167617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette } 168617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette 169617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette /** 170617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * Creates a new TextureView. 171617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * 172617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param context The context to associate this view with. 173617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param attrs The attributes of the XML tag that is inflating the view. 174617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleAttr An attribute in the current theme that contains a 175617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * reference to a style resource that supplies default values for 176617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * the view. Can be 0 to not look for defaults. 177617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleRes A resource identifier of a style resource that 178617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * supplies default values for the view, used only if 179617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * defStyleAttr is 0 or can not be found in the theme. Can be 0 180617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * to not look for defaults. 181617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette */ 182617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette @SuppressWarnings({"UnusedDeclaration"}) 183617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public TextureView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 184617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette super(context, attrs, defStyleAttr, defStyleRes); 185aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 186aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 187aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 188aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private void init() { 189aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mLayerPaint = new Paint(); 190aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 191aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 192a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy /** 193a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * {@inheritDoc} 194a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy */ 195a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy @Override 196a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy public boolean isOpaque() { 197a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy return mOpaque; 198a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 199a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 200a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy /** 201a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * Indicates whether the content of this TextureView is opaque. The 202a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * content is assumed to be opaque by default. 203a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * 204a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * @param opaque True if the content of this TextureView is opaque, 205a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * false otherwise 206a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy */ 207a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy public void setOpaque(boolean opaque) { 208a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy if (opaque != mOpaque) { 209a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy mOpaque = opaque; 210a8a2f97c100af4ba52c61c3b59c933f44a53dad4Romain Guy if (mLayer != null) { 21188801b270f693ffd4125534724f204135f592f72Romain Guy updateLayerAndInvalidate(); 212a8a2f97c100af4ba52c61c3b59c933f44a53dad4Romain Guy } 213a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 214a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 215a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 216aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 217aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy protected void onAttachedToWindow() { 218aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super.onAttachedToWindow(); 219aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 220aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (!isHardwareAccelerated()) { 22177a811610f99e21da7f88dafef60d09f345d0506Romain Guy Log.w(LOG_TAG, "A TextureView or a subclass can only be " 222aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy + "used with hardware acceleration enabled."); 223aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 22467603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy 22567603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy if (mHadSurface) { 22667603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy invalidate(true); 22767603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy mHadSurface = false; 22867603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy } 229aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 230aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 231b14dfe20ef300c47cc5cdfbd844c21f7fd302f0cJohn Reck /** @hide */ 232451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy @Override 233b14dfe20ef300c47cc5cdfbd844c21f7fd302f0cJohn Reck protected void onDetachedFromWindowInternal() { 23419b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck destroySurface(); 235b14dfe20ef300c47cc5cdfbd844c21f7fd302f0cJohn Reck super.onDetachedFromWindowInternal(); 23631f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy } 237451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 23831f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy private void destroySurface() { 23980429c458506485904715180d10584092a5cd082Romain Guy if (mLayer != null) { 24019b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck mLayer.detachSurfaceTexture(mSurface); 2412af3524beb75150d347accc925022daa53b4a789Jamie Gennis 242402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba boolean shouldRelease = true; 243451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy if (mListener != null) { 244402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface); 245451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 246451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 2476be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy synchronized (mNativeWindowLock) { 2486be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy nDestroyNativeWindow(); 2496be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 2506be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 2516be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mLayer.destroy(); 252402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba if (shouldRelease) mSurface.release(); 253451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mSurface = null; 254451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mLayer = null; 25567603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy 25667603c6e1b88fa20db58f69354e3925ffba037d1Romain Guy mHadSurface = true; 257451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 258451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 259451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 260aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 261aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * The layer type of a TextureView is ignored since a TextureView is always 262aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * considered to act as a hardware layer. The optional paint supplied to this 263aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * method will however be taken into account when rendering the content of 264aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * this TextureView. 265aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 266aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param layerType The ype of layer to use with this view, must be one of 267aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or 268aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_HARDWARE} 269aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param paint The paint used to compose the layer. This argument is optional 270aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * and can be null. It is ignored when the layer type is 271aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_NONE} 272aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 273aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 274aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void setLayerType(int layerType, Paint paint) { 275aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (paint != mLayerPaint) { 276c2079c968dc0a1da455be5be1c44a35028b00c70Romain Guy mLayerPaint = paint == null ? new Paint() : paint; 277aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy invalidate(); 278aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 279aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 280aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 281aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 282aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Always returns {@link #LAYER_TYPE_HARDWARE}. 283aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 284aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 285aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public int getLayerType() { 286aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return LAYER_TYPE_HARDWARE; 287aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 288aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 28959c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy @Override 29059c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy boolean hasStaticLayer() { 29159c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy return true; 29259c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy } 29359c7f80dd20258cefa1fc4bdd3c9a709a8dd53b8Romain Guy 294aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 295aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Calling this method has no effect. 296aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 297aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 298aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void buildLayer() { 299aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 300aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 301aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 302aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Subclasses of TextureView cannot do their own rendering 303aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * with the {@link Canvas} object. 304aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 305aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param canvas The Canvas to which the View is rendered. 306aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 307aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 308aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public final void draw(Canvas canvas) { 30952b307ebc86e12e368694442eb8751e7e0de239eRomain Guy // NOTE: Maintain this carefully (see View.java) 31052b307ebc86e12e368694442eb8751e7e0de239eRomain Guy mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN; 31152b307ebc86e12e368694442eb8751e7e0de239eRomain Guy 31258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy applyUpdate(); 313c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias applyTransformMatrix(); 314aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 315aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 316aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 317aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Subclasses of TextureView cannot do their own rendering 318aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * with the {@link Canvas} object. 319aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 320aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param canvas The Canvas to which the View is rendered. 321aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 322aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 323aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy protected final void onDraw(Canvas canvas) { 324aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 325aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 326aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 3278f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy protected void onSizeChanged(int w, int h, int oldw, int oldh) { 3288f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy super.onSizeChanged(w, h, oldw, oldh); 3298f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (mSurface != null) { 33052a9a10b6b8c7b7a9f97777541841b94d4fd9754Mathias Agopian mSurface.setDefaultBufferSize(getWidth(), getHeight()); 33188801b270f693ffd4125534724f204135f592f72Romain Guy updateLayer(); 332451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy if (mListener != null) { 333451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight()); 334451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 3358f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 3368f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 3378f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 3388f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @Override 339a998dff5d49a423aaf7097aa8f96bf5bdc681d25Romain Guy boolean destroyLayer(boolean valid) { 34016260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy return false; 34116260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy } 34216260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy 3431766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy /** 3441766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy * @hide 3451766b0e25de5a66f9d0f6e73a2c342272fcadc71Romain Guy */ 34616260e73f6c1c9dc94acf0d328a3c564426b8711Romain Guy @Override 34731f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy protected void destroyHardwareResources() { 34831f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy super.destroyHardwareResources(); 34931f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy destroySurface(); 35031f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy invalidateParentCaches(); 35131f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy invalidate(true); 35231f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy } 35331f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy 35431f2c2e94656530fbf6282803e62edb47e9a894dRomain Guy @Override 3557e52caf6db5feef2b847cfaa3d13690257122c3aMichael Jurka HardwareLayer getHardwareLayer() { 35652b307ebc86e12e368694442eb8751e7e0de239eRomain Guy // NOTE: Maintain these two lines very carefully (see View.java) 35752b307ebc86e12e368694442eb8751e7e0de239eRomain Guy mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID; 35852b307ebc86e12e368694442eb8751e7e0de239eRomain Guy mPrivateFlags &= ~PFLAG_DIRTY_MASK; 35952b307ebc86e12e368694442eb8751e7e0de239eRomain Guy 360aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (mLayer == null) { 36158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) { 36258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy return null; 36358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 36458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 36504fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck mLayer = mAttachInfo.mHardwareRenderer.createTextureLayer(); 3662af3524beb75150d347accc925022daa53b4a789Jamie Gennis if (!mUpdateSurface) { 3678a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis // Create a new SurfaceTexture for the layer. 3682af3524beb75150d347accc925022daa53b4a789Jamie Gennis mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer); 3692af3524beb75150d347accc925022daa53b4a789Jamie Gennis } 37052a9a10b6b8c7b7a9f97777541841b94d4fd9754Mathias Agopian mSurface.setDefaultBufferSize(getWidth(), getHeight()); 3712af3524beb75150d347accc925022daa53b4a789Jamie Gennis nCreateNativeWindow(mSurface); 372aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 3738f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() { 374aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 375aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void onFrameAvailable(SurfaceTexture surfaceTexture) { 376aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy // Per SurfaceTexture's documentation, the callback may be invoked 377aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy // from an arbitrary thread 37888801b270f693ffd4125534724f204135f592f72Romain Guy updateLayer(); 37952c145ff5ce34980d866155bb154913a847d955bRomain Guy 38052c145ff5ce34980d866155bb154913a847d955bRomain Guy if (Looper.myLooper() == Looper.getMainLooper()) { 38152c145ff5ce34980d866155bb154913a847d955bRomain Guy invalidate(); 38252c145ff5ce34980d866155bb154913a847d955bRomain Guy } else { 38352c145ff5ce34980d866155bb154913a847d955bRomain Guy postInvalidate(); 38452c145ff5ce34980d866155bb154913a847d955bRomain Guy } 385aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 3868f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy }; 3878f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mSurface.setOnFrameAvailableListener(mUpdateListener); 388aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 3898a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis if (mListener != null && !mUpdateSurface) { 390451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight()); 391aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 392d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase mLayer.setLayerPaint(mLayerPaint); 393aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 394aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 3952af3524beb75150d347accc925022daa53b4a789Jamie Gennis if (mUpdateSurface) { 3962af3524beb75150d347accc925022daa53b4a789Jamie Gennis // Someone has requested that we use a specific SurfaceTexture, so 3972af3524beb75150d347accc925022daa53b4a789Jamie Gennis // tell mLayer about it and set the SurfaceTexture to use the 3982af3524beb75150d347accc925022daa53b4a789Jamie Gennis // current view size. 3992af3524beb75150d347accc925022daa53b4a789Jamie Gennis mUpdateSurface = false; 40051f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy 40151f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy // Since we are updating the layer, force an update to ensure its 40251f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy // parameters are correct (width, height, transform, etc.) 40388801b270f693ffd4125534724f204135f592f72Romain Guy updateLayer(); 40451f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy mMatrixChanged = true; 40551f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy 40604fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck mLayer.setSurfaceTexture(mSurface); 40752a9a10b6b8c7b7a9f97777541841b94d4fd9754Mathias Agopian mSurface.setDefaultBufferSize(getWidth(), getHeight()); 4082af3524beb75150d347accc925022daa53b4a789Jamie Gennis } 4092af3524beb75150d347accc925022daa53b4a789Jamie Gennis 41058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy applyUpdate(); 411c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias applyTransformMatrix(); 412302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 413aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mLayer; 414aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 415aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 4168f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @Override 4178f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy protected void onVisibilityChanged(View changedView, int visibility) { 4188f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy super.onVisibilityChanged(changedView, visibility); 4198f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 4208f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (mSurface != null) { 4218f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // When the view becomes invisible, stop updating it, it's a waste of CPU 4228f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // To cancel updates, the easiest thing to do is simply to remove the 4238f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // updates listener 4248f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (visibility == VISIBLE) { 4258f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mSurface.setOnFrameAvailableListener(mUpdateListener); 42688801b270f693ffd4125534724f204135f592f72Romain Guy updateLayerAndInvalidate(); 4278f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } else { 4288f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mSurface.setOnFrameAvailableListener(null); 4298f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 4308f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 4318f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 4328f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 433a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy private void updateLayer() { 43488801b270f693ffd4125534724f204135f592f72Romain Guy synchronized (mLock) { 43588801b270f693ffd4125534724f204135f592f72Romain Guy mUpdateLayer = true; 43688801b270f693ffd4125534724f204135f592f72Romain Guy } 43788801b270f693ffd4125534724f204135f592f72Romain Guy } 43888801b270f693ffd4125534724f204135f592f72Romain Guy 43988801b270f693ffd4125534724f204135f592f72Romain Guy private void updateLayerAndInvalidate() { 44088801b270f693ffd4125534724f204135f592f72Romain Guy synchronized (mLock) { 44188801b270f693ffd4125534724f204135f592f72Romain Guy mUpdateLayer = true; 44288801b270f693ffd4125534724f204135f592f72Romain Guy } 44358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy invalidate(); 44458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 4452af3524beb75150d347accc925022daa53b4a789Jamie Gennis 44658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy private void applyUpdate() { 44758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy if (mLayer == null) { 448a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy return; 449a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 450a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 45158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy synchronized (mLock) { 45258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy if (mUpdateLayer) { 45358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy mUpdateLayer = false; 45458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } else { 45558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy return; 45658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 45758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 45858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 45904fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck mLayer.prepare(getWidth(), getHeight(), mOpaque); 46004fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck mLayer.updateSurfaceTexture(); 461a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 462cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba if (mListener != null) { 463cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba mListener.onSurfaceTextureUpdated(mSurface); 464cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba } 465a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 466a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 467aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 468302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * <p>Sets the transform to associate with this texture view. 469302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * The specified transform applies to the underlying surface 470302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * texture and does not affect the size or position of the view 471302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * itself, only of its content.</p> 472302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 473302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * <p>Some transforms might prevent the content from drawing 474302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * all the pixels contained within this view's bounds. In such 475302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * situations, make sure this texture view is not marked opaque.</p> 476302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 477302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @param transform The transform to apply to the content of 478302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * this view. 479302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 480302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @see #getTransform(android.graphics.Matrix) 481302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @see #isOpaque() 482302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @see #setOpaque(boolean) 483302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy */ 484302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy public void setTransform(Matrix transform) { 485302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy mMatrix.set(transform); 486302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy mMatrixChanged = true; 487c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias invalidateParentIfNeeded(); 488302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy } 489302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 490302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy /** 491302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * Returns the transform associated with this texture view. 492302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 493302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @param transform The {@link Matrix} in which to copy the current 494302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * transform. Can be null. 495302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 496302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @return The specified matrix if not null or a new {@link Matrix} 497302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * instance otherwise. 498302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * 499302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy * @see #setTransform(android.graphics.Matrix) 500302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy */ 501302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy public Matrix getTransform(Matrix transform) { 502302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy if (transform == null) { 503302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy transform = new Matrix(); 504302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy } 505302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 506302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy transform.set(mMatrix); 507302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 508302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy return transform; 509302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy } 510302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy 511c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias private void applyTransformMatrix() { 51251f7c6b3620549429cd6c62e38bace43085e04fbRomain Guy if (mMatrixChanged && mLayer != null) { 513c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias mLayer.setTransform(mMatrix); 514c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias mMatrixChanged = false; 515c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias } 516c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias } 517c01391fb4eb0eef33d142e89e060eac7e75de39dAlexandre Elias 518302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy /** 51977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Returns a {@link android.graphics.Bitmap} representation of the content 52077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * of the associated surface texture. If the surface texture is not available, 52177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * this method returns null.</p> 52277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 52377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888} 52477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * pixel format and its dimensions are the same as this view's.</p> 52577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 52677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 52777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 52877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 529d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs during the copy, an empty bitmap will be returned.</p> 530d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 53177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface 53277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture is not available or the width <= 0 or the height <= 0 53377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 53477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 53577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(android.graphics.Bitmap) 53677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(int, int) 53777a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 53877a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap() { 53977a811610f99e21da7f88dafef60d09f345d0506Romain Guy return getBitmap(getWidth(), getHeight()); 54077a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 54177a811610f99e21da7f88dafef60d09f345d0506Romain Guy 54277a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 54377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Returns a {@link android.graphics.Bitmap} representation of the content 54477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * of the associated surface texture. If the surface texture is not available, 54577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * this method returns null.</p> 54677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 54777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888} 54877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * pixel format.</p> 54977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 55077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 55177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 55277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 553d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs during the copy, an empty bitmap will be returned.</p> 554d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 55577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param width The width of the bitmap to create 55677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param height The height of the bitmap to create 55777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 55877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface 55977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture is not available or width is <= 0 or height is <= 0 56077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 56177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 56277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(android.graphics.Bitmap) 56377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap() 56477a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 56577a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap(int width, int height) { 56677a811610f99e21da7f88dafef60d09f345d0506Romain Guy if (isAvailable() && width > 0 && height > 0) { 567dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn return getBitmap(Bitmap.createBitmap(getResources().getDisplayMetrics(), 568dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn width, height, Bitmap.Config.ARGB_8888)); 56977a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 57077a811610f99e21da7f88dafef60d09f345d0506Romain Guy return null; 57177a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 57277a811610f99e21da7f88dafef60d09f345d0506Romain Guy 57377a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 57477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Copies the content of this view's surface texture into the specified 57577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * bitmap. If the surface texture is not available, the copy is not executed. 57677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * The content of the surface texture will be scaled to fit exactly inside 57777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * the specified bitmap.</p> 57877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 57977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 58077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 58177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 582d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs, the bitmap is left unchanged.</p> 583d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 58477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param bitmap The bitmap to copy the content of the surface texture into, 58577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * cannot be null, all configurations are supported 58677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 58777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return The bitmap specified as a parameter 58877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 58977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 59077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(int, int) 59177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap() 592589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy * 593589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy * @throws IllegalStateException if the hardware rendering context cannot be 594589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy * acquired to capture the bitmap 59577a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 59677a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap(Bitmap bitmap) { 59777a811610f99e21da7f88dafef60d09f345d0506Romain Guy if (bitmap != null && isAvailable()) { 598589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy applyUpdate(); 599589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy applyTransformMatrix(); 600589b0bb6ab81657ba201cbc441a49f85305170bcRomain Guy 60178245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy // This case can happen if the app invokes setSurfaceTexture() before 60278245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy // we are able to create the hardware layer. We can safely initialize 60378245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy // the layer here thanks to the validate() call at the beginning of 60478245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy // this method 60578245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy if (mLayer == null && mUpdateSurface) { 60678245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy getHardwareLayer(); 60778245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy } 60878245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy 60978245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy if (mLayer != null) { 61078245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy mLayer.copyInto(bitmap); 61178245f77d2724ee3a053f13fbcb0359751b9f842Romain Guy } 61277a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 61377a811610f99e21da7f88dafef60d09f345d0506Romain Guy return bitmap; 61477a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 61577a811610f99e21da7f88dafef60d09f345d0506Romain Guy 61677a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 61777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * Returns true if the {@link SurfaceTexture} associated with this 61877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * TextureView is available for rendering. When this method returns 61977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * true, {@link #getSurfaceTexture()} returns a valid surface texture. 62077a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 62177a811610f99e21da7f88dafef60d09f345d0506Romain Guy public boolean isAvailable() { 62277a811610f99e21da7f88dafef60d09f345d0506Romain Guy return mSurface != null; 62377a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 62477a811610f99e21da7f88dafef60d09f345d0506Romain Guy 62577a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 6266be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * <p>Start editing the pixels in the surface. The returned Canvas can be used 6276be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to draw into the surface's bitmap. A null is returned if the surface has 6286be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * not been created or otherwise cannot be edited. You will usually need 6296be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to implement 6306be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * {@link SurfaceTextureListener#onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int)} 6316be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to find out when the Surface is available for use.</p> 6326be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6336be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * <p>The content of the Surface is never preserved between unlockCanvas() 6346be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * and lockCanvas(), for this reason, every pixel within the Surface area 6356be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * must be written. The only exception to this rule is when a dirty 6366be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * rectangle is specified, in which case, non-dirty pixels will be 6376be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * preserved.</p> 638462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * 639462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * <p>This method can only be used if the underlying surface is not already 640462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * owned by another producer. For instance, if the TextureView is being used 641462785fa257671fe4905d1d3e6ca27e4a61ee946Romain Guy * to render the camera's preview you cannot invoke this method.</p> 6426be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6436be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @return A Canvas used to draw into the surface. 6446be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6456be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas(android.graphics.Rect) 6466be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #unlockCanvasAndPost(android.graphics.Canvas) 6476be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy */ 6486be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy public Canvas lockCanvas() { 6496be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy return lockCanvas(null); 6506be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6516be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6526be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy /** 6536be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * Just like {@link #lockCanvas()} but allows specification of a dirty 6546be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * rectangle. Every pixel within that rectangle must be written; however 6556be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * pixels outside the dirty rectangle will be preserved by the next call 6566be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to lockCanvas(). 65753bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * 65853bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * This method can return null if the underlying surface texture is not 65953bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * available (see {@link #isAvailable()} or if the surface texture is 66053bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * already connected to an image producer (for instance: the camera, 66153bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * OpenGL, a media player, etc.) 6626be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6636be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @param dirty Area of the surface that will be modified. 6646be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6656be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @return A Canvas used to draw into the surface. 6666be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6676be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas() 66853bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * @see #unlockCanvasAndPost(android.graphics.Canvas) 66953bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy * @see #isAvailable() 6706be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy */ 6716be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy public Canvas lockCanvas(Rect dirty) { 6726be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy if (!isAvailable()) return null; 6736be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6746be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy if (mCanvas == null) { 6756be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mCanvas = new Canvas(); 6766be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6776be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6786be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy synchronized (mNativeWindowLock) { 67953bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy if (!nLockCanvas(mNativeWindow, mCanvas, dirty)) { 68053bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy return null; 68153bacf5a91a760f6c0a966ed2f50a25e7fe12aebRomain Guy } 6826be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6836be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mSaveCount = mCanvas.save(); 6846be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6856be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy return mCanvas; 6866be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 6876be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6886be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy /** 6896be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * Finish editing pixels in the surface. After this call, the surface's 6906be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * current pixels will be shown on the screen, but its content is lost, 6916be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * in particular there is no guarantee that the content of the Surface 6926be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * will remain unchanged when lockCanvas() is called again. 6936be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6946be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @param canvas The Canvas previously returned by lockCanvas() 6956be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 6966be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas() 6976be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas(android.graphics.Rect) 6986be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy */ 6996be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy public void unlockCanvasAndPost(Canvas canvas) { 7006be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy if (mCanvas != null && canvas == mCanvas) { 7016be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy canvas.restoreToCount(mSaveCount); 7026be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mSaveCount = 0; 7036be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 7046be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy synchronized (mNativeWindowLock) { 7056be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy nUnlockCanvasAndPost(mNativeWindow, mCanvas); 7066be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 7076be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 7086be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 7096be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 7106be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy /** 711aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Returns the {@link SurfaceTexture} used by this view. This method 71277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * may return null if the view is not attached to a window or if the surface 71377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture has not been initialized yet. 71477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 71577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 716aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 717aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public SurfaceTexture getSurfaceTexture() { 718aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mSurface; 719aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 720aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 721aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 7222af3524beb75150d347accc925022daa53b4a789Jamie Gennis * Set the {@link SurfaceTexture} for this view to use. If a {@link 7232af3524beb75150d347accc925022daa53b4a789Jamie Gennis * SurfaceTexture} is already being used by this view, it is immediately 7242af3524beb75150d347accc925022daa53b4a789Jamie Gennis * released and not be usable any more. The {@link 7252af3524beb75150d347accc925022daa53b4a789Jamie Gennis * SurfaceTextureListener#onSurfaceTextureDestroyed} callback is <b>not</b> 7268a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis * called for the previous {@link SurfaceTexture}. Similarly, the {@link 7278a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis * SurfaceTextureListener#onSurfaceTextureAvailable} callback is <b>not</b> 7288a34d6800e70da45bac662873f6951c8d8295a15Jamie Gennis * called for the {@link SurfaceTexture} passed to setSurfaceTexture. 7292af3524beb75150d347accc925022daa53b4a789Jamie Gennis * 7302af3524beb75150d347accc925022daa53b4a789Jamie Gennis * The {@link SurfaceTexture} object must be detached from all OpenGL ES 7312af3524beb75150d347accc925022daa53b4a789Jamie Gennis * contexts prior to calling this method. 7322af3524beb75150d347accc925022daa53b4a789Jamie Gennis * 7332af3524beb75150d347accc925022daa53b4a789Jamie Gennis * @param surfaceTexture The {@link SurfaceTexture} that the view should use. 7342af3524beb75150d347accc925022daa53b4a789Jamie Gennis * @see SurfaceTexture#detachFromGLContext() 7352af3524beb75150d347accc925022daa53b4a789Jamie Gennis */ 7362af3524beb75150d347accc925022daa53b4a789Jamie Gennis public void setSurfaceTexture(SurfaceTexture surfaceTexture) { 7372af3524beb75150d347accc925022daa53b4a789Jamie Gennis if (surfaceTexture == null) { 7382af3524beb75150d347accc925022daa53b4a789Jamie Gennis throw new NullPointerException("surfaceTexture must not be null"); 7392af3524beb75150d347accc925022daa53b4a789Jamie Gennis } 7402af3524beb75150d347accc925022daa53b4a789Jamie Gennis if (mSurface != null) { 7412af3524beb75150d347accc925022daa53b4a789Jamie Gennis mSurface.release(); 7422af3524beb75150d347accc925022daa53b4a789Jamie Gennis } 7432af3524beb75150d347accc925022daa53b4a789Jamie Gennis mSurface = surfaceTexture; 7442af3524beb75150d347accc925022daa53b4a789Jamie Gennis mUpdateSurface = true; 7452af3524beb75150d347accc925022daa53b4a789Jamie Gennis invalidateParentIfNeeded(); 7462af3524beb75150d347accc925022daa53b4a789Jamie Gennis } 7472af3524beb75150d347accc925022daa53b4a789Jamie Gennis 7482af3524beb75150d347accc925022daa53b4a789Jamie Gennis /** 749aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Returns the {@link SurfaceTextureListener} currently associated with this 750aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * texture view. 751aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 752aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener) 753aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTextureListener 754aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 755aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public SurfaceTextureListener getSurfaceTextureListener() { 756aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mListener; 757aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 758aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 759aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 760aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Sets the {@link SurfaceTextureListener} used to listen to surface 761aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * texture events. 762aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 763aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see #getSurfaceTextureListener() 764aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTextureListener 765aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 766aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void setSurfaceTextureListener(SurfaceTextureListener listener) { 767aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mListener = listener; 768aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 769aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 770aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 771aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * This listener can be used to be notified when the surface texture 772aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * associated with this texture view is available. 773aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 774aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public static interface SurfaceTextureListener { 775aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 776aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use. 777aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 778aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param surface The surface returned by 779aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link android.view.TextureView#getSurfaceTexture()} 780451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param width The width of the surface 781451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param height The height of the surface 782aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 783451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height); 7848f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 7858f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy /** 7868f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * Invoked when the {@link SurfaceTexture}'s buffers size changed. 7878f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * 7888f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param surface The surface returned by 7898f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * {@link android.view.TextureView#getSurfaceTexture()} 7908f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param width The new width of the surface 7918f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param height The new height of the surface 7928f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy */ 7938f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height); 794451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 795451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy /** 796451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * Invoked when the specified {@link SurfaceTexture} is about to be destroyed. 797402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba * If returns true, no rendering should happen inside the surface texture after this method 798402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba * is invoked. If returns false, the client needs to call {@link SurfaceTexture#release()}. 79952b307ebc86e12e368694442eb8751e7e0de239eRomain Guy * Most applications should return true. 800451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * 801451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param surface The surface about to be destroyed 802451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy */ 803402f05530352f34d5320c2d23be43c274d97c4e2Grace Kloba public boolean onSurfaceTextureDestroyed(SurfaceTexture surface); 804cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba 805cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba /** 806cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * Invoked when the specified {@link SurfaceTexture} is updated through 807cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * {@link SurfaceTexture#updateTexImage()}. 808cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 809cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * @param surface The surface just updated 810cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba */ 811cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba public void onSurfaceTextureUpdated(SurfaceTexture surface); 812aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 8138f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 8146be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private native void nCreateNativeWindow(SurfaceTexture surface); 8156be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private native void nDestroyNativeWindow(); 8166be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 81736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native boolean nLockCanvas(long nativeWindow, Canvas canvas, Rect dirty); 81836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native void nUnlockCanvasAndPost(long nativeWindow, Canvas canvas); 819aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy} 820