From 91dee22da5d644806287e8423ccf2ba6d19a309c Mon Sep 17 00:00:00 2001 From: modeco80 Date: Mon, 5 Aug 2024 22:23:41 -0400 Subject: [PATCH] cleanup OpenGL FBO creation/deletion --- crates/retrovnc/src/main.rs | 63 +++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/crates/retrovnc/src/main.rs b/crates/retrovnc/src/main.rs index 1543169..0d85f1d 100644 --- a/crates/retrovnc/src/main.rs +++ b/crates/retrovnc/src/main.rs @@ -182,34 +182,42 @@ impl App { /// Destroys EGL resources. unsafe fn hw_gl_egl_exit(&mut self) { // Release the EGL context we created before destroying it - egl::MakeCurrent(self.egl_display, egl::NO_SURFACE, egl::NO_SURFACE, egl::NO_CONTEXT); + egl::MakeCurrent( + self.egl_display, + egl::NO_SURFACE, + egl::NO_SURFACE, + egl::NO_CONTEXT, + ); egl::DestroyContext(self.egl_display, self.egl_context); egl::Terminate(self.egl_display); self.egl_display = std::ptr::null(); self.egl_context = std::ptr::null(); } - fn hw_gl_resize_fbo(&mut self, width: u32, height: u32) { - // TODO: cleanup "delete" codepaths so we aren't duplicating as much. - // (we should probably just have a "create" and "delete" FBO pair.) + fn hw_gl_delete_fbo(&mut self) { unsafe { - if self.fbo_id == 0 { - gl::GenFramebuffers(1, std::ptr::addr_of_mut!(self.fbo_id)); - gl::BindFramebuffer(gl::FRAMEBUFFER, self.fbo_id); - } else { - gl::DeleteFramebuffers(1, std::ptr::addr_of_mut!(self.fbo_id)); - gl::GenFramebuffers(1, std::ptr::addr_of_mut!(self.fbo_id)); - gl::BindFramebuffer(gl::FRAMEBUFFER, self.fbo_id); + gl::DeleteFramebuffers(1, std::ptr::addr_of_mut!(self.fbo_id)); + self.fbo_id = 0; + + gl::DeleteTextures(1, std::ptr::addr_of_mut!(self.texture_id)); + self.texture_id = 0; + + gl::DeleteRenderbuffers(1, std::ptr::addr_of_mut!(self.renderbuffer_id)); + self.renderbuffer_id = 0; + } + } + + fn hw_gl_create_fbo(&mut self, width: u32, height: u32) { + unsafe { + if self.fbo_id != 0 { + self.hw_gl_delete_fbo(); } - if self.texture_id == 0 { - gl::GenTextures(1, std::ptr::addr_of_mut!(self.texture_id)); - gl::BindTexture(gl::TEXTURE_2D, self.texture_id); - } else { - gl::DeleteTextures(1, std::ptr::addr_of_mut!(self.texture_id)); - gl::GenTextures(1, std::ptr::addr_of_mut!(self.texture_id)); - gl::BindTexture(gl::TEXTURE_2D, self.texture_id); - } + gl::GenFramebuffers(1, std::ptr::addr_of_mut!(self.fbo_id)); + gl::BindFramebuffer(gl::FRAMEBUFFER, self.fbo_id); + + gl::GenTextures(1, std::ptr::addr_of_mut!(self.texture_id)); + gl::BindTexture(gl::TEXTURE_2D, self.texture_id); gl::TexImage2D( gl::TEXTURE_2D, @@ -223,14 +231,8 @@ impl App { std::ptr::null(), ); - if self.renderbuffer_id == 0 { - gl::GenRenderbuffers(1, std::ptr::addr_of_mut!(self.renderbuffer_id)); - gl::BindRenderbuffer(gl::RENDERBUFFER, self.renderbuffer_id); - } else { - gl::DeleteRenderbuffers(1, std::ptr::addr_of_mut!(self.renderbuffer_id)); - gl::GenRenderbuffers(1, std::ptr::addr_of_mut!(self.renderbuffer_id)); - gl::BindRenderbuffer(gl::RENDERBUFFER, self.renderbuffer_id); - } + gl::GenRenderbuffers(1, std::ptr::addr_of_mut!(self.renderbuffer_id)); + gl::BindRenderbuffer(gl::RENDERBUFFER, self.renderbuffer_id); gl::RenderbufferStorage( gl::RENDERBUFFER, @@ -262,6 +264,7 @@ impl App { gl::BindFramebuffer(gl::FRAMEBUFFER, 0); + // Notify the frontend layer about the new FBO let id = self.fbo_id; self.get_frontend().set_gl_fbo(id); @@ -292,7 +295,7 @@ impl FrontendInterface for App { // Resize OpenGL resources if we need to. if self.hw_render { - self.hw_gl_resize_fbo(width, height); + self.hw_gl_create_fbo(width, height); } self.rfb_server.resize(width as u16, height as u16); @@ -360,9 +363,9 @@ impl FrontendInterface for App { gl::Enable(gl::DEBUG_OUTPUT); gl::DebugMessageCallback(Some(opengl_message_callback), std::ptr::null()); - // Resize to initial dimensions (this will create an FBO as well) + // Create the initial FBO for the core to render to let dimensions = self.get_frontend().get_size(); - self.hw_gl_resize_fbo(dimensions.0, dimensions.1); + self.hw_gl_create_fbo(dimensions.0, dimensions.1); } self.hw_render = true;