From 9f888435b7dd568e767c7f4cdf531576127c92b7 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Mon, 27 Mar 2023 03:36:47 -0700 Subject: [PATCH] Internal change PiperOrigin-RevId: 519674123 --- mediapipe/gpu/BUILD | 2 +- mediapipe/gpu/gl_quad_renderer.cc | 67 +++++++++++++++++++------------ mediapipe/gpu/gl_quad_renderer.h | 7 +++- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/mediapipe/gpu/BUILD b/mediapipe/gpu/BUILD index 9b538a7f..d74e8ad3 100644 --- a/mediapipe/gpu/BUILD +++ b/mediapipe/gpu/BUILD @@ -858,7 +858,7 @@ cc_library( ":scale_mode_cc_proto", ":shader_util", "//mediapipe/framework/port:ret_check", - "//mediapipe/framework/port:status", + "@com_google_absl//absl/status", ], ) diff --git a/mediapipe/gpu/gl_quad_renderer.cc b/mediapipe/gpu/gl_quad_renderer.cc index 309b8323..ec779b59 100644 --- a/mediapipe/gpu/gl_quad_renderer.cc +++ b/mediapipe/gpu/gl_quad_renderer.cc @@ -87,6 +87,17 @@ absl::Status QuadRenderer::GlSetup( glGenVertexArrays(1, &vao_); glGenBuffers(2, vbo_); + glBindVertexArray(vao_); + glEnableVertexAttribArray(ATTRIB_VERTEX); + glEnableVertexAttribArray(ATTRIB_TEXTURE_POSITION); + + glBindBuffer(GL_ARRAY_BUFFER, vbo_[1]); + glBufferData(GL_ARRAY_BUFFER, sizeof(mediapipe::kBasicTextureVertices), + kBasicTextureVertices, GL_STATIC_DRAW); + glVertexAttribPointer(ATTRIB_TEXTURE_POSITION, 2, GL_FLOAT, 0, 0, nullptr); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + return absl::OkStatus(); } @@ -104,6 +115,7 @@ void QuadRenderer::GlTeardown() { vbo_[0] = 0; vbo_[1] = 0; } + rotation_ = std::nullopt; } absl::Status QuadRenderer::GlRender(float frame_width, float frame_height, @@ -145,12 +157,42 @@ absl::Status QuadRenderer::GlRender(float frame_width, float frame_height, break; } + if (flip_texture) { + switch (rotation) { + case FrameRotation::kNone: // Fall-through intended. + case FrameRotation::k180: + flip_vertical = !flip_vertical; + break; + case FrameRotation::k90: + flip_horizontal = !flip_horizontal; + break; + case FrameRotation::k270: + flip_horizontal = !flip_horizontal; + break; + } + } const int h_flip_factor = flip_horizontal ? -1 : 1; const int v_flip_factor = flip_vertical ? -1 : 1; + GLfloat scale[] = {scale_width * h_flip_factor, scale_height * v_flip_factor, 1.0, 1.0}; glUniform4fv(scale_unif_, 1, scale); + // Draw. + + glBindVertexArray(vao_); + if (rotation != rotation_) { + rotation_ = rotation; + UpdateVertices(rotation); + } + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + glBindVertexArray(0); + + return absl::OkStatus(); +} + +void QuadRenderer::UpdateVertices(FrameRotation rotation) const { // Choose vertices for rotation. const GLfloat* vertices; // quad used to render the texture. switch (rotation) { @@ -168,36 +210,11 @@ absl::Status QuadRenderer::GlRender(float frame_width, float frame_height, break; } - // Draw. - - // TODO: In practice, our vertex attributes almost never change, so - // convert this to being actually static, with initialization done in the - // GLSetup. - glBindVertexArray(vao_); - glEnableVertexAttribArray(ATTRIB_VERTEX); glBindBuffer(GL_ARRAY_BUFFER, vbo_[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(mediapipe::kBasicSquareVertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, nullptr); - - glEnableVertexAttribArray(ATTRIB_TEXTURE_POSITION); - glBindBuffer(GL_ARRAY_BUFFER, vbo_[1]); - glBufferData( - GL_ARRAY_BUFFER, - flip_texture ? sizeof(mediapipe::kBasicTextureVerticesFlipY) - : sizeof(mediapipe::kBasicTextureVertices), - flip_texture ? kBasicTextureVerticesFlipY : kBasicTextureVertices, - GL_STATIC_DRAW); - - glVertexAttribPointer(ATTRIB_TEXTURE_POSITION, 2, GL_FLOAT, 0, 0, nullptr); - - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - glDisableVertexAttribArray(ATTRIB_VERTEX); - glDisableVertexAttribArray(ATTRIB_TEXTURE_POSITION); glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - - return absl::OkStatus(); } absl::Status FrameRotationFromInt(FrameRotation* rotation, int degrees_ccw) { diff --git a/mediapipe/gpu/gl_quad_renderer.h b/mediapipe/gpu/gl_quad_renderer.h index 3d0e9d64..431757c7 100644 --- a/mediapipe/gpu/gl_quad_renderer.h +++ b/mediapipe/gpu/gl_quad_renderer.h @@ -15,7 +15,9 @@ #ifndef MEDIAPIPE_GPU_GL_QUAD_RENDERER_H_ #define MEDIAPIPE_GPU_GL_QUAD_RENDERER_H_ -#include "mediapipe/framework/port/status.h" +#include + +#include "absl/status/status.h" #include "mediapipe/gpu/gl_base.h" #include "mediapipe/gpu/scale_mode.pb.h" @@ -78,11 +80,14 @@ class QuadRenderer { void GlTeardown(); private: + void UpdateVertices(FrameRotation rotation) const; + GLuint program_ = 0; GLint scale_unif_ = -1; std::vector frame_unifs_; GLuint vao_ = 0; // vertex array object GLuint vbo_[2] = {0, 0}; // for vertex buffer storage + mutable std::optional rotation_; }; absl::Status FrameRotationFromInt(FrameRotation* rotation, int degrees_ccw);