From 50babfbc7a7793474a68e6f2c49fa8790a821f97 Mon Sep 17 00:00:00 2001 From: Neil Roberts Date: Fri, 21 Jan 2011 17:37:10 +0000 Subject: [PATCH] cogl-atlas-texture: Make copying a texture out more robust Previously when _cogl_atlas_texture_migrate_out_of_atlas is called it would unreference the atlas texture's sub-texture before calling _cogl_atlas_copy_rectangle. This would leave the atlas texture in an inconsistent state during the copy. This doesn't normally matter but if the copy ends up doing a render then the atlas texture may end up being referenced. In particular it would cause problems if the texture is left in a texture unit because then Cogl may try to call get_gl_texture even though the texture isn't actually being used for rendering. To fix this the sub texture is now unrefed after the copy call instead. --- cogl/cogl-atlas-texture.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/cogl/cogl-atlas-texture.c b/cogl/cogl-atlas-texture.c index 6f81f97b3..df06006c4 100644 --- a/cogl/cogl-atlas-texture.c +++ b/cogl/cogl-atlas-texture.c @@ -292,6 +292,8 @@ _cogl_atlas_texture_migrate_out_of_atlas (CoglAtlasTexture *atlas_tex) /* Make sure this texture is not in the atlas */ if (atlas_tex->atlas) { + CoglHandle sub_texture; + COGL_NOTE (ATLAS, "Migrating texture out of the atlas"); /* We don't know if any journal entries currently depend on @@ -304,14 +306,7 @@ _cogl_atlas_texture_migrate_out_of_atlas (CoglAtlasTexture *atlas_tex) */ cogl_flush (); - /* Notify cogl-pipeline.c that the texture's underlying GL texture - * storage is changing so it knows it may need to bind a new texture - * if the CoglTexture is reused with the same texture unit. */ - _cogl_pipeline_texture_storage_change_notify (atlas_tex); - - cogl_handle_unref (atlas_tex->sub_texture); - - atlas_tex->sub_texture = + sub_texture = _cogl_atlas_copy_rectangle (atlas_tex->atlas, atlas_tex->rectangle.x + 1, atlas_tex->rectangle.y + 1, @@ -320,6 +315,18 @@ _cogl_atlas_texture_migrate_out_of_atlas (CoglAtlasTexture *atlas_tex) COGL_TEXTURE_NO_ATLAS, atlas_tex->format); + /* Notify cogl-pipeline.c that the texture's underlying GL texture + * storage is changing so it knows it may need to bind a new texture + * if the CoglTexture is reused with the same texture unit. */ + _cogl_pipeline_texture_storage_change_notify (atlas_tex); + + /* We need to unref the sub texture after doing the copy because + the copy can involve rendering which might cause the texture + to be used if it is used from a layer that is left in a + texture unit */ + cogl_handle_unref (atlas_tex->sub_texture); + atlas_tex->sub_texture = sub_texture; + _cogl_atlas_texture_remove_from_atlas (atlas_tex); } }