Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Unified Diff: talk/app/webrtc/java/android/org/webrtc/SurfaceTextureHelper.java

Issue 1460703002: Implement AndroidTextureBuffer::NativeToI420. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Get a shared ref to the java SurfaceTectureHelper. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: talk/app/webrtc/java/android/org/webrtc/SurfaceTextureHelper.java
diff --git a/talk/app/webrtc/java/android/org/webrtc/SurfaceTextureHelper.java b/talk/app/webrtc/java/android/org/webrtc/SurfaceTextureHelper.java
index 2df7716446cb11bd5e2560feb87b9fe879ef038b..31833c598b3564bae4e22f06e78efc62a35399b0 100644
--- a/talk/app/webrtc/java/android/org/webrtc/SurfaceTextureHelper.java
+++ b/talk/app/webrtc/java/android/org/webrtc/SurfaceTextureHelper.java
@@ -35,6 +35,10 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.os.SystemClock;
+import org.webrtc.Logging;
+
+import java.nio.ByteBuffer;
+import java.nio.FloatBuffer;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -94,11 +98,305 @@ class SurfaceTextureHelper {
});
}
+ // State for RGBA conversion, instantiated on demand.
+ static private class ConvertRGBA {
magjed_webrtc 2015/12/03 12:33:58 I don't want to land a ConvertToRGBA until we need
nisse-webrtc 2015/12/03 13:35:20 I'll do. I wonder if it's worth the effort to keep
magjed_webrtc 2015/12/03 14:30:46 Yes, do that :) It is also saved in the patch hist
+ private final EglBase eglBase;
+ private GlRectDrawer drawer;
+ ConvertRGBA (EGLContext sharedContext) {
+ eglBase = new EglBase(sharedContext, EglBase.CONFIG_PIXEL_BUFFER);
+ drawer = null;
+ }
+
+ public void convert(ByteBuffer buf,
+ int width, int height, int textureId, float [] transformMatrix) {
+ synchronized(this) {
+ int size = 4 * width * height;
+ if (buf.capacity() < size) {
+ Logging.e(TAG, "needed size: " + size + ", available size: " + buf.capacity());
+ throw new IllegalStateException("ConvertRGBA.convert called with too small buffer");
+ }
+ // Produce a frame buffer starting at top-left corner, not
+ // bottom-left.
+ transformMatrix =
+ RendererCommon.multiplyMatrices(transformMatrix,
+ RendererCommon.verticalFlipMatrix());
+
+ try {
+ // Reuse surface, if possible.
+ if (eglBase.hasSurface()) {
+ if (eglBase.surfaceWidth() != width ||
+ eglBase.surfaceHeight() != height){
+ eglBase.releaseSurface();
+ eglBase.createPbufferSurface(width, height);
+ }
+ } else {
+ eglBase.createPbufferSurface(width, height);
+ }
+
+ eglBase.makeCurrent();
+ GLES20.glViewport(0, 0, width, height);
+ GlUtil.checkNoGLES2Error("Viewport");
+
+ if (drawer == null)
+ drawer = new GlRectDrawer();
+
+ drawer.drawOes(textureId, transformMatrix);
+ GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
+ GlUtil.checkNoGLES2Error("glReadPixels");
+ }
+ finally {
+ eglBase.detachCurrent();
+ }
+ }
+ }
+ public void release() {
+ synchronized(this) {
+ eglBase.makeCurrent();
+ if (drawer != null)
+ drawer.release();
+ eglBase.release();
+ }
+ }
+ }
+
+ // State for YUV conversion, instantiated on demand.
+ static private class ConvertYUV {
+ private final EglBase eglBase;
+ private final GlShader shader;
+ private boolean released = false;
+
+ // Vertex coordinates in Normalized Device Coordinates, i.e.
+ // (-1, -1) is bottom-left and (1, 1) is top-right.
+ private static final FloatBuffer DEVICE_RECTANGLE =
+ GlUtil.createFloatBuffer(new float[] {
+ -1.0f, -1.0f, // Bottom left.
+ 1.0f, -1.0f, // Bottom right.
+ -1.0f, 1.0f, // Top left.
+ 1.0f, 1.0f, // Top right.
+ });
+
+ // Texture coordinates - (0, 0) is bottom-left and (1, 1) is top-right.
+ private static final FloatBuffer TEXTURE_RECTANGLE =
+ GlUtil.createFloatBuffer(new float[] {
+ 0.0f, 0.0f, // Bottom left.
+ 1.0f, 0.0f, // Bottom right.
+ 0.0f, 1.0f, // Top left.
+ 1.0f, 1.0f // Top right.
+ });
+
+ private static final String VERTEX_SHADER =
+ "varying vec2 interp_tc;\n"
+ + "attribute vec4 in_pos;\n"
+ + "attribute vec4 in_tc;\n"
+ + "\n"
+ + "uniform mat4 texMatrix;\n"
+ + "\n"
+ + "void main() {\n"
+ + " gl_Position = in_pos;\n"
+ + " interp_tc = (texMatrix * in_tc).xy;\n"
+ + "}\n";
+
+ private static final String FRAGMENT_SHADER =
+ "#extension GL_OES_EGL_image_external : require\n"
+ + "precision mediump float;\n"
+ + "varying vec2 interp_tc;\n"
+ + "\n"
+ + "uniform samplerExternalOES oesTex;\n"
+ // Difference in texture coordinate corresponding to one
+ // sub-pixel in the x direction.
+ + "uniform vec2 xUnit;\n"
+ // Color conversion coefficients, including constant term
+ + "uniform vec4 coeffs;\n"
+ + "\n"
+ + "void main() {\n"
+ // TODO(nisse): Arrange color values into a matrix?
+ + " gl_FragColor.r = coeffs.a + dot(coeffs.rgb,\n"
+ + " texture2D(oesTex, interp_tc - 1.5 * xUnit).rgb);\n"
magjed_webrtc 2015/12/03 12:33:58 The alpha value from the texture2D should always b
+ + " gl_FragColor.g = coeffs.a + dot(coeffs.rgb,\n"
+ + " texture2D(oesTex, interp_tc - 0.5 * xUnit).rgb);\n"
+ + " gl_FragColor.b = coeffs.a + dot(coeffs.rgb,\n"
+ + " texture2D(oesTex, interp_tc + 0.5 * xUnit).rgb);\n"
+ + " gl_FragColor.a = coeffs.a + dot(coeffs.rgb,\n"
+ + " texture2D(oesTex, interp_tc + 1.5 * xUnit).rgb);\n"
+ + "}\n";
+
+ private int texMatrixLoc;
+ private int xUnitLoc;
+ private int coeffsLoc;;
+
+ ConvertYUV (EGLContext sharedContext) {
+ eglBase = new EglBase(sharedContext, EglBase.CONFIG_PIXEL_RGBA_BUFFER);
+ eglBase.createDummyPbufferSurface();
+ eglBase.makeCurrent();
+ try {
magjed_webrtc 2015/12/03 12:33:58 You don't need this try-finally. Detaching the EGL
nisse-webrtc 2015/12/04 09:40:34 Done.
+ shader = new GlShader(VERTEX_SHADER, FRAGMENT_SHADER);
+ shader.useProgram();
+ texMatrixLoc = shader.getUniformLocation("texMatrix");
+ xUnitLoc = shader.getUniformLocation("xUnit");
+ coeffsLoc = shader.getUniformLocation("coeffs");
+ GLES20.glUniform1i(shader.getUniformLocation("oesTex"), 0);
+ GlUtil.checkNoGLES2Error("Initialize fragment shader uniform values.");
+ // Initialize vertex shader attributes.
+ shader.setVertexAttribArray("in_pos", 2, DEVICE_RECTANGLE);
+ // If the width is not a multiple of 4 pixels, the texture
+ // will be scaled up slightly and clipped at the right border.
+ shader.setVertexAttribArray("in_tc", 2, TEXTURE_RECTANGLE);
+ }
+ finally {
+ eglBase.detachCurrent();
+ }
+ }
+
+ public void convert(ByteBuffer buf,
+ int width, int height, int stride, int textureId, float [] transformMatrix) {
magjed_webrtc 2015/12/03 12:33:58 why do you need a stride value? width/height shoul
nisse-webrtc 2015/12/03 13:35:20 To allow for some additional alignment, and at the
+ synchronized(this) {
magjed_webrtc 2015/12/03 12:33:58 If the whole function is synchronized on |this|, y
nisse-webrtc 2015/12/03 13:35:20 Acknowledged.
nisse-webrtc 2015/12/04 09:40:34 Done.
+ // TODO(nisse): For now only produces the Y plane.
+ if (released) {
+ throw new IllegalStateException(
+ "ConvertYUV.convert called on released object");
+ }
+ if (stride % 8 != 0) {
+ throw new IllegalArgumentException(
+ "Invalid stride, must be a multiple of 8");
magjed_webrtc 2015/12/03 12:33:58 why?
nisse-webrtc 2015/12/03 13:35:20 Because we do a split in half for drawing u and v,
+ }
+ if (stride < width){
+ throw new IllegalArgumentException(
+ "Invalid stride, must >= width");
+ }
+
+ int y_width = (width+3) / 4;
+ int uv_width = (width+7) / 8;
+ int uv_height = (height+1)/2;
+ int total_height = height + uv_height;
+ int size = stride * total_height;
+
+ Logging.e(TAG, "width: " + width + ", height: " + height +
+ ", y-width: " + y_width + ", stride: " + stride);
+
+ Logging.e(TAG, "matrix: \n " + transformMatrix[0]
+ + " " + transformMatrix[1]
+ + " " + transformMatrix[2]
+ + " " + transformMatrix[3]
+ + "\n " + transformMatrix[4]
+ + " " + transformMatrix[5]
+ + " " + transformMatrix[6]
+ + " " + transformMatrix[7]
+ + "\n " + transformMatrix[8]
+ + " " + transformMatrix[9]
+ + " " + transformMatrix[10]
+ + " " + transformMatrix[11]
+ + "\n " + transformMatrix[12]
+ + " " + transformMatrix[13]
+ + " " + transformMatrix[14]
+ + " " + transformMatrix[15]);
+
+ if (buf.capacity() < size) {
+ Logging.e(TAG, "needed size: " + size + ", available size: " + buf.capacity());
+ throw new IllegalStateException("ConvertYUV.convert called with too small buffer");
magjed_webrtc 2015/12/03 12:33:58 IllegalArgument
nisse-webrtc 2015/12/04 09:40:34 Done.
+ }
+ // Produce a frame buffer starting at top-left corner, not
+ // bottom-left.
+ transformMatrix =
+ RendererCommon.multiplyMatrices(transformMatrix,
+ RendererCommon.verticalFlipMatrix());
+
+ try {
+ // Reuse surface, if possible. TODO(nisse): Add an eglBase
+ // helper function, say, makeCurrentWithSize.
+ if (eglBase.hasSurface()) {
+ if (eglBase.surfaceWidth() != stride/4 ||
+ eglBase.surfaceHeight() != total_height){
+ eglBase.releaseSurface();
+ eglBase.createPbufferSurface(stride/4, total_height);
+ }
+ } else {
+ eglBase.createPbufferSurface(stride/4, total_height);
+ }
+
+ eglBase.makeCurrent();
+
+ GlUtil.checkNoGLES2Error("Initialize fragment shader uniform values.");
+
+ GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
+ GlUtil.checkNoGLES2Error("Active texture");
+ GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId);
+ GLES20.glUniformMatrix4fv(texMatrixLoc, 1, false, transformMatrix, 0);
+
+ // Draw Y */
+ GLES20.glViewport(0, 0, y_width, height);
+ GlUtil.checkNoGLES2Error("Viewport");
+ // Matrix * (1;0;0;0) / width. Note that opengl uses column major order.
+ GLES20.glUniform2f(xUnitLoc,
+ transformMatrix[0] / width,
+ transformMatrix[1] / width);
+ // Y'UV444 to RGB888, see
+ // https://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion
+ GLES20.glUniform4f(coeffsLoc, 0.299f, 0.587f, 0.114f, 0.0f);
+ GlUtil.checkNoGLES2Error("Binding texture and matrix");
+
+ Logging.e(TAG, "ConvertYUV: glDrawArrays");
+ GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
+ GlUtil.checkNoGLES2Error("glDrawArrays, Y");
+
+ // Draw U */
+ GLES20.glViewport(0, height, uv_width, uv_height);
+ GlUtil.checkNoGLES2Error("Viewport");
+ // Matrix * (1;0;0;0) / (2*width). Note that opengl uses column major order.
+ GLES20.glUniform2f(xUnitLoc,
+ transformMatrix[0] / (2.0f*width),
+ transformMatrix[1] / (2.0f*width));
+ /* Use ITU-R coefficients for U and V */
+ GLES20.glUniform4f(coeffsLoc, -0.169f, -0.331f, 0.499f, 0.5f);
+ GlUtil.checkNoGLES2Error("Binding texture and matrix");
+
+ Logging.e(TAG, "ConvertYUV: glDrawArrays");
+ GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
+ GlUtil.checkNoGLES2Error("glDrawArrays, U");
+
+ // Draw V */
+ GLES20.glViewport(stride/8, height, uv_width, uv_height);
+ GlUtil.checkNoGLES2Error("Viewport");
+ /* Use ITU-R coefficients for U and V */
+ GLES20.glUniform4f(coeffsLoc, 0.499f, -0.418f, -0.0813f, 0.5f);
+ GlUtil.checkNoGLES2Error("Binding texture and matrix");
+
+ Logging.e(TAG, "ConvertYUV: glDrawArrays");
+ GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
+ GlUtil.checkNoGLES2Error("glDrawArrays, V");
+
+ Logging.e(TAG, "ConvertYUV: glReadPixels");
+ GLES20.glReadPixels(0, 0, stride/4, total_height, GLES20.GL_RGBA,
+ GLES20.GL_UNSIGNED_BYTE, buf);
+ GlUtil.checkNoGLES2Error("glReadPixels");
+
+ // Unbind texture. Reportedly needed on some devices to get
+ // the texture updated from the camera.
+ GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
+ }
+ finally {
+ eglBase.detachCurrent();
+ }
+ }
+ }
+
+ public void release() {
+ synchronized(this) {
+ released = true;
+ shader.release();
+ eglBase.makeCurrent();
magjed_webrtc 2015/12/03 12:33:58 shader.release() need an EGLContext current, so yo
nisse-webrtc 2015/12/04 09:40:34 Done.
+ eglBase.release();
+ }
+ }
+ }
+
private final Handler handler;
private boolean isOwningThread;
private final EglBase eglBase;
private final SurfaceTexture surfaceTexture;
private final int oesTextureId;
+ private ConvertRGBA convertRGBA;
+ private ConvertYUV convertYUV;
+
private OnTextureFrameAvailableListener listener;
// The possible states of this class.
private boolean hasPendingTexture = false;
@@ -112,7 +410,7 @@ class SurfaceTextureHelper {
this.handler = handler;
this.isOwningThread = isOwningThread;
- eglBase = new EglBase(sharedContext, EglBase.ConfigType.PIXEL_BUFFER);
+ eglBase = new EglBase(sharedContext, EglBase.CONFIG_PIXEL_BUFFER);
eglBase.createDummyPbufferSurface();
eglBase.makeCurrent();
@@ -120,6 +418,30 @@ class SurfaceTextureHelper {
surfaceTexture = new SurfaceTexture(oesTextureId);
}
+ private ConvertRGBA getConvertRGBA() {
+ // convertRGBA is assign once
+ if (convertRGBA != null)
+ return convertRGBA;
+
+ synchronized(this) {
+ if (convertRGBA == null)
+ convertRGBA = new ConvertRGBA(eglBase.getContext());
+ return convertRGBA;
+ }
+ }
+
+ private ConvertYUV getConvertYUV() {
+ // convertYUV is assign once
+ if (convertYUV != null)
+ return convertYUV;
+
+ synchronized(this) {
+ if (convertYUV == null)
+ convertYUV = new ConvertYUV(eglBase.getContext());
+ return convertYUV;
+ }
+ }
+
/**
* Start to stream textures to the given |listener|.
* A Listener can only be set once.
@@ -207,6 +529,27 @@ class SurfaceTextureHelper {
disconnect();
}
+ public void textureToRGBA(ByteBuffer buf,
+ int width, int height, int textureId, float [] transformMatrix) {
+
+ Logging.e(TAG, "textureToRGBA called");
+
+ if (textureId != oesTextureId)
+ throw new IllegalStateException("textureToByteBuffer called with unexpected textureId");
+
+ getConvertRGBA().convert(buf, width, height, textureId, transformMatrix);
+ }
+
+ public void textureToYUV(ByteBuffer buf,
+ int width, int height, int stride, int textureId, float [] transformMatrix) {
+ Logging.e(TAG, "textureToYUV called");
+
+ if (textureId != oesTextureId)
+ throw new IllegalStateException("textureToByteBuffer called with unexpected textureId");
+
+ getConvertYUV().convert(buf, width, height, stride, textureId, transformMatrix);
+ }
+
private void tryDeliverTextureFrame() {
if (handler.getLooper().getThread() != Thread.currentThread()) {
throw new IllegalStateException("Wrong thread.");
@@ -235,6 +578,12 @@ class SurfaceTextureHelper {
if (isTextureInUse || !isQuitting) {
throw new IllegalStateException("Unexpected release.");
}
+ synchronized (this) {
+ if (convertRGBA != null)
+ convertRGBA.release();
+ if (convertYUV != null)
+ convertYUV.release();
+ }
eglBase.makeCurrent();
GLES20.glDeleteTextures(1, new int[] {oesTextureId}, 0);
surfaceTexture.release();

Powered by Google App Engine
This is Rietveld 408576698