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