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 =
// Method.1: with MethodChannel Timer.periodic(const Duration(milliseconds: 1000 ~/ 60), (timer) async {
// final res = if (methodId == 0) {
// await _textureRgbaRendererPlugin.onRgba(key, data!, height, width); // Method.1: with MethodChannel
// if (!res) { data = mockPicture(width, height);
// debugPrint("WARN: render failed"); final t1 = DateTime.now().microsecondsSinceEpoch;
// } final res =
final dataPtr = mockPicturePtr(width, height); await _textureRgbaRendererPlugin.onRgba(key, data!, height, width);
// Method.2: with native ffi final t2 = DateTime.now().microsecondsSinceEpoch;
Native.instance.onRgba( setState(() {
Pointer.fromAddress(texturePtr).cast<Void>(), dataPtr, width, height); time = t2 - t1;
malloc.free(dataPtr); });
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<Void>(),
dataPtr, width, height);
final t2 = DateTime.now().microsecondsSinceEpoch;
setState(() {
time = t2 - t1;
});
malloc.free(dataPtr);
}
}); });
} }
@ -102,27 +121,41 @@ 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(
crossAxisAlignment: CrossAxisAlignment.center, child: SingleChildScrollView(
mainAxisAlignment: MainAxisAlignment.center, child: Column(
children: [ crossAxisAlignment: CrossAxisAlignment.center,
textureId == -1 mainAxisAlignment: MainAxisAlignment.center,
? const Offstage() children: [
: Container( textureId == -1
alignment: Alignment.center, ? const Offstage()
child: Container( : Container(
decoration: const BoxDecoration(color: Colors.black), alignment: Alignment.center,
decoration: const BoxDecoration(color: Colors.blue),
// 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)}"), TextButton.icon(
IconButton( 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()
],
), ),
], ),
), ),
), ),
); );