app.dhover×299 error×46all
// F04 — vsync / frame pacing, X11 edition (../../../features/f04-frame-pacing.md).
// Derived from the X11 scaffold (../scaffold/app.d). Core X11 has no frame
// clock at all; the platform primitive is the PRESENT EXTENSION, which is
// xcb-only (no Xlib binding exists). This demo therefore uses the documented
// Xlib/XCB interop: the window/setup side stays Xlib like every other demo in
// this tree, XGetXCBConnection exposes the shared xcb_connection_t, and
// XSetEventQueueOwner(XCBOwnsEventQueue) — called immediately after
// XOpenDisplay, before any event-generating request — routes ALL events to
// xcb_poll_for_event so Present's GenericEvents can be read.
//
// The pacing chain (the "draw only when the display says so" contract):
//
//   xcb_present_select_input(eid, win, COMPLETE_NOTIFY)   — subscribe
//   xcb_present_notify_msc(win, serial, target_msc, 0, 0) — "wake me at msc"
//   PresentCompleteNotify(kind=NOTIFY_MSC) → ust,msc      — the frame clock
//   draw a trivially cheap solid-color flip, request msc+1, repeat
//
// Per frame: `frame_callback t=<ust> msc=<msc> mode=<mode>` (ust is the
// server's CLOCK_MONOTONIC microseconds; msc the media stream counter). After
// WSI_FRAMES frames (default 600): min/median/p99/max inter-frame delta and a
// coarse jitter histogram on STDOUT (the F04 contract), then an occlusion
// probe — XUnmapWindow for 3 s while the notify_msc chain keeps running, to
// answer "does the clock stop when hidden?" — then remap and exit.
//
// Fallback path (Present absent, or forced with WSI_NO_PRESENT=1): a 16.67 ms
// timerfd drives the same redraw + logging with mode=timer and msc=0 — the
// "no frame clock" degradation the F04 spec asks to be recorded as a finding.
//
// Headless-safe: no display prints `SKIP:` and exits 0. Under Xvfb there is
// no real vblank — Present still works, but its msc/ust come from a software
// fallback timer; the observed cadence IS the finding (../../f04-frame-pacing.md).
module 
(module) app
app
;
import
(module) c
c
; // ImportC: Xlib + Xlib-xcb + xcb + xcb/present + timerfd + poll
C preprocess command `cpp` failed for file `/home/runner/work/sparkles/sparkles/docs/research/window-system-integration/os-apis/x11/examples/f04-frame-pacing/c.c`, exit status 1
import
(module) instrument
instrument
;
import
(package) core
core
.
(package) core.stdc
stdc
.
(module) core.stdc.config

D compatible types that correspond to various basic types in associated C and C++ compilers.

Source

core/stdc/config.d

@copyrightCopyright Sean Kelly 2005 - 2009.@licenseDistributed under the Boost Software License 1.0. (See accompanying file LICENSE)@authorsSean Kelly@standardsISO/IEC 9899:1999 (E)
config
: c_long, c_ulong;
import
(package) core
core
.
(package) core.stdc
stdc
.
(module) core.stdc.stdio

D header file for C99 <stdio.h>

pubs.opengroup.org/onlinepubs/009695399/basedefs/stdio.h.html, stdio.h

Source

core/stdc/stdio.d

@copyrightCopyright Sean Kelly 2005 - 2009.@licenseDistributed under the Boost Software License 1.0. (See accompanying file LICENSE)@authorsSean Kelly, Alex Rønne Petersen@standardsISO/IEC 9899:1999 (E)
stdio
:
(alias) app.printf = int core.stdc.stdio.printf(scope const(char*) format, scope const ...) nothrow @nogc
printf
;
import
(package) core
core
.
(package) core.stdc
stdc
.
(module) core.stdc.stdlib

D header file for C99.

pubs.opengroup.org/onlinepubs/009695399/basedefs/stdlib.h.html, stdlib.h

Source

core/stdc/stdlib.d

@copyrightCopyright Sean Kelly 2005 - 2014.@licenseDistributed under the Boost Software License 1.0. (See accompanying file LICENSE)@authorsSean Kelly@standardsISO/IEC 9899:1999 (E)
stdlib
:
(alias) app.free = void core.stdc.stdlib.free(void* ptr) nothrow @nogc
free
,
(alias) app.getenv = char* core.stdc.stdlib.getenv(scope const(char*) name) nothrow @nogc
getenv
,
(alias) app.qsort = void core.stdc.stdlib.qsort(void* base, ulong nmemb, ulong size, extern (C) int function(const(void*), const(void*)) compar) nothrow @nogc
qsort
;
import
(package) core
core
.
(package) core.sys
sys
.
(package) core.sys.posix
posix
.
(module) core.sys.posix.unistd

D header file for POSIX.

@copyrightCopyright Sean Kelly 2005 - 2009.@licenseBoost License 1.0.@authorsSean Kelly@standardsThe Open Group Base Specifications Issue 8, IEEE Std 1003.1, 2024 Edition
unistd
:
(alias) app.read = long core.sys.posix.unistd.read(int, void*, ulong) nothrow @nogc
read
;
// Constants Xlib/xcb expose only as macros (not ImportC-able); re-declared // per the ImportC guide. The XCB_* event codes are #defines in xproto.h. enum : c_long {
(enum value) app.KeyPressMask = 1L
KeyPressMask
= 1L << 0,
(enum value) app.ExposureMask = 32768L
ExposureMask
= 1L << 15,
(enum value) app.StructureNotifyMask = 131072L
StructureNotifyMask
= 1L << 17,
} enum // xcb response_type discriminators (#defines in xcb/xproto.h) {
(enum value) app.XCB_KEY_PRESS = 2
XCB_KEY_PRESS
= 2,
(enum value) app.XCB_EXPOSE = 12
XCB_EXPOSE
= 12,
(enum value) app.XCB_UNMAP_NOTIFY = 18
XCB_UNMAP_NOTIFY
= 18,
(enum value) app.XCB_MAP_NOTIFY = 19
XCB_MAP_NOTIFY
= 19,
(enum value) app.XCB_CONFIGURE_NOTIFY = 22
XCB_CONFIGURE_NOTIFY
= 22,
(enum value) app.XCB_CLIENT_MESSAGE = 33
XCB_CLIENT_MESSAGE
= 33,
(enum value) app.XCB_GE_GENERIC = 35
XCB_GE_GENERIC
= 35,
} enum
(constant) int app.XCB_PRESENT_COMPLETE_NOTIFY_EVTYPE = 1
XCB_PRESENT_COMPLETE_NOTIFY_EVTYPE
= 1; // #define XCB_PRESENT_COMPLETE_NOTIFY
enum
(constant) int app.False = 0
False
= 0;
enum
(constant) int app.POLLIN = 1
POLLIN
= 0x001;
enum
(constant) int app.CLOCK_MONOTONIC = 1
CLOCK_MONOTONIC
= 1;
// xcb declares xcb_present_complete_notify_event_t XCB_PACKED // (__attribute__((__packed__))): on the wire `msc` sits at byte offset 36, // unaligned for a uint64. ImportC (ldc 1.41) IGNORES the packed attribute — // the imported struct is 48 bytes with msc naturally aligned to offset 40, // so reading msc through it yields garbage. Re-declare the exact wire layout // in D with align(1), the same re-declaration discipline as the macro // constants above. struct
(struct) app.PresentCompleteNotify
PresentCompleteNotify
// = xcb_present_complete_notify_event_t, packed
{ align(1): ubyte
(field) ubyte app.PresentCompleteNotify.response_type
response_type
;
ubyte
(field) ubyte app.PresentCompleteNotify.extension
extension
;
ushort
(field) ushort app.PresentCompleteNotify.sequence
sequence
;
uint
(field) uint app.PresentCompleteNotify.length
length
;
ushort
(field) ushort app.PresentCompleteNotify.event_type
event_type
;
ubyte
(field) ubyte app.PresentCompleteNotify.kind
kind
;
ubyte
(field) ubyte app.PresentCompleteNotify.mode
mode
;
uint
(field) uint app.PresentCompleteNotify.event
event
;
uint
(field) uint app.PresentCompleteNotify.window
window
;
uint
(field) uint app.PresentCompleteNotify.serial
serial
;
ulong
(field) ulong app.PresentCompleteNotify.ust
ust
;
uint
(field) uint app.PresentCompleteNotify.full_sequence
full_sequence
;
ulong
(field) ulong app.PresentCompleteNotify.msc
msc
;
} static assert(
(struct) app.PresentCompleteNotify
PresentCompleteNotify
.
(constant) ulong app.PresentCompleteNotify.sizeof = 44LU
sizeof
== 44);
static assert(
(struct) app.PresentCompleteNotify
PresentCompleteNotify
.
(field) ulong app.PresentCompleteNotify.msc
msc
.offsetof == 36);
static assert(xcb_present_complete_notify_event_t.
(field) _error_ xcb_present_complete_notify_event_t.sizeof
sizeof
== 48); // the ImportC trap
undefined identifier `xcb_present_complete_notify_event_t` while evaluating: `static assert(xcb_present_complete_notify_event_t.sizeof == 48)`
// --------------------------------------------------------------------------- // Inter-frame-delta bookkeeping + the exit report the F04 spec mandates. enum
(constant) int app.maxFrames = 4096
maxFrames
= 4096;
__gshared long[
(constant) int app.maxFrames = 4096
maxFrames
]
(__gshared global) long[4096] app.g_deltas
g_deltas
;
__gshared int
(__gshared global) int app.g_nDeltas
g_nDeltas
= 0;
extern (C) int
int app.cmpLong(const(void*) a, const(void*) b) nothrow @nogc
cmpLong
(const void*
(parameter) const(void*) a
a
, const void*
(parameter) const(void*) b
b
) @nogc nothrow
{ const
(local variable) const(long) x
x
= *cast(const long*)
(parameter) const(void*) a
a
,
(local variable) const(long) y
y
= *cast(const long*)
(parameter) const(void*) b
b
;
return
(local variable) const(long) x
x
<
(local variable) const(long) y
y
? -1 :
(local variable) const(long) x
x
>
(local variable) const(long) y
y
? 1 : 0;
} void
void app.recordDelta(long d) nothrow @nogc
recordDelta
(long
(parameter) long d
d
) @nogc nothrow
{ if (
(__gshared global) int app.g_nDeltas
g_nDeltas
<
(constant) int app.maxFrames = 4096
maxFrames
)
(__gshared global) long[4096] app.g_deltas
g_deltas
[
(__gshared global) int app.g_nDeltas
g_nDeltas
++] =
(parameter) long d
d
;
} /// min/median/p99/max + coarse histogram, printed to STDOUT (spec req. 2). void
void app.printStats(const(char)* path, int frames) nothrow @nogc

min/median/p99/max + coarse histogram, printed to STDOUT (spec req. 2).

printStats
(const(char)*
(parameter) const(char)* path
path
, int
(parameter) int frames
frames
) @nogc nothrow
{ if (
(__gshared global) int app.g_nDeltas
g_nDeltas
== 0)
return;
void core.stdc.stdlib.qsort(void* base, ulong nmemb, ulong size, extern (C) int function(const(void*), const(void*)) compar) nothrow @nogc
qsort
(
(__gshared global) long[4096] app.g_deltas
g_deltas
.
(constant) long* long[4096].ptr = &g_deltas
ptr
,
(__gshared global) int app.g_nDeltas
g_nDeltas
, long.
(constant) ulong long.sizeof = 8LU
sizeof
, &
int app.cmpLong(const(void*) a, const(void*) b) nothrow @nogc
cmpLong
);
const
(local variable) const(long) lo
lo
=
(__gshared global) long[4096] app.g_deltas
g_deltas
[0],
(local variable) const(long) hi
hi
=
(__gshared global) long[4096] app.g_deltas
g_deltas
[
(__gshared global) int app.g_nDeltas
g_nDeltas
- 1];
const
(local variable) const(long) med
med
=
(__gshared global) long[4096] app.g_deltas
g_deltas
[
(__gshared global) int app.g_nDeltas
g_nDeltas
/ 2];
const
(local variable) const(long) p99
p99
=
(__gshared global) long[4096] app.g_deltas
g_deltas
[(
(__gshared global) int app.g_nDeltas
g_nDeltas
* 99) / 100 <
(__gshared global) int app.g_nDeltas
g_nDeltas
? (
(__gshared global) int app.g_nDeltas
g_nDeltas
* 99) / 100 :
(__gshared global) int app.g_nDeltas
g_nDeltas
- 1];
int core.stdc.stdio.printf(scope const(char*) format, scope const ...) nothrow @nogc
printf
("stats path=%s frames=%d deltas=%d min_us=%lld median_us=%lld p99_us=%lld max_us=%lld\n",
(parameter) const(char)* path
path
,
(parameter) int frames
frames
,
(__gshared global) int app.g_nDeltas
g_nDeltas
,
(local variable) const(long) lo
lo
,
(local variable) const(long) med
med
,
(local variable) const(long) p99
p99
,
(local variable) const(long) hi
hi
);
static immutable long[8]
(immutable global) immutable(long[8]) app.printStats.edges
edges
= [1_000, 4_000, 8_000, 12_000, 16_000,
20_000, 33_000, 100_000]; static immutable
(alias) object.string = string
string
[9]
(immutable global) immutable(string[9]) app.printStats.labels
labels
= [
"<1ms", "1-4ms", "4-8ms", "8-12ms", "12-16ms", "16-20ms", "20-33ms", "33-100ms", ">=100ms", ]; int[9]
(local variable) int[9] counts
counts
= 0;
foreach (
(local variable) int i
i
; 0 ..
(__gshared global) int app.g_nDeltas
g_nDeltas
)
{
(alias) object.size_t = ulong
size_t
(local variable) ulong b
b
=
(immutable global) immutable(long[8]) app.printStats.edges
edges
.
(constant) ulong immutable(long[8]).length = 8LU
length
; // overflow bucket
foreach (
(parameter) ulong j
j
,
(parameter) immutable(long) e
e
;
(immutable global) immutable(long[8]) app.printStats.edges
edges
)
if (
(__gshared global) long[4096] app.g_deltas
g_deltas
[
(local variable) int i
i
] <
(local variable) immutable(long) e
e
)
{
(local variable) ulong b
b
=
(local variable) ulong j
j
;
break; } ++
(local variable) int[9] counts
counts
[
(local variable) ulong b
b
];
} foreach (
(parameter) ulong j
j
,
(parameter) immutable(string) label
label
;
(immutable global) immutable(string[9]) app.printStats.labels
labels
)
if (
(local variable) int[9] counts
counts
[
(local variable) ulong j
j
] > 0)
int core.stdc.stdio.printf(scope const(char*) format, scope const ...) nothrow @nogc
printf
("histogram bucket=%s count=%d\n",
(local variable) immutable(string) label
label
.
(field) immutable(char)* immutable(string).ptr
ptr
,
(local variable) int[9] counts
counts
[
(local variable) ulong j
j
]);
} int
int D main()
main
()
{
void instrument.initInstrument(const(char)* demoName) nothrow @nogc

Names the demo, starts the monotonic clock, and emits init_start. Call this first, before any platform API call.

initInstrument
("f04_x11");
const
(local variable) const(char*) envFrames
envFrames
=
char* core.stdc.stdlib.getenv(scope const(char*) name) nothrow @nogc
getenv
("WSI_FRAMES");
int
(local variable) int targetFrames
targetFrames
= 600;
if (
(local variable) const(char*) envFrames
envFrames
!is null)
{ import
(package) core
core
.
(package) core.stdc
stdc
.
(module) core.stdc.stdlib

D header file for C99.

pubs.opengroup.org/onlinepubs/009695399/basedefs/stdlib.h.html, stdlib.h

Source

core/stdc/stdlib.d

@copyrightCopyright Sean Kelly 2005 - 2014.@licenseDistributed under the Boost Software License 1.0. (See accompanying file LICENSE)@authorsSean Kelly@standardsISO/IEC 9899:1999 (E)
stdlib
:
(alias) atoi = int core.stdc.stdlib.atoi(scope const(char*) nptr) nothrow @nogc
atoi
;
const
(local variable) const(int) v
v
=
int core.stdc.stdlib.atoi(scope const(char*) nptr) nothrow @nogc
atoi
(
(local variable) const(char*) envFrames
envFrames
);
if (
(local variable) const(int) v
v
> 0 &&
(local variable) const(int) v
v
<
(constant) int app.maxFrames = 4096
maxFrames
)
(local variable) int targetFrames
targetFrames
=
(local variable) const(int) v
v
;
} // -- Connect; hand the event queue to xcb BEFORE any other Xlib call ------ Display*
(local variable) _error_ dpy
dpy
= XOpenDisplay(null);
undefined identifier `Display`
undefined identifier `XOpenDisplay`
if (dpy is null) {
int core.stdc.stdio.printf(scope const(char*) format, scope const ...) nothrow @nogc
printf
("SKIP: no X11 display (XOpenDisplay returned null)\n");
return 0; } XSetEventQueueOwner(dpy, XCBOwnsEventQueue);
undefined identifier `XSetEventQueueOwner`
xcb_connection_t*
(local variable) _error_ conn
conn
= XGetXCBConnection(dpy);
undefined identifier `xcb_connection_t`
undefined identifier `XGetXCBConnection`
void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogc

Emit an event with a printf-formatted key=value ... payload.

emitf
("step", "name=XGetXCBConnection fd=%d queue_owner=xcb",
xcb_get_file_descriptor(conn));
undefined identifier `xcb_get_file_descriptor`
const
(local variable) const(_error_) screen
screen
= XDefaultScreen(dpy);
undefined identifier `XDefaultScreen`
const
(local variable) const(_error_) root
root
= XRootWindow(dpy, screen);
undefined identifier `XRootWindow`
// -- Present availability + version (round-trips) ------------------------- auto
(local variable) _error_ extData
extData
= xcb_get_extension_data(conn, &xcb_present_id);
undefined identifier `xcb_get_extension_data`
const
(local variable) const(_error_) havePresentExt
havePresentExt
= extData !is null && extData.present != 0;
void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogc

Emit an event with a printf-formatted key=value ... payload.

emitf
("step", "name=xcb_get_extension_data ext=Present present=%d major_opcode=%d first_event=%d",
cast(int) havePresentExt, havePresentExt ? extData.major_opcode : -1, havePresentExt ? extData.first_event : -1); uint
(local variable) uint presentMajor
presentMajor
= 0,
(local variable) uint presentMinor
presentMinor
= 0;
if (havePresentExt) { auto
(local variable) _error_ ck
ck
= xcb_present_query_version(conn, 1, 4);
undefined identifier `xcb_present_query_version`
auto
(local variable) _error_ reply
reply
= xcb_present_query_version_reply(conn, ck, null);
undefined identifier `xcb_present_query_version_reply`
if (reply !is null) {
(local variable) uint presentMajor
presentMajor
= reply.major_version;
(local variable) uint presentMinor
presentMinor
= reply.minor_version;
void core.stdc.stdlib.free(void* ptr) nothrow @nogc
free
(reply);
}
void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogc

Emit an event with a printf-formatted key=value ... payload.

emitf
("step", "name=xcb_present_query_version version=%u.%u",
(local variable) uint presentMajor
presentMajor
,
(local variable) uint presentMinor
presentMinor
);
} bool
(local variable) bool usePresent
usePresent
= havePresentExt;
if (
(local variable) bool usePresent
usePresent
&&
char* core.stdc.stdlib.getenv(scope const(char*) name) nothrow @nogc
getenv
("WSI_NO_PRESENT") !is null)
{
(local variable) bool usePresent
usePresent
= false;
void instrument.emit(scope const(char)* kind) nothrow @nogc

Emit an event with no key=value payload.

emit
("step name=fallback reason=WSI_NO_PRESENT");
} else if (!
(local variable) bool usePresent
usePresent
)
void instrument.emit(scope const(char)* kind) nothrow @nogc

Emit an event with no key=value payload.

emit
("step name=fallback reason=present_absent");
// -- Window (Xlib side, scaffold-style) ------------------------------------ int
(local variable) int width
width
= 480,
(local variable) int height
height
= 320;
Window
(local variable) _error_ win
win
= XCreateSimpleWindow(dpy, root, 0, 0, width, height, 1,
undefined identifier `Window`
undefined identifier `XCreateSimpleWindow`
XBlackPixel(dpy, screen), XWhitePixel(dpy, screen));
void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogc

Emit an event with a printf-formatted key=value ... payload.

emitf
("step", "name=XCreateSimpleWindow xid=0x%lx", win);
XStoreName(dpy, win, "Sparkles · X11 F04");
undefined identifier `XStoreName`
Atom
(local variable) _error_ wmDelete
wmDelete
= XInternAtom(dpy, "WM_DELETE_WINDOW", False);
undefined identifier `Atom`
undefined identifier `XInternAtom`
XSetWMProtocols(dpy, win, &wmDelete, 1);
undefined identifier `XSetWMProtocols`
XSelectInput(dpy, win, ExposureMask | KeyPressMask | StructureNotifyMask);
undefined identifier `XSelectInput`
XMapWindow(dpy, win);
undefined identifier `XMapWindow`
// Interop gotcha: with the queue owned by xcb the event loop below reads // via xcb_poll_for_event, which — unlike XPending — does NOT flush Xlib's // output buffer. Without this flush the MapWindow request never leaves // the client on the fallback path and the demo deadlocks waiting for its // first Expose. (On the Present path the first xcb_present_* call rescues // it by accident: xcb takes the socket back from libX11, forcing a flush.) XFlush(dpy);
undefined identifier `XFlush`
void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogc

Emit an event with a printf-formatted key=value ... payload.

emitf
("window_created", "xid=0x%lx size=%dx%d", win,
(local variable) int width
width
,
(local variable) int height
height
);
GC
(local variable) _error_ gc
gc
= XDefaultGC(dpy, screen);
undefined identifier `GC`
undefined identifier `XDefaultGC`
// -- Present event subscription -------------------------------------------- if (
(local variable) bool usePresent
usePresent
)
{ const
(local variable) const(_error_) eid
eid
= xcb_generate_id(conn);
undefined identifier `xcb_generate_id`
xcb_present_select_input(conn, eid, cast(uint) win,
undefined identifier `xcb_present_select_input`
XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY);
void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogc

Emit an event with a printf-formatted key=value ... payload.

emitf
("step", "name=xcb_present_select_input eid=0x%x mask=COMPLETE_NOTIFY", eid);
} // -- Fallback clock: 16.67 ms timerfd (armed only on the fallback path) ---- const
(local variable) const(_error_) tfd
tfd
= timerfd_create(CLOCK_MONOTONIC, 0);
undefined identifier `timerfd_create`
if (!
(local variable) bool usePresent
usePresent
)
{ itimerspec
(local variable) _error_ its
its
;
undefined identifier `itimerspec`
its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 16_666_667; its.it_value = its.it_interval; timerfd_settime(tfd, 0, &its, null);
undefined identifier `timerfd_settime`
void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogc

Emit an event with a printf-formatted key=value ... payload.

emitf
("step", "name=timerfd_create fd=%d interval_us=16667", tfd);
} scope (exit) { XDestroyWindow(dpy, win);
undefined identifier `XDestroyWindow`
XCloseDisplay(dpy); // owns conn: closes the shared xcb connection too
undefined identifier `XCloseDisplay`
} /// One trivially cheap frame: a solid-color flip (alternating fill). int
(local variable) int frames

One trivially cheap frame: a solid-color flip (alternating fill).

frames
= 0;
void
void app.main.drawFrame() pure nothrow @nogc @safe
drawFrame
() @nogc nothrow
{ const c_ulong
(local variable) const(ulong) color
color
= (
(local variable) int frames

One trivially cheap frame: a solid-color flip (alternating fill).

frames
& 1) ? 0xff_60_30 : 0x20_50_c0;
XSetForeground(dpy, gc, color);
undefined identifier `XSetForeground`
XFillRectangle(dpy, win, gc, 0, 0, cast(uint) width, cast(uint) height);
undefined identifier `XFillRectangle`
} const
(local variable) const(_error_) fd
fd
= xcb_get_file_descriptor(conn);
undefined identifier `xcb_get_file_descriptor`
bool
(local variable) bool running
running
= true,
(local variable) bool chainStarted
chainStarted
= false,
(local variable) bool exposed
exposed
= false;
bool
(local variable) bool occluding
occluding
= false,
(local variable) bool occlusionDone
occlusionDone
= false;
long
(local variable) long lastUst
lastUst
= 0,
(local variable) long occlusionEndUs
occlusionEndUs
= 0;
ulong
(local variable) ulong lastMsc
lastMsc
= 0;
uint
(local variable) uint notifySerial
notifySerial
= 0;
int
(local variable) int occlFrames
occlFrames
= 0;
/// Schedule the next frame-clock wakeup. target=0 means "at the current /// msc" (used once to seed the chain); afterwards we ask for lastMsc+1. void
void app.main.requestNotify(ulong targetMsc) pure nothrow @nogc @safe

Schedule the next frame-clock wakeup. target=0 means "at the current msc" (used once to seed the chain); afterwards we ask for lastMsc+1.

requestNotify
(ulong
(parameter) ulong targetMsc
targetMsc
) @nogc nothrow
{ xcb_present_notify_msc(conn, cast(uint) win, ++notifySerial,
undefined identifier `xcb_present_notify_msc`
targetMsc, 0, 0); xcb_flush(conn);
undefined identifier `xcb_flush`
} while (
(local variable) bool running
running
)
{ // Drain everything xcb has already read or can read without blocking. for (;;) { xcb_generic_event_t*
(local variable) _error_ ev
ev
= xcb_poll_for_event(conn);
undefined identifier `xcb_generic_event_t`
undefined identifier `xcb_poll_for_event`
if (ev is null) break; scope (exit)
void core.stdc.stdlib.free(void* ptr) nothrow @nogc
free
(ev); // xcb events are malloc'd; the caller frees
const
(local variable) const(_error_) rt
rt
= ev.
(field) _error_ ev.response_type
response_type
& 0x7f;
switch (rt) { case XCB_EXPOSE: const
_error_ xe
xe
= cast(xcb_expose_event_t*) ev;
if (xe.count == 0 && !exposed) { exposed = true; drawFrame(); XFlush(dpy); emit("first_pixel_presented method=XFillRectangle note=no_confirmation_without_Present"); if (usePresent && !chainStarted) { chainStarted = true; requestNotify(0); // seed: complete at the current msc emit("step name=xcb_present_notify_msc target=current note=chain_seed"); } } break; case XCB_GE_GENERIC: const
_error_ ge
ge
= cast(xcb_present_generic_event_t*) ev;
if (!havePresentExt || ge.
ge.extension
extension
!= extData.major_opcode)
break; if (ge.evtype != XCB_PRESENT_COMPLETE_NOTIFY_EVTYPE) break; const
_error_ cn
cn
= cast(
(unresolved type) PresentCompleteNotify
PresentCompleteNotify
*) ev;
if (cn.
cn.kind
kind
!= XCB_PRESENT_COMPLETE_KIND_NOTIFY_MSC)
break; // The frame clock tick: ust/msc straight from the server. lastMsc = cn.
cn.msc
msc
;
if (occluding) { ++occlFrames; emitf("frame_callback", "t=%llu msc=%llu mode=%d occluded=1", cast(ulong) cn.
cn.ust
ust
, cast(ulong) cn.
cn.msc
msc
, cast(int) cn.
cn.mode
mode
);
requestNotify(cn.
cn.msc
msc
+ 1);
break; } ++frames; if (lastUst != 0) recordDelta(cast(long)(cn.
cn.ust
ust
- lastUst));
lastUst = cast(long) cn.
cn.ust
ust
;
emitf("frame_callback", "t=%llu msc=%llu mode=%d frame=%d", cast(ulong) cn.
cn.ust
ust
, cast(ulong) cn.
cn.msc
msc
, cast(int) cn.
cn.mode
mode
, frames);
if (frames < targetFrames) { drawFrame(); XFlush(dpy); requestNotify(cn.
cn.msc
msc
+ 1);
} break; case XCB_UNMAP_NOTIFY: emit("vis_change state=unmapped"); break; case XCB_MAP_NOTIFY: emit("vis_change state=mapped"); break; case XCB_CLIENT_MESSAGE: const
_error_ cm
cm
= cast(xcb_client_message_event_t*) ev;
if (cm.data.data32[0] == cast(uint) wmDelete) { emit("close_requested via=WM_DELETE_WINDOW"); running = false; } break; case XCB_KEY_PRESS: emit("close_requested via=KeyPress"); running = false; break; default: break; } } if (xcb_connection_has_error(conn))
undefined identifier `xcb_connection_has_error`
{
void instrument.emit(scope const(char)* kind) nothrow @nogc

Emit an event with no key=value payload.

emit
("x_error connection_lost=1");
return 1; } // -- Occlusion probe (spec req. 3): unmap for 3 s, keep the chain up -- if (
(local variable) int frames

One trivially cheap frame: a solid-color flip (alternating fill).

frames
>=
(local variable) int targetFrames
targetFrames
&& !
(local variable) bool occluding
occluding
&& !
(local variable) bool occlusionDone
occlusionDone
)
{
(local variable) bool occluding
occluding
= true;
(local variable) long occlusionEndUs
occlusionEndUs
=
long instrument.nowUs() nothrow @nogc

Microseconds elapsed since initInstrument.

nowUs
() + 3_000_000;
XUnmapWindow(dpy, win);
undefined identifier `XUnmapWindow`
XFlush(dpy);
undefined identifier `XFlush`
void instrument.emit(scope const(char)* kind) nothrow @nogc

Emit an event with no key=value payload.

emit
("step name=XUnmapWindow note=occlusion_probe_3s");
if (
(local variable) bool usePresent
usePresent
)
void app.main.requestNotify(ulong targetMsc) pure nothrow @nogc @safe

Schedule the next frame-clock wakeup. target=0 means "at the current msc" (used once to seed the chain); afterwards we ask for lastMsc+1.

requestNotify
(
(local variable) ulong lastMsc
lastMsc
+ 1); // does the clock still tick?
} if (
(local variable) bool occluding
occluding
&&
long instrument.nowUs() nothrow @nogc

Microseconds elapsed since initInstrument.

nowUs
() >=
(local variable) long occlusionEndUs
occlusionEndUs
)
{
(local variable) bool occluding
occluding
= false;
(local variable) bool occlusionDone
occlusionDone
= true;
XMapWindow(dpy, win);
undefined identifier `XMapWindow`
XFlush(dpy);
undefined identifier `XFlush`
void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogc

Emit an event with a printf-formatted key=value ... payload.

emitf
("occlusion", "frames_while_unmapped=%d duration_ms=3000 clock=%s",
(local variable) int occlFrames
occlFrames
,
(local variable) bool usePresent
usePresent
? "present".
(constant) immutable(char)* "present".ptr = "present"
ptr
: "timer".
(constant) immutable(char)* "timer".ptr = "timer"
ptr
);
(local variable) bool running
running
= false; // measured + probed: done
} if (
(local variable) bool running
running
)
{ pollfd[2]
(local variable) _error_ pfds
pfds
;
undefined identifier `pollfd`
pfds[0].
(__error)[0].fd
fd
= fd;
pfds[0].events = POLLIN; pfds[0].revents = 0; pfds[1].
(__error)[1].fd
fd
= tfd;
pfds[1].events = POLLIN; pfds[1].revents = 0; // Bounded sleep so the occlusion deadline fires even when the // (unmapped, Present-less) connection goes silent. poll(pfds.
pfds.ptr
ptr
, 2, 200);
undefined identifier `poll`
if (!
(local variable) bool usePresent
usePresent
&& (pfds[1].revents & POLLIN))
{ ulong
(local variable) ulong expirations
expirations
;
long core.sys.posix.unistd.read(int, void*, ulong) nothrow @nogc
read
(tfd, &
(local variable) ulong expirations
expirations
,
(local variable) ulong expirations
expirations
.
(constant) ulong ulong.sizeof = 8LU
sizeof
);
if (
(local variable) bool exposed
exposed
&& (
(local variable) int frames

One trivially cheap frame: a solid-color flip (alternating fill).

frames
<
(local variable) int targetFrames
targetFrames
||
(local variable) bool occluding
occluding
))
{ const
(local variable) const(long) now
now
=
long instrument.nowUs() nothrow @nogc

Microseconds elapsed since initInstrument.

nowUs
();
if (
(local variable) bool occluding
occluding
)
{ ++
(local variable) int occlFrames
occlFrames
;
void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogc

Emit an event with a printf-formatted key=value ... payload.

emitf
("frame_callback", "t=%lld msc=0 mode=timer occluded=1",
(local variable) const(long) now
now
);
} else { ++
(local variable) int frames

One trivially cheap frame: a solid-color flip (alternating fill).

frames
;
if (
(local variable) long lastUst
lastUst
!= 0)
void app.recordDelta(long d) nothrow @nogc
recordDelta
(
(local variable) const(long) now
now
-
(local variable) long lastUst
lastUst
);
(local variable) long lastUst
lastUst
=
(local variable) const(long) now
now
;
void app.main.drawFrame() pure nothrow @nogc @safe
drawFrame
();
XFlush(dpy);
undefined identifier `XFlush`
void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogc

Emit an event with a printf-formatted key=value ... payload.

emitf
("frame_callback", "t=%lld msc=0 mode=timer frame=%d",
(local variable) const(long) now
now
,
(local variable) int frames

One trivially cheap frame: a solid-color flip (alternating fill).

frames
);
} } } } }
void app.printStats(const(char)* path, int frames) nothrow @nogc

min/median/p99/max + coarse histogram, printed to STDOUT (spec req. 2).

printStats
(
(local variable) bool usePresent
usePresent
? "present".
(constant) immutable(char)* "present".ptr = "present"
ptr
: "timer".
(constant) immutable(char)* "timer".ptr = "timer"
ptr
,
(local variable) int frames

One trivially cheap frame: a solid-color flip (alternating fill).

frames
);
void instrument.emit(scope const(char)* kind) nothrow @nogc

Emit an event with no key=value payload.

emit
("teardown");
return 0; }