Android: Generalize and make TextureBufferImpl public

This CL generalizes TextureBufferImpl so it's useful from other contexts than
from a SurfaceTextureHelper, and fixes a bug in cropAndScale(). It also exposes
the class in the api so that clients don't have to duplicate the logic.

Bug: None
Change-Id: Ib82aa8bee025ec14de74a7be9d91fd4e5298a248
Reviewed-on: https://webrtc-review.googlesource.com/69819
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22875}
This commit is contained in:
Magnus Jedvert
2018-04-16 13:31:28 +02:00
committed by Commit Bot
parent e31be15d47
commit 28111d7fa0
5 changed files with 26 additions and 35 deletions

View File

@ -766,11 +766,11 @@ rtc_android_library("video_api_java") {
"api/org/webrtc/JavaI420Buffer.java", "api/org/webrtc/JavaI420Buffer.java",
"api/org/webrtc/RendererCommon.java", "api/org/webrtc/RendererCommon.java",
"api/org/webrtc/SurfaceTextureHelper.java", "api/org/webrtc/SurfaceTextureHelper.java",
"api/org/webrtc/TextureBufferImpl.java",
"api/org/webrtc/YuvConverter.java", "api/org/webrtc/YuvConverter.java",
"api/org/webrtc/YuvHelper.java", "api/org/webrtc/YuvHelper.java",
"src/java/org/webrtc/EglBase10.java", "src/java/org/webrtc/EglBase10.java",
"src/java/org/webrtc/EglBase14.java", "src/java/org/webrtc/EglBase14.java",
"src/java/org/webrtc/TextureBufferImpl.java",
] ]
deps = [ deps = [

View File

@ -320,12 +320,7 @@ public class SurfaceTextureHelper {
* buffer calls returnTextureFrame() when it is released. * buffer calls returnTextureFrame() when it is released.
*/ */
public TextureBuffer createTextureBuffer(int width, int height, Matrix transformMatrix) { public TextureBuffer createTextureBuffer(int width, int height, Matrix transformMatrix) {
return new TextureBufferImpl( return new TextureBufferImpl(width, height, TextureBuffer.Type.OES, oesTextureId,
width, height, TextureBuffer.Type.OES, oesTextureId, transformMatrix, this, new Runnable() { transformMatrix, handler, yuvConverter, this ::returnTextureFrame);
@Override
public void run() {
returnTextureFrame();
}
});
} }
} }

View File

@ -11,31 +11,32 @@
package org.webrtc; package org.webrtc;
import android.graphics.Matrix; import android.graphics.Matrix;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import android.os.Handler;
/** /**
* Android texture buffer backed by a SurfaceTextureHelper's texture. The buffer calls * Android texture buffer that glues together the necessary information together with a generic
* |releaseCallback| when it is released. * release callback. ToI420() is implemented by providing a Handler and a YuvConverter.
*/ */
class TextureBufferImpl implements VideoFrame.TextureBuffer { public class TextureBufferImpl implements VideoFrame.TextureBuffer {
private final int width; private final int width;
private final int height; private final int height;
private final Type type; private final Type type;
private final int id; private final int id;
private final Matrix transformMatrix; private final Matrix transformMatrix;
private final SurfaceTextureHelper surfaceTextureHelper; private final Handler toI420Handler;
private final YuvConverter yuvConverter;
private final RefCountDelegate refCountDelegate; private final RefCountDelegate refCountDelegate;
public TextureBufferImpl(int width, int height, Type type, int id, Matrix transformMatrix, public TextureBufferImpl(int width, int height, Type type, int id, Matrix transformMatrix,
SurfaceTextureHelper surfaceTextureHelper, @Nullable Runnable releaseCallback) { Handler toI420Handler, YuvConverter yuvConverter, @Nullable Runnable releaseCallback) {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.type = type; this.type = type;
this.id = id; this.id = id;
this.transformMatrix = transformMatrix; this.transformMatrix = transformMatrix;
this.surfaceTextureHelper = surfaceTextureHelper; this.toI420Handler = toI420Handler;
this.yuvConverter = yuvConverter;
this.refCountDelegate = new RefCountDelegate(releaseCallback); this.refCountDelegate = new RefCountDelegate(releaseCallback);
} }
@ -66,7 +67,8 @@ class TextureBufferImpl implements VideoFrame.TextureBuffer {
@Override @Override
public VideoFrame.I420Buffer toI420() { public VideoFrame.I420Buffer toI420() {
return surfaceTextureHelper.textureToYuv(this); return ThreadUtils.invokeAtFrontUninterruptibly(
toI420Handler, () -> yuvConverter.convert(this));
} }
@Override @Override
@ -82,17 +84,12 @@ class TextureBufferImpl implements VideoFrame.TextureBuffer {
@Override @Override
public VideoFrame.Buffer cropAndScale( public VideoFrame.Buffer cropAndScale(
int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) { int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) {
retain(); final Matrix newMatrix = new Matrix(transformMatrix);
Matrix newMatrix = new Matrix(transformMatrix); newMatrix.preTranslate(cropX / (float) width, cropY / (float) height);
newMatrix.postScale(cropWidth / (float) width, cropHeight / (float) height); newMatrix.preScale(cropWidth / (float) width, cropHeight / (float) height);
newMatrix.postTranslate(cropX / (float) width, cropY / (float) height);
retain();
return new TextureBufferImpl( return new TextureBufferImpl(
scaleWidth, scaleHeight, type, id, newMatrix, surfaceTextureHelper, new Runnable() { scaleWidth, scaleHeight, type, id, newMatrix, toI420Handler, yuvConverter, this ::release);
@Override
public void run() {
release();
}
});
} }
} }

View File

@ -171,10 +171,10 @@ public class VideoRenderer {
yuvStrides[1], yuvPlanes[2], yuvStrides[2], yuvStrides[1], yuvPlanes[2], yuvStrides[2],
() -> { VideoRenderer.renderFrameDone(this); }); () -> { VideoRenderer.renderFrameDone(this); });
} else { } else {
// Note: surfaceTextureHelper being null means calling toI420 will crash. // Note: No Handler or YuvConverter means calling toI420 will crash.
buffer = new TextureBufferImpl(width, height, VideoFrame.TextureBuffer.Type.OES, textureId, buffer = new TextureBufferImpl(width, height, VideoFrame.TextureBuffer.Type.OES, textureId,
RendererCommon.convertMatrixToAndroidGraphicsMatrix(samplingMatrix), RendererCommon.convertMatrixToAndroidGraphicsMatrix(samplingMatrix), null /* handler */,
null /* surfaceTextureHelper */, () -> { VideoRenderer.renderFrameDone(this); }); null /* yuvConverter */, () -> VideoRenderer.renderFrameDone(this));
} }
return new VideoFrame(buffer, rotationDegree, 0 /* timestampNs */); return new VideoFrame(buffer, rotationDegree, 0 /* timestampNs */);
} }

View File

@ -18,8 +18,8 @@ import org.webrtc.VideoFrame.I420Buffer;
import org.webrtc.VideoFrame.TextureBuffer; import org.webrtc.VideoFrame.TextureBuffer;
/** /**
* Class for converting OES textures to a YUV ByteBuffer. It should be constructed on a thread with * Class for converting OES textures to a YUV ByteBuffer. It can be constructed on any thread, but
* an active EGL context, and only be used from that thread. * should only be operated from a single thread with an active EGL context.
*/ */
public class YuvConverter { public class YuvConverter {
// Vertex coordinates in Normalized Device Coordinates, i.e. // Vertex coordinates in Normalized Device Coordinates, i.e.
@ -111,7 +111,7 @@ public class YuvConverter {
// clang-format on // clang-format on
private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.ThreadChecker(); private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.ThreadChecker();
private final GlTextureFrameBuffer textureFrameBuffer; private final GlTextureFrameBuffer textureFrameBuffer = new GlTextureFrameBuffer(GLES20.GL_RGBA);
private TextureBuffer.Type shaderTextureType; private TextureBuffer.Type shaderTextureType;
private GlShader shader; private GlShader shader;
private int texMatrixLoc; private int texMatrixLoc;
@ -123,8 +123,7 @@ public class YuvConverter {
* This class should be constructed on a thread that has an active EGL context. * This class should be constructed on a thread that has an active EGL context.
*/ */
public YuvConverter() { public YuvConverter() {
threadChecker.checkIsOnValidThread(); threadChecker.detachThread();
textureFrameBuffer = new GlTextureFrameBuffer(GLES20.GL_RGBA);
} }
/** Converts the texture buffer to I420. */ /** Converts the texture buffer to I420. */