From d5556ec78f65c286ae281a17d7b6b2ddc38c4c00 Mon Sep 17 00:00:00 2001
From: Simon Ser <contact@emersion.fr>
Date: Thu, 22 Feb 2024 19:26:06 +0100
Subject: render/egl: add save_context parameter to wlr_egl_make_current()

Saving the old context and immediately making our own context
current is a common pattern. Let's make it easier to do.

No functional change, just refactoring.
---
 render/gles2/renderer.c | 22 ++++++++--------------
 render/gles2/texture.c  | 23 ++++++++---------------
 2 files changed, 16 insertions(+), 29 deletions(-)

(limited to 'render/gles2')

diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c
index e095f469..3aed64ee 100644
--- a/render/gles2/renderer.c
+++ b/render/gles2/renderer.c
@@ -56,8 +56,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_make_current(buffer->renderer->egl);
+	wlr_egl_make_current(buffer->renderer->egl, &prev_ctx);
 
 	push_gles2_debug(buffer->renderer);
 
@@ -208,7 +207,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_make_current(renderer->egl, NULL);
 
 	struct wlr_gles2_texture *tex, *tex_tmp;
 	wl_list_for_each_safe(tex, tex_tmp, &renderer->textures, link) {
@@ -247,8 +246,7 @@ static struct wlr_render_pass *gles2_begin_buffer_pass(struct wlr_renderer *wlr_
 	struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
 
 	struct wlr_egl_context prev_ctx = {0};
-	wlr_egl_save_context(&prev_ctx);
-	if (!wlr_egl_make_current(renderer->egl)) {
+	if (!wlr_egl_make_current(renderer->egl, &prev_ctx)) {
 		return NULL;
 	}
 
@@ -276,8 +274,7 @@ GLuint wlr_gles2_renderer_get_buffer_fbo(struct wlr_renderer *wlr_renderer,
 	GLuint fbo = 0;
 
 	struct wlr_egl_context prev_ctx = {0};
-	wlr_egl_save_context(&prev_ctx);
-	if (!wlr_egl_make_current(renderer->egl)) {
+	if (!wlr_egl_make_current(renderer->egl, &prev_ctx)) {
 		return 0;
 	}
 
@@ -305,8 +302,7 @@ static struct wlr_render_timer *gles2_render_timer_create(struct wlr_renderer *w
 	timer->renderer = renderer;
 
 	struct wlr_egl_context prev_ctx;
-	wlr_egl_save_context(&prev_ctx);
-	wlr_egl_make_current(renderer->egl);
+	wlr_egl_make_current(renderer->egl, &prev_ctx);
 	renderer->procs.glGenQueriesEXT(1, &timer->id);
 	wlr_egl_restore_context(&prev_ctx);
 
@@ -318,8 +314,7 @@ static int gles2_get_render_time(struct wlr_render_timer *wlr_timer) {
 	struct wlr_gles2_renderer *renderer = timer->renderer;
 
 	struct wlr_egl_context prev_ctx;
-	wlr_egl_save_context(&prev_ctx);
-	wlr_egl_make_current(renderer->egl);
+	wlr_egl_make_current(renderer->egl, &prev_ctx);
 
 	GLint64 disjoint;
 	renderer->procs.glGetInteger64vEXT(GL_GPU_DISJOINT_EXT, &disjoint);
@@ -353,8 +348,7 @@ static void gles2_render_timer_destroy(struct wlr_render_timer *wlr_timer) {
 	struct wlr_gles2_renderer *renderer = timer->renderer;
 
 	struct wlr_egl_context prev_ctx;
-	wlr_egl_save_context(&prev_ctx);
-	wlr_egl_make_current(renderer->egl);
+	wlr_egl_make_current(renderer->egl, &prev_ctx);
 	renderer->procs.glDeleteQueriesEXT(1, &timer->id);
 	wlr_egl_restore_context(&prev_ctx);
 	free(timer);
@@ -521,7 +515,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_make_current(egl, NULL)) {
 		return NULL;
 	}
 
diff --git a/render/gles2/texture.c b/render/gles2/texture.c
index d66e9c16..f9ad4cc5 100644
--- a/render/gles2/texture.c
+++ b/render/gles2/texture.c
@@ -68,8 +68,7 @@ static bool gles2_texture_update_from_buffer(struct wlr_texture *wlr_texture,
 	}
 
 	struct wlr_egl_context prev_ctx;
-	wlr_egl_save_context(&prev_ctx);
-	wlr_egl_make_current(texture->renderer->egl);
+	wlr_egl_make_current(texture->renderer->egl, &prev_ctx);
 
 	push_gles2_debug(texture->renderer);
 
@@ -112,8 +111,7 @@ void gles2_texture_destroy(struct wlr_gles2_texture *texture) {
 		wlr_buffer_unlock(texture->buffer->buffer);
 	} else {
 		struct wlr_egl_context prev_ctx;
-		wlr_egl_save_context(&prev_ctx);
-		wlr_egl_make_current(texture->renderer->egl);
+		wlr_egl_make_current(texture->renderer->egl, &prev_ctx);
 
 		push_gles2_debug(texture->renderer);
 
@@ -196,9 +194,7 @@ static bool gles2_texture_read_pixels(struct wlr_texture *wlr_texture,
 
 	push_gles2_debug(texture->renderer);
 	struct wlr_egl_context prev_ctx;
-	wlr_egl_save_context(&prev_ctx);
-
-	if (!wlr_egl_make_current(texture->renderer->egl)) {
+	if (!wlr_egl_make_current(texture->renderer->egl, &prev_ctx)) {
 		return false;
 	}
 
@@ -240,13 +236,12 @@ static uint32_t gles2_texture_preferred_read_format(struct wlr_texture *wlr_text
 	struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture);
 
 	push_gles2_debug(texture->renderer);
-	struct wlr_egl_context prev_ctx;
-	wlr_egl_save_context(&prev_ctx);
 
 	uint32_t fmt = DRM_FORMAT_INVALID;
 
-	if (!wlr_egl_make_current(texture->renderer->egl)) {
-		goto out;
+	struct wlr_egl_context prev_ctx;
+	if (!wlr_egl_make_current(texture->renderer->egl, &prev_ctx)) {
+		return fmt;
 	}
 
 	if (!gles2_texture_bind(texture)) {
@@ -339,8 +334,7 @@ static struct wlr_texture *gles2_texture_from_pixels(
 	}
 
 	struct wlr_egl_context prev_ctx;
-	wlr_egl_save_context(&prev_ctx);
-	wlr_egl_make_current(renderer->egl);
+	wlr_egl_make_current(renderer->egl, &prev_ctx);
 
 	push_gles2_debug(renderer);
 
@@ -387,8 +381,7 @@ static struct wlr_texture *gles2_texture_from_dmabuf(
 	texture->has_alpha = pixel_format_has_alpha(attribs->format);
 
 	struct wlr_egl_context prev_ctx;
-	wlr_egl_save_context(&prev_ctx);
-	wlr_egl_make_current(renderer->egl);
+	wlr_egl_make_current(renderer->egl, &prev_ctx);
 	push_gles2_debug(texture->renderer);
 
 	bool invalid;
-- 
cgit v1.2.3