This commit is contained in:
Nick Fisher
2024-04-30 14:14:54 +08:00
parent 8f9e309c34
commit 14b0b674c5
28 changed files with 423 additions and 1787 deletions

View File

@@ -1,16 +1,15 @@
import Foundation
import Foundation
import GLKit
@objc public class DartFilamentTexture : NSObject {
var pixelBuffer: CVPixelBuffer?
public var pixelBuffer: CVPixelBuffer?
var pixelBufferAttrs = [
kCVPixelBufferPixelFormatTypeKey: NSNumber(value: kCVPixelFormatType_32ABGR ),
// kCVPixelBufferOpenGLCompatibilityKey: kCFBooleanTrue!,
kCVPixelBufferIOSurfacePropertiesKey: [:] as CFDictionary
] as CFDictionary
kCVPixelBufferOpenGLCompatibilityKey: kCFBooleanFalse!,
// kCVPixelBufferIOSurfacePropertiesKey: [Any:Any] as CFDictionary
] as [CFString : Any] as CFDictionary
@objc public var cvMetalTextureCache:CVMetalTextureCache?
@objc public var cvMetalTexture:CVMetalTexture?
@@ -19,7 +18,7 @@ import GLKit
@objc public var metalTextureAddress:Int = -1
@objc override public init() {
print("VAnilla init")
}
@objc public init(width:Int64, height:Int64) {

View File

@@ -0,0 +1,33 @@
import Foundation
import GLKit
import FlutterMacOS
public class FlutterFilamentTexture : NSObject, FlutterTexture {
var texture:DartFilamentTexture
var flutterTextureId: Int64 = -1
var registry: FlutterTextureRegistry
init(registry:FlutterTextureRegistry, texture:DartFilamentTexture) {
self.texture = texture
self.registry = registry
super.init()
self.flutterTextureId = registry.register(self)
}
public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? {
return Unmanaged.passRetained(texture.pixelBuffer!);
}
public func onTextureUnregistered(_ texture:FlutterTexture) {
print("Texture unregistered")
}
public func destroy() {
self.registry.unregisterTexture(self.flutterTextureId)
self.texture.destroyTexture()
}
}

View File

@@ -1,36 +1,6 @@
import FlutterMacOS
import GLKit
public class FlutterFilamentTexture : NSObject, FlutterTexture {
var texture:DartFilamentTexture
var flutterTextureId: Int64 = -1
var registry: FlutterTextureRegistry
init(registry:FlutterTextureRegistry, texture:DartFilamentTexture) {
self.texture = texture
self.registry = registry
super.init()
self.flutterTextureId = registry.register(self)
}
public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? {
return Unmanaged.passRetained(texture.pixelBuffer!);
}
public func onTextureUnregistered(_ texture:FlutterTexture) {
print("Texture unregistered")
}
public func destroy() {
self.registry.unregisterTexture(self.flutterTextureId)
self.texture.destroyTexture()
}
}
public class SwiftFlutterFilamentPlugin: NSObject, FlutterPlugin {
var registrar : FlutterPluginRegistrar
@@ -121,8 +91,6 @@ public class SwiftFlutterFilamentPlugin: NSObject, FlutterPlugin {
result(nil)
case "getSharedContext":
result(nil)
case "getWindow":
result(nil)
case "createTexture":
let args = call.arguments as! [Any]
let width = args[0] as! Int64
@@ -131,7 +99,7 @@ public class SwiftFlutterFilamentPlugin: NSObject, FlutterPlugin {
let texture = DartFilamentTexture(width: width, height: height)
self.texture = FlutterFilamentTexture(registry: registry, texture: texture)
result([self.texture!.flutterTextureId as Any, texture.metalTextureAddress])
result([self.texture!.flutterTextureId as Any, texture.metalTextureAddress, nil])
case "destroyTexture":
self.texture?.destroy()
self.texture = nil

View File

@@ -0,0 +1,117 @@
#ifndef RESOURCE_BUFFER_H
#define RESOURCE_BUFFER_H
#include <stdint.h>
#include <stdlib.h>
//
// A ResourceBuffer is a unified interface for working with
// binary assets across various platforms.
// This is simply:
// 1) a pointer to some data
// 2) the length of the data
// 3) an ID that can be passed back to the native platform to release the underlying asset when needed.
//
struct ResourceBuffer
{
const void *const data;
const int32_t size;
const int32_t id;
};
typedef struct ResourceBuffer ResourceBuffer;
typedef ResourceBuffer (*LoadFilamentResource)(const char *uri);
typedef ResourceBuffer (*LoadFilamentResourceFromOwner)(const char *const, void *const owner);
typedef void (*FreeFilamentResource)(ResourceBuffer);
typedef void (*FreeFilamentResourceFromOwner)(ResourceBuffer, void *const owner);
struct ResourceLoaderWrapper
{
LoadFilamentResource loadResource;
FreeFilamentResource freeResource;
LoadFilamentResourceFromOwner loadFromOwner;
FreeFilamentResourceFromOwner freeFromOwner;
void *owner;
};
typedef struct ResourceLoaderWrapper ResourceLoaderWrapper;
#if defined(__cplusplus)
namespace flutter_filament {
// struct ResourceBufferImpl : public ResourceBuffer
// {
// ResourceBufferImpl(const void *const mData, const int32_t mSize, const int32_t mId)
// {
// data = mData;
// size = mSize;
// id = mId;
// };
// ResourceBufferImpl(const ResourceBufferImpl &rb)
// {
// (void *)data = rb.data;
// size = rb.size;
// id = rb.id;
// };
// ResourceBufferImpl(const ResourceBufferImpl &&rb) noexcept
// {
// data = rb.data;
// size = rb.size;
// id = rb.id;
// };
// ResourceBufferImpl &operator=(const ResourceBufferImpl &other) = delete;
// };
struct ResourceLoaderWrapperImpl : public ResourceLoaderWrapper
{
ResourceLoaderWrapperImpl(LoadFilamentResource loader, FreeFilamentResource freeResource)
{
loadFromOwner = nullptr;
freeFromOwner = nullptr;
loadResource = loader;
freeResource = freeResource;
owner = nullptr;
}
ResourceLoaderWrapperImpl(LoadFilamentResourceFromOwner loader, FreeFilamentResourceFromOwner freeResource, void * owner)
{
loadResource = nullptr;
freeResource = nullptr;
loadFromOwner = loader;
freeFromOwner = freeResource;
owner = owner;
}
ResourceBuffer load(const char *uri) const
{
if (loadFromOwner)
{
auto rb = loadFromOwner(uri, owner);
return rb;
}
auto rb = loadResource(uri);
return rb;
}
void free(ResourceBuffer rb) const
{
if (freeFromOwner)
{
freeFromOwner(rb, owner);
}
else
{
freeResource(rb);
}
}
};
}
#endif
#endif

View File

@@ -0,0 +1,17 @@
#ifndef SwiftFlutterFilamentPlugin_Bridging_Header_h
#define SwiftFlutterFilamentPlugin_Bridging_Header_h
#include <stdint.h>
#include "ResourceBuffer.hpp"
ResourceLoaderWrapper *make_resource_loader(LoadFilamentResourceFromOwner loadFn, FreeFilamentResourceFromOwner freeFn, void *const owner)
{
ResourceLoaderWrapper *rlw = (ResourceLoaderWrapper *)malloc(sizeof(ResourceLoaderWrapper));
rlw->loadFromOwner = loadFn;
rlw->freeFromOwner = freeFn;
rlw->owner = owner;
return rlw;
}
#endif