From 05a3bfc9596d41161969fa7da5d1efce39a94919 Mon Sep 17 00:00:00 2001 From: Kingtous Date: Mon, 20 Feb 2023 00:52:40 -0800 Subject: [PATCH] opt: add example --- README.md | 6 ++- example/lib/main.dart | 95 +++++++++++++++++++++++++++++-------------- 2 files changed, 69 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 736271e..fdd46ae 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # Texture Rgba Renderer -A texture helper which brings a high level api to handle **BGRA** data. No frame copy required, and with full hardware acceleration. +A general texture helper which brings both of high and low level api to handle **BGRA** data for Flutter. Only one time frame copy required, and aims to get a full hardware acceleration. + +This plugin is originally developed for [RustDesk](https://github.com/rustdesk/rustdesk). + +Now it's in active development, which aims to improve the memory usage and performance. ![](./res/banner.png) diff --git a/example/lib/main.dart b/example/lib/main.dart index 5b247c8..130c01d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -31,6 +31,8 @@ class _MyAppState extends State { final random = Random(); Uint8List? data; Timer? _timer; + int time = 0; + int method = 0; @override void initState() { @@ -53,22 +55,39 @@ class _MyAppState extends State { }); } - void start() { + void start(int methodId) { debugPrint("start mockPic"); + method = methodId; _timer?.cancel(); - _timer = Timer.periodic(const Duration(milliseconds: 30), (timer) async { - // data = mockPicture(width, height); - // Method.1: with MethodChannel - // final res = - // await _textureRgbaRendererPlugin.onRgba(key, data!, height, width); - // if (!res) { - // debugPrint("WARN: render failed"); - // } - final dataPtr = mockPicturePtr(width, height); - // Method.2: with native ffi - Native.instance.onRgba( - Pointer.fromAddress(texturePtr).cast(), dataPtr, width, height); - malloc.free(dataPtr); + // 60 fps + _timer = + Timer.periodic(const Duration(milliseconds: 1000 ~/ 60), (timer) async { + if (methodId == 0) { + // Method.1: with MethodChannel + data = mockPicture(width, height); + final t1 = DateTime.now().microsecondsSinceEpoch; + final res = + await _textureRgbaRendererPlugin.onRgba(key, data!, height, width); + final t2 = DateTime.now().microsecondsSinceEpoch; + setState(() { + time = t2 - t1; + }); + + if (!res) { + debugPrint("WARN: render failed"); + } + } else { + final dataPtr = mockPicturePtr(width, height); + // Method.2: with native ffi + final t1 = DateTime.now().microsecondsSinceEpoch; + Native.instance.onRgba(Pointer.fromAddress(texturePtr).cast(), + dataPtr, width, height); + final t2 = DateTime.now().microsecondsSinceEpoch; + setState(() { + time = t2 - t1; + }); + malloc.free(dataPtr); + } }); } @@ -102,27 +121,41 @@ class _MyAppState extends State { Widget build(BuildContext context) { return MaterialApp( home: Scaffold( - body: Column( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - textureId == -1 - ? const Offstage() - : Container( - alignment: Alignment.center, - child: Container( - decoration: const BoxDecoration(color: Colors.black), + body: Center( + child: SingleChildScrollView( + child: Column( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + textureId == -1 + ? const Offstage() + : 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)}"), - IconButton( - icon: const Icon(Icons.play_arrow), - onPressed: start, + Text( + "texture id: $textureId, texture memory address: ${texturePtr.toRadixString(16)}"), + TextButton.icon( + label: const Text("play with texture (method channel API)"), + icon: const Icon(Icons.play_arrow), + onPressed: () => start(0), + ), + TextButton.icon( + label: const Text("play with texture (native API, faster)"), + icon: const Icon(Icons.play_arrow), + onPressed: () => start(1), + ), + Text( + "Current mode: ${method == 0 ? 'Method Channel API' : 'Native API'}"), + time != 0 + ? Text("FPS: ${1000000 ~/ time} fps") + : const Offstage() + ], ), - ], + ), ), ), );