Skip to content

Commit

Permalink
feat(sdk): CreateDestinationTexture from GpuBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Dec 31, 2020
1 parent ae8d81e commit 48ba090
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Gpu/GlCalculatorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public class GlCalculatorHelper : MpResourceHandle {
return new GlTexture(texturePtr);
}

public GlTexture CreateDestinationTexture(GpuBuffer gpuBuffer) {
UnsafeNativeMethods.mp_GlCalculatorHelper__CreateDestinationTexture__Rgb(mpPtr, gpuBuffer.mpPtr, out var texturePtr).Assert();

GC.KeepAlive(this);
GC.KeepAlive(gpuBuffer);
return new GlTexture(texturePtr);
}

public uint framebuffer {
get { return SafeNativeMethods.mp_GlCalculatorHelper__framebuffer(mpPtr); }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ internal static partial class UnsafeNativeMethods {
public static extern MpReturnCode mp_GlCalculatorHelper__CreateDestinationTexture__i_i_ui(
IntPtr glCalculatorHelper, int outputWidth, int outputHeight, GpuBufferFormat formatCode, out IntPtr glTexture);

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_GlCalculatorHelper__CreateDestinationTexture__Rgb(
IntPtr glCalculatorHelper, IntPtr gpuBuffer, out IntPtr glTexture);

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_GlCalculatorHelper__BindFrameBuffer__Rtexture(IntPtr glCalculatorHelper, IntPtr glTexture);
}
Expand Down
1 change: 1 addition & 0 deletions C/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ http_archive(
patches = [
"@//third_party:mediapipe_visibility.diff",
"@//third_party:mediapipe_model_path.diff",
"@//third_party:mediapipe_extension.diff",
# "@//third_party:mediapipe_debug.diff",
],
patch_args = [
Expand Down
1 change: 0 additions & 1 deletion C/mediapipe_api/framework/calculator_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ void mp_CalculatorGraph__delete(mediapipe::CalculatorGraph* graph) {

MpReturnCode mp_CalculatorGraph__Rcgc(const char* serialized_config, int size, mediapipe::CalculatorGraph** graph_out) {
TRY_ALL {
LOG(INFO) << serialized_config;
auto config = ParseFromStringAsCalculatorGraphConfig(serialized_config, size);
*graph_out = new mediapipe::CalculatorGraph(config);
RETURN_CODE(MpReturnCode::Success);
Expand Down
15 changes: 12 additions & 3 deletions C/mediapipe_api/gpu/gl_calculator_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ MpReturnCode mp_GlCalculatorHelper__CreateSourceTexture__Rif(mediapipe::GlCalcul
mediapipe::ImageFrame* image_frame,
mediapipe::GlTexture** gl_texture_out) {
TRY_ALL {
*gl_texture_out = new mediapipe::GlTexture { gl_calculator_helper->CreateSourceTexture(std::move(*image_frame)) };
*gl_texture_out = new mediapipe::GlTexture { gl_calculator_helper->CreateSourceTexture(*image_frame) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_ALL
}
Expand All @@ -45,7 +45,7 @@ MpReturnCode mp_GlCalculatorHelper__CreateSourceTexture__Rgb(mediapipe::GlCalcul
mediapipe::GpuBuffer* gpu_buffer,
mediapipe::GlTexture** gl_texture_out) {
TRY_ALL {
*gl_texture_out = new mediapipe::GlTexture { gl_calculator_helper->CreateSourceTexture(std::move(*gpu_buffer)) };
*gl_texture_out = new mediapipe::GlTexture { gl_calculator_helper->CreateSourceTexture(*gpu_buffer) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_ALL
}
Expand All @@ -66,14 +66,23 @@ MpReturnCode mp_GlCalculatorHelper__CreateDestinationTexture__i_i_ui(mediapipe::
} CATCH_EXCEPTION
}

MpReturnCode mp_GlCalculatorHelper__CreateDestinationTexture__Rgb(mediapipe::GlCalculatorHelper* gl_calculator_helper,
mediapipe::GpuBuffer* gpu_buffer,
mediapipe::GlTexture** gl_texture_out) {
TRY_ALL {
*gl_texture_out = new mediapipe::GlTexture { gl_calculator_helper->CreateDestinationTexture(*gpu_buffer) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_ALL
}

GLuint mp_GlCalculatorHelper__framebuffer(mediapipe::GlCalculatorHelper* gl_calculator_helper) {
return gl_calculator_helper->framebuffer();
}

MpReturnCode mp_GlCalculatorHelper__BindFrameBuffer__Rtexture(mediapipe::GlCalculatorHelper* gl_calculator_helper,
mediapipe::GlTexture* gl_texture) {
TRY {
gl_calculator_helper->BindFramebuffer(std::move(*gl_texture));
gl_calculator_helper->BindFramebuffer(*gl_texture);
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}
Expand Down
3 changes: 3 additions & 0 deletions C/mediapipe_api/gpu/gl_calculator_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ MP_CAPI(MpReturnCode) mp_GlCalculatorHelper__CreateDestinationTexture__i_i_ui(me
int output_height,
mediapipe::GpuBufferFormat format,
mediapipe::GlTexture** gl_texture_out);
MP_CAPI(MpReturnCode) mp_GlCalculatorHelper__CreateDestinationTexture__Rgb(mediapipe::GlCalculatorHelper* gl_calculator_helper,
mediapipe::GpuBuffer* gpu_buffer,
mediapipe::GlTexture** gl_texture_out);
MP_CAPI(GLuint) mp_GlCalculatorHelper__framebuffer(mediapipe::GlCalculatorHelper* gl_calculator_helper);
MP_CAPI(MpReturnCode) mp_GlCalculatorHelper__BindFrameBuffer__Rtexture(mediapipe::GlCalculatorHelper* gl_calculator_helper,
mediapipe::GlTexture* gl_texture);
Expand Down
145 changes: 145 additions & 0 deletions C/third_party/mediapipe_extension.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
diff --git a/mediapipe/gpu/gl_calculator_helper.cc b/mediapipe/gpu/gl_calculator_helper.cc
index 8b63735..a60b815 100644
--- a/mediapipe/gpu/gl_calculator_helper.cc
+++ b/mediapipe/gpu/gl_calculator_helper.cc
@@ -136,6 +136,10 @@ GlTexture GlCalculatorHelper::CreateDestinationTexture(int output_width,
return impl_->CreateDestinationTexture(output_width, output_height, format);
}

+GlTexture GlCalculatorHelper::CreateDestinationTexture(const GpuBuffer& pixel_buffer) {
+ return impl_->CreateDestinationTexture(pixel_buffer);
+}
+
GlContext& GlCalculatorHelper::GetGlContext() const {
return impl_->GetGlContext();
}
diff --git a/mediapipe/gpu/gl_calculator_helper.h b/mediapipe/gpu/gl_calculator_helper.h
index 53178da..f400c43 100644
--- a/mediapipe/gpu/gl_calculator_helper.h
+++ b/mediapipe/gpu/gl_calculator_helper.h
@@ -144,6 +144,8 @@ class GlCalculatorHelper {
int output_width, int output_height,
GpuBufferFormat format = GpuBufferFormat::kBGRA32);

+ GlTexture CreateDestinationTexture(const GpuBuffer& pixel_buffer);
+
// The OpenGL name of the output framebuffer.
GLuint framebuffer() const;

diff --git a/mediapipe/gpu/gl_calculator_helper_impl.h b/mediapipe/gpu/gl_calculator_helper_impl.h
index 2a20b84..9953081 100644
--- a/mediapipe/gpu/gl_calculator_helper_impl.h
+++ b/mediapipe/gpu/gl_calculator_helper_impl.h
@@ -50,6 +50,7 @@ class GlCalculatorHelperImpl {
// Creates a framebuffer and returns the texture that it is bound to.
GlTexture CreateDestinationTexture(int output_width, int output_height,
GpuBufferFormat format);
+ GlTexture CreateDestinationTexture(const GpuBuffer& gpu_buffer);

GLuint framebuffer() const { return framebuffer_; }
void BindFramebuffer(const GlTexture& dst);
diff --git a/mediapipe/gpu/gl_calculator_helper_impl_common.cc b/mediapipe/gpu/gl_calculator_helper_impl_common.cc
index 5a46042..7f01362 100644
--- a/mediapipe/gpu/gl_calculator_helper_impl_common.cc
+++ b/mediapipe/gpu/gl_calculator_helper_impl_common.cc
@@ -190,14 +190,18 @@ GlTextureBufferSharedPtr GlCalculatorHelperImpl::MakeGlTextureBuffer(

GlTexture GlCalculatorHelperImpl::CreateDestinationTexture(
int width, int height, GpuBufferFormat format) {
+ GpuBuffer buffer =
+ gpu_resources_.gpu_buffer_pool().GetBuffer(width, height, format);
+
+ return CreateDestinationTexture(buffer);
+}
+
+GlTexture GlCalculatorHelperImpl::CreateDestinationTexture(const GpuBuffer& gpu_buffer) {
if (!framebuffer_) {
CreateFramebuffer();
}

- GpuBuffer buffer =
- gpu_resources_.gpu_buffer_pool().GetBuffer(width, height, format);
- GlTexture texture = MapGpuBuffer(buffer, 0);
-
+ GlTexture texture = MapGpuBuffer(gpu_buffer, 0);
return texture;
}

diff --git a/mediapipe/gpu/gl_scaler_calculator.cc b/mediapipe/gpu/gl_scaler_calculator.cc
index 8806267..92200ae 100644
--- a/mediapipe/gpu/gl_scaler_calculator.cc
+++ b/mediapipe/gpu/gl_scaler_calculator.cc
@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

+// Modified to enable to specify the target GpuBuffer
+
#include "mediapipe/framework/calculator_framework.h"
#include "mediapipe/framework/port/ret_check.h"
#include "mediapipe/framework/port/status.h"
@@ -58,6 +60,7 @@ namespace mediapipe {
// existing calculator options, depending on field merge_fields.
// OUTPUT_DIMENSIONS: the output width and height in pixels.
// ROTATION: the counterclockwise rotation angle in degrees.
+// DESTINATION: the target GpuBuffer
// These can also be specified as options.
// To enable horizontal or vertical flip, specify them in options.
// The flipping is applied after rotation.
@@ -82,6 +85,7 @@ class GlScalerCalculator : public CalculatorBase {

private:
GlCalculatorHelper helper_;
+ GpuBuffer dst_buffer_;
int dst_width_ = 0;
int dst_height_ = 0;
float dst_scale_ = -1.f;
@@ -109,6 +113,9 @@ REGISTER_CALCULATOR(GlScalerCalculator);
}
MP_RETURN_IF_ERROR(GlCalculatorHelper::UpdateContract(cc));

+ if (cc->InputSidePackets().HasTag("DESTINATION")) {
+ cc->InputSidePackets().Tag("DESTINATION").Set<GpuBuffer>();
+ }
if (cc->InputSidePackets().HasTag("OPTIONS")) {
cc->InputSidePackets().Tag("OPTIONS").Set<GlScalerCalculatorOptions>();
}
@@ -175,6 +182,13 @@ REGISTER_CALCULATOR(GlScalerCalculator);
dst_width_ = dimensions[0];
dst_height_ = dimensions[1];
}
+
+ if (HasTagOrIndex(cc->InputSidePackets(), "DESTINATION", 1)) {
+ dst_buffer_ = cc->InputSidePackets().Tag("DESTINATION").Get<GpuBuffer>();
+ dst_width_ = dst_buffer_.width();
+ dst_height_ = dst_buffer_.height();
+ }
+
if (cc->InputSidePackets().HasTag("ROTATION")) {
rotation_ccw = cc->InputSidePackets().Tag("ROTATION").Get<int>();
}
@@ -185,7 +199,7 @@ REGISTER_CALCULATOR(GlScalerCalculator);
}

::mediapipe::Status GlScalerCalculator::Process(CalculatorContext* cc) {
- if (cc->Inputs().HasTag("OUTPUT_DIMENSIONS")) {
+ if (!dst_buffer_ && cc->Inputs().HasTag("OUTPUT_DIMENSIONS")) {
if (cc->Inputs().Tag("OUTPUT_DIMENSIONS").IsEmpty()) {
// OUTPUT_DIMENSIONS input stream is specified, but value is missing.
return ::mediapipe::OkStatus();
@@ -264,8 +278,13 @@ REGISTER_CALCULATOR(GlScalerCalculator);
MakePacket<float>(left_right_padding).At(cc->InputTimestamp()));
}

- auto dst = helper_.CreateDestinationTexture(dst_width, dst_height,
- GetOutputFormat());
+ GlTexture dst;
+ if (dst_buffer_) {
+ dst_buffer_.GetGlTextureBufferSharedPtr()->Reuse();
+ dst = helper_.CreateDestinationTexture(dst_buffer_);
+ } else {
+ dst = helper_.CreateDestinationTexture(dst_width, dst_height, GetOutputFormat());
+ }

helper_.BindFramebuffer(dst);
glActiveTexture(GL_TEXTURE1);

0 comments on commit 48ba090

Please sign in to comment.