blob: 161fe334118a1b426574fc4af1414b7aff17e347 [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
19import android.content.Context;
Romain Guy77a81162011-06-14 16:45:55 -070020import android.graphics.Bitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -070021import android.graphics.Canvas;
Romain Guy302a9df2011-08-16 13:55:02 -070022import android.graphics.Matrix;
Romain Guyaa6c24c2011-04-28 18:40:04 -070023import android.graphics.Paint;
Romain Guy6be3d552011-07-14 18:08:37 -070024import android.graphics.Rect;
Romain Guyaa6c24c2011-04-28 18:40:04 -070025import android.graphics.SurfaceTexture;
Romain Guy52c145f2012-05-17 18:06:23 -070026import android.os.Looper;
Romain Guyaa6c24c2011-04-28 18:40:04 -070027import android.util.AttributeSet;
28import android.util.Log;
29
30/**
31 * <p>A TextureView can be used to display a content stream. Such a content
32 * stream can for instance be a video or an OpenGL scene. The content stream
33 * can come from the application's process as well as a remote process.</p>
34 *
35 * <p>TextureView can only be used in a hardware accelerated window. When
36 * rendered in software, TextureView will draw nothing.</p>
37 *
38 * <p>Unlike {@link SurfaceView}, TextureView does not create a separate
39 * window but behaves as a regular View. This key difference allows a
40 * TextureView to be moved, transformed, animated, etc. For instance, you
41 * can make a TextureView semi-translucent by calling
42 * <code>myView.setAlpha(0.5f)</code>.</p>
43 *
44 * <p>Using a TextureView is simple: all you need to do is get its
45 * {@link SurfaceTexture}. The {@link SurfaceTexture} can then be used to
46 * render content. The following example demonstrates how to render the
47 * camera preview into a TextureView:</p>
48 *
49 * <pre>
50 * public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
51 * private Camera mCamera;
52 * private TextureView mTextureView;
53 *
Romain Guyaa6c24c2011-04-28 18:40:04 -070054 * protected void onCreate(Bundle savedInstanceState) {
55 * super.onCreate(savedInstanceState);
56 *
57 * mTextureView = new TextureView(this);
58 * mTextureView.setSurfaceTextureListener(this);
59 *
60 * setContentView(mTextureView);
61 * }
62 *
Romain Guy451ce442011-06-10 15:40:36 -070063 * public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Romain Guyaa6c24c2011-04-28 18:40:04 -070064 * mCamera = Camera.open();
65 *
66 * try {
67 * mCamera.setPreviewTexture(surface);
68 * mCamera.startPreview();
69 * } catch (IOException ioe) {
70 * // Something bad happened
71 * }
72 * }
Grace Klobacf559372011-06-22 23:05:40 -070073 *
Romain Guy8f0095c2011-05-02 17:24:22 -070074 * public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
75 * // Ignored, Camera does all the work for us
76 * }
Grace Klobacf559372011-06-22 23:05:40 -070077 *
Grace Kloba402f0552011-08-09 18:47:17 -070078 * public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Romain Guy451ce442011-06-10 15:40:36 -070079 * mCamera.stopPreview();
80 * mCamera.release();
Grace Kloba402f0552011-08-09 18:47:17 -070081 * return true;
Romain Guy451ce442011-06-10 15:40:36 -070082 * }
Grace Klobacf559372011-06-22 23:05:40 -070083 *
84 * public void onSurfaceTextureUpdated(SurfaceTexture surface) {
Romain Guy58f4edb2011-06-24 14:51:38 -070085 * // Invoked every time there's a new Camera preview frame
Grace Klobacf559372011-06-22 23:05:40 -070086 * }
Romain Guyaa6c24c2011-04-28 18:40:04 -070087 * }
88 * </pre>
89 *
90 * <p>A TextureView's SurfaceTexture can be obtained either by invoking
91 * {@link #getSurfaceTexture()} or by using a {@link SurfaceTextureListener}.
92 * It is important to know that a SurfaceTexture is available only after the
93 * TextureView is attached to a window (and {@link #onAttachedToWindow()} has
94 * been invoked.) It is therefore highly recommended you use a listener to
95 * be notified when the SurfaceTexture becomes available.</p>
96 *
Romain Guy462785f2011-09-27 17:42:10 -070097 * <p>It is important to note that only one producer can use the TextureView.
98 * For instance, if you use a TextureView to display the camera preview, you
99 * cannot use {@link #lockCanvas()} to draw onto the TextureView at the same
100 * time.</p>
101 *
Romain Guyaa6c24c2011-04-28 18:40:04 -0700102 * @see SurfaceView
103 * @see SurfaceTexture
104 */
105public class TextureView extends View {
Romain Guy77a81162011-06-14 16:45:55 -0700106 private static final String LOG_TAG = "TextureView";
107
Romain Guyaa6c24c2011-04-28 18:40:04 -0700108 private HardwareLayer mLayer;
109 private SurfaceTexture mSurface;
110 private SurfaceTextureListener mListener;
Romain Guy67603c62013-06-27 10:58:10 -0700111 private boolean mHadSurface;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700112
Romain Guya9489272011-06-22 20:58:11 -0700113 private boolean mOpaque = true;
Romain Guyc989d862011-06-22 14:53:39 -0700114
Romain Guy302a9df2011-08-16 13:55:02 -0700115 private final Matrix mMatrix = new Matrix();
116 private boolean mMatrixChanged;
117
Romain Guy58f4edb2011-06-24 14:51:38 -0700118 private final Object[] mLock = new Object[0];
119 private boolean mUpdateLayer;
Jamie Gennis2af35242012-04-05 11:44:30 -0700120 private boolean mUpdateSurface;
Romain Guy58f4edb2011-06-24 14:51:38 -0700121
Romain Guy8f0095c2011-05-02 17:24:22 -0700122 private SurfaceTexture.OnFrameAvailableListener mUpdateListener;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700123
Romain Guy6be3d552011-07-14 18:08:37 -0700124 private Canvas mCanvas;
125 private int mSaveCount;
126
127 private final Object[] mNativeWindowLock = new Object[0];
128 // Used from native code, do not write!
129 @SuppressWarnings({"UnusedDeclaration"})
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000130 private long mNativeWindow;
Romain Guy6be3d552011-07-14 18:08:37 -0700131
Romain Guyaa6c24c2011-04-28 18:40:04 -0700132 /**
133 * Creates a new TextureView.
134 *
135 * @param context The context to associate this view with.
136 */
137 public TextureView(Context context) {
138 super(context);
139 init();
140 }
141
142 /**
143 * Creates a new TextureView.
144 *
145 * @param context The context to associate this view with.
146 * @param attrs The attributes of the XML tag that is inflating the view.
147 */
148 @SuppressWarnings({"UnusedDeclaration"})
149 public TextureView(Context context, AttributeSet attrs) {
150 super(context, attrs);
151 init();
152 }
153
154 /**
155 * Creates a new TextureView.
156 *
157 * @param context The context to associate this view with.
158 * @param attrs The attributes of the XML tag that is inflating the view.
Alan Viverette617feb92013-09-09 18:09:13 -0700159 * @param defStyleAttr An attribute in the current theme that contains a
160 * reference to a style resource that supplies default values for
161 * the view. Can be 0 to not look for defaults.
Romain Guyaa6c24c2011-04-28 18:40:04 -0700162 */
163 @SuppressWarnings({"UnusedDeclaration"})
Alan Viverette617feb92013-09-09 18:09:13 -0700164 public TextureView(Context context, AttributeSet attrs, int defStyleAttr) {
165 super(context, attrs, defStyleAttr);
166 init();
167 }
168
169 /**
170 * Creates a new TextureView.
171 *
172 * @param context The context to associate this view with.
173 * @param attrs The attributes of the XML tag that is inflating the view.
174 * @param defStyleAttr An attribute in the current theme that contains a
175 * reference to a style resource that supplies default values for
176 * the view. Can be 0 to not look for defaults.
177 * @param defStyleRes A resource identifier of a style resource that
178 * supplies default values for the view, used only if
179 * defStyleAttr is 0 or can not be found in the theme. Can be 0
180 * to not look for defaults.
181 */
182 @SuppressWarnings({"UnusedDeclaration"})
183 public TextureView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
184 super(context, attrs, defStyleAttr, defStyleRes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700185 init();
186 }
187
188 private void init() {
189 mLayerPaint = new Paint();
190 }
191
Romain Guya9489272011-06-22 20:58:11 -0700192 /**
193 * {@inheritDoc}
194 */
195 @Override
196 public boolean isOpaque() {
197 return mOpaque;
198 }
199
200 /**
201 * Indicates whether the content of this TextureView is opaque. The
202 * content is assumed to be opaque by default.
203 *
204 * @param opaque True if the content of this TextureView is opaque,
205 * false otherwise
206 */
207 public void setOpaque(boolean opaque) {
208 if (opaque != mOpaque) {
209 mOpaque = opaque;
Romain Guya8a2f972012-04-12 16:33:44 -0700210 if (mLayer != null) {
Romain Guy88801b22012-10-05 14:58:33 -0700211 updateLayerAndInvalidate();
Romain Guya8a2f972012-04-12 16:33:44 -0700212 }
Romain Guya9489272011-06-22 20:58:11 -0700213 }
214 }
215
Romain Guyaa6c24c2011-04-28 18:40:04 -0700216 @Override
217 protected void onAttachedToWindow() {
218 super.onAttachedToWindow();
219
220 if (!isHardwareAccelerated()) {
Romain Guy77a81162011-06-14 16:45:55 -0700221 Log.w(LOG_TAG, "A TextureView or a subclass can only be "
Romain Guyaa6c24c2011-04-28 18:40:04 -0700222 + "used with hardware acceleration enabled.");
223 }
Romain Guy67603c62013-06-27 10:58:10 -0700224
225 if (mHadSurface) {
226 invalidate(true);
227 mHadSurface = false;
228 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700229 }
230
Romain Guy451ce442011-06-10 15:40:36 -0700231 @Override
232 protected void onDetachedFromWindow() {
233 super.onDetachedFromWindow();
Romain Guy33ed7382013-09-26 14:57:30 -0700234 if (mLayer != null) {
235 boolean success = executeHardwareAction(new Runnable() {
Romain Guy1ac47652012-04-11 18:15:20 -0700236 @Override
237 public void run() {
238 destroySurface();
239 }
240 });
241
242 if (!success) {
243 Log.w(LOG_TAG, "TextureView was not able to destroy its surface: " + this);
244 }
245 }
Romain Guy31f2c2e2011-11-21 10:55:41 -0800246 }
Romain Guy451ce442011-06-10 15:40:36 -0700247
Romain Guy31f2c2e2011-11-21 10:55:41 -0800248 private void destroySurface() {
Romain Guy80429c42011-06-24 17:20:32 -0700249 if (mLayer != null) {
Jamie Gennis2af35242012-04-05 11:44:30 -0700250 mSurface.detachFromGLContext();
Romain Guy52b307e2012-10-07 17:55:17 -0700251 // SurfaceTexture owns the texture name and detachFromGLContext
252 // should have deleted it
Romain Guyef09a212012-09-25 12:17:14 -0700253 mLayer.clearStorage();
Jamie Gennis2af35242012-04-05 11:44:30 -0700254
Grace Kloba402f0552011-08-09 18:47:17 -0700255 boolean shouldRelease = true;
Romain Guy451ce442011-06-10 15:40:36 -0700256 if (mListener != null) {
Grace Kloba402f0552011-08-09 18:47:17 -0700257 shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface);
Romain Guy451ce442011-06-10 15:40:36 -0700258 }
259
Romain Guy6be3d552011-07-14 18:08:37 -0700260 synchronized (mNativeWindowLock) {
261 nDestroyNativeWindow();
262 }
263
264 mLayer.destroy();
Grace Kloba402f0552011-08-09 18:47:17 -0700265 if (shouldRelease) mSurface.release();
Romain Guy451ce442011-06-10 15:40:36 -0700266 mSurface = null;
267 mLayer = null;
Romain Guy67603c62013-06-27 10:58:10 -0700268
269 mHadSurface = true;
Romain Guy451ce442011-06-10 15:40:36 -0700270 }
271 }
272
Romain Guyaa6c24c2011-04-28 18:40:04 -0700273 /**
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.
278 *
279 * @param layerType The ype of layer to use with this view, must be one of
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
287 public void setLayerType(int layerType, Paint paint) {
288 if (paint != mLayerPaint) {
Romain Guyc2079c92013-04-30 10:25:35 -0700289 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700290 invalidate();
291 }
292 }
293
294 /**
295 * Always returns {@link #LAYER_TYPE_HARDWARE}.
296 */
297 @Override
298 public int getLayerType() {
299 return LAYER_TYPE_HARDWARE;
300 }
301
Romain Guy59c7f802011-09-29 17:21:45 -0700302 @Override
303 boolean hasStaticLayer() {
304 return true;
305 }
306
Romain Guyaa6c24c2011-04-28 18:40:04 -0700307 /**
308 * Calling this method has no effect.
309 */
310 @Override
311 public void buildLayer() {
312 }
313
314 /**
315 * Subclasses of TextureView cannot do their own rendering
316 * with the {@link Canvas} object.
317 *
318 * @param canvas The Canvas to which the View is rendered.
319 */
320 @Override
321 public final void draw(Canvas canvas) {
Romain Guy52b307e2012-10-07 17:55:17 -0700322 // NOTE: Maintain this carefully (see View.java)
323 mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
324
Romain Guy58f4edb2011-06-24 14:51:38 -0700325 applyUpdate();
Alexandre Eliasc01391f2011-08-19 16:03:16 -0700326 applyTransformMatrix();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700327 }
328
329 /**
330 * Subclasses of TextureView cannot do their own rendering
331 * with the {@link Canvas} object.
332 *
333 * @param canvas The Canvas to which the View is rendered.
334 */
335 @Override
336 protected final void onDraw(Canvas canvas) {
337 }
338
339 @Override
Romain Guy8f0095c2011-05-02 17:24:22 -0700340 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
341 super.onSizeChanged(w, h, oldw, oldh);
342 if (mSurface != null) {
Mathias Agopian52a9a102013-08-02 01:38:38 -0700343 mSurface.setDefaultBufferSize(getWidth(), getHeight());
Romain Guy88801b22012-10-05 14:58:33 -0700344 updateLayer();
Romain Guy451ce442011-06-10 15:40:36 -0700345 if (mListener != null) {
346 mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight());
347 }
Romain Guy8f0095c2011-05-02 17:24:22 -0700348 }
349 }
350
351 @Override
Romain Guya998dff2012-03-23 18:58:36 -0700352 boolean destroyLayer(boolean valid) {
Romain Guy16260e72011-09-01 14:26:11 -0700353 return false;
354 }
355
Romain Guy1766b0e2011-11-21 18:28:49 -0800356 /**
357 * @hide
358 */
Romain Guy16260e72011-09-01 14:26:11 -0700359 @Override
Romain Guy31f2c2e2011-11-21 10:55:41 -0800360 protected void destroyHardwareResources() {
361 super.destroyHardwareResources();
362 destroySurface();
363 invalidateParentCaches();
364 invalidate(true);
365 }
366
367 @Override
Michael Jurka7e52caf2012-03-06 15:57:06 -0800368 HardwareLayer getHardwareLayer() {
Romain Guy52b307e2012-10-07 17:55:17 -0700369 // NOTE: Maintain these two lines very carefully (see View.java)
370 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
371 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
372
Romain Guyaa6c24c2011-04-28 18:40:04 -0700373 if (mLayer == null) {
Romain Guy58f4edb2011-06-24 14:51:38 -0700374 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
375 return null;
376 }
377
Romain Guya9489272011-06-22 20:58:11 -0700378 mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(mOpaque);
Jamie Gennis2af35242012-04-05 11:44:30 -0700379 if (!mUpdateSurface) {
Jamie Gennis8a34d682012-04-17 16:01:34 -0700380 // Create a new SurfaceTexture for the layer.
Jamie Gennis2af35242012-04-05 11:44:30 -0700381 mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
382 }
Mathias Agopian52a9a102013-08-02 01:38:38 -0700383 mSurface.setDefaultBufferSize(getWidth(), getHeight());
Jamie Gennis2af35242012-04-05 11:44:30 -0700384 nCreateNativeWindow(mSurface);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700385
Romain Guy8f0095c2011-05-02 17:24:22 -0700386 mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700387 @Override
388 public void onFrameAvailable(SurfaceTexture surfaceTexture) {
389 // Per SurfaceTexture's documentation, the callback may be invoked
390 // from an arbitrary thread
Romain Guy88801b22012-10-05 14:58:33 -0700391 updateLayer();
Romain Guy52c145f2012-05-17 18:06:23 -0700392
393 if (Looper.myLooper() == Looper.getMainLooper()) {
394 invalidate();
395 } else {
396 postInvalidate();
397 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700398 }
Romain Guy8f0095c2011-05-02 17:24:22 -0700399 };
400 mSurface.setOnFrameAvailableListener(mUpdateListener);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700401
Jamie Gennis8a34d682012-04-17 16:01:34 -0700402 if (mListener != null && !mUpdateSurface) {
Romain Guy451ce442011-06-10 15:40:36 -0700403 mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700404 }
Chet Haased15ebf22012-09-05 11:40:29 -0700405 mLayer.setLayerPaint(mLayerPaint);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700406 }
407
Jamie Gennis2af35242012-04-05 11:44:30 -0700408 if (mUpdateSurface) {
409 // Someone has requested that we use a specific SurfaceTexture, so
410 // tell mLayer about it and set the SurfaceTexture to use the
411 // current view size.
412 mUpdateSurface = false;
Romain Guy51f7c6b2012-05-21 16:32:59 -0700413
414 // Since we are updating the layer, force an update to ensure its
415 // parameters are correct (width, height, transform, etc.)
Romain Guy88801b22012-10-05 14:58:33 -0700416 updateLayer();
Romain Guy51f7c6b2012-05-21 16:32:59 -0700417 mMatrixChanged = true;
418
Jamie Gennis2af35242012-04-05 11:44:30 -0700419 mAttachInfo.mHardwareRenderer.setSurfaceTexture(mLayer, mSurface);
Mathias Agopian52a9a102013-08-02 01:38:38 -0700420 mSurface.setDefaultBufferSize(getWidth(), getHeight());
Jamie Gennis2af35242012-04-05 11:44:30 -0700421 }
422
Romain Guy58f4edb2011-06-24 14:51:38 -0700423 applyUpdate();
Alexandre Eliasc01391f2011-08-19 16:03:16 -0700424 applyTransformMatrix();
Romain Guy302a9df2011-08-16 13:55:02 -0700425
Romain Guyaa6c24c2011-04-28 18:40:04 -0700426 return mLayer;
427 }
428
Romain Guy8f0095c2011-05-02 17:24:22 -0700429 @Override
430 protected void onVisibilityChanged(View changedView, int visibility) {
431 super.onVisibilityChanged(changedView, visibility);
432
433 if (mSurface != null) {
434 // When the view becomes invisible, stop updating it, it's a waste of CPU
435 // To cancel updates, the easiest thing to do is simply to remove the
436 // updates listener
437 if (visibility == VISIBLE) {
438 mSurface.setOnFrameAvailableListener(mUpdateListener);
Romain Guy88801b22012-10-05 14:58:33 -0700439 updateLayerAndInvalidate();
Romain Guy8f0095c2011-05-02 17:24:22 -0700440 } else {
441 mSurface.setOnFrameAvailableListener(null);
442 }
443 }
444 }
445
Romain Guya9489272011-06-22 20:58:11 -0700446 private void updateLayer() {
Romain Guy88801b22012-10-05 14:58:33 -0700447 synchronized (mLock) {
448 mUpdateLayer = true;
449 }
450 }
451
452 private void updateLayerAndInvalidate() {
453 synchronized (mLock) {
454 mUpdateLayer = true;
455 }
Romain Guy58f4edb2011-06-24 14:51:38 -0700456 invalidate();
457 }
Jamie Gennis2af35242012-04-05 11:44:30 -0700458
Romain Guy58f4edb2011-06-24 14:51:38 -0700459 private void applyUpdate() {
460 if (mLayer == null) {
Romain Guya9489272011-06-22 20:58:11 -0700461 return;
462 }
463
Romain Guy58f4edb2011-06-24 14:51:38 -0700464 synchronized (mLock) {
465 if (mUpdateLayer) {
466 mUpdateLayer = false;
467 } else {
468 return;
469 }
470 }
471
Romain Guy02ccac62011-06-24 13:20:23 -0700472 mLayer.update(getWidth(), getHeight(), mOpaque);
Romain Guya9489272011-06-22 20:58:11 -0700473
Grace Klobacf559372011-06-22 23:05:40 -0700474 if (mListener != null) {
475 mListener.onSurfaceTextureUpdated(mSurface);
476 }
Romain Guya9489272011-06-22 20:58:11 -0700477 }
478
Romain Guyaa6c24c2011-04-28 18:40:04 -0700479 /**
Romain Guy302a9df2011-08-16 13:55:02 -0700480 * <p>Sets the transform to associate with this texture view.
481 * The specified transform applies to the underlying surface
482 * texture and does not affect the size or position of the view
483 * itself, only of its content.</p>
484 *
485 * <p>Some transforms might prevent the content from drawing
486 * all the pixels contained within this view's bounds. In such
487 * situations, make sure this texture view is not marked opaque.</p>
488 *
489 * @param transform The transform to apply to the content of
490 * this view.
491 *
492 * @see #getTransform(android.graphics.Matrix)
493 * @see #isOpaque()
494 * @see #setOpaque(boolean)
495 */
496 public void setTransform(Matrix transform) {
497 mMatrix.set(transform);
498 mMatrixChanged = true;
Alexandre Eliasc01391f2011-08-19 16:03:16 -0700499 invalidateParentIfNeeded();
Romain Guy302a9df2011-08-16 13:55:02 -0700500 }
501
502 /**
503 * Returns the transform associated with this texture view.
504 *
505 * @param transform The {@link Matrix} in which to copy the current
506 * transform. Can be null.
507 *
508 * @return The specified matrix if not null or a new {@link Matrix}
509 * instance otherwise.
510 *
511 * @see #setTransform(android.graphics.Matrix)
512 */
513 public Matrix getTransform(Matrix transform) {
514 if (transform == null) {
515 transform = new Matrix();
516 }
517
518 transform.set(mMatrix);
519
520 return transform;
521 }
522
Alexandre Eliasc01391f2011-08-19 16:03:16 -0700523 private void applyTransformMatrix() {
Romain Guy51f7c6b2012-05-21 16:32:59 -0700524 if (mMatrixChanged && mLayer != null) {
Alexandre Eliasc01391f2011-08-19 16:03:16 -0700525 mLayer.setTransform(mMatrix);
526 mMatrixChanged = false;
527 }
528 }
529
Romain Guy302a9df2011-08-16 13:55:02 -0700530 /**
Romain Guy77a81162011-06-14 16:45:55 -0700531 * <p>Returns a {@link android.graphics.Bitmap} representation of the content
532 * of the associated surface texture. If the surface texture is not available,
533 * this method returns null.</p>
534 *
535 * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
536 * pixel format and its dimensions are the same as this view's.</p>
537 *
538 * <p><strong>Do not</strong> invoke this method from a drawing method
539 * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
540 *
Romain Guyd6b2a002011-06-17 17:45:59 -0700541 * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
542 *
Romain Guy77a81162011-06-14 16:45:55 -0700543 * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
544 * texture is not available or the width &lt;= 0 or the height &lt;= 0
545 *
546 * @see #isAvailable()
547 * @see #getBitmap(android.graphics.Bitmap)
548 * @see #getBitmap(int, int)
549 */
550 public Bitmap getBitmap() {
551 return getBitmap(getWidth(), getHeight());
552 }
553
554 /**
555 * <p>Returns a {@link android.graphics.Bitmap} representation of the content
556 * of the associated surface texture. If the surface texture is not available,
557 * this method returns null.</p>
558 *
559 * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
560 * pixel format.</p>
561 *
562 * <p><strong>Do not</strong> invoke this method from a drawing method
563 * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
564 *
Romain Guyd6b2a002011-06-17 17:45:59 -0700565 * <p>If an error occurs during the copy, an empty bitmap will be returned.</p>
566 *
Romain Guy77a81162011-06-14 16:45:55 -0700567 * @param width The width of the bitmap to create
568 * @param height The height of the bitmap to create
569 *
570 * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
571 * texture is not available or width is &lt;= 0 or height is &lt;= 0
572 *
573 * @see #isAvailable()
574 * @see #getBitmap(android.graphics.Bitmap)
575 * @see #getBitmap()
576 */
577 public Bitmap getBitmap(int width, int height) {
578 if (isAvailable() && width > 0 && height > 0) {
Dianne Hackborndde331c2012-08-03 14:01:57 -0700579 return getBitmap(Bitmap.createBitmap(getResources().getDisplayMetrics(),
580 width, height, Bitmap.Config.ARGB_8888));
Romain Guy77a81162011-06-14 16:45:55 -0700581 }
582 return null;
583 }
584
585 /**
586 * <p>Copies the content of this view's surface texture into the specified
587 * bitmap. If the surface texture is not available, the copy is not executed.
588 * The content of the surface texture will be scaled to fit exactly inside
589 * the specified bitmap.</p>
590 *
591 * <p><strong>Do not</strong> invoke this method from a drawing method
592 * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
593 *
Romain Guyd6b2a002011-06-17 17:45:59 -0700594 * <p>If an error occurs, the bitmap is left unchanged.</p>
595 *
Romain Guy77a81162011-06-14 16:45:55 -0700596 * @param bitmap The bitmap to copy the content of the surface texture into,
597 * cannot be null, all configurations are supported
598 *
599 * @return The bitmap specified as a parameter
600 *
601 * @see #isAvailable()
602 * @see #getBitmap(int, int)
603 * @see #getBitmap()
Romain Guy589b0bb2011-10-10 13:57:47 -0700604 *
605 * @throws IllegalStateException if the hardware rendering context cannot be
606 * acquired to capture the bitmap
Romain Guy77a81162011-06-14 16:45:55 -0700607 */
608 public Bitmap getBitmap(Bitmap bitmap) {
609 if (bitmap != null && isAvailable()) {
Romain Guy589b0bb2011-10-10 13:57:47 -0700610 AttachInfo info = mAttachInfo;
611 if (info != null && info.mHardwareRenderer != null &&
612 info.mHardwareRenderer.isEnabled()) {
613 if (!info.mHardwareRenderer.validate()) {
614 throw new IllegalStateException("Could not acquire hardware rendering context");
615 }
616 }
617
618 applyUpdate();
619 applyTransformMatrix();
620
Romain Guy78245f72012-05-11 10:01:42 -0700621 // This case can happen if the app invokes setSurfaceTexture() before
622 // we are able to create the hardware layer. We can safely initialize
623 // the layer here thanks to the validate() call at the beginning of
624 // this method
625 if (mLayer == null && mUpdateSurface) {
626 getHardwareLayer();
627 }
628
629 if (mLayer != null) {
630 mLayer.copyInto(bitmap);
631 }
Romain Guy77a81162011-06-14 16:45:55 -0700632 }
633 return bitmap;
634 }
635
636 /**
637 * Returns true if the {@link SurfaceTexture} associated with this
638 * TextureView is available for rendering. When this method returns
639 * true, {@link #getSurfaceTexture()} returns a valid surface texture.
640 */
641 public boolean isAvailable() {
642 return mSurface != null;
643 }
644
645 /**
Romain Guy6be3d552011-07-14 18:08:37 -0700646 * <p>Start editing the pixels in the surface. The returned Canvas can be used
647 * to draw into the surface's bitmap. A null is returned if the surface has
648 * not been created or otherwise cannot be edited. You will usually need
649 * to implement
650 * {@link SurfaceTextureListener#onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int)}
651 * to find out when the Surface is available for use.</p>
652 *
653 * <p>The content of the Surface is never preserved between unlockCanvas()
654 * and lockCanvas(), for this reason, every pixel within the Surface area
655 * must be written. The only exception to this rule is when a dirty
656 * rectangle is specified, in which case, non-dirty pixels will be
657 * preserved.</p>
658 *
Romain Guy462785f2011-09-27 17:42:10 -0700659 * <p>This method can only be used if the underlying surface is not already
660 * owned by another producer. For instance, if the TextureView is being used
661 * to render the camera's preview you cannot invoke this method.</p>
662 *
Romain Guy6be3d552011-07-14 18:08:37 -0700663 * @return A Canvas used to draw into the surface.
664 *
665 * @see #lockCanvas(android.graphics.Rect)
666 * @see #unlockCanvasAndPost(android.graphics.Canvas)
667 */
668 public Canvas lockCanvas() {
669 return lockCanvas(null);
670 }
671
672 /**
673 * Just like {@link #lockCanvas()} but allows specification of a dirty
674 * rectangle. Every pixel within that rectangle must be written; however
675 * pixels outside the dirty rectangle will be preserved by the next call
676 * to lockCanvas().
Romain Guy53bacf52013-04-30 11:30:10 -0700677 *
678 * This method can return null if the underlying surface texture is not
679 * available (see {@link #isAvailable()} or if the surface texture is
680 * already connected to an image producer (for instance: the camera,
681 * OpenGL, a media player, etc.)
Romain Guy6be3d552011-07-14 18:08:37 -0700682 *
683 * @param dirty Area of the surface that will be modified.
684
685 * @return A Canvas used to draw into the surface.
686 *
687 * @see #lockCanvas()
Romain Guy53bacf52013-04-30 11:30:10 -0700688 * @see #unlockCanvasAndPost(android.graphics.Canvas)
689 * @see #isAvailable()
Romain Guy6be3d552011-07-14 18:08:37 -0700690 */
691 public Canvas lockCanvas(Rect dirty) {
692 if (!isAvailable()) return null;
693
694 if (mCanvas == null) {
695 mCanvas = new Canvas();
696 }
697
698 synchronized (mNativeWindowLock) {
Romain Guy53bacf52013-04-30 11:30:10 -0700699 if (!nLockCanvas(mNativeWindow, mCanvas, dirty)) {
700 return null;
701 }
Romain Guy6be3d552011-07-14 18:08:37 -0700702 }
703 mSaveCount = mCanvas.save();
704
705 return mCanvas;
706 }
707
708 /**
709 * Finish editing pixels in the surface. After this call, the surface's
710 * current pixels will be shown on the screen, but its content is lost,
711 * in particular there is no guarantee that the content of the Surface
712 * will remain unchanged when lockCanvas() is called again.
713 *
714 * @param canvas The Canvas previously returned by lockCanvas()
715 *
716 * @see #lockCanvas()
717 * @see #lockCanvas(android.graphics.Rect)
718 */
719 public void unlockCanvasAndPost(Canvas canvas) {
720 if (mCanvas != null && canvas == mCanvas) {
721 canvas.restoreToCount(mSaveCount);
722 mSaveCount = 0;
723
724 synchronized (mNativeWindowLock) {
725 nUnlockCanvasAndPost(mNativeWindow, mCanvas);
726 }
727 }
728 }
729
730 /**
Romain Guyaa6c24c2011-04-28 18:40:04 -0700731 * Returns the {@link SurfaceTexture} used by this view. This method
Romain Guy77a81162011-06-14 16:45:55 -0700732 * may return null if the view is not attached to a window or if the surface
733 * texture has not been initialized yet.
734 *
735 * @see #isAvailable()
Romain Guyaa6c24c2011-04-28 18:40:04 -0700736 */
737 public SurfaceTexture getSurfaceTexture() {
738 return mSurface;
739 }
740
741 /**
Jamie Gennis2af35242012-04-05 11:44:30 -0700742 * Set the {@link SurfaceTexture} for this view to use. If a {@link
743 * SurfaceTexture} is already being used by this view, it is immediately
744 * released and not be usable any more. The {@link
745 * SurfaceTextureListener#onSurfaceTextureDestroyed} callback is <b>not</b>
Jamie Gennis8a34d682012-04-17 16:01:34 -0700746 * called for the previous {@link SurfaceTexture}. Similarly, the {@link
747 * SurfaceTextureListener#onSurfaceTextureAvailable} callback is <b>not</b>
748 * called for the {@link SurfaceTexture} passed to setSurfaceTexture.
Jamie Gennis2af35242012-04-05 11:44:30 -0700749 *
750 * The {@link SurfaceTexture} object must be detached from all OpenGL ES
751 * contexts prior to calling this method.
752 *
753 * @param surfaceTexture The {@link SurfaceTexture} that the view should use.
754 * @see SurfaceTexture#detachFromGLContext()
Jamie Gennis2af35242012-04-05 11:44:30 -0700755 */
756 public void setSurfaceTexture(SurfaceTexture surfaceTexture) {
757 if (surfaceTexture == null) {
758 throw new NullPointerException("surfaceTexture must not be null");
759 }
760 if (mSurface != null) {
761 mSurface.release();
762 }
763 mSurface = surfaceTexture;
764 mUpdateSurface = true;
765 invalidateParentIfNeeded();
766 }
767
768 /**
Romain Guyaa6c24c2011-04-28 18:40:04 -0700769 * Returns the {@link SurfaceTextureListener} currently associated with this
770 * texture view.
771 *
772 * @see #setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener)
773 * @see SurfaceTextureListener
774 */
775 public SurfaceTextureListener getSurfaceTextureListener() {
776 return mListener;
777 }
778
779 /**
780 * Sets the {@link SurfaceTextureListener} used to listen to surface
781 * texture events.
782 *
783 * @see #getSurfaceTextureListener()
784 * @see SurfaceTextureListener
785 */
786 public void setSurfaceTextureListener(SurfaceTextureListener listener) {
787 mListener = listener;
788 }
789
790 /**
791 * This listener can be used to be notified when the surface texture
792 * associated with this texture view is available.
793 */
794 public static interface SurfaceTextureListener {
795 /**
796 * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use.
797 *
798 * @param surface The surface returned by
799 * {@link android.view.TextureView#getSurfaceTexture()}
Romain Guy451ce442011-06-10 15:40:36 -0700800 * @param width The width of the surface
801 * @param height The height of the surface
Romain Guyaa6c24c2011-04-28 18:40:04 -0700802 */
Romain Guy451ce442011-06-10 15:40:36 -0700803 public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height);
Romain Guy8f0095c2011-05-02 17:24:22 -0700804
805 /**
806 * Invoked when the {@link SurfaceTexture}'s buffers size changed.
807 *
808 * @param surface The surface returned by
809 * {@link android.view.TextureView#getSurfaceTexture()}
810 * @param width The new width of the surface
811 * @param height The new height of the surface
812 */
813 public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height);
Romain Guy451ce442011-06-10 15:40:36 -0700814
815 /**
816 * Invoked when the specified {@link SurfaceTexture} is about to be destroyed.
Grace Kloba402f0552011-08-09 18:47:17 -0700817 * If returns true, no rendering should happen inside the surface texture after this method
818 * is invoked. If returns false, the client needs to call {@link SurfaceTexture#release()}.
Romain Guy52b307e2012-10-07 17:55:17 -0700819 * Most applications should return true.
Romain Guy451ce442011-06-10 15:40:36 -0700820 *
821 * @param surface The surface about to be destroyed
822 */
Grace Kloba402f0552011-08-09 18:47:17 -0700823 public boolean onSurfaceTextureDestroyed(SurfaceTexture surface);
Grace Klobacf559372011-06-22 23:05:40 -0700824
825 /**
826 * Invoked when the specified {@link SurfaceTexture} is updated through
827 * {@link SurfaceTexture#updateTexImage()}.
828 *
829 * @param surface The surface just updated
830 */
831 public void onSurfaceTextureUpdated(SurfaceTexture surface);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700832 }
Romain Guy8f0095c2011-05-02 17:24:22 -0700833
Romain Guy6be3d552011-07-14 18:08:37 -0700834 private native void nCreateNativeWindow(SurfaceTexture surface);
835 private native void nDestroyNativeWindow();
836
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000837 private static native boolean nLockCanvas(long nativeWindow, Canvas canvas, Rect dirty);
838 private static native void nUnlockCanvasAndPost(long nativeWindow, Canvas canvas);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700839}