85 lines
2.9 KiB
Swift
85 lines
2.9 KiB
Swift
//
|
|
// TextRgba.swift
|
|
// texture_rgba_renderer
|
|
//
|
|
// Created by kingtous on 2023/2/17.
|
|
//
|
|
|
|
import Foundation
|
|
import FlutterMacOS
|
|
import CoreVideo
|
|
|
|
@objc public class TextRgba: NSObject, FlutterTexture {
|
|
public var textureId: Int64 = -1
|
|
private var registry: FlutterTextureRegistry?
|
|
private var data: CVPixelBuffer?
|
|
private var width: Int = 0
|
|
private var height: Int = 0
|
|
private let queue = DispatchQueue(label: "text_rgba_sync_queue")
|
|
|
|
public static func new(registry: FlutterTextureRegistry?) -> TextRgba {
|
|
let textRgba = TextRgba()
|
|
textRgba.registry = registry
|
|
textRgba.textureId = registry?.register(textRgba) ?? -1
|
|
return textRgba
|
|
}
|
|
|
|
public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? {
|
|
queue.sync {
|
|
if (data == nil) {
|
|
return nil
|
|
}
|
|
return Unmanaged.passRetained(data!)
|
|
}
|
|
}
|
|
|
|
private func _markFrameAvaliable(buffer: UnsafePointer<UInt8>, len: Int, width: Int, height: Int, stride_align: Int) -> Bool {
|
|
var pixelBufferCopy: CVPixelBuffer?
|
|
// macOS only support 32BGRA currently.
|
|
let dict: [String: Any] = [
|
|
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
|
|
kCVPixelBufferMetalCompatibilityKey as String: true,
|
|
kCVPixelBufferOpenGLCompatibilityKey as String: true,
|
|
// https://developer.apple.com/forums/thread/712709
|
|
kCVPixelBufferBytesPerRowAlignmentKey as String: 64
|
|
]
|
|
let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, dict as CFDictionary, &pixelBufferCopy)
|
|
guard result == kCVReturnSuccess else {
|
|
return false
|
|
}
|
|
|
|
CVPixelBufferLockBaseAddress(pixelBufferCopy!, [])
|
|
let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)!
|
|
memcpy(ptr, buffer, len)
|
|
CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, [])
|
|
self.data = pixelBufferCopy
|
|
self.width = width
|
|
self.height = height
|
|
if textureId != -1 && self.data != nil {
|
|
registry?.textureFrameAvailable(textureId)
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
@objc public func markFrameAvaliableRaw(buffer: UnsafePointer<UInt8>, len: Int, width: Int, height: Int, stride_align: Int) -> Bool {
|
|
queue.sync {
|
|
_markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align)
|
|
}
|
|
}
|
|
|
|
|
|
@objc public func markFrameAvaliable(data: Data, width: Int, height: Int, stride_align: Int) -> Bool {
|
|
data.withUnsafeBytes { buffer in
|
|
markFrameAvaliableRaw(
|
|
buffer: buffer.baseAddress!.assumingMemoryBound(to: UInt8.self),
|
|
len: data.count,
|
|
width: width,
|
|
height: height,
|
|
stride_align: stride_align)
|
|
}
|
|
}
|
|
}
|
|
|
|
|