linux: recycle closed textures per engine, closeTexture(recycle:) (Kitedesk B212)

Each session created a new TextureRgba and closeTexture never released it, so
the engine's FlPixelBufferTexture kept its GL texture (one frame of GPU or,
with llvmpipe, process memory per session). Releasing the object is not safe:
FlPixelBufferTexture deletes its GL texture in dispose without selecting a GL
context, so a release from another window deletes that window's texture of
the same name (upstream PR #2 turned the remaining window white).

closeTexture(key, recycle: true) now keeps the texture in a list of the
plugin instance, i.e. of the engine (window) that created it, and the next
createTexture of that engine registers it again. Its GL texture is only ever
touched by that engine's populate in its own context, nothing is released,
and the GL memory is bounded by the number of textures a window had open at
once. When the engine goes away the recycled textures stay referenced for the
same reason. Without recycle (old callers, or a tab that moves to another
window while its session still holds the pointer) a closed texture stays
terminated and is never handed out again.

With no frame waiting, copy_pixels hands out one transparent pixel instead
of no buffer, so a recycled texture cannot show the last frame of its previous
session before the first new frame.

Dart: closeTexture(int key, {bool recycle = false}) sends "recycle"; other
platforms ignore it. Tests: all ten Linux lifecycle tests pass (clang and
gcc, -Wall -Werror, also under ASan/UBSan); Dart 4/4.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
yunchan 2026-10-11 15:38:06 +09:00
parent 48fa1d7ddf
commit 8cd34a80ff
7 changed files with 106 additions and 12 deletions

View file

@ -1,5 +1,7 @@
// Modified by the Kitedesk fork (2026, B212): frame buffers are freed when the
// texture is closed and when the object is finalized.
// texture is closed and when the object is finalized; a closed texture can be
// restarted for reuse; with no frame waiting, one transparent pixel is handed
// out instead of no buffer.
#ifndef TEXTURE_RGBA_RENDERER_H
#define TEXTURE_RGBA_RENDERER_H
@ -66,6 +68,14 @@ static void texture_rgba_terminate(TextureRgba* self) {
g_mutex_unlock(&self->mutex);
}
// Makes a texture that was closed for recycling usable again (its frame
// buffers were freed when it was closed).
static void texture_rgba_restart(TextureRgba* self) {
g_mutex_lock(&self->mutex);
g_atomic_int_set(&self->terminated, FALSE);
g_mutex_unlock(&self->mutex);
}
static gboolean texture_rgba_is_terminate(TextureRgba* self) {
return g_atomic_int_get(&self->terminated);
}
@ -109,6 +119,14 @@ static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture,
*error = g_error_new(g_quark_from_static_string("TextureRgba Renderer"), -1, "the texture is already terminated, ignoring.");
return FALSE;
} else {
// No frame yet (first raster frame, or a recycled texture). Hand out one
// transparent pixel instead of no buffer: the engine would otherwise
// re-specify the GL texture without data and could show its previous
// content, e.g. the last frame of the session that used this texture.
static const uint8_t no_frame[4] = {0, 0, 0, 0};
*out_buffer = no_frame;
*width = 1;
*height = 1;
g_mutex_unlock(&self->mutex);
// *error = g_error_new(g_quark_from_static_string("TextureRgba Renderer"), 1, "waiting for new incoming images.");
return TRUE;