flutter_texture_rgba_renderer/windows/texture_rgba_renderer_plugin.cpp
yunchan f38b9420ac 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>
2026-10-27 03:19:05 +09:00

161 lines
6.3 KiB
C++

// 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.
#include <windows.h>
// For getPlatformVersion; remove unless needed for your plugin implementation.
#include <VersionHelpers.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#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) {
auto channel =
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
registrar->messenger(), "texture_rgba_renderer",
&flutter::StandardMethodCodec::GetInstance());
auto plugin = std::make_unique<TextureRgbaRendererPlugin>();
plugin->texture_registrar = registrar->texture_registrar();
channel->SetMethodCallHandler(
[plugin_pointer = plugin.get()](const auto &call, auto result) {
plugin_pointer->HandleMethodCall(call, std::move(result));
});
registrar->AddPlugin(std::move(plugin));
}
TextureRgbaRendererPlugin::TextureRgbaRendererPlugin() {
this->texture_registrar = nullptr;
}
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,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (method_call.method_name().compare("createTexture") == 0) {
auto args = std::get<flutter::EncodableMap>(*method_call.arguments());
auto key = std::get<int>(args.at(flutter::EncodableValue("key")));
auto [it, add] = textures_.try_emplace(key, nullptr);
if (add) {
// Create a texture.
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")));
auto it = textures_.find(key);
if (it == textures_.end()) {
result->Success(flutter::EncodableValue(false));
}
else {
// "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));
}
}
else if (method_call.method_name().compare("onRgba") == 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()) {
result->Success(flutter::EncodableValue(false));
}
else {
auto& data = std::get<std::vector<uint8_t>>(args.at(flutter::EncodableValue("data")));
auto height = std::get<int>(args.at(flutter::EncodableValue("height")));
auto width = std::get<int>(args.at(flutter::EncodableValue("width")));
textures_[key]->MarkVideoFrameAvailable(data, width, height);
result->Success(flutter::EncodableValue(true));
}
}
else if (method_call.method_name().compare("getTexturePtr") == 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()) {
// Not open (e.g. closed meanwhile): no pointer for the session.
result->Success(flutter::EncodableValue(0));
}
else {
// Return an address.
size_t rgba = reinterpret_cast<size_t>((void*) textures_[key].get());
result->Success(flutter::EncodableValue(int64_t(rgba)));
}
}
else {
result->NotImplemented();
}
}
} // namespace texture_rgba_renderer