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

Add option to create headless windows #3835

Closed
wants to merge 3 commits into from

Conversation

Wcubed
Copy link
Contributor

@Wcubed Wcubed commented Jan 31, 2022

This adds the ability to pass None to a Window's raw_window_handle field.
Includes a test to demonstrate how this allows for headless testing of UI related stuff which requires windows. In this case: the positioning of a button.

Closes #3754

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Jan 31, 2022
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Feature A new feature, making something new possible and removed S-Needs-Triage This issue needs to be labelled labels Jan 31, 2022
Copy link
Contributor

@Shatur Shatur left a comment

Choose a reason for hiding this comment

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

Thank you!

@@ -294,6 +287,17 @@ impl Plugin for RenderPlugin {
}
}

fn try_create_surface(app: &mut App, wgpu_instance: &wgpu::Instance) -> Option<Surface> {
Copy link
Member

Choose a reason for hiding this comment

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

This should return a Result, not an Option IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can see why that would make sense. Then the caller would know why they didn't get a surface (no window resource for example). On the other hand, we don't actually use the reason for not getting a surface anywhere (yet?).
If this returned an error, currently we'd have to convert it back into an Option to pass it into RequestAdapterOptions

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, that's fair. Since this isn't pub that's probably fine for now.

let surface = window_surfaces
.surfaces
.entry(window.id)
.or_insert_with(|| unsafe {
Copy link
Member

Choose a reason for hiding this comment

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

Missing safety comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. As far as I can tell, it has never had a safety comment. I am also not experienced enough with the codebase to know why this unsafe is OK here (or maybe it even isn't, and we haven't noticed yet).

Copy link
Member

Choose a reason for hiding this comment

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

Alright. @cart / @superdump if you can quickly explain why this is safe during review that would be appreciated, but it shouldn't block this PR.

Copy link
Member

Choose a reason for hiding this comment

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

from https://docs.rs/wgpu/latest/wgpu/struct.Instance.html#safety-2:

Raw Window Handle must be a valid object to create a surface upon and must remain valid for the lifetime of the returned surface.

Copy link
Member

Choose a reason for hiding this comment

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

These safety requirements have always been slightly spooky. That is, imo a reasonable safety comment here is ¯\_(ツ)_/¯ :).

To my knowledge, it's safe, but it's very difficult to prove that. I don't think winit documents that this requirement is met, so any use of this is mostly best effort.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Excellent! This a very useful functionality, and generally looks very well done. I've left a couple nits.

IMO we can expand the "How to test UI" test in further PRs.

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 1, 2022
@alice-i-cecile
Copy link
Member

@Wcubed I'd like to merge this soon; can you please rebase?

@Shatur
Copy link
Contributor

Shatur commented Apr 25, 2022

@alice-i-cecile cart didn't like the idea last time I asked him :(
But maybe #4530 could replace it?

@alice-i-cecile
Copy link
Member

Alright, fair. I'm going to close this out for now then; we can try again once #4530 is in, which does have consensus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The ability to create fake windows
5 participants