This commit is contained in:
Nick Fisher
2024-04-30 15:48:50 +08:00
parent 14b0b674c5
commit 8267a0c5f8
6 changed files with 43 additions and 38 deletions

View File

@@ -1,5 +1,6 @@
library filament_dart; library filament_dart;
export 'dart_filament/entities/filament_entity.dart';
export 'dart_filament/abstract_filament_viewer.dart'; export 'dart_filament/abstract_filament_viewer.dart';
export 'dart_filament/filament_viewer_impl.dart'; export 'dart_filament/filament_viewer_impl.dart';
export 'dart_filament/dart_filament.g.dart'; export 'dart_filament/dart_filament.g.dart';

View File

@@ -1017,6 +1017,7 @@ namespace flutter_filament
else if (!_swapChain) else if (!_swapChain)
{ {
Log("No swapchain"); Log("No swapchain");
return;
} }
// if (_frameCount == 60) // if (_frameCount == 60)

View File

@@ -61,7 +61,8 @@ class FlutterFilamentPlugin extends FilamentViewer {
int width, int height, int offsetLeft, int offsetRight) async { int width, int height, int offsetLeft, int offsetRight) async {
var result = await _channel var result = await _channel
.invokeMethod("createTexture", [width, height, offsetLeft, offsetLeft]); .invokeMethod("createTexture", [width, height, offsetLeft, offsetLeft]);
if (result == null) {
if (result == null || result[0] == -1) {
throw Exception("Failed to create texture"); throw Exception("Failed to create texture");
} }
viewportDimensions = (width.toDouble(), height.toDouble()); viewportDimensions = (width.toDouble(), height.toDouble());
@@ -99,7 +100,7 @@ class FlutterFilamentPlugin extends FilamentViewer {
var newTexture = var newTexture =
await createTexture(width, height, offsetLeft, offsetRight); await createTexture(width, height, offsetLeft, offsetRight);
if (newTexture == null) { if (newTexture == null || newTexture.flutterTextureId == -1) {
throw Exception("Failed to create texture"); throw Exception("Failed to create texture");
} }
await createSwapChain(width.toDouble(), height.toDouble(), await createSwapChain(width.toDouble(), height.toDouble(),

View File

@@ -7,8 +7,7 @@ import GLKit
var pixelBufferAttrs = [ var pixelBufferAttrs = [
kCVPixelBufferPixelFormatTypeKey: NSNumber(value: kCVPixelFormatType_32ABGR ), kCVPixelBufferPixelFormatTypeKey: NSNumber(value: kCVPixelFormatType_32ABGR ),
kCVPixelBufferOpenGLCompatibilityKey: kCFBooleanFalse!, kCVPixelBufferIOSurfacePropertiesKey: [:] as CFDictionary
// kCVPixelBufferIOSurfacePropertiesKey: [Any:Any] as CFDictionary
] as [CFString : Any] as CFDictionary ] as [CFString : Any] as CFDictionary
@objc public var cvMetalTextureCache:CVMetalTextureCache? @objc public var cvMetalTextureCache:CVMetalTextureCache?
@@ -22,6 +21,7 @@ import GLKit
} }
@objc public init(width:Int64, height:Int64) { @objc public init(width:Int64, height:Int64) {
self.metalDevice = MTLCreateSystemDefaultDevice()! self.metalDevice = MTLCreateSystemDefaultDevice()!
// create pixel buffer // create pixel buffer
@@ -39,6 +39,7 @@ import GLKit
nil, nil,
&cvMetalTextureCache); &cvMetalTextureCache);
if(cvret != 0) { if(cvret != 0) {
print("Error creating Metal texture cache")
metalTextureAddress = -1 metalTextureAddress = -1
return return
} }
@@ -51,6 +52,7 @@ import GLKit
0, 0,
&cvMetalTexture) &cvMetalTexture)
if(cvret != 0) { if(cvret != 0) {
print("Error creating texture from image")
metalTextureAddress = -1 metalTextureAddress = -1
return return
} }
@@ -60,35 +62,35 @@ import GLKit
print("Created metal texture @ \(metalTextureAddress)") print("Created metal texture @ \(metalTextureAddress)")
CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0)) // CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let bufferWidth = Int(CVPixelBufferGetWidth(pixelBuffer!)) // let bufferWidth = Int(CVPixelBufferGetWidth(pixelBuffer!))
let bufferHeight = Int(CVPixelBufferGetHeight(pixelBuffer!)) // let bufferHeight = Int(CVPixelBufferGetHeight(pixelBuffer!))
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer!) // let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer!)
guard let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer!) else { // guard let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer!) else {
return // return
} // }
for row in 0..<bufferHeight { // for row in 0..<bufferHeight {
var pixel = baseAddress + row * bytesPerRow // var pixel = baseAddress + row * bytesPerRow
for col in 0..<bufferWidth { // for col in 0..<bufferWidth {
let blue = pixel // let blue = pixel
blue.storeBytes(of: 255, as: UInt8.self) // blue.storeBytes(of: 255, as: UInt8.self)
let red = pixel + 1 // let red = pixel + 1
red.storeBytes(of: 0, as: UInt8.self) // red.storeBytes(of: 0, as: UInt8.self)
let green = pixel + 2 // let green = pixel + 2
green.storeBytes(of: 0, as: UInt8.self) // green.storeBytes(of: 0, as: UInt8.self)
let alpha = pixel + 3 // let alpha = pixel + 3
alpha.storeBytes(of: 255, as: UInt8.self) // alpha.storeBytes(of: 255, as: UInt8.self)
pixel += 4; // pixel += 4;
} // }
} // }
CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0)) // CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
} }

