[m-rev.] Trivial diff: improve latency in easyx

Ralph Becket rafe at cs.mu.OZ.AU
Thu Sep 30 09:42:49 AEST 2004


Estimated hours taken: 1
Branches: main

Fixed a latency bug in the easyx interface.  One has to tell the
Xlib XSync function to discard any pending events in the input queue
in order to avoid noticable event processing lag in applications with a high
refresh rate.

easyx.m:
	Moved the import of xlib to the implementation section (xlib is
	not used in the interface.)

	Added easyx.sync/3.

xlib.m:
	Changed xlib.sync/1 to discard any events waiting in the input queue.

samples/bounce.m:
	Now uses easyx.sync/3 rather than easyx.flush/3, which dramatically
	improves responsiveness.

Index: easyx.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/graphics/easyx/easyx.m,v
retrieving revision 1.1
diff -u -r1.1 easyx.m
--- easyx.m	28 Sep 2004 02:10:41 -0000	1.1
+++ easyx.m	29 Sep 2004 23:35:54 -0000
@@ -13,9 +13,10 @@
 % applications.  This library aims for ease of use over efficiency.
 %
 % All drawing is done to a backing pixmap and the user must explicitly call
-% easyx.flush/3 to make visible any changes since the last call to
-% easyx.flush/3.  Repainting after exposure events is handled automatically,
-% although resizing the window does require the user to redraw.
+% easyx.flush/3 or easyx.sync/3 to make visible any changes since the last
+% call to easyx.flush/3 or easyx.sync/3.  Repainting after exposure events is
+% handled automatically, although resizing the window does require the user
+% to redraw.
 %
 % An abstract coordinate space is used, but in keeping with Xlib, the
 % origin of the coordinate space is at the top-left of a window, with
@@ -40,7 +41,6 @@
 :- import_module io.
 :- import_module list.
 :- import_module std_util.
-:- use_module xlib.
 
 
 
@@ -77,6 +77,14 @@
     %
 :- pred flush(window::in, io::di, io::uo) is det.
 
+    % sync(Window, !IO)
+    % Like flush/3, but this predicate blocks until the X server has
+    % finished rendering.  Also, any pending events in the input queue
+    % are discarded.  This is a better choice than flush/3 for low-
+    % latency applications such as real-time games.
+    %
+:- pred sync(window::in, io::di, io::uo) is det.
+
     % clear_window(Window, !IO)
     % Clear the window using the current colour.
     %
@@ -382,6 +390,7 @@
 :- import_module math.
 :- import_module store.
 :- import_module string.
+:- use_module xlib.
 
 
 
@@ -535,6 +544,14 @@
     restore_from_backing_pixmap(Window, !IO),
     store.get_mutvar(Window^display, Display, !IO),
     impure xlib.flush(Display).
+
+
+:- pragma promise_pure(sync/3).
+
+sync(Window, !IO) :-
+    restore_from_backing_pixmap(Window, !IO),
+    store.get_mutvar(Window^display, Display, !IO),
+    impure xlib.sync(Display).
 
 %-----------------------------------------------------------------------------%
 
Index: xlib.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/graphics/easyx/xlib.m,v
retrieving revision 1.1
diff -u -r1.1 xlib.m
--- xlib.m	28 Sep 2004 02:10:41 -0000	1.1
+++ xlib.m	29 Sep 2004 23:35:58 -0000
@@ -274,7 +274,7 @@
 :- pragma foreign_proc("C", sync(DisplayPtr::in),
     [will_not_call_mercury, thread_safe],
     "
-        XSync(DisplayPtr, 0 /* Don't discard pending events */);
+        XSync(DisplayPtr, 1 /* Discard pending events */);
     ").
 
 %-----------------------------------------------------------------------------%
Index: samples/bounce.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/graphics/easyx/samples/bounce.m,v
retrieving revision 1.1
diff -u -r1.1 bounce.m
--- samples/bounce.m	28 Sep 2004 02:10:41 -0000	1.1
+++ samples/bounce.m	29 Sep 2004 23:36:04 -0000
@@ -270,9 +270,9 @@
     Drawing = paddle_drawing(!.Paddle) ++ objects_drawing(Black, !.Objects),
 
     easyx.draw(Window, Drawing, !IO),
-    easyx.flush(Window, !IO),
+    easyx.sync(Window, !IO),
 
-    u_sleep(20000, !IO),
+    u_sleep(10000, !IO),
 
     process_events(Window, Quit, !Paddle, !IO),
 
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list