radeonsi: fix shader disk cache key
[mesa.git] / src / gallium / state_trackers / wgl / stw_framebuffer.c
index 09b57766dc0b72874d20a7a6f4b58dcdbd7c38a8..232ab1d230599d3a7c947f6f796b96b4f1ab6990 100644 (file)
@@ -30,6 +30,7 @@
 #include "pipe/p_screen.h"
 #include "util/u_memory.h"
 #include "hud/hud_context.h"
+#include "util/os_time.h"
 #include "state_tracker/st_api.h"
 
 #include "stw_icd.h"
@@ -580,6 +581,41 @@ stw_framebuffer_present_locked(HDC hdc,
 }
 
 
+/**
+ * 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;
+}
+
+
 BOOL APIENTRY
 DrvSwapBuffers(HDC hdc)
 {
@@ -605,7 +641,7 @@ DrvSwapBuffers(HDC hdc)
          struct pipe_resource *back =
             stw_get_framebuffer_resource(fb->stfb, ST_ATTACHMENT_BACK_LEFT);
          if (back) {
-            hud_draw(ctx->hud, back);
+            hud_run(ctx->hud, NULL, back);
          }
       }
 
@@ -615,6 +651,10 @@ DrvSwapBuffers(HDC hdc)
       }
    }
 
+   if (stw_dev->swap_interval != 0) {
+      wait_swap_interval(fb);
+   }
+
    return stw_st_swap_framebuffer_locked(hdc, fb->stfb);
 }