Use kUtilityFramebuffer in ReadTexture
This avoids creating a temporary framebuffer each time. PiperOrigin-RevId: 490163892
This commit is contained in:
parent
a8b7761022
commit
bacbac8d92
|
@ -15,6 +15,7 @@
|
||||||
#include "mediapipe/gpu/gl_texture_buffer.h"
|
#include "mediapipe/gpu/gl_texture_buffer.h"
|
||||||
|
|
||||||
#include "mediapipe/framework/formats/image_frame.h"
|
#include "mediapipe/framework/formats/image_frame.h"
|
||||||
|
#include "mediapipe/gpu/gl_context.h"
|
||||||
#include "mediapipe/gpu/gl_texture_view.h"
|
#include "mediapipe/gpu/gl_texture_view.h"
|
||||||
#include "mediapipe/gpu/gpu_buffer_storage_image_frame.h"
|
#include "mediapipe/gpu/gpu_buffer_storage_image_frame.h"
|
||||||
|
|
||||||
|
@ -333,8 +334,8 @@ void GlTextureBuffer::ViewDoneWriting(const GlTextureView& view) {
|
||||||
#endif // __ANDROID__
|
#endif // __ANDROID__
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ReadTexture(const GlTextureView& view, GpuBufferFormat format,
|
static void ReadTexture(GlContext& ctx, const GlTextureView& view,
|
||||||
void* output, size_t size) {
|
GpuBufferFormat format, void* output, size_t size) {
|
||||||
// TODO: check buffer size? We could use glReadnPixels where available
|
// TODO: check buffer size? We could use glReadnPixels where available
|
||||||
// (OpenGL ES 3.2, i.e. nowhere). Note that, to fully check that the read
|
// (OpenGL ES 3.2, i.e. nowhere). Note that, to fully check that the read
|
||||||
// won't overflow the buffer with glReadPixels, we'd also need to check or
|
// won't overflow the buffer with glReadPixels, we'd also need to check or
|
||||||
|
@ -347,10 +348,7 @@ static void ReadTexture(const GlTextureView& view, GpuBufferFormat format,
|
||||||
GLint previous_fbo;
|
GLint previous_fbo;
|
||||||
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previous_fbo);
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previous_fbo);
|
||||||
|
|
||||||
// We use a temp fbo to avoid depending on the app having an existing one.
|
GLuint fbo = kUtilityFramebuffer.Get(ctx);
|
||||||
// TODO: keep a utility fbo around in the context?
|
|
||||||
GLuint fbo = 0;
|
|
||||||
glGenFramebuffers(1, &fbo);
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, view.target(),
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, view.target(),
|
||||||
view.name(), 0);
|
view.name(), 0);
|
||||||
|
@ -360,7 +358,6 @@ static void ReadTexture(const GlTextureView& view, GpuBufferFormat format,
|
||||||
0);
|
0);
|
||||||
// TODO: just set the binding to 0 to avoid the get call?
|
// TODO: just set the binding to 0 to avoid the get call?
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, previous_fbo);
|
glBindFramebuffer(GL_FRAMEBUFFER, previous_fbo);
|
||||||
glDeleteFramebuffers(1, &fbo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::shared_ptr<GpuBufferStorageImageFrame> ConvertToImageFrame(
|
static std::shared_ptr<GpuBufferStorageImageFrame> ConvertToImageFrame(
|
||||||
|
@ -370,9 +367,10 @@ static std::shared_ptr<GpuBufferStorageImageFrame> ConvertToImageFrame(
|
||||||
auto output =
|
auto output =
|
||||||
absl::make_unique<ImageFrame>(image_format, buf->width(), buf->height(),
|
absl::make_unique<ImageFrame>(image_format, buf->width(), buf->height(),
|
||||||
ImageFrame::kGlDefaultAlignmentBoundary);
|
ImageFrame::kGlDefaultAlignmentBoundary);
|
||||||
buf->GetProducerContext()->Run([buf, &output] {
|
auto ctx = buf->GetProducerContext();
|
||||||
|
ctx->Run([buf, &output, &ctx] {
|
||||||
auto view = buf->GetReadView(internal::types<GlTextureView>{}, /*plane=*/0);
|
auto view = buf->GetReadView(internal::types<GlTextureView>{}, /*plane=*/0);
|
||||||
ReadTexture(view, buf->format(), output->MutablePixelData(),
|
ReadTexture(*ctx, view, buf->format(), output->MutablePixelData(),
|
||||||
output->PixelDataSize());
|
output->PixelDataSize());
|
||||||
});
|
});
|
||||||
return std::make_shared<GpuBufferStorageImageFrame>(std::move(output));
|
return std::make_shared<GpuBufferStorageImageFrame>(std::move(output));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user