}
}
+ GLES20TextureLayer(SurfaceTexture surface, boolean isOpaque) {
+ this(isOpaque);
+ mSurface = surface;
+ mSurface.attachToGLContext(mTexture);
+ }
+
@Override
boolean isValid() {
return mLayer != 0 && mTexture != 0;
return mSurface;
}
+ void setSurfaceTexture(SurfaceTexture surfaceTexture) {
+ if (mSurface != null) {
+ mSurface.release();
+ }
+ mSurface = surfaceTexture;
+ mSurface.attachToGLContext(mTexture);
+ }
+
@Override
void update(int width, int height, boolean isOpaque) {
super.update(width, height, isOpaque);
* @param isOpaque Whether the layer should be opaque or not
*
* @return A hardware layer
- */
+ */
abstract HardwareLayer createHardwareLayer(boolean isOpaque);
-
+
/**
* Creates a new hardware layer.
*
abstract SurfaceTexture createSurfaceTexture(HardwareLayer layer);
/**
+ * Sets the {@link android.graphics.SurfaceTexture} that will be used to
+ * render into the specified hardware layer.
+ *
+ * @param layer The layer to render into using a {@link android.graphics.SurfaceTexture}
+ * @param surfaceTexture The {@link android.graphics.SurfaceTexture} to use for the layer
+ */
+ abstract void setSurfaceTexture(HardwareLayer layer, SurfaceTexture surfaceTexture);
+
+ /**
* Initializes the hardware renderer for the specified surface and setup the
* renderer for drawing, if needed. This is invoked when the ViewAncestor has
* potentially lost the hardware renderer. The hardware renderer should be
}
@Override
+ void setSurfaceTexture(HardwareLayer layer, SurfaceTexture surfaceTexture) {
+ ((GLES20TextureLayer) layer).setSurfaceTexture(surfaceTexture);
+ }
+
+ @Override
void destroyLayers(View view) {
if (view != null && isEnabled() && checkCurrent() != SURFACE_STATE_ERROR) {
destroyHardwareLayer(view);
private final Object[] mLock = new Object[0];
private boolean mUpdateLayer;
+ private boolean mUpdateSurface;
private SurfaceTexture.OnFrameAvailableListener mUpdateListener;
private void destroySurface() {
if (mLayer != null) {
+ mSurface.detachFromGLContext();
+
boolean shouldRelease = true;
if (mListener != null) {
shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface);
}
mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(mOpaque);
- mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
+ if (!mUpdateSurface) {
+ // We already have a SurfaceTexture to use, and we will pass it
+ // to mLayer below.
+ mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
+ }
nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
- nCreateNativeWindow(mSurface);
+ nCreateNativeWindow(mSurface);
mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() {
@Override
}
}
+ if (mUpdateSurface) {
+ // Someone has requested that we use a specific SurfaceTexture, so
+ // tell mLayer about it and set the SurfaceTexture to use the
+ // current view size.
+ mUpdateSurface = false;
+ mAttachInfo.mHardwareRenderer.setSurfaceTexture(mLayer, mSurface);
+ nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
+ }
+
applyUpdate();
applyTransformMatrix();
mUpdateLayer = true;
invalidate();
}
-
+
private void applyUpdate() {
if (mLayer == null) {
return;
}
/**
+ * Set the {@link SurfaceTexture} for this view to use. If a {@link
+ * SurfaceTexture} is already being used by this view, it is immediately
+ * released and not be usable any more. The {@link
+ * SurfaceTextureListener#onSurfaceTextureDestroyed} callback is <b>not</b>
+ * called.
+ *
+ * The {@link SurfaceTexture} object must be detached from all OpenGL ES
+ * contexts prior to calling this method.
+ *
+ * @param surfaceTexture The {@link SurfaceTexture} that the view should use.
+ * @see SurfaceTexture#detachFromGLContext()
+ * @hide
+ */
+ public void setSurfaceTexture(SurfaceTexture surfaceTexture) {
+ if (surfaceTexture == null) {
+ throw new NullPointerException("surfaceTexture must not be null");
+ }
+ if (mSurface != null) {
+ mSurface.release();
+ }
+ mSurface = surfaceTexture;
+ mUpdateSurface = true;
+ invalidateParentIfNeeded();
+ }
+
+ /**
* Returns the {@link SurfaceTextureListener} currently associated with this
* texture view.
*