From bf0771af59019f6a7554868f620d7d57c8993361 Mon Sep 17 00:00:00 2001 From: Chris Lomont Date: Thu, 28 Sep 2023 18:28:52 -0700 Subject: [PATCH] Exporting Android native hardware buffers out of IGL Summary: This diff exposes an Android shared CPU+GPU memory buffer on EGL. Differential Revision: D49734694 fbshipit-source-id: 5d84543cb5ece77ced184ba1f173ac05fb6d76a4 --- src/igl/opengl/egl/PlatformDevice.cpp | 32 +++++++++++++++++++ src/igl/opengl/egl/PlatformDevice.h | 9 ++++++ src/igl/opengl/egl/android/NativeHWBuffer.cpp | 2 +- src/igl/opengl/egl/android/NativeHWBuffer.h | 2 +- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/igl/opengl/egl/PlatformDevice.cpp b/src/igl/opengl/egl/PlatformDevice.cpp index ccec01baa5..71bb244370 100644 --- a/src/igl/opengl/egl/PlatformDevice.cpp +++ b/src/igl/opengl/egl/PlatformDevice.cpp @@ -10,6 +10,9 @@ #include #include #include +#if IGL_PLATFORM_ANDROID && __ANDROID_MIN_SDK_VERSION__ >= 26 +#include +#endif #include #include @@ -142,6 +145,35 @@ std::shared_ptr PlatformDevice::createTextureFromNativeDepth(Result* o return texture; } +#if IGL_PLATFORM_ANDROID && __ANDROID_MIN_SDK_VERSION__ >= 26 + +/// returns a android::NativeHWTextureBuffer on platforms supporting it +/// this texture allows CPU and GPU to both read/write memory +std::shared_ptr PlatformDevice::createTextureWithSharedMemory(const TextureDesc& desc, + Result* outResult) { + auto context = static_cast(getSharedContext().get()); + if (context == nullptr) { + Result::setResult(outResult, Result::Code::InvalidOperation, "No EGL context found!"); + return nullptr; + } + + Result subResult; + + auto texture = std::make_shared(getContext(), desc.format); + subResult = texture->create(desc, false); + Result::setResult(outResult, subResult.code, subResult.message); + if (!subResult.isOk()) { + return nullptr; + } + + if (auto resourceTracker = owner_.getResourceTracker()) { + texture->initResourceTracker(resourceTracker); + } + + return texture; +} +#endif + void PlatformDevice::updateSurfaces(EGLSurface readSurface, EGLSurface drawSurface, Result* outResult) { diff --git a/src/igl/opengl/egl/PlatformDevice.h b/src/igl/opengl/egl/PlatformDevice.h index cb16dee9fe..2c5dc53a53 100644 --- a/src/igl/opengl/egl/PlatformDevice.h +++ b/src/igl/opengl/egl/PlatformDevice.h @@ -39,6 +39,15 @@ class PlatformDevice : public opengl::PlatformDevice { /// Returns a texture representing the EGL depth texture associated with this device's context. std::shared_ptr createTextureFromNativeDepth(Result* outResult); +#if IGL_PLATFORM_ANDROID && __ANDROID_MIN_SDK_VERSION__ >= 26 + + /// returns a android::NativeHWTextureBuffer on platforms supporting it + /// this texture allows CPU and GPU to both read/write memory + std::shared_ptr createTextureWithSharedMemory(const TextureDesc& desc, + Result* outResult); + +#endif + /// This function must be called every time the currently bound EGL read and/or draw surfaces /// change, in order to notify IGL of these changes. void updateSurfaces(EGLSurface readSurface, EGLSurface drawSurface, Result* outResult); diff --git a/src/igl/opengl/egl/android/NativeHWBuffer.cpp b/src/igl/opengl/egl/android/NativeHWBuffer.cpp index 2840011854..565fc55316 100644 --- a/src/igl/opengl/egl/android/NativeHWBuffer.cpp +++ b/src/igl/opengl/egl/android/NativeHWBuffer.cpp @@ -16,7 +16,7 @@ #include #include -#if defined(__ANDROID_API__) && __ANDROID_MIN_SDK_VERSION__ >= 26 +#if IGL_PLATFORM_ANDROID && __ANDROID_MIN_SDK_VERSION__ >= 26 #if defined(IGL_API_LOG) && (IGL_DEBUG || defined(IGL_FORCE_ENABLE_LOGS)) #define APILOG_DEC_DRAW_COUNT() \ diff --git a/src/igl/opengl/egl/android/NativeHWBuffer.h b/src/igl/opengl/egl/android/NativeHWBuffer.h index f64899a429..ee10796cc6 100644 --- a/src/igl/opengl/egl/android/NativeHWBuffer.h +++ b/src/igl/opengl/egl/android/NativeHWBuffer.h @@ -9,7 +9,7 @@ #include -#if defined(__ANDROID_API__) && __ANDROID_MIN_SDK_VERSION__ >= 26 +#if IGL_PLATFORM_ANDROID && __ANDROID_MIN_SDK_VERSION__ >= 26 struct AHardwareBuffer;