fix Windows build.dart to avoid native_assets fork; add implementations for ThermionFlutterWindows

This commit is contained in:
Nick Fisher
2024-10-12 02:14:37 +11:00
parent 6d1e56caac
commit e2d11014d0
23 changed files with 524 additions and 215 deletions

View File

@@ -95,7 +95,7 @@ class _ThermionWidgetState extends State<ThermionWidget> {
}
if (Platform.isWindows) {
return ThermionWidgetWindows(viewer: widget.viewer);
return ThermionWidgetWindows(viewer: widget.viewer, view: view!, initial: widget.initial, onResize: widget.onResize);
}
return ThermionTextureWidget(

View File

@@ -1,14 +1,158 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:thermion_flutter/thermion_flutter.dart';
import 'package:thermion_flutter/src/widgets/src/resize_observer.dart';
import 'package:thermion_flutter/src/widgets/src/transparent_filament_widget.dart';
import 'package:thermion_flutter/thermion_flutter.dart' as t;
import 'package:thermion_flutter_platform_interface/thermion_flutter_window.dart';
class ThermionWidgetWindows extends StatelessWidget {
class ThermionWidgetWindows extends StatefulWidget {
final ThermionViewer viewer;
final t.ThermionViewer viewer;
final t.View view;
///
///
///
final Widget? initial;
///
/// A callback that will be invoked whenever this widget (and the underlying texture is resized).
///
final Future Function(Size size, t.View view, double pixelRatio)? onResize;
const ThermionWidgetWindows({super.key, required this.viewer, this.initial, this.onResize, required this.view});
@override
State<StatefulWidget> createState() => _ThermionWidgetWindowsState();
}
class _ThermionWidgetWindowsState extends State<ThermionWidgetWindows> {
ThermionFlutterWindow? _window;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
await widget.viewer.initialized;
var dpr = MediaQuery.of(context).devicePixelRatio;
var size = ((context.findRenderObject()) as RenderBox).size;
var width = (size.width * dpr).ceil();
var height = (size.height * dpr).ceil();
_window = await t.ThermionFlutterPlatform.instance.createWindow(width, height, 0, 0);
await widget.view.updateViewport(_window!.width, _window!.height);
try {
await widget.onResize?.call(
Size(_window!.width.toDouble(), _window!.height.toDouble()),
widget.view,
dpr);
} catch (err, st) {
print(err);
print(st);
}
if (mounted) {
setState(() {});
}
_requestFrame();
widget.viewer.onDispose(() async {
var window = _window;
if (mounted) {
setState(() {});
}
await window?.destroy();
});
});
}
bool _rendering = false;
void _requestFrame() {
WidgetsBinding.instance.scheduleFrameCallback((d) async {
if (widget.viewer.rendering && !_rendering) {
_rendering = true;
await widget.viewer.requestFrame();
_rendering = false;
}
_requestFrame();
});
}
final _resizing = <Future>[];
Timer? _resizeTimer;
Future _resize(Size oldSize, Size newSize) async {
await Future.wait(_resizing);
_resizeTimer?.cancel();
_resizeTimer = Timer(const Duration(milliseconds: 100), () async {
await Future.wait(_resizing);
if (!mounted) {
return;
}
if (newSize.width == _window?.width &&
newSize.height == _window?.height) {
return;
}
final completer = Completer();
_resizing.add(completer.future);
final dpr = MediaQuery.of(context).devicePixelRatio;
newSize *= dpr;
var newWidth = newSize.width.ceil();
var newHeight = newSize.height.ceil();
await _window?.resize(
newWidth,
newHeight,
0,
0,
);
await widget.view.updateViewport(_window!.width, _window!.height);
await widget.onResize?.call(
Size(_window!.width.toDouble(), _window!.height.toDouble()),
widget.view,
dpr);
if (!mounted) {
return;
}
setState(() {});
completer.complete();
_resizing.remove(completer.future);
});
}
const ThermionWidgetWindows({super.key, required this.viewer});
@override
Widget build(BuildContext context) {
// TODO: implement build
throw UnimplementedError();
if (_window == null) {
return widget.initial ?? Container(color: Colors.red);
}
return ResizeObserver(
onResized: _resize,
child: CustomPaint(painter:TransparencyPainter()));
}
}

View File

@@ -23,7 +23,13 @@ dependencies:
thermion_flutter_web: ^0.1.0+7
logging: ^1.2.0
web: ^1.0.0
dependency_overrides:
thermion_dart:
path: ../../thermion_dart
thermion_flutter_platform_interface:
path: ../thermion_flutter_platform_interface
thermion_flutter_ffi:
path: ../thermion_flutter_ffi
dev_dependencies:
flutter_test:
sdk: flutter

View File

@@ -152,8 +152,20 @@ LRESULT CALLBACK FilamentWindowProc(HWND const window, UINT const message,
break;
}
case WM_ERASEBKGND: {
// Prevent erasing of |window| when it is unfocused and minimized or
// moved out of screen etc.
HDC hdc = (HDC)wparam;
RECT rect;
GetClientRect(window, &rect);
// Get the BackingWindow instance associated with this window
BackingWindow* backing_window = reinterpret_cast<BackingWindow*>(
GetWindowLongPtr(window, GWLP_USERDATA));
if (backing_window) {
HBRUSH brush = CreateSolidBrush(RGB(0, 255, 0));
FillRect(hdc, &rect, brush);
DeleteObject(brush);
}
break;
}
case WM_SIZE:
@@ -346,8 +358,6 @@ void BackingWindow::Resize(int width, int height, int left, int top) {
_top = top;
RECT flutterViewRect;
::GetWindowRect(_flutterViewWindow, &flutterViewRect);
std::cout << "Resizing to " << _width << " x " << _height << " with LT" << _left << " " << _top << " flutter view rect" << flutterViewRect.left << " " << flutterViewRect.top << " " << flutterViewRect.right << " " << flutterViewRect.bottom << std::endl;
::SetWindowPos(_windowHandle, _flutterRootWindow, flutterViewRect.left + _left,
flutterViewRect.top + _top, _width, _height,
SWP_NOACTIVATE);

View File

@@ -172,8 +172,6 @@ void ThermionFlutterPlugin::CreateTexture(
auto height = (uint32_t)round(dHeight );
auto left = (uint32_t)round(dLeft );
auto top = (uint32_t)round(dTop );
std::cout << "Using " << width << "x" << height << std::endl;
// create a single shared context for the life of the application
// this will be used to create a backing texture and passed to Filament
@@ -182,8 +180,10 @@ void ThermionFlutterPlugin::CreateTexture(
_context = std::make_unique<FlutterEGLContext>(_pluginRegistrar, _textureRegistrar);
#else
_context = std::make_unique<WGLContext>(_pluginRegistrar, _textureRegistrar);
std::cout << "Created WGL context" << std::endl;
#endif
}
_context->CreateRenderingSurface(width, height, std::move(result), left, top);
}
@@ -210,7 +210,7 @@ void ThermionFlutterPlugin::DestroyTexture(
void ThermionFlutterPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &methodCall,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
std::cout << methodCall.method_name().c_str() << std::endl;
if (methodCall.method_name() == "usesBackingWindow") {
result->Success(flutter::EncodableValue(
#ifdef WGL_USE_BACKING_WINDOW
@@ -243,14 +243,16 @@ void ThermionFlutterPlugin::HandleMethodCall(
auto height = (uint32_t)round(dHeight );
auto left = (uint32_t)round(dLeft );
auto top = (uint32_t)round(dTop );
_context->ResizeRenderingSurface(width, height, left, top);
std::cout << "resized window to " << width << "x" << height << " at " << left << "," << top << std::endl;
result->Success();
#else
result->Error("ERROR", "resizeWindow is only available when using a backing window");
#endif
} else if (methodCall.method_name() == "createTexture") {
} else if (methodCall.method_name() == "createWindow") {
CreateTexture(methodCall, std::move(result));
} else if (methodCall.method_name() == "destroyTexture") {
} else if (methodCall.method_name() == "destroyWindow") {
DestroyTexture(methodCall, std::move(result));
} else if (methodCall.method_name() == "getRenderCallback") {
flutter::EncodableList resultList;

View File

@@ -45,7 +45,7 @@ WGLContext::WGLContext(flutter::PluginRegistrarWindows *pluginRegistrar,
0,
0,
0,
16, // Number of bits for the depthbuffer
24, // Number of bits for the depthbuffer
0, // Number of bits for the stencilbuffer
0, // Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
@@ -117,6 +117,8 @@ void WGLContext::CreateRenderingSurface(
} else {
ResizeRenderingSurface(width, height, left, top);
}
std::cout << "created window size " << width << "x" << height << " at " << left << "," << top << " with backing handle" << _backingWindow->GetHandle() << std::endl;
std::vector<flutter::EncodableValue> resultList;
resultList.push_back(flutter::EncodableValue()); // return null for Flutter texture ID
resultList.push_back(flutter::EncodableValue()); // return null for hardware texture ID

View File

@@ -7,16 +7,19 @@ import 'package:thermion_flutter_ffi/thermion_flutter_method_channel_interface.d
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 surface/render target(s).
/// 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;
@@ -52,11 +55,10 @@ class ThermionFlutterTextureBackedPlatform
await texture.resize(width, height, 0, 0);
return texture;
}
// On MacOS, we currently use textures/render targets, so there's no window to resize
@override
Future<ThermionFlutterTexture?> resizeWindow(
int width, int height, int offsetTop, int offsetRight) {
Future<ThermionFlutterWindow> createWindow(int width, int height, int offsetLeft, int offsetTop) {
// TODO: implement createWindow
throw UnimplementedError();
}
}

View File

@@ -1,34 +1,45 @@
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_ffi/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';
///
/// An implementation of [ThermionFlutterPlatform] that uses
/// Flutter platform channels to create a rendering context,
/// resource loaders, and surface/render target(s).
/// 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");
ThermionViewerFFI? _viewer;
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);
return _viewer!;
}
///
/// Not supported on Windows. Throws an exception.
///
@@ -37,64 +48,85 @@ class ThermionFlutterWindows
throw UnimplementedError();
}
bool _resizing = false;
@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);
throw Exception();
// var view = await _viewer!.getViewAt(0);
// await view.updateViewport(width, height);
// print("Set viewport dimensions to ${width} ${height}");
// _swapChain = await _viewer!.createSwapChain(window.handle);
// 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", [width, height, offsetLeft, offsetLeft]);
}
@override
Future markFrameAvailable() {
// TODO: implement markFrameAvailable
throw UnimplementedError();
}
bool _resizing = false;
///
/// Called by [ThermionWidget] to resize a texture. Don't call this yourself.
/// Called by [ThermionWidget] to resize the window. Don't call this yourself.
///
@override
Future resizeWindow(
Future resize(
int width, int height, int offsetLeft, int offsetTop) async {
if (_resizing) {
throw Exception("Resize underway");
}
throw Exception("TODO");
if (width == this.width && height == this.height) {
return;
}
// final view = await this._viewer!.getViewAt(0);
// final viewport = await view.getViewport();
// final swapChain = await this._viewer.getSwapChainAt(0);
this.width = width;
this.height = height;
this.offsetLeft = offsetLeft;
this.offsetTop = offsetTop;
// if (width == viewport.width && height - viewport.height == 0) {
// return;
// }
_resizing = true;
// _resizing = true;
// bool wasRendering = _viewer!.rendering;
// await _viewer!.setRendering(false);
// await _swapChain?.destroy();
// var result = await _channel
// .invokeMethod("createTexture", [width, height, offsetLeft, offsetLeft]);
// if (result == null || result[0] == -1) {
// throw Exception("Failed to create texture");
// }
// var newTexture =
// ThermionFlutterTexture(result[0], result[1], width, height, result[2]);
// await _viewer!.createSwapChain(width, height,
// surface: newTexture.surfaceAddress == null
// ? nullptr
// : Pointer<Void>.fromAddress(newTexture.surfaceAddress!));
// if (newTexture.hardwareTextureId != null) {
// // ignore: unused_local_variable
// var renderTarget = await _viewer!
// .createRenderTarget(width, height, newTexture.hardwareTextureId!);
// }
// await _viewer!
// .updateViewportAndCameraProjection(width.toDouble(), height.toDouble());
// if (wasRendering) {
// await _viewer!.setRendering(true);
// }
// _textures.add(newTexture);
// _resizing = false;
// return newTexture;
await _channel
.invokeMethod("resizeWindow", [width, height, offsetLeft, offsetLeft]);
_resizing = false;
}
}
}