View File

@@ -2,23 +2,20 @@ import Foundation
import GLKit import GLKit
import FlutterMacOS import FlutterMacOS
public class FlutterFilamentTexture : NSObject, FlutterTexture { public class FlutterFilamentTexture : DartFilamentTexture, FlutterTexture {
var texture:DartFilamentTexture
var flutterTextureId: Int64 = -1 var flutterTextureId: Int64 = -1
var registry: FlutterTextureRegistry var registry: FlutterTextureRegistry
init(registry:FlutterTextureRegistry, texture:DartFilamentTexture) { init(registry:FlutterTextureRegistry, width:Int64, height:Int64) {
self.texture = texture
self.registry = registry self.registry = registry
super.init() super.init(width:width, height:height)
self.flutterTextureId = registry.register(self) self.flutterTextureId = registry.register(self)
} }
public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? { public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? {
return Unmanaged.passRetained(texture.pixelBuffer!); return Unmanaged.passRetained(pixelBuffer!);
} }
public func onTextureUnregistered(_ texture:FlutterTexture) { public func onTextureUnregistered(_ texture:FlutterTexture) {
@@ -27,7 +24,7 @@ public class FlutterFilamentTexture : NSObject, FlutterTexture {
public func destroy() { public func destroy() {
self.registry.unregisterTexture(self.flutterTextureId) self.registry.unregisterTexture(self.flutterTextureId)
self.texture.destroyTexture() self.destroyTexture()
} }
} }

View File

@@ -96,10 +96,13 @@ public class SwiftFlutterFilamentPlugin: NSObject, FlutterPlugin {
let width = args[0] as! Int64 let width = args[0] as! Int64
let height = args[1] as! Int64 let height = args[1] as! Int64
let texture = DartFilamentTexture(width: width, height: height) self.texture = FlutterFilamentTexture(registry: registry, width: width, height: height)
self.texture = FlutterFilamentTexture(registry: registry, texture: texture)
result([self.texture!.flutterTextureId as Any, texture.metalTextureAddress, nil]) if(self.texture?.metalTextureAddress == -1) {
result(nil)
} else {
result([self.texture!.flutterTextureId as Any, self.texture?.metalTextureAddress, nil])
}
case "destroyTexture": case "destroyTexture":
self.texture?.destroy() self.texture?.destroy()
self.texture = nil self.texture = nil