B252: keep closed textures valid until the engine is done with them
Windows: closeTexture hands the texture to UnregisterTexture's callback and frees its frame buffers there; frames pushed after close are dropped. Without "recycle" the object is kept for the process lifetime (the session may still hold its pointer); an engine that shuts down keeps its open textures the same way. A duplicate createTexture key gets -1, getTexturePtr of an unknown key 0. macOS: closeTexture drops the frame and closes the texture to later frames; only "recycle" removes it from the plugin. Linux: the frame handed to the engine last is freed at that engine's next copy_pixels or once the engine has shut down, not at close: from Flutter 3.32 the raster thread can still be uploading it. Tests: windows/test/run_tests.sh (plain, --asan, --tsan) and linux/test/run_tests.sh (plain, --asan). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8cd34a80ff
commit
f38b9420ac
15 changed files with 1388 additions and 31 deletions
|
|
@ -1,7 +1,8 @@
|
|||
// Modified by the Kitedesk fork (2026, B212): frame buffers are freed when the
|
||||
// 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.
|
||||
// Modified by the Kitedesk fork (2026, B212, B252): frame buffers are freed
|
||||
// when the texture is closed (the frame last handed to the engine once the
|
||||
// engine cannot be uploading it any more) 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
|
||||
|
|
@ -9,6 +10,8 @@
|
|||
#include <flutter_linux/flutter_linux.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "texture_rgba_renderer_plugin.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
|
@ -59,11 +62,46 @@ static void texture_rgba_free_buffers(TextureRgba* self) {
|
|||
g_atomic_int_set(&self->buffer_ready, FALSE);
|
||||
}
|
||||
|
||||
// Frames that copy_pixels handed to an engine and whose texture was closed
|
||||
// since. The engine can still be uploading such a frame: populate calls
|
||||
// glTexImage2D after copy_pixels returns, and unregistering a texture does not
|
||||
// wait for that (from Flutter 3.32 the engine rasterizes on its own thread). A
|
||||
// frame is freed at the engine's next copy_pixels, which comes after the
|
||||
// upload on the same raster thread, or once the engine has shut down.
|
||||
struct RetiredFrame {
|
||||
FlTextureRegistrar* engine;
|
||||
uint8_t* buffer;
|
||||
};
|
||||
static GMutex retired_frames_mutex;
|
||||
// Never destroyed: a raster thread can still run while the process exits.
|
||||
static std::vector<RetiredFrame>& retired_frames =
|
||||
*new std::vector<RetiredFrame>();
|
||||
|
||||
static void texture_rgba_free_retired_frames(FlTextureRegistrar* engine) {
|
||||
g_mutex_lock(&retired_frames_mutex);
|
||||
for (auto it = retired_frames.begin(); it != retired_frames.end();) {
|
||||
if (it->engine == engine) {
|
||||
delete[] it->buffer;
|
||||
it = retired_frames.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
g_mutex_unlock(&retired_frames_mutex);
|
||||
}
|
||||
|
||||
static void texture_rgba_terminate(TextureRgba* self) {
|
||||
g_mutex_lock(&self->mutex);
|
||||
// OnRgba stops copying frames and copy_pixels stops handing them out once
|
||||
// terminated is set. The frame waiting was never handed out; the one handed
|
||||
// out last waits in retired_frames.
|
||||
g_atomic_int_set(&self->terminated, TRUE);
|
||||
// The texture is unregistered, so the engine reads neither frame again, and
|
||||
// OnRgba stops copying frames once terminated is set.
|
||||
if (self->prior_buffer != nullptr) {
|
||||
g_mutex_lock(&retired_frames_mutex);
|
||||
retired_frames.push_back({self->texture_registrar, self->prior_buffer});
|
||||
g_mutex_unlock(&retired_frames_mutex);
|
||||
self->prior_buffer = nullptr;
|
||||
}
|
||||
texture_rgba_free_buffers(self);
|
||||
g_mutex_unlock(&self->mutex);
|
||||
}
|
||||
|
|
@ -88,6 +126,8 @@ static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture,
|
|||
TextureRgba *self = TEXTURE_RGBA(texture);
|
||||
// This method is called on Render Thread. Be careful with your
|
||||
// cross-thread operation.
|
||||
// The engine's earlier uploads on this thread are done.
|
||||
texture_rgba_free_retired_frames(self->texture_registrar);
|
||||
g_mutex_lock(&self->mutex);
|
||||
// @width and @height are initially stored the canvas size in Flutter.
|
||||
// You must prepare your pixel buffer in RGBA format.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue