Effect: Add FloatRange to public GlEffect impl interfaces.

Using these annotations/checkers should hopefully make it marginally harder to use the wrong input values in the API.

PiperOrigin-RevId: 510966941
This commit is contained in:
huangdarwin 2023-02-20 12:51:38 +00:00 committed by Andrew Lewis
parent cbb6878f9f
commit c79eb7466a
4 changed files with 12 additions and 7 deletions

View File

@ -19,6 +19,7 @@ package com.google.android.exoplayer2.effect;
import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import android.content.Context;
import androidx.annotation.FloatRange;
import com.google.android.exoplayer2.util.VideoFrameProcessingException;
/** A {@link GlEffect} to control the contrast of video frames. */
@ -33,7 +34,7 @@ public class Contrast implements GlEffect {
* <p>Contrast values range from -1 (all gray pixels) to 1 (maximum difference of colors). 0 means
* to add no contrast and leaves the frames unchanged.
*/
public Contrast(float contrast) {
public Contrast(@FloatRange(from = -1, to = 1) float contrast) {
checkArgument(-1 <= contrast && contrast <= 1, "Contrast needs to be in the interval [-1, 1].");
this.contrast = contrast;
}

View File

@ -19,6 +19,7 @@ package com.google.android.exoplayer2.effect;
import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import android.content.Context;
import androidx.annotation.FloatRange;
import com.google.android.exoplayer2.util.VideoFrameProcessingException;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
@ -61,7 +62,7 @@ public class HslAdjustment implements GlEffect {
* {@code 0}, which means no change is applied.
*/
@CanIgnoreReturnValue
public Builder adjustSaturation(float saturationAdjustment) {
public Builder adjustSaturation(@FloatRange(from = -100, to = 100) float saturationAdjustment) {
checkArgument(
-100 <= saturationAdjustment && saturationAdjustment <= 100,
"Can adjust the saturation by only 100 in either direction, but provided "
@ -81,7 +82,7 @@ public class HslAdjustment implements GlEffect {
* {@code 0}, which means no change is applied.
*/
@CanIgnoreReturnValue
public Builder adjustLightness(float lightnessAdjustment) {
public Builder adjustLightness(@FloatRange(from = -100, to = 100) float lightnessAdjustment) {
checkArgument(
-100 <= lightnessAdjustment && lightnessAdjustment <= 100,
"Can adjust the lightness by only 100 in either direction, but provided "

View File

@ -21,6 +21,7 @@ import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.SOURCE;
import android.graphics.Matrix;
import androidx.annotation.FloatRange;
import androidx.annotation.IntDef;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Size;
@ -114,7 +115,8 @@ public final class Presentation implements MatrixTransformation {
* @param aspectRatio The aspect ratio (width/height ratio) of the output frame. Must be positive.
* @param layout The layout of the output frame.
*/
public static Presentation createForAspectRatio(float aspectRatio, @Layout int layout) {
public static Presentation createForAspectRatio(
@FloatRange(from = 0, fromInclusive = false) float aspectRatio, @Layout int layout) {
checkArgument(
aspectRatio == C.LENGTH_UNSET || aspectRatio > 0,
"aspect ratio " + aspectRatio + " must be positive or unset");

View File

@ -19,6 +19,7 @@ package com.google.android.exoplayer2.effect;
import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import android.opengl.Matrix;
import androidx.annotation.FloatRange;
import com.google.android.exoplayer2.util.GlUtil;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
@ -45,7 +46,7 @@ public final class RgbAdjustment implements RgbMatrix {
* default value is {@code 1}.
*/
@CanIgnoreReturnValue
public Builder setRedScale(float redScale) {
public Builder setRedScale(@FloatRange(from = 0) float redScale) {
checkArgument(0 <= redScale, "Red scale needs to be non-negative.");
this.redScale = redScale;
return this;
@ -58,7 +59,7 @@ public final class RgbAdjustment implements RgbMatrix {
* default value is {@code 1}.
*/
@CanIgnoreReturnValue
public Builder setGreenScale(float greenScale) {
public Builder setGreenScale(@FloatRange(from = 0) float greenScale) {
checkArgument(0 <= greenScale, "Green scale needs to be non-negative.");
this.greenScale = greenScale;
return this;
@ -71,7 +72,7 @@ public final class RgbAdjustment implements RgbMatrix {
* default value is {@code 1}.
*/
@CanIgnoreReturnValue
public Builder setBlueScale(float blueScale) {
public Builder setBlueScale(@FloatRange(from = 0) float blueScale) {
checkArgument(0 <= blueScale, "Blue scale needs to be non-negative.");
this.blueScale = blueScale;
return this;