From 6eff6c365bec91d7825a177c5fd9ed383b113612 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Tue, 21 Sep 2021 09:36:44 -0400 Subject: [PATCH 1/8] render/egl: make wlr_egl_context public --- include/render/egl.h | 19 ------------------- include/wlr/render/egl.h | 19 +++++++++++++++++++ render/egl.c | 6 ++++-- render/gles2/renderer.c | 4 ++-- render/gles2/texture.c | 22 +++++++++++----------- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/include/render/egl.h b/include/render/egl.h index 55e9ce80f1..af38af7e10 100644 --- a/include/render/egl.h +++ b/include/render/egl.h @@ -3,13 +3,6 @@ #include -struct wlr_egl_context { - EGLDisplay display; - EGLContext context; - EGLSurface draw_surface; - EGLSurface read_surface; -}; - /** * Initializes an EGL context for the given DRM FD. * @@ -48,16 +41,4 @@ bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImageKHR image); int wlr_egl_dup_drm_fd(struct wlr_egl *egl); -/** - * Save the current EGL context to the structure provided in the argument. - * - * This includes display, context, draw surface and read surface. - */ -void wlr_egl_save_context(struct wlr_egl_context *context); - -/** - * Restore EGL context that was previously saved using wlr_egl_save_current(). - */ -bool wlr_egl_restore_context(struct wlr_egl_context *context); - #endif diff --git a/include/wlr/render/egl.h b/include/wlr/render/egl.h index 903b70a3fc..88db130f7f 100644 --- a/include/wlr/render/egl.h +++ b/include/wlr/render/egl.h @@ -29,6 +29,13 @@ #include #include +struct wlr_egl_context { + EGLDisplay display; + EGLContext context; + EGLSurface draw_surface; + EGLSurface read_surface; +}; + struct wlr_egl { EGLDisplay display; EGLContext context; @@ -80,4 +87,16 @@ bool wlr_egl_unset_current(struct wlr_egl *egl); bool wlr_egl_is_current(struct wlr_egl *egl); +/** + * Save the current EGL context to the structure provided in the argument. + * + * This includes display, context, draw surface and read surface. + */ +void wlr_egl_context_save(struct wlr_egl_context *context); + +/** + * Restore EGL context that was previously saved using wlr_egl_context_save(). + */ +bool wlr_egl_context_restore(struct wlr_egl_context *context); + #endif diff --git a/render/egl.c b/render/egl.c index 712a61c81b..e7734f86cf 100644 --- a/render/egl.c +++ b/render/egl.c @@ -560,14 +560,16 @@ bool wlr_egl_is_current(struct wlr_egl *egl) { return eglGetCurrentContext() == egl->context; } -void wlr_egl_save_context(struct wlr_egl_context *context) { + +void wlr_egl_context_save(struct wlr_egl_context *context) { context->display = eglGetCurrentDisplay(); context->context = eglGetCurrentContext(); context->draw_surface = eglGetCurrentSurface(EGL_DRAW); context->read_surface = eglGetCurrentSurface(EGL_READ); } -bool wlr_egl_restore_context(struct wlr_egl_context *context) { + +bool wlr_egl_context_restore(struct wlr_egl_context *context) { // If the saved context is a null-context, we must use the current // display instead of the saved display because eglMakeCurrent() can't // handle EGL_NO_DISPLAY. diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 01efaf1de9..bde21f4742 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -51,7 +51,7 @@ static void destroy_buffer(struct wlr_gles2_buffer *buffer) { wlr_addon_finish(&buffer->addon); struct wlr_egl_context prev_ctx; - wlr_egl_save_context(&prev_ctx); + wlr_egl_context_save(&prev_ctx); wlr_egl_make_current(buffer->renderer->egl); push_gles2_debug(buffer->renderer); @@ -63,7 +63,7 @@ static void destroy_buffer(struct wlr_gles2_buffer *buffer) { wlr_egl_destroy_image(buffer->renderer->egl, buffer->image); - wlr_egl_restore_context(&prev_ctx); + wlr_egl_context_restore(&prev_ctx); free(buffer); } diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 293b7a19b8..bd3cf53f0a 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -73,7 +73,7 @@ static bool gles2_texture_write_pixels(struct wlr_texture *wlr_texture, } struct wlr_egl_context prev_ctx; - wlr_egl_save_context(&prev_ctx); + wlr_egl_context_save(&prev_ctx); wlr_egl_make_current(texture->renderer->egl); push_gles2_debug(texture->renderer); @@ -95,7 +95,7 @@ static bool gles2_texture_write_pixels(struct wlr_texture *wlr_texture, pop_gles2_debug(texture->renderer); - wlr_egl_restore_context(&prev_ctx); + wlr_egl_context_restore(&prev_ctx); return true; } @@ -110,7 +110,7 @@ static bool gles2_texture_invalidate(struct wlr_gles2_texture *texture) { } struct wlr_egl_context prev_ctx; - wlr_egl_save_context(&prev_ctx); + wlr_egl_context_save(&prev_ctx); wlr_egl_make_current(texture->renderer->egl); push_gles2_debug(texture->renderer); @@ -122,7 +122,7 @@ static bool gles2_texture_invalidate(struct wlr_gles2_texture *texture) { pop_gles2_debug(texture->renderer); - wlr_egl_restore_context(&prev_ctx); + wlr_egl_context_restore(&prev_ctx); return true; } @@ -134,7 +134,7 @@ void gles2_texture_destroy(struct wlr_gles2_texture *texture) { } struct wlr_egl_context prev_ctx; - wlr_egl_save_context(&prev_ctx); + wlr_egl_context_save(&prev_ctx); wlr_egl_make_current(texture->renderer->egl); push_gles2_debug(texture->renderer); @@ -144,7 +144,7 @@ void gles2_texture_destroy(struct wlr_gles2_texture *texture) { pop_gles2_debug(texture->renderer); - wlr_egl_restore_context(&prev_ctx); + wlr_egl_context_restore(&prev_ctx); free(texture); } @@ -211,7 +211,7 @@ static struct wlr_texture *gles2_texture_from_pixels( texture->drm_format = fmt->drm_format; struct wlr_egl_context prev_ctx; - wlr_egl_save_context(&prev_ctx); + wlr_egl_context_save(&prev_ctx); wlr_egl_make_current(renderer->egl); push_gles2_debug(renderer); @@ -230,7 +230,7 @@ static struct wlr_texture *gles2_texture_from_pixels( pop_gles2_debug(renderer); - wlr_egl_restore_context(&prev_ctx); + wlr_egl_context_restore(&prev_ctx); return &texture->wlr_texture; } @@ -263,7 +263,7 @@ static struct wlr_texture *gles2_texture_from_dmabuf( } struct wlr_egl_context prev_ctx; - wlr_egl_save_context(&prev_ctx); + wlr_egl_context_save(&prev_ctx); wlr_egl_make_current(renderer->egl); bool external_only; @@ -271,7 +271,7 @@ static struct wlr_texture *gles2_texture_from_dmabuf( wlr_egl_create_image_from_dmabuf(renderer->egl, attribs, &external_only); if (texture->image == EGL_NO_IMAGE_KHR) { wlr_log(WLR_ERROR, "Failed to create EGL image from DMA-BUF"); - wlr_egl_restore_context(&prev_ctx); + wlr_egl_context_restore(&prev_ctx); wl_list_remove(&texture->link); free(texture); return NULL; @@ -290,7 +290,7 @@ static struct wlr_texture *gles2_texture_from_dmabuf( pop_gles2_debug(renderer); - wlr_egl_restore_context(&prev_ctx); + wlr_egl_context_restore(&prev_ctx); return &texture->wlr_texture; } From f70f37a2c2e8fb93cbba153a6f95ffecc16493ca Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Tue, 21 Sep 2021 10:00:17 -0400 Subject: [PATCH 2/8] render/egl: make wlr_egl context private, replace them with public wlr_egl_context functions --- include/render/egl.h | 15 +++++++++++++++ include/wlr/render/egl.h | 9 ++++++--- render/egl.c | 22 +++++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/include/render/egl.h b/include/render/egl.h index af38af7e10..20ebc09e46 100644 --- a/include/render/egl.h +++ b/include/render/egl.h @@ -41,4 +41,19 @@ bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImageKHR image); int wlr_egl_dup_drm_fd(struct wlr_egl *egl); +/** + * Makes the provided EGL context current + * + * Callers are expected to clear the current context when they are done by + * calling wlr_egl_unset_current. + */ +bool wlr_egl_make_current(struct wlr_egl *egl); + +/** + * Clears the current EGLContext + */ +bool wlr_egl_unset_current(struct wlr_egl *egl); + +bool wlr_egl_is_current(struct wlr_egl *egl); + #endif diff --git a/include/wlr/render/egl.h b/include/wlr/render/egl.h index 88db130f7f..297f49f3bc 100644 --- a/include/wlr/render/egl.h +++ b/include/wlr/render/egl.h @@ -81,11 +81,14 @@ struct wlr_egl { * Callers are expected to clear the current context when they are done by * calling wlr_egl_unset_current. */ -bool wlr_egl_make_current(struct wlr_egl *egl); +bool wlr_egl_context_set_current(struct wlr_egl_context *ctx); -bool wlr_egl_unset_current(struct wlr_egl *egl); +/** + * Clear the EGL context + */ +bool wlr_egl_context_unset_current(struct wlr_egl_context *ctx); -bool wlr_egl_is_current(struct wlr_egl *egl); +bool wlr_egl_context_is_current(struct wlr_egl_context *ctx); /** * Save the current EGL context to the structure provided in the argument. diff --git a/render/egl.c b/render/egl.c index e7734f86cf..892a0cafce 100644 --- a/render/egl.c +++ b/render/egl.c @@ -560,6 +560,27 @@ bool wlr_egl_is_current(struct wlr_egl *egl) { return eglGetCurrentContext() == egl->context; } +bool wlr_egl_context_set_current(struct wlr_egl_context *ctx) { + if (!eglMakeCurrent(ctx->display, EGL_NO_SURFACE, EGL_NO_SURFACE, + ctx->context)) { + wlr_log(WLR_ERROR, "eglMakeCurrent failed"); + return false; + } + return true; +} + +bool wlr_egl_context_unset_current(struct wlr_egl_context *ctx) { + if (!eglMakeCurrent(ctx->display, EGL_NO_SURFACE, EGL_NO_SURFACE, + EGL_NO_CONTEXT)) { + wlr_log(WLR_ERROR, "eglMakeCurrent failed"); + return false; + } + return true; +} + +bool wlr_egl_context_is_current(struct wlr_egl_context *ctx) { + return eglGetCurrentContext() == ctx->context; +} void wlr_egl_context_save(struct wlr_egl_context *context) { context->display = eglGetCurrentDisplay(); @@ -568,7 +589,6 @@ void wlr_egl_context_save(struct wlr_egl_context *context) { context->read_surface = eglGetCurrentSurface(EGL_READ); } - bool wlr_egl_context_restore(struct wlr_egl_context *context) { // If the saved context is a null-context, we must use the current // display instead of the saved display because eglMakeCurrent() can't From c22655cffb36b84823052e9b09875dd141421686 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Tue, 21 Sep 2021 10:01:02 -0400 Subject: [PATCH 3/8] render/egl: make wlr_egl private --- include/render/egl.h | 39 +++++++++++++++++++++++++++++++++++++++ include/wlr/render/egl.h | 39 --------------------------------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/include/render/egl.h b/include/render/egl.h index 20ebc09e46..3c7743c934 100644 --- a/include/render/egl.h +++ b/include/render/egl.h @@ -3,6 +3,45 @@ #include +struct wlr_egl { + EGLDisplay display; + EGLContext context; + EGLDeviceEXT device; // may be EGL_NO_DEVICE_EXT + struct gbm_device *gbm_device; + + struct { + // Display extensions + bool KHR_image_base; + bool EXT_image_dma_buf_import; + bool EXT_image_dma_buf_import_modifiers; + + // Device extensions + bool EXT_device_drm; + bool EXT_device_drm_render_node; + + // Client extensions + bool EXT_device_query; + bool KHR_platform_gbm; + bool EXT_platform_device; + } exts; + + struct { + PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT; + PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR; + PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR; + PFNEGLQUERYWAYLANDBUFFERWL eglQueryWaylandBufferWL; + PFNEGLQUERYDMABUFFORMATSEXTPROC eglQueryDmaBufFormatsEXT; + PFNEGLQUERYDMABUFMODIFIERSEXTPROC eglQueryDmaBufModifiersEXT; + PFNEGLDEBUGMESSAGECONTROLKHRPROC eglDebugMessageControlKHR; + PFNEGLQUERYDISPLAYATTRIBEXTPROC eglQueryDisplayAttribEXT; + PFNEGLQUERYDEVICESTRINGEXTPROC eglQueryDeviceStringEXT; + PFNEGLQUERYDEVICESEXTPROC eglQueryDevicesEXT; + } procs; + + struct wlr_drm_format_set dmabuf_texture_formats; + struct wlr_drm_format_set dmabuf_render_formats; +}; + /** * Initializes an EGL context for the given DRM FD. * diff --git a/include/wlr/render/egl.h b/include/wlr/render/egl.h index 297f49f3bc..4c1e4933b2 100644 --- a/include/wlr/render/egl.h +++ b/include/wlr/render/egl.h @@ -36,45 +36,6 @@ struct wlr_egl_context { EGLSurface read_surface; }; -struct wlr_egl { - EGLDisplay display; - EGLContext context; - EGLDeviceEXT device; // may be EGL_NO_DEVICE_EXT - struct gbm_device *gbm_device; - - struct { - // Display extensions - bool KHR_image_base; - bool EXT_image_dma_buf_import; - bool EXT_image_dma_buf_import_modifiers; - - // Device extensions - bool EXT_device_drm; - bool EXT_device_drm_render_node; - - // Client extensions - bool EXT_device_query; - bool KHR_platform_gbm; - bool EXT_platform_device; - } exts; - - struct { - PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT; - PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR; - PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR; - PFNEGLQUERYWAYLANDBUFFERWL eglQueryWaylandBufferWL; - PFNEGLQUERYDMABUFFORMATSEXTPROC eglQueryDmaBufFormatsEXT; - PFNEGLQUERYDMABUFMODIFIERSEXTPROC eglQueryDmaBufModifiersEXT; - PFNEGLDEBUGMESSAGECONTROLKHRPROC eglDebugMessageControlKHR; - PFNEGLQUERYDISPLAYATTRIBEXTPROC eglQueryDisplayAttribEXT; - PFNEGLQUERYDEVICESTRINGEXTPROC eglQueryDeviceStringEXT; - PFNEGLQUERYDEVICESEXTPROC eglQueryDevicesEXT; - } procs; - - struct wlr_drm_format_set dmabuf_texture_formats; - struct wlr_drm_format_set dmabuf_render_formats; -}; - /** * Make the EGL context current. * From 7ff1664030e7efcd7d4e6c3bd826294555e861b4 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Tue, 21 Sep 2021 10:18:35 -0400 Subject: [PATCH 4/8] render/egl: clean includes --- include/render/egl.h | 1 + include/wlr/render/egl.h | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/include/render/egl.h b/include/render/egl.h index 3c7743c934..083b040c9f 100644 --- a/include/render/egl.h +++ b/include/render/egl.h @@ -1,6 +1,7 @@ #ifndef RENDER_EGL_H #define RENDER_EGL_H +#include #include struct wlr_egl { diff --git a/include/wlr/render/egl.h b/include/wlr/render/egl.h index 4c1e4933b2..03313c5ce9 100644 --- a/include/wlr/render/egl.h +++ b/include/wlr/render/egl.h @@ -23,10 +23,7 @@ #include #include -#include #include -#include -#include #include struct wlr_egl_context { From 95928cec720fd0fade36fda72901c7c011a2dfff Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Tue, 21 Sep 2021 13:17:02 -0400 Subject: [PATCH 5/8] render/egl: make wlr_egl hold a wlr_egl_context --- include/render/egl.h | 3 +- render/egl.c | 70 ++++++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/include/render/egl.h b/include/render/egl.h index 083b040c9f..f60ed2bfdc 100644 --- a/include/render/egl.h +++ b/include/render/egl.h @@ -5,8 +5,7 @@ #include struct wlr_egl { - EGLDisplay display; - EGLContext context; + struct wlr_egl_context ctx; EGLDeviceEXT device; // may be EGL_NO_DEVICE_EXT struct gbm_device *gbm_device; diff --git a/render/egl.c b/render/egl.c index 892a0cafce..2b00988919 100644 --- a/render/egl.c +++ b/render/egl.c @@ -223,20 +223,21 @@ static struct wlr_egl *egl_create(void) { static bool egl_init(struct wlr_egl *egl, EGLenum platform, void *remote_display) { - egl->display = egl->procs.eglGetPlatformDisplayEXT(platform, + egl->ctx.display = egl->procs.eglGetPlatformDisplayEXT(platform, remote_display, NULL); - if (egl->display == EGL_NO_DISPLAY) { + if (egl->ctx.display == EGL_NO_DISPLAY) { wlr_log(WLR_ERROR, "Failed to create EGL display"); return false; } EGLint major, minor; - if (eglInitialize(egl->display, &major, &minor) == EGL_FALSE) { + if (eglInitialize(egl->ctx.display, &major, &minor) == EGL_FALSE) { wlr_log(WLR_ERROR, "Failed to initialize EGL"); return false; } - const char *display_exts_str = eglQueryString(egl->display, EGL_EXTENSIONS); + const char *display_exts_str = + eglQueryString(egl->ctx.display, EGL_EXTENSIONS); if (display_exts_str == NULL) { wlr_log(WLR_ERROR, "Failed to query EGL display extensions"); return false; @@ -262,7 +263,7 @@ static bool egl_init(struct wlr_egl *egl, EGLenum platform, const char *device_exts_str = NULL, *driver_name = NULL; if (egl->exts.EXT_device_query) { EGLAttrib device_attrib; - if (!egl->procs.eglQueryDisplayAttribEXT(egl->display, + if (!egl->procs.eglQueryDisplayAttribEXT(egl->ctx.display, EGL_DEVICE_EXT, &device_attrib)) { wlr_log(WLR_ERROR, "eglQueryDisplayAttribEXT(EGL_DEVICE_EXT) failed"); return false; @@ -318,7 +319,8 @@ static bool egl_init(struct wlr_egl *egl, EGLenum platform, wlr_log(WLR_INFO, "Supported EGL device extensions: %s", device_exts_str); } wlr_log(WLR_INFO, "Using EGL %d.%d", (int)major, (int)minor); - wlr_log(WLR_INFO, "EGL vendor: %s", eglQueryString(egl->display, EGL_VENDOR)); + wlr_log(WLR_INFO, "EGL vendor: %s", + eglQueryString(egl->ctx.display, EGL_VENDOR)); if (driver_name != NULL) { wlr_log(WLR_INFO, "EGL driver name: %s", driver_name); } @@ -347,16 +349,16 @@ static bool egl_init(struct wlr_egl *egl, EGLenum platform, attribs[atti++] = EGL_NONE; assert(atti <= sizeof(attribs)/sizeof(attribs[0])); - egl->context = eglCreateContext(egl->display, EGL_NO_CONFIG_KHR, + egl->ctx.context = eglCreateContext(egl->ctx.display, EGL_NO_CONFIG_KHR, EGL_NO_CONTEXT, attribs); - if (egl->context == EGL_NO_CONTEXT) { + if (egl->ctx.context == EGL_NO_CONTEXT) { wlr_log(WLR_ERROR, "Failed to create EGL context"); return false; } if (request_high_priority) { EGLint priority = EGL_CONTEXT_PRIORITY_MEDIUM_IMG; - eglQueryContext(egl->display, egl->context, + eglQueryContext(egl->ctx.display, egl->ctx.context, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &priority); if (priority != EGL_CONTEXT_PRIORITY_HIGH_IMG) { wlr_log(WLR_INFO, "Failed to obtain a high priority context"); @@ -495,10 +497,10 @@ struct wlr_egl *wlr_egl_create_with_drm_fd(int drm_fd) { error: wlr_log(WLR_ERROR, "Failed to initialize EGL context"); - if (egl->display) { - eglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, + if (egl->ctx.display) { + eglMakeCurrent(egl->ctx.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); - eglTerminate(egl->display); + eglTerminate(egl->ctx.display); } free(egl); eglReleaseThread(); @@ -513,10 +515,11 @@ void wlr_egl_destroy(struct wlr_egl *egl) { wlr_drm_format_set_finish(&egl->dmabuf_render_formats); wlr_drm_format_set_finish(&egl->dmabuf_texture_formats); - eglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + eglMakeCurrent(egl->ctx.display, EGL_NO_SURFACE, EGL_NO_SURFACE, + EGL_NO_CONTEXT); - eglDestroyContext(egl->display, egl->context); - eglTerminate(egl->display); + eglDestroyContext(egl->ctx.display, egl->ctx.context); + eglTerminate(egl->ctx.display); eglReleaseThread(); if (egl->gbm_device) { @@ -535,29 +538,19 @@ bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImage image) { if (!image) { return true; } - return egl->procs.eglDestroyImageKHR(egl->display, image); + return egl->procs.eglDestroyImageKHR(egl->ctx.display, image); } bool wlr_egl_make_current(struct wlr_egl *egl) { - if (!eglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, - egl->context)) { - wlr_log(WLR_ERROR, "eglMakeCurrent failed"); - return false; - } - return true; + return wlr_egl_context_set_current(&egl->ctx); } bool wlr_egl_unset_current(struct wlr_egl *egl) { - if (!eglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, - EGL_NO_CONTEXT)) { - wlr_log(WLR_ERROR, "eglMakeCurrent failed"); - return false; - } - return true; + return wlr_egl_context_unset_current(&egl->ctx); } bool wlr_egl_is_current(struct wlr_egl *egl) { - return eglGetCurrentContext() == egl->context; + return wlr_egl_context_is_current(&egl->ctx); } bool wlr_egl_context_set_current(struct wlr_egl_context *ctx) { @@ -694,15 +687,15 @@ EGLImageKHR wlr_egl_create_image_from_dmabuf(struct wlr_egl *egl, attribs[atti++] = EGL_NONE; assert(atti < sizeof(attribs)/sizeof(attribs[0])); - EGLImageKHR image = egl->procs.eglCreateImageKHR(egl->display, EGL_NO_CONTEXT, - EGL_LINUX_DMA_BUF_EXT, NULL, attribs); + EGLImageKHR image = egl->procs.eglCreateImageKHR(egl->ctx.display, + EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attribs); if (image == EGL_NO_IMAGE_KHR) { wlr_log(WLR_ERROR, "eglCreateImageKHR failed"); return EGL_NO_IMAGE_KHR; } *external_only = !wlr_drm_format_set_has(&egl->dmabuf_render_formats, - attributes->format, attributes->modifier); + attributes->format, attributes->modifier); return image; } @@ -736,7 +729,7 @@ static int get_egl_dmabuf_formats(struct wlr_egl *egl, int **formats) { } EGLint num; - if (!egl->procs.eglQueryDmaBufFormatsEXT(egl->display, 0, NULL, &num)) { + if (!egl->procs.eglQueryDmaBufFormatsEXT(egl->ctx.display, 0, NULL, &num)) { wlr_log(WLR_ERROR, "Failed to query number of dmabuf formats"); return -1; } @@ -747,7 +740,8 @@ static int get_egl_dmabuf_formats(struct wlr_egl *egl, int **formats) { return -1; } - if (!egl->procs.eglQueryDmaBufFormatsEXT(egl->display, num, *formats, &num)) { + if (!egl->procs.eglQueryDmaBufFormatsEXT(egl->ctx.display, num, *formats, + &num)) { wlr_log(WLR_ERROR, "Failed to query dmabuf format"); free(*formats); return -1; @@ -769,7 +763,7 @@ static int get_egl_dmabuf_modifiers(struct wlr_egl *egl, int format, } EGLint num; - if (!egl->procs.eglQueryDmaBufModifiersEXT(egl->display, format, 0, + if (!egl->procs.eglQueryDmaBufModifiersEXT(egl->ctx.display, format, 0, NULL, NULL, &num)) { wlr_log(WLR_ERROR, "Failed to query dmabuf number of modifiers"); return -1; @@ -791,7 +785,7 @@ static int get_egl_dmabuf_modifiers(struct wlr_egl *egl, int format, return -1; } - if (!egl->procs.eglQueryDmaBufModifiersEXT(egl->display, format, num, + if (!egl->procs.eglQueryDmaBufModifiersEXT(egl->ctx.display, format, num, *modifiers, *external_only, &num)) { wlr_log(WLR_ERROR, "Failed to query dmabuf modifiers"); free(*modifiers); @@ -892,8 +886,8 @@ int wlr_egl_dup_drm_fd(struct wlr_egl *egl) { #endif if (render_name == NULL) { - const char *primary_name = egl->procs.eglQueryDeviceStringEXT(egl->device, - EGL_DRM_DEVICE_FILE_EXT); + const char *primary_name = egl->procs.eglQueryDeviceStringEXT( + egl->device, EGL_DRM_DEVICE_FILE_EXT); if (primary_name == NULL) { wlr_log(WLR_ERROR, "eglQueryDeviceStringEXT(EGL_DRM_DEVICE_FILE_EXT) failed"); From 72073e57927a3e45ba63ae835fc99155ba0ac429 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Tue, 21 Sep 2021 15:03:53 -0400 Subject: [PATCH 6/8] render/egl: remove wlr_egl context functions --- include/render/egl.h | 15 --------------- render/egl.c | 12 ------------ render/gles2/renderer.c | 20 ++++++++++---------- render/gles2/texture.c | 10 +++++----- 4 files changed, 15 insertions(+), 42 deletions(-) diff --git a/include/render/egl.h b/include/render/egl.h index f60ed2bfdc..fa48456133 100644 --- a/include/render/egl.h +++ b/include/render/egl.h @@ -80,19 +80,4 @@ bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImageKHR image); int wlr_egl_dup_drm_fd(struct wlr_egl *egl); -/** - * Makes the provided EGL context current - * - * Callers are expected to clear the current context when they are done by - * calling wlr_egl_unset_current. - */ -bool wlr_egl_make_current(struct wlr_egl *egl); - -/** - * Clears the current EGLContext - */ -bool wlr_egl_unset_current(struct wlr_egl *egl); - -bool wlr_egl_is_current(struct wlr_egl *egl); - #endif diff --git a/render/egl.c b/render/egl.c index 2b00988919..a04b045871 100644 --- a/render/egl.c +++ b/render/egl.c @@ -541,18 +541,6 @@ bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImage image) { return egl->procs.eglDestroyImageKHR(egl->ctx.display, image); } -bool wlr_egl_make_current(struct wlr_egl *egl) { - return wlr_egl_context_set_current(&egl->ctx); -} - -bool wlr_egl_unset_current(struct wlr_egl *egl) { - return wlr_egl_context_unset_current(&egl->ctx); -} - -bool wlr_egl_is_current(struct wlr_egl *egl) { - return wlr_egl_context_is_current(&egl->ctx); -} - bool wlr_egl_context_set_current(struct wlr_egl_context *ctx) { if (!eglMakeCurrent(ctx->display, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx->context)) { diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index bde21f4742..36403f7e13 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -41,7 +41,7 @@ struct wlr_gles2_renderer *gles2_get_renderer( static struct wlr_gles2_renderer *gles2_get_renderer_in_context( struct wlr_renderer *wlr_renderer) { struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer); - assert(wlr_egl_is_current(renderer->egl)); + assert(wlr_egl_context_is_current(&renderer->egl->ctx)); assert(renderer->current_buffer != NULL); return renderer; } @@ -52,7 +52,7 @@ static void destroy_buffer(struct wlr_gles2_buffer *buffer) { struct wlr_egl_context prev_ctx; wlr_egl_context_save(&prev_ctx); - wlr_egl_make_current(buffer->renderer->egl); + wlr_egl_context_set_current(&buffer->renderer->egl->ctx); push_gles2_debug(buffer->renderer); @@ -156,7 +156,7 @@ static bool gles2_bind_buffer(struct wlr_renderer *wlr_renderer, struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer); if (renderer->current_buffer != NULL) { - assert(wlr_egl_is_current(renderer->egl)); + assert(wlr_egl_context_is_current(&renderer->egl->ctx)); push_gles2_debug(renderer); glFlush(); @@ -168,11 +168,11 @@ static bool gles2_bind_buffer(struct wlr_renderer *wlr_renderer, } if (wlr_buffer == NULL) { - wlr_egl_unset_current(renderer->egl); + wlr_egl_context_unset_current(&renderer->egl->ctx); return true; } - wlr_egl_make_current(renderer->egl); + wlr_egl_context_set_current(&renderer->egl->ctx); struct wlr_gles2_buffer *buffer = get_buffer(renderer, wlr_buffer); if (buffer == NULL) { @@ -503,7 +503,7 @@ struct wlr_egl *wlr_gles2_renderer_get_egl(struct wlr_renderer *wlr_renderer) { static void gles2_destroy(struct wlr_renderer *wlr_renderer) { struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer); - wlr_egl_make_current(renderer->egl); + wlr_egl_context_set_current(&renderer->egl->ctx); struct wlr_gles2_buffer *buffer, *buffer_tmp; wl_list_for_each_safe(buffer, buffer_tmp, &renderer->buffers, link) { @@ -527,7 +527,7 @@ static void gles2_destroy(struct wlr_renderer *wlr_renderer) { renderer->procs.glDebugMessageCallbackKHR(NULL, NULL); } - wlr_egl_unset_current(renderer->egl); + wlr_egl_context_unset_current(&renderer->egl->ctx); wlr_egl_destroy(renderer->egl); if (renderer->drm_fd >= 0) { @@ -705,7 +705,7 @@ struct wlr_renderer *wlr_gles2_renderer_create_with_drm_fd(int drm_fd) { } struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { - if (!wlr_egl_make_current(egl)) { + if (!wlr_egl_context_set_current(&egl->ctx)) { return NULL; } @@ -844,7 +844,7 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { pop_gles2_debug(renderer); - wlr_egl_unset_current(renderer->egl); + wlr_egl_context_unset_current(&renderer->egl->ctx); return &renderer->wlr_renderer; @@ -861,7 +861,7 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { renderer->procs.glDebugMessageCallbackKHR(NULL, NULL); } - wlr_egl_unset_current(renderer->egl); + wlr_egl_context_unset_current(&renderer->egl->ctx); free(renderer); return NULL; diff --git a/render/gles2/texture.c b/render/gles2/texture.c index bd3cf53f0a..43833cc7ce 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -74,7 +74,7 @@ static bool gles2_texture_write_pixels(struct wlr_texture *wlr_texture, struct wlr_egl_context prev_ctx; wlr_egl_context_save(&prev_ctx); - wlr_egl_make_current(texture->renderer->egl); + wlr_egl_context_set_current(&texture->renderer->egl->ctx); push_gles2_debug(texture->renderer); @@ -111,7 +111,7 @@ static bool gles2_texture_invalidate(struct wlr_gles2_texture *texture) { struct wlr_egl_context prev_ctx; wlr_egl_context_save(&prev_ctx); - wlr_egl_make_current(texture->renderer->egl); + wlr_egl_context_set_current(&texture->renderer->egl->ctx); push_gles2_debug(texture->renderer); @@ -135,7 +135,7 @@ void gles2_texture_destroy(struct wlr_gles2_texture *texture) { struct wlr_egl_context prev_ctx; wlr_egl_context_save(&prev_ctx); - wlr_egl_make_current(texture->renderer->egl); + wlr_egl_context_set_current(&texture->renderer->egl->ctx); push_gles2_debug(texture->renderer); @@ -212,7 +212,7 @@ static struct wlr_texture *gles2_texture_from_pixels( struct wlr_egl_context prev_ctx; wlr_egl_context_save(&prev_ctx); - wlr_egl_make_current(renderer->egl); + wlr_egl_context_set_current(&renderer->egl->ctx); push_gles2_debug(renderer); @@ -264,7 +264,7 @@ static struct wlr_texture *gles2_texture_from_dmabuf( struct wlr_egl_context prev_ctx; wlr_egl_context_save(&prev_ctx); - wlr_egl_make_current(renderer->egl); + wlr_egl_context_set_current(&renderer->egl->ctx); bool external_only; texture->image = From f12fa92a8595932040958a3cb5172095cf3405a0 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Tue, 21 Sep 2021 15:13:42 -0400 Subject: [PATCH 7/8] render/gles2: make wlr_renderer_create private --- include/render/gles2.h | 1 + include/wlr/render/gles2.h | 1 - render/gles2/renderer.c | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/render/gles2.h b/include/render/gles2.h index fb2de1ab1e..7f887c18c6 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -111,6 +111,7 @@ struct wlr_gles2_texture { struct wlr_addon buffer_addon; }; +struct wlr_renderer *gles2_renderer_create(struct wlr_egl *egl); bool is_gles2_pixel_format_supported(const struct wlr_gles2_renderer *renderer, const struct wlr_gles2_pixel_format *format); diff --git a/include/wlr/render/gles2.h b/include/wlr/render/gles2.h index dabe49ddee..f8ca22bc78 100644 --- a/include/wlr/render/gles2.h +++ b/include/wlr/render/gles2.h @@ -16,7 +16,6 @@ struct wlr_egl; struct wlr_renderer *wlr_gles2_renderer_create_with_drm_fd(int drm_fd); -struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl); struct wlr_egl *wlr_gles2_renderer_get_egl(struct wlr_renderer *renderer); bool wlr_gles2_renderer_check_ext(struct wlr_renderer *renderer, diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 36403f7e13..dcf4535c1b 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -694,7 +694,7 @@ struct wlr_renderer *wlr_gles2_renderer_create_with_drm_fd(int drm_fd) { return NULL; } - struct wlr_renderer *renderer = wlr_gles2_renderer_create(egl); + struct wlr_renderer *renderer = gles2_renderer_create(egl); if (!renderer) { wlr_log(WLR_ERROR, "Failed to create GLES2 renderer"); wlr_egl_destroy(egl); @@ -704,7 +704,7 @@ struct wlr_renderer *wlr_gles2_renderer_create_with_drm_fd(int drm_fd) { return renderer; } -struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { +struct wlr_renderer *gles2_renderer_create(struct wlr_egl *egl) { if (!wlr_egl_context_set_current(&egl->ctx)) { return NULL; } From 4a4c8ca32082123364f3fb6e4f63ee539b31148f Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Tue, 21 Sep 2021 15:20:11 -0400 Subject: [PATCH 8/8] render/gles2: add getter for wlr_egl_context instead of wlr_egl --- include/wlr/render/gles2.h | 10 ++++++++-- render/gles2/renderer.c | 8 ++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/wlr/render/gles2.h b/include/wlr/render/gles2.h index f8ca22bc78..933a969d51 100644 --- a/include/wlr/render/gles2.h +++ b/include/wlr/render/gles2.h @@ -13,11 +13,17 @@ #include #include -struct wlr_egl; +struct wlr_egl_context; struct wlr_renderer *wlr_gles2_renderer_create_with_drm_fd(int drm_fd); -struct wlr_egl *wlr_gles2_renderer_get_egl(struct wlr_renderer *renderer); +/** + * Returns the current EGL context + * The pointer returned is not owned by the caller + */ +struct wlr_egl_context *wlr_gles2_renderer_get_egl_context( + struct wlr_renderer *wlr_renderer); + bool wlr_gles2_renderer_check_ext(struct wlr_renderer *renderer, const char *ext); /** diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index dcf4535c1b..62aab86202 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -494,10 +494,10 @@ static uint32_t gles2_get_render_buffer_caps(struct wlr_renderer *wlr_renderer) return WLR_BUFFER_CAP_DMABUF; } -struct wlr_egl *wlr_gles2_renderer_get_egl(struct wlr_renderer *wlr_renderer) { - struct wlr_gles2_renderer *renderer = - gles2_get_renderer(wlr_renderer); - return renderer->egl; +struct wlr_egl_context *wlr_gles2_renderer_get_egl_context( + struct wlr_renderer *wlr_renderer) { + struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer); + return &renderer->egl->ctx; } static void gles2_destroy(struct wlr_renderer *wlr_renderer) {