G26: keep Windows textures alive through asynchronous unregister

Retain closed raw-pointer identities for late frames, release frame buffers
after engine completion, and serialize termination with decoded frame writes.
Add native regression controls for held raster buffers, inline completion,
failed raster posts, missing registrations, teardown and concurrent closure.

B252; Apache-2.0 dependency license and upstream history preserved.
This commit is contained in:
yunchan 2026-10-29 12:13:44 +09:00
parent 8cd34a80ff
commit 50efc18ec5
10 changed files with 1268 additions and 14 deletions

View file

@ -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.