Skip unnecessary cpu<->gpu conversion if the input and output are already on the same storage.

PiperOrigin-RevId: 518573284
This commit is contained in:
Jiuqiang Tang 2023-03-22 08:19:49 -07:00 committed by Copybara-Service
parent 18b4caa7f3
commit 21e0ff3d4e

View File

@ -81,7 +81,8 @@ class ImageCloneCalculator : public Node {
absl::Status Process(CalculatorContext* cc) override { absl::Status Process(CalculatorContext* cc) override {
std::unique_ptr<Image> output; std::unique_ptr<Image> output;
const auto& input = *kIn(cc); const auto& input = *kIn(cc);
if (input.UsesGpu()) { bool input_on_gpu = input.UsesGpu();
if (input_on_gpu) {
#if !MEDIAPIPE_DISABLE_GPU #if !MEDIAPIPE_DISABLE_GPU
// Create an output Image that co-owns the underlying texture buffer as // Create an output Image that co-owns the underlying texture buffer as
// the input Image. // the input Image.
@ -97,15 +98,15 @@ class ImageCloneCalculator : public Node {
// Image. This ensures a correct life span of the shared pixel data. // Image. This ensures a correct life span of the shared pixel data.
output = std::make_unique<Image>(std::make_unique<mediapipe::ImageFrame>( output = std::make_unique<Image>(std::make_unique<mediapipe::ImageFrame>(
input.image_format(), input.width(), input.height(), input.step(), input.image_format(), input.width(), input.height(), input.step(),
const_cast<uint8*>(input.GetImageFrameSharedPtr()->PixelData()), const_cast<uint8_t*>(input.GetImageFrameSharedPtr()->PixelData()),
[packet_copy_ptr](uint8*) { delete packet_copy_ptr; })); [packet_copy_ptr](uint8_t*) { delete packet_copy_ptr; }));
} }
if (output_on_gpu_) { if (output_on_gpu_ && !input_on_gpu) {
#if !MEDIAPIPE_DISABLE_GPU #if !MEDIAPIPE_DISABLE_GPU
gpu_helper_.RunInGlContext([&output]() { output->ConvertToGpu(); }); gpu_helper_.RunInGlContext([&output]() { output->ConvertToGpu(); });
#endif // !MEDIAPIPE_DISABLE_GPU #endif // !MEDIAPIPE_DISABLE_GPU
} else { } else if (!output_on_gpu_ && input_on_gpu) {
output->ConvertToCpu(); output->ConvertToCpu();
} }
kOut(cc).Send(std::move(output)); kOut(cc).Send(std::move(output));