FrameProcessor: Avoid early rounding in Presentation output dimensions.

PiperOrigin-RevId: 444253425
This commit is contained in:
huangdarwin 2022-04-25 14:38:17 +01:00 committed by Ian Baker
parent bf16273350
commit f1cda5fe2a

View File

@ -233,8 +233,9 @@ public final class PresentationFrameProcessor implements GlFrameProcessor {
private final float requestedAspectRatio; private final float requestedAspectRatio;
private final @Layout int layout; private final @Layout int layout;
private int outputWidth; private float outputWidth;
private int outputHeight; private float outputHeight;
private @MonotonicNonNull Size outputSize;
private @MonotonicNonNull Matrix transformationMatrix; private @MonotonicNonNull Matrix transformationMatrix;
private @MonotonicNonNull AdvancedFrameProcessor advancedFrameProcessor; private @MonotonicNonNull AdvancedFrameProcessor advancedFrameProcessor;
@ -270,10 +271,10 @@ public final class PresentationFrameProcessor implements GlFrameProcessor {
@Override @Override
public Size getOutputSize() { public Size getOutputSize() {
checkState( checkStateNotNull(
outputWidth != C.LENGTH_UNSET && outputHeight != C.LENGTH_UNSET, outputSize,
"configureOutputSizeAndTransformationMatrix must be called before getOutputSize"); "configureOutputSizeAndTransformationMatrix must be called before getOutputSize");
return new Size(outputWidth, outputHeight); return outputSize;
} }
@Override @Override
@ -309,9 +310,10 @@ public final class PresentationFrameProcessor implements GlFrameProcessor {
// Scale width and height to desired requestedHeightPixels, preserving aspect ratio. // Scale width and height to desired requestedHeightPixels, preserving aspect ratio.
if (requestedHeightPixels != C.LENGTH_UNSET && requestedHeightPixels != outputHeight) { if (requestedHeightPixels != C.LENGTH_UNSET && requestedHeightPixels != outputHeight) {
outputWidth = Math.round((float) requestedHeightPixels * outputWidth / outputHeight); outputWidth = requestedHeightPixels * outputWidth / outputHeight;
outputHeight = requestedHeightPixels; outputHeight = requestedHeightPixels;
} }
outputSize = new Size(Math.round(outputWidth), Math.round(outputHeight));
} }
@RequiresNonNull("transformationMatrix") @RequiresNonNull("transformationMatrix")
@ -324,34 +326,34 @@ public final class PresentationFrameProcessor implements GlFrameProcessor {
transformationMatrix.postTranslate(-centerX, -centerY); transformationMatrix.postTranslate(-centerX, -centerY);
transformationMatrix.postScale(1f / scaleX, 1f / scaleY); transformationMatrix.postScale(1f / scaleX, 1f / scaleY);
outputWidth = Math.round(outputWidth * scaleX); outputWidth = outputWidth * scaleX;
outputHeight = Math.round(outputHeight * scaleY); outputHeight = outputHeight * scaleY;
} }
@RequiresNonNull("transformationMatrix") @RequiresNonNull("transformationMatrix")
private void applyAspectRatio() { private void applyAspectRatio() {
float inputAspectRatio = (float) outputWidth / outputHeight; float inputAspectRatio = outputWidth / outputHeight;
if (layout == LAYOUT_SCALE_TO_FIT) { if (layout == LAYOUT_SCALE_TO_FIT) {
if (requestedAspectRatio > inputAspectRatio) { if (requestedAspectRatio > inputAspectRatio) {
transformationMatrix.setScale(inputAspectRatio / requestedAspectRatio, 1f); transformationMatrix.setScale(inputAspectRatio / requestedAspectRatio, 1f);
outputWidth = Math.round(outputHeight * requestedAspectRatio); outputWidth = outputHeight * requestedAspectRatio;
} else { } else {
transformationMatrix.setScale(1f, requestedAspectRatio / inputAspectRatio); transformationMatrix.setScale(1f, requestedAspectRatio / inputAspectRatio);
outputHeight = Math.round(outputWidth / requestedAspectRatio); outputHeight = outputWidth / requestedAspectRatio;
} }
} else if (layout == LAYOUT_SCALE_TO_FIT_WITH_CROP) { } else if (layout == LAYOUT_SCALE_TO_FIT_WITH_CROP) {
if (requestedAspectRatio > inputAspectRatio) { if (requestedAspectRatio > inputAspectRatio) {
transformationMatrix.setScale(1f, requestedAspectRatio / inputAspectRatio); transformationMatrix.setScale(1f, requestedAspectRatio / inputAspectRatio);
outputHeight = Math.round(outputWidth / requestedAspectRatio); outputHeight = outputWidth / requestedAspectRatio;
} else { } else {
transformationMatrix.setScale(inputAspectRatio / requestedAspectRatio, 1f); transformationMatrix.setScale(inputAspectRatio / requestedAspectRatio, 1f);
outputWidth = Math.round(outputHeight * requestedAspectRatio); outputWidth = outputHeight * requestedAspectRatio;
} }
} else if (layout == LAYOUT_STRETCH_TO_FIT) { } else if (layout == LAYOUT_STRETCH_TO_FIT) {
if (requestedAspectRatio > inputAspectRatio) { if (requestedAspectRatio > inputAspectRatio) {
outputWidth = Math.round(outputHeight * requestedAspectRatio); outputWidth = outputHeight * requestedAspectRatio;
} else { } else {
outputHeight = Math.round(outputWidth / requestedAspectRatio); outputHeight = outputWidth / requestedAspectRatio;
} }
} }
} }