From 99484d68384549455faa309663f08dfc5fb400c9 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Fri, 29 Sep 2023 18:40:29 +0800 Subject: [PATCH] remove superseded cruft from MacOS plugin --- lib/filament_controller.dart | 2 +- lib/filament_controller_ffi.dart | 11 ++- lib/filament_controller_method_channel.dart | 7 +- .../Classes/SwiftPolyvoxFilamentPlugin.swift | 80 +++---------------- 4 files changed, 21 insertions(+), 79 deletions(-) diff --git a/lib/filament_controller.dart b/lib/filament_controller.dart index a359b006..9cfebe5d 100644 --- a/lib/filament_controller.dart +++ b/lib/filament_controller.dart @@ -34,7 +34,7 @@ abstract class FilamentController { /// If you need to wait until a FilamentViewer has been created, [await] the [isReadyForScene] Future. /// Future createViewer(int width, int height); - Future resize(int width, int height, {double contentScaleFactor = 1.0}); + Future resize(int width, int height, {double scaleFactor = 1.0}); Future clearBackgroundImage(); Future setBackgroundImage(String path, {bool fillHeight = false}); diff --git a/lib/filament_controller_ffi.dart b/lib/filament_controller_ffi.dart index 387cb9ca..b08ed82e 100644 --- a/lib/filament_controller_ffi.dart +++ b/lib/filament_controller_ffi.dart @@ -145,13 +145,16 @@ class FilamentControllerFFI extends FilamentController { _isReadyForScene.complete(true); } - Future resize(int width, int height, - {double contentScaleFactor = 1.0}) async { + Future resize(int width, int height, {double scaleFactor = 1.0}) async { _resizing = true; - _textureId = await _channel.invokeMethod("resize", - [width * _pixelRatio, height * _pixelRatio, contentScaleFactor]); + setRendering(false); + _textureId = await _channel.invokeMethod( + "resize", [width * _pixelRatio, height * _pixelRatio, scaleFactor]); _textureIdController.add(_textureId); + _lib.update_viewport_and_camera_projection_ffi( + _viewer!, width, height, scaleFactor); _resizing = false; + setRendering(true); } Future clearBackgroundImage() async { diff --git a/lib/filament_controller_method_channel.dart b/lib/filament_controller_method_channel.dart index 094c9c1a..4c41e543 100644 --- a/lib/filament_controller_method_channel.dart +++ b/lib/filament_controller_method_channel.dart @@ -109,11 +109,10 @@ class FilamentControllerMethodChannel extends FilamentController { bool _resizing = false; - Future resize(int width, int height, - {double contentScaleFactor = 1.0}) async { + Future resize(int width, int height, {double scaleFactor = 1.0}) async { _resizing = true; - _textureId = await _channel.invokeMethod("resize", - [width * _pixelRatio, height * _pixelRatio, contentScaleFactor]); + _textureId = await _channel.invokeMethod( + "resize", [width * _pixelRatio, height * _pixelRatio, scaleFactor]); _textureIdController.add(_textureId); _resizing = false; } diff --git a/macos/Classes/SwiftPolyvoxFilamentPlugin.swift b/macos/Classes/SwiftPolyvoxFilamentPlugin.swift index 436879e2..c1843121 100644 --- a/macos/Classes/SwiftPolyvoxFilamentPlugin.swift +++ b/macos/Classes/SwiftPolyvoxFilamentPlugin.swift @@ -18,13 +18,7 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture ] as CFDictionary var resources:[UInt32:NSData] = [:] - - var viewer:UnsafeRawPointer? = nil - var displayLink:CVDisplayLink? = nil - var rendering:Bool = false - - var frameInterval:Double = 1 / 60.0 - + static var messenger : FlutterBinaryMessenger? = nil; var loadResource : @convention(c) (UnsafePointer?, UnsafeMutableRawPointer?) -> ResourceBuffer = { uri, resourcesPtr in @@ -74,35 +68,6 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture instance.registry.textureFrameAvailable(instance.flutterTextureId!) } - var displayLinkRenderCallback : @convention(c) (CVDisplayLink, UnsafePointer, UnsafePointer, CVOptionFlags, UnsafeMutablePointer, UnsafeMutableRawPointer?) -> CVReturn = { displayLink, ts1, ts2, options, optionsPtr, resourcesPtr in - let instance:SwiftPolyvoxFilamentPlugin = Unmanaged.fromOpaque(resourcesPtr!).takeUnretainedValue() - - if(instance.viewer != nil && instance.rendering) { - instance.doRender() - } - return 0 - } - - func doRender() { - DispatchQueue.main.async { - render(self.viewer, 0) - self.registry.textureFrameAvailable(self.flutterTextureId!) - } - } - - func createDisplayLink() { - let displayID = CGMainDisplayID() - let error = CVDisplayLinkCreateWithCGDisplay(displayID, &displayLink); - if (error != 0) - { - print("DisplayLink created with error \(error)"); - } - CVDisplayLinkSetOutputCallback(displayLink!, displayLinkRenderCallback, unsafeBitCast(self, to:UnsafeMutableRawPointer.self)) - - CVDisplayLinkStart(displayLink!); - - } - public func copyPixelBuffer() -> Unmanaged? { if(pixelBuffer == nil) { return nil; @@ -136,12 +101,6 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture self.flutterTextureId = self.registry.register(self) } - private func resize(width:Int32, height:Int32) { - if(self.flutterTextureId != nil) { - self.registry.unregisterTexture(self.flutterTextureId!) - } - createPixelBuffer(width: Int(width), height:Int(height)) - } var cvMetalTextureCache:CVMetalTextureCache? = nil var cvMetalTexture:CVMetalTexture? = nil @@ -185,7 +144,6 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture 0, &cvMetalTexture); metalTexture = CVMetalTextureGetTexture(cvMetalTexture!); - // createDisplayLink() let pixelBufferPtr = CVPixelBufferGetBaseAddress(pixelBuffer!); let pixelBufferAddress = Int(bitPattern:pixelBufferPtr); let metalTexturePtr = Unmanaged.passUnretained(metalTexture!).toOpaque() @@ -195,39 +153,21 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture result([self.flutterTextureId as Any, nil, metalTextureAddress]) case "destroyTexture": - if(viewer != nil) { - result(FlutterError(code: "INVALID_ARGUMENTS", message: "Destroy the viewer before destroying the texture", details: nil)) - } else { - - if(self.flutterTextureId != nil) { - self.registry.unregisterTexture(self.flutterTextureId!) - } - self.flutterTextureId = nil - self.pixelBuffer = nil + if(self.flutterTextureId != nil) { + self.registry.unregisterTexture(self.flutterTextureId!) } - case "destroyViewer": - if(viewer != nil) { - destroy_swap_chain(viewer) - destroy_filament_viewer(viewer) - viewer = nil - } - result(true) + self.flutterTextureId = nil + self.pixelBuffer = nil + self.metalTexture = nil case "resize": - if(viewer == nil) { - print("Error: cannot resize before a viewer has been created") - result(nil); - } - rendering = false - destroy_swap_chain(viewer) let args = call.arguments as! [Any] let width = UInt32(args[0] as! Int64) let height = UInt32(args[1] as! Int64) - resize(width:Int32(width), height:Int32(height)) - create_swap_chain(viewer, CVPixelBufferGetBaseAddress(pixelBuffer!), width, height) + if(self.flutterTextureId != nil) { + self.registry.unregisterTexture(self.flutterTextureId!) + } + createPixelBuffer(width: Int(width), height:Int(height)) let metalTextureId = Int(bitPattern:Unmanaged.passUnretained(metalTexture!).toOpaque()) - create_render_target(viewer, metalTextureId, width, height); - update_viewport_and_camera_projection(viewer, width, height, Float(args[2] as! Double)) - rendering = true print("Resized to \(args[0])x\(args[1])") result(self.flutterTextureId); default: