chore: rearrange thermion_flutter_ffi src dir

This commit is contained in:
Nick Fisher
2024-10-23 02:22:04 +11:00
parent f080720712
commit 707793c6b2
6 changed files with 8 additions and 8 deletions

View File

@@ -0,0 +1,122 @@
import 'package:logging/logging.dart';
import 'package:thermion_dart/thermion_dart.dart';
import 'thermion_flutter_method_channel_interface.dart';
class FlutterPlatformTexture extends MethodChannelFlutterTexture {
final _logger = Logger("ThermionFlutterTexture");
final ThermionViewer viewer;
final View view;
int flutterId = -1;
int _lastFlutterId = -1;
int _lastHardwareId = -1;
int hardwareId = -1;
int width = -1;
int height = -1;
SwapChain? swapChain;
RenderTarget? _renderTarget;
late bool destroySwapChainOnResize;
bool destroyed = false;
FlutterPlatformTexture(
super.channel, this.viewer, this.view, this.swapChain) {
if (swapChain == null) {
destroySwapChainOnResize = true;
} else {
destroySwapChainOnResize = false;
}
}
@override
Future<void> resize(
int newWidth, int newHeight, int newLeft, int newTop) async {
_logger.info(
"Resizing texture to $newWidth x $newHeight (offset $newLeft, $newTop)");
if (newWidth == this.width &&
newHeight == this.height &&
newLeft == 0 &&
newTop == 0) {
_logger.info("Existing texture matches requested dimensions");
return;
}
this.width = newWidth;
this.height = newHeight;
var result =
await channel.invokeMethod("createTexture", [width, height, 0, 0]);
if (result == null || (result[0] == -1)) {
throw Exception("Failed to create texture");
}
_lastFlutterId = flutterId;
_lastHardwareId = hardwareId;
flutterId = result[0] as int;
hardwareId = result[1] as int;
_logger.info("Created texture ${flutterId} / ${hardwareId}");
if (destroySwapChainOnResize) {
if (swapChain != null) {
await viewer.destroySwapChain(swapChain!);
}
swapChain = await viewer.createSwapChain(result[2]);
await view.setRenderable(true, swapChain!);
} else if (hardwareId != _lastHardwareId) {
if (_renderTarget != null) {
await viewer.destroyRenderTarget(_renderTarget!);
}
_renderTarget =
await viewer.createRenderTarget(width, height, hardwareId);
await view.setRenderTarget(_renderTarget!);
await view.setRenderable(true, swapChain!);
if (_lastFlutterId != -1 && _lastHardwareId != -1) {
await _destroyTexture(_lastFlutterId, _lastHardwareId);
_lastFlutterId = -1;
_lastHardwareId = -1;
}
}
}
Future<void> _destroyTexture(
int flutterTextureId, int hardwareTextureId) async {
try {
await channel.invokeMethod(
"destroyTexture", [flutterTextureId, hardwareTextureId]);
_logger.info("Destroyed texture $flutterTextureId / $hardwareTextureId");
} catch (e) {
_logger.severe("Failed to destroy texture: $e");
}
}
bool destroying = false;
Future destroy() async {
if (destroyed || destroying) {
return;
}
destroying = true;
await view.setRenderTarget(null);
if (_renderTarget != null) {
await viewer.destroyRenderTarget(_renderTarget!);
_renderTarget = null;
}
if (destroySwapChainOnResize && swapChain != null) {
await viewer.destroySwapChain(swapChain!);
swapChain = null;
}
await _destroyTexture(flutterId, hardwareId);
flutterId = -1;
hardwareId = -1;
destroying = false;
destroyed = true;
}
Future markFrameAvailable() async {
await channel.invokeMethod("markTextureFrameAvailable", this.flutterId);
}
}

View File

