From 4bda012bba8fa7b5e0b4a04ebdfae8519329bc32 Mon Sep 17 00:00:00 2001 From: Camillo Lugaresi Date: Tue, 15 Nov 2022 17:07:26 -0800 Subject: [PATCH] Factor out gl_texture_util PiperOrigin-RevId: 488797985 --- mediapipe/gpu/BUILD | 11 +++++++ mediapipe/gpu/gl_texture_util.cc | 30 ++++++++++++++++++ mediapipe/gpu/gl_texture_util.h | 34 +++++++++++++++++++++ mediapipe/gpu/gpu_buffer_test.cc | 52 ++++---------------------------- 4 files changed, 81 insertions(+), 46 deletions(-) create mode 100644 mediapipe/gpu/gl_texture_util.cc create mode 100644 mediapipe/gpu/gl_texture_util.h diff --git a/mediapipe/gpu/BUILD b/mediapipe/gpu/BUILD index 747d131ba..68e788c52 100644 --- a/mediapipe/gpu/BUILD +++ b/mediapipe/gpu/BUILD @@ -689,6 +689,17 @@ cc_library( }), ) +cc_library( + name = "gl_texture_util", + srcs = ["gl_texture_util.cc"], + hdrs = ["gl_texture_util.h"], + visibility = ["//visibility:public"], + deps = [ + ":gl_base", + ":gl_texture_view", + ], +) + cc_library( name = "shader_util", srcs = ["shader_util.cc"], diff --git a/mediapipe/gpu/gl_texture_util.cc b/mediapipe/gpu/gl_texture_util.cc new file mode 100644 index 000000000..603e82a46 --- /dev/null +++ b/mediapipe/gpu/gl_texture_util.cc @@ -0,0 +1,30 @@ +#include "mediapipe/gpu/gl_texture_util.h" + +namespace mediapipe { + +void CopyGlTexture(const GlTextureView& src, GlTextureView& dst) { + glViewport(0, 0, src.width(), src.height()); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, src.target(), + src.name(), 0); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(dst.target(), dst.name()); + glCopyTexSubImage2D(dst.target(), 0, 0, 0, 0, 0, dst.width(), dst.height()); + + glBindTexture(dst.target(), 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, src.target(), 0, + 0); +} + +void FillGlTextureRgba(GlTextureView& view, float r, float g, float b, + float a) { + glViewport(0, 0, view.width(), view.height()); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, view.target(), + view.name(), 0); + glClearColor(r, g, b, a); + glClear(GL_COLOR_BUFFER_BIT); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, view.target(), 0, + 0); +} + +} // namespace mediapipe diff --git a/mediapipe/gpu/gl_texture_util.h b/mediapipe/gpu/gl_texture_util.h new file mode 100644 index 000000000..73ac37ade --- /dev/null +++ b/mediapipe/gpu/gl_texture_util.h @@ -0,0 +1,34 @@ +#ifndef MEDIAPIPE_GPU_GL_TEXTURE_UTIL_H_ +#define MEDIAPIPE_GPU_GL_TEXTURE_UTIL_H_ + +#include "mediapipe/gpu/gl_base.h" +#include "mediapipe/gpu/gl_texture_view.h" + +namespace mediapipe { + +// Copies a texture to another. +// Assumes a framebuffer is already set up +void CopyGlTexture(const GlTextureView& src, GlTextureView& dst); + +// Fills a texture with a color. +void FillGlTextureRgba(GlTextureView& view, float r, float g, float b, float a); + +// RAII class to set up a temporary framebuffer. Mainly for test use. +class TempGlFramebuffer { + public: + TempGlFramebuffer() { + glGenFramebuffers(1, &framebuffer_); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_); + } + ~TempGlFramebuffer() { + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glDeleteFramebuffers(1, &framebuffer_); + } + + private: + GLuint framebuffer_; +}; + +} // namespace mediapipe + +#endif // MEDIAPIPE_GPU_GL_TEXTURE_UTIL_H_ diff --git a/mediapipe/gpu/gpu_buffer_test.cc b/mediapipe/gpu/gpu_buffer_test.cc index 3fd519b21..796cb1d9d 100644 --- a/mediapipe/gpu/gpu_buffer_test.cc +++ b/mediapipe/gpu/gpu_buffer_test.cc @@ -18,6 +18,7 @@ #include "mediapipe/framework/port/gmock.h" #include "mediapipe/framework/port/gtest.h" #include "mediapipe/framework/tool/test_util.h" +#include "mediapipe/gpu/gl_texture_util.h" #include "mediapipe/gpu/gpu_buffer_storage_ahwb.h" #include "mediapipe/gpu/gpu_buffer_storage_image_frame.h" #include "mediapipe/gpu/gpu_test_base.h" @@ -41,47 +42,6 @@ void FillImageFrameRGBA(ImageFrame& image, uint8 r, uint8 g, uint8 b, uint8 a) { } } -// Assumes a framebuffer is already set up -void CopyGlTexture(const GlTextureView& src, GlTextureView& dst) { - glViewport(0, 0, src.width(), src.height()); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, src.target(), - src.name(), 0); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(dst.target(), dst.name()); - glCopyTexSubImage2D(dst.target(), 0, 0, 0, 0, 0, dst.width(), dst.height()); - - glBindTexture(dst.target(), 0); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, src.target(), 0, - 0); -} - -void FillGlTextureRgba(GlTextureView& view, float r, float g, float b, - float a) { - glViewport(0, 0, view.width(), view.height()); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, view.target(), - view.name(), 0); - glClearColor(r, g, b, a); - glClear(GL_COLOR_BUFFER_BIT); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, view.target(), 0, - 0); -} - -class TempGlFramebuffer { - public: - TempGlFramebuffer() { - glGenFramebuffers(1, &framebuffer_); - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_); - } - ~TempGlFramebuffer() { - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glDeleteFramebuffers(1, &framebuffer_); - } - - private: - GLuint framebuffer_; -}; - class GpuBufferTest : public GpuTestBase {}; TEST_F(GpuBufferTest, BasicTest) { @@ -127,7 +87,7 @@ TEST_F(GpuBufferTest, GlTextureView) { ImageFrame red(ImageFormat::SRGBA, 300, 200); FillImageFrameRGBA(red, 255, 0, 0, 255); - EXPECT_TRUE(mediapipe::CompareImageFrames(*view, red, 0.0, 0.0)); + EXPECT_TRUE(CompareImageFrames(*view, red, 0.0, 0.0)); MP_EXPECT_OK(SavePngTestOutput(red, "gltv_red_gold")); MP_EXPECT_OK(SavePngTestOutput(*view, "gltv_red_view")); } @@ -162,7 +122,7 @@ TEST_F(GpuBufferTest, ImageFrame) { ImageFrame red(ImageFormat::SRGBA, 300, 200); FillImageFrameRGBA(red, 255, 0, 0, 255); - EXPECT_TRUE(mediapipe::CompareImageFrames(*view, red, 0.0, 0.0)); + EXPECT_TRUE(CompareImageFrames(*view, red, 0.0, 0.0)); MP_EXPECT_OK(SavePngTestOutput(red, "if_red_gold")); MP_EXPECT_OK(SavePngTestOutput(*view, "if_red_view")); } @@ -196,7 +156,7 @@ TEST_F(GpuBufferTest, Overwrite) { ImageFrame red(ImageFormat::SRGBA, 300, 200); FillImageFrameRGBA(red, 255, 0, 0, 255); - EXPECT_TRUE(mediapipe::CompareImageFrames(*view, red, 0.0, 0.0)); + EXPECT_TRUE(CompareImageFrames(*view, red, 0.0, 0.0)); MP_EXPECT_OK(SavePngTestOutput(red, "ow_red_gold")); MP_EXPECT_OK(SavePngTestOutput(*view, "ow_red_view")); } @@ -230,7 +190,7 @@ TEST_F(GpuBufferTest, Overwrite) { ImageFrame green(ImageFormat::SRGBA, 300, 200); FillImageFrameRGBA(green, 0, 255, 0, 255); - EXPECT_TRUE(mediapipe::CompareImageFrames(*view, green, 0.0, 0.0)); + EXPECT_TRUE(CompareImageFrames(*view, green, 0.0, 0.0)); MP_EXPECT_OK(SavePngTestOutput(green, "ow_green_gold")); MP_EXPECT_OK(SavePngTestOutput(*view, "ow_green_view")); } @@ -240,7 +200,7 @@ TEST_F(GpuBufferTest, Overwrite) { ImageFrame blue(ImageFormat::SRGBA, 300, 200); FillImageFrameRGBA(blue, 0, 0, 255, 255); - EXPECT_TRUE(mediapipe::CompareImageFrames(*view, blue, 0.0, 0.0)); + EXPECT_TRUE(CompareImageFrames(*view, blue, 0.0, 0.0)); MP_EXPECT_OK(SavePngTestOutput(blue, "ow_blue_gold")); MP_EXPECT_OK(SavePngTestOutput(*view, "ow_blue_view")); }