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.
*
* <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) {
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.
*/
@CanIgnoreReturnValue
public Builder setAlpha(@FloatRange(from = 0, to = 1) float alpha) {
checkArgument(0 <= alpha && alpha <= 1, "Alpha needs to be in the interval [0, 1].");
public Builder setAlpha(@FloatRange(from = 0) float alpha) {
checkArgument(0 <= alpha, "Alpha needs to be more than or equal to zero.");
this.alpha = alpha;
return this;
}

View File

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