split D3D/GLES texture creation

This commit is contained in:
Nick Fisher
2024-11-04 17:17:24 +08:00
parent f3e96fe94a
commit bdcbd90ec6
26 changed files with 897 additions and 646 deletions

View File

@@ -1,122 +1,123 @@
import 'package:logging/logging.dart';
import 'package:thermion_dart/thermion_dart.dart';
import 'thermion_flutter_method_channel_interface.dart';
// 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");
// class FlutterPlatformTexture extends ThermionFlutterTexture {
// final _logger = Logger("ThermionFlutterTexture");
final ThermionViewer viewer;
final View view;
// final ThermionViewer viewer;
// final View view;
int flutterId = -1;
int _lastFlutterId = -1;
int _lastHardwareId = -1;
int hardwareId = -1;
int width = -1;
int height = -1;
// int flutterId = -1;
// int _lastFlutterId = -1;
// int _lastHardwareId = -1;
// int hardwareId = -1;
// int width = -1;
// int height = -1;
SwapChain? swapChain;
// SwapChain? swapChain;
RenderTarget? _renderTarget;
// RenderTarget? _renderTarget;
late bool destroySwapChainOnResize;
// late bool destroySwapChainOnResize;
bool destroyed = false;
// bool destroyed = false;
FlutterPlatformTexture(
super.channel, this.viewer, this.view, this.swapChain) {
if (swapChain == null) {
destroySwapChainOnResize = true;
} else {
destroySwapChainOnResize = 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;
}
// @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;
// 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;
// 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;
// var window = result[2] as int; // usually 0 for nullptr
_logger.info("Created texture ${flutterId} / ${hardwareId}");
// _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;
}
}
}
// if (destroySwapChainOnResize) {
// if (swapChain != null) {
// await viewer.destroySwapChain(swapChain!);
// }
// swapChain = await viewer.createSwapChain(window);
// 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");
}
}
// 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;
}
// 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;
}
// 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);
}
}
// Future markFrameAvailable() async {
// await channel.invokeMethod("markTextureFrameAvailable", this.flutterId);
// }
// }

View File

@@ -1,77 +0,0 @@
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,132 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';
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_platform.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';
///
/// An abstract implementation of [ThermionFlutterPlatform] that uses
/// Flutter platform channels to create a rendering context,
/// resource loaders, and surface/render target(s).
///
class ThermionFlutterMethodChannelPlatform
extends ThermionFlutterPlatform {
final channel = const MethodChannel("dev.thermion.flutter/event");
final _logger = Logger("ThermionFlutterMethodChannelPlatform");
static SwapChain? _swapChain;
ThermionFlutterMethodChannelPlatform._();
static ThermionFlutterMethodChannelPlatform? instance;
static void registerWith() {
instance ??= ThermionFlutterMethodChannelPlatform._();
ThermionFlutterPlatform.instance = instance!;
}
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 {
_swapChain = null;
this.viewer = null;
});
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 || Platform.isWindows) {
_swapChain = await viewer!.createHeadlessSwapChain(1, 1);
}
return viewer!;
}
Future<ThermionFlutterTexture?> createTexture(int width, int height) async {
var result =
await channel.invokeMethod("createTexture", [width, height, 0, 0]);
if (result == null || (result[0] == -1)) {
throw Exception("Failed to create texture");
}
final flutterId = result[0] as int;
final hardwareId = result[1] as int;
var window = result[2] as int; // usually 0 for nullptr
return ThermionFlutterTexture(flutterId:flutterId, hardwareId:hardwareId, height:height, width:width, window:window);
}
@override
Future bind(view, ThermionFlutterTexture texture) {
// TODO: implement bind
throw UnimplementedError();
}
@override
Future<ThermionFlutterWindow> createWindow(int width, int height, int offsetLeft, int offsetTop) {
// TODO: implement createWindow
throw UnimplementedError();
}
@override
Future destroyTexture(ThermionFlutterTexture texture) {
// TODO: implement destroyTexture
throw UnimplementedError();
}
@override
Future markTextureFrameAvailable(ThermionFlutterTexture texture) async {
await channel.invokeMethod("markTextureFrameAvailable", texture.flutterId);
}
@override
Future<ThermionFlutterTexture> resizeTexture(ThermionFlutterTexture texture, int width, int height) async {
return texture;
}
}

View File

@@ -1,71 +0,0 @@
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 || Platform.isWindows) {
_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 || Platform.isWindows) ? _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

@@ -1,135 +1,135 @@
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';
// 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';
import 'platform_texture.dart';
// import 'platform_texture.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 {
// ///
// /// 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 ThermionFlutterMethodChannelPlatform {
final _channel = const MethodChannel("dev.thermion.flutter/event");
// final _channel = const MethodChannel("dev.thermion.flutter/event");
final _logger = Logger("ThermionFlutterWindows");
// final _logger = Logger("ThermionFlutterWindows");
ThermionViewer? _viewer;
// ThermionViewer? _viewer;
SwapChain? _swapChain;
// SwapChain? _swapChain;
ThermionFlutterWindows._() {}
// ThermionFlutterWindows._() {}
static void registerWith() {
ThermionFlutterPlatform.instance = 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!;
}
// @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) async {
var texture = FlutterPlatformTexture(channel, viewer!, view, null);
await texture.resize(width, height, 0, 0);
return texture;
}
// ///
// /// Not supported on Windows. Throws an exception.
// ///
// @override
// Future<ThermionFlutterTexture?> createTexture(View view, int width, int height) async {
// var texture = FlutterPlatformTexture(channel, viewer!, view, null);
// await texture.resize(width, height, 0, 0);
// return texture;
// }
@override
Future<ThermionFlutterWindow> createWindow(int width, int height, int offsetLeft, int offsetTop) async {
// @override
// Future<ThermionFlutterWindow> createWindow(int width, int height, int offsetLeft, int offsetTop) async {
var result = await _channel
.invokeMethod("createWindow", [width, height, offsetLeft, offsetLeft]);
// var result = await _channel
// .invokeMethod("createWindow", [width, height, offsetLeft, offsetLeft]);
if (result == null || result[2] == -1) {
throw Exception("Failed to create window");
}
// 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);
// 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;
}
// await view.updateViewport(width, height);
// _swapChain = await _viewer!.createSwapChain(window.handle);
// await view.setRenderable(true, _swapChain!);
// return window;
// }
}
// }
class ThermionFlutterWindowImpl extends ThermionFlutterWindow {
// 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;
// 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);
// ThermionFlutterWindowImpl(this.handle, this._channel, this.viewer);
@override
Future destroy() async {
await _channel
.invokeMethod("destroyWindow", this.handle);
}
// @override
// Future destroy() async {
// await _channel
// .invokeMethod("destroyWindow", this.handle);
// }
@override
Future markFrameAvailable() {
// TODO: implement markFrameAvailable
throw UnimplementedError();
}
// @override
// Future markFrameAvailable() {
// // TODO: implement markFrameAvailable
// throw UnimplementedError();
// }
bool _resizing = false;
// 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");
}
// ///
// /// 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;
}
// 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;
// this.width = width;
// this.height = height;
// this.offsetLeft = offsetLeft;
// this.offsetTop = offsetTop;
_resizing = true;
// _resizing = true;
await _channel
.invokeMethod("resizeWindow", [width, height, offsetLeft, offsetTop]);
_resizing = false;
}
// await _channel
// .invokeMethod("resizeWindow", [width, height, offsetLeft, offsetTop]);
// _resizing = false;
// }
}
// }