From f3413815a905fde1647440eb6e489a59537041d5 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 3 Aug 2021 03:58:30 +1000 Subject: [PATCH] [client] egl: re-implement DMABUF (untested) --- client/renderers/EGL/egl.c | 3 + client/renderers/EGL/texture.c | 1 + client/renderers/EGL/texture_buffer.c | 66 +++--- client/renderers/EGL/texture_buffer.h | 2 +- client/renderers/EGL/texture_dmabuf.c | 229 ++++++++++++++++++--- client/renderers/EGL/texture_framebuffer.c | 4 +- client/src/main.c | 14 +- 7 files changed, 249 insertions(+), 70 deletions(-) diff --git a/client/renderers/EGL/egl.c b/client/renderers/EGL/egl.c index 1150c038..15031217 100644 --- a/client/renderers/EGL/egl.c +++ b/client/renderers/EGL/egl.c @@ -748,6 +748,9 @@ static bool egl_render_startup(void * opaque, bool useDMA) else DEBUG_INFO("glEGLImageTargetTexture2DOES unavilable, DMA support disabled"); + if (!this->dmaSupport) + useDMA = false; + if (debugContext) { if ((esMaj > 3 || (esMaj == 3 && esMin >= 2)) && g_egl_dynProcs.glDebugMessageCallback) diff --git a/client/renderers/EGL/texture.c b/client/renderers/EGL/texture.c index 862c143d..8b9852ef 100644 --- a/client/renderers/EGL/texture.c +++ b/client/renderers/EGL/texture.c @@ -60,6 +60,7 @@ bool egl_texture_init(EGL_Texture ** texture, EGLDisplay * display, return false; } + *texture = NULL; if (!ops->init(texture, display)) return false; diff --git a/client/renderers/EGL/texture_buffer.c b/client/renderers/EGL/texture_buffer.c index 768b330a..06512fe9 100644 --- a/client/renderers/EGL/texture_buffer.c +++ b/client/renderers/EGL/texture_buffer.c @@ -50,33 +50,41 @@ static void eglTexBuffer_cleanup(TextureBuffer * this) // common functions -bool eglTexBuffer_init(EGL_Texture ** texture_, EGLDisplay * display) +bool eglTexBuffer_init(EGL_Texture ** texture, EGLDisplay * display) { - TextureBuffer * this = (TextureBuffer *)calloc(1, sizeof(*this)); - if (!this) + TextureBuffer * this; + if (!*texture) { - DEBUG_ERROR("Failed to malloc TexB"); - return false; + this = (TextureBuffer *)calloc(1, sizeof(*this)); + if (!this) + { + DEBUG_ERROR("Failed to malloc TexB"); + return false; + } + *texture = &this->base; + this->free = true; } - *texture_ = &this->base; + else + this = UPCAST(TextureBuffer, *texture); - this->display = display; this->texCount = 1; return true; } -void eglTexBuffer_free(EGL_Texture * texture_) +void eglTexBuffer_free(EGL_Texture * texture) { - TextureBuffer * this = UPCAST(TextureBuffer, texture_); + TextureBuffer * this = UPCAST(TextureBuffer, texture); eglTexBuffer_cleanup(this); LG_LOCK_FREE(this->copyLock); - free(this); + + if (this->free) + free(this); } -bool eglTexBuffer_setup(EGL_Texture * texture_, const EGL_TexSetup * setup) +bool eglTexBuffer_setup(EGL_Texture * texture, const EGL_TexSetup * setup) { - TextureBuffer * this = UPCAST(TextureBuffer, texture_); + TextureBuffer * this = UPCAST(TextureBuffer, texture); eglTexBuffer_cleanup(this); @@ -110,9 +118,9 @@ bool eglTexBuffer_setup(EGL_Texture * texture_, const EGL_TexSetup * setup) return true; } -static bool eglTexBuffer_update(EGL_Texture * texture_, const EGL_TexUpdate * update) +static bool eglTexBuffer_update(EGL_Texture * texture, const EGL_TexUpdate * update) { - TextureBuffer * this = UPCAST(TextureBuffer, texture_); + TextureBuffer * this = UPCAST(TextureBuffer, texture); assert(update->type == EGL_TEXTYPE_BUFFER); glBindTexture(GL_TEXTURE_2D, this->tex[0]); @@ -129,14 +137,14 @@ static bool eglTexBuffer_update(EGL_Texture * texture_, const EGL_TexUpdate * up return true; } -EGL_TexStatus eglTexBuffer_process(EGL_Texture * texture_) +EGL_TexStatus eglTexBuffer_process(EGL_Texture * texture) { return EGL_TEX_STATUS_OK; } -EGL_TexStatus eglTexBuffer_bind(EGL_Texture * texture_) +EGL_TexStatus eglTexBuffer_bind(EGL_Texture * texture) { - TextureBuffer * this = UPCAST(TextureBuffer, texture_); + TextureBuffer * this = UPCAST(TextureBuffer, texture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, this->tex[0]); @@ -147,31 +155,31 @@ EGL_TexStatus eglTexBuffer_bind(EGL_Texture * texture_) // streaming functions -bool eglTexBuffer_stream_init(EGL_Texture ** texture_, EGLDisplay * display) +bool eglTexBuffer_stream_init(EGL_Texture ** texture, EGLDisplay * display) { - if (!eglTexBuffer_init(texture_, display)) + if (!eglTexBuffer_init(texture, display)) return false; - TextureBuffer * this = UPCAST(TextureBuffer, *texture_); + TextureBuffer * this = UPCAST(TextureBuffer, *texture); this->texCount = 2; LG_LOCK_INIT(this->copyLock); return true; } -bool eglTexBuffer_stream_setup(EGL_Texture * texture_, const EGL_TexSetup * setup) +bool eglTexBuffer_stream_setup(EGL_Texture * texture, const EGL_TexSetup * setup) { - if (!eglTexBuffer_setup(texture_, setup)) + if (!eglTexBuffer_setup(texture, setup)) return false; - TextureBuffer * this = UPCAST(TextureBuffer, texture_); + TextureBuffer * this = UPCAST(TextureBuffer, texture); return eglTexUtilGenBuffers(&this->format, this->buf, this->texCount); } -static bool eglTexBuffer_stream_update(EGL_Texture * texture_, +static bool eglTexBuffer_stream_update(EGL_Texture * texture, const EGL_TexUpdate * update) { - TextureBuffer * this = UPCAST(TextureBuffer, texture_); + TextureBuffer * this = UPCAST(TextureBuffer, texture); assert(update->type == EGL_TEXTYPE_BUFFER); LG_LOCK(this->copyLock); @@ -183,9 +191,9 @@ static bool eglTexBuffer_stream_update(EGL_Texture * texture_, return true; } -EGL_TexStatus eglTexBuffer_stream_process(EGL_Texture * texture_) +EGL_TexStatus eglTexBuffer_stream_process(EGL_Texture * texture) { - TextureBuffer * this = UPCAST(TextureBuffer, texture_); + TextureBuffer * this = UPCAST(TextureBuffer, texture); LG_LOCK(this->copyLock); @@ -225,9 +233,9 @@ EGL_TexStatus eglTexBuffer_stream_process(EGL_Texture * texture_) return EGL_TEX_STATUS_OK; } -EGL_TexStatus eglTexBuffer_stream_bind(EGL_Texture * texture_) +EGL_TexStatus eglTexBuffer_stream_bind(EGL_Texture * texture) { - TextureBuffer * this = UPCAST(TextureBuffer, texture_); + TextureBuffer * this = UPCAST(TextureBuffer, texture); if (this->rIndex == -1) return EGL_TEX_STATUS_NOTREADY; diff --git a/client/renderers/EGL/texture_buffer.h b/client/renderers/EGL/texture_buffer.h index 7651f7ca..13b1ab24 100644 --- a/client/renderers/EGL/texture_buffer.h +++ b/client/renderers/EGL/texture_buffer.h @@ -27,8 +27,8 @@ typedef struct TextureBuffer { EGL_Texture base; + bool free; - EGLDisplay display; EGL_TexFormat format; int texCount; GLuint tex[2]; diff --git a/client/renderers/EGL/texture_dmabuf.c b/client/renderers/EGL/texture_dmabuf.c index e65d2222..18042d59 100644 --- a/client/renderers/EGL/texture_dmabuf.c +++ b/client/renderers/EGL/texture_dmabuf.c @@ -19,62 +19,227 @@ */ #include "texture.h" +#include "texture_buffer.h" + +#include "egl_dynprocs.h" +#include "egldebug.h" typedef struct TexDMABUF { - EGL_Texture base; + TextureBuffer base; + + EGLDisplay display; + + GLuint fbo; + GLuint tex; + + size_t imageCount; + size_t imageUsed; + struct + { + int fd; + EGLImage image; + } + * images; } TexDMABUF; EGL_TextureOps EGL_TextureDMABUF; -static bool eglTexDMABUF_init(EGL_Texture ** texture_, EGLDisplay * display) +// internal functions + +static void eglTexDMABUF_cleanup(TexDMABUF * this) { - TexDMABUF * texture = (TexDMABUF *)calloc(sizeof(*texture), 1); - *texture_ = &texture->base; + if (this->fbo) + { + glDeleteFramebuffers(1, &this->fbo); + this->fbo = 0; + } + + if (this->tex) + { + glDeleteTextures(1, &this->tex); + this->tex = 0; + } + + for(size_t i = 0; i < this->imageUsed; ++i) + eglDestroyImage(this->display, this->images[i].image); + + this->imageUsed = 0; +} + +// dmabuf functions + +static bool eglTexDMABUF_init(EGL_Texture ** texture, EGLDisplay * display) +{ + TexDMABUF * this = (TexDMABUF *)calloc(sizeof(*this), 1); + *texture = &this->base.base; + + EGL_Texture * parent = &this->base.base; + if (!eglTexBuffer_init(&parent, display)) + { + free(this); + *texture = NULL; + return false; + } + + this->display = display; + return true; +} + +static void eglTexDMABUF_free(EGL_Texture * texture) +{ + TextureBuffer * parent = UPCAST(TextureBuffer, texture); + TexDMABUF * this = UPCAST(TexDMABUF , parent); + + eglTexDMABUF_cleanup(this); + free(this->images); + + eglTexBuffer_free(&parent->base); + free(this); +} + +static bool eglTexDMABUF_setup(EGL_Texture * texture, const EGL_TexSetup * setup) +{ + TextureBuffer * parent = UPCAST(TextureBuffer, texture); + TexDMABUF * this = UPCAST(TexDMABUF , parent); + + eglTexDMABUF_cleanup(this); + + if (!eglTexBuffer_setup(&parent->base, setup)) + return false; + + glGenFramebuffers(1, &this->fbo); + glGenTextures (1, &this->tex); + + glBindTexture(GL_TEXTURE_2D, parent->tex[0]); + glTexImage2D(GL_TEXTURE_2D, + 0, + parent->format.intFormat, + parent->format.width, + parent->format.height, + 0, + parent->format.format, + parent->format.dataType, + NULL); return true; } -static void eglTexDMABUF_free(EGL_Texture * texture_) -{ - TexDMABUF * texture = UPCAST(TexDMABUF, texture_); - - free(texture); -} - -static bool eglTexDMABUF_setup(EGL_Texture * texture_, - const EGL_TexSetup * setup) -{ - TexDMABUF * texture = UPCAST(TexDMABUF, texture_); - (void)texture; - - return false; -} - -static bool eglTexDMABUF_update(EGL_Texture * texture_, +static bool eglTexDMABUF_update(EGL_Texture * texture, const EGL_TexUpdate * update) { - TexDMABUF * texture = UPCAST(TexDMABUF, texture_); - (void)texture; + TextureBuffer * parent = UPCAST(TextureBuffer, texture); + TexDMABUF * this = UPCAST(TexDMABUF , parent); - return false; + EGLImage image = EGL_NO_IMAGE; + + for(int i = 0; i < this->imageUsed; ++i) + if (this->images[i].fd == update->dmaFD) + { + image = this->images[i].image; + break; + } + + if (image == EGL_NO_IMAGE) + { + EGLAttrib const attribs[] = + { + EGL_WIDTH , parent->format.width, + EGL_HEIGHT , parent->format.height, + EGL_LINUX_DRM_FOURCC_EXT , parent->format.fourcc, + EGL_DMA_BUF_PLANE0_FD_EXT , update->dmaFD, + EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0, + EGL_DMA_BUF_PLANE0_PITCH_EXT , parent->format.stride, + EGL_NONE , EGL_NONE + }; + + image = eglCreateImage( + this->display, + EGL_NO_CONTEXT, + EGL_LINUX_DMA_BUF_EXT, + (EGLClientBuffer)NULL, + attribs); + + if (image == EGL_NO_IMAGE) + { + DEBUG_EGL_ERROR("Failed to create EGLImage for DMA transfer"); + return false; + } + + if (this->imageUsed == this->imageCount) + { + size_t newCount = this->imageCount * 2 + 2; + void * new = realloc(this->images, newCount * sizeof(*this->images)); + if (!new) + { + DEBUG_ERROR("Failed to allocate memory"); + eglDestroyImage(this->display, image); + return false; + } + + this->imageCount = newCount; + this->images = new; + } + + const size_t index = this->imageUsed++; + this->images[index].fd = update->dmaFD; + this->images[index].image = image; + } + + glBindTexture(GL_TEXTURE_2D, this->tex); + g_egl_dynProcs.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image); + + /* should any of the following even be here? move to process? - gnif */ + + glBindFramebuffer(GL_FRAMEBUFFER, this->fbo); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + this->tex, 0); + + glBindTexture(GL_TEXTURE_2D, parent->tex[0]); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, parent->format.width, + parent->format.height); + + GLsync fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); + glFlush(); + + switch(glClientWaitSync(fence, 0, 20000000)) //20ms + { + case GL_ALREADY_SIGNALED: + case GL_CONDITION_SATISFIED: + break; + + case GL_TIMEOUT_EXPIRED: + break; + + case GL_WAIT_FAILED: + case GL_INVALID_VALUE: + DEBUG_GL_ERROR("glClientWaitSync failed"); + break; + } + + glDeleteSync(fence); + return true; } -static EGL_TexStatus eglTexDMABUF_process(EGL_Texture * texture_) +static EGL_TexStatus eglTexDMABUF_process(EGL_Texture * texture) { - TexDMABUF * texture = UPCAST(TexDMABUF, texture_); - (void)texture; +// TextureBuffer * parent = UPCAST(TextureBuffer, texture); +// TexDMABUF * this = UPCAST(TexDMABUF , parent); - return EGL_TEX_STATUS_ERROR; + return EGL_TEX_STATUS_OK; } -static EGL_TexStatus eglTexDMABUF_bind(EGL_Texture * texture_) +static EGL_TexStatus eglTexDMABUF_bind(EGL_Texture * texture) { - TexDMABUF * texture = UPCAST(TexDMABUF, texture_); - (void)texture; + TextureBuffer * parent = UPCAST(TextureBuffer, texture); +// TexDMABUF * this = UPCAST(TexDMABUF , parent); - return EGL_TEX_STATUS_ERROR; + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, parent->tex[0]); + glBindSampler(0, parent->sampler); + + return EGL_TEX_STATUS_OK; } EGL_TextureOps EGL_TextureDMABUF = diff --git a/client/renderers/EGL/texture_framebuffer.c b/client/renderers/EGL/texture_framebuffer.c index cf26db93..c10077af 100644 --- a/client/renderers/EGL/texture_framebuffer.c +++ b/client/renderers/EGL/texture_framebuffer.c @@ -25,9 +25,9 @@ #include "texture_buffer.h" #include "common/debug.h" -static bool eglTexFB_update(EGL_Texture * texture_, const EGL_TexUpdate * update) +static bool eglTexFB_update(EGL_Texture * texture, const EGL_TexUpdate * update) { - TextureBuffer * this = UPCAST(TextureBuffer, texture_); + TextureBuffer * this = UPCAST(TextureBuffer, texture); assert(update->type == EGL_TEXTYPE_FRAMEBUFFER); LG_LOCK(this->copyLock); diff --git a/client/src/main.c b/client/src/main.c index 130592df..63447cb4 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -149,6 +149,10 @@ static int renderThread(void * unused) return 1; } + if (g_state.lgr->supports && + !g_state.lgr->supports(g_state.lgrData, LG_SUPPORTS_DMABUF)) + g_state.useDMA = false; + /* start up the fps timer */ LGTimer * fpsTimer; if (!lgCreateTimer(500, fpsTimerFn, NULL, &fpsTimer)) @@ -925,18 +929,16 @@ static int lg_run(void) } } - g_state.useDMA = - g_params.allowDMA && - ivshmemHasDMA(&g_state.shm) && - g_state.lgr->supports && - g_state.lgr->supports(g_state.lgrData, LG_SUPPORTS_DMABUF); - if (!g_state.lgr) { DEBUG_INFO("Unable to find a suitable renderer"); return -1; } + g_state.useDMA = + g_params.allowDMA && + ivshmemHasDMA(&g_state.shm); + // initialize the window dimensions at init for renderers g_state.windowW = g_params.w; g_state.windowH = g_params.h;