[canvas/surface cleanup]

- properly destroy gl canvas backend
- properly destroy gl surface
This commit is contained in:
martinfouilleul 2023-02-09 18:40:42 +01:00
parent af7cbae1fa
commit 08d3521b61
5 changed files with 51 additions and 24 deletions

View File

@ -238,6 +238,8 @@ int main()
frameTime = mp_get_time(MP_CLOCK_MONOTONIC) - startTime; frameTime = mp_get_time(MP_CLOCK_MONOTONIC) - startTime;
} }
mg_canvas_destroy(canvas);
mg_surface_destroy(surface);
mp_terminate(); mp_terminate();
return(0); return(0);

View File

@ -17,19 +17,20 @@ typedef struct mg_gl_canvas_backend
mg_canvas_backend interface; mg_canvas_backend interface;
mg_surface surface; mg_surface surface;
GLint dummyVertexBuffer; GLuint vao;
GLint vertexBuffer; GLuint dummyVertexBuffer;
GLint shapeBuffer; GLuint vertexBuffer;
GLint indexBuffer; GLuint shapeBuffer;
GLint tileCounterBuffer; GLuint indexBuffer;
GLint tileArrayBuffer; GLuint tileCounterBuffer;
GLint clearCounterProgram; GLuint tileArrayBuffer;
GLint tileProgram; GLuint clearCounterProgram;
GLint sortProgram; GLuint tileProgram;
GLint drawProgram; GLuint sortProgram;
GLint blitProgram; GLuint drawProgram;
GLuint blitProgram;
GLint outTexture; GLuint outTexture;
char* indexMapping; char* indexMapping;
char* vertexMapping; 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; 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) 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); mg_surface_prepare(surface);
GLuint vao; glGenVertexArrays(1, &backend->vao);
glGenVertexArrays(1, &vao); glBindVertexArray(backend->vao);
glBindVertexArray(vao);
glGenBuffers(1, &backend->dummyVertexBuffer); glGenBuffers(1, &backend->dummyVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, backend->dummyVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, backend->dummyVertexBuffer);
@ -444,10 +458,7 @@ mg_canvas_backend* mg_gl_canvas_create(mg_surface surface)
if(err) if(err)
{ {
free(backend->shapeMapping); mg_gl_canvas_destroy((mg_canvas_backend*)backend);
free(backend->vertexMapping);
free(backend->indexMapping);
free(backend);
backend = 0; backend = 0;
} }
else else

View File

@ -37,6 +37,8 @@
GL_PROC(GLBINDBUFFER, glBindBuffer) \ GL_PROC(GLBINDBUFFER, glBindBuffer) \
GL_PROC(GLBUFFERDATA, glBufferData) \ GL_PROC(GLBUFFERDATA, glBufferData) \
GL_PROC(GLBUFFERSUBDATA, glBufferSubData) \ GL_PROC(GLBUFFERSUBDATA, glBufferSubData) \
GL_PROC(GLDELETEVERTEXARRAYS, glDeleteVertexArrays) \
GL_PROC(GLDELETEBUFFERS, glDeleteBuffers) \
GL_PROC(GLUNIFORMMATRIX4FV, glUniformMatrix4fv) \ GL_PROC(GLUNIFORMMATRIX4FV, glUniformMatrix4fv) \
GL_PROC(GLVERTEXATTRIBPOINTER, glVertexAttribPointer) \ GL_PROC(GLVERTEXATTRIBPOINTER, glVertexAttribPointer) \
GL_PROC(GLENABLEVERTEXATTRIBARRAY, glEnableVertexAttribArray) \ GL_PROC(GLENABLEVERTEXATTRIBARRAY, glEnableVertexAttribArray) \

View File

@ -26,7 +26,13 @@ typedef struct mg_gl_surface
void mg_gl_surface_destroy(mg_surface_data* interface) void mg_gl_surface_destroy(mg_surface_data* interface)
{ {
mg_gl_surface* surface = (mg_gl_surface*)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) void mg_gl_surface_prepare(mg_surface_data* interface)

View File

@ -3,7 +3,8 @@ Overview
-------- --------
[x] Pan/Zoom on text example [x] Pan/Zoom on text example
[.] Clean+Fixes of canvas code and examples [.] Clean+Fixes of canvas code and examples
[ ] Make backend selection easier
[>] Make backend selection easier
[ ] Image API and backend [ ] Image API and backend
[ ] Build image atlas on top [ ] Build image atlas on top
@ -12,14 +13,15 @@ Overview
[ ] Allow different versions of GL to co-exist [ ] Allow different versions of GL to co-exist
[?] Backport canvas to GLES [?] Backport canvas to GLES
[ ] Back surface by child windows and implement moving frame/hiding/overlay
[ ] Delegated drawing API+Impl [ ] Delegated drawing API+Impl
Clean+Fixes Clean+Fixes
----------- -----------
[x] Rename MG_GL_CANVAS_TILE_ARRAY_SIZE/LENGTH unambiguously and make it consistent between C and glsl code [x] Rename MG_GL_CANVAS_TILE_ARRAY_SIZE/LENGTH unambiguously and make it consistent between C and glsl code
[x] Clean shaders (folder/filenames, version string, debug flags, ...)
[.] Clean shaders (folder/filenames, version string, debug flags, ...)
[x] Simplify shader names, prefix embedded strings by "glsl_" [x] Simplify shader names, prefix embedded strings by "glsl_"
[x] Extract shader compilation in a function [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) [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 [x] Clean-up context on error and return nil handle
[/] Could return non-nil "invalid" handle and set error flags in 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 [>] 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). [ ] GL loader: allow using different GL versions (eg using a desktop GL surface and a GLES surface in the same app).