View File

@@ -26,7 +26,13 @@ dependencies:
thermion_dart: ^0.2.1-dev.0.0.6
logging: ^1.2.0
dependency_overrides:
thermion_dart:
path: ../../thermion_dart
thermion_flutter_platform_interface:
path: ../thermion_flutter_platform_interface
dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^5.0.0

View File

@@ -4,6 +4,7 @@ import 'package:thermion_dart/thermion_dart.dart' as t;
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:thermion_dart/thermion_dart.dart';
import 'thermion_flutter_texture.dart';
import 'thermion_flutter_window.dart';
class ThermionFlutterOptions {
final String? uberarchivePath;
@@ -41,7 +42,13 @@ abstract class ThermionFlutterPlatform extends PlatformInterface {
t.View view, int width, int height);
///
/// Create a rendering window.
///
/// This is internal; unless you are [thermion_*] package developer, don't
/// call this yourself. May not be supported on all platforms.
///
Future resizeWindow(int width, int height, int offsetTop, int offsetRight);
Future<ThermionFlutterWindow> createWindow(
int width, int height, int offsetLeft, int offsetTop);
}

View File

@@ -0,0 +1,16 @@
abstract class ThermionFlutterWindow {
int get width;
int get height;
int get handle;
///
/// Destroy a texture and clean up the texture cache (if applicable).
///
Future destroy();
Future resize(int width, int height, int left, int top);
Future markFrameAvailable();
}