From 9810d6ae92b5ac1de3008babf2bd98f42030b93f Mon Sep 17 00:00:00 2001 From: huangdarwin Date: Fri, 25 Aug 2023 05:40:47 -0700 Subject: [PATCH] Overlay: Improve alpha documentation and extract reused variables PiperOrigin-RevId: 560064663 --- .../androidx/media3/effect/AlphaScale.java | 2 +- .../media3/effect/OverlaySettings.java | 9 ++++--- .../media3/effect/OverlayShaderProgram.java | 26 +++++++++---------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/libraries/effect/src/main/java/androidx/media3/effect/AlphaScale.java b/libraries/effect/src/main/java/androidx/media3/effect/AlphaScale.java index de8d290196..c7dc236d3e 100644 --- a/libraries/effect/src/main/java/androidx/media3/effect/AlphaScale.java +++ b/libraries/effect/src/main/java/androidx/media3/effect/AlphaScale.java @@ -32,7 +32,7 @@ public final class AlphaScale implements GlEffect { * modify translucency. * *

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); diff --git a/libraries/effect/src/main/java/androidx/media3/effect/OverlaySettings.java b/libraries/effect/src/main/java/androidx/media3/effect/OverlaySettings.java index b5c0372525..35e947c7e8 100644 --- a/libraries/effect/src/main/java/androidx/media3/effect/OverlaySettings.java +++ b/libraries/effect/src/main/java/androidx/media3/effect/OverlaySettings.java @@ -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. * - *

Alpha values range from 0 (all transparent) to 1 (completely opaque). + *

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. * *

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; } diff --git a/libraries/effect/src/main/java/androidx/media3/effect/OverlayShaderProgram.java b/libraries/effect/src/main/java/androidx/media3/effect/OverlayShaderProgram.java index f22e3dbbcf..f1faa82cb3 100644 --- a/libraries/effect/src/main/java/androidx/media3/effect/OverlayShaderProgram.java +++ b/libraries/effect/src/main/java/androidx/media3/effect/OverlayShaderProgram.java @@ -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 videoFrameAnchor = - overlay.getOverlaySettings(presentationTimeUs).videoFrameAnchor; + Pair 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 scale = overlay.getOverlaySettings(presentationTimeUs).scale; + Pair 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 overlayAnchor = - overlay.getOverlaySettings(presentationTimeUs).overlayAnchor; + Pair 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")