Skip to content

Commit

Permalink
Move inputTexId parameter to GlFrameProcessor#initialize().
Browse files Browse the repository at this point in the history
This parameter will not change between frames in the near
future.

PiperOrigin-RevId: 433765986
  • Loading branch information
hmsch authored and ojw28 committed Mar 15, 2022
1 parent 81d5055 commit 64d174b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ public void updateProgramAndDraw_noEdits_producesExpectedOutput() throws Excepti
Matrix identityMatrix = new Matrix();
transformationFrameProcessor =
new TransformationFrameProcessor(getApplicationContext(), identityMatrix);
transformationFrameProcessor.initialize();
transformationFrameProcessor.initialize(inputTexId);
Bitmap expectedBitmap = BitmapTestUtil.readBitmap(FIRST_FRAME_PNG_ASSET_STRING);

transformationFrameProcessor.updateProgramAndDraw(inputTexId, /* presentationTimeNs= */ 0);
transformationFrameProcessor.updateProgramAndDraw(/* presentationTimeNs= */ 0);
Bitmap actualBitmap =
BitmapTestUtil.createArgb8888BitmapFromCurrentGlFramebuffer(width, height);

Expand All @@ -117,11 +117,11 @@ public void updateProgramAndDraw_translateRight_producesExpectedOutput() throws
translateRightMatrix.postTranslate(/* dx= */ 1, /* dy= */ 0);
transformationFrameProcessor =
new TransformationFrameProcessor(getApplicationContext(), translateRightMatrix);
transformationFrameProcessor.initialize();
transformationFrameProcessor.initialize(inputTexId);
Bitmap expectedBitmap =
BitmapTestUtil.readBitmap(TRANSLATE_RIGHT_EXPECTED_OUTPUT_PNG_ASSET_STRING);

transformationFrameProcessor.updateProgramAndDraw(inputTexId, /* presentationTimeNs= */ 0);
transformationFrameProcessor.updateProgramAndDraw(/* presentationTimeNs= */ 0);
Bitmap actualBitmap =
BitmapTestUtil.createArgb8888BitmapFromCurrentGlFramebuffer(width, height);

Expand All @@ -141,11 +141,11 @@ public void updateProgramAndDraw_scaleNarrow_producesExpectedOutput() throws Exc
scaleNarrowMatrix.postScale(.5f, 1.2f);
transformationFrameProcessor =
new TransformationFrameProcessor(getApplicationContext(), scaleNarrowMatrix);
transformationFrameProcessor.initialize();
transformationFrameProcessor.initialize(inputTexId);
Bitmap expectedBitmap =
BitmapTestUtil.readBitmap(SCALE_NARROW_EXPECTED_OUTPUT_PNG_ASSET_STRING);

transformationFrameProcessor.updateProgramAndDraw(inputTexId, /* presentationTimeNs= */ 0);
transformationFrameProcessor.updateProgramAndDraw(/* presentationTimeNs= */ 0);
Bitmap actualBitmap =
BitmapTestUtil.createArgb8888BitmapFromCurrentGlFramebuffer(width, height);

Expand All @@ -168,10 +168,10 @@ public void updateProgramAndDraw_rotate90_producesExpectedOutput() throws Except
rotate90Matrix.postRotate(/* degrees= */ 90);
transformationFrameProcessor =
new TransformationFrameProcessor(getApplicationContext(), rotate90Matrix);
transformationFrameProcessor.initialize();
transformationFrameProcessor.initialize(inputTexId);
Bitmap expectedBitmap = BitmapTestUtil.readBitmap(ROTATE_90_EXPECTED_OUTPUT_PNG_ASSET_STRING);

transformationFrameProcessor.updateProgramAndDraw(inputTexId, /* presentationTimeNs= */ 0);
transformationFrameProcessor.updateProgramAndDraw(/* presentationTimeNs= */ 0);
Bitmap actualBitmap =
BitmapTestUtil.createArgb8888BitmapFromCurrentGlFramebuffer(width, height);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ExternalCopyFrameProcessor(Context context, boolean enableExperimentalHdr
}

@Override
public void initialize() throws IOException {
public void initialize(int inputTexId) throws IOException {
// TODO(b/205002913): check the loaded program is consistent with the attributes and uniforms
// expected in the code.
String vertexShaderFilePath =
Expand All @@ -70,6 +70,7 @@ public void initialize() throws IOException {
? FRAGMENT_SHADER_COPY_EXTERNAL_YUV_ES3_PATH
: FRAGMENT_SHADER_COPY_EXTERNAL_PATH;
glProgram = new GlProgram(context, vertexShaderFilePath, fragmentShaderFilePath);
glProgram.setSamplerTexIdUniform("uTexSampler", inputTexId, /* unit= */ 0);
// Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y.
glProgram.setBufferAttribute(
"aFramePosition", GlUtil.getNormalizedCoordinateBounds(), GlUtil.RECTANGLE_VERTICES_COUNT);
Expand All @@ -94,9 +95,8 @@ public void setTextureTransformMatrix(float[] textureTransformMatrix) {
}

@Override
public void updateProgramAndDraw(int inputTexId, long presentationTimeNs) {
public void updateProgramAndDraw(long presentationTimeNs) {
checkStateNotNull(glProgram);
glProgram.setSamplerTexIdUniform("uTexSampler", inputTexId, /* unit= */ 0);
glProgram.use();
glProgram.bindAttributesAndUniforms();
GLES20.glClearColor(/* red= */ 0, /* green= */ 0, /* blue= */ 0, /* alpha= */ 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ private static FrameEditor createOpenGlObjectsAndFrameEditor(

GlUtil.focusEglSurface(eglDisplay, eglContext, eglSurface, outputWidth, outputHeight);
int inputExternalTexId = GlUtil.createExternalTexture();
externalCopyFrameProcessor.initialize(inputExternalTexId);
int intermediateTexId = GlUtil.createTexture(outputWidth, outputHeight);
int frameBuffer = GlUtil.createFboForTexture(intermediateTexId);
externalCopyFrameProcessor.initialize();
transformationFrameProcessor.initialize();
transformationFrameProcessor.initialize(intermediateTexId);

return new FrameEditor(
singleThreadExecutorService,
Expand All @@ -196,7 +196,6 @@ private static FrameEditor createOpenGlObjectsAndFrameEditor(
externalCopyFrameProcessor,
transformationFrameProcessor,
inputExternalTexId,
intermediateTexId,
frameBuffer,
outputWidth,
outputHeight,
Expand Down Expand Up @@ -228,11 +227,10 @@ private static FrameEditor createOpenGlObjectsAndFrameEditor(
private final float[] textureTransformMatrix;

/**
* Identifier of the texture where the output of the {@link ExternalCopyFrameProcessor} is written
* to and the {@link TransformationFrameProcessor} reads its input from.
* Identifier of a framebuffer object associated with the intermediate texture that the output of
* the {@link ExternalCopyFrameProcessor} is written to and the {@link
* TransformationFrameProcessor} reads its input from.
*/
private final int intermediateTexId;
/** Identifier of a framebuffer object associated with the intermediate texture. */
private final int frameBuffer;

private final int outputWidth;
Expand All @@ -255,7 +253,6 @@ private FrameEditor(
ExternalCopyFrameProcessor externalCopyFrameProcessor,
GlFrameProcessor transformationFrameProcessor,
int inputExternalTexId,
int intermediateTexId,
int frameBuffer,
int outputWidth,
int outputHeight,
Expand All @@ -269,7 +266,6 @@ private FrameEditor(
this.externalCopyFrameProcessor = externalCopyFrameProcessor;
this.transformationFrameProcessor = transformationFrameProcessor;
this.inputExternalTexId = inputExternalTexId;
this.intermediateTexId = intermediateTexId;
this.frameBuffer = frameBuffer;
this.outputWidth = outputWidth;
this.outputHeight = outputHeight;
Expand Down Expand Up @@ -402,10 +398,10 @@ private void processFrame() {
GlUtil.focusFramebuffer(
eglDisplay, eglContext, eglSurface, frameBuffer, outputWidth, outputHeight);
externalCopyFrameProcessor.setTextureTransformMatrix(textureTransformMatrix);
externalCopyFrameProcessor.updateProgramAndDraw(inputExternalTexId, presentationTimeNs);
externalCopyFrameProcessor.updateProgramAndDraw(presentationTimeNs);

GlUtil.focusEglSurface(eglDisplay, eglContext, eglSurface, outputWidth, outputHeight);
transformationFrameProcessor.updateProgramAndDraw(intermediateTexId, presentationTimeNs);
transformationFrameProcessor.updateProgramAndDraw(presentationTimeNs);
EGLExt.eglPresentationTimeANDROID(eglDisplay, eglSurface, presentationTimeNs);
EGL14.eglSwapBuffers(eglDisplay, eglSurface);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@
*
* <p>This method may only be called after creating the OpenGL context and focusing a render
* target.
*
* @param inputTexId The identifier of an OpenGL texture that the fragment shader can sample from.
*/
void initialize() throws IOException;
void initialize(int inputTexId) throws IOException;

/**
* Updates the shader program's vertex attributes and uniforms, binds them, and draws.
*
* <p>The frame processor must be {@link #initialize() initialized}. The caller is responsible for
* focussing the correct render target before calling this method.
* <p>The frame processor must be {@link #initialize(int) initialized}. The caller is responsible
* for focussing the correct render target before calling this method.
*
* @param inputTexId The identifier of an OpenGL texture that the fragment shader can sample from.
* @param presentationTimeNs The presentation timestamp of the current frame, in nanoseconds.
*/
void updateProgramAndDraw(int inputTexId, long presentationTimeNs);
void updateProgramAndDraw(long presentationTimeNs);

/** Releases all resources. */
void release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ public TransformationFrameProcessor(Context context, Matrix transformationMatrix
}

@Override
public void initialize() throws IOException {
public void initialize(int inputTexId) throws IOException {
// TODO(b/205002913): check the loaded program is consistent with the attributes and uniforms
// expected in the code.
glProgram = new GlProgram(context, VERTEX_SHADER_TRANSFORMATION_PATH, FRAGMENT_SHADER_PATH);
glProgram.setSamplerTexIdUniform("uTexSampler", inputTexId, /* unit= */ 0);
// Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y.
glProgram.setBufferAttribute(
"aFramePosition", GlUtil.getNormalizedCoordinateBounds(), GlUtil.RECTANGLE_VERTICES_COUNT);
Expand All @@ -106,9 +107,8 @@ public void initialize() throws IOException {
}

@Override
public void updateProgramAndDraw(int inputTexId, long presentationTimeNs) {
public void updateProgramAndDraw(long presentationTimeNs) {
checkStateNotNull(glProgram);
glProgram.setSamplerTexIdUniform("uTexSampler", inputTexId, /* unit= */ 0);
glProgram.use();
glProgram.bindAttributesAndUniforms();
GLES20.glClearColor(/* red= */ 0, /* green= */ 0, /* blue= */ 0, /* alpha= */ 0);
Expand Down

0 comments on commit 64d174b

Please sign in to comment.