Skip to content

Implement changes to RedrawRequested event - #1062

Merged
goddessfreya merged 4 commits into
rust-windowing:redraw-requested-2.0from
murarth:redraw-reform
Jul 31, 2019
Merged

goddessfreya merged 4 commits into
rust-windowing:redraw-requested-2.0from
murarth:redraw-reform

Conversation

@murarth

@murarth murarth commented Jul 23, 2019

Copy link
Copy Markdown
Contributor

Implements the changes described in #1041 for the X11 platform and for
platform-independent public-facing code.

  • Tested on all platforms changed
  • cargo fmt has been run on this branch
  • Added an entry to CHANGELOG.md if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality
  • Updated feature matrix, if new features were added or implemented

Implements the changes described in rust-windowing#1041 for the X11 platform and for
platform-independent public-facing code.
@murarth

murarth commented Jul 23, 2019

Copy link
Copy Markdown
Contributor Author

I've implemented the proposed changes for X11 only, inserting an unimplemented!() in the Wayland code so that this code will compile. Thus, this changeset will only compile on Linux and will only run as expected on X11.

I'm not sure how incomplete implementations are usually done, so please let me know if I should do something differently with this PR.

@goddessfreya goddessfreya left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'll give this a test run later this week when I finally get X11 working with glutin v0.23... so might be a while.

Anyways, until then, have a singular "Yike" :).

if refresh {
sink.send_window_event(crate::event::WindowEvent::RedrawRequested, wid);
unimplemented!()
//sink.send_window_event(crate::event::WindowEvent::RedrawRequested, wid);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yike.

@Osspial
Osspial changed the base branch from master to redraw-requested-2.0 July 23, 2019 02:37
@goddessfreya

Copy link
Copy Markdown
Contributor

Yeah, it would be a very long while...

@FliegendeWurst, since you are on the testers list, would you like to test this? Please make sure the examples don't freeze, and the ordering of the events is consistent with what is proposed here: #1041

Thanks :)

@FliegendeWurst

FliegendeWurst commented Jul 26, 2019 •

Copy link
Copy Markdown

The 'request_redraw' example no longer compiles since the event naming changed. Other examples ('cursor', 'cursor_grab', 'transparent', 'timer', 'window_icon', 'window_run_return') still seem to work. I adjusted the 'resizable' example to print debug information on all events. You can download the full log below. It seems like the event ordering works as intended, but some events are sometimes in two separate 'event cycles' (see comments below) and in the same cycle at other times. I'm not sure that's intended.

NewEvents(WaitCancelled { start: Instant { tv_sec: 3708, tv_nsec: 833814233 }, requested_resume: None })  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: Motion { axis: 0, value: -23.0 } }  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: Motion { axis: 1, value: -5.0 } }  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: MouseMotion { delta: (-23.0, -5.0) } }  
WindowEvent { window_id: WindowId(X(WindowId(153092097))), event: CursorEntered { device_id: DeviceId(X(DeviceId(2))) } }  
WindowEvent { window_id: WindowId(X(WindowId(153092097))), event: CursorMoved { device_id: DeviceId(X(DeviceId(2))), position: LogicalPosition { x: 527.0769230769231, y: 138.46153846153848 }, modifiers: ModifiersState { shift: false, ctrl: false, alt: false, logo: false } } }  
WindowEvent { window_id: WindowId(X(WindowId(153092097))), event: CursorLeft { device_id: DeviceId(X(DeviceId(2))) } }  
MainEventsCleared  
RedrawEventsCleared  
NewEvents(WaitCancelled { start: Instant { tv_sec: 3708, tv_nsec: 835169670 }, requested_resume: None })  
WindowEvent { window_id: WindowId(X(WindowId(153092097))), event: Resized(LogicalSize { width: 525.2307692307693, height: 200.30769230769232 }) }  
MainEventsCleared  
RedrawRequested(WindowId(X(WindowId(153092097)))) // Redraw immediately after Resized event  
RedrawEventsCleared  
NewEvents(WaitCancelled { start: Instant { tv_sec: 3708, tv_nsec: 844635764 }, requested_resume: None })  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: Motion { axis: 0, value: -67.0 } }  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: Motion { axis: 1, value: 0.0 } }  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: MouseMotion { delta: (-67.0, 0.0) } }  
MainEventsCleared  
RedrawEventsCleared  
NewEvents(WaitCancelled { start: Instant { tv_sec: 3708, tv_nsec: 856688181 }, requested_resume: None })  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: Motion { axis: 0, value: -83.0 } }  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: Motion { axis: 1, value: 3.5714285713620484 } }  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: MouseMotion { delta: (-83.0, 3.5714285713620484) } }  
MainEventsCleared  
RedrawEventsCleared  
NewEvents(WaitCancelled { start: Instant { tv_sec: 3708, tv_nsec: 867070153 }, requested_resume: None })  
MainEventsCleared  
RedrawEventsCleared  
NewEvents(WaitCancelled { start: Instant { tv_sec: 3708, tv_nsec: 868223645 }, requested_resume: None })  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: Motion { axis: 0, value: -92.0 } }  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: Motion { axis: 1, value: 3.5714285713620484 } }  
DeviceEvent { device_id: DeviceId(X(DeviceId(10))), event: MouseMotion { delta: (-92.0, 3.5714285713620484) } }  
MainEventsCleared  
RedrawEventsCleared  
NewEvents(WaitCancelled { start: Instant { tv_sec: 3708, tv_nsec: 868638730 }, requested_resume: None })  
WindowEvent { window_id: WindowId(X(WindowId(153092097))), event: Resized(LogicalSize { width: 493.84615384615387, height: 200.30769230769232 }) }  
MainEventsCleared  
// no redraw event after resized event?  
RedrawEventsCleared  
NewEvents(WaitCancelled { start: Instant { tv_sec: 3708, tv_nsec: 868749321 }, requested_resume: None })  
MainEventsCleared  
RedrawRequested(WindowId(X(WindowId(153092097)))) // here it is, apparently  
RedrawEventsCleared

log.txt

@goddessfreya

Copy link
Copy Markdown
Contributor

Yeah, it should be on the same cycle, otherwise applications that use both Poll and issue request_redraw on every MainEventsCleared will do one unnecessary redraw. I can't think of an obvious solution to this beyond just waiting for a RedrawRequested event to land, which seams unreasonable.

Maybe we can make it so that MainEventsCleared only gets sent if there are new main events? Same with RedrawEventsCleared? And then we can add an AllEventsCleared, which is sent after all the events are cleared (both redraw and main), where users can preferably put stuff like setting control_flow, or checking some logic they want done every time the events loop is about to end a cycle.
How do you feel about this solution, @Osspial?

@murarth

murarth commented Jul 26, 2019

Copy link
Copy Markdown
Contributor Author

I've fixed the example. I'm not sure what can be done about the issue of related events being split across separate event loop iterations.

@Osspial

Osspial commented Jul 29, 2019 •

Copy link
Copy Markdown
Contributor

IIRC X11 is an asynchronous API. Perhaps we're seeing the redraw event splitting because of synchronization issues between Winit and the X11 server, roughly as so?:

  1. X11 posts a Resize event
  2. Winit drains the event buffer, pulling the Resize event
  3. X11 posts a Redraw event while Winit is processing the event buffer
  4. Winit finishes event processing, starts a new iteration of the event loop, checks the event buffer, and finally sees the Redraw event
  5. Winit dispatches Redraw in a new iteration of the event loop

That's based on my extremely rough understanding of X11's structure, so it may or may not be accurate, but I think it seems relatively plausible.

Also re:@zegentzy - I'm strongly opposed to only sending MainEventsCleared/RedrawEventsCleared when the relevant events exist. Those events are incredibly useful as marker events that help applications react to the event loop, and dispatching them inconsistently means downstream applications that use a Poll model (like games) can't put their update logic in MainEventsCleared. That'd be a major usability headache.

@goddessfreya

goddessfreya commented Jul 31, 2019 •

Copy link
Copy Markdown
Contributor

@Osspial There is no guarantee that after an ConfigureNotify notify event (Resized) we will receive a Expose event (RedrawRequested). For that reason, we simply can't wait till the Expose event arrives, as it may never.

Referring to the docs, every X function that X clients may call says that XYZ may generate Expose events. For example, two functions of interest are:

[...] Changing the size of a mapped window may lose its contents and generate Expose events. [...]

-- XResizeWindow, emphasis mine.

[...] Moving and resizing a mapped window may generate an Expose event on the window. [...]

-- XMoveResizeWindow, emphasis mine.

There is no reason to suggest that Expose events caused by the X server/compositor/user/whatever are anymore guaranteed. In fact:

The X server can report Expose events to clients wanting information about when the contents of window regions have been lost. The circumstances in which the X server generates Expose events are not as definite as those for other events. [then it goes on to list some reasons that the event may be generated.]

-- §10.9.1 Expose Events, emphasis mine.

I did some googling, and I found this document:

[...] A ConfigureNotify event is always followed by one or more Expose events.

-- http://csweb.cs.wfu.edu/~torgerse/Kokua/Irix_6.5.21_doc_cd/usr/share/Insight/library/SGI_bookshelves/SGI_Developer/books/XLib_PG/sgi_html/ch03.html

However, that claim seems to contradict this mozilla bug here which mentions the expose event arriving before the configure one: https://bugzilla.mozilla.org/show_bug.cgi?id=1280653#c11

Fwiw, I tested this out with this small patch against master (not this PR) and found that an Expose event always happened after the ConfigureNotify w/ i3wm, however, there is no reason to believe that this will hold true for all hardware/software configurations. Waiting for an Expose event seams destined to cause issues on some obscure setups.

diff --git a/examples/window.rs b/examples/window.rs
index 12e0593e..e0949ba0 100644
--- a/examples/window.rs
+++ b/examples/window.rs
@@ -13,13 +13,21 @@ fn main() {
         .unwrap();
 
     event_loop.run(move |event, _, control_flow| {
-        println!("{:?}", event);
 
         match event {
             Event::WindowEvent {
                 event: WindowEvent::CloseRequested,
                 window_id,
             } if window_id == window.id() => *control_flow = ControlFlow::Exit,
+            Event::WindowEvent {
+                event: WindowEvent::Resized(_),
+                window_id,
+            } => println!("{:?}", event),
+            Event::WindowEvent {
+                event: WindowEvent::RedrawRequested,
+                window_id,
+            } => println!("{:?}", event),
+            Event::EventsCleared => println!("{:?}", event),
             _ => *control_flow = ControlFlow::Wait,
         }
     });

In fact, I could reproduce this redraw splitting on master, so this isn't even a regression from what's currently happening on master:

EventsCleared
WindowEvent { window_id: WindowId(X(WindowId(69206017))), event: Resized(LogicalSize { width: 377.53846153846155, height: 375.69230769230774 }) }
WindowEvent { window_id: WindowId(X(WindowId(69206017))), event: RedrawRequested }
EventsCleared
EventsCleared
WindowEvent { window_id: WindowId(X(WindowId(69206017))), event: Resized(LogicalSize { width: 357.2307692307693, height: 359.0769230769231 }) }
WindowEvent { window_id: WindowId(X(WindowId(69206017))), event: RedrawRequested }
EventsCleared
EventsCleared
EventsCleared
WindowEvent { window_id: WindowId(X(WindowId(69206017))), event: Resized(LogicalSize { width: 343.3846153846154, height: 348.0 }) }
EventsCleared
WindowEvent { window_id: WindowId(X(WindowId(69206017))), event: RedrawRequested }
EventsCleared

Given that, I'm approving this PR.

@goddessfreya goddessfreya left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lgtm, just fix the examples in src/lib.rs so CI stops complaining.

@murarth

murarth commented Jul 31, 2019

Copy link
Copy Markdown
Contributor Author

Fixed the doctests. The remaining CI failures are on Windows and Mac.

@goddessfreya

goddessfreya commented Jul 31, 2019 •

Copy link
Copy Markdown
Contributor

One more change, we should only issue RequestRedraw if Expose's count == 0.

EDIT: Rationale:

[...] Simple applications that do not want to optimize redisplay by distinguishing between subareas of its window can just ignore all Expose events with nonzero counts and perform full redisplays on events with zero counts.

-- §10.9.1 Expose Events

@goddessfreya
goddessfreya merged commit db89e61 into rust-windowing:redraw-requested-2.0 Jul 31, 2019
@murarth
murarth deleted the redraw-reform branch July 31, 2019 06:36
hecrj pushed a commit to hecrj/winit that referenced this pull request Aug 26, 2019
* Implement changes to `RedrawRequested` event

Implements the changes described in rust-windowing#1041 for the X11 platform and for
platform-independent public-facing code.

* Fix `request_redraw` example

* Fix examples in lib docs

* Only issue `RedrawRequested` on final `Expose` event
hecrj pushed a commit to hecrj/winit that referenced this pull request Aug 31, 2019
* Implement changes to `RedrawRequested` event

Implements the changes described in rust-windowing#1041 for the X11 platform and for
platform-independent public-facing code.

* Fix `request_redraw` example

* Fix examples in lib docs

* Only issue `RedrawRequested` on final `Expose` event
Osspial pushed a commit that referenced this pull request Oct 3, 2019
* Implement changes to `RedrawRequested` event

Implements the changes described in #1041 for the X11 platform and for
platform-independent public-facing code.

* Fix `request_redraw` example

* Fix examples in lib docs

* Only issue `RedrawRequested` on final `Expose` event
Osspial pushed a commit that referenced this pull request Oct 23, 2019
* Implement changes to `RedrawRequested` event

Implements the changes described in #1041 for the X11 platform and for
platform-independent public-facing code.

* Fix `request_redraw` example

* Fix examples in lib docs

* Only issue `RedrawRequested` on final `Expose` event
Osspial pushed a commit that referenced this pull request Dec 22, 2019
* Implement changes to `RedrawRequested` event

Implements the changes described in #1041 for the X11 platform and for
platform-independent public-facing code.

* Fix `request_redraw` example

* Fix examples in lib docs

* Only issue `RedrawRequested` on final `Expose` event
Osspial pushed a commit that referenced this pull request Dec 22, 2019
* Implement changes to `RedrawRequested` event

Implements the changes described in #1041 for the X11 platform and for
platform-independent public-facing code.

* Fix `request_redraw` example

* Fix examples in lib docs

* Only issue `RedrawRequested` on final `Expose` event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants