opt: add example

This commit is contained in:
Kingtous 2023-02-20 00:52:40 -08:00
parent f898d19a27
commit 05a3bfc959
2 changed files with 69 additions and 32 deletions

View file

@ -1,6 +1,10 @@
# Texture Rgba Renderer # 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) ![](./res/banner.png)

View file

@ -31,6 +31,8 @@ class _MyAppState extends State<MyApp> {
final random = Random(); final random = Random();
Uint8List? data; Uint8List? data;
Timer? _timer; Timer? _timer;
int time = 0;
int method = 0;
@override @override
void initState() { void initState() {
@ -53,22 +55,39 @@ class _MyAppState extends State<MyApp> {
}); });
} }
void start() { void start(int methodId) {
debugPrint("start mockPic"); debugPrint("start mockPic");
method = methodId;
_timer?.cancel(); _timer?.cancel();
_timer = Timer.periodic(const Duration(milliseconds: 30), (timer) async { // 60 fps
// data = mockPicture(width, height); _timer =
Timer.periodic(const Duration(milliseconds: 1000 ~/ 60), (timer) async {
if (methodId == 0) {
// Method.1: with MethodChannel // Method.1: with MethodChannel
// final res = data = mockPicture(width, height);
// await _textureRgbaRendererPlugin.onRgba(key, data!, height, width); final t1 = DateTime.now().microsecondsSinceEpoch;
// if (!res) { final res =
// debugPrint("WARN: render failed"); 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); final dataPtr = mockPicturePtr(width, height);
// Method.2: with native ffi // Method.2: with native ffi
Native.instance.onRgba( final t1 = DateTime.now().microsecondsSinceEpoch;
Pointer.fromAddress(texturePtr).cast<Void>(), dataPtr, width, height); Native.instance.onRgba(Pointer.fromAddress(texturePtr).cast<Void>(),
dataPtr, width, height);
final t2 = DateTime.now().microsecondsSinceEpoch;
setState(() {
time = t2 - t1;
});
malloc.free(dataPtr); malloc.free(dataPtr);
}
}); });
} }
@ -102,7 +121,9 @@ class _MyAppState extends State<MyApp> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
home: Scaffold( home: Scaffold(
body: Column( body: Center(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
@ -110,21 +131,33 @@ class _MyAppState extends State<MyApp> {
? const Offstage() ? const Offstage()
: Container( : Container(
alignment: Alignment.center, alignment: Alignment.center,
child: Container( decoration: const BoxDecoration(color: Colors.blue),
decoration: const BoxDecoration(color: Colors.black), // decoration: const BoxDecoration(color: Colors.black),
width: width.toDouble(), width: width.toDouble(),
height: height.toDouble(), height: height.toDouble(),
child: Texture(textureId: textureId)), child: Texture(textureId: textureId)),
),
Text( Text(
"texture id: $textureId, texture memory address: ${texturePtr.toRadixString(16)}"), "texture id: $textureId, texture memory address: ${texturePtr.toRadixString(16)}"),
IconButton( TextButton.icon(
label: const Text("play with texture (method channel API)"),
icon: const Icon(Icons.play_arrow), icon: const Icon(Icons.play_arrow),
onPressed: start, 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()
], ],
), ),
), ),
),
),
); );
} }
} }