Skip to content

Commit

Permalink
Exporting Android native hardware buffers out of IGL
Browse files Browse the repository at this point in the history
Summary: This diff exposes an Android shared CPU+GPU memory buffer on EGL.

Differential Revision: D49734694

fbshipit-source-id: 5d84543cb5ece77ced184ba1f173ac05fb6d76a4
  • Loading branch information
Chris Lomont authored and facebook-github-bot committed Sep 29, 2023
1 parent b107f38 commit bf0771a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/igl/opengl/egl/PlatformDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <igl/opengl/egl/Context.h>
#include <igl/opengl/egl/Device.h>
#include <igl/opengl/egl/PlatformDevice.h>
#if IGL_PLATFORM_ANDROID && __ANDROID_MIN_SDK_VERSION__ >= 26
#include <igl/opengl/egl/android/NativeHWBuffer.h>
#endif
#include <sstream>
#include <utility>

Expand Down Expand Up @@ -142,6 +145,35 @@ std::shared_ptr<ITexture> 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<ITexture> PlatformDevice::createTextureWithSharedMemory(const TextureDesc& desc,
Result* outResult) {
auto context = static_cast<Context*>(getSharedContext().get());
if (context == nullptr) {
Result::setResult(outResult, Result::Code::InvalidOperation, "No EGL context found!");
return nullptr;
}

Result subResult;

auto texture = std::make_shared<android::NativeHWTextureBuffer>(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) {
Expand Down
9 changes: 9 additions & 0 deletions src/igl/opengl/egl/PlatformDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITexture> 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<ITexture> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/igl/opengl/egl/android/NativeHWBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <igl/opengl/Errors.h>
#include <igl/opengl/GLIncludes.h>

#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() \
Expand Down
2 changes: 1 addition & 1 deletion src/igl/opengl/egl/android/NativeHWBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <igl/opengl/TextureBufferBase.h>

#if defined(__ANDROID_API__) && __ANDROID_MIN_SDK_VERSION__ >= 26
#if IGL_PLATFORM_ANDROID && __ANDROID_MIN_SDK_VERSION__ >= 26

struct AHardwareBuffer;

Expand Down

0 comments on commit bf0771a

Please sign in to comment.