]> git.cworth.org Git - fips/commitdiff
context: Avoid doing excessive work if application re-sets the same context
authorCarl Worth <cworth@cworth.org>
Mon, 4 Nov 2013 21:40:32 +0000 (13:40 -0800)
committerCarl Worth <cworth@cworth.org>
Mon, 4 Nov 2013 21:40:32 +0000 (13:40 -0800)
I've heard rumors that firefox (at least as of some version) had a
tendency to call glXMakeCurrent with the current context on every frame.

As of this commit, fips will notice the current context being set with
MakeCurrent and will do not tear things down and re-create them in
this case.

context.c
context.h

index f4694c1ae8052152c9d36383e64170ccca2a0832..284e4ae8d8e7ce246e37246fc616ad77b6d3713f 100644 (file)
--- a/context.c
+++ b/context.c
  */
 
 #include "context.h"
-
 #include "metrics.h"
+#include "xmalloc.h"
 
-/* FIXME: Need a map from integers to context objects and track the
- * current context with glXMakeContextCurrent, eglMakeCurrent, etc. */
-
-context_t current_context;
+context_t *current_context;
 
-void
-context_enter (fips_api_t api, void *system_context_id unused)
+static context_t *
+context_create (fips_api_t api, void *system_context_id)
 {
-       context_t *ctx = &current_context;
+       context_t *ctx;
+
+       ctx = xcalloc (1, sizeof (*ctx));
+
+       ctx->system_id = system_context_id;
 
        fips_dispatch_init (api);
 
        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 ();
 }
@@ -44,10 +67,13 @@ context_enter (fips_api_t api, void *system_context_id unused)
 void
 context_leave (void)
 {
-       context_t *ctx = &current_context;
+       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) {
@@ -84,13 +110,11 @@ context_leave (void)
        ctx->monitor_head = NULL;
        ctx->monitor_tail = NULL;
 
-       current_context.monitors_in_flight = 0;
-
-       metrics_info_fini (&ctx->metrics_info);
+       ctx->monitors_in_flight = 0;
 }
 
 context_t *
 context_get_current (void)
 {
-       return &current_context;
+       return current_context;
 }
index 941f4f04b95eccf7762d565d707f9a2d4d1b3def..9b838812722e29e01e382233e00ac8b8fe29775c 100644 (file)
--- a/context.h
+++ b/context.h
@@ -58,6 +58,9 @@ typedef struct op_metrics
 
 typedef struct context
 {
+       /* Pointer to the system's context ID, (such as a GLXContext) */
+       void *system_id;
+
        metrics_info_t metrics_info;
 
        metrics_op_t op;