Overlay: Improve alpha documentation and extract reused variables

PiperOrigin-RevId: 560064663
This commit is contained in:
huangdarwin 2023-08-25 05:40:47 -07:00 committed by Copybara-Service
parent 01689ba2ec
commit 9810d6ae92
3 changed files with 18 additions and 19 deletions

View File

@ -32,7 +32,7 @@ public final class AlphaScale implements GlEffect {
* modify translucency. * modify translucency.
* *
* <p>An {@code alphaScale} value of {@code 1} means no change is applied. A value below {@code 1} * <p>An {@code alphaScale} value of {@code 1} means no change is applied. A value below {@code 1}
* reduces translucency, and a value above {@code 1} increases translucency. * increases translucency, and a value above {@code 1} reduces translucency.
*/ */
public AlphaScale(@FloatRange(from = 0) float alphaScale) { public AlphaScale(@FloatRange(from = 0) float alphaScale) {
checkArgument(0 <= alphaScale); checkArgument(0 <= alphaScale);

View File

@ -78,15 +78,16 @@ public final class OverlaySettings {
} }
/** /**
* Sets the alpha value of the overlay, altering its transparency. * Sets the alpha scale value of the overlay, altering its translucency.
* *
* <p>Alpha values range from 0 (all transparent) to 1 (completely opaque). * <p>An {@code alpha} value of {@code 1} means no change is applied. A value below {@code 1}
* increases translucency, and a value above {@code 1} reduces translucency.
* *
* <p>Set to always return {@code 1} by default. * <p>Set to always return {@code 1} by default.
*/ */
@CanIgnoreReturnValue @CanIgnoreReturnValue
public Builder setAlpha(@FloatRange(from = 0, to = 1) float alpha) { public Builder setAlpha(@FloatRange(from = 0) float alpha) {
checkArgument(0 <= alpha && alpha <= 1, "Alpha needs to be in the interval [0, 1]."); checkArgument(0 <= alpha, "Alpha needs to be more than or equal to zero.");
this.alpha = alpha; this.alpha = alpha;
return this; return this;
} }

View File

@ -117,6 +117,8 @@ import com.google.common.collect.ImmutableList;
Util.formatInvariant("uOverlayTexSampler%d", texUnitIndex), Util.formatInvariant("uOverlayTexSampler%d", texUnitIndex),
overlay.getTextureId(presentationTimeUs), overlay.getTextureId(presentationTimeUs),
texUnitIndex); texUnitIndex);
OverlaySettings overlaySettings = overlay.getOverlaySettings(presentationTimeUs);
Size overlaySize = overlay.getTextureSize(presentationTimeUs);
GlUtil.setToIdentity(aspectRatioMatrix); GlUtil.setToIdentity(aspectRatioMatrix);
GlUtil.setToIdentity(videoFrameAnchorMatrix); GlUtil.setToIdentity(videoFrameAnchorMatrix);
@ -131,8 +133,7 @@ import com.google.common.collect.ImmutableList;
GlUtil.setToIdentity(transformationMatrix); GlUtil.setToIdentity(transformationMatrix);
// Anchor point of overlay within output frame. // Anchor point of overlay within output frame.
Pair<Float, Float> videoFrameAnchor = Pair<Float, Float> videoFrameAnchor = overlaySettings.videoFrameAnchor;
overlay.getOverlaySettings(presentationTimeUs).videoFrameAnchor;
Matrix.translateM( Matrix.translateM(
videoFrameAnchorMatrix, videoFrameAnchorMatrix,
MATRIX_OFFSET, MATRIX_OFFSET,
@ -145,12 +146,12 @@ import com.google.common.collect.ImmutableList;
Matrix.scaleM( Matrix.scaleM(
aspectRatioMatrix, aspectRatioMatrix,
MATRIX_OFFSET, MATRIX_OFFSET,
videoWidth / (float) overlay.getTextureSize(presentationTimeUs).getWidth(), videoWidth / (float) overlaySize.getWidth(),
videoHeight / (float) overlay.getTextureSize(presentationTimeUs).getHeight(), videoHeight / (float) overlaySize.getHeight(),
/* z= */ 1f); /* z= */ 1f);
// Scale the image. // Scale the image.
Pair<Float, Float> scale = overlay.getOverlaySettings(presentationTimeUs).scale; Pair<Float, Float> scale = overlaySettings.scale;
Matrix.scaleM( Matrix.scaleM(
scaleMatrix, scaleMatrix,
MATRIX_OFFSET, MATRIX_OFFSET,
@ -162,8 +163,7 @@ import com.google.common.collect.ImmutableList;
Matrix.invertM(scaleMatrixInv, MATRIX_OFFSET, scaleMatrix, MATRIX_OFFSET); Matrix.invertM(scaleMatrixInv, MATRIX_OFFSET, scaleMatrix, MATRIX_OFFSET);
// Translate the overlay within its frame. // Translate the overlay within its frame.
Pair<Float, Float> overlayAnchor = Pair<Float, Float> overlayAnchor = overlaySettings.overlayAnchor;
overlay.getOverlaySettings(presentationTimeUs).overlayAnchor;
Matrix.translateM( Matrix.translateM(
overlayAnchorMatrix, overlayAnchorMatrix,
MATRIX_OFFSET, MATRIX_OFFSET,
@ -178,7 +178,7 @@ import com.google.common.collect.ImmutableList;
MATRIX_OFFSET, MATRIX_OFFSET,
rotateMatrix, rotateMatrix,
MATRIX_OFFSET, MATRIX_OFFSET,
overlay.getOverlaySettings(presentationTimeUs).rotationDegrees, overlaySettings.rotationDegrees,
/* x= */ 0f, /* x= */ 0f,
/* y= */ 0f, /* y= */ 0f,
/* z= */ 1f); /* z= */ 1f);
@ -188,8 +188,7 @@ import com.google.common.collect.ImmutableList;
Matrix.scaleM( Matrix.scaleM(
overlayAspectRatioMatrix, overlayAspectRatioMatrix,
MATRIX_OFFSET, MATRIX_OFFSET,
(float) overlay.getTextureSize(presentationTimeUs).getHeight() (float) overlaySize.getHeight() / (float) overlaySize.getWidth(),
/ (float) overlay.getTextureSize(presentationTimeUs).getWidth(),
/* y= */ 1f, /* y= */ 1f,
/* z= */ 1f); /* z= */ 1f);
Matrix.invertM( Matrix.invertM(
@ -277,8 +276,7 @@ import com.google.common.collect.ImmutableList;
Util.formatInvariant("uTransformationMatrix%d", texUnitIndex), transformationMatrix); Util.formatInvariant("uTransformationMatrix%d", texUnitIndex), transformationMatrix);
glProgram.setFloatUniform( glProgram.setFloatUniform(
Util.formatInvariant("uOverlayAlpha%d", texUnitIndex), Util.formatInvariant("uOverlayAlpha%d", texUnitIndex), overlaySettings.alpha);
overlay.getOverlaySettings(presentationTimeUs).alpha);
} }
} }
glProgram.setSamplerTexIdUniform("uVideoTexSampler0", inputTexId, /* texUnitIndex= */ 0); glProgram.setSamplerTexIdUniform("uVideoTexSampler0", inputTexId, /* texUnitIndex= */ 0);
@ -352,13 +350,13 @@ import com.google.common.collect.ImmutableList;
.append( .append(
"// (https://open.gl/textures) since it's not implemented until OpenGL ES 3.2.\n") "// (https://open.gl/textures) since it's not implemented until OpenGL ES 3.2.\n")
.append("vec4 getClampToBorderOverlayColor(\n") .append("vec4 getClampToBorderOverlayColor(\n")
.append(" sampler2D texSampler, vec2 texSamplingCoord, float alpha){\n") .append(" sampler2D texSampler, vec2 texSamplingCoord, float alphaScale){\n")
.append(" if (texSamplingCoord.x > 1.0 || texSamplingCoord.x < 0.0\n") .append(" if (texSamplingCoord.x > 1.0 || texSamplingCoord.x < 0.0\n")
.append(" || texSamplingCoord.y > 1.0 || texSamplingCoord.y < 0.0) {\n") .append(" || texSamplingCoord.y > 1.0 || texSamplingCoord.y < 0.0) {\n")
.append(" return vec4(0.0, 0.0, 0.0, 0.0);\n") .append(" return vec4(0.0, 0.0, 0.0, 0.0);\n")
.append(" } else {\n") .append(" } else {\n")
.append(" vec4 overlayColor = vec4(texture2D(texSampler, texSamplingCoord));\n") .append(" vec4 overlayColor = vec4(texture2D(texSampler, texSamplingCoord));\n")
.append(" overlayColor.a = alpha * overlayColor.a;\n") .append(" overlayColor.a = alphaScale * overlayColor.a;\n")
.append(" return overlayColor;\n") .append(" return overlayColor;\n")
.append(" }\n") .append(" }\n")
.append("}\n") .append("}\n")