Repository navigation
Description
The current RedrawRequested event is currently hard to understand, which makes it difficult to implement and difficult to use. This issue will outline why I believe that is the case, and how I think we should fix it.
The current problems
RedrawRequested is extremely inconsistent with the rest of our API. This is exhibited in a couple ways:
- It's inconsistent with itself, since it sometimes provides ordering guarantees.
- It's explicitly guaranteed to be the last event emitted if
request_redrawis called, but isn't explicitly guaranteed to be the last event if the OS requested the redraw.
- It's explicitly guaranteed to be the last event emitted if
- It's inconsistent with
WindowEvent, since no other window event provides any sort of ordering guarantees. - It's inconsistent with
EventsCleared, since it can be dispatched afterEventsClearedif queued withrequest_redrawduring theEventsClearedhandler.
Some of that inconsistency is intentional (point 3), and some isn't (point 1), but the end result is that the API is difficult to explain and difficult to implement. Even beyond the complexity those inconsistencies introduce, the current model can result in duplication of RedrawRequested events when using the recommended event loop structure described in the root docs. Consider the following chain of events, with an application using the linked event loop structure:
- The user resizes the window, resulting in the OS triggering a
RequestRedrawevent. - The event loop dispatches
RequestRedrawbetweenNewEventsandEventsCleared, as part of the standard control flow. EventsClearedgets dispatched, upon which the user updates the application's state and callsrequest_redraw.RequestRedrawgets dispatched immediately afterEventsCleared.
The above situation has a couple problems: the first RedrawRequested is dispatched before the application has a chance to update its internal state, and the application goes through the expensive process of rendering a frame twice in a single iteration of the event loop. Ideally, our API wouldn't exhibit either issue, allowing the user to update their application state before all redraws occur and allowing Winit to unify redraw requests from the user and OS into a single event.
Proposed changes
- Rename
EventsClearedtoMainEventsCleared - Remove
RedrawRequestedfromWindowEvent. - Add
RedrawRequested(WindowId)toEvent.- Guarantee that
RedrawRequestedget dispatched betweenMainEventsClearedandRedrawEventsCleared.
- Guarantee that
- Add
RedrawEventsClearedtoEvent.
The rough event loop would end up behaving something like this (adapted from @icefoxen's example in #1032):
let event_loop_windows = /* event loop windows */;
let mut control_flow = ControlFlow::Poll;
event_handler(NewEvents(StartCause::Init), &event_loop_windows, &mut control_flow);
while control_flow != ControlFlow::Exit {
event_handler(NewEvents(/* get start cause */), &event_loop_windows, &mut control_flow);
for e in (window events, user events, device events) {
event_handler(e, &event_loop_windows, &mut control_flow);
}
event_handler(MainEventsCleared, &event_loop_windows, &mut control_flow);
for window_id in (redraw_windows) {
event_handler(RedrawRequested(window_id));
}
event_handler(RedrawEventsCleared, &event_loop_windows, &mut control_flow);
/* wait or poll, as the control_flow dictates */
}
event_handler(LoopDestroyed, &event_loop_windows, &mut control_flow);This results in numerous usability improvements over the current model:
RedrawRequested's status as a special event is made more clear, as it's structurally distinct from standard window events.RedrawRequestedis now guaranteed to be dispatched after event processing and application update code has been run, fitting it more naturally into the application update/render loop.- Winit can now unify all OS-requested and user-requested redraw events, avoiding redundant redrawing.
Feedback is encouraged on the above designs, especially WRT naming - I'm not entirely happy with the names I've provided, and mainly included them because I can't think of anything better right now. There may be a better solution to the current API's problems, but I'm presently unable to think of one and we should certainly address them before 0.20's full release.
Activity
icefoxen commentedon Jul 11, 2019
My gut impulse is that you're trying to make winit do more than it needs to here. It seems like, from the application's perspective, the right thing to do is set a flag if you get a resize event, and then when you get to the point when you decide whether or not to draw, you draw if your application needs to redraw, or if the OS has told you to redraw via a resize event. Either way, from the application writer's perspective, your drawing only happens in one place and only when it's actually necessary.
I will have to think about this more, since I still haven't internalized the new event loop as much as you have.
aloucks commentedon Jul 11, 2019
Part of the confusion with
RedrawRequestedis that it combines separate concerns: window damage/refresh and the users request to turn the event loop. I think it also interacts withWaitandWaitUntilbut I'm getting confused thinking about it while looking at the example (why do I need torequest_redrawand useWaitUntilwith a timeout?)Have you considered decoupling these?
I think ideally there would be only one "meta" event that indicates that the event loop has come full circle, but this proposal is adding more. What if
EventsClearedwas eliminated? Would that solve the problem with the (lack of) ordering guarantee? Could you just callrequest_redrawfromNewEventsand run all your app logic there as well?Osspial commentedon Jul 12, 2019
@icefoxen That's actually not entirely correct! Doing that works fine for games, but it leads to visual issues when resizing the window (at least on Windows) which isn't great for desktop applications. If you want to avoid those visual issues, you must perform all redrawing inside of
RedrawRequested, and this proposal lets you do that more easily.There's a bit of vagueness in my description there, since it's harder to concretely describe what happens without concrete examples. Luckily, I've prepared some! Check out this repository and run the examples to (hopefully) see what I'm talking about - they illustrate my point nicely on my Windows machine, and I'd be curious to see what they look like on other platforms.
resize_smoothlooks like this, since it handles resizing where the OS wants it to: https://gfycat.com/belatedperfumedjanenschiaThe window's content in
resize_laggylags behind the window's resize, since it redraws after the OS has requested a redraw: https://gfycat.com/fancypassionatecaudata@aloucks Wait, what? They are decoupled.
RedrawRequestedis intended exclusively for redrawing the window, not turning the event loop - notifying the user of a event loop's completion isEventsCleared's job. When functioning properly, it shouldn't interact withWaitorWaitUntilat all. If there's confusion here, that's a documentation deficit.Here's my rationale behind all the meta-events, for clarity:
NewEventsnotifies the user why the event loop woke back up. This doesn't do a whole lot whencontrol_flowis set toPoll, but when the user is usingWaitUntilit helps them figure out if the loop was woken up because more events were received or because their timer expired.EventsCleared(orMainEventsCleared) notifies the user that all events that affect application state have been dispatched, and that it's safe to aggregate the events they've received into an application update. This does not always imply that the application needs to be redrawn. Desktop applications are able to be extremely conservative about when they redraw, and for the sake of resource usage should only redraw when absolutely necessary. Exposing this separately from redrawing makes that significantly easier.RedrawEventsCleared... well, I wouldn't expect it to do a whole lot. It is genuinely useful in some circumstances (this simplifies an async event loop's internal logic significantly, for example), but the primary purpose for most people is to provide an obvious book-end for the redraw events and clarify that they happen afterMainEventsCleared.aloucks commentedon Jul 12, 2019
What is
request_redrawneeded for? I thought it was basically the equivalent ofglfwPostEmptyEvent()but triggers as window refresh. I can see how this would be useful for waking up the event loop from another thread, but I'm trying to understand why it's needed in the single-threaded scenario.After thinking more about my initial suggestion (single meta event), I can see where it might be problematic for the non-game-loop style app.
I think the problem though is still centered around
request_redrawand trying to useRedrawRequestedas the sole place for drawing (and how it fits in the overall API). It makes sense in the UI paint-on-window-damage app, but maybe not as much on the traditional game loop.Here's another thought:
request_redrawRedrawRequstedtoRefresh(now this is only triggered from OS messages)And adjust the API usage recommendation to something more like:
And finally (some bikeshedding)...
Rename:
NewEventstoEventsBeginEventsClearedtoEventsEndI think this naming makes them stand out more as being very closely related.
EDIT 1:
I definitely think
request_redrawis what's creating this wrinkle in the API. Game loops and UI apps are just fundamentally different when it comes to their repaint frequency. I think the attempt to make a game loop react to paint messages is what's making things a little more complicated than they need to be.EDIT 2:
I just changed one of my projects and could definitely see that resizing wasn't as nice 😢. Back to the drawing board I guess.
to
So I'm guessing that repainting from
RedrawRequestedis required on Windows so that it's a reaction toWM_PAINT?Osspial commentedon Jul 12, 2019
@aloucks
request_redrawis used so that all redraw logic can be placed in theRedrawRequestedhandler, unifying application-requested and OS-requested redraws into a single event that can cleanly handle both cases without the need for additional complexity. The intention is, if you do redrawing in there, you don't have to think about how to handle OS-requested redrawing differently from application-requested redrawing, or where to place application update code in relation to application redraw code, or how to avoid redrawing twice if the OS requests a redraw and you're redrawing inEventsCleared. All of that complexity gets abstracted away, all rendering processing is treated identically, and everything Just Works without having to think about the underlying semantics of the event loop or the platform. It isn't strictly necessary, but exposing it properly makes everything so much nicer and easy-to-handle, and I don't want to throw all of that away.The issue is, the current API can't do that because it doesn't always unify application-requested and OS-requested redraws. The redesign proposed here fixes that.
That is correct. I don't know if it has the exact same semantics on other platforms, but I'd be extremely surprised if no other platform made similar assumptions that resulted in similar behavior when
RedrawRequestedis handled improperly.aloucks commentedon Jul 12, 2019
Could this be fixed by injecting another
NewEvents?Osspial commentedon Jul 12, 2019
Injecting another
NewEventsimplies dispatching anotherEventsCleared, callingrequest_redrawagain, dispatchingNewEventsagain, ad infinitum. That breaksWaitandWaitUntilsince it creates an always-running loop, and also means that redrawing doesn't logically occur in the same event loop iteration that the application update did, which is needlessly confusing.goddessfreya commentedon Jul 17, 2019
@Osspial Can we also pass a vector of damage reports with
RedrawRequested? Unless you got another idea on how to expose them?Osspial commentedon Jul 17, 2019
@zegentzy It's definitely worthwhile to expose the damage report. We may want to expose it via some sort of platform-specific
UpdateRegionstruct, which would allow us to more easily expose things like the region bounding rectangle, but that's a design I haven't put much thought into and may or may not be the correct way to go about exposing it.goddessfreya commentedon Jul 17, 2019
Well, for Linux, we got these three things:
https://tronche.com/gui/x/xlib/events/exposure/expose.html
https://people.freedesktop.org/~whot/wayland-doxygen/wayland/Server/structwl__surface__interface.html#ad3cff78b33a03a17c9c148edc44d14eahttps://people.freedesktop.org/~whot/wayland-doxygen/wayland/Server/structwl__surface__interface.html#a922bb3cc09d0819c04cd02f79e79b17f
Afaik, on windows, you just need to call this function here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getupdaterect
And macOS seams to pass a rect:
winit/src/platform_impl/macos/view.rs
Line 283 in a195ce8
Given the similarities, I propose the following:
DamageRegion, which contains the size and position of the damaged rect.DamageRegionshould expose some basic functions, like union and difference.request_redrawshould take aDamageRegionRedrawRequestedshould also contain a vector ofDamageRegions.I think this solution is simple enough.
Unresolved issues:
DamageRegions partially overlap?DamageRegions completely overlap?elinorbgr commentedon Jul 18, 2019
11 remaining items
goddessfreya commentedon Jul 26, 2019
@jlogandavison Thanks for contacting me.
mtak- commentedon Jul 31, 2019
If I understand correctly, the new API expects that other platforms with that assumption will also send
RedrawRequestedout afterMainEventsCleared. I haven't done much nativeUIKitorAppKitprogramming, but os-driven redraw events do occur during the main event loop. Macos enters a modal event loop during a resize which meansEventsClearedis only sent out when the user finishes the resize (when mouse1 is released). I think the ordering ofRedrawRequestedin the new API is a quirk of Windows more than anything - which is OK if macos/other platforms don't have the same redrawing assumptions as windows and/or do have the same event ordering as windows.goddessfreya commentedon Jul 31, 2019
Adding
Blocking a Release, as I think these changes should be implemented before we finalize 0.20.Implement changes to `RedrawRequested` event (#1062)
Osspial commentedon Aug 11, 2019
Really? I've looked through the cocoa documentation, and it seems to suggest otherwise:
I'd consider that a major bug that should be fixed. The Windows API exhibits similar behavior by default, and we do some fairly significant hoop-jumping to avoid exposing that behavior publicly.
Admittedly, this redesign assumes that that's the case. However, since it's pretty much the only sensible way to dispatch redraw events in the context of the broader application, I'd be extremely surprised if other platforms didn't enforce similar ordering.
mtak- commentedon Aug 12, 2019
Sorry, the apple situation isn't as bad as I made it out to be.
iOS's docs say a similar thing as macos's:
I wasn't considering redraw events triggered by
setNeedsDisplayas being os-driven. I've seendrawRect:in between other events 1) when aUIViewis first displayed and 2) on background/foreground.I haven't yet tested macos, but it sounds like they typically send redraws at the top of the eventloop? I guess that can be worked around somehow.
Implement changes to `RedrawRequested` event (rust-windowing#1062)
Implement changes to `RedrawRequested` event (rust-windowing#1062)
Implement changes to `RedrawRequested` event (#1062)
Implement changes to `RedrawRequested` event (#1062)
Implement changes to `RedrawRequested` event (#1062)
Implement changes to `RedrawRequested` event (#1062)
fix(sdl3): coalesce redraw requests to fix a real "Not Responding" fr…