Skip to content

Commit

Permalink
Add EglSyncPoint/CreateEglSyncPoint.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 694273478
  • Loading branch information
MediaPipe Team authored and copybara-github committed Nov 7, 2024
1 parent 4e1ee5d commit e552e27
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 10 deletions.
28 changes: 25 additions & 3 deletions mediapipe/gpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ cc_library(
}),
)

cc_library(
name = "egl_base",
textual_hdrs = ["egl_base.h"],
visibility = ["//visibility:public"],
deps = [":gl_base"],
)

cc_library(
name = "gl_base_hdr",
hdrs = ["gl_base.h"],
Expand Down Expand Up @@ -219,6 +226,7 @@ cc_library(
"//mediapipe/framework/port:status",
"//mediapipe/framework/port:statusor",
"//mediapipe/framework/port:threadpool",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:dynamic_annotations",
"@com_google_absl//absl/debugging:leak_check",
"@com_google_absl//absl/log:absl_check",
Expand Down Expand Up @@ -849,7 +857,7 @@ cc_library(
hdrs = ["egl_errors.h"],
visibility = ["//visibility:public"],
deps = [
":gl_base",
":egl_base",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
Expand All @@ -861,13 +869,12 @@ cc_library(
hdrs = ["egl_sync.h"],
visibility = ["//visibility:public"],
deps = [
":egl_base",
":egl_errors",
":gl_base",
"//mediapipe/framework/deps:no_destructor",
"//mediapipe/framework/formats:unique_fd",
"//mediapipe/framework/port:ret_check",
"//mediapipe/framework/port:status",
"//third_party/GL:EGL_headers",
"@com_google_absl//absl/cleanup",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/status",
Expand All @@ -876,6 +883,21 @@ cc_library(
],
)

cc_library(
name = "egl_sync_point",
srcs = ["egl_sync_point.cc"],
hdrs = ["egl_sync_point.h"],
visibility = ["//visibility:public"],
deps = [
":egl_base",
":egl_sync",
":gl_context",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
],
)

cc_library(
name = "gl_texture_util",
srcs = ["gl_texture_util.cc"],
Expand Down
15 changes: 15 additions & 0 deletions mediapipe/gpu/egl_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef MEDIAPIPE_GPU_EGL_BASE_H_
#define MEDIAPIPE_GPU_EGL_BASE_H_

#include "mediapipe/gpu/gl_base.h"

#if defined(HAS_EGL)

// TODO: b/377324183 - merge into gl_base.h

#include <EGL/egl.h>
#include <EGL/eglext.h>

#endif // defined(HAS_EGL)

#endif // MEDIAPIPE_GPU_EGL_BASE_H_
2 changes: 1 addition & 1 deletion mediapipe/gpu/egl_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "mediapipe/gpu/gl_base.h"
#include "mediapipe/gpu/egl_base.h"

namespace mediapipe {

Expand Down
11 changes: 9 additions & 2 deletions mediapipe/gpu/egl_sync.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "mediapipe/gpu/egl_sync.h"

#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <unistd.h>

#include <cstring>
Expand All @@ -16,6 +14,7 @@
#include "mediapipe/framework/formats/unique_fd.h"
#include "mediapipe/framework/port/ret_check.h"
#include "mediapipe/framework/port/status_macros.h"
#include "mediapipe/gpu/egl_base.h"
#include "mediapipe/gpu/egl_errors.h"

namespace mediapipe {
Expand Down Expand Up @@ -142,6 +141,14 @@ absl::StatusOr<EglSync> EglSync::CreateNative(EGLDisplay display,
return EglSync(display, egl_sync);
}

bool EglSync::IsSupported(EGLDisplay display) {
return CheckEglSyncSupported(display).ok();
}

bool EglSync::IsNativeSupported(EGLDisplay display) {
return CheckEglNativeSyncSupported(display).ok();
}

EglSync::EglSync(EglSync&& sync) { *this = std::move(sync); }

EglSync& EglSync::operator=(EglSync&& sync) {
Expand Down
8 changes: 4 additions & 4 deletions mediapipe/gpu/egl_sync.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#ifndef MEDIAPIPE_GPU_EGL_SYNC_H_
#define MEDIAPIPE_GPU_EGL_SYNC_H_

#include <EGL/egl.h>
#include <EGL/eglext.h>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "mediapipe/framework/formats/unique_fd.h"
#include "mediapipe/gpu/gl_base.h"
#include "mediapipe/gpu/egl_base.h"

namespace mediapipe {

Expand All @@ -26,6 +23,9 @@ class EglSync {
static absl::StatusOr<EglSync> CreateNative(EGLDisplay display,
const UniqueFd& native_fence_fd);

static bool IsSupported(EGLDisplay display);
static bool IsNativeSupported(EGLDisplay display);

// Move-only
EglSync(EglSync&& sync);
EglSync& operator=(EglSync&& sync);
Expand Down
88 changes: 88 additions & 0 deletions mediapipe/gpu/egl_sync_point.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "mediapipe/gpu/egl_sync_point.h"

#include <memory>
#include <utility>

#include "absl/log/absl_log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "mediapipe/gpu/egl_sync.h"
#include "mediapipe/gpu/gl_context.h"

namespace mediapipe {

namespace {

class EglFenceSyncPoint : public GlSyncPoint {
public:
explicit EglFenceSyncPoint(std::shared_ptr<GlContext> gl_context,
EglSync egl_sync)
: GlSyncPoint(std::move(gl_context)), egl_sync_(std::move(egl_sync)) {}

~EglFenceSyncPoint() override {
gl_context_->RunWithoutWaiting(
[ptr = new EglSync(std::move(egl_sync_))]() { delete ptr; });
}

EglFenceSyncPoint(const EglFenceSyncPoint&) = delete;
EglFenceSyncPoint& operator=(const EglFenceSyncPoint&) = delete;

void Wait() override {
if (GlContext::IsAnyContextCurrent()) {
WaitInternal();
}
// Fall back to GL context used during sync creation.
gl_context_->Run([this] { WaitInternal(); });
}

void WaitInternal() {
absl::Status result = egl_sync_.Wait();
if (!result.ok()) {
ABSL_LOG(DFATAL) << "EGL sync Wait failed: " << result;
}
}

void WaitOnGpu() override {
if (!GlContext::IsAnyContextCurrent()) {
ABSL_LOG(DFATAL) << "WaitOnGpu without current context.";
}

absl::Status result = egl_sync_.WaitOnGpu();
if (!result.ok()) {
ABSL_LOG(DFATAL) << "EGL sync WaitOnGpu failed: " << result;
}
}

bool IsReady() override {
if (GlContext::IsAnyContextCurrent()) {
return IsReadyInternal();
}

// Fall back to GL context used during sync creation.
bool ready = false;
gl_context_->Run([this, &ready] { ready = IsReadyInternal(); });
return ready;
}

bool IsReadyInternal() {
absl::StatusOr<bool> is_ready = egl_sync_.IsSignaled();
if (!is_ready.ok()) {
ABSL_LOG(DFATAL) << "EGL sync IsSignaled failed: " << is_ready.status();
return false;
}
return *is_ready;
}

private:
EglSync egl_sync_;
};

} // namespace

absl::StatusOr<std::unique_ptr<GlSyncPoint>> CreateEglSyncPoint(
std::shared_ptr<GlContext> gl_context, EglSync egl_sync) {
return std::make_unique<EglFenceSyncPoint>(std::move(gl_context),
std::move(egl_sync));
}

} // namespace mediapipe
17 changes: 17 additions & 0 deletions mediapipe/gpu/egl_sync_point.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef MEDIAPIPE_GPU_EGL_SYNC_POINT_H_
#define MEDIAPIPE_GPU_EGL_SYNC_POINT_H_

#include <memory>

#include "absl/status/statusor.h"
#include "mediapipe/gpu/egl_sync.h"
#include "mediapipe/gpu/gl_context.h"

namespace mediapipe {

absl::StatusOr<std::unique_ptr<GlSyncPoint>> CreateEglSyncPoint(
std::shared_ptr<GlContext> gl_context, EglSync egl_sync);

} // namespace mediapipe

#endif // MEDIAPIPE_GPU_EGL_SYNC_POINT_H_

0 comments on commit e552e27

Please sign in to comment.