// 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 // For getPlatformVersion; remove unless needed for your plugin implementation. #include #include #include #include #include #include #include #include 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> textures; }; Retired& retired() { static Retired* instance = new Retired(); return *instance; } } // namespace // static void TextureRgbaRendererPlugin::RegisterWithRegistrar( flutter::PluginRegistrarWindows *registrar) { auto channel = std::make_unique>( registrar->messenger(), "texture_rgba_renderer", &flutter::StandardMethodCodec::GetInstance()); auto plugin = std::make_unique(); 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 texture, bool recycle) { std::shared_ptr closed = std::move(texture); closed->Terminate(); if (!recycle) { std::lock_guard 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 &method_call, std::unique_ptr> result) { if (method_call.method_name().compare("createTexture") == 0) { auto args = std::get(*method_call.arguments()); auto key = std::get(args.at(flutter::EncodableValue("key"))); auto [it, add] = textures_.try_emplace(key, nullptr); if (add) { // Create a texture. it->second = std::make_unique(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(*method_call.arguments()); auto key = std::get(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(&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(*method_call.arguments()); auto key = std::get(args.at(flutter::EncodableValue("key"))); if (textures_.find(key) == textures_.end()) { result->Success(flutter::EncodableValue(false)); } else { auto& data = std::get>(args.at(flutter::EncodableValue("data"))); auto height = std::get(args.at(flutter::EncodableValue("height"))); auto width = std::get(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(*method_call.arguments()); auto key = std::get(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((void*) textures_[key].get()); result->Success(flutter::EncodableValue(int64_t(rgba))); } } else { result->NotImplemented(); } } } // namespace texture_rgba_renderer