Skip to content

Commit

Permalink
move android egl methods to egl context
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Alex Chong authored and facebook-github-bot committed Mar 20, 2024
1 parent 1623c92 commit 79c3693
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
40 changes: 39 additions & 1 deletion src/igl/opengl/egl/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

#include <igl/opengl/egl/Context.h>

#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <igl/opengl/ComputeCommandAdapter.h>
#include <igl/opengl/HWDevice.h>
#include <igl/opengl/RenderCommandAdapter.h>
#include <igl/opengl/egl/Context.h>

#include <cassert>
#include <igl/Macros.h>
Expand Down Expand Up @@ -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<GLeglImageOES>(eglImage));
IGL_DEBUG_LOG("glEGLImageTargetTexture2DOES(%u, %#x)\n",
GL_TEXTURE_2D,
static_cast<GLeglImageOES>(eglImage));
this->checkForErrors(__FUNCTION__, __LINE__);
}

} // namespace egl
} // namespace opengl
} // namespace igl
Expand Down
5 changes: 5 additions & 0 deletions src/igl/opengl/egl/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 10 additions & 30 deletions src/igl/opengl/egl/android/NativeHWBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
Expand Down Expand Up @@ -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<GLeglImageOES>(eglImage));
APILOG("glEGLImageTargetTexture2DOES(%u, %#x)\n",
GL_TEXTURE_2D,
static_cast<GLeglImageOES>(eglImage));

context.checkForErrors(__FUNCTION__, __LINE__);

return Result{};
}

} // namespace igl::opengl::egl::android

#endif
3 changes: 0 additions & 3 deletions src/igl/opengl/egl/android/NativeHWBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 79c3693

Please sign in to comment.