Skip to content
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

OpenXR: Support composition layers based on Android surfaces #96185

Merged

Conversation

dsnopek
Copy link
Contributor

@dsnopek dsnopek commented Aug 27, 2024

This adds support for composition layers that get their 2D content from Android surfaces.

It depends on PR #96182 in order to allow GDScript to get the Android surface as a JavaObject, and then either directly interact with it via JavaClassWrapper, or, more likely, pass it to a JNISingleton registered by a custom Godot Android Plugin.

This could be used to, for example, play a video via Android libraries, and have the output displayed on a composition layer in XR.

You enable it by simply checking "Use Android Surface" on the OpenXRCompositionLayer, which will hide the "Viewport Layer" property, and show the "Android Surface Size" property:

Selection_203

Here's a GDScript example that simply fills the Android surface with red, which can be used to test that it's working:

func _process(delta):
	var android_surface = $OpenXRCompositionLayerQuad.get_android_surface()
	if android_surface:
		var color = JavaClassWrapper.wrap('android.graphics.Color')
		var canvas = android_surface.lockCanvas(null)
		if canvas != null:
			var red = color.rgb(1.0, 0.0, 0.0)
			canvas.drawColor(red)
		android_surface.unlockCanvasAndPost(canvas)

Again, in a real app you'd probably pass the surface to your Godot Android Plugin, but this is much easier to test. :-)

Marking as a DRAFT until PR #96182 is merged.

@dsnopek dsnopek added this to the 4.x milestone Aug 27, 2024
@dsnopek dsnopek requested review from m4gr3d and devloglogan August 27, 2024 20:19
@dsnopek dsnopek requested review from a team as code owners August 27, 2024 20:19
@dsnopek dsnopek marked this pull request as draft August 27, 2024 20:19
@dsnopek dsnopek force-pushed the openxr-composition-layer-android-surface branch 3 times, most recently from 835c18f to 2a58c28 Compare August 27, 2024 20:49
@dsnopek dsnopek force-pushed the openxr-composition-layer-android-surface branch 5 times, most recently from 9fc5611 to a01b75c Compare September 3, 2024 14:11
@dsnopek dsnopek marked this pull request as ready for review September 3, 2024 14:12
@dsnopek
Copy link
Contributor Author

dsnopek commented Sep 3, 2024

Taking out of draft now that PR #96182 is merged!

@akien-mga akien-mga changed the title [DRAFT] OpenXR: Support composition layers based on Android surfaces OpenXR: Support composition layers based on Android surfaces Sep 3, 2024
@fire
Copy link
Member

fire commented Sep 3, 2024

What does adding compositional layers look like? Can you make a video?

I don't know how to test this.

Edited:

There's a temporary failure, so I'll trigger a Github actions build again.

/usr/share/dotnet/sdk/8.0.401/NuGet.targets(174,5): error : The feed 'nuget.org [https://api.nuget.org/v3/index.json]' lists package 'JetBrains.Annotations.2019.1.3' but multiple attempts to download the nupkg have failed. The feed is either invalid or required packages were removed while the current operation was in progress. Verify the package exists on the feed and try again. [/home/runner/work/godot/godot/modules/mono/editor/GodotTools/GodotTools.sln]

@dsnopek
Copy link
Contributor Author

dsnopek commented Sep 3, 2024

@fire:

What does adding compositional layers look like? Can you make a video?
I don't know how to test this.

A composition layer is basically a floating panel with some 2D content on it (although, the panel can be a flat quad, or a slice of a cylinder or sphere which curves).

Testing is fairly simple:

  1. Make a 3D scene with all the pieces necessary for an XR app
  2. Add an OpenXRCompositionLayerQuad and check the "Use Android Surface" property
  3. Add a script to the scene with the code up in the description
  4. Run on an Android-based VR headset (should work on any Meta Quest or the Pico 4 per the OpenXR extension support page - I'm not sure about other headsets)
  5. If the composition layer is red (via the code in the description), then all is working!

Rather than making a new project, you could also modify the OpenXR composition layer demo.

There's also some good docs on composition layers - the difference here is that rather than getting the content from a SubViewport like those docs show, it comes from an Android surface instead. There's a couple of screenshots in there that show what a composition layer looks like, in case just having a visual would help.

@fire
Copy link
Member

fire commented Sep 3, 2024

Does this work with the HTCVive XR-Elite we have?

@dsnopek
Copy link
Contributor Author

dsnopek commented Sep 4, 2024

@fire I just tested it, and, it turns out that it does! However, it only supports quad composition layers (that is the OpenXRCompositionLayerQuad node), so be sure to use one of those.

@dsnopek dsnopek force-pushed the openxr-composition-layer-android-surface branch from a01b75c to 864851b Compare September 10, 2024 14:13
Copy link
Contributor

@devloglogan devloglogan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and seems to be working well! Code looks good to me, just have some extremely minor nits.

@dsnopek dsnopek force-pushed the openxr-composition-layer-android-surface branch from 864851b to 988d5ad Compare September 10, 2024 21:17
@dsnopek
Copy link
Contributor Author

dsnopek commented Sep 10, 2024

@devloglogan Thanks for the testing and review! I've made all the suggested changes in my latest push

@dsnopek dsnopek force-pushed the openxr-composition-layer-android-surface branch from 988d5ad to 804b3c9 Compare September 10, 2024 21:21
OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
ERR_FAIL_NULL_V(openxr_api, false);

// @todo We need a way to add to the next pointer chain.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a capability that depends on chaining or is it for future proofing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically to support XR_FB_android_surface_swapchain_create, which we'll want to be able to add from the vendor extension

Copy link
Contributor

@m4gr3d m4gr3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look great!

@m4gr3d m4gr3d modified the milestones: 4.x, 4.4 Sep 12, 2024
@dsnopek dsnopek force-pushed the openxr-composition-layer-android-surface branch from 804b3c9 to 3afa268 Compare September 12, 2024 02:15
@akien-mga akien-mga merged commit ea9ad8d into godotengine:master Sep 12, 2024
20 checks passed
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants