linux: recycle closed textures per engine, closeTexture(recycle:) (Kitedesk B212)

Each session created a new TextureRgba and closeTexture never released it, so
the engine's FlPixelBufferTexture kept its GL texture (one frame of GPU or,
with llvmpipe, process memory per session). Releasing the object is not safe:
FlPixelBufferTexture deletes its GL texture in dispose without selecting a GL
context, so a release from another window deletes that window's texture of
the same name (upstream PR #2 turned the remaining window white).

closeTexture(key, recycle: true) now keeps the texture in a list of the
plugin instance, i.e. of the engine (window) that created it, and the next
createTexture of that engine registers it again. Its GL texture is only ever
touched by that engine's populate in its own context, nothing is released,
and the GL memory is bounded by the number of textures a window had open at
once. When the engine goes away the recycled textures stay referenced for the
same reason. Without recycle (old callers, or a tab that moves to another
window while its session still holds the pointer) a closed texture stays
terminated and is never handed out again.

With no frame waiting, copy_pixels hands out one transparent pixel instead
of no buffer, so a recycled texture cannot show the last frame of its previous
session before the first new frame.

Dart: closeTexture(int key, {bool recycle = false}) sends "recycle"; other
platforms ignore it. Tests: all ten Linux lifecycle tests pass (clang and
gcc, -Wall -Werror, also under ASan/UBSan); Dart 4/4.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
yunchan 2026-10-11 15:38:06 +09:00
parent 48fa1d7ddf
commit 8cd34a80ff
7 changed files with 106 additions and 12 deletions

View file

@ -1,3 +1,4 @@
// Modified by the Kitedesk fork (2026, B212): closeTexture takes `recycle`.
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:texture_rgba_renderer/texture_rgba_renderer_method_channel.dart';
@ -22,4 +23,20 @@ void main() {
test('getPlatformVersion', () async {
expect(await platform.getPlatformVersion(), '42');
});
// Kitedesk fork (B212): closeTexture tells the Linux side whether the
// texture may be recycled.
test('closeTexture sends recycle', () async {
final calls = <MethodCall>[];
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
calls.add(methodCall);
return true;
});
expect(await platform.closeTexture(7), true);
expect(await platform.closeTexture(8, recycle: true), true);
expect(calls.map((c) => c.method), ['closeTexture', 'closeTexture']);
expect(calls[0].arguments, {'key': 7, 'recycle': false});
expect(calls[1].arguments, {'key': 8, 'recycle': true});
});
}

View file

@ -1,6 +1,8 @@
// Modified by the Kitedesk fork (2026, B212): closeTexture takes `recycle`.
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:texture_rgba_renderer/texture_rgba_renderer.dart';
import 'package:texture_rgba_renderer/texture_rgba_renderer_platform_interface.dart';
import 'package:texture_rgba_renderer/texture_rgba_renderer_method_channel.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
@ -11,8 +13,13 @@ class MockTextureRgbaRendererPlatform
@override
Future<String?> getPlatformVersion() => Future.value('42');
final closed = <List<Object>>[];
@override
Future<bool> closeTexture(int key) => Future.value(true);
Future<bool> closeTexture(int key, {bool recycle = false}) {
closed.add([key, recycle]);
return Future.value(true);
}
@override
Future<int> createTexture(int key) => Future.value(1);
@ -35,4 +42,18 @@ void main() {
test('$MethodChannelTextureRgbaRenderer is the default instance', () {
expect(initialPlatform, isInstanceOf<MethodChannelTextureRgbaRenderer>());
});
// Kitedesk fork (B212).
test('closeTexture forwards recycle', () async {
final fake = MockTextureRgbaRendererPlatform();
TextureRgbaRendererPlatform.instance = fake;
final renderer = TextureRgbaRenderer();
expect(await renderer.closeTexture(9), true);
expect(await renderer.closeTexture(10, recycle: true), true);
expect(fake.closed, [
[9, false],
[10, true],
]);
TextureRgbaRendererPlatform.instance = initialPlatform;
});
}