From 6fd900c4794646161b11574277b36718123f9743 Mon Sep 17 00:00:00 2001 From: Kingtous Date: Sun, 19 Feb 2023 19:14:48 -0800 Subject: [PATCH] feat: texture rgba interface --- lib/texture_rgba_renderer_method_channel.dart | 7 +- macos/Classes/TextRgba.swift | 67 ++++++++++--------- macos/Classes/TextureRgbaApi.m | 24 +++++++ macos/Classes/TextureRgbaRendererPlugin.swift | 14 ++++ 4 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 macos/Classes/TextureRgbaApi.m diff --git a/lib/texture_rgba_renderer_method_channel.dart b/lib/texture_rgba_renderer_method_channel.dart index feb20ec..693c2c4 100644 --- a/lib/texture_rgba_renderer_method_channel.dart +++ b/lib/texture_rgba_renderer_method_channel.dart @@ -1,4 +1,8 @@ +// ignore: unused_import 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/services.dart'; @@ -41,9 +45,6 @@ class MethodChannelTextureRgbaRenderer extends TextureRgbaRendererPlatform { @override Future getTexturePtr(int key) async { - if (Platform.isMacOS) { - return -1; - } final ptr = await methodChannel.invokeMethod('getTexturePtr', {"key": key}) ?? false; diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index 42bbc58..db85cb2 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -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? { - 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 + } + } + } } diff --git a/macos/Classes/TextureRgbaApi.m b/macos/Classes/TextureRgbaApi.m new file mode 100644 index 0000000..0063c94 --- /dev/null +++ b/macos/Classes/TextureRgbaApi.m @@ -0,0 +1,24 @@ +// +// TextureRgbaApi.m +// texture_rgba_renderer +// +// Created by kingtous on 2023/2/19. +// + +#import +#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 diff --git a/macos/Classes/TextureRgbaRendererPlugin.swift b/macos/Classes/TextureRgbaRendererPlugin.swift index 148c8f3..6481b26 100644 --- a/macos/Classes/TextureRgbaRendererPlugin.swift +++ b/macos/Classes/TextureRgbaRendererPlugin.swift @@ -55,8 +55,22 @@ public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin { } else { result(textureRgba!.markFrameAvaliable(data: data.data, width: width, height: height)) } + case "getTexturePtr": + let args = call.arguments as! Dictionary + 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: result(FlutterMethodNotImplemented) } } } +