From bdb78b2af6f5c9a2857822bb7604cf52a3ecf2be Mon Sep 17 00:00:00 2001 From: Trigger Huang Date: Fri, 24 Apr 2026 11:16:06 +0800 Subject: [PATCH] renderer-gl: split init/destroy into two parts Split the renderer init/destroy into two parts: one to init/destroy (E)GL, and one to init/destroy the renderer state around it. Introduce the following two new helper functions to centralize the handling of GL-related operations - gl_renderer_init_context - gl_renderer_destroy_context Signed-off-by: Trigger Huang --- libweston/renderer-gl/gl-renderer.c | 181 ++++++++++++++++++---------- 1 file changed, 120 insertions(+), 61 deletions(-) diff --git a/libweston/renderer-gl/gl-renderer.c b/libweston/renderer-gl/gl-renderer.c index 1c1ba0d34..39d0aa873 100644 --- a/libweston/renderer-gl/gl-renderer.c +++ b/libweston/renderer-gl/gl-renderer.c @@ -5118,23 +5118,26 @@ gl_renderer_allocator_create(struct gl_renderer *gr, } static void -gl_renderer_destroy(struct weston_compositor *ec) +gl_renderer_destroy_context(struct weston_compositor *ec) { struct gl_renderer *gr = get_renderer(ec); struct dmabuf_format *format, *next_format; struct gl_capture_task *gl_task, *tmp; - wl_signal_emit(&gr->destroy_signal, gr); - - if (gr->display_bound) + if (gr->display_bound) { gr->unbind_display(gr->egl_display, ec->wl_display); + gr->display_bound = false; + } wl_list_for_each_safe(gl_task, tmp, &gr->pending_capture_list, link) destroy_capture_task(gl_task); gl_renderer_shader_list_destroy(gr); - if (gr->fallback_shader) + if (gr->fallback_shader) { gl_shader_destroy(gr, gr->fallback_shader); + gr->fallback_shader = NULL; + } + gr->current_shader = NULL; if (gr->wireframe_tex) gl_texture_fini(&gr->wireframe_tex); @@ -5149,11 +5152,29 @@ gl_renderer_destroy(struct weston_compositor *ec) weston_drm_format_array_fini(&gr->supported_dmabuf_formats); free(gr->supported_rendering_formats); + gr->supported_rendering_formats = NULL; + gr->supported_rendering_formats_count = 0; - gl_renderer_allocator_destroy(gr->allocator); + gr->base.dmabuf_alloc = NULL; + gr->base.import_dmabuf = NULL; + gr->base.get_supported_dmabuf_formats = NULL; + gr->base.create_renderbuffer_dmabuf = NULL; eglTerminate(gr->egl_display); eglReleaseThread(); +} + +static void +gl_renderer_destroy(struct weston_compositor *ec) +{ + struct gl_renderer *gr = get_renderer(ec); + + wl_signal_emit(&gr->destroy_signal, gr); + + gl_renderer_destroy_context(ec); + + gl_renderer_allocator_destroy(gr->allocator); + gr->allocator = NULL; wl_array_release(&gr->position_stream); wl_array_release(&gr->barycentric_stream); @@ -5202,57 +5223,32 @@ create_default_dmabuf_feedback(struct weston_compositor *ec, } static int -gl_renderer_display_create(struct weston_compositor *ec, - const struct gl_renderer_display_options *options) +gl_renderer_init_context(struct weston_compositor *ec, + const struct gl_renderer_display_options *options) { - struct gl_renderer *gr; - const struct pixel_format_info *info; - int ret, nformats, i, j; - bool supported; + struct gl_renderer *gr = get_renderer(ec); + int ret; - gr = zalloc(sizeof *gr); - if (gr == NULL) - return -1; - - gr->compositor = ec; - wl_list_init(&gr->shader_list); gr->platform = options->egl_platform; - - gr->extensions_scope = weston_compositor_add_log_scope(ec, "gl-renderer-ext", - "Print GL-renderer extensions\n", NULL, NULL, gr); - gr->paint_node_scope = weston_compositor_add_log_scope(ec, "gl-renderer-paint-nodes", - "Print GL-renderer debug information about paint nodes\n", NULL, NULL, gr); - gr->shader_scope = gl_shader_scope_create(gr); - - if (gl_renderer_setup_egl_client_extensions(gr) < 0) - goto fail; - - gr->base.repaint_output = gl_renderer_repaint_output; - gr->base.resize_output = gl_renderer_resize_output; - gr->base.create_renderbuffer = gl_renderer_create_renderbuffer; - gr->base.destroy_renderbuffer = gl_renderer_destroy_renderbuffer; - gr->base.flush_damage = gl_renderer_flush_damage; - gr->base.attach = gl_renderer_attach; - gr->base.destroy = gl_renderer_destroy; - gr->base.surface_copy_content = gl_renderer_surface_copy_content; - gr->base.fill_buffer_info = gl_renderer_fill_buffer_info; - gr->base.buffer_init = gl_renderer_buffer_init; - gr->base.output_set_border = gl_renderer_output_set_border; - gr->base.type = WESTON_RENDERER_GL; + gr->egl_device_extensions = 0; + gr->egl_display_extensions = 0; + gr->gl_extensions = 0; + gr->features = 0; + gr->get_graphics_reset_status = NULL; + gr->supported_rendering_formats = NULL; + gr->supported_rendering_formats_count = 0; + gr->base.dmabuf_alloc = NULL; + gr->base.import_dmabuf = NULL; + gr->base.get_supported_dmabuf_formats = NULL; + gr->base.create_renderbuffer_dmabuf = NULL; if (gl_renderer_setup_egl_display(gr, options->egl_native_display) < 0) goto fail; - gr->allocator = gl_renderer_allocator_create(gr, options); - if (!gr->allocator) - weston_log("failed to initialize allocator\n"); - weston_drm_format_array_init(&gr->supported_dmabuf_formats); log_egl_info(gr, gr->egl_display); - ec->renderer = &gr->base; - if (gl_renderer_setup_egl_extensions(ec) < 0) goto fail_with_error; @@ -5317,8 +5313,85 @@ gl_renderer_display_create(struct weston_compositor *ec, } wl_list_init(&gr->dmabuf_formats); + return 0; + +fail_with_error: + gl_renderer_print_egl_error_state(); + if (gr->drm_device) { + weston_dmabuf_feedback_destroy(ec->default_dmabuf_feedback); + ec->default_dmabuf_feedback = NULL; + } +fail_feedback: + if (gr->drm_device) { + weston_dmabuf_feedback_format_table_destroy(ec->dmabuf_feedback_format_table); + ec->dmabuf_feedback_format_table = NULL; + } +fail_terminate: + free(gr->supported_rendering_formats); + gr->supported_rendering_formats = NULL; + gr->supported_rendering_formats_count = 0; + weston_drm_format_array_fini(&gr->supported_dmabuf_formats); + gr->base.dmabuf_alloc = NULL; + gr->base.import_dmabuf = NULL; + gr->base.get_supported_dmabuf_formats = NULL; + gr->base.create_renderbuffer_dmabuf = NULL; + eglTerminate(gr->egl_display); +fail: + return -1; +} + +static int +gl_renderer_display_create(struct weston_compositor *ec, + const struct gl_renderer_display_options *options) +{ + struct gl_renderer *gr; + const struct pixel_format_info *info; + int nformats, i, j; + bool supported; + + gr = zalloc(sizeof *gr); + if (gr == NULL) + return -1; + + gr->compositor = ec; + wl_list_init(&gr->shader_list); + gr->platform = options->egl_platform; + + gr->extensions_scope = weston_compositor_add_log_scope(ec, "gl-renderer-ext", + "Print GL-renderer extensions\n", NULL, NULL, gr); + gr->paint_node_scope = weston_compositor_add_log_scope(ec, "gl-renderer-paint-nodes", + "Print GL-renderer debug information about paint nodes\n", NULL, NULL, gr); + gr->shader_scope = gl_shader_scope_create(gr); + + if (gl_renderer_setup_egl_client_extensions(gr) < 0) + goto fail; + + gr->base.repaint_output = gl_renderer_repaint_output; + gr->base.resize_output = gl_renderer_resize_output; + gr->base.create_renderbuffer = gl_renderer_create_renderbuffer; + gr->base.destroy_renderbuffer = gl_renderer_destroy_renderbuffer; + gr->base.flush_damage = gl_renderer_flush_damage; + gr->base.attach = gl_renderer_attach; + gr->base.destroy = gl_renderer_destroy; + gr->base.surface_copy_content = gl_renderer_surface_copy_content; + gr->base.fill_buffer_info = gl_renderer_fill_buffer_info; + gr->base.buffer_init = gl_renderer_buffer_init; + gr->base.output_set_border = gl_renderer_output_set_border; + gr->base.type = WESTON_RENDERER_GL; + + ec->renderer = &gr->base; + wl_signal_init(&gr->destroy_signal); + if (gl_renderer_init_context(ec, options) < 0) + goto fail_context; + + gr->allocator = gl_renderer_allocator_create(gr, options); + if (!gr->allocator) + weston_log("failed to initialize allocator\n"); + else + gr->base.dmabuf_alloc = gl_renderer_dmabuf_alloc; + /* Register supported wl_shm RGB formats. */ nformats = pixel_format_get_info_count(); for (i = 0; i < nformats; i++) { @@ -5371,21 +5444,7 @@ gl_renderer_display_create(struct weston_compositor *ec, return 0; -fail_with_error: - gl_renderer_print_egl_error_state(); - if (gr->drm_device) { - weston_dmabuf_feedback_destroy(ec->default_dmabuf_feedback); - ec->default_dmabuf_feedback = NULL; - } -fail_feedback: - if (gr->drm_device) { - weston_dmabuf_feedback_format_table_destroy(ec->dmabuf_feedback_format_table); - ec->dmabuf_feedback_format_table = NULL; - } -fail_terminate: - free(gr->supported_rendering_formats); - weston_drm_format_array_fini(&gr->supported_dmabuf_formats); - eglTerminate(gr->egl_display); +fail_context: fail: weston_log_scope_destroy(gr->shader_scope); weston_log_scope_destroy(gr->extensions_scope);