From 08d3521b6175c44dd062d487a9f03590624d8b35 Mon Sep 17 00:00:00 2001 From: martinfouilleul Date: Thu, 9 Feb 2023 18:40:42 +0100 Subject: [PATCH] [canvas/surface cleanup] - properly destroy gl canvas backend - properly destroy gl surface --- examples/win_canvas/main.c | 2 ++ src/gl_canvas.c | 51 +++++++++++++++++++++++--------------- src/win32_gl_loader.h | 2 ++ src/win32_gl_surface.c | 8 +++++- todo.txt | 12 ++++++--- 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/examples/win_canvas/main.c b/examples/win_canvas/main.c index 3649639..ed4887f 100644 --- a/examples/win_canvas/main.c +++ b/examples/win_canvas/main.c @@ -238,6 +238,8 @@ int main() frameTime = mp_get_time(MP_CLOCK_MONOTONIC) - startTime; } + mg_canvas_destroy(canvas); + mg_surface_destroy(surface); mp_terminate(); return(0); diff --git a/src/gl_canvas.c b/src/gl_canvas.c index 8205cf7..7d816ec 100644 --- a/src/gl_canvas.c +++ b/src/gl_canvas.c @@ -17,19 +17,20 @@ typedef struct mg_gl_canvas_backend mg_canvas_backend interface; mg_surface surface; - GLint dummyVertexBuffer; - GLint vertexBuffer; - GLint shapeBuffer; - GLint indexBuffer; - GLint tileCounterBuffer; - GLint tileArrayBuffer; - GLint clearCounterProgram; - GLint tileProgram; - GLint sortProgram; - GLint drawProgram; - GLint blitProgram; + GLuint vao; + GLuint dummyVertexBuffer; + GLuint vertexBuffer; + GLuint shapeBuffer; + GLuint indexBuffer; + GLuint tileCounterBuffer; + GLuint tileArrayBuffer; + GLuint clearCounterProgram; + GLuint tileProgram; + GLuint sortProgram; + GLuint drawProgram; + GLuint blitProgram; - GLint outTexture; + GLuint outTexture; char* indexMapping; char* vertexMapping; @@ -255,7 +256,21 @@ void mg_gl_canvas_destroy(mg_canvas_backend* interface) { mg_gl_canvas_backend* backend = (mg_gl_canvas_backend*)interface; - //TODO + glDeleteTextures(1, &backend->outTexture); + + glDeleteBuffers(1, &backend->dummyVertexBuffer); + glDeleteBuffers(1, &backend->vertexBuffer); + glDeleteBuffers(1, &backend->shapeBuffer); + glDeleteBuffers(1, &backend->indexBuffer); + glDeleteBuffers(1, &backend->tileCounterBuffer); + glDeleteBuffers(1, &backend->tileArrayBuffer); + + glDeleteVertexArrays(1, &backend->vao); + + free(backend->shapeMapping); + free(backend->vertexMapping); + free(backend->indexMapping); + free(backend); } void mg_gl_canvas_atlas_upload(mg_canvas_backend* interface, mp_rect rect, u8* bytes) @@ -391,9 +406,8 @@ mg_canvas_backend* mg_gl_canvas_create(mg_surface surface) mg_surface_prepare(surface); - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + glGenVertexArrays(1, &backend->vao); + glBindVertexArray(backend->vao); glGenBuffers(1, &backend->dummyVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, backend->dummyVertexBuffer); @@ -444,10 +458,7 @@ mg_canvas_backend* mg_gl_canvas_create(mg_surface surface) if(err) { - free(backend->shapeMapping); - free(backend->vertexMapping); - free(backend->indexMapping); - free(backend); + mg_gl_canvas_destroy((mg_canvas_backend*)backend); backend = 0; } else diff --git a/src/win32_gl_loader.h b/src/win32_gl_loader.h index bfebdb1..ed7c649 100644 --- a/src/win32_gl_loader.h +++ b/src/win32_gl_loader.h @@ -37,6 +37,8 @@ GL_PROC(GLBINDBUFFER, glBindBuffer) \ GL_PROC(GLBUFFERDATA, glBufferData) \ GL_PROC(GLBUFFERSUBDATA, glBufferSubData) \ + GL_PROC(GLDELETEVERTEXARRAYS, glDeleteVertexArrays) \ + GL_PROC(GLDELETEBUFFERS, glDeleteBuffers) \ GL_PROC(GLUNIFORMMATRIX4FV, glUniformMatrix4fv) \ GL_PROC(GLVERTEXATTRIBPOINTER, glVertexAttribPointer) \ GL_PROC(GLENABLEVERTEXATTRIBARRAY, glEnableVertexAttribArray) \ diff --git a/src/win32_gl_surface.c b/src/win32_gl_surface.c index a3223c5..c6aa844 100644 --- a/src/win32_gl_surface.c +++ b/src/win32_gl_surface.c @@ -26,7 +26,13 @@ typedef struct mg_gl_surface void mg_gl_surface_destroy(mg_surface_data* interface) { mg_gl_surface* surface = (mg_gl_surface*)interface; - //TODO + + if(surface->glContext == wglGetCurrentContext()) + { + wglMakeCurrent(NULL, NULL); + } + wglDeleteContext(surface->glContext); + free(surface); } void mg_gl_surface_prepare(mg_surface_data* interface) diff --git a/todo.txt b/todo.txt index 2078ec3..06c00f5 100644 --- a/todo.txt +++ b/todo.txt @@ -3,7 +3,8 @@ Overview -------- [x] Pan/Zoom on text example [.] Clean+Fixes of canvas code and examples -[ ] Make backend selection easier + +[>] Make backend selection easier [ ] Image API and backend [ ] Build image atlas on top @@ -12,14 +13,15 @@ Overview [ ] Allow different versions of GL to co-exist [?] Backport canvas to GLES +[ ] Back surface by child windows and implement moving frame/hiding/overlay + [ ] Delegated drawing API+Impl Clean+Fixes ----------- [x] Rename MG_GL_CANVAS_TILE_ARRAY_SIZE/LENGTH unambiguously and make it consistent between C and glsl code - -[.] Clean shaders (folder/filenames, version string, debug flags, ...) +[x] Clean shaders (folder/filenames, version string, debug flags, ...) [x] Simplify shader names, prefix embedded strings by "glsl_" [x] Extract shader compilation in a function [x] Add version strings/common structs in canvas code (maybe revisit later when we have multiple versions, but ok for now) @@ -28,6 +30,10 @@ Clean+Fixes [x] Clean-up context on error and return nil handle [/] Could return non-nil "invalid" handle and set error flags in handle +[x] Destroy canvas backend properly +[x] Destroy surface properly +[ ] Destroy window properly + [>] Make surface backend and canvas backend compile-time and run-time selections easier [ ] GL loader: allow using different GL versions (eg using a desktop GL surface and a GLES surface in the same app).