feat: texture rgba interface
This commit is contained in:
parent
44d3a78b9b
commit
6fd900c479
4 changed files with 78 additions and 34 deletions
|
|
@ -9,12 +9,13 @@ import Foundation
|
|||
import FlutterMacOS
|
||||
import CoreVideo
|
||||
|
||||
public final class TextRgba: NSObject, FlutterTexture {
|
||||
@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()
|
||||
|
|
@ -24,41 +25,45 @@ public final class TextRgba: NSObject, FlutterTexture {
|
|||
}
|
||||
|
||||
public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? {
|
||||
if (data == nil) {
|
||||
return nil
|
||||
}
|
||||
return Unmanaged.passRetained(data!)
|
||||
}
|
||||
|
||||
public func markFrameAvaliable(data: Data, width: Int, height: Int) -> Bool {
|
||||
self.data = data.withUnsafeBytes { buffer in
|
||||
var pixelBufferCopy: CVPixelBuffer!
|
||||
// macOS only support 32BGRA currently.
|
||||
let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, [
|
||||
kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA,
|
||||
kCVPixelBufferMetalCompatibilityKey: true,
|
||||
kCVPixelBufferOpenGLCompatibilityKey: true
|
||||
] as CFDictionary, &pixelBufferCopy)
|
||||
guard result == kCVReturnSuccess else {
|
||||
queue.sync {
|
||||
if (data == nil) {
|
||||
return nil
|
||||
}
|
||||
CVPixelBufferLockBaseAddress(pixelBufferCopy, [])
|
||||
let source = buffer.baseAddress!
|
||||
let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)!
|
||||
memcpy(ptr, source, data.count)
|
||||
CVPixelBufferUnlockBaseAddress(pixelBufferCopy, [])
|
||||
return pixelBufferCopy
|
||||
}
|
||||
self.width = width
|
||||
self.height = height
|
||||
if (textureId != -1 && self.data != nil) {
|
||||
registry?.textureFrameAvailable(textureId)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
return Unmanaged.passRetained(data!)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@objc public func markFrameAvaliable(data: Data, width: Int, height: Int) -> Bool {
|
||||
queue.sync {
|
||||
self.data = data.withUnsafeBytes { buffer in
|
||||
var pixelBufferCopy: CVPixelBuffer!
|
||||
// macOS only support 32BGRA currently.
|
||||
let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, [
|
||||
kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA,
|
||||
kCVPixelBufferMetalCompatibilityKey: true,
|
||||
kCVPixelBufferOpenGLCompatibilityKey: true
|
||||
] as CFDictionary, &pixelBufferCopy)
|
||||
guard result == kCVReturnSuccess else {
|
||||
return nil
|
||||
}
|
||||
CVPixelBufferLockBaseAddress(pixelBufferCopy, [])
|
||||
let source = buffer.baseAddress!
|
||||
let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)!
|
||||
memcpy(ptr, source, data.count)
|
||||
CVPixelBufferUnlockBaseAddress(pixelBufferCopy, [])
|
||||
return pixelBufferCopy
|
||||
}
|
||||
self.width = width
|
||||
self.height = height
|
||||
if (textureId != -1 && self.data != nil) {
|
||||
registry?.textureFrameAvailable(textureId)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue