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,3 +1,9 @@
|
|||
// Modified by the Kitedesk fork (2026, B252): closeTexture no longer destroys
|
||||
// the texture before the engine is done with it, and keeps it for good unless
|
||||
// "recycle" is set; the textures of a closing engine are kept the same way;
|
||||
// createTexture of a key in use answers -1; getTexturePtr of a key that is not
|
||||
// open answers 0.
|
||||
|
||||
#include "texture_rgba_renderer_plugin.h"
|
||||
|
||||
// This must be included before many other Windows headers.
|
||||
|
|
@ -11,10 +17,32 @@
|
|||
#include <flutter/standard_method_codec.h>
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
namespace texture_rgba_renderer {
|
||||
|
||||
namespace {
|
||||
|
||||
// Textures closed while frames may still be pushed to the pointer from
|
||||
// getTexturePtr: closeTexture without "recycle" (a tab moving to another
|
||||
// window keeps its session's pointer), or an engine going away first. They are
|
||||
// never destroyed, so such a push finds a closed texture and not freed memory,
|
||||
// and their address is never handed out again for another texture (ABA). Their
|
||||
// lifetime is the process; each one keeps the object, not its frame buffers.
|
||||
struct Retired {
|
||||
std::mutex mutex;
|
||||
std::vector<std::shared_ptr<TextureRgba>> textures;
|
||||
};
|
||||
|
||||
Retired& retired() {
|
||||
static Retired* instance = new Retired();
|
||||
return *instance;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// static
|
||||
void TextureRgbaRendererPlugin::RegisterWithRegistrar(
|
||||
flutter::PluginRegistrarWindows *registrar) {
|
||||
|
|
@ -38,7 +66,28 @@ TextureRgbaRendererPlugin::TextureRgbaRendererPlugin() {
|
|||
this->texture_registrar = nullptr;
|
||||
}
|
||||
|
||||
TextureRgbaRendererPlugin::~TextureRgbaRendererPlugin() {}
|
||||
TextureRgbaRendererPlugin::~TextureRgbaRendererPlugin() {
|
||||
// The engine is going away (its texture registrar outlives the plugins), and
|
||||
// sessions may still push frames to these textures.
|
||||
for (auto &entry : textures_) {
|
||||
CloseTexture(std::move(entry.second), false);
|
||||
}
|
||||
}
|
||||
|
||||
void TextureRgbaRendererPlugin::CloseTexture(
|
||||
std::unique_ptr<TextureRgba> texture, bool recycle) {
|
||||
std::shared_ptr<TextureRgba> closed = std::move(texture);
|
||||
closed->Terminate();
|
||||
if (!recycle) {
|
||||
std::lock_guard<std::mutex> lock(retired().mutex);
|
||||
retired().textures.push_back(closed);
|
||||
}
|
||||
// The engine removes the texture on its raster thread, which may draw it
|
||||
// until then, and calls back there. Only then are the frame buffers freed,
|
||||
// and a recycled texture is destroyed with the last reference.
|
||||
this->texture_registrar->UnregisterTexture(closed->texture_id(),
|
||||
[closed] { closed->FreeBuffers(); });
|
||||
}
|
||||
|
||||
void TextureRgbaRendererPlugin::HandleMethodCall(
|
||||
const flutter::MethodCall<flutter::EncodableValue> &method_call,
|
||||
|
|
@ -52,16 +101,27 @@ void TextureRgbaRendererPlugin::HandleMethodCall(
|
|||
it->second = std::make_unique<TextureRgba>(this->texture_registrar);
|
||||
result->Success(flutter::EncodableValue(it->second->texture_id()));
|
||||
}
|
||||
else {
|
||||
// The key is in use. Without an answer the Dart future never completes.
|
||||
result->Success(flutter::EncodableValue(int64_t(-1)));
|
||||
}
|
||||
}
|
||||
else if (method_call.method_name().compare("closeTexture") == 0) {
|
||||
auto args = std::get<flutter::EncodableMap>(*method_call.arguments());
|
||||
auto key = std::get<int>(args.at(flutter::EncodableValue("key")));
|
||||
if (textures_.find(key) == textures_.end()) {
|
||||
auto it = textures_.find(key);
|
||||
if (it == textures_.end()) {
|
||||
result->Success(flutter::EncodableValue(false));
|
||||
}
|
||||
else {
|
||||
this->texture_registrar->UnregisterTexture(textures_[key]->texture_id());
|
||||
textures_.erase(key);
|
||||
// "recycle": true promises that nothing pushes frames to the pointer
|
||||
// from getTexturePtr any more, so the texture may be destroyed.
|
||||
auto recycle = args.find(flutter::EncodableValue("recycle"));
|
||||
const bool* recycle_value = recycle == args.end()
|
||||
? nullptr : std::get_if<bool>(&recycle->second);
|
||||
CloseTexture(std::move(it->second),
|
||||
recycle_value != nullptr && *recycle_value);
|
||||
textures_.erase(it);
|
||||
result->Success(flutter::EncodableValue(true));
|
||||
}
|
||||
}
|
||||
|
|
@ -83,7 +143,8 @@ void TextureRgbaRendererPlugin::HandleMethodCall(
|
|||
auto args = std::get<flutter::EncodableMap>(*method_call.arguments());
|
||||
auto key = std::get<int>(args.at(flutter::EncodableValue("key")));
|
||||
if (textures_.find(key) == textures_.end()) {
|
||||
result->Success(flutter::EncodableValue(-1));
|
||||
// Not open (e.g. closed meanwhile): no pointer for the session.
|
||||
result->Success(flutter::EncodableValue(0));
|
||||
}
|
||||
else {
|
||||
// Return an address.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue