Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
sergeyv | 342a7e6 | 2016-03-24 16:06:46 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 23 | import android.graphics.Canvas; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 24 | import android.graphics.Matrix; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 25 | import android.graphics.Paint; |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 26 | import android.graphics.RecordingCanvas; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 27 | import android.graphics.Rect; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 28 | import android.graphics.SurfaceTexture; |
Chris Craik | 2e931ea | 2015-08-19 16:13:04 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 30 | import android.util.AttributeSet; |
| 31 | import android.util.Log; |
| 32 | |
| 33 | /** |
| 34 | * <p>A TextureView can be used to display a content stream. Such a content |
| 35 | * stream can for instance be a video or an OpenGL scene. The content stream |
| 36 | * can come from the application's process as well as a remote process.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 37 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 38 | * <p>TextureView can only be used in a hardware accelerated window. When |
| 39 | * rendered in software, TextureView will draw nothing.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 40 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 41 | * <p>Unlike {@link SurfaceView}, TextureView does not create a separate |
| 42 | * window but behaves as a regular View. This key difference allows a |
| 43 | * TextureView to be moved, transformed, animated, etc. For instance, you |
| 44 | * can make a TextureView semi-translucent by calling |
| 45 | * <code>myView.setAlpha(0.5f)</code>.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 46 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 47 | * <p>Using a TextureView is simple: all you need to do is get its |
| 48 | * {@link SurfaceTexture}. The {@link SurfaceTexture} can then be used to |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 49 | * render content. The following example demonstrates how to render the |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 50 | * camera preview into a TextureView:</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 51 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 52 | * <pre> |
| 53 | * public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener { |
| 54 | * private Camera mCamera; |
| 55 | * private TextureView mTextureView; |
| 56 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 57 | * protected void onCreate(Bundle savedInstanceState) { |
| 58 | * super.onCreate(savedInstanceState); |
| 59 | * |
| 60 | * mTextureView = new TextureView(this); |
| 61 | * mTextureView.setSurfaceTextureListener(this); |
| 62 | * |
| 63 | * setContentView(mTextureView); |
| 64 | * } |
| 65 | * |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 66 | * public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 67 | * mCamera = Camera.open(); |
| 68 | * |
| 69 | * try { |
| 70 | * mCamera.setPreviewTexture(surface); |
| 71 | * mCamera.startPreview(); |
| 72 | * } catch (IOException ioe) { |
| 73 | * // Something bad happened |
| 74 | * } |
| 75 | * } |
Grace Kloba | cf55937 | 2011-06-22 23:05:40 -0700 | [diff] [blame] | 76 | * |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 77 | * public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { |
| 78 | * // Ignored, Camera does all the work for us |
| 79 | * } |
Grace Kloba | cf55937 | 2011-06-22 23:05:40 -0700 | [diff] [blame] | 80 | * |
Grace Kloba | 402f055 | 2011-08-09 18:47:17 -0700 | [diff] [blame] | 81 | * public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 82 | * mCamera.stopPreview(); |
| 83 | * mCamera.release(); |
Grace Kloba | 402f055 | 2011-08-09 18:47:17 -0700 | [diff] [blame] | 84 | * return true; |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 85 | * } |
Grace Kloba | cf55937 | 2011-06-22 23:05:40 -0700 | [diff] [blame] | 86 | * |
| 87 | * public void onSurfaceTextureUpdated(SurfaceTexture surface) { |
Romain Guy | 58f4edb | 2011-06-24 14:51:38 -0700 | [diff] [blame] | 88 | * // Invoked every time there's a new Camera preview frame |
Grace Kloba | cf55937 | 2011-06-22 23:05:40 -0700 | [diff] [blame] | 89 | * } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 90 | * } |
| 91 | * </pre> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 92 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 93 | * <p>A TextureView's SurfaceTexture can be obtained either by invoking |
| 94 | * {@link #getSurfaceTexture()} or by using a {@link SurfaceTextureListener}. |
| 95 | * It is important to know that a SurfaceTexture is available only after the |
| 96 | * TextureView is attached to a window (and {@link #onAttachedToWindow()} has |
| 97 | * been invoked.) It is therefore highly recommended you use a listener to |
| 98 | * be notified when the SurfaceTexture becomes available.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 99 | * |
Romain Guy | 462785f | 2011-09-27 17:42:10 -0700 | [diff] [blame] | 100 | * <p>It is important to note that only one producer can use the TextureView. |
| 101 | * For instance, if you use a TextureView to display the camera preview, you |
| 102 | * cannot use {@link #lockCanvas()} to draw onto the TextureView at the same |
| 103 | * time.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 104 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 105 | * @see SurfaceView |
| 106 | * @see SurfaceTexture |
| 107 | */ |
| 108 | public class TextureView extends View { |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 109 | private static final String LOG_TAG = "TextureView"; |
| 110 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 111 | @UnsupportedAppUsage |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 112 | private TextureLayer mLayer; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 113 | @UnsupportedAppUsage |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 114 | private SurfaceTexture mSurface; |
| 115 | private SurfaceTextureListener mListener; |
Romain Guy | 67603c6 | 2013-06-27 10:58:10 -0700 | [diff] [blame] | 116 | private boolean mHadSurface; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 117 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 118 | @UnsupportedAppUsage |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 119 | private boolean mOpaque = true; |
Romain Guy | c989d86 | 2011-06-22 14:53:39 -0700 | [diff] [blame] | 120 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 121 | private final Matrix mMatrix = new Matrix(); |
| 122 | private boolean mMatrixChanged; |
| 123 | |
Romain Guy | 58f4edb | 2011-06-24 14:51:38 -0700 | [diff] [blame] | 124 | private final Object[] mLock = new Object[0]; |
| 125 | private boolean mUpdateLayer; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 126 | @UnsupportedAppUsage |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 127 | private boolean mUpdateSurface; |
Romain Guy | 58f4edb | 2011-06-24 14:51:38 -0700 | [diff] [blame] | 128 | |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 129 | private Canvas mCanvas; |
| 130 | private int mSaveCount; |
| 131 | |
| 132 | private final Object[] mNativeWindowLock = new Object[0]; |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 133 | // Set by native code, do not write! |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 134 | @UnsupportedAppUsage |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 135 | private long mNativeWindow; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 136 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 137 | /** |
| 138 | * Creates a new TextureView. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 139 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 140 | * @param context The context to associate this view with. |
| 141 | */ |
| 142 | public TextureView(Context context) { |
| 143 | super(context); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Creates a new TextureView. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 148 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 149 | * @param context The context to associate this view with. |
| 150 | * @param attrs The attributes of the XML tag that is inflating the view. |
| 151 | */ |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 152 | public TextureView(Context context, AttributeSet attrs) { |
| 153 | super(context, attrs); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Creates a new TextureView. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 158 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 159 | * @param context The context to associate this view with. |
| 160 | * @param attrs The attributes of the XML tag that is inflating the view. |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 161 | * @param defStyleAttr An attribute in the current theme that contains a |
| 162 | * reference to a style resource that supplies default values for |
| 163 | * the view. Can be 0 to not look for defaults. |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 164 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 165 | public TextureView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 166 | super(context, attrs, defStyleAttr); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Creates a new TextureView. |
| 171 | * |
| 172 | * @param context The context to associate this view with. |
| 173 | * @param attrs The attributes of the XML tag that is inflating the view. |
| 174 | * @param defStyleAttr An attribute in the current theme that contains a |
| 175 | * reference to a style resource that supplies default values for |
| 176 | * the view. Can be 0 to not look for defaults. |
| 177 | * @param defStyleRes A resource identifier of a style resource that |
| 178 | * supplies default values for the view, used only if |
| 179 | * defStyleAttr is 0 or can not be found in the theme. Can be 0 |
| 180 | * to not look for defaults. |
| 181 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 182 | public TextureView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 183 | super(context, attrs, defStyleAttr, defStyleRes); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 186 | /** |
| 187 | * {@inheritDoc} |
| 188 | */ |
| 189 | @Override |
| 190 | public boolean isOpaque() { |
| 191 | return mOpaque; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Indicates whether the content of this TextureView is opaque. The |
| 196 | * content is assumed to be opaque by default. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 197 | * |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 198 | * @param opaque True if the content of this TextureView is opaque, |
| 199 | * false otherwise |
| 200 | */ |
| 201 | public void setOpaque(boolean opaque) { |
| 202 | if (opaque != mOpaque) { |
| 203 | mOpaque = opaque; |
Romain Guy | a8a2f97 | 2012-04-12 16:33:44 -0700 | [diff] [blame] | 204 | if (mLayer != null) { |
Romain Guy | 88801b2 | 2012-10-05 14:58:33 -0700 | [diff] [blame] | 205 | updateLayerAndInvalidate(); |
Romain Guy | a8a2f97 | 2012-04-12 16:33:44 -0700 | [diff] [blame] | 206 | } |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 210 | @Override |
| 211 | protected void onAttachedToWindow() { |
| 212 | super.onAttachedToWindow(); |
| 213 | |
| 214 | if (!isHardwareAccelerated()) { |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 215 | Log.w(LOG_TAG, "A TextureView or a subclass can only be " |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 216 | + "used with hardware acceleration enabled."); |
| 217 | } |
Romain Guy | 67603c6 | 2013-06-27 10:58:10 -0700 | [diff] [blame] | 218 | |
| 219 | if (mHadSurface) { |
| 220 | invalidate(true); |
| 221 | mHadSurface = false; |
| 222 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 223 | } |
| 224 | |
John Reck | b14dfe2 | 2014-03-06 22:06:20 +0000 | [diff] [blame] | 225 | /** @hide */ |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 226 | @Override |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 227 | @UnsupportedAppUsage |
John Reck | b14dfe2 | 2014-03-06 22:06:20 +0000 | [diff] [blame] | 228 | protected void onDetachedFromWindowInternal() { |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 229 | destroyHardwareLayer(); |
| 230 | releaseSurfaceTexture(); |
John Reck | b14dfe2 | 2014-03-06 22:06:20 +0000 | [diff] [blame] | 231 | super.onDetachedFromWindowInternal(); |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 232 | } |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 233 | |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 234 | /** |
| 235 | * @hide |
| 236 | */ |
| 237 | @Override |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 238 | @UnsupportedAppUsage |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 239 | protected void destroyHardwareResources() { |
sergeyv | 17dd01b | 2016-08-30 14:03:32 -0700 | [diff] [blame] | 240 | super.destroyHardwareResources(); |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 241 | destroyHardwareLayer(); |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 244 | @UnsupportedAppUsage |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 245 | private void destroyHardwareLayer() { |
Romain Guy | 80429c4 | 2011-06-24 17:20:32 -0700 | [diff] [blame] | 246 | if (mLayer != null) { |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 247 | mLayer.detachSurfaceTexture(); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 248 | mLayer.destroy(); |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 249 | mLayer = null; |
Doris Liu | 6b47199 | 2015-04-30 17:00:35 -0700 | [diff] [blame] | 250 | mMatrixChanged = true; |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 254 | private void releaseSurfaceTexture() { |
John Reck | 3c2587f | 2016-08-04 07:55:38 -0700 | [diff] [blame] | 255 | if (mSurface != null) { |
| 256 | boolean shouldRelease = true; |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 257 | |
John Reck | 3c2587f | 2016-08-04 07:55:38 -0700 | [diff] [blame] | 258 | if (mListener != null) { |
| 259 | shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface); |
| 260 | } |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 261 | |
John Reck | 3c2587f | 2016-08-04 07:55:38 -0700 | [diff] [blame] | 262 | synchronized (mNativeWindowLock) { |
| 263 | nDestroyNativeWindow(); |
| 264 | } |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 265 | |
John Reck | 3c2587f | 2016-08-04 07:55:38 -0700 | [diff] [blame] | 266 | if (shouldRelease) { |
| 267 | mSurface.release(); |
| 268 | } |
| 269 | mSurface = null; |
| 270 | mHadSurface = true; |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 271 | } |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 274 | /** |
| 275 | * The layer type of a TextureView is ignored since a TextureView is always |
| 276 | * considered to act as a hardware layer. The optional paint supplied to this |
| 277 | * method will however be taken into account when rendering the content of |
| 278 | * this TextureView. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 279 | * |
sergeyv | 342a7e6 | 2016-03-24 16:06:46 -0700 | [diff] [blame] | 280 | * @param layerType The type of layer to use with this view, must be one of |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 281 | * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or |
| 282 | * {@link #LAYER_TYPE_HARDWARE} |
| 283 | * @param paint The paint used to compose the layer. This argument is optional |
| 284 | * and can be null. It is ignored when the layer type is |
| 285 | * {@link #LAYER_TYPE_NONE} |
| 286 | */ |
| 287 | @Override |
sergeyv | 342a7e6 | 2016-03-24 16:06:46 -0700 | [diff] [blame] | 288 | public void setLayerType(int layerType, @Nullable Paint paint) { |
| 289 | setLayerPaint(paint); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 290 | } |
| 291 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 292 | @Override |
sergeyv | 342a7e6 | 2016-03-24 16:06:46 -0700 | [diff] [blame] | 293 | public void setLayerPaint(@Nullable Paint paint) { |
| 294 | if (paint != mLayerPaint) { |
| 295 | mLayerPaint = paint; |
| 296 | invalidate(); |
| 297 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 300 | /** |
| 301 | * Always returns {@link #LAYER_TYPE_HARDWARE}. |
| 302 | */ |
| 303 | @Override |
| 304 | public int getLayerType() { |
| 305 | return LAYER_TYPE_HARDWARE; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Calling this method has no effect. |
| 310 | */ |
| 311 | @Override |
| 312 | public void buildLayer() { |
| 313 | } |
| 314 | |
Chris Craik | 2e931ea | 2015-08-19 16:13:04 -0700 | [diff] [blame] | 315 | @Override |
| 316 | public void setForeground(Drawable foreground) { |
Chris Craik | b724480 | 2016-01-04 15:47:19 -0800 | [diff] [blame] | 317 | if (foreground != null && !sTextureViewIgnoresDrawableSetters) { |
Chris Craik | 2e931ea | 2015-08-19 16:13:04 -0700 | [diff] [blame] | 318 | throw new UnsupportedOperationException( |
| 319 | "TextureView doesn't support displaying a foreground drawable"); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void setBackgroundDrawable(Drawable background) { |
Chris Craik | b724480 | 2016-01-04 15:47:19 -0800 | [diff] [blame] | 325 | if (background != null && !sTextureViewIgnoresDrawableSetters) { |
Chris Craik | 2e931ea | 2015-08-19 16:13:04 -0700 | [diff] [blame] | 326 | throw new UnsupportedOperationException( |
| 327 | "TextureView doesn't support displaying a background drawable"); |
| 328 | } |
| 329 | } |
| 330 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 331 | /** |
| 332 | * Subclasses of TextureView cannot do their own rendering |
| 333 | * with the {@link Canvas} object. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 334 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 335 | * @param canvas The Canvas to which the View is rendered. |
| 336 | */ |
| 337 | @Override |
| 338 | public final void draw(Canvas canvas) { |
Chris Craik | 3aadd60 | 2015-08-20 12:41:40 -0700 | [diff] [blame] | 339 | // NOTE: Maintain this carefully (see View#draw) |
Romain Guy | 52b307e | 2012-10-07 17:55:17 -0700 | [diff] [blame] | 340 | mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN; |
| 341 | |
Chris Craik | 3aadd60 | 2015-08-20 12:41:40 -0700 | [diff] [blame] | 342 | /* Simplify drawing to guarantee the layer is the only thing drawn - so e.g. no background, |
| 343 | scrolling, or fading edges. This guarantees all drawing is in the layer, so drawing |
| 344 | properties (alpha, layer paint) affect all of the content of a TextureView. */ |
| 345 | |
| 346 | if (canvas.isHardwareAccelerated()) { |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 347 | RecordingCanvas recordingCanvas = (RecordingCanvas) canvas; |
Chris Craik | 3aadd60 | 2015-08-20 12:41:40 -0700 | [diff] [blame] | 348 | |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 349 | TextureLayer layer = getTextureLayer(); |
Chris Craik | 3aadd60 | 2015-08-20 12:41:40 -0700 | [diff] [blame] | 350 | if (layer != null) { |
| 351 | applyUpdate(); |
| 352 | applyTransformMatrix(); |
| 353 | |
| 354 | mLayer.setLayerPaint(mLayerPaint); // ensure layer paint is up to date |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 355 | recordingCanvas.drawTextureLayer(layer); |
Chris Craik | 3aadd60 | 2015-08-20 12:41:40 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Subclasses of TextureView cannot do their own rendering |
| 362 | * with the {@link Canvas} object. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 363 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 364 | * @param canvas The Canvas to which the View is rendered. |
| 365 | */ |
| 366 | @Override |
| 367 | protected final void onDraw(Canvas canvas) { |
| 368 | } |
| 369 | |
| 370 | @Override |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 371 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 372 | super.onSizeChanged(w, h, oldw, oldh); |
| 373 | if (mSurface != null) { |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 374 | mSurface.setDefaultBufferSize(getWidth(), getHeight()); |
Romain Guy | 88801b2 | 2012-10-05 14:58:33 -0700 | [diff] [blame] | 375 | updateLayer(); |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 376 | if (mListener != null) { |
| 377 | mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight()); |
| 378 | } |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 382 | TextureLayer getTextureLayer() { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 383 | if (mLayer == null) { |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 384 | if (mAttachInfo == null || mAttachInfo.mThreadedRenderer == null) { |
Romain Guy | 58f4edb | 2011-06-24 14:51:38 -0700 | [diff] [blame] | 385 | return null; |
| 386 | } |
| 387 | |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 388 | mLayer = mAttachInfo.mThreadedRenderer.createTextureLayer(); |
John Reck | 7e23718 | 2016-08-11 10:43:14 -0700 | [diff] [blame] | 389 | boolean createNewSurface = (mSurface == null); |
| 390 | if (createNewSurface) { |
Jamie Gennis | 8a34d68 | 2012-04-17 16:01:34 -0700 | [diff] [blame] | 391 | // Create a new SurfaceTexture for the layer. |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 392 | mSurface = new SurfaceTexture(false); |
sergeyv | 1c16c37 | 2016-08-02 14:46:12 -0700 | [diff] [blame] | 393 | nCreateNativeWindow(mSurface); |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 394 | } |
John Reck | 03df083 | 2016-08-16 09:44:23 -0700 | [diff] [blame] | 395 | mLayer.setSurfaceTexture(mSurface); |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 396 | mSurface.setDefaultBufferSize(getWidth(), getHeight()); |
Jeff Brown | c7282e5 | 2014-05-06 16:00:38 -0700 | [diff] [blame] | 397 | mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 398 | |
John Reck | 7e23718 | 2016-08-11 10:43:14 -0700 | [diff] [blame] | 399 | if (mListener != null && createNewSurface) { |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 400 | mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight()); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 401 | } |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 402 | mLayer.setLayerPaint(mLayerPaint); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 405 | if (mUpdateSurface) { |
| 406 | // Someone has requested that we use a specific SurfaceTexture, so |
| 407 | // tell mLayer about it and set the SurfaceTexture to use the |
| 408 | // current view size. |
| 409 | mUpdateSurface = false; |
Romain Guy | 51f7c6b | 2012-05-21 16:32:59 -0700 | [diff] [blame] | 410 | |
| 411 | // Since we are updating the layer, force an update to ensure its |
| 412 | // parameters are correct (width, height, transform, etc.) |
Romain Guy | 88801b2 | 2012-10-05 14:58:33 -0700 | [diff] [blame] | 413 | updateLayer(); |
Romain Guy | 51f7c6b | 2012-05-21 16:32:59 -0700 | [diff] [blame] | 414 | mMatrixChanged = true; |
| 415 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 416 | mLayer.setSurfaceTexture(mSurface); |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 417 | mSurface.setDefaultBufferSize(getWidth(), getHeight()); |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 420 | return mLayer; |
| 421 | } |
| 422 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 423 | @Override |
| 424 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 425 | super.onVisibilityChanged(changedView, visibility); |
| 426 | |
| 427 | if (mSurface != null) { |
| 428 | // When the view becomes invisible, stop updating it, it's a waste of CPU |
| 429 | // To cancel updates, the easiest thing to do is simply to remove the |
| 430 | // updates listener |
| 431 | if (visibility == VISIBLE) { |
John Reck | 2dedafb | 2014-05-29 16:15:41 -0700 | [diff] [blame] | 432 | if (mLayer != null) { |
| 433 | mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler); |
| 434 | } |
Romain Guy | 88801b2 | 2012-10-05 14:58:33 -0700 | [diff] [blame] | 435 | updateLayerAndInvalidate(); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 436 | } else { |
| 437 | mSurface.setOnFrameAvailableListener(null); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 442 | private void updateLayer() { |
Romain Guy | 88801b2 | 2012-10-05 14:58:33 -0700 | [diff] [blame] | 443 | synchronized (mLock) { |
| 444 | mUpdateLayer = true; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | private void updateLayerAndInvalidate() { |
| 449 | synchronized (mLock) { |
| 450 | mUpdateLayer = true; |
| 451 | } |
Romain Guy | 58f4edb | 2011-06-24 14:51:38 -0700 | [diff] [blame] | 452 | invalidate(); |
| 453 | } |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 454 | |
Romain Guy | 58f4edb | 2011-06-24 14:51:38 -0700 | [diff] [blame] | 455 | private void applyUpdate() { |
| 456 | if (mLayer == null) { |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
Romain Guy | 58f4edb | 2011-06-24 14:51:38 -0700 | [diff] [blame] | 460 | synchronized (mLock) { |
| 461 | if (mUpdateLayer) { |
| 462 | mUpdateLayer = false; |
| 463 | } else { |
| 464 | return; |
| 465 | } |
| 466 | } |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 467 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 468 | mLayer.prepare(getWidth(), getHeight(), mOpaque); |
| 469 | mLayer.updateSurfaceTexture(); |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 470 | |
Grace Kloba | cf55937 | 2011-06-22 23:05:40 -0700 | [diff] [blame] | 471 | if (mListener != null) { |
| 472 | mListener.onSurfaceTextureUpdated(mSurface); |
| 473 | } |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 476 | /** |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 477 | * <p>Sets the transform to associate with this texture view. |
| 478 | * The specified transform applies to the underlying surface |
| 479 | * texture and does not affect the size or position of the view |
| 480 | * itself, only of its content.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 481 | * |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 482 | * <p>Some transforms might prevent the content from drawing |
| 483 | * all the pixels contained within this view's bounds. In such |
| 484 | * situations, make sure this texture view is not marked opaque.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 485 | * |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 486 | * @param transform The transform to apply to the content of |
| 487 | * this view. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 488 | * |
| 489 | * @see #getTransform(android.graphics.Matrix) |
| 490 | * @see #isOpaque() |
| 491 | * @see #setOpaque(boolean) |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 492 | */ |
| 493 | public void setTransform(Matrix transform) { |
| 494 | mMatrix.set(transform); |
| 495 | mMatrixChanged = true; |
Alexandre Elias | c01391f | 2011-08-19 16:03:16 -0700 | [diff] [blame] | 496 | invalidateParentIfNeeded(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Returns the transform associated with this texture view. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 501 | * |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 502 | * @param transform The {@link Matrix} in which to copy the current |
| 503 | * transform. Can be null. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 504 | * |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 505 | * @return The specified matrix if not null or a new {@link Matrix} |
| 506 | * instance otherwise. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 507 | * |
| 508 | * @see #setTransform(android.graphics.Matrix) |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 509 | */ |
| 510 | public Matrix getTransform(Matrix transform) { |
| 511 | if (transform == null) { |
| 512 | transform = new Matrix(); |
| 513 | } |
| 514 | |
| 515 | transform.set(mMatrix); |
| 516 | |
| 517 | return transform; |
| 518 | } |
| 519 | |
Alexandre Elias | c01391f | 2011-08-19 16:03:16 -0700 | [diff] [blame] | 520 | private void applyTransformMatrix() { |
Romain Guy | 51f7c6b | 2012-05-21 16:32:59 -0700 | [diff] [blame] | 521 | if (mMatrixChanged && mLayer != null) { |
Alexandre Elias | c01391f | 2011-08-19 16:03:16 -0700 | [diff] [blame] | 522 | mLayer.setTransform(mMatrix); |
| 523 | mMatrixChanged = false; |
| 524 | } |
| 525 | } |
| 526 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 527 | /** |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 528 | * <p>Returns a {@link android.graphics.Bitmap} representation of the content |
| 529 | * of the associated surface texture. If the surface texture is not available, |
| 530 | * this method returns null.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 531 | * |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 532 | * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888} |
| 533 | * pixel format and its dimensions are the same as this view's.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 534 | * |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 535 | * <p><strong>Do not</strong> invoke this method from a drawing method |
| 536 | * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 537 | * |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 538 | * <p>If an error occurs during the copy, an empty bitmap will be returned.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 539 | * |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 540 | * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface |
| 541 | * texture is not available or the width <= 0 or the height <= 0 |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 542 | * |
| 543 | * @see #isAvailable() |
| 544 | * @see #getBitmap(android.graphics.Bitmap) |
| 545 | * @see #getBitmap(int, int) |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 546 | */ |
| 547 | public Bitmap getBitmap() { |
| 548 | return getBitmap(getWidth(), getHeight()); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * <p>Returns a {@link android.graphics.Bitmap} representation of the content |
| 553 | * of the associated surface texture. If the surface texture is not available, |
| 554 | * this method returns null.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 555 | * |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 556 | * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888} |
| 557 | * pixel format.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 558 | * |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 559 | * <p><strong>Do not</strong> invoke this method from a drawing method |
| 560 | * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 561 | * |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 562 | * <p>If an error occurs during the copy, an empty bitmap will be returned.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 563 | * |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 564 | * @param width The width of the bitmap to create |
| 565 | * @param height The height of the bitmap to create |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 566 | * |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 567 | * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface |
| 568 | * texture is not available or width is <= 0 or height is <= 0 |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 569 | * |
| 570 | * @see #isAvailable() |
| 571 | * @see #getBitmap(android.graphics.Bitmap) |
| 572 | * @see #getBitmap() |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 573 | */ |
| 574 | public Bitmap getBitmap(int width, int height) { |
| 575 | if (isAvailable() && width > 0 && height > 0) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 576 | return getBitmap(Bitmap.createBitmap(getResources().getDisplayMetrics(), |
| 577 | width, height, Bitmap.Config.ARGB_8888)); |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 578 | } |
| 579 | return null; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * <p>Copies the content of this view's surface texture into the specified |
| 584 | * bitmap. If the surface texture is not available, the copy is not executed. |
| 585 | * The content of the surface texture will be scaled to fit exactly inside |
| 586 | * the specified bitmap.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 587 | * |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 588 | * <p><strong>Do not</strong> invoke this method from a drawing method |
| 589 | * ({@link #onDraw(android.graphics.Canvas)} for instance).</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 590 | * |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 591 | * <p>If an error occurs, the bitmap is left unchanged.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 592 | * |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 593 | * @param bitmap The bitmap to copy the content of the surface texture into, |
| 594 | * cannot be null, all configurations are supported |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 595 | * |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 596 | * @return The bitmap specified as a parameter |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 597 | * |
| 598 | * @see #isAvailable() |
| 599 | * @see #getBitmap(int, int) |
| 600 | * @see #getBitmap() |
| 601 | * |
Romain Guy | 589b0bb | 2011-10-10 13:57:47 -0700 | [diff] [blame] | 602 | * @throws IllegalStateException if the hardware rendering context cannot be |
| 603 | * acquired to capture the bitmap |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 604 | */ |
| 605 | public Bitmap getBitmap(Bitmap bitmap) { |
| 606 | if (bitmap != null && isAvailable()) { |
Romain Guy | 589b0bb | 2011-10-10 13:57:47 -0700 | [diff] [blame] | 607 | applyUpdate(); |
| 608 | applyTransformMatrix(); |
| 609 | |
Romain Guy | 78245f7 | 2012-05-11 10:01:42 -0700 | [diff] [blame] | 610 | // This case can happen if the app invokes setSurfaceTexture() before |
| 611 | // we are able to create the hardware layer. We can safely initialize |
| 612 | // the layer here thanks to the validate() call at the beginning of |
| 613 | // this method |
| 614 | if (mLayer == null && mUpdateSurface) { |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 615 | getTextureLayer(); |
Romain Guy | 78245f7 | 2012-05-11 10:01:42 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | if (mLayer != null) { |
| 619 | mLayer.copyInto(bitmap); |
| 620 | } |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 621 | } |
| 622 | return bitmap; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Returns true if the {@link SurfaceTexture} associated with this |
| 627 | * TextureView is available for rendering. When this method returns |
| 628 | * true, {@link #getSurfaceTexture()} returns a valid surface texture. |
| 629 | */ |
| 630 | public boolean isAvailable() { |
| 631 | return mSurface != null; |
| 632 | } |
| 633 | |
| 634 | /** |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 635 | * <p>Start editing the pixels in the surface. The returned Canvas can be used |
| 636 | * to draw into the surface's bitmap. A null is returned if the surface has |
| 637 | * not been created or otherwise cannot be edited. You will usually need |
| 638 | * to implement |
| 639 | * {@link SurfaceTextureListener#onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int)} |
| 640 | * to find out when the Surface is available for use.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 641 | * |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 642 | * <p>The content of the Surface is never preserved between unlockCanvas() |
| 643 | * and lockCanvas(), for this reason, every pixel within the Surface area |
| 644 | * must be written. The only exception to this rule is when a dirty |
| 645 | * rectangle is specified, in which case, non-dirty pixels will be |
| 646 | * preserved.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 647 | * |
Romain Guy | 462785f | 2011-09-27 17:42:10 -0700 | [diff] [blame] | 648 | * <p>This method can only be used if the underlying surface is not already |
| 649 | * owned by another producer. For instance, if the TextureView is being used |
| 650 | * to render the camera's preview you cannot invoke this method.</p> |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 651 | * |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 652 | * @return A Canvas used to draw into the surface. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 653 | * |
| 654 | * @see #lockCanvas(android.graphics.Rect) |
| 655 | * @see #unlockCanvasAndPost(android.graphics.Canvas) |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 656 | */ |
| 657 | public Canvas lockCanvas() { |
| 658 | return lockCanvas(null); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Just like {@link #lockCanvas()} but allows specification of a dirty |
| 663 | * rectangle. Every pixel within that rectangle must be written; however |
| 664 | * pixels outside the dirty rectangle will be preserved by the next call |
| 665 | * to lockCanvas(). |
Romain Guy | 53bacf5 | 2013-04-30 11:30:10 -0700 | [diff] [blame] | 666 | * |
| 667 | * This method can return null if the underlying surface texture is not |
| 668 | * available (see {@link #isAvailable()} or if the surface texture is |
| 669 | * already connected to an image producer (for instance: the camera, |
| 670 | * OpenGL, a media player, etc.) |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 671 | * |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 672 | * @param dirty Area of the surface that will be modified. |
| 673 | |
| 674 | * @return A Canvas used to draw into the surface. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 675 | * |
| 676 | * @see #lockCanvas() |
Romain Guy | 53bacf5 | 2013-04-30 11:30:10 -0700 | [diff] [blame] | 677 | * @see #unlockCanvasAndPost(android.graphics.Canvas) |
| 678 | * @see #isAvailable() |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 679 | */ |
| 680 | public Canvas lockCanvas(Rect dirty) { |
| 681 | if (!isAvailable()) return null; |
| 682 | |
| 683 | if (mCanvas == null) { |
| 684 | mCanvas = new Canvas(); |
| 685 | } |
| 686 | |
| 687 | synchronized (mNativeWindowLock) { |
Romain Guy | 53bacf5 | 2013-04-30 11:30:10 -0700 | [diff] [blame] | 688 | if (!nLockCanvas(mNativeWindow, mCanvas, dirty)) { |
| 689 | return null; |
| 690 | } |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 691 | } |
| 692 | mSaveCount = mCanvas.save(); |
| 693 | |
| 694 | return mCanvas; |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Finish editing pixels in the surface. After this call, the surface's |
| 699 | * current pixels will be shown on the screen, but its content is lost, |
| 700 | * in particular there is no guarantee that the content of the Surface |
| 701 | * will remain unchanged when lockCanvas() is called again. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 702 | * |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 703 | * @param canvas The Canvas previously returned by lockCanvas() |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 704 | * |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 705 | * @see #lockCanvas() |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 706 | * @see #lockCanvas(android.graphics.Rect) |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 707 | */ |
| 708 | public void unlockCanvasAndPost(Canvas canvas) { |
| 709 | if (mCanvas != null && canvas == mCanvas) { |
| 710 | canvas.restoreToCount(mSaveCount); |
| 711 | mSaveCount = 0; |
| 712 | |
| 713 | synchronized (mNativeWindowLock) { |
| 714 | nUnlockCanvasAndPost(mNativeWindow, mCanvas); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /** |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 720 | * Returns the {@link SurfaceTexture} used by this view. This method |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 721 | * may return null if the view is not attached to a window or if the surface |
| 722 | * texture has not been initialized yet. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 723 | * |
| 724 | * @see #isAvailable() |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 725 | */ |
| 726 | public SurfaceTexture getSurfaceTexture() { |
| 727 | return mSurface; |
| 728 | } |
| 729 | |
| 730 | /** |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 731 | * Set the {@link SurfaceTexture} for this view to use. If a {@link |
| 732 | * SurfaceTexture} is already being used by this view, it is immediately |
Mark Lu | e8691d1 | 2016-08-30 17:38:42 -0700 | [diff] [blame] | 733 | * released and not usable any more. The {@link |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 734 | * SurfaceTextureListener#onSurfaceTextureDestroyed} callback is <b>not</b> |
Jamie Gennis | 8a34d68 | 2012-04-17 16:01:34 -0700 | [diff] [blame] | 735 | * called for the previous {@link SurfaceTexture}. Similarly, the {@link |
| 736 | * SurfaceTextureListener#onSurfaceTextureAvailable} callback is <b>not</b> |
| 737 | * called for the {@link SurfaceTexture} passed to setSurfaceTexture. |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 738 | * |
| 739 | * The {@link SurfaceTexture} object must be detached from all OpenGL ES |
| 740 | * contexts prior to calling this method. |
| 741 | * |
| 742 | * @param surfaceTexture The {@link SurfaceTexture} that the view should use. |
| 743 | * @see SurfaceTexture#detachFromGLContext() |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 744 | */ |
| 745 | public void setSurfaceTexture(SurfaceTexture surfaceTexture) { |
| 746 | if (surfaceTexture == null) { |
| 747 | throw new NullPointerException("surfaceTexture must not be null"); |
| 748 | } |
John Reck | 6d8371e | 2015-05-14 15:47:03 -0700 | [diff] [blame] | 749 | if (surfaceTexture == mSurface) { |
| 750 | throw new IllegalArgumentException("Trying to setSurfaceTexture to " + |
| 751 | "the same SurfaceTexture that's already set."); |
| 752 | } |
| 753 | if (surfaceTexture.isReleased()) { |
| 754 | throw new IllegalArgumentException("Cannot setSurfaceTexture to a " + |
| 755 | "released SurfaceTexture"); |
| 756 | } |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 757 | if (mSurface != null) { |
John Reck | 7e23718 | 2016-08-11 10:43:14 -0700 | [diff] [blame] | 758 | nDestroyNativeWindow(); |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 759 | mSurface.release(); |
| 760 | } |
| 761 | mSurface = surfaceTexture; |
John Reck | 7e23718 | 2016-08-11 10:43:14 -0700 | [diff] [blame] | 762 | nCreateNativeWindow(mSurface); |
Naveen Kalla | fd69e2a | 2015-03-17 15:20:53 -0700 | [diff] [blame] | 763 | |
John Reck | 8bc511e | 2015-05-18 15:11:52 -0700 | [diff] [blame] | 764 | /* |
| 765 | * If the view is visible and we already made a layer, update the |
| 766 | * listener in the new surface to use the existing listener in the view. |
| 767 | * Otherwise this will be called when the view becomes visible or the |
| 768 | * layer is created |
| 769 | */ |
| 770 | if (((mViewFlags & VISIBILITY_MASK) == VISIBLE) && mLayer != null) { |
Naveen Kalla | fd69e2a | 2015-03-17 15:20:53 -0700 | [diff] [blame] | 771 | mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler); |
| 772 | } |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 773 | mUpdateSurface = true; |
| 774 | invalidateParentIfNeeded(); |
| 775 | } |
| 776 | |
| 777 | /** |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 778 | * Returns the {@link SurfaceTextureListener} currently associated with this |
| 779 | * texture view. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 780 | * |
| 781 | * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener) |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 782 | * @see SurfaceTextureListener |
| 783 | */ |
| 784 | public SurfaceTextureListener getSurfaceTextureListener() { |
| 785 | return mListener; |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * Sets the {@link SurfaceTextureListener} used to listen to surface |
| 790 | * texture events. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 791 | * |
| 792 | * @see #getSurfaceTextureListener() |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 793 | * @see SurfaceTextureListener |
| 794 | */ |
| 795 | public void setSurfaceTextureListener(SurfaceTextureListener listener) { |
| 796 | mListener = listener; |
| 797 | } |
| 798 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 799 | @UnsupportedAppUsage |
Jeff Brown | c7282e5 | 2014-05-06 16:00:38 -0700 | [diff] [blame] | 800 | private final SurfaceTexture.OnFrameAvailableListener mUpdateListener = |
| 801 | new SurfaceTexture.OnFrameAvailableListener() { |
| 802 | @Override |
| 803 | public void onFrameAvailable(SurfaceTexture surfaceTexture) { |
| 804 | updateLayer(); |
| 805 | invalidate(); |
| 806 | } |
| 807 | }; |
| 808 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 809 | /** |
| 810 | * This listener can be used to be notified when the surface texture |
| 811 | * associated with this texture view is available. |
| 812 | */ |
| 813 | public static interface SurfaceTextureListener { |
| 814 | /** |
| 815 | * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 816 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 817 | * @param surface The surface returned by |
| 818 | * {@link android.view.TextureView#getSurfaceTexture()} |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 819 | * @param width The width of the surface |
| 820 | * @param height The height of the surface |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 821 | */ |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 822 | public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 823 | |
| 824 | /** |
| 825 | * Invoked when the {@link SurfaceTexture}'s buffers size changed. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 826 | * |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 827 | * @param surface The surface returned by |
| 828 | * {@link android.view.TextureView#getSurfaceTexture()} |
| 829 | * @param width The new width of the surface |
| 830 | * @param height The new height of the surface |
| 831 | */ |
| 832 | public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height); |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 833 | |
| 834 | /** |
| 835 | * Invoked when the specified {@link SurfaceTexture} is about to be destroyed. |
Grace Kloba | 402f055 | 2011-08-09 18:47:17 -0700 | [diff] [blame] | 836 | * If returns true, no rendering should happen inside the surface texture after this method |
| 837 | * is invoked. If returns false, the client needs to call {@link SurfaceTexture#release()}. |
Romain Guy | 52b307e | 2012-10-07 17:55:17 -0700 | [diff] [blame] | 838 | * Most applications should return true. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 839 | * |
Romain Guy | 451ce44 | 2011-06-10 15:40:36 -0700 | [diff] [blame] | 840 | * @param surface The surface about to be destroyed |
| 841 | */ |
Grace Kloba | 402f055 | 2011-08-09 18:47:17 -0700 | [diff] [blame] | 842 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surface); |
Grace Kloba | cf55937 | 2011-06-22 23:05:40 -0700 | [diff] [blame] | 843 | |
| 844 | /** |
| 845 | * Invoked when the specified {@link SurfaceTexture} is updated through |
| 846 | * {@link SurfaceTexture#updateTexImage()}. |
John Reck | 5ba0948 | 2015-05-18 15:13:30 -0700 | [diff] [blame] | 847 | * |
Grace Kloba | cf55937 | 2011-06-22 23:05:40 -0700 | [diff] [blame] | 848 | * @param surface The surface just updated |
| 849 | */ |
| 850 | public void onSurfaceTextureUpdated(SurfaceTexture surface); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 851 | } |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 852 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 853 | @UnsupportedAppUsage |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 854 | private native void nCreateNativeWindow(SurfaceTexture surface); |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 855 | @UnsupportedAppUsage |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 856 | private native void nDestroyNativeWindow(); |
| 857 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 858 | private static native boolean nLockCanvas(long nativeWindow, Canvas canvas, Rect dirty); |
| 859 | private static native void nUnlockCanvasAndPost(long nativeWindow, Canvas canvas); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 860 | } |