diff options
author | sanine-a <sanine.not@pm.me> | 2020-12-17 23:27:39 -0600 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-12-17 23:27:39 -0600 |
commit | ac16a4e0ae9fcc272cf5f8a9c1350f27817df7d0 (patch) | |
tree | 5fdb84f9d92032e9b0a85469b1ab8c9b7a31954f | |
parent | 505b497cb1afed4a2b3c78292adaeca59fac12c3 (diff) |
fix segfault when destroying cairo textures
-rw-r--r-- | demo/main.lua | 16 | ||||
-rw-r--r-- | src/cairo_bindings.c | 23 |
2 files changed, 28 insertions, 11 deletions
diff --git a/demo/main.lua b/demo/main.lua index 75b9966..eb05beb 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -19,9 +19,22 @@ end) local tex = honey.texture.load('lowres.png', {minFilter='nearest', magFilter='nearest'}) +local cairo = honey.cairo() +cairo:setColor(1, 1, 1) +cairo:moveTo(1024,0) +cairo:lineTo(0,1024) +cairo:stroke() + +cairo:setColor(1, 0, 1, 0.2) +cairo:moveTo(0,0) +cairo:lineTo(1024,1024) +cairo:stroke() + +cairo:updateTexture() + local sceneRoot = Node.new() -local shader = SpatialShader.new(tex) +local shader = SpatialShader.new(cairo:getTexture()) local lightDirection = honey.glm.vec3{1,1,1} lightDirection:normalize() shader:setVec3('directional_lights[0].direction', lightDirection) @@ -70,6 +83,7 @@ function honey.update(dt) sceneRoot:updateCascade(dt) if total_time > 1 then print('FPS: '..tostring(total_frames/total_time)) + print(cairo) total_time = 0 total_frames = 0 end diff --git a/src/cairo_bindings.c b/src/cairo_bindings.c index 3becfb9..1c4c57b 100644 --- a/src/cairo_bindings.c +++ b/src/cairo_bindings.c @@ -27,14 +27,14 @@ int honey_setup_cairo(lua_State* L) /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ struct texture_ref { - int id; + int ref; lua_State* L; }; static void destroy_texture_ref(void* data) { struct texture_ref* ref = data; - luaL_unref(ref->L, LUA_REGISTRYINDEX, ref->id); + luaL_unref(ref->L, LUA_REGISTRYINDEX, ref->ref); free(ref); } @@ -53,10 +53,9 @@ int honey_cairo_new(lua_State* L) /* configure texture swizzling ARGB -> RGBA */ honey_texture* texture = lua_touserdata(L, -1); glBindTexture(GL_TEXTURE_2D, texture->id); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_GREEN); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_BLUE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_ALPHA); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED); /* setup texture reference */ struct texture_ref* ref = malloc(sizeof(struct texture_ref)); @@ -64,7 +63,8 @@ int honey_cairo_new(lua_State* L) honey_lua_throw_error (L, "failed to allocate memory for texture handle!"); - ref->id = luaL_ref(L, LUA_REGISTRYINDEX); + ref->ref = luaL_ref(L, LUA_REGISTRYINDEX); + ref->L = L; /* create cairo surface */ cairo_surface_t* surface = @@ -86,7 +86,7 @@ int honey_cairo_new(lua_State* L) (L, "error creating cairo context: %s", cairo_status_to_string(status)); cairo_surface_destroy(surface); - + /* bind texture ref to context userdata */ status = cairo_set_user_data(*cr, &TEXTURE_KEY, @@ -110,7 +110,7 @@ int honey_cairo_update_texture(lua_State* L) honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &cr); struct texture_ref *ref = cairo_get_user_data(*cr, &TEXTURE_KEY); - lua_rawgeti(L, LUA_REGISTRYINDEX, ref->id); + lua_rawgeti(L, LUA_REGISTRYINDEX, ref->ref); honey_texture* texture = lua_touserdata(L, -1); lua_pop(L, 1); @@ -125,6 +125,7 @@ int honey_cairo_update_texture(lua_State* L) GL_RGBA, GL_UNSIGNED_BYTE, image_data); + return 0; } @@ -136,7 +137,7 @@ int honey_cairo_get_texture(lua_State* L) honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &cr); struct texture_ref *ref = cairo_get_user_data(*cr, &TEXTURE_KEY); - lua_rawgeti(L, LUA_REGISTRYINDEX, ref->id); + lua_rawgeti(L, LUA_REGISTRYINDEX, ref->ref); return 1; } @@ -147,6 +148,8 @@ int honey_cairo_destroy(lua_State* L) cairo_t** cr; honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &cr); + struct texture_ref* ref = cairo_get_user_data(*cr, &TEXTURE_KEY); + cairo_destroy(*cr); return 0; |