]> git.cworth.org Git - fips/blobdiff - context.c
context: Avoid doing excessive work if application re-sets the same context
[fips] / context.c
index 6fbd7f2228ea2bfc2ff3b4b1d2d933f24f4ed27a..284e4ae8d8e7ce246e37246fc616ad77b6d3713f 100644 (file)
--- a/context.c
+++ b/context.c
  */
 
 #include "context.h"
-
 #include "metrics.h"
+#include "xmalloc.h"
 
-void
-context_enter (fips_api_t api, void *system_context_id unused)
+context_t *current_context;
+
+static context_t *
+context_create (fips_api_t api, void *system_context_id)
 {
+       context_t *ctx;
+
+       ctx = xcalloc (1, sizeof (*ctx));
+
+       ctx->system_id = system_context_id;
+
        fips_dispatch_init (api);
 
-       metrics_info_init ();
+       metrics_info_init (&ctx->metrics_info);
+
+       return ctx;
+}
+
+static void
+context_destroy (context_t *ctx)
+{
+       metrics_info_fini (&ctx->metrics_info);
+}
+
+void
+context_enter (fips_api_t api, void *system_context_id)
+{
+       /* Do nothing if the application is setting the same context
+        * as is already current. */
+       if (current_context && current_context->system_id == system_context_id)
+               return;
+
+       if (current_context)
+               context_destroy (current_context);
+
+       current_context = context_create (api, system_context_id);
 
        metrics_set_current_op (METRICS_OP_SHADER + 0);
        metrics_counter_start ();
@@ -37,5 +67,54 @@ context_enter (fips_api_t api, void *system_context_id unused)
 void
 context_leave (void)
 {
-       metrics_info_fini ();
+       context_t *ctx = current_context;
+       timer_query_t *timer, *timer_next;
+       monitor_t *monitor, *monitor_next;
+
+       if (ctx == NULL)
+               return;
+
+       metrics_collect_available ();
+
+       if (ctx->timer_begun_id) {
+               glEndQuery (GL_TIME_ELAPSED);
+               glDeleteQueries (1, &ctx->timer_begun_id);
+               ctx->timer_begun_id = 0;
+       }
+
+       for (timer = ctx->timer_head;
+            timer;
+            timer = timer_next)
+       {
+               glDeleteQueries (1, &timer->id);
+               timer_next = timer->next;
+               free (timer);
+       }
+       ctx->timer_head = NULL;
+       ctx->timer_tail = NULL;
+
+       if (ctx->monitor_begun_id) {
+               glEndPerfMonitorAMD (ctx->monitor_begun_id);
+               glDeletePerfMonitorsAMD (1, &ctx->monitor_begun_id);
+               ctx->monitor_begun_id = 0;
+       }
+
+       for (monitor = ctx->monitor_head;
+            monitor;
+            monitor = monitor_next)
+       {
+               glDeletePerfMonitorsAMD (1, &monitor->id);
+               monitor_next = monitor->next;
+               free (monitor);
+       }
+       ctx->monitor_head = NULL;
+       ctx->monitor_tail = NULL;
+
+       ctx->monitors_in_flight = 0;
+}
+
+context_t *
+context_get_current (void)
+{
+       return current_context;
 }