add: macos impl
This commit is contained in:
parent
7ab1554948
commit
1f012dffac
8 changed files with 235 additions and 1 deletions
66
macos/Classes/TextRgba.swift
Normal file
66
macos/Classes/TextRgba.swift
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
//
|
||||
// TextRgba.swift
|
||||
// texture_rgba_renderer
|
||||
//
|
||||
// Created by kingtous on 2023/2/17.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import FlutterMacOS
|
||||
import CoreVideo
|
||||
|
||||
public final 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
|
||||
|
||||
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>? {
|
||||
if (data == nil) {
|
||||
return nil
|
||||
}
|
||||
return Unmanaged.passUnretained(data!)
|
||||
}
|
||||
|
||||
public func markFrameAvaliable(data: Data, width: Int, height: Int) -> Bool {
|
||||
self.data = data.withUnsafeBytes { buffer in
|
||||
var pixelBufferCopy: CVPixelBuffer? = nil
|
||||
CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32RGBA, nil, &pixelBufferCopy)
|
||||
if (pixelBufferCopy == nil) {
|
||||
return nil
|
||||
}
|
||||
CVPixelBufferLockBaseAddress(pixelBufferCopy!, [])
|
||||
defer {CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, [])}
|
||||
var source = buffer.baseAddress!
|
||||
|
||||
for plane in 0 ..< CVPixelBufferGetPlaneCount(pixelBufferCopy!) {
|
||||
let dest = CVPixelBufferGetBaseAddressOfPlane(pixelBufferCopy!, plane)
|
||||
let height = CVPixelBufferGetHeightOfPlane(pixelBufferCopy!, plane)
|
||||
let bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(pixelBufferCopy!, plane)
|
||||
let planceSize = height * bytesPerRow
|
||||
memcpy(dest, source, planceSize)
|
||||
source += planceSize
|
||||
}
|
||||
return pixelBufferCopy
|
||||
}
|
||||
self.width = width
|
||||
self.height = height
|
||||
if (textureId != -1) {
|
||||
registry?.textureFrameAvailable(textureId)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -2,16 +2,57 @@ import Cocoa
|
|||
import FlutterMacOS
|
||||
|
||||
public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin {
|
||||
|
||||
private var renderer: Dictionary<Int64, TextRgba> = [:]
|
||||
private var texture_registry: FlutterTextureRegistry?
|
||||
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let channel = FlutterMethodChannel(name: "texture_rgba_renderer", binaryMessenger: registrar.messenger)
|
||||
let instance = TextureRgbaRendererPlugin()
|
||||
instance.setTextureRegistry(registry: registrar.textures)
|
||||
registrar.addMethodCallDelegate(instance, channel: channel)
|
||||
}
|
||||
|
||||
public func setTextureRegistry(registry: FlutterTextureRegistry) {
|
||||
texture_registry = registry
|
||||
}
|
||||
|
||||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||
switch call.method {
|
||||
case "getPlatformVersion":
|
||||
result("macOS " + ProcessInfo.processInfo.operatingSystemVersionString)
|
||||
case "createTexture":
|
||||
let args = call.arguments as! Dictionary<String, Any?>
|
||||
let key = args["key"] as! Int64
|
||||
if (renderer[key] != nil) {
|
||||
result(-1)
|
||||
} else {
|
||||
let textRgba = TextRgba.new(registry: texture_registry)
|
||||
renderer[key] = textRgba
|
||||
result(textRgba.textureId)
|
||||
}
|
||||
case "closeTexture":
|
||||
let args = call.arguments as! Dictionary<String, Any?>
|
||||
let key = args["key"] as! Int64
|
||||
if (renderer[key] != nil) {
|
||||
texture_registry?.unregisterTexture(renderer[key]?.textureId ?? -1)
|
||||
result(true)
|
||||
} else {
|
||||
result(false)
|
||||
}
|
||||
|
||||
case "onRgba":
|
||||
let args = call.arguments as! Dictionary<String, Any?>
|
||||
let key = args["key"] as! Int64
|
||||
let data = args["data"] as! Data
|
||||
let height = args["height"] as! Int
|
||||
let width = args["height"] as! Int
|
||||
let textureRgba = renderer[key]
|
||||
if (textureRgba == nil) {
|
||||
result(false)
|
||||
} else {
|
||||
result(textureRgba!.markFrameAvaliable(data: data, width: width, height: height))
|
||||
}
|
||||
default:
|
||||
result(FlutterMethodNotImplemented)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue