-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SurfaceTexture NDK bindings #262
Comments
there is gl, glutin and probably a dozen other rust crates that provide opengl bindings. I doubt it's in scope for this project, as it's not android specific. |
I've seen one pure rust glutin example that works on Android. However, if I have a Kotlin
that is, pass the |
For starters you could look at how Then, is there any reference code (ie. written in Kotlin, Java or C++) that converts a Kotlin |
@MarijnS95 in flutter, it creates a SurfaceTexture for me, and it looks like there's no way to get the |
Don't I need at least https://developer.android.com/ndk/reference/group/surface-texture? I think I need to at least attach this SurfaceTexture to the opengl context. The problem is, how do I get a SurfaceTexture on the Rust side given only the texId... ASurfaceTexture_fromSurfaceTexture(JNIEnv *env, jobject surfacetexture) is out of question because I want to call Rust code directly from Flutter, so no JVM, the JVM is only used to create the SurfaceTexture using Flutter's API |
I this hinges on You'll have to ask @dvc94ch for Flutter support, as I don't have the slightest clue. If Flutter hands you a Again, you should rewrite this issue and title to document what you want to achieve, rather than requesting something opaque. |
when building a flutter-rs plugin for video playback, flutter had a Texture widget. So you'd create an opengl texture, render the paced frames to the texture and pass the texture id to the Texture widget on the flutter side. When it comes to embedding flutter in a native android ui, don't really know. Don't have any experience with native android ui's. |
@dvc94ch ok, changed the title. I don't want to embed a flutter view on Android, I want to render to a flutter texture in Rust side. It turns out that to create a texture on Flutter you need to call kotlin/java code: override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else if (call.method=="create_texture") {
Log.d(LOG_TAG, "createVideoRenderer called")
val entry: TextureRegistry.SurfaceTextureEntry = texture_registry.createSurfaceTexture();
val surfaceTexture: surfaceTexture = entry.surfaceTexture();
//Store the surfaceTexture somewhere for rendering later It gives me a Anyways, there is still a major problem: I want to call Rust code through Dart's FFI, which completely ignores the JVM, but ASurfaceTexture_fromSurfaceTexture(JNIEnv *env, jobject surfacetexture) obviously uses JVM. I could call it from the Kotlin side, but will the returned ASurfaceTexture * be valid on the Rust side? |
ah I see. looks like what you're missing is some jni bindings to the android flutter embedder. your dart application uses the ffi to call into your rust code, which then uses the jni to create a surface texture. you can then render to the surface texture using opengl. |
@dvc94ch I'm a bit lost. If I load the rust .so through Flutter/Dart, then it wouldn't have access to the JVM, so I cannot call the android embedder, which is Kotlin/Java, right? Or is the embedder C++ and just called through Kotlin/Java? |
all android applications are java applications. your activity initializes the flutter android embedder, which initializes the flutter engine which loads your dart code. you need to initialize the ndk-context from your activity and then your dart code can call your rust code, which can use the ndk-context to create the surface texture. if you're asking how cargo-apk helps you with this, the answer is it doesn't. You need to use xbuild or roll your own. |
Yes, the Kotlin code: override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else if (call.method=="create_texture") {
Log.d(LOG_TAG, "createVideoRenderer called")
val entry: TextureRegistry.SurfaceTextureEntry = texture_registry.createSurfaceTexture();
val surfaceTexture: surfaceTexture = entry.surfaceTexture();
//Store the surfaceTexture somewhere for rendering later
//here which is called from Flutter as a plugin method call, initializes a int ASurfaceTexture_attachToGLContext( to attach whatever texture it creates, and also call things like ASurfaceTexture_updateTexImage(ASurfaceTexture *st) and ASurfaceTexture_attachToGLContext(ASurfaceTexture *st, uint32_t texName) to update/render the images. Is this what you meant? There's still the issue of having to call https://github.com/flutter/engine/blob/8ab4124eeff5b259153e56fadfaabf670655687b/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java#L417 every time a new frame should be rendered, which would be hard to do from Rust as it's Java code. Also, I find it very ugly and unsafe to use Kotlin to call C++ to get the pointer, then pass as a number to Flutter, which passes to Rust, which casts to Is there something I'm missing here? |
yep, we can add bindings for |
@dvc94ch it'd be nice for me to do a PR to get high level support for |
I'd create a new one called |
Do you think my plan is ok? It's very ugly to pass pointers around as numbers between JVM/C++ and then Rust, over Flutter. Also, everytime the rust side writes a new frame to a texture, it needs to call Flutter's |
Why don't you dlopen your rust so and initialize ndk-context from your kotlin activity? |
Because if I open the rust .so from kotlin then whenever I want to pass a message from flutter to rust it needs to go through a platform channel which is very slow and requires me to create maps and deep copy data, etc.
Therefore I have to open the rust side from dart but create the surface texture from kotlin, but also call markTextureAvailable's kotlin function from Dart every time there's a new frame.
…-------- Mensagem Original --------
Ativo 22 de abr. de 2022 23:57, David Craven escreveu:
Why don't you dlopen your rust so and initialize ndk-context from your kotlin activity?
—
Reply to this email directly, [view it on GitHub](#262 (comment)), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/ABSTHALSTIG45XTAZG7GQUTVGNRK3ANCNFSM5TTMI74Q).
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
You should be able to dlopen it from java and from flutter |
If I understood correctly, #267 adds support to get a native window from a surface texture, right? That is awesome! |
So wasn't 100% sure how multiple dlopens to the same library work:
|
@maxammann I didn't do for this purpose but it looks like yes, it's possible :) |
@lattice0 Did you succeed opening the library and initializing the pointers? If you can call C++ from Kotlin and perform whatever operation you happen to need there, you can also do that directly with Rust. |
@MarijnS95 gonna try soon |
Relevant links for this issue:
|
I think you're saying that it's possible to open twice and get it loaded as just one lib, so any pointer or static var in kotlin is also accessible in dart. I was thinking that dart could be loading it as a secondary lib, I don't know exactly how dynamic library loading works for Dart on Android, gotta research but this seems promising if true.
So I should load twice at startup and then I can store the surface pointer in a rust static map or something, so it can be acessed in both sides.
One strange thing though is that I'd have to have JNI stuff embedded into the rust .so but I think that's ok.
🤔🤔🤔🤔🤔🤔
…-------- Mensagem Original --------
Ativo 23 de abr. de 2022 00:11, David Craven escreveu:
You should be able to dlopen it from java and from flutter
—
Reply to this email directly, [view it on GitHub](#262 (comment)), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/ABSTHAKA64ZHTKLPWKWRMJ3VGNS6LANCNFSM5TTMI74Q).
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Are there plans to support OpenGL?The text was updated successfully, but these errors were encountered: