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:
yunchan 2026-10-27 03:19:05 +09:00
parent 8cd34a80ff
commit f38b9420ac
15 changed files with 1388 additions and 31 deletions

View file

@ -1,7 +1,14 @@
// Modified by the Kitedesk fork (2026, B252): the texture no longer
// unregisters itself when destroyed (closeTexture does, and the plugin keeps
// the object alive until the engine is done with it); a closed texture drops
// pushed frames, hands the engine no buffer, and frees its frame buffers once
// the engine has unregistered it.
#ifndef TEXTURE_RGBA_H_
#define TEXTURE_RGBA_H_
#include <flutter/texture_registrar.h>
#include <atomic>
#include <mutex>
#include <chrono>
@ -9,7 +16,6 @@ class TextureRgba
{
public:
TextureRgba(flutter::TextureRegistrar* texture_registrar);
~TextureRgba();
void MarkVideoFrameAvailable(std::vector<uint8_t>& buffer, size_t width, size_t height);
@ -19,12 +25,23 @@ public:
const FlutterDesktopPixelBuffer* buffer();
// Closes the texture: frames pushed from now on are dropped without
// touching the texture registrar, and the engine gets no buffer any more.
void Terminate();
// Frees the frame buffers. Call after Terminate, once the engine has
// unregistered the texture and so no longer reads them.
void FreeBuffers();
private:
FlutterDesktopPixelBuffer flutter_pixel_buffer_{};
flutter::TextureRegistrar* texture_registrar_ = nullptr;
std::unique_ptr<flutter::TextureVariant> texture_ = nullptr;
int64_t texture_id_;
std::mutex mutex_;
// Set and read with mutex_ held, so that no frame is copied or announced
// to the engine after Terminate returns.
std::atomic<bool> terminated_{false};
int fg_index_ = 0;
bool buff_ready_ = false;
size_t width_[2];