From 79c3693f7c28724829c4a0b1ef6c68e9102f1137 Mon Sep 17 00:00:00 2001 From: Alex Chong Date: Wed, 20 Mar 2024 12:50:34 -0700 Subject: [PATCH] move android egl methods to egl context Summary: # Motivation: This is a followup to D53689710. The methods added there don't fit nicely into NativeHWBuffer and are a better fit in egl::Context # Overview: - log params for eglCreateImageKHR - revert changes from D53689710 and add them to egl::Context instead` Reviewed By: EricGriffith Differential Revision: D54870657 fbshipit-source-id: b4bc6cb81514a12a3943230bebb91b770bbde0d9 --- src/igl/opengl/egl/Context.cpp | 40 ++++++++++++++++++- src/igl/opengl/egl/Context.h | 5 +++ src/igl/opengl/egl/android/NativeHWBuffer.cpp | 40 +++++-------------- src/igl/opengl/egl/android/NativeHWBuffer.h | 3 -- 4 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/igl/opengl/egl/Context.cpp b/src/igl/opengl/egl/Context.cpp index e0d7df4f6c..b8b2de3578 100644 --- a/src/igl/opengl/egl/Context.cpp +++ b/src/igl/opengl/egl/Context.cpp @@ -5,11 +5,13 @@ * LICENSE file in the root directory of this source tree. */ +#include + #include +#include #include #include #include -#include #include #include @@ -403,6 +405,42 @@ EGLConfig Context::getConfig() const { return config_; } +#if IGL_PLATFORM_ANDROID && __ANDROID_MIN_SDK_VERSION__ >= 26 +EGLImageKHR Context::createImageFromAndroidHardwareBuffer(AHardwareBuffer* hwb) const { + EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(hwb); + EGLint attribs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE, EGL_NONE, EGL_NONE}; + + EGLDisplay display = this->getDisplay(); + // eglCreateImageKHR will add a ref to the AHardwareBuffer + EGLImageKHR eglImage = + eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attribs); + IGL_DEBUG_LOG("eglCreateImageKHR(%p, %x, %x, %p, {%d, %d, %d, %d, %d})\n", + display, + EGL_NO_CONTEXT, + EGL_NATIVE_BUFFER_ANDROID, + clientBuffer, + attribs[0], + attribs[1], + attribs[2], + attribs[3], + attribs[4]); + + this->checkForErrors(__FUNCTION__, __LINE__); + + IGL_REPORT_ERROR(this->isCurrentContext() || this->isCurrentSharegroup()); + + return eglImage; +} +#endif + +void Context::imageTargetTexture(EGLImageKHR eglImage, GLenum target) const { + glEGLImageTargetTexture2DOES(target, static_cast(eglImage)); + IGL_DEBUG_LOG("glEGLImageTargetTexture2DOES(%u, %#x)\n", + GL_TEXTURE_2D, + static_cast(eglImage)); + this->checkForErrors(__FUNCTION__, __LINE__); +} + } // namespace egl } // namespace opengl } // namespace igl diff --git a/src/igl/opengl/egl/Context.h b/src/igl/opengl/egl/Context.h index a7c092482c..2f3c54e082 100644 --- a/src/igl/opengl/egl/Context.h +++ b/src/igl/opengl/egl/Context.h @@ -72,6 +72,11 @@ class Context final : public IContext { /// Mark this context as belonging to a sharegroup with another context. void markSharegroup(Context& context); + void imageTargetTexture(EGLImageKHR eglImage, GLenum target) const; + +#if IGL_PLATFORM_ANDROID && __ANDROID_MIN_SDK_VERSION__ >= 26 + EGLImageKHR createImageFromAndroidHardwareBuffer(AHardwareBuffer* hwb) const; +#endif private: Context(RenderingAPI api, EGLContext shareContext, diff --git a/src/igl/opengl/egl/android/NativeHWBuffer.cpp b/src/igl/opengl/egl/android/NativeHWBuffer.cpp index af69afc5ae..b705307a3a 100644 --- a/src/igl/opengl/egl/android/NativeHWBuffer.cpp +++ b/src/igl/opengl/egl/android/NativeHWBuffer.cpp @@ -169,7 +169,16 @@ Result NativeHWTextureBuffer::createHWBuffer(const TextureDesc& desc, // eglCreateImageKHR will add a ref to the AHardwareBuffer EGLImageKHR eglImage = eglCreateImageKHR( display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attribs); - APILOG("eglCreateImageKHR()\n"); + APILOG("eglCreateImageKHR(%p, %x, %x, %p, {%d, %d, %d, %d, %d})\n", + display, + EGL_NO_CONTEXT, + EGL_NATIVE_BUFFER_ANDROID, + clientBuffer, + attribs[0], + attribs[1], + attribs[2], + attribs[3], + attribs[4]); if (EGL_NO_IMAGE_KHR == eglImage) { return Result{Result::Code::RuntimeError, "Could not create EGL image, err"}; @@ -299,35 +308,6 @@ bool NativeHWTextureBuffer::isValidFormat(TextureFormat format) { return toNativeHWFormat(format) > 0; } -Result NativeHWTextureBuffer::bindTextureWithHWBuffer(IContext& context, - GLuint target, - const AHardwareBuffer* hwb) noexcept { - EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(hwb); - EGLint attribs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE, EGL_NONE, EGL_NONE}; - - EGLDisplay display = ((egl::Context*)&context)->getDisplay(); - // eglCreateImageKHR will add a ref to the AHardwareBuffer - EGLImageKHR eglImage = - eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attribs); - APILOG("eglCreateImageKHR()\n"); - - if (EGL_NO_IMAGE_KHR == eglImage) { - return Result{Result::Code::RuntimeError, "Could not create EGL image, err"}; - } - context.checkForErrors(__FUNCTION__, __LINE__); - - IGL_REPORT_ERROR(context.isCurrentContext() || context.isCurrentSharegroup()); - - glEGLImageTargetTexture2DOES(target, static_cast(eglImage)); - APILOG("glEGLImageTargetTexture2DOES(%u, %#x)\n", - GL_TEXTURE_2D, - static_cast(eglImage)); - - context.checkForErrors(__FUNCTION__, __LINE__); - - return Result{}; -} - } // namespace igl::opengl::egl::android #endif diff --git a/src/igl/opengl/egl/android/NativeHWBuffer.h b/src/igl/opengl/egl/android/NativeHWBuffer.h index 781dce224a..b5546df4b9 100644 --- a/src/igl/opengl/egl/android/NativeHWBuffer.h +++ b/src/igl/opengl/egl/android/NativeHWBuffer.h @@ -32,9 +32,6 @@ class NativeHWTextureBuffer : public TextureBufferBase { // Texture overrides Result create(const TextureDesc& desc, bool hasStorageAlready) override; Result createHWBuffer(const TextureDesc& desc, bool hasStorageAlready, bool surfaceComposite); - [[nodiscard]] static Result bindTextureWithHWBuffer(IContext& context, - GLuint target, - const AHardwareBuffer* hwb) noexcept; void bind() override; void bindImage(size_t unit) override; Result lockHWBuffer(std::byte* IGL_NULLABLE* IGL_NONNULL dst, RangeDesc& outRange) const;