use Unmanaged.passRetained rather than passUnretained for ThermionDartTexture

This commit is contained in:
Nick Fisher
2024-06-19 13:21:47 +08:00
parent 534e5b46a8
commit ab793387bc

View File

@@ -11,9 +11,10 @@ import GLKit
] as [CFString : Any] as CFDictionary ] as [CFString : Any] as CFDictionary
@objc public var cvMetalTextureCache:CVMetalTextureCache? @objc public var cvMetalTextureCache:CVMetalTextureCache?
@objc public var metalDevice:MTLDevice?
@objc public var cvMetalTexture:CVMetalTexture? @objc public var cvMetalTexture:CVMetalTexture?
@objc public var metalTexture:MTLTexture? @objc public var metalTexture:MTLTexture?
@objc public var metalDevice:MTLDevice?
@objc public var metalTextureAddress:Int = -1 @objc public var metalTextureAddress:Int = -1
@objc override public init() { @objc override public init() {
@@ -21,8 +22,9 @@ import GLKit
} }
@objc public init(width:Int64, height:Int64) { @objc public init(width:Int64, height:Int64) {
if(self.metalDevice == nil) {
self.metalDevice = MTLCreateSystemDefaultDevice()! self.metalDevice = MTLCreateSystemDefaultDevice()!
}
// create pixel buffer // create pixel buffer
if(CVPixelBufferCreate(kCFAllocatorDefault, Int(width), Int(height), if(CVPixelBufferCreate(kCFAllocatorDefault, Int(width), Int(height),
@@ -31,37 +33,36 @@ import GLKit
metalTextureAddress = -1; metalTextureAddress = -1;
return return
} }
if self.cvMetalTextureCache == nil {
var cvret = CVMetalTextureCacheCreate( let cacheCreationResult = CVMetalTextureCacheCreate(
kCFAllocatorDefault, kCFAllocatorDefault,
nil, nil,
metalDevice!, self.metalDevice!,
nil, nil,
&cvMetalTextureCache); &self.cvMetalTextureCache)
if(cvret != 0) { if(cacheCreationResult != kCVReturnSuccess) {
print("Error creating Metal texture cache") print("Error creating Metal texture cache")
metalTextureAddress = -1 metalTextureAddress = -1
return return
} }
cvret = CVMetalTextureCacheCreateTextureFromImage( }
let cvret = CVMetalTextureCacheCreateTextureFromImage(
kCFAllocatorDefault, kCFAllocatorDefault,
cvMetalTextureCache!, self.cvMetalTextureCache!,
pixelBuffer!, nil, pixelBuffer!, nil,
MTLPixelFormat.bgra8Unorm, MTLPixelFormat.bgra8Unorm,
Int(width), Int(height), Int(width), Int(height),
0, 0,
&cvMetalTexture) &cvMetalTexture)
if(cvret != 0) { if(cvret != kCVReturnSuccess) {
print("Error creating texture from image") print("Error creating texture from image")
metalTextureAddress = -1 metalTextureAddress = -1
return return
} }
metalTexture = CVMetalTextureGetTexture(cvMetalTexture!) metalTexture = CVMetalTextureGetTexture(cvMetalTexture!)
let metalTexturePtr = Unmanaged.passUnretained(metalTexture!).toOpaque() let metalTexturePtr = Unmanaged.passRetained(metalTexture!).toOpaque()
metalTextureAddress = Int(bitPattern:metalTexturePtr) metalTextureAddress = Int(bitPattern:metalTexturePtr)
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!))
@@ -95,7 +96,12 @@ import GLKit
} }
@objc public func destroyTexture() { @objc public func destroyTexture() {
metalTexture = nil CVMetalTextureCacheFlush(self.cvMetalTextureCache!, 0)
self.metalTexture = nil
self.cvMetalTexture = nil
self.pixelBuffer = nil
self.metalDevice = nil
self.cvMetalTextureCache = nil
} }