@@ -0,0 +1,11 @@
class TextureCacheEntry {
final int flutterId;
final int hardwareId;
final DateTime creationTime;
DateTime? removalTime;
bool inUse;
TextureCacheEntry(this.flutterId, this.hardwareId,
{this.removalTime, this.inUse = true})
: creationTime = DateTime.now();
}

View File

@@ -0,0 +1,77 @@
import 'dart:async';
import 'package:flutter/services.dart';
import 'dart:ffi';
import 'package:thermion_dart/thermion_dart.dart';
import 'package:thermion_dart/src/viewer/src/ffi/thermion_viewer_ffi.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart';
import 'package:logging/logging.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart';
///
/// An abstract implementation of [ThermionFlutterPlatform] that uses
/// Flutter platform channels to create a rendering context,
/// resource loaders, and surface/render target(s).
///
abstract class ThermionFlutterMethodChannelInterface
extends ThermionFlutterPlatform {
final channel = const MethodChannel("dev.thermion.flutter/event");
final _logger = Logger("ThermionFlutterMethodChannelInterface");
ThermionViewerFFI? viewer;
Future<ThermionViewer> createViewer({ThermionFlutterOptions? options}) async {
if (viewer != null) {
throw Exception(
"Only one ThermionViewer can be created at any given time; ensure you have called [dispose] on the previous instance before constructing a new instance.");
}
var resourceLoader = Pointer<Void>.fromAddress(
await channel.invokeMethod("getResourceLoaderWrapper"));
if (resourceLoader == nullptr) {
throw Exception("Failed to get resource loader");
}
var driverPlatform = await channel.invokeMethod("getDriverPlatform");
var driverPtr = driverPlatform == null
? nullptr
: Pointer<Void>.fromAddress(driverPlatform);
var sharedContext = await channel.invokeMethod("getSharedContext");
var sharedContextPtr = sharedContext == null
? nullptr
: Pointer<Void>.fromAddress(sharedContext);
viewer = ThermionViewerFFI(
resourceLoader: resourceLoader,
driver: driverPtr,
sharedContext: sharedContextPtr,
uberArchivePath: options?.uberarchivePath);
await viewer!.initialized;
viewer!.onDispose(() async {
this.viewer = null;
});
return viewer!;
}
}
abstract class MethodChannelFlutterTexture extends ThermionFlutterTexture {
final MethodChannel channel;
MethodChannelFlutterTexture(this.channel);
@override
int get flutterId;
@override
int get hardwareId;
@override
int get height;
@override
int get width;
}

View File

@@ -0,0 +1,71 @@
import 'dart:async';
import 'dart:io';
import 'package:thermion_dart/thermion_dart.dart';
import 'package:thermion_dart/thermion_dart.dart' as t;
import 'package:thermion_flutter_ffi/src/thermion_flutter_method_channel_interface.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart';
import 'package:logging/logging.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_window.dart';
import 'platform_texture.dart';
///
/// An implementation of [ThermionFlutterPlatform] that uses
/// Flutter platform channels to create a rendering context,
/// resource loaders, and a texture that will be used as a render target
/// for a headless swapchain.
///
class ThermionFlutterTextureBackedPlatform
extends ThermionFlutterMethodChannelInterface {
final _logger = Logger("ThermionFlutterTextureBackedPlatform");
static SwapChain? _swapChain;
ThermionFlutterTextureBackedPlatform._();
static ThermionFlutterTextureBackedPlatform? instance;
static void registerWith() {
instance ??= ThermionFlutterTextureBackedPlatform._();
ThermionFlutterPlatform.instance = instance!;
}
@override
Future<ThermionViewer> createViewer({ThermionFlutterOptions? options}) async {
var viewer = await super.createViewer(options: options);
if (_swapChain != null) {
throw Exception("Only a single swapchain can be created");
}
// this implementation renders directly into a texture/render target
// we still need to create a (headless) swapchain, but the actual dimensions
// don't matter
if (Platform.isMacOS || Platform.isIOS) {
_swapChain = await viewer.createHeadlessSwapChain(1, 1);
}
viewer.onDispose(() async {
_swapChain = null;
});
return viewer;
}
// On desktop platforms, textures are always created
Future<ThermionFlutterTexture?> createTexture(
t.View view, int width, int height) async {
var texture = FlutterPlatformTexture(channel, viewer!, view,
(Platform.isMacOS || Platform.isIOS) ? _swapChain : null);
await texture.resize(width, height, 0, 0);
return texture;
}
@override
Future<ThermionFlutterWindow> createWindow(
int width, int height, int offsetLeft, int offsetTop) {
// TODO: implement createWindow
throw UnimplementedError();
}
}

View File

@@ -0,0 +1,131 @@
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:thermion_dart/thermion_dart.dart';
import 'package:thermion_flutter_ffi/src/thermion_flutter_method_channel_interface.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart';
import 'package:logging/logging.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_window.dart';
///
/// A Windows-only implementation of [ThermionFlutterPlatform] that uses
/// a Flutter platform channel to create a rendering context,
/// resource loader and a native HWND that will be sit behind the running
/// Flutter application.
///
class ThermionFlutterWindows
extends ThermionFlutterMethodChannelInterface {
final _channel = const MethodChannel("dev.thermion.flutter/event");
final _logger = Logger("ThermionFlutterWindows");
ThermionViewer? _viewer;
SwapChain? _swapChain;
ThermionFlutterWindows._() {}
static void registerWith() {
ThermionFlutterPlatform.instance = ThermionFlutterWindows._();
}
@override
Future<ThermionViewer> createViewer({ThermionFlutterOptions? options}) async {
if(_viewer != null) {
throw Exception("Only one viewer should be instantiated over the life of the app");
}
_viewer = await super.createViewer(options: options);
_viewer!.onDispose(() async {
_viewer = null;
});
return _viewer!;
}
///
/// Not supported on Windows. Throws an exception.
///
@override
Future<ThermionFlutterTexture?> createTexture(View view, int width, int height) {
throw UnimplementedError();
}
@override
Future<ThermionFlutterWindow> createWindow(int width, int height, int offsetLeft, int offsetTop) async {
var result = await _channel
.invokeMethod("createWindow", [width, height, offsetLeft, offsetLeft]);
if (result == null || result[2] == -1) {
throw Exception("Failed to create window");
}
var window =
ThermionFlutterWindowImpl(result[2], _channel, viewer!);
await window.resize(width, height, offsetLeft, offsetTop);
var view = await _viewer!.getViewAt(0);
await view.updateViewport(width, height);
_swapChain = await _viewer!.createSwapChain(window.handle);
await view.setRenderable(true, _swapChain!);
return window;
}
}
class ThermionFlutterWindowImpl extends ThermionFlutterWindow {
final ThermionViewer viewer;
final int handle;
int height = 0;
int width = 0;
int offsetLeft = 0;
int offsetTop = 0;
final MethodChannel _channel;
ThermionFlutterWindowImpl(this.handle, this._channel, this.viewer);
@override
Future destroy() async {
await _channel
.invokeMethod("destroyWindow", this.handle);
}
@override
Future markFrameAvailable() {
// TODO: implement markFrameAvailable
throw UnimplementedError();
}
bool _resizing = false;
///
/// Called by [ThermionWidget] to resize the window. Don't call this yourself.
///
@override
Future resize(
int width, int height, int offsetLeft, int offsetTop) async {
if (_resizing) {
throw Exception("Resize underway");
}
if (width == this.width && height == this.height && this.offsetLeft == offsetLeft && this.offsetTop == offsetTop) {
return;
}
this.width = width;
this.height = height;
this.offsetLeft = offsetLeft;
this.offsetTop = offsetTop;
_resizing = true;
await _channel
.invokeMethod("resizeWindow", [width, height, offsetLeft, offsetTop]);
_resizing = false;
}
}