From 044bb260d90435c065da7b7f07a98f853ef611d4 Mon Sep 17 00:00:00 2001 From: fufesou Date: Thu, 2 Mar 2023 21:38:55 +0800 Subject: [PATCH] add stride_align for onRgba (#6) Signed-off-by: fufesou --- example/lib/main.dart | 80 ++++++++++++++----- example/macos/Podfile.lock | 2 +- lib/texture_rgba_ffi.dart | 10 +-- lib/texture_rgba_renderer.dart | 5 +- lib/texture_rgba_renderer_method_channel.dart | 12 ++- ...ture_rgba_renderer_platform_interface.dart | 3 +- macos/Classes/TextRgba.swift | 6 +- macos/Classes/TextureRgbaApi.m | 6 +- macos/Classes/TextureRgbaRendererPlugin.swift | 5 +- 9 files changed, 89 insertions(+), 40 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 161adaf..8c3f4b9 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -24,7 +24,7 @@ class _MyAppState extends State { final _textureRgbaRendererPlugin = TextureRgbaRenderer(); int textureId = -1; int height = 500; - int width = 500; + int width = 528; int cnt = 0; var key = 0; int texturePtr = 0; @@ -66,8 +66,8 @@ class _MyAppState extends State { // Method.1: with MethodChannel data = mockPicture(width, height); final t1 = DateTime.now().microsecondsSinceEpoch; - final res = - await _textureRgbaRendererPlugin.onRgba(key, data!, height, width); + final res = await _textureRgbaRendererPlugin.onRgba( + key, data!, height, width, 16); final t2 = DateTime.now().microsecondsSinceEpoch; setState(() { time = t2 - t1; @@ -80,7 +80,7 @@ class _MyAppState extends State { // Method.2: with native ffi final t1 = DateTime.now().microsecondsSinceEpoch; Native.instance.onRgba(Pointer.fromAddress(texturePtr).cast(), - dataPtr, width, height); + dataPtr, width, height, 16); final t2 = DateTime.now().microsecondsSinceEpoch; setState(() { time = t2 - t1; @@ -101,14 +101,52 @@ class _MyAppState extends State { Uint8List mockPicture(int width, int height) { final pic = List.generate(width * height * 4, (index) { - return random.nextInt(255); + final h = (index / 4) / width; + final w = (index / 4) % width; + final p = index & 0x03; + if (h > 20 && h < 30) { + if (w > 20 && w < 25) { + if (p == 0 || p == 3) { + return 255; + } else { + return 0; + } + } + if (w > 40 && w < 45) { + if (p == 1 || p == 3) { + return 255; + } else { + return 0; + } + } + if (w > 60 && w < 65) { + if (p == 2 || p == 3) { + return 255; + } else { + return 0; + } + } + } + return 255; }); return Uint8List.fromList(pic); } Pointer mockPicturePtr(int width, int height) { final pic = List.generate(width * height * 4, (index) { - return random.nextInt(255); + final h = (index / 4) / width; + final w = (index / 4) % width; + final p = index & 0x03; + final edgeH = (h >= 0 && h < 10) || ((h >= height - 10) && h < height); + final edgeW = (w >= 0 && w < 10) || ((w >= width - 10) && w < width); + if (edgeH || edgeW) { + if (p == 0 || p == 3) { + return 255; + } else { + return 0; + } + } + return 255; }); final picAddr = malloc.allocate(pic.length).cast(); final list = picAddr.asTypedList(pic.length); @@ -124,18 +162,20 @@ class _MyAppState extends State { crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ - Expanded(child: textureId == -1 - ? const Offstage() - : Padding( - padding: const EdgeInsets.all(8.0), - child: Container( - alignment: Alignment.center, - decoration: const BoxDecoration(color: Colors.blue), - // decoration: const BoxDecoration(color: Colors.black), - // width: width.toDouble(), - // height: height.toDouble(), - child: Texture(textureId: textureId)), - ),), + Expanded( + child: textureId == -1 + ? const Offstage() + : Padding( + padding: const EdgeInsets.all(8.0), + child: Container( + alignment: Alignment.center, + decoration: const BoxDecoration(color: Colors.blue), + // decoration: const BoxDecoration(color: Colors.black), + // width: width.toDouble(), + // height: height.toDouble(), + child: Texture(textureId: textureId)), + ), + ), Text( "texture id: $textureId, texture memory address: ${texturePtr.toRadixString(16)}"), TextButton.icon( @@ -150,9 +190,7 @@ class _MyAppState extends State { ), Text( "Current mode: ${method == 0 ? 'Method Channel API' : 'Native API'}"), - time != 0 - ? Text("FPS: ${1000000 ~/ time} fps") - : const Offstage() + time != 0 ? Text("FPS: ${1000000 ~/ time} fps") : const Offstage() ], ), ), diff --git a/example/macos/Podfile.lock b/example/macos/Podfile.lock index 150a074..14543af 100644 --- a/example/macos/Podfile.lock +++ b/example/macos/Podfile.lock @@ -14,7 +14,7 @@ EXTERNAL SOURCES: :path: Flutter/ephemeral/.symlinks/plugins/texture_rgba_renderer/macos SPEC CHECKSUMS: - FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424 + FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24 texture_rgba_renderer: cbed959a3c127122194a364e14b8577bd62dc8f2 PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7 diff --git a/lib/texture_rgba_ffi.dart b/lib/texture_rgba_ffi.dart index f9dea82..7c26601 100644 --- a/lib/texture_rgba_ffi.dart +++ b/lib/texture_rgba_ffi.dart @@ -1,9 +1,9 @@ import 'dart:ffi'; -typedef F1 = Void Function( - Pointer ptr, Pointer buffer, Int32 width, Int32 height); -typedef F1Dart = void Function( - Pointer ptr, Pointer buffer, int width, int height); +typedef F1 = Void Function(Pointer ptr, Pointer buffer, + Int32 width, Int32 height, Int stride_align); +typedef F1Dart = void Function(Pointer ptr, Pointer buffer, + int width, int height, int stride_align); class Native { Native._(); @@ -17,4 +17,4 @@ class Native { final lib = DynamicLibrary.process(); onRgba = lib.lookupFunction("FlutterRgbaRendererPluginOnRgba"); } -} \ No newline at end of file +} diff --git a/lib/texture_rgba_renderer.dart b/lib/texture_rgba_renderer.dart index 45bd692..f3a1f02 100644 --- a/lib/texture_rgba_renderer.dart +++ b/lib/texture_rgba_renderer.dart @@ -23,9 +23,10 @@ class TextureRgbaRenderer { } /// Provide the rgba data to the texture. - Future onRgba(int key, Uint8List data, int height, int width) { + Future onRgba( + int key, Uint8List data, int height, int width, int stride_align) { return TextureRgbaRendererPlatform.instance - .onRgba(key, data, height, width); + .onRgba(key, data, height, width, stride_align); } Future getTexturePtr(int key) { diff --git a/lib/texture_rgba_renderer_method_channel.dart b/lib/texture_rgba_renderer_method_channel.dart index 0078f18..f45fcc4 100644 --- a/lib/texture_rgba_renderer_method_channel.dart +++ b/lib/texture_rgba_renderer_method_channel.dart @@ -37,9 +37,15 @@ class MethodChannelTextureRgbaRenderer extends TextureRgbaRendererPlatform { } @override - Future onRgba(int key, Uint8List data, int height, int width) async { - return await methodChannel.invokeMethod('onRgba', - {"data": data, "height": height, "width": width, "key": key}) ?? + Future onRgba( + int key, Uint8List data, int height, int width, int stride_align) async { + return await methodChannel.invokeMethod('onRgba', { + "data": data, + "height": height, + "width": width, + "key": key, + "stride_align": stride_align, + }) ?? false; } diff --git a/lib/texture_rgba_renderer_platform_interface.dart b/lib/texture_rgba_renderer_platform_interface.dart index bc8a437..6e56981 100644 --- a/lib/texture_rgba_renderer_platform_interface.dart +++ b/lib/texture_rgba_renderer_platform_interface.dart @@ -39,7 +39,8 @@ abstract class TextureRgbaRendererPlatform extends PlatformInterface { throw UnimplementedError('closeTexture() has not been implemented.'); } - Future onRgba(int key, Uint8List data, int height, int width) { + Future onRgba( + int key, Uint8List data, int height, int width, int stride_align) { throw UnimplementedError('onRgba() has not been implemented.'); } diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index db85cb2..f1c1a3e 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -34,7 +34,7 @@ import CoreVideo } - @objc public func markFrameAvaliable(data: Data, width: Int, height: Int) -> Bool { + @objc public func markFrameAvaliable(data: Data, width: Int, height: Int, stride_align: Int) -> Bool { queue.sync { self.data = data.withUnsafeBytes { buffer in var pixelBufferCopy: CVPixelBuffer! @@ -42,7 +42,9 @@ import CoreVideo let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, [ kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA, kCVPixelBufferMetalCompatibilityKey: true, - kCVPixelBufferOpenGLCompatibilityKey: true + kCVPixelBufferOpenGLCompatibilityKey: true, + // https://developer.apple.com/forums/thread/712709 + kCVPixelBufferBytesPerRowAlignmentKey: stride_align * 4 ] as CFDictionary, &pixelBufferCopy) guard result == kCVReturnSuccess else { return nil diff --git a/macos/Classes/TextureRgbaApi.m b/macos/Classes/TextureRgbaApi.m index dde55d2..77ba983 100644 --- a/macos/Classes/TextureRgbaApi.m +++ b/macos/Classes/TextureRgbaApi.m @@ -13,10 +13,10 @@ extern "C" { #endif /// Keep the same name with Windows. -void FlutterRgbaRendererPluginOnRgba(void* texture_rgba_ptr, const uint8_t* buffer, int width, int height) { +void FlutterRgbaRendererPluginOnRgba(void* texture_rgba_ptr, const uint8_t* buffer, int width, int height, int stride_align) { TextRgba* texture_rgba = (__bridge TextRgba *)(texture_rgba_ptr); - NSData* data = [NSData dataWithBytesNoCopy:(void*)buffer length:width * height * 4 freeWhenDone:FALSE]; - [texture_rgba markFrameAvaliableWithData:data width:width height:height]; + NSData* data = [NSData dataWithBytesNoCopy:(void*)buffer length:((width + stride_align - 1) & ~(stride_align - 1)) * height * 4 freeWhenDone:FALSE]; + [texture_rgba markFrameAvaliableWithData:data width:width height:height stride_align: stride_align]; } #if __cplusplus diff --git a/macos/Classes/TextureRgbaRendererPlugin.swift b/macos/Classes/TextureRgbaRendererPlugin.swift index 2b5c372..8c991a6 100644 --- a/macos/Classes/TextureRgbaRendererPlugin.swift +++ b/macos/Classes/TextureRgbaRendererPlugin.swift @@ -48,12 +48,13 @@ public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin { let key = args["key"] as! Int64 let data = args["data"] as! FlutterStandardTypedData let height = args["height"] as! Int - let width = args["height"] as! Int + let width = args["width"] as! Int + let stride_align = args["stride_align"] as! Int let textureRgba = renderer[key] if (textureRgba == nil) { result(false) } else { - result(textureRgba!.markFrameAvaliable(data: data.data, width: width, height: height)) + result(textureRgba!.markFrameAvaliable(data: data.data, width: width, height: height, stride_align: stride_align)) } case "getTexturePtr": let args = call.arguments as! Dictionary