TextureView.java revision 6be3d5561cbeccf0a8257a4acb155657f868e548
1aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/* 2aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Copyright (C) 2011 The Android Open Source Project 3aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 4aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Licensed under the Apache License, Version 2.0 (the "License"); 5aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * you may not use this file except in compliance with the License. 6aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * You may obtain a copy of the License at 7aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 8aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * http://www.apache.org/licenses/LICENSE-2.0 9aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 10aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Unless required by applicable law or agreed to in writing, software 11aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * distributed under the License is distributed on an "AS IS" BASIS, 12aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * See the License for the specific language governing permissions and 14aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * limitations under the License. 15aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 16aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 17aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypackage android.view; 18aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 19aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.content.Context; 2077a811610f99e21da7f88dafef60d09f345d0506Romain Guyimport android.graphics.Bitmap; 21aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.Canvas; 22aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.Paint; 236be3d5561cbeccf0a8257a4acb155657f868e548Romain Guyimport android.graphics.Rect; 24aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.graphics.SurfaceTexture; 25aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.AttributeSet; 26aa6c24c21c727a196451332448d4e3b11a80be69Romain Guyimport android.util.Log; 27aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 28aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy/** 29aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView can be used to display a content stream. Such a content 30aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * stream can for instance be a video or an OpenGL scene. The content stream 31aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can come from the application's process as well as a remote process.</p> 32aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 33aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>TextureView can only be used in a hardware accelerated window. When 34aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * rendered in software, TextureView will draw nothing.</p> 35aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 36aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Unlike {@link SurfaceView}, TextureView does not create a separate 37aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * window but behaves as a regular View. This key difference allows a 38aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView to be moved, transformed, animated, etc. For instance, you 39aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * can make a TextureView semi-translucent by calling 40aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <code>myView.setAlpha(0.5f)</code>.</p> 41aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 42aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>Using a TextureView is simple: all you need to do is get its 43aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link SurfaceTexture}. The {@link SurfaceTexture} can then be used to 44aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * render content. The following example demonstrates how to render the 45aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * camera preview into a TextureView:</p> 46aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 47aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <pre> 48aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener { 49aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * private Camera mCamera; 50aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * private TextureView mTextureView; 51aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 52aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * protected void onCreate(Bundle savedInstanceState) { 53aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * super.onCreate(savedInstanceState); 54aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 55aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mTextureView = new TextureView(this); 56aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mTextureView.setSurfaceTextureListener(this); 57aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 58aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * setContentView(mTextureView); 59aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 60aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 61451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 62aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mCamera = Camera.open(); 63aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 64aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * try { 65aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mCamera.setPreviewTexture(surface); 66aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * mCamera.startPreview(); 67aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } catch (IOException ioe) { 68aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * // Something bad happened 69aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 70aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 71cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 728f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { 738f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * // Ignored, Camera does all the work for us 748f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * } 75cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 76451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * public void onSurfaceTextureDestroyed(SurfaceTexture surface) { 77451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * mCamera.stopPreview(); 78451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * mCamera.release(); 79451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * } 80cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 81cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * public void onSurfaceTextureUpdated(SurfaceTexture surface) { 8258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy * // Invoked every time there's a new Camera preview frame 83cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * } 84aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * } 85aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * </pre> 86aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 87aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * <p>A TextureView's SurfaceTexture can be obtained either by invoking 88aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #getSurfaceTexture()} or by using a {@link SurfaceTextureListener}. 89aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * It is important to know that a SurfaceTexture is available only after the 90aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * TextureView is attached to a window (and {@link #onAttachedToWindow()} has 91aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * been invoked.) It is therefore highly recommended you use a listener to 92aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * be notified when the SurfaceTexture becomes available.</p> 93aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 94aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceView 95aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTexture 96aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 97aa6c24c21c727a196451332448d4e3b11a80be69Romain Guypublic class TextureView extends View { 9877a811610f99e21da7f88dafef60d09f345d0506Romain Guy private static final String LOG_TAG = "TextureView"; 9977a811610f99e21da7f88dafef60d09f345d0506Romain Guy 100aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private HardwareLayer mLayer; 101aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private SurfaceTexture mSurface; 102aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private SurfaceTextureListener mListener; 103aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 104a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy private boolean mOpaque = true; 105c989d867f2580a99cde25fab0e49e445aea33f2fRomain Guy 10658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy private final Object[] mLock = new Object[0]; 10758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy private boolean mUpdateLayer; 10858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 1098f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy private SurfaceTexture.OnFrameAvailableListener mUpdateListener; 110aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 1116be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private Canvas mCanvas; 1126be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private int mSaveCount; 1136be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 1146be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private final Object[] mNativeWindowLock = new Object[0]; 1156be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy // Used from native code, do not write! 1166be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy @SuppressWarnings({"UnusedDeclaration"}) 1176be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private int mNativeWindow; 1186be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 119aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 120aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 121aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 122aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 123aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 124aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public TextureView(Context context) { 125aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super(context); 126aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 127aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 128aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 129aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 130aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 131aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 132aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 133aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param attrs The attributes of the XML tag that is inflating the view. 134aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 135aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @SuppressWarnings({"UnusedDeclaration"}) 136aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public TextureView(Context context, AttributeSet attrs) { 137aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super(context, attrs); 138aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 139aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 140aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 141aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 142aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Creates a new TextureView. 143aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 144aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param context The context to associate this view with. 145aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param attrs The attributes of the XML tag that is inflating the view. 146aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param defStyle The default style to apply to this view. If 0, no style 147aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * will be applied (beyond what is included in the theme). This may 148aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * either be an attribute resource, whose value will be retrieved 149aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * from the current theme, or an explicit style resource. 150aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 151aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @SuppressWarnings({"UnusedDeclaration"}) 152aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public TextureView(Context context, AttributeSet attrs, int defStyle) { 153aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super(context, attrs, defStyle); 154aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy init(); 155aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 156aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 157aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy private void init() { 158aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mLayerPaint = new Paint(); 159aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 160aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 161a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy /** 162a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * {@inheritDoc} 163a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy */ 164a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy @Override 165a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy public boolean isOpaque() { 166a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy return mOpaque; 167a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 168a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 169a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy /** 170a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * Indicates whether the content of this TextureView is opaque. The 171a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * content is assumed to be opaque by default. 172a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * 173a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * @param opaque True if the content of this TextureView is opaque, 174a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy * false otherwise 175a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy */ 176a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy public void setOpaque(boolean opaque) { 177a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy if (opaque != mOpaque) { 178a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy mOpaque = opaque; 17958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy updateLayer(); 180a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 181a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 182a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 183aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 184aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy protected void onAttachedToWindow() { 185aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy super.onAttachedToWindow(); 186aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 187aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (!isHardwareAccelerated()) { 18877a811610f99e21da7f88dafef60d09f345d0506Romain Guy Log.w(LOG_TAG, "A TextureView or a subclass can only be " 189aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy + "used with hardware acceleration enabled."); 190aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 191aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 192aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 193451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy @Override 194451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy protected void onDetachedFromWindow() { 195451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy super.onDetachedFromWindow(); 196451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 19780429c458506485904715180d10584092a5cd082Romain Guy if (mLayer != null) { 198451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy if (mListener != null) { 199451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mListener.onSurfaceTextureDestroyed(mSurface); 200451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 201451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 2026be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy synchronized (mNativeWindowLock) { 2036be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy nDestroyNativeWindow(); 2046be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 2056be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 2066be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mLayer.destroy(); 207451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mSurface = null; 208451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mLayer = null; 209451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 210451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 211451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 212aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 213aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * The layer type of a TextureView is ignored since a TextureView is always 214aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * considered to act as a hardware layer. The optional paint supplied to this 215aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * method will however be taken into account when rendering the content of 216aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * this TextureView. 217aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 218aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param layerType The ype of layer to use with this view, must be one of 219aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or 220aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_HARDWARE} 221aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param paint The paint used to compose the layer. This argument is optional 222aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * and can be null. It is ignored when the layer type is 223aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link #LAYER_TYPE_NONE} 224aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 225aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 226aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void setLayerType(int layerType, Paint paint) { 227aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (paint != mLayerPaint) { 228aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mLayerPaint = paint; 229aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy invalidate(); 230aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 231aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 232aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 233aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 234aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Always returns {@link #LAYER_TYPE_HARDWARE}. 235aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 236aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 237aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public int getLayerType() { 238aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return LAYER_TYPE_HARDWARE; 239aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 240aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 241aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 242aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Calling this method has no effect. 243aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 244aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 245aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void buildLayer() { 246aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 247aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 248aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 249aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Subclasses of TextureView cannot do their own rendering 250aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * with the {@link Canvas} object. 251aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 252aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param canvas The Canvas to which the View is rendered. 253aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 254aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 255aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public final void draw(Canvas canvas) { 25658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy applyUpdate(); 257aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 258aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 259aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 260aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Subclasses of TextureView cannot do their own rendering 261aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * with the {@link Canvas} object. 262aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 263aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param canvas The Canvas to which the View is rendered. 264aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 265aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 266aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy protected final void onDraw(Canvas canvas) { 267aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 268aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 269aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 2708f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy protected void onSizeChanged(int w, int h, int oldw, int oldh) { 2718f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy super.onSizeChanged(w, h, oldw, oldh); 2728f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (mSurface != null) { 273e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy nSetDefaultBufferSize(mSurface, getWidth(), getHeight()); 274451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy if (mListener != null) { 275451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight()); 276451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy } 2778f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 2788f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 2798f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 2808f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @Override 281aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy HardwareLayer getHardwareLayer() { 282aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (mLayer == null) { 28358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) { 28458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy return null; 28558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 28658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 287a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(mOpaque); 288e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer); 289e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy nSetDefaultBufferSize(mSurface, getWidth(), getHeight()); 2906be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy nCreateNativeWindow(mSurface); 291aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 2928f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() { 293aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy @Override 294aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void onFrameAvailable(SurfaceTexture surfaceTexture) { 295aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy // Per SurfaceTexture's documentation, the callback may be invoked 296aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy // from an arbitrary thread 29758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy synchronized (mLock) { 29858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy mUpdateLayer = true; 29958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 30058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy postInvalidateDelayed(0); 301aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 3028f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy }; 3038f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mSurface.setOnFrameAvailableListener(mUpdateListener); 304aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 305aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy if (mListener != null) { 306451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight()); 307aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 308aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 309aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 31058f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy applyUpdate(); 31158f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 312aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mLayer; 313aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 314aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 3158f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy @Override 3168f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy protected void onVisibilityChanged(View changedView, int visibility) { 3178f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy super.onVisibilityChanged(changedView, visibility); 3188f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 3198f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (mSurface != null) { 3208f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // When the view becomes invisible, stop updating it, it's a waste of CPU 3218f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // To cancel updates, the easiest thing to do is simply to remove the 3228f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy // updates listener 3238f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy if (visibility == VISIBLE) { 3248f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mSurface.setOnFrameAvailableListener(mUpdateListener); 325a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy updateLayer(); 3268f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } else { 3278f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy mSurface.setOnFrameAvailableListener(null); 3288f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 3298f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 3308f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy } 3318f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 332a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy private void updateLayer() { 33358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy mUpdateLayer = true; 33458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy invalidate(); 33558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 33658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 33758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy private void applyUpdate() { 33858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy if (mLayer == null) { 339a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy return; 340a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 341a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 34258f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy synchronized (mLock) { 34358f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy if (mUpdateLayer) { 34458f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy mUpdateLayer = false; 34558f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } else { 34658f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy return; 34758f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 34858f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy } 34958f4edb7701bf20925468fa5fd1a06a461ff085bRomain Guy 35002ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy mLayer.update(getWidth(), getHeight(), mOpaque); 351a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 352cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba if (mListener != null) { 353cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba mListener.onSurfaceTextureUpdated(mSurface); 354cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba } 355a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy } 356a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy 357aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 35877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Returns a {@link android.graphics.Bitmap} representation of the content 35977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * of the associated surface texture. If the surface texture is not available, 36077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * this method returns null.</p> 36177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 36277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888} 36377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * pixel format and its dimensions are the same as this view's.</p> 36477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 36577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 36677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 36777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 368d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs during the copy, an empty bitmap will be returned.</p> 369d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 37077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface 37177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture is not available or the width <= 0 or the height <= 0 37277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 37377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 37477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(android.graphics.Bitmap) 37577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(int, int) 37677a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 37777a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap() { 37877a811610f99e21da7f88dafef60d09f345d0506Romain Guy return getBitmap(getWidth(), getHeight()); 37977a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 38077a811610f99e21da7f88dafef60d09f345d0506Romain Guy 38177a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 38277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Returns a {@link android.graphics.Bitmap} representation of the content 38377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * of the associated surface texture. If the surface texture is not available, 38477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * this method returns null.</p> 38577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 38677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888} 38777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * pixel format.</p> 38877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 38977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 39077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 39177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 392d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs during the copy, an empty bitmap will be returned.</p> 393d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 39477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param width The width of the bitmap to create 39577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param height The height of the bitmap to create 39677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 39777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface 39877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture is not available or width is <= 0 or height is <= 0 39977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 40077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 40177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(android.graphics.Bitmap) 40277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap() 40377a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 40477a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap(int width, int height) { 40577a811610f99e21da7f88dafef60d09f345d0506Romain Guy if (isAvailable() && width > 0 && height > 0) { 40677a811610f99e21da7f88dafef60d09f345d0506Romain Guy return getBitmap(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)); 40777a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 40877a811610f99e21da7f88dafef60d09f345d0506Romain Guy return null; 40977a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 41077a811610f99e21da7f88dafef60d09f345d0506Romain Guy 41177a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 41277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p>Copies the content of this view's surface texture into the specified 41377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * bitmap. If the surface texture is not available, the copy is not executed. 41477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * The content of the surface texture will be scaled to fit exactly inside 41577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * the specified bitmap.</p> 41677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 41777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * <p><strong>Do not</strong> invoke this method from a drawing method 41877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> 41977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 420d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * <p>If an error occurs, the bitmap is left unchanged.</p> 421d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy * 42277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @param bitmap The bitmap to copy the content of the surface texture into, 42377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * cannot be null, all configurations are supported 42477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 42577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @return The bitmap specified as a parameter 42677a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 42777a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 42877a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap(int, int) 42977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #getBitmap() 43077a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 43177a811610f99e21da7f88dafef60d09f345d0506Romain Guy public Bitmap getBitmap(Bitmap bitmap) { 43277a811610f99e21da7f88dafef60d09f345d0506Romain Guy if (bitmap != null && isAvailable()) { 43302ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy mLayer.copyInto(bitmap); 43477a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 43577a811610f99e21da7f88dafef60d09f345d0506Romain Guy return bitmap; 43677a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 43777a811610f99e21da7f88dafef60d09f345d0506Romain Guy 43877a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 43977a811610f99e21da7f88dafef60d09f345d0506Romain Guy * Returns true if the {@link SurfaceTexture} associated with this 44077a811610f99e21da7f88dafef60d09f345d0506Romain Guy * TextureView is available for rendering. When this method returns 44177a811610f99e21da7f88dafef60d09f345d0506Romain Guy * true, {@link #getSurfaceTexture()} returns a valid surface texture. 44277a811610f99e21da7f88dafef60d09f345d0506Romain Guy */ 44377a811610f99e21da7f88dafef60d09f345d0506Romain Guy public boolean isAvailable() { 44477a811610f99e21da7f88dafef60d09f345d0506Romain Guy return mSurface != null; 44577a811610f99e21da7f88dafef60d09f345d0506Romain Guy } 44677a811610f99e21da7f88dafef60d09f345d0506Romain Guy 44777a811610f99e21da7f88dafef60d09f345d0506Romain Guy /** 4486be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * <p>Start editing the pixels in the surface. The returned Canvas can be used 4496be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to draw into the surface's bitmap. A null is returned if the surface has 4506be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * not been created or otherwise cannot be edited. You will usually need 4516be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to implement 4526be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * {@link SurfaceTextureListener#onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int)} 4536be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to find out when the Surface is available for use.</p> 4546be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 4556be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * <p>The content of the Surface is never preserved between unlockCanvas() 4566be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * and lockCanvas(), for this reason, every pixel within the Surface area 4576be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * must be written. The only exception to this rule is when a dirty 4586be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * rectangle is specified, in which case, non-dirty pixels will be 4596be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * preserved.</p> 4606be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 4616be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @return A Canvas used to draw into the surface. 4626be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 4636be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas(android.graphics.Rect) 4646be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #unlockCanvasAndPost(android.graphics.Canvas) 4656be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy */ 4666be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy public Canvas lockCanvas() { 4676be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy return lockCanvas(null); 4686be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 4696be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 4706be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy /** 4716be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * Just like {@link #lockCanvas()} but allows specification of a dirty 4726be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * rectangle. Every pixel within that rectangle must be written; however 4736be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * pixels outside the dirty rectangle will be preserved by the next call 4746be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * to lockCanvas(). 4756be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 4766be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @param dirty Area of the surface that will be modified. 4776be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 4786be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @return A Canvas used to draw into the surface. 4796be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 4806be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas() 4816be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #unlockCanvasAndPost(android.graphics.Canvas) 4826be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy */ 4836be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy public Canvas lockCanvas(Rect dirty) { 4846be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy if (!isAvailable()) return null; 4856be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 4866be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy if (mCanvas == null) { 4876be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mCanvas = new Canvas(); 4886be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 4896be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 4906be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy synchronized (mNativeWindowLock) { 4916be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy nLockCanvas(mNativeWindow, mCanvas, dirty); 4926be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 4936be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mSaveCount = mCanvas.save(); 4946be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 4956be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy return mCanvas; 4966be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 4976be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 4986be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy /** 4996be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * Finish editing pixels in the surface. After this call, the surface's 5006be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * current pixels will be shown on the screen, but its content is lost, 5016be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * in particular there is no guarantee that the content of the Surface 5026be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * will remain unchanged when lockCanvas() is called again. 5036be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 5046be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @param canvas The Canvas previously returned by lockCanvas() 5056be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * 5066be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas() 5076be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy * @see #lockCanvas(android.graphics.Rect) 5086be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy */ 5096be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy public void unlockCanvasAndPost(Canvas canvas) { 5106be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy if (mCanvas != null && canvas == mCanvas) { 5116be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy canvas.restoreToCount(mSaveCount); 5126be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy mSaveCount = 0; 5136be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 5146be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy synchronized (mNativeWindowLock) { 5156be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy nUnlockCanvasAndPost(mNativeWindow, mCanvas); 5166be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 5176be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 5186be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy } 5196be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 5206be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy /** 521aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Returns the {@link SurfaceTexture} used by this view. This method 52277a811610f99e21da7f88dafef60d09f345d0506Romain Guy * may return null if the view is not attached to a window or if the surface 52377a811610f99e21da7f88dafef60d09f345d0506Romain Guy * texture has not been initialized yet. 52477a811610f99e21da7f88dafef60d09f345d0506Romain Guy * 52577a811610f99e21da7f88dafef60d09f345d0506Romain Guy * @see #isAvailable() 526aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 527aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public SurfaceTexture getSurfaceTexture() { 528aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mSurface; 529aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 530aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 531aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 532aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Returns the {@link SurfaceTextureListener} currently associated with this 533aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * texture view. 534aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 535aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener) 536aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTextureListener 537aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 538aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public SurfaceTextureListener getSurfaceTextureListener() { 539aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy return mListener; 540aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 541aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 542aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 543aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Sets the {@link SurfaceTextureListener} used to listen to surface 544aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * texture events. 545aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 546aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see #getSurfaceTextureListener() 547aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @see SurfaceTextureListener 548aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 549aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public void setSurfaceTextureListener(SurfaceTextureListener listener) { 550aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy mListener = listener; 551aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 552aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy 553aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 554aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * This listener can be used to be notified when the surface texture 555aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * associated with this texture view is available. 556aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 557aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy public static interface SurfaceTextureListener { 558aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy /** 559aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use. 560aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * 561aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * @param surface The surface returned by 562aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy * {@link android.view.TextureView#getSurfaceTexture()} 563451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param width The width of the surface 564451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param height The height of the surface 565aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy */ 566451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height); 5678f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 5688f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy /** 5698f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * Invoked when the {@link SurfaceTexture}'s buffers size changed. 5708f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * 5718f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param surface The surface returned by 5728f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * {@link android.view.TextureView#getSurfaceTexture()} 5738f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param width The new width of the surface 5748f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy * @param height The new height of the surface 5758f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy */ 5768f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height); 577451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy 578451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy /** 579451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * Invoked when the specified {@link SurfaceTexture} is about to be destroyed. 580451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * After this method is invoked, no rendering should happen inside the surface 581451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * texture. 582451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * 583451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy * @param surface The surface about to be destroyed 584451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy */ 585451ce44a18e4c48f8a43aa250957f76967a35d31Romain Guy public void onSurfaceTextureDestroyed(SurfaceTexture surface); 586cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba 587cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba /** 588cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * Invoked when the specified {@link SurfaceTexture} is updated through 589cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * {@link SurfaceTexture#updateTexImage()}. 590cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * 591cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba * @param surface The surface just updated 592cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba */ 593cf559377b750271472aa0a717bf3b7d34abc0b39Grace Kloba public void onSurfaceTextureUpdated(SurfaceTexture surface); 594aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy } 5958f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy 5966be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private native void nCreateNativeWindow(SurfaceTexture surface); 5976be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private native void nDestroyNativeWindow(); 5986be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 599d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy private static native void nSetDefaultBufferSize(SurfaceTexture surfaceTexture, 600d6b2a00dd43257d1498b09175bff63663f6cb861Romain Guy int width, int height); 6016be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy 6026be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private static native void nLockCanvas(int nativeWindow, Canvas canvas, Rect dirty); 6036be3d5561cbeccf0a8257a4acb155657f868e548Romain Guy private static native void nUnlockCanvasAndPost(int nativeWindow, Canvas canvas); 604aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy} 605