Effect: Early return and rename variable (+ nits).

Reduce nesting by using an early return. Also, rename
`outputSizeOrRotationChanged` to `outputChanged`, because this also applies to when the output surface changes.

Also, update local variable initialization, add some javadoc, and remove unneeded
local variable

PiperOrigin-RevId: 518241708
This commit is contained in:
huangdarwin 2023-03-21 12:15:02 +00:00 committed by microkatz
parent 87cd90eb15
commit cbaf0f8232

View File

@ -93,7 +93,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@Nullable private SurfaceView debugSurfaceView; @Nullable private SurfaceView debugSurfaceView;
private boolean frameProcessingStarted; private boolean frameProcessingStarted;
private volatile boolean outputSizeOrRotationChanged; private volatile boolean outputChanged;
@GuardedBy("this") @GuardedBy("this")
@Nullable @Nullable
@ -270,26 +270,28 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
* @see VideoFrameProcessor#setOutputSurfaceInfo(SurfaceInfo) * @see VideoFrameProcessor#setOutputSurfaceInfo(SurfaceInfo)
*/ */
public synchronized void setOutputSurfaceInfo(@Nullable SurfaceInfo outputSurfaceInfo) { public synchronized void setOutputSurfaceInfo(@Nullable SurfaceInfo outputSurfaceInfo) {
if (!Util.areEqual(this.outputSurfaceInfo, outputSurfaceInfo)) { if (Util.areEqual(this.outputSurfaceInfo, outputSurfaceInfo)) {
if (outputSurfaceInfo != null return;
&& this.outputSurfaceInfo != null
&& !this.outputSurfaceInfo.surface.equals(outputSurfaceInfo.surface)) {
try {
GlUtil.destroyEglSurface(eglDisplay, outputEglSurface);
} catch (GlUtil.GlException e) {
videoFrameProcessorListenerExecutor.execute(
() -> videoFrameProcessorListener.onError(VideoFrameProcessingException.from(e)));
}
this.outputEglSurface = null;
}
outputSizeOrRotationChanged =
this.outputSurfaceInfo == null
|| outputSurfaceInfo == null
|| this.outputSurfaceInfo.width != outputSurfaceInfo.width
|| this.outputSurfaceInfo.height != outputSurfaceInfo.height
|| this.outputSurfaceInfo.orientationDegrees != outputSurfaceInfo.orientationDegrees;
this.outputSurfaceInfo = outputSurfaceInfo;
} }
if (outputSurfaceInfo != null
&& this.outputSurfaceInfo != null
&& !this.outputSurfaceInfo.surface.equals(outputSurfaceInfo.surface)) {
try {
GlUtil.destroyEglSurface(eglDisplay, outputEglSurface);
} catch (GlUtil.GlException e) {
videoFrameProcessorListenerExecutor.execute(
() -> videoFrameProcessorListener.onError(VideoFrameProcessingException.from(e)));
}
this.outputEglSurface = null;
}
outputChanged =
this.outputSurfaceInfo == null
|| outputSurfaceInfo == null
|| this.outputSurfaceInfo.width != outputSurfaceInfo.width
|| this.outputSurfaceInfo.height != outputSurfaceInfo.height
|| this.outputSurfaceInfo.orientationDegrees != outputSurfaceInfo.orientationDegrees;
this.outputSurfaceInfo = outputSurfaceInfo;
} }
private void renderFrameToSurfaces( private void renderFrameToSurfaces(
@ -336,16 +338,22 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
EGL14.eglSwapBuffers(eglDisplay, outputEglSurface); EGL14.eglSwapBuffers(eglDisplay, outputEglSurface);
} }
/**
* Ensures the instance is configured.
*
* <p>Returns {@code false} if {@code outputSurfaceInfo} is unset.
*/
@EnsuresNonNullIf( @EnsuresNonNullIf(
expression = {"outputSurfaceInfo", "outputEglSurface", "defaultShaderProgram"}, expression = {"outputSurfaceInfo", "outputEglSurface", "defaultShaderProgram"},
result = true) result = true)
private synchronized boolean ensureConfigured(int inputWidth, int inputHeight) private synchronized boolean ensureConfigured(int inputWidth, int inputHeight)
throws VideoFrameProcessingException, GlUtil.GlException { throws VideoFrameProcessingException, GlUtil.GlException {
boolean inputSizeChanged = false; boolean inputSizeChanged =
if (this.inputWidth != inputWidth this.inputWidth != inputWidth
|| this.inputHeight != inputHeight || this.inputHeight != inputHeight
|| this.outputSizeBeforeSurfaceTransformation == null) { || this.outputSizeBeforeSurfaceTransformation == null;
if (inputSizeChanged) {
this.inputWidth = inputWidth; this.inputWidth = inputWidth;
this.inputHeight = inputHeight; this.inputHeight = inputHeight;
Size outputSizeBeforeSurfaceTransformation = Size outputSizeBeforeSurfaceTransformation =
@ -359,7 +367,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
outputSizeBeforeSurfaceTransformation.getWidth(), outputSizeBeforeSurfaceTransformation.getWidth(),
outputSizeBeforeSurfaceTransformation.getHeight())); outputSizeBeforeSurfaceTransformation.getHeight()));
} }
inputSizeChanged = true;
} }
if (outputSurfaceInfo == null) { if (outputSurfaceInfo == null) {
@ -395,10 +402,10 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
this.debugSurfaceView = debugSurfaceView; this.debugSurfaceView = debugSurfaceView;
} }
if (defaultShaderProgram != null && (outputSizeOrRotationChanged || inputSizeChanged)) { if (defaultShaderProgram != null && (outputChanged || inputSizeChanged)) {
defaultShaderProgram.release(); defaultShaderProgram.release();
defaultShaderProgram = null; defaultShaderProgram = null;
outputSizeOrRotationChanged = false; outputChanged = false;
} }
if (defaultShaderProgram == null) { if (defaultShaderProgram == null) {
defaultShaderProgram = createDefaultShaderProgramForOutputSurface(outputSurfaceInfo); defaultShaderProgram = createDefaultShaderProgramForOutputSurface(outputSurfaceInfo);