Allow conversion of GlTextureBuffer to CVPixelBufferRef

This means that, if an iOS application sends in a GlTextureBuffer but expects a CVPixelBufferRef as output, everything will work even if the graph just forwards the same input. Also, access by Metal calculators will also work transparently.

PiperOrigin-RevId: 488812748
This commit is contained in:
Camillo Lugaresi 2022-11-15 18:31:27 -08:00 committed by Copybara-Service
parent fe66de3714
commit 4c874fe4cd
2 changed files with 55 additions and 1 deletions

View File

@ -226,7 +226,13 @@ cc_library(
# depend on having an indirect image_frame dependency, need to be
# fixed first.
"//mediapipe/framework/formats:image_frame",
],
] + select({
"//conditions:default": [],
":platform_ios_with_gpu": [
":gl_texture_util",
":gpu_buffer_storage_cv_pixel_buffer",
],
}),
)
cc_library(
@ -344,6 +350,25 @@ cc_library(
],
)
mediapipe_cc_test(
name = "gpu_buffer_storage_cv_pixel_buffer_test",
size = "small",
timeout = "moderate",
srcs = ["gpu_buffer_storage_cv_pixel_buffer_test.cc"],
platforms = ["ios"],
deps = [
":gl_texture_buffer",
":gl_texture_util",
":gpu_buffer",
":gpu_buffer_storage_cv_pixel_buffer",
":gpu_test_base",
"//mediapipe/framework/port:gtest_main",
"//mediapipe/framework/tool:test_util",
"//mediapipe/objc:util",
"@com_google_absl//absl/strings",
],
)
cc_library(
name = "cv_texture_cache_manager",
srcs = ["cv_texture_cache_manager.cc"],

View File

@ -18,6 +18,11 @@
#include "mediapipe/gpu/gl_texture_view.h"
#include "mediapipe/gpu/gpu_buffer_storage_image_frame.h"
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
#include "mediapipe/gpu/gl_texture_util.h"
#include "mediapipe/gpu/gpu_buffer_storage_cv_pixel_buffer.h"
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
namespace mediapipe {
std::unique_ptr<GlTextureBuffer> GlTextureBuffer::Wrap(
@ -380,4 +385,28 @@ static auto kConverterRegistration2 =
.RegisterConverter<GpuBufferStorageImageFrame, GlTextureBuffer>(
ConvertFromImageFrame);
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
static std::shared_ptr<GpuBufferStorageCvPixelBuffer> ConvertToCvPixelBuffer(
std::shared_ptr<GlTextureBuffer> buf) {
auto output = absl::make_unique<GpuBufferStorageCvPixelBuffer>(
buf->width(), buf->height(), buf->format());
buf->GetProducerContext()->Run([buf, &output] {
TempGlFramebuffer framebuffer;
auto src = buf->GetReadView(internal::types<GlTextureView>{}, nullptr, 0);
auto dst =
output->GetWriteView(internal::types<GlTextureView>{}, nullptr, 0);
CopyGlTexture(src, dst);
glFlush();
});
return output;
}
static auto kConverterRegistrationCvpb =
internal::GpuBufferStorageRegistry::Get()
.RegisterConverter<GlTextureBuffer, GpuBufferStorageCvPixelBuffer>(
ConvertToCvPixelBuffer);
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
} // namespace mediapipe