add stride_align for onRgba (#6)

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-03-02 21:38:55 +08:00 • committed by GitHub
parent 1c79d8dd88
commit 044bb260d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 40 deletions

View file

@ -24,7 +24,7 @@ class _MyAppState extends State<MyApp> {
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<MyApp> {
// 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<MyApp> {
// Method.2: with native ffi
final t1 = DateTime.now().microsecondsSinceEpoch;
Native.instance.onRgba(Pointer.fromAddress(texturePtr).cast<Void>(),
dataPtr, width, height);
dataPtr, width, height, 16);
final t2 = DateTime.now().microsecondsSinceEpoch;
setState(() {
time = t2 - t1;
@ -101,14 +101,52 @@ class _MyAppState extends State<MyApp> {
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<Uint8> 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<Uint8>();
final list = picAddr.asTypedList(pic.length);
@ -124,18 +162,20 @@ class _MyAppState extends State<MyApp> {
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<MyApp> {
),
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()
],
),
),

View file

@ -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

View file

@ -1,9 +1,9 @@
import 'dart:ffi';
typedef F1 = Void Function(
Pointer<Void> ptr, Pointer<Uint8> buffer, Int32 width, Int32 height);
typedef F1Dart = void Function(
Pointer<Void> ptr, Pointer<Uint8> buffer, int width, int height);
typedef F1 = Void Function(Pointer<Void> ptr, Pointer<Uint8> buffer,
Int32 width, Int32 height, Int stride_align);
typedef F1Dart = void Function(Pointer<Void> ptr, Pointer<Uint8> buffer,
int width, int height, int stride_align);
class Native {
Native._();
@ -17,4 +17,4 @@ class Native {
final lib = DynamicLibrary.process();
onRgba = lib.lookupFunction<F1, F1Dart>("FlutterRgbaRendererPluginOnRgba");
}
}
}

View file

@ -23,9 +23,10 @@ class TextureRgbaRenderer {
}
/// Provide the rgba data to the texture.
Future<bool> onRgba(int key, Uint8List data, int height, int width) {
Future<bool> 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<int> getTexturePtr(int key) {

View file

@ -37,9 +37,15 @@ class MethodChannelTextureRgbaRenderer extends TextureRgbaRendererPlatform {
}
@override
Future<bool> onRgba(int key, Uint8List data, int height, int width) async {
return await methodChannel.invokeMethod<bool>('onRgba',
{"data": data, "height": height, "width": width, "key": key}) ??
Future<bool> onRgba(
int key, Uint8List data, int height, int width, int stride_align) async {
return await methodChannel.invokeMethod<bool>('onRgba', {
"data": data,
"height": height,
"width": width,
"key": key,
"stride_align": stride_align,
}) ??
false;
}

View file

@ -39,7 +39,8 @@ abstract class TextureRgbaRendererPlatform extends PlatformInterface {
throw UnimplementedError('closeTexture() has not been implemented.');
}
Future<bool> onRgba(int key, Uint8List data, int height, int width) {
Future<bool> onRgba(
int key, Uint8List data, int height, int width, int stride_align) {
throw UnimplementedError('onRgba() has not been implemented.');
}

View file

@ -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

View file

@ -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

View file

@ -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<String, Any?>