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 MonitorHandle::current_video_mode() #3802

Merged
merged 4 commits into from
Aug 7, 2024

Conversation

daxpedda
Copy link
Member

@daxpedda daxpedda commented Jul 19, 2024

After some discussion we decided its more useful to encode current monitor information into VideoModeHandle then move more information to MonitorHandle. To this end this PR implements MonitorHandle::current_video_mode().

Additionally MonitorHandle::size() and refresh_rate_millihertz() were removed, because it logically fits into video modes and not the monitor itself.

To improve the output, this PR also changes VideoModeHandle::refresh_rate_millihertz() to return a Option<NonZeroU32> and size() and bit_depth() an Option.

Follow up to #3801.

@daxpedda daxpedda added the S - enhancement Wouldn't this be the coolest? label Jul 19, 2024
@kchibisov
Copy link
Member

The bit_depth is more of a property of the active video format, so if I'd change the way handle works I'd probably just keep a handle and an option to get the active VideoMode for stuff like that.

The only information which is not video mode related is basically monitor name/position, etc, depth is not it.

@daxpedda
Copy link
Member Author

The bit_depth is more of a property of the active video format, so if I'd change the way handle works I'd probably just keep a handle and an option to get the active VideoMode for stuff like that.

The only information which is not video mode related is basically monitor name/position, etc, depth is not it.

That's a good point. That would solve the issue for Web as well.
Will look into it and adapt the PR.

@daxpedda daxpedda marked this pull request as draft July 20, 2024 19:14
@daxpedda
Copy link
Member Author

daxpedda commented Jul 20, 2024

The bit_depth is more of a property of the active video format, so if I'd change the way handle works I'd probably just keep a handle and an option to get the active VideoMode for stuff like that.

The only information which is not video mode related is basically monitor name/position, etc, depth is not it.

@kchibisov I started working on it right now and realized that this argument invalidates MonitorHandle currently having size and refresh rate.
Is there some story behind it or should we remove it in favor of current_video_mode() exposing the current size and refresh rate?

@kchibisov
Copy link
Member

Is there some story behind it or should we remove it in favor of current_video_mode() exposing the current size and refresh rate?

I think it was always like that, which doesn't make much sense. Probably it was like that for convenience or something. Just route everything into current_video_mode.

@daxpedda daxpedda force-pushed the monitor-bit-depth branch from d34b3dc to 521e071 Compare July 20, 2024 22:01
@daxpedda daxpedda changed the title Add MonitorHandle::bit_depth() Add MonitorHandle::current_video_mode() Jul 20, 2024
@daxpedda daxpedda marked this pull request as ready for review July 20, 2024 22:41
@daxpedda
Copy link
Member Author

daxpedda commented Jul 20, 2024

Is there some story behind it or should we remove it in favor of current_video_mode() exposing the current size and refresh rate?

I think it was always like that, which doesn't make much sense. Probably it was like that for convenience or something. Just route everything into current_video_mode.

Done.

I also changed VideoModeHandle::refresh_rate_millihertz() to return an Option.
Figured this was better then just falling back to 0. or 60_000. everywhere.

@daxpedda daxpedda force-pushed the monitor-bit-depth branch from 9b26bdd to 0f42269 Compare July 20, 2024 22:49
@daxpedda daxpedda mentioned this pull request Jul 20, 2024
@daxpedda daxpedda force-pushed the monitor-bit-depth branch from 0f42269 to 949e9d4 Compare July 23, 2024 13:58
src/changelog/unreleased.md Outdated Show resolved Hide resolved
@daxpedda daxpedda force-pushed the monitor-bit-depth branch from 949e9d4 to ac685d3 Compare July 23, 2024 17:59
@daxpedda daxpedda force-pushed the monitor-bit-depth branch from ac685d3 to 49d38ff Compare July 23, 2024 18:47
Copy link
Member

@MarijnS95 MarijnS95 left a comment

Choose a reason for hiding this comment

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

Approving the Android side given that all the weirdness in there (due to a lack of API in the NDK - we could call into Java for this though) was already there and is only moving in this patch but not fundamentally changing.

src/platform_impl/android/mod.rs Outdated Show resolved Hide resolved
src/platform_impl/android/mod.rs Outdated Show resolved Hide resolved
src/platform_impl/android/mod.rs Outdated Show resolved Hide resolved
src/platform_impl/android/mod.rs Show resolved Hide resolved
@daxpedda daxpedda requested review from kchibisov and MarijnS95 July 24, 2024 23:44
@daxpedda
Copy link
Member Author

Re-requested reviews because I changed VideoModeHandle::size() and bit_depth() to return an Option now.
Some backends obviously make use of that now instead of returning default values.

src/platform_impl/android/mod.rs Show resolved Hide resolved
src/platform_impl/android/mod.rs Outdated Show resolved Hide resolved
@daxpedda daxpedda force-pushed the monitor-bit-depth branch from 6d4bbd2 to a045f81 Compare July 25, 2024 11:47
@daxpedda daxpedda requested a review from MarijnS95 July 25, 2024 11:48
@daxpedda daxpedda force-pushed the monitor-bit-depth branch from a045f81 to dd70949 Compare July 25, 2024 11:50
ffi::context(&mut env)
.display_manager(&mut env)
.display(&mut env, self.id)
.unwrap()
Copy link
Member Author

Choose a reason for hiding this comment

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

This could panic, but is a known issue on all backends.
See #3258.

Copy link
Member

Choose a reason for hiding this comment

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

Well, if you function returns Option you can easily avoid panic.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will make a follow-up PR where I finally tackle #3258.

Copy link
Member

Choose a reason for hiding this comment

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

The thing is that monitor data, such as name, etc, is static more or less. The position could change, indeed, but generally unless we make all of that into events and lifetime I'm not sure we can do much.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point.
I'm happy to cache the name, but again, lets discuss it in a follow-up PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

@MarijnS95 let me know if you think anything in here might actually panic.

Copy link
Member

@kchibisov kchibisov left a comment

Choose a reason for hiding this comment

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

I'd probably separate android impl into separate commit and rebase 2 commits in the end, but up to you.

src/monitor.rs Outdated Show resolved Hide resolved
@daxpedda daxpedda force-pushed the monitor-bit-depth branch from dd70949 to 694fa95 Compare July 25, 2024 12:36
@daxpedda
Copy link
Member Author

I'd probably separate android impl into separate commit and rebase 2 commits in the end, but up to you.

I did change it up a bit to make it easier to review.
Thanks for the suggestion!

@MarijnS95
Copy link
Member

MarijnS95 commented Jul 25, 2024

Without going through the details, it looks like the Android implementation is quite overdone, now with JNI. Even if we discussed this before, I'd like to revise my statement.

When I proposed looking at Android functions and eventually calling them through JNI, it was meant as a hint that there appear to be matching APIs, and could eventually be used if there is a pressing need for it.
Using JNI also beats the user preference for a "native" activity (even though that itself is driven by some Java code, the underlying implementations that you talk against are all native once they've left the Activity lifecycle).


After all, having multiple displays on Android is extremely unlikely, and even for the current display, what is the user going to use this information for? Anything involving dimensions, pixel formats, refresh rate or color spaces will always end up going through the Surface. I think Wayland and specifically its color management protocol are opting for the same thing: everything goes through the surface, and the app doesn't and shouldn't really need to know about the display layout.

EDIT: The only thing I cannot find available in the NDK is a list of available frame rates to help the user guide if it makes sense to request a switch:
https://developer.android.com/media/optimize/performance/frame-rate


Summary: even if we could (as you have already shown): are you sure we want to start having and managing this blob of JNI "noise" in the Android backend? For likely zero actual use-case?

@daxpedda
Copy link
Member Author

Summary: even if we could (as you have already shown): are you sure we want to start having and managing this blob of JNI "noise" in the Android backend? For likely zero actual use-case?

AFAIC I was only doing this to prevent having an Option on this method and learning a bit of JNI on the way.
I'm happy to remove the whole thing but Android should then return no MonitorHandle at all to prevent this issue, exactly like it was for Web before #3801.

On the other hand I've already implemented it and we will need some JNI sooner or later anyway (I assume?). I don't know if a multi-monitor setup on Android would even work with the current API (not talking about the implementation) or if it actually needs a dedicated platform-specific API (which I would assume).

Let me know how you would like to proceed.

@daxpedda daxpedda force-pushed the monitor-bit-depth branch from 694fa95 to f938fc5 Compare July 25, 2024 13:23
@MarijnS95
Copy link
Member

Besides the effort you've put in, my preference goes out to not returning this information to the user at all. In case someone requests it we can always aim to re-land your commit from this PR (let's track it somewhere just in case) or keep it around as a reference for users desiring to implement this themselves?

I am not thinking of much that needs JNI now, except maybe the PRs that are ramping up towards IME support (whether that belongs in our out winit, I don't know)?

Either way, I've always desired to create an "android-support" kind of crate (under the rust-mobile umbrella) that abstracts away the JNI code (and could use precompiled Java/Kotlin classes for the real nasty stuff that is just too horrible to write in Rust via JNI). Details aside, we could move your JNI code there already?

@kchibisov
Copy link
Member

I'd note that if Android stuff is controversial, we can split it into separate PR, since the first 3 commits should be good to go.

@daxpedda
Copy link
Member Author

daxpedda commented Jul 26, 2024

Besides the effort you've put in, my preference goes out to not returning this information to the user at all. In case someone requests it we can always aim to re-land your commit from this PR (let's track it somewhere just in case) or keep it around as a reference for users desiring to implement this themselves?

I'm unsure why we would remove it in the first place if we would be willing to put it back in?
But maybe we just don't know if and how we want JNI in Winit yet?

EDIT: Considering we may want that as a reference at a future date daxpedda@f938fc5.

Either way, I've always desired to create an "android-support" kind of crate (under the rust-mobile umbrella) that abstracts away the JNI code (and could use precompiled Java/Kotlin classes for the real nasty stuff that is just too horrible to write in Rust via JNI). Details aside, we could move your JNI code there already?

Yes, lets discuss it on Matrix further!

I'd note that if Android stuff is controversial, we can split it into separate PR, since the first 3 commits should be good to go.

I'm gonna do the removal in a couple of hours or so.
But we should wait for somebody to test/properly review the MacOS changes.

@daxpedda daxpedda force-pushed the monitor-bit-depth branch 2 times, most recently from b41ae81 to c48740a Compare July 26, 2024 14:00
@daxpedda
Copy link
Member Author

@MarijnS95 PR is ready for your approval.

@MarijnS95
Copy link
Member

I'm unsure why we would remove it in the first place if we would be willing to put it back in?
But maybe we just don't know if and how we want JNI in Winit yet?

Yeah, this mostly. I don't want to commit to maintaining JNI inside winit now, especially given that there doesn't appear to be any usefulness in exposing Android displays and modes via winit. If someone comes up with a tangible use-case, we can re-evaluate, perhaps by having your code in a separate crate that's more generically usable by the Android Rust ecosystem (as proposed above, kind of like a sibling to the ndk crate but for Java-only interfaces), and call that from winit instead 👍

Copy link
Member

@MarijnS95 MarijnS95 left a comment

Choose a reason for hiding this comment

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

Android looks good!

src/platform_impl/android/mod.rs Show resolved Hide resolved
src/changelog/unreleased.md Show resolved Hide resolved
@daxpedda daxpedda force-pushed the monitor-bit-depth branch from c48740a to ae7550c Compare August 7, 2024 23:18
`VideoModeHandle::refresh_rate_millihertz()` and `bit_depth()` now return a `Option<NonZero*>`.
`MonitorHandle::position()` now returns an `Option`.
On Orbital `MonitorHandle::name()` now returns `None` instead of a dummy name.
Because we don't want to force all methods on `VideoModeHandle` to return `Option`, we decided to remove the already incomplete support in Android for both types.
@daxpedda daxpedda force-pushed the monitor-bit-depth branch from ae7550c to 3bab4ef Compare August 7, 2024 23:30
@daxpedda daxpedda merged commit 3bab4ef into rust-windowing:master Aug 7, 2024
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S - enhancement Wouldn't this be the coolest?
Development

Successfully merging this pull request may close these issues.

3 participants