+/**
+ * This is called just before issuing the buffer swap/present.
+ * We query the current time and determine if we should sleep before
+ * issuing the swap/present.
+ * This is a bit of a hack and is certainly not very accurate but it
+ * basically works.
+ * This is for the WGL_ARB_swap_interval extension.
+ */
+static void
+wait_swap_interval(struct stw_framebuffer *fb)
+{
+ /* Note: all time variables here are in units of microseconds */
+ int64_t cur_time = os_time_get_nano() / 1000;
+
+ if (fb->prev_swap_time != 0) {
+ /* Compute time since previous swap */
+ int64_t delta = cur_time - fb->prev_swap_time;
+ int64_t min_swap_period =
+ 1.0e6 / stw_dev->refresh_rate * stw_dev->swap_interval;
+
+ /* If time since last swap is less than wait period, wait.
+ * Note that it's possible for the delta to be negative because of
+ * rollover. See https://bugs.freedesktop.org/show_bug.cgi?id=102241
+ */
+ if ((delta >= 0) && (delta < min_swap_period)) {
+ float fudge = 1.75f; /* emperical fudge factor */
+ int64_t wait = (min_swap_period - delta) * fudge;
+ os_time_sleep(wait);
+ }
+ }
+
+ fb->prev_swap_time = cur_time;
+}
+
+