feat: texture rgba interface
This commit is contained in:
parent
44d3a78b9b
commit
6fd900c479
4 changed files with 78 additions and 34 deletions
|
|
@ -1,4 +1,8 @@
|
||||||
|
// ignore: unused_import
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
// Compatible with lint in Flutter 3.0.5
|
||||||
|
// ignore: unused_import
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
@ -41,9 +45,6 @@ class MethodChannelTextureRgbaRenderer extends TextureRgbaRendererPlatform {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<int> getTexturePtr(int key) async {
|
Future<int> getTexturePtr(int key) async {
|
||||||
if (Platform.isMacOS) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
final ptr =
|
final ptr =
|
||||||
await methodChannel.invokeMethod('getTexturePtr', {"key": key}) ??
|
await methodChannel.invokeMethod('getTexturePtr', {"key": key}) ??
|
||||||
false;
|
false;
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,13 @@ import Foundation
|
||||||
import FlutterMacOS
|
import FlutterMacOS
|
||||||
import CoreVideo
|
import CoreVideo
|
||||||
|
|
||||||
public final class TextRgba: NSObject, FlutterTexture {
|
@objc public class TextRgba: NSObject, FlutterTexture {
|
||||||
public var textureId: Int64 = -1
|
public var textureId: Int64 = -1
|
||||||
private var registry: FlutterTextureRegistry?
|
private var registry: FlutterTextureRegistry?
|
||||||
private var data: CVPixelBuffer?
|
private var data: CVPixelBuffer?
|
||||||
private var width: Int = 0
|
private var width: Int = 0
|
||||||
private var height: Int = 0
|
private var height: Int = 0
|
||||||
|
private let queue = DispatchQueue(label: "text_rgba_sync_queue")
|
||||||
|
|
||||||
public static func new(registry: FlutterTextureRegistry?) -> TextRgba {
|
public static func new(registry: FlutterTextureRegistry?) -> TextRgba {
|
||||||
let textRgba = TextRgba()
|
let textRgba = TextRgba()
|
||||||
|
|
@ -24,41 +25,45 @@ public final class TextRgba: NSObject, FlutterTexture {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? {
|
public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? {
|
||||||
if (data == nil) {
|
queue.sync {
|
||||||
return nil
|
if (data == 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 {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
CVPixelBufferLockBaseAddress(pixelBufferCopy, [])
|
return Unmanaged.passRetained(data!)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
24
macos/Classes/TextureRgbaApi.m
Normal file
24
macos/Classes/TextureRgbaApi.m
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
//
|
||||||
|
// TextureRgbaApi.m
|
||||||
|
// texture_rgba_renderer
|
||||||
|
//
|
||||||
|
// Created by kingtous on 2023/2/19.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
#import "texture_rgba_renderer/texture_rgba_renderer-Swift.h"
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/// Keep the same name with Windows.
|
||||||
|
void FlutterRgbaRendererPluginOnRgba(void* texture_rgba_ptr, const uint8_t* buffer, int width, int height) {
|
||||||
|
TextRgba* texture_rgba = (__bridge TextRgba *)(texture_rgba_ptr);
|
||||||
|
NSData* data = [NSData dataWithBytesNoCopy:(void*)buffer length:width * height * 4];
|
||||||
|
[texture_rgba markFrameAvaliableWithData:data width:width height:height];
|
||||||
|
}
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
} //Extern C
|
||||||
|
#endif
|
||||||
|
|
@ -55,8 +55,22 @@ public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin {
|
||||||
} else {
|
} else {
|
||||||
result(textureRgba!.markFrameAvaliable(data: data.data, width: width, height: height))
|
result(textureRgba!.markFrameAvaliable(data: data.data, width: width, height: height))
|
||||||
}
|
}
|
||||||
|
case "getTexturePtr":
|
||||||
|
let args = call.arguments as! Dictionary<String, Any?>
|
||||||
|
let key = args["key"] as! Int64
|
||||||
|
let textureRgba = renderer[key]
|
||||||
|
if (textureRgba == nil) {
|
||||||
|
result(0)
|
||||||
|
} else {
|
||||||
|
let ptr = withUnsafePointer(to: textureRgba!) { p in
|
||||||
|
return UnsafeRawPointer(p);
|
||||||
|
};
|
||||||
|
let intAddr = UInt(bitPattern: ptr)
|
||||||
|
result(intAddr)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
result(FlutterMethodNotImplemented)
|
result(FlutterMethodNotImplemented)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue