feat: getTexturePtr

This commit is contained in:
Kingtous 2023-02-18 00:53:26 +08:00
parent 1a164ee5a0
commit 0f59fef831
5 changed files with 32 additions and 4 deletions

View file

@ -25,6 +25,7 @@ class _MyAppState extends State<MyApp> {
int width = 500;
int cnt = 0;
var key = 0;
int texturePtr = 0;
final random = Random();
Uint8List? data;
@ -34,6 +35,11 @@ class _MyAppState extends State<MyApp> {
_textureRgbaRendererPlugin.createTexture(key).then((textureId) {
if (textureId != -1) {
debugPrint("Texture register success, textureId=$textureId");
_textureRgbaRendererPlugin.getTexturePtr(key).then((value) {
setState(() {
texturePtr = value;
});
});
setState(() {
this.textureId = textureId;
});
@ -91,7 +97,8 @@ class _MyAppState extends State<MyApp> {
),
Align(
alignment: Alignment.topLeft,
child: Text("texture id: $textureId"),
child: Text(
"texture id: $textureId, texture memory address: $texturePtr"),
),
Align(
alignment: Alignment.bottomCenter,

View file

@ -25,4 +25,8 @@ class TextureRgbaRenderer {
return TextureRgbaRendererPlatform.instance
.onRgba(key, data, height, width);
}
Future<int> getTexturePtr(int key) {
return TextureRgbaRendererPlatform.instance.getTexturePtr(key);
}
}

View file

@ -1,3 +1,5 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
@ -32,9 +34,20 @@ class MethodChannelTextureRgbaRenderer extends TextureRgbaRendererPlatform {
@override
Future<bool> onRgba(int key, Uint8List data, int height, int width) async {
assert(data.length == height * width * 4);
return await methodChannel.invokeMethod<bool>('onRgba',
{"data": data, "height": height, "width": width, "key": key}) ??
false;
}
@override
Future<int> getTexturePtr(int key) async {
// TODO: currently only supports Windows.
if (!Platform.isWindows) {
return -1;
}
final ptr =
await methodChannel.invokeMethod('getTexturePtr', {"key": key}) ??
false;
return ptr;
}
}

View file

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

View file

@ -87,8 +87,8 @@ void TextureRgbaRendererPlugin::HandleMethodCall(
}
else {
// Return an address.
void* rgba = (void*) textures_[key].get();
result->Success(flutter::EncodableValue(rgba));
size_t rgba = reinterpret_cast<size_t>((void*) textures_[key].get());
result->Success(flutter::EncodableValue(int64_t(rgba)));
}
}
else {