blob: 277b872a8cd03cb9ceac5224426e84e24bd43d23 [file] [log] [blame]
Romain Guyaa6c24c2011-04-28 18:40:04 -07001/*
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
17package android.view;
18
John Reck582f6bd2019-10-30 10:04:37 -070019import android.annotation.NonNull;
sergeyv342a7e62016-03-24 16:06:46 -070020import android.annotation.Nullable;
Artur Satayevad9254c2019-12-10 17:47:54 +000021import android.compat.annotation.UnsupportedAppUsage;
Romain Guyaa6c24c2011-04-28 18:40:04 -070022import android.content.Context;
Romain Guy77a81162011-06-14 16:45:55 -070023import android.graphics.Bitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -070024import android.graphics.Canvas;
Romain Guy302a9df2011-08-16 13:55:02 -070025import android.graphics.Matrix;
Romain Guyaa6c24c2011-04-28 18:40:04 -070026import android.graphics.Paint;
John Reck32f140aa62018-10-04 15:08:24 -070027import android.graphics.RecordingCanvas;
Romain Guy6be3d552011-07-14 18:08:37 -070028import android.graphics.Rect;
Romain Guyaa6c24c2011-04-28 18:40:04 -070029import android.graphics.SurfaceTexture;
Chris Craik2e931ea2015-08-19 16:13:04 -070030import android.graphics.drawable.Drawable;
Romain Guyaa6c24c2011-04-28 18:40:04 -070031import android.util.AttributeSet;
32import android.util.Log;
33
34/**
35 * <p>A TextureView can be used to display a content stream. Such a content
36 * stream can for instance be a video or an OpenGL scene. The content stream
37 * can come from the application's process as well as a remote process.</p>
John Reck5ba09482015-05-18 15:13:30 -070038 *
Romain Guyaa6c24c2011-04-28 18:40:04 -070039 * <p>TextureView can only be used in a hardware accelerated window. When
40 * rendered in software, TextureView will draw nothing.</p>
John Reck5ba09482015-05-18 15:13:30 -070041 *
Romain Guyaa6c24c2011-04-28 18:40:04 -070042 * <p>Unlike {@link SurfaceView}, TextureView does not create a separate
43 * window but behaves as a regular View. This key difference allows a
44 * TextureView to be moved, transformed, animated, etc. For instance, you
45 * can make a TextureView semi-translucent by calling
46 * <code>myView.setAlpha(0.5f)</code>.</p>
John Reck5ba09482015-05-18 15:13:30 -070047 *
Romain Guyaa6c24c2011-04-28 18:40:04 -070048 * <p>Using a TextureView is simple: all you need to do is get its
49 * {@link SurfaceTexture}. The {@link SurfaceTexture} can then be used to
John Reck5ba09482015-05-18 15:13:30 -070050 * render content. The following example demonstrates how to render the
Romain Guyaa6c24c2011-04-28 18:40:04 -070051 * camera preview into a TextureView:</p>
John Reck5ba09482015-05-18 15:13:30 -070052 *
Romain Guyaa6c24c2011-04-28 18:40:04 -070053 * <pre>
54 * public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
55 * private Camera mCamera;
56 * private TextureView mTextureView;
57 *
Romain Guyaa6c24c2011-04-28 18:40:04 -070058 * protected void onCreate(Bundle savedInstanceState) {
59 * super.onCreate(savedInstanceState);
60 *
61 * mTextureView = new TextureView(this);
62 * mTextureView.setSurfaceTextureListener(this);
63 *
64 * setContentView(mTextureView);
65 * }
66 *
Romain Guy451ce442011-06-10 15:40:36 -070067 * public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Romain Guyaa6c24c2011-04-28 18:40:04 -070068 * mCamera = Camera.open();
69 *
70 * try {
71 * mCamera.setPreviewTexture(surface);
72 * mCamera.startPreview();
73 * } catch (IOException ioe) {
74 * // Something bad happened
75 * }
76 * }
Grace Klobacf559372011-06-22 23:05:40 -070077 *
Romain Guy8f0095c2011-05-02 17:24:22 -070078 * public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
79 * // Ignored, Camera does all the work for us
80 * }
Grace Klobacf559372011-06-22 23:05:40 -070081 *
Grace Kloba402f0552011-08-09 18:47:17 -070082 * public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Romain Guy451ce442011-06-10 15:40:36 -070083 * mCamera.stopPreview();
84 * mCamera.release();
Grace Kloba402f0552011-08-09 18:47:17 -070085 * return true;
Romain Guy451ce442011-06-10 15:40:36 -070086 * }
Grace Klobacf559372011-06-22 23:05:40 -070087 *
88 * public void onSurfaceTextureUpdated(SurfaceTexture surface) {
Romain Guy58f4edb2011-06-24 14:51:38 -070089 * // Invoked every time there's a new Camera preview frame
Grace Klobacf559372011-06-22 23:05:40 -070090 * }
Romain Guyaa6c24c2011-04-28 18:40:04 -070091 * }
92 * </pre>
John Reck5ba09482015-05-18 15:13:30 -070093 *
Romain Guyaa6c24c2011-04-28 18:40:04 -070094 * <p>A TextureView's SurfaceTexture can be obtained either by invoking
95 * {@link #getSurfaceTexture()} or by using a {@link SurfaceTextureListener}.
96 * It is important to know that a SurfaceTexture is available only after the
97 * TextureView is attached to a window (and {@link #onAttachedToWindow()} has
98 * been invoked.) It is therefore highly recommended you use a listener to
99 * be notified when the SurfaceTexture becomes available.</p>
John Reck5ba09482015-05-18 15:13:30 -0700100 *
Romain Guy462785f2011-09-27 17:42:10 -0700101 * <p>It is important to note that only one producer can use the TextureView.
102 * For instance, if you use a TextureView to display the camera preview, you
103 * cannot use {@link #lockCanvas()} to draw onto the TextureView at the same
104 * time.</p>
John Reck5ba09482015-05-18 15:13:30 -0700105 *
Romain Guyaa6c24c2011-04-28 18:40:04 -0700106 * @see SurfaceView
107 * @see SurfaceTexture
108 */
109public class TextureView extends View {
Romain Guy77a81162011-06-14 16:45:55 -0700110 private static final String LOG_TAG = "TextureView";
111
Mathew Inwooda570dee2018-08-17 14:56:00 +0100112 @UnsupportedAppUsage
John Reck9d8d99d2018-02-21 12:55:41 -0800113 private TextureLayer mLayer;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100114 @UnsupportedAppUsage
Romain Guyaa6c24c2011-04-28 18:40:04 -0700115 private SurfaceTexture mSurface;
116 private SurfaceTextureListener mListener;
Romain Guy67603c62013-06-27 10:58:10 -0700117 private boolean mHadSurface;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700118
Mathew Inwooda570dee2018-08-17 14:56:00 +0100119 @UnsupportedAppUsage
Romain Guya9489272011-06-22 20:58:11 -0700120 private boolean mOpaque = true;
Romain Guyc989d862011-06-22 14:53:39 -0700121
Romain Guy302a9df2011-08-16 13:55:02 -0700122 private final Matrix mMatrix = new Matrix();
123 private boolean mMatrixChanged;
124
Romain Guy58f4edb2011-06-24 14:51:38 -0700125 private final Object[] mLock = new Object[0];
126 private boolean mUpdateLayer;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100127 @UnsupportedAppUsage
Jamie Gennis2af35242012-04-05 11:44:30 -0700128 private boolean mUpdateSurface;
Romain Guy58f4edb2011-06-24 14:51:38 -0700129
Romain Guy6be3d552011-07-14 18:08:37 -0700130 private Canvas mCanvas;
131 private int mSaveCount;
132
133 private final Object[] mNativeWindowLock = new Object[0];
John Reck918ad522014-06-27 14:45:25 -0700134 // Set by native code, do not write!
Mathew Inwooda570dee2018-08-17 14:56:00 +0100135 @UnsupportedAppUsage
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000136 private long mNativeWindow;
Romain Guy6be3d552011-07-14 18:08:37 -0700137
Romain Guyaa6c24c2011-04-28 18:40:04 -0700138 /**
139 * Creates a new TextureView.
John Reck5ba09482015-05-18 15:13:30 -0700140 *
Romain Guyaa6c24c2011-04-28 18:40:04 -0700141 * @param context The context to associate this view with.
142 */
John Reck582f6bd2019-10-30 10:04:37 -0700143 public TextureView(@NonNull Context context) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700144 super(context);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700145 }
146
147 /**
148 * Creates a new TextureView.
John Reck5ba09482015-05-18 15:13:30 -0700149 *
Romain Guyaa6c24c2011-04-28 18:40:04 -0700150 * @param context The context to associate this view with.
151 * @param attrs The attributes of the XML tag that is inflating the view.
152 */
John Reck582f6bd2019-10-30 10:04:37 -0700153 public TextureView(@NonNull Context context, @Nullable AttributeSet attrs) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700154 super(context, attrs);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700155 }
156
157 /**
158 * Creates a new TextureView.
John Reck5ba09482015-05-18 15:13:30 -0700159 *
Romain Guyaa6c24c2011-04-28 18:40:04 -0700160 * @param context The context to associate this view with.
161 * @param attrs The attributes of the XML tag that is inflating the view.
Alan Viverette617feb92013-09-09 18:09:13 -0700162 * @param defStyleAttr An attribute in the current theme that contains a
163 * reference to a style resource that supplies default values for
164 * the view. Can be 0 to not look for defaults.
Romain Guyaa6c24c2011-04-28 18:40:04 -0700165 */
John Reck582f6bd2019-10-30 10:04:37 -0700166 public TextureView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
Alan Viverette617feb92013-09-09 18:09:13 -0700167 super(context, attrs, defStyleAttr);
Alan Viverette617feb92013-09-09 18:09:13 -0700168 }
169
170 /**
171 * Creates a new TextureView.
172 *
173 * @param context The context to associate this view with.
174 * @param attrs The attributes of the XML tag that is inflating the view.
175 * @param defStyleAttr An attribute in the current theme that contains a
176 * reference to a style resource that supplies default values for
177 * the view. Can be 0 to not look for defaults.
178 * @param defStyleRes A resource identifier of a style resource that
179 * supplies default values for the view, used only if
180 * defStyleAttr is 0 or can not be found in the theme. Can be 0
181 * to not look for defaults.
182 */
John Reck582f6bd2019-10-30 10:04:37 -0700183 public TextureView(@NonNull Context context, @Nullable AttributeSet attrs,
184 int defStyleAttr, int defStyleRes) {
Alan Viverette617feb92013-09-09 18:09:13 -0700185 super(context, attrs, defStyleAttr, defStyleRes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700186 }
187
Romain Guya9489272011-06-22 20:58:11 -0700188 /**
189 * {@inheritDoc}
190 */
191 @Override
192 public boolean isOpaque() {
193 return mOpaque;
194 }
195
196 /**
197 * Indicates whether the content of this TextureView is opaque. The
198 * content is assumed to be opaque by default.
John Reck5ba09482015-05-18 15:13:30 -0700199 *
Romain Guya9489272011-06-22 20:58:11 -0700200 * @param opaque True if the content of this TextureView is opaque,
201 * false otherwise
202 */
203 public void setOpaque(boolean opaque) {
204 if (opaque != mOpaque) {
205 mOpaque = opaque;
Romain Guya8a2f972012-04-12 16:33:44 -0700206 if (mLayer != null) {
Romain Guy88801b22012-10-05 14:58:33 -0700207 updateLayerAndInvalidate();
Romain Guya8a2f972012-04-12 16:33:44 -0700208 }
Romain Guya9489272011-06-22 20:58:11 -0700209 }
210 }
211
Romain Guyaa6c24c2011-04-28 18:40:04 -0700212 @Override
213 protected void onAttachedToWindow() {
214 super.onAttachedToWindow();
215
216 if (!isHardwareAccelerated()) {
Romain Guy77a81162011-06-14 16:45:55 -0700217 Log.w(LOG_TAG, "A TextureView or a subclass can only be "
Romain Guyaa6c24c2011-04-28 18:40:04 -0700218 + "used with hardware acceleration enabled.");
219 }
Romain Guy67603c62013-06-27 10:58:10 -0700220
221 if (mHadSurface) {
222 invalidate(true);
223 mHadSurface = false;
224 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700225 }
226
John Reckb14dfe22014-03-06 22:06:20 +0000227 /** @hide */
Romain Guy451ce442011-06-10 15:40:36 -0700228 @Override
Mathew Inwooda570dee2018-08-17 14:56:00 +0100229 @UnsupportedAppUsage
John Reckb14dfe22014-03-06 22:06:20 +0000230 protected void onDetachedFromWindowInternal() {
sergeyv1c16c372016-08-02 14:46:12 -0700231 destroyHardwareLayer();
232 releaseSurfaceTexture();
John Reckb14dfe22014-03-06 22:06:20 +0000233 super.onDetachedFromWindowInternal();
Romain Guy31f2c2e2011-11-21 10:55:41 -0800234 }
Romain Guy451ce442011-06-10 15:40:36 -0700235
sergeyv1c16c372016-08-02 14:46:12 -0700236 /**
237 * @hide
238 */
239 @Override
Mathew Inwooda570dee2018-08-17 14:56:00 +0100240 @UnsupportedAppUsage
sergeyv1c16c372016-08-02 14:46:12 -0700241 protected void destroyHardwareResources() {
sergeyv17dd01b2016-08-30 14:03:32 -0700242 super.destroyHardwareResources();
sergeyv1c16c372016-08-02 14:46:12 -0700243 destroyHardwareLayer();
sergeyv1c16c372016-08-02 14:46:12 -0700244 }
245
Mathew Inwooda570dee2018-08-17 14:56:00 +0100246 @UnsupportedAppUsage
sergeyv1c16c372016-08-02 14:46:12 -0700247 private void destroyHardwareLayer() {
Romain Guy80429c42011-06-24 17:20:32 -0700248 if (mLayer != null) {
John Reck918ad522014-06-27 14:45:25 -0700249 mLayer.detachSurfaceTexture();
Romain Guy6be3d552011-07-14 18:08:37 -0700250 mLayer.destroy();
Romain Guy451ce442011-06-10 15:40:36 -0700251 mLayer = null;
Doris Liu6b471992015-04-30 17:00:35 -0700252 mMatrixChanged = true;
Romain Guy451ce442011-06-10 15:40:36 -0700253 }
254 }
255
sergeyv1c16c372016-08-02 14:46:12 -0700256 private void releaseSurfaceTexture() {
John Reck3c2587f2016-08-04 07:55:38 -0700257 if (mSurface != null) {
258 boolean shouldRelease = true;
sergeyv1c16c372016-08-02 14:46:12 -0700259
John Reck3c2587f2016-08-04 07:55:38 -0700260 if (mListener != null) {
261 shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface);
262 }
sergeyv1c16c372016-08-02 14:46:12 -0700263
John Reck3c2587f2016-08-04 07:55:38 -0700264 synchronized (mNativeWindowLock) {
265 nDestroyNativeWindow();
266 }
sergeyv1c16c372016-08-02 14:46:12 -0700267
John Reck3c2587f2016-08-04 07:55:38 -0700268 if (shouldRelease) {
269 mSurface.release();
270 }
271 mSurface = null;
272 mHadSurface = true;
sergeyv1c16c372016-08-02 14:46:12 -0700273 }
sergeyv1c16c372016-08-02 14:46:12 -0700274 }
275
Romain Guyaa6c24c2011-04-28 18:40:04 -0700276 /**
277 * The layer type of a TextureView is ignored since a TextureView is always
278 * considered to act as a hardware layer. The optional paint supplied to this
279 * method will however be taken into account when rendering the content of
280 * this TextureView.
John Reck5ba09482015-05-18 15:13:30 -0700281 *
sergeyv342a7e62016-03-24 16:06:46 -0700282 * @param layerType The type of layer to use with this view, must be one of
Romain Guyaa6c24c2011-04-28 18:40:04 -0700283 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
284 * {@link #LAYER_TYPE_HARDWARE}
285 * @param paint The paint used to compose the layer. This argument is optional
286 * and can be null. It is ignored when the layer type is
287 * {@link #LAYER_TYPE_NONE}
288 */
289 @Override
sergeyv342a7e62016-03-24 16:06:46 -0700290 public void setLayerType(int layerType, @Nullable Paint paint) {
291 setLayerPaint(paint);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700292 }
293
John Reck25fbb3f2014-06-12 13:46:45 -0700294 @Override
sergeyv342a7e62016-03-24 16:06:46 -0700295 public void setLayerPaint(@Nullable Paint paint) {
296 if (paint != mLayerPaint) {
297 mLayerPaint = paint;
298 invalidate();
299 }
John Reck25fbb3f2014-06-12 13:46:45 -0700300 }
301
Romain Guyaa6c24c2011-04-28 18:40:04 -0700302 /**
303 * Always returns {@link #LAYER_TYPE_HARDWARE}.
304 */
305 @Override
306 public int getLayerType() {
307 return LAYER_TYPE_HARDWARE;
308 }
309
310 /**
311 * Calling this method has no effect.
312 */
313 @Override
314 public void buildLayer() {
315 }
316
Chris Craik2e931ea2015-08-19 16:13:04 -0700317 @Override
318 public void setForeground(Drawable foreground) {
Chris Craikb7244802016-01-04 15:47:19 -0800319 if (foreground != null && !sTextureViewIgnoresDrawableSetters) {
Chris Craik2e931ea2015-08-19 16:13:04 -0700320 throw new UnsupportedOperationException(
321 "TextureView doesn't support displaying a foreground drawable");
322 }
323 }
324
325 @Override
326 public void setBackgroundDrawable(Drawable background) {
Chris Craikb7244802016-01-04 15:47:19 -0800327 if (background != null && !sTextureViewIgnoresDrawableSetters) {
Chris Craik2e931ea2015-08-19 16:13:04 -0700328 throw new UnsupportedOperationException(
329 "TextureView doesn't support displaying a background drawable");
330 }
331 }
332
Romain Guyaa6c24c2011-04-28 18:40:04 -0700333 /**
334 * Subclasses of TextureView cannot do their own rendering
335 * with the {@link Canvas} object.
John Reck5ba09482015-05-18 15:13:30 -0700336 *
Romain Guyaa6c24c2011-04-28 18:40:04 -0700337 * @param canvas The Canvas to which the View is rendered.
338 */
339 @Override
340 public final void draw(Canvas canvas) {
Chris Craik3aadd602015-08-20 12:41:40 -0700341 // NOTE: Maintain this carefully (see View#draw)
Romain Guy52b307e2012-10-07 17:55:17 -0700342 mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
343
Chris Craik3aadd602015-08-20 12:41:40 -0700344 /* Simplify drawing to guarantee the layer is the only thing drawn - so e.g. no background,
345 scrolling, or fading edges. This guarantees all drawing is in the layer, so drawing
346 properties (alpha, layer paint) affect all of the content of a TextureView. */
347
348 if (canvas.isHardwareAccelerated()) {
John Reck32f140aa62018-10-04 15:08:24 -0700349 RecordingCanvas recordingCanvas = (RecordingCanvas) canvas;
Chris Craik3aadd602015-08-20 12:41:40 -0700350
John Reck9d8d99d2018-02-21 12:55:41 -0800351 TextureLayer layer = getTextureLayer();
Chris Craik3aadd602015-08-20 12:41:40 -0700352 if (layer != null) {
353 applyUpdate();
354 applyTransformMatrix();
355
356 mLayer.setLayerPaint(mLayerPaint); // ensure layer paint is up to date
John Reck32f140aa62018-10-04 15:08:24 -0700357 recordingCanvas.drawTextureLayer(layer);
Chris Craik3aadd602015-08-20 12:41:40 -0700358 }
359 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700360 }
361
362 /**
363 * Subclasses of TextureView cannot do their own rendering
364 * with the {@link Canvas} object.
John Reck5ba09482015-05-18 15:13:30 -0700365 *
Romain Guyaa6c24c2011-04-28 18:40:04 -0700366 * @param canvas The Canvas to which the View is rendered.
367 */
368 @Override
369 protected final void onDraw(Canvas canvas) {
370 }
371
372 @Override
Romain Guy8f0095c2011-05-02 17:24:22 -0700373 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
374 super.onSizeChanged(w, h, oldw, oldh);
375 if (mSurface != null) {
Mathias Agopian52a9a102013-08-02 01:38:38 -0700376 mSurface.setDefaultBufferSize(getWidth(), getHeight());
Romain Guy88801b22012-10-05 14:58:33 -0700377 updateLayer();
Romain Guy451ce442011-06-10 15:40:36 -0700378 if (mListener != null) {
379 mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight());
380 }
Romain Guy8f0095c2011-05-02 17:24:22 -0700381 }
382 }
383
John Reck9d8d99d2018-02-21 12:55:41 -0800384 TextureLayer getTextureLayer() {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700385 if (mLayer == null) {
Stan Iliev45faba52016-06-28 13:33:15 -0400386 if (mAttachInfo == null || mAttachInfo.mThreadedRenderer == null) {
Romain Guy58f4edb2011-06-24 14:51:38 -0700387 return null;
388 }
389
Stan Iliev45faba52016-06-28 13:33:15 -0400390 mLayer = mAttachInfo.mThreadedRenderer.createTextureLayer();
John Reck7e237182016-08-11 10:43:14 -0700391 boolean createNewSurface = (mSurface == null);
392 if (createNewSurface) {
Jamie Gennis8a34d682012-04-17 16:01:34 -0700393 // Create a new SurfaceTexture for the layer.
John Reck918ad522014-06-27 14:45:25 -0700394 mSurface = new SurfaceTexture(false);
sergeyv1c16c372016-08-02 14:46:12 -0700395 nCreateNativeWindow(mSurface);
Jamie Gennis2af35242012-04-05 11:44:30 -0700396 }
John Reck03df0832016-08-16 09:44:23 -0700397 mLayer.setSurfaceTexture(mSurface);
Mathias Agopian52a9a102013-08-02 01:38:38 -0700398 mSurface.setDefaultBufferSize(getWidth(), getHeight());
Jeff Brownc7282e52014-05-06 16:00:38 -0700399 mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700400
John Reck7e237182016-08-11 10:43:14 -0700401 if (mListener != null && createNewSurface) {
Romain Guy451ce442011-06-10 15:40:36 -0700402 mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700403 }
Chet Haased15ebf22012-09-05 11:40:29 -0700404 mLayer.setLayerPaint(mLayerPaint);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700405 }
406
Jamie Gennis2af35242012-04-05 11:44:30 -0700407 if (mUpdateSurface) {
408 // Someone has requested that we use a specific SurfaceTexture, so
409 // tell mLayer about it and set the SurfaceTexture to use the
410 // current view size.
411 mUpdateSurface = false;
Romain Guy51f7c6b2012-05-21 16:32:59 -0700412
413 // Since we are updating the layer, force an update to ensure its
414 // parameters are correct (width, height, transform, etc.)
Romain Guy88801b22012-10-05 14:58:33 -0700415 updateLayer();
Romain Guy51f7c6b2012-05-21 16:32:59 -0700416 mMatrixChanged = true;
417
John Reck04fc5832014-02-05 16:38:25 -0800418 mLayer.setSurfaceTexture(mSurface);
Mathias Agopian52a9a102013-08-02 01:38:38 -0700419 mSurface.setDefaultBufferSize(getWidth(), getHeight());
Jamie Gennis2af35242012-04-05 11:44:30 -0700420 }
421
Romain Guyaa6c24c2011-04-28 18:40:04 -0700422 return mLayer;
423 }
424
Romain Guy8f0095c2011-05-02 17:24:22 -0700425 @Override
426 protected void onVisibilityChanged(View changedView, int visibility) {
427 super.onVisibilityChanged(changedView, visibility);
428
429 if (mSurface != null) {
430 // When the view becomes invisible, stop updating it, it's a waste of CPU
431 // To cancel updates, the easiest thing to do is simply to remove the
432 // updates listener
433 if (visibility == VISIBLE) {
John Reck2dedafb2014-05-29 16:15:41 -0700434 if (mLayer != null) {
435 mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler);
436 }
Romain Guy88801b22012-10-05 14:58:33 -0700437 updateLayerAndInvalidate();
Romain Guy8f0095c2011-05-02 17:24:22 -0700438 } else {
439 mSurface.setOnFrameAvailableListener(null);
440 }
441 }
442 }
443
Romain Guya9489272011-06-22 20:58:11 -0700444 private void updateLayer() {
Romain Guy88801b22012-10-05 14:58:33 -0700445 synchronized (mLock) {
446 mUpdateLayer = true;
447 }
448 }
449
450 private void updateLayerAndInvalidate() {
451 synchronized (mLock) {
452 mUpdateLayer = true;
453 }
Romain Guy58f4edb2011-06-24 14:51:38 -0700454 invalidate();
455 }
Jamie Gennis2af35242012-04-05 11:44:30 -0700456
Romain Guy58f4edb2011-06-24 14:51:38 -0700457 private void applyUpdate() {
458 if (mLayer == null) {
Romain Guya9489272011-06-22 20:58:11 -0700459 return;
460 }
461
Romain Guy58f4edb2011-06-24 14:51:38 -0700462 synchronized (mLock) {
463 if (mUpdateLayer) {
464 mUpdateLayer = false;
465 } else {
466 return;
467 }
468 }
John Reck5ba09482015-05-18 15:13:30 -0700469
John Reck04fc5832014-02-05 16:38:25 -0800470 mLayer.prepare(getWidth(), getHeight(), mOpaque);
471 mLayer.updateSurfaceTexture();
Romain Guya9489272011-06-22 20:58:11 -0700472
Grace Klobacf559372011-06-22 23:05:40 -0700473 if (mListener != null) {
474 mListener.onSurfaceTextureUpdated(mSurface);
475 }
Romain Guya9489272011-06-22 20:58:11 -0700476 }
477
Romain Guyaa6c24c2011-04-28 18:40:04 -0700478 /**
Romain Guy302a9df2011-08-16 13:55:02 -0700479 * <p>Sets the transform to associate with this texture view.
480 * The specified transform applies to the underlying surface
481 * texture and does not affect the size or position of the view
482 * itself, only of its content.</p>
John Reck5ba09482015-05-18 15:13:30 -0700483 *
Romain Guy302a9df2011-08-16 13:55:02 -0700484 * <p>Some transforms might prevent the content from drawing
485 * all the pixels contained within this view's bounds. In such
486 * situations, make sure this texture view is not marked opaque.</p>
John Reck5ba09482015-05-18 15:13:30 -0700487 *
Romain Guy302a9df2011-08-16 13:55:02 -0700488 * @param transform The transform to apply to the content of
John Reck582f6bd2019-10-30 10:04:37 -0700489 * this view. If null the transform will be set to identity.
John Reck5ba09482015-05-18 15:13:30 -0700490 *
491 * @see #getTransform(android.graphics.Matrix)
492 * @see #isOpaque()
493 * @see #setOpaque(boolean)
Romain Guy302a9df2011-08-16 13:55:02 -0700494 */
John Reck582f6bd2019-10-30 10:04:37 -0700495 public void setTransform(@Nullable Matrix transform) {
Romain Guy302a9df2011-08-16 13:55:02 -0700496 mMatrix.set(transform);
497 mMatrixChanged = true;
Alexandre Eliasc01391f2011-08-19 16:03:16 -0700498 invalidateParentIfNeeded();
Romain Guy302a9df2011-08-16 13:55:02 -0700499 }
500
501 /**
502 * Returns the transform associated with this texture view.
John Reck5ba09482015-05-18 15:13:30 -0700503 *
Romain Guy302a9df2011-08-16 13:55:02 -0700504 * @param transform The {@link Matrix} in which to copy the current
505 * transform. Can be null.
John Reck5ba09482015-05-18 15:13:30 -0700506 *
Romain Guy302a9df2011-08-16 13:55:02 -0700507 * @return The specified matrix if not null or a new {@link Matrix}
508 * instance otherwise.
John Reck5ba09482015-05-18 15:13:30 -0700509 *
510 * @see #setTransform(android.graphics.Matrix)
Romain Guy302a9df2011-08-16 13:55:02 -0700511 */
John Reck582f6bd2019-10-30 10:04:37 -0700512 public @NonNull Matrix getTransform(@Nullable Matrix transform) {
Romain Guy302a9df2011-08-16 13:55:02 -0700513 if (transform == null) {
514 transform = new Matrix();
515 }
516
517 transform.set(mMatrix);
518
519 return transform;
520 }
521
Alexandre Eliasc01391f2011-08-19 16:03:16 -0700522 private void applyTransformMatrix() {
Romain Guy51f7c6b2012-05-21 16:32:59 -0700523 if (mMatrixChanged && mLayer != null) {
Alexandre Eliasc01391f2011-08-19 16:03:16 -0700524 mLayer.setTransform(mMatrix);
525 mMatrixChanged = false;
526 }
527 }
528
Romain Guy302a9df2011-08-16 13:55:02 -0700529 /**
Romain Guy77a81162011-06-14 16:45:55 -0700530 * <p>Returns a {@link android.graphics.Bitmap} representation of the content
531 * of the associated surface texture. If the surface texture is not available,
532 * this method returns null.</p>
John Reck5ba09482015-05-18 15:13:30 -0700533 *
Romain Guy77a81162011-06-14 16:45:55 -0700534 * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
535 * pixel format and its dimensions are the same as this view's.</p>
John Reck5ba09482015-05-18 15:13:30 -0700536 *
Romain Guy77a81162011-06-14 16:45:55 -0700537 * <p><strong>Do not</strong> invoke this method from a drawing method
538 * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
John Reck5ba09482015-05-18 15:13:30 -0700539 *
Romain Guyd6b2a002011-06-17 17:45:59 -0700540 * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
John Reck5ba09482015-05-18 15:13:30 -0700541 *
Romain Guy77a81162011-06-14 16:45:55 -0700542 * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
543 * texture is not available or the width &lt;= 0 or the height &lt;= 0
John Reck5ba09482015-05-18 15:13:30 -0700544 *
545 * @see #isAvailable()
546 * @see #getBitmap(android.graphics.Bitmap)
547 * @see #getBitmap(int, int)
Romain Guy77a81162011-06-14 16:45:55 -0700548 */
John Reck582f6bd2019-10-30 10:04:37 -0700549 public @Nullable Bitmap getBitmap() {
Romain Guy77a81162011-06-14 16:45:55 -0700550 return getBitmap(getWidth(), getHeight());
551 }
552
553 /**
554 * <p>Returns a {@link android.graphics.Bitmap} representation of the content
555 * of the associated surface texture. If the surface texture is not available,
556 * this method returns null.</p>
John Reck5ba09482015-05-18 15:13:30 -0700557 *
Romain Guy77a81162011-06-14 16:45:55 -0700558 * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
559 * pixel format.</p>
John Reck5ba09482015-05-18 15:13:30 -0700560 *
Romain Guy77a81162011-06-14 16:45:55 -0700561 * <p><strong>Do not</strong> invoke this method from a drawing method
562 * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
John Reck5ba09482015-05-18 15:13:30 -0700563 *
Romain Guyd6b2a002011-06-17 17:45:59 -0700564 * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
John Reck5ba09482015-05-18 15:13:30 -0700565 *
Romain Guy77a81162011-06-14 16:45:55 -0700566 * @param width The width of the bitmap to create
567 * @param height The height of the bitmap to create
John Reck5ba09482015-05-18 15:13:30 -0700568 *
Romain Guy77a81162011-06-14 16:45:55 -0700569 * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
570 * texture is not available or width is &lt;= 0 or height is &lt;= 0
John Reck5ba09482015-05-18 15:13:30 -0700571 *
572 * @see #isAvailable()
573 * @see #getBitmap(android.graphics.Bitmap)
574 * @see #getBitmap()
Romain Guy77a81162011-06-14 16:45:55 -0700575 */
John Reck582f6bd2019-10-30 10:04:37 -0700576 public @Nullable Bitmap getBitmap(int width, int height) {
Romain Guy77a81162011-06-14 16:45:55 -0700577 if (isAvailable() && width > 0 && height > 0) {
Dianne Hackborndde331c2012-08-03 14:01:57 -0700578 return getBitmap(Bitmap.createBitmap(getResources().getDisplayMetrics(),
579 width, height, Bitmap.Config.ARGB_8888));
Romain Guy77a81162011-06-14 16:45:55 -0700580 }
581 return null;
582 }
583
584 /**
585 * <p>Copies the content of this view's surface texture into the specified
586 * bitmap. If the surface texture is not available, the copy is not executed.
587 * The content of the surface texture will be scaled to fit exactly inside
588 * the specified bitmap.</p>
John Reck5ba09482015-05-18 15:13:30 -0700589 *
Romain Guy77a81162011-06-14 16:45:55 -0700590 * <p><strong>Do not</strong> invoke this method from a drawing method
591 * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
John Reck5ba09482015-05-18 15:13:30 -0700592 *
Romain Guyd6b2a002011-06-17 17:45:59 -0700593 * <p>If an error occurs, the bitmap is left unchanged.</p>
John Reck5ba09482015-05-18 15:13:30 -0700594 *
Romain Guy77a81162011-06-14 16:45:55 -0700595 * @param bitmap The bitmap to copy the content of the surface texture into,
596 * cannot be null, all configurations are supported
John Reck5ba09482015-05-18 15:13:30 -0700597 *
Romain Guy77a81162011-06-14 16:45:55 -0700598 * @return The bitmap specified as a parameter
John Reck5ba09482015-05-18 15:13:30 -0700599 *
600 * @see #isAvailable()
601 * @see #getBitmap(int, int)
602 * @see #getBitmap()
603 *
Romain Guy589b0bb2011-10-10 13:57:47 -0700604 * @throws IllegalStateException if the hardware rendering context cannot be
605 * acquired to capture the bitmap
Romain Guy77a81162011-06-14 16:45:55 -0700606 */
John Reck582f6bd2019-10-30 10:04:37 -0700607 public @NonNull Bitmap getBitmap(@NonNull Bitmap bitmap) {
Romain Guy77a81162011-06-14 16:45:55 -0700608 if (bitmap != null && isAvailable()) {
Romain Guy589b0bb2011-10-10 13:57:47 -0700609 applyUpdate();
610 applyTransformMatrix();
611
Romain Guy78245f72012-05-11 10:01:42 -0700612 // This case can happen if the app invokes setSurfaceTexture() before
613 // we are able to create the hardware layer. We can safely initialize
614 // the layer here thanks to the validate() call at the beginning of
615 // this method
616 if (mLayer == null && mUpdateSurface) {
John Reck9d8d99d2018-02-21 12:55:41 -0800617 getTextureLayer();
Romain Guy78245f72012-05-11 10:01:42 -0700618 }
619
620 if (mLayer != null) {
621 mLayer.copyInto(bitmap);
622 }
Romain Guy77a81162011-06-14 16:45:55 -0700623 }
624 return bitmap;
625 }
626
627 /**
628 * Returns true if the {@link SurfaceTexture} associated with this
629 * TextureView is available for rendering. When this method returns
630 * true, {@link #getSurfaceTexture()} returns a valid surface texture.
631 */
632 public boolean isAvailable() {
633 return mSurface != null;
634 }
635
636 /**
Romain Guy6be3d552011-07-14 18:08:37 -0700637 * <p>Start editing the pixels in the surface. The returned Canvas can be used
638 * to draw into the surface's bitmap. A null is returned if the surface has
639 * not been created or otherwise cannot be edited. You will usually need
640 * to implement
641 * {@link SurfaceTextureListener#onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int)}
642 * to find out when the Surface is available for use.</p>
John Reck5ba09482015-05-18 15:13:30 -0700643 *
Romain Guy6be3d552011-07-14 18:08:37 -0700644 * <p>The content of the Surface is never preserved between unlockCanvas()
645 * and lockCanvas(), for this reason, every pixel within the Surface area
646 * must be written. The only exception to this rule is when a dirty
647 * rectangle is specified, in which case, non-dirty pixels will be
648 * preserved.</p>
John Reck5ba09482015-05-18 15:13:30 -0700649 *
Romain Guy462785f2011-09-27 17:42:10 -0700650 * <p>This method can only be used if the underlying surface is not already
651 * owned by another producer. For instance, if the TextureView is being used
652 * to render the camera's preview you cannot invoke this method.</p>
John Reck5ba09482015-05-18 15:13:30 -0700653 *
John Reck582f6bd2019-10-30 10:04:37 -0700654 * @return A Canvas used to draw into the surface, or null if the surface cannot be locked for
655 * drawing (see {@link #isAvailable()}).
John Reck5ba09482015-05-18 15:13:30 -0700656 *
657 * @see #lockCanvas(android.graphics.Rect)
658 * @see #unlockCanvasAndPost(android.graphics.Canvas)
Romain Guy6be3d552011-07-14 18:08:37 -0700659 */
John Reck582f6bd2019-10-30 10:04:37 -0700660 public @Nullable Canvas lockCanvas() {
Romain Guy6be3d552011-07-14 18:08:37 -0700661 return lockCanvas(null);
662 }
663
664 /**
665 * Just like {@link #lockCanvas()} but allows specification of a dirty
666 * rectangle. Every pixel within that rectangle must be written; however
667 * pixels outside the dirty rectangle will be preserved by the next call
668 * to lockCanvas().
Romain Guy53bacf52013-04-30 11:30:10 -0700669 *
670 * This method can return null if the underlying surface texture is not
671 * available (see {@link #isAvailable()} or if the surface texture is
672 * already connected to an image producer (for instance: the camera,
673 * OpenGL, a media player, etc.)
John Reck5ba09482015-05-18 15:13:30 -0700674 *
John Reck582f6bd2019-10-30 10:04:37 -0700675 * @param dirty Area of the surface that will be modified. If null the area of the entire
676 * surface is used.
Romain Guy6be3d552011-07-14 18:08:37 -0700677
John Reck582f6bd2019-10-30 10:04:37 -0700678 * @return A Canvas used to draw into the surface, or null if the surface cannot be locked for
679 * drawing (see {@link #isAvailable()}).
John Reck5ba09482015-05-18 15:13:30 -0700680 *
681 * @see #lockCanvas()
Romain Guy53bacf52013-04-30 11:30:10 -0700682 * @see #unlockCanvasAndPost(android.graphics.Canvas)
683 * @see #isAvailable()
Romain Guy6be3d552011-07-14 18:08:37 -0700684 */
John Reck582f6bd2019-10-30 10:04:37 -0700685 public @Nullable Canvas lockCanvas(@Nullable Rect dirty) {
Romain Guy6be3d552011-07-14 18:08:37 -0700686 if (!isAvailable()) return null;
687
688 if (mCanvas == null) {
689 mCanvas = new Canvas();
690 }
691
692 synchronized (mNativeWindowLock) {
Romain Guy53bacf52013-04-30 11:30:10 -0700693 if (!nLockCanvas(mNativeWindow, mCanvas, dirty)) {
694 return null;
695 }
Romain Guy6be3d552011-07-14 18:08:37 -0700696 }
697 mSaveCount = mCanvas.save();
698
699 return mCanvas;
700 }
701
702 /**
703 * Finish editing pixels in the surface. After this call, the surface's
704 * current pixels will be shown on the screen, but its content is lost,
705 * in particular there is no guarantee that the content of the Surface
706 * will remain unchanged when lockCanvas() is called again.
John Reck5ba09482015-05-18 15:13:30 -0700707 *
Romain Guy6be3d552011-07-14 18:08:37 -0700708 * @param canvas The Canvas previously returned by lockCanvas()
John Reck5ba09482015-05-18 15:13:30 -0700709 *
Romain Guy6be3d552011-07-14 18:08:37 -0700710 * @see #lockCanvas()
John Reck5ba09482015-05-18 15:13:30 -0700711 * @see #lockCanvas(android.graphics.Rect)
Romain Guy6be3d552011-07-14 18:08:37 -0700712 */
John Reck582f6bd2019-10-30 10:04:37 -0700713 public void unlockCanvasAndPost(@NonNull Canvas canvas) {
Romain Guy6be3d552011-07-14 18:08:37 -0700714 if (mCanvas != null && canvas == mCanvas) {
715 canvas.restoreToCount(mSaveCount);
716 mSaveCount = 0;
717
718 synchronized (mNativeWindowLock) {
719 nUnlockCanvasAndPost(mNativeWindow, mCanvas);
720 }
721 }
722 }
723
724 /**
Romain Guyaa6c24c2011-04-28 18:40:04 -0700725 * Returns the {@link SurfaceTexture} used by this view. This method
Romain Guy77a81162011-06-14 16:45:55 -0700726 * may return null if the view is not attached to a window or if the surface
727 * texture has not been initialized yet.
John Reck5ba09482015-05-18 15:13:30 -0700728 *
729 * @see #isAvailable()
Romain Guyaa6c24c2011-04-28 18:40:04 -0700730 */
John Reck582f6bd2019-10-30 10:04:37 -0700731 public @Nullable SurfaceTexture getSurfaceTexture() {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700732 return mSurface;
733 }
734
735 /**
Jamie Gennis2af35242012-04-05 11:44:30 -0700736 * Set the {@link SurfaceTexture} for this view to use. If a {@link
737 * SurfaceTexture} is already being used by this view, it is immediately
Mark Lue8691d12016-08-30 17:38:42 -0700738 * released and not usable any more. The {@link
Jamie Gennis2af35242012-04-05 11:44:30 -0700739 * SurfaceTextureListener#onSurfaceTextureDestroyed} callback is <b>not</b>
Jamie Gennis8a34d682012-04-17 16:01:34 -0700740 * called for the previous {@link SurfaceTexture}. Similarly, the {@link
741 * SurfaceTextureListener#onSurfaceTextureAvailable} callback is <b>not</b>
742 * called for the {@link SurfaceTexture} passed to setSurfaceTexture.
Jamie Gennis2af35242012-04-05 11:44:30 -0700743 *
744 * The {@link SurfaceTexture} object must be detached from all OpenGL ES
745 * contexts prior to calling this method.
746 *
747 * @param surfaceTexture The {@link SurfaceTexture} that the view should use.
748 * @see SurfaceTexture#detachFromGLContext()
Jamie Gennis2af35242012-04-05 11:44:30 -0700749 */
John Reck582f6bd2019-10-30 10:04:37 -0700750 public void setSurfaceTexture(@NonNull SurfaceTexture surfaceTexture) {
Jamie Gennis2af35242012-04-05 11:44:30 -0700751 if (surfaceTexture == null) {
752 throw new NullPointerException("surfaceTexture must not be null");
753 }
John Reck6d8371e2015-05-14 15:47:03 -0700754 if (surfaceTexture == mSurface) {
755 throw new IllegalArgumentException("Trying to setSurfaceTexture to " +
756 "the same SurfaceTexture that's already set.");
757 }
758 if (surfaceTexture.isReleased()) {
759 throw new IllegalArgumentException("Cannot setSurfaceTexture to a " +
760 "released SurfaceTexture");
761 }
Jamie Gennis2af35242012-04-05 11:44:30 -0700762 if (mSurface != null) {
John Reck7e237182016-08-11 10:43:14 -0700763 nDestroyNativeWindow();
Jamie Gennis2af35242012-04-05 11:44:30 -0700764 mSurface.release();
765 }
766 mSurface = surfaceTexture;
John Reck7e237182016-08-11 10:43:14 -0700767 nCreateNativeWindow(mSurface);
Naveen Kallafd69e2a2015-03-17 15:20:53 -0700768
John Reck8bc511e2015-05-18 15:11:52 -0700769 /*
770 * If the view is visible and we already made a layer, update the
771 * listener in the new surface to use the existing listener in the view.
772 * Otherwise this will be called when the view becomes visible or the
773 * layer is created
774 */
775 if (((mViewFlags & VISIBILITY_MASK) == VISIBLE) && mLayer != null) {
Naveen Kallafd69e2a2015-03-17 15:20:53 -0700776 mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler);
777 }
Jamie Gennis2af35242012-04-05 11:44:30 -0700778 mUpdateSurface = true;
779 invalidateParentIfNeeded();
780 }
781
782 /**
Romain Guyaa6c24c2011-04-28 18:40:04 -0700783 * Returns the {@link SurfaceTextureListener} currently associated with this
784 * texture view.
John Reck5ba09482015-05-18 15:13:30 -0700785 *
786 * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener)
Romain Guyaa6c24c2011-04-28 18:40:04 -0700787 * @see SurfaceTextureListener
788 */
John Reck582f6bd2019-10-30 10:04:37 -0700789 public @Nullable SurfaceTextureListener getSurfaceTextureListener() {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700790 return mListener;
791 }
792
793 /**
794 * Sets the {@link SurfaceTextureListener} used to listen to surface
795 * texture events.
John Reck5ba09482015-05-18 15:13:30 -0700796 *
797 * @see #getSurfaceTextureListener()
Romain Guyaa6c24c2011-04-28 18:40:04 -0700798 * @see SurfaceTextureListener
799 */
John Reck582f6bd2019-10-30 10:04:37 -0700800 public void setSurfaceTextureListener(@Nullable SurfaceTextureListener listener) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700801 mListener = listener;
802 }
803
Mathew Inwooda570dee2018-08-17 14:56:00 +0100804 @UnsupportedAppUsage
Jeff Brownc7282e52014-05-06 16:00:38 -0700805 private final SurfaceTexture.OnFrameAvailableListener mUpdateListener =
John Reck582f6bd2019-10-30 10:04:37 -0700806 surfaceTexture -> {
807 updateLayer();
808 invalidate();
809 };
Jeff Brownc7282e52014-05-06 16:00:38 -0700810
Romain Guyaa6c24c2011-04-28 18:40:04 -0700811 /**
812 * This listener can be used to be notified when the surface texture
813 * associated with this texture view is available.
814 */
John Reck582f6bd2019-10-30 10:04:37 -0700815 public interface SurfaceTextureListener {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700816 /**
817 * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use.
John Reck5ba09482015-05-18 15:13:30 -0700818 *
Romain Guyaa6c24c2011-04-28 18:40:04 -0700819 * @param surface The surface returned by
820 * {@link android.view.TextureView#getSurfaceTexture()}
Romain Guy451ce442011-06-10 15:40:36 -0700821 * @param width The width of the surface
822 * @param height The height of the surface
Romain Guyaa6c24c2011-04-28 18:40:04 -0700823 */
John Reck582f6bd2019-10-30 10:04:37 -0700824 void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height);
Romain Guy8f0095c2011-05-02 17:24:22 -0700825
826 /**
827 * Invoked when the {@link SurfaceTexture}'s buffers size changed.
John Reck5ba09482015-05-18 15:13:30 -0700828 *
Romain Guy8f0095c2011-05-02 17:24:22 -0700829 * @param surface The surface returned by
830 * {@link android.view.TextureView#getSurfaceTexture()}
831 * @param width The new width of the surface
832 * @param height The new height of the surface
833 */
John Reck582f6bd2019-10-30 10:04:37 -0700834 void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height);
Romain Guy451ce442011-06-10 15:40:36 -0700835
836 /**
837 * Invoked when the specified {@link SurfaceTexture} is about to be destroyed.
Grace Kloba402f0552011-08-09 18:47:17 -0700838 * If returns true, no rendering should happen inside the surface texture after this method
839 * is invoked. If returns false, the client needs to call {@link SurfaceTexture#release()}.
Romain Guy52b307e2012-10-07 17:55:17 -0700840 * Most applications should return true.
John Reck5ba09482015-05-18 15:13:30 -0700841 *
Romain Guy451ce442011-06-10 15:40:36 -0700842 * @param surface The surface about to be destroyed
843 */
John Reck582f6bd2019-10-30 10:04:37 -0700844 boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface);
Grace Klobacf559372011-06-22 23:05:40 -0700845
846 /**
847 * Invoked when the specified {@link SurfaceTexture} is updated through
848 * {@link SurfaceTexture#updateTexImage()}.
John Reck5ba09482015-05-18 15:13:30 -0700849 *
Grace Klobacf559372011-06-22 23:05:40 -0700850 * @param surface The surface just updated
851 */
John Reck582f6bd2019-10-30 10:04:37 -0700852 void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700853 }
Romain Guy8f0095c2011-05-02 17:24:22 -0700854
Mathew Inwooda570dee2018-08-17 14:56:00 +0100855 @UnsupportedAppUsage
Romain Guy6be3d552011-07-14 18:08:37 -0700856 private native void nCreateNativeWindow(SurfaceTexture surface);
Mathew Inwooda570dee2018-08-17 14:56:00 +0100857 @UnsupportedAppUsage
Romain Guy6be3d552011-07-14 18:08:37 -0700858 private native void nDestroyNativeWindow();
859
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000860 private static native boolean nLockCanvas(long nativeWindow, Canvas canvas, Rect dirty);
861 private static native void nUnlockCanvasAndPost(long nativeWindow, Canvas canvas);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700862}