Use calloc instead of malloc/memset-0
[mesa.git] / src / glx / dri2_glx.c
index 51d45c9a3b903d66e1c9792c046d6f40f7180c99..f2fc187329889a7a813f0e2ed25a08bb9dee0acc 100644 (file)
@@ -43,6 +43,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/mman.h>
+#include <sys/time.h>
 #include "xf86drm.h"
 #include "dri2.h"
 #include "dri_common.h"
@@ -90,6 +91,8 @@ struct dri2_screen {
 
    void *driver;
    int fd;
+
+   Bool show_fps;
 };
 
 struct dri2_context
@@ -108,6 +111,9 @@ struct dri2_drawable
    int have_back;
    int have_fake_front;
    int swap_interval;
+
+   double previous_time;
+   unsigned frames;
 };
 
 static const struct glx_context_vtable dri2_context_vtable;
@@ -120,12 +126,11 @@ dri2_destroy_context(struct glx_context *context)
 
    driReleaseDrawables(&pcp->base);
 
-   if (context->extensions)
-      XFree((char *) context->extensions);
+   free((char *) context->extensions);
 
    (*psc->core->destroyContext) (pcp->driContext);
 
-   Xfree(pcp);
+   free(pcp);
 }
 
 static Bool
@@ -193,13 +198,12 @@ dri2_create_context(struct glx_screen *base,
       shared = pcp_shared->driContext;
    }
 
-   pcp = Xmalloc(sizeof *pcp);
+   pcp = calloc(1, sizeof *pcp);
    if (pcp == NULL)
       return NULL;
 
-   memset(pcp, 0, sizeof *pcp);
    if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
-      Xfree(pcp);
+      free(pcp);
       return NULL;
    }
 
@@ -208,7 +212,7 @@ dri2_create_context(struct glx_screen *base,
                                       config->driConfig, shared, pcp);
 
    if (pcp->driContext == NULL) {
-      Xfree(pcp);
+      free(pcp);
       return NULL;
    }
 
@@ -235,7 +239,8 @@ dri2_create_context_attribs(struct glx_screen *base,
    uint32_t major_ver = 2;
    uint32_t flags = 0;
    unsigned api;
-   uint32_t ctx_attribs[2 * 4];
+   int reset = __DRI_CTX_RESET_NO_NOTIFICATION;
+   uint32_t ctx_attribs[2 * 5];
    unsigned num_ctx_attribs = 0;
 
    if (psc->dri2->base.version < 3) {
@@ -246,7 +251,8 @@ dri2_create_context_attribs(struct glx_screen *base,
    /* Remap the GLX tokens to DRI2 tokens.
     */
    if (!dri2_convert_glx_attribs(num_attribs, attribs,
-                                &major_ver, &minor_ver, &flags, &api, error))
+                                &major_ver, &minor_ver, &flags, &api, &reset,
+                                 error))
       goto error_exit;
 
    if (shareList) {
@@ -254,13 +260,12 @@ dri2_create_context_attribs(struct glx_screen *base,
       shared = pcp_shared->driContext;
    }
 
-   pcp = Xmalloc(sizeof *pcp);
+   pcp = calloc(1, sizeof *pcp);
    if (pcp == NULL) {
       *error = __DRI_CTX_ERROR_NO_MEMORY;
       goto error_exit;
    }
 
-   memset(pcp, 0, sizeof *pcp);
    if (!glx_context_init(&pcp->base, &psc->base, &config->base))
       goto error_exit;
 
@@ -269,6 +274,15 @@ dri2_create_context_attribs(struct glx_screen *base,
    ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
    ctx_attribs[num_ctx_attribs++] = minor_ver;
 
+   /* Only send a value when the non-default value is requested.  By doing
+    * this we don't have to check the driver's DRI2 version before sending the
+    * default value.
+    */
+   if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
+      ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
+      ctx_attribs[num_ctx_attribs++] = reset;
+   }
+
    if (flags != 0) {
       ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
 
@@ -296,8 +310,7 @@ dri2_create_context_attribs(struct glx_screen *base,
    return &pcp->base;
 
 error_exit:
-   if (pcp != NULL)
-      Xfree(pcp);
+   free(pcp);
 
    return NULL;
 }
@@ -323,7 +336,7 @@ dri2DestroyDrawable(__GLXDRIdrawable *base)
    if (pdraw->base.xDrawable != pdraw->base.drawable)
       DRI2DestroyDrawable(psc->base.dpy, pdraw->base.xDrawable);
 
-   Xfree(pdraw);
+   free(pdraw);
 }
 
 static __GLXDRIdrawable *
@@ -337,11 +350,10 @@ dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
    struct dri2_display *pdp;
    GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
 
-   pdraw = Xmalloc(sizeof(*pdraw));
+   pdraw = calloc(1, sizeof(*pdraw));
    if (!pdraw)
       return NULL;
 
-   memset(pdraw, 0, sizeof *pdraw);
    pdraw->base.destroyDrawable = dri2DestroyDrawable;
    pdraw->base.xDrawable = xDrawable;
    pdraw->base.drawable = drawable;
@@ -377,14 +389,14 @@ dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
 
    if (!pdraw->driDrawable) {
       DRI2DestroyDrawable(psc->base.dpy, xDrawable);
-      Xfree(pdraw);
+      free(pdraw);
       return NULL;
    }
 
    if (__glxHashInsert(pdp->dri2Hash, xDrawable, pdraw)) {
       (*psc->core->destroyDrawable) (pdraw->driDrawable);
       DRI2DestroyDrawable(psc->base.dpy, xDrawable);
-      Xfree(pdraw);
+      free(pdraw);
       return None;
    }
 
@@ -616,7 +628,7 @@ dri2DestroyScreen(struct glx_screen *base)
    (*psc->core->destroyScreen) (psc->driScreen);
    driDestroyConfigs(psc->driver_configs);
    close(psc->fd);
-   Xfree(psc);
+   free(psc);
 }
 
 /**
@@ -661,6 +673,26 @@ unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
       return glx_dpy->codes->first_event + GLX_BufferSwapComplete;
 }
 
+static void show_fps(struct dri2_drawable *draw)
+{
+   struct timeval tv;
+   double current_time;
+
+   gettimeofday(&tv, 0);
+   current_time = (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
+
+   draw->frames++;
+
+   if (draw->previous_time + 1 < current_time) {
+      if (draw->previous_time) {
+         fprintf(stderr, "libGL: FPS = %.1f\n",
+                 draw->frames / (current_time - draw->previous_time));
+      }
+      draw->frames = 0;
+      draw->previous_time = current_time;
+   }
+}
+
 static int64_t
 dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
                int64_t remainder)
@@ -699,6 +731,10 @@ dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
 #endif
     }
 
+    if (psc->show_fps) {
+       show_fps(priv);
+    }
+
     /* Old servers don't send invalidate events */
     if (!pdp->invalidateAvailable)
        dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
@@ -724,7 +760,7 @@ dri2GetBuffers(__DRIdrawable * driDrawable,
    pdraw->height = *height;
    process_buffers(pdraw, buffers, *out_count);
 
-   Xfree(buffers);
+   free(buffers);
 
    return pdraw->buffers;
 }
@@ -749,7 +785,7 @@ dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
    pdraw->height = *height;
    process_buffers(pdraw, buffers, *out_count);
 
-   Xfree(buffers);
+   free(buffers);
 
    return pdraw->buffers;
 }
@@ -949,6 +985,14 @@ dri2BindExtensions(struct dri2_screen *psc, const __DRIextension **extensions)
 
       if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
         psc->throttle = (__DRI2throttleExtension *) extensions[i];
+
+      /* DRI2 version 3 is also required because
+       * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
+       */
+      if (psc->dri2->base.version >= 3
+          && strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
+         __glXEnableDirectExtension(&psc->base,
+                                    "GLX_ARB_create_context_robustness");
    }
 }
 
@@ -967,26 +1011,25 @@ dri2CreateScreen(int screen, struct glx_display * priv)
    struct dri2_screen *psc;
    __GLXDRIscreen *psp;
    struct glx_config *configs = NULL, *visuals = NULL;
-   char *driverName, *deviceName;
+   char *driverName, *deviceName, *tmp;
    drm_magic_t magic;
    int i;
 
-   psc = Xmalloc(sizeof *psc);
+   psc = calloc(1, sizeof *psc);
    if (psc == NULL)
       return NULL;
 
-   memset(psc, 0, sizeof *psc);
    psc->fd = -1;
 
    if (!glx_screen_init(&psc->base, screen, priv)) {
-      Xfree(psc);
+      free(psc);
       return NULL;
    }
 
    if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
                    &driverName, &deviceName)) {
       glx_screen_cleanup(&psc->base);
-      XFree(psc);
+      free(psc);
       InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen);
       return NULL;
    }
@@ -1015,7 +1058,15 @@ dri2CreateScreen(int screen, struct glx_display * priv)
       goto handle_error;
    }
 
-   psc->fd = open(deviceName, O_RDWR);
+#ifdef O_CLOEXEC
+   psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
+   if (psc->fd == -1 && errno == EINVAL)
+#endif
+   {
+      psc->fd = open(deviceName, O_RDWR);
+      if (psc->fd != -1)
+         fcntl(psc->fd, F_SETFD, fcntl(psc->fd, F_GETFD) | FD_CLOEXEC);
+   }
    if (psc->fd < 0) {
       ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
       goto handle_error;
@@ -1096,12 +1147,17 @@ dri2CreateScreen(int screen, struct glx_display * priv)
    psp->copySubBuffer = dri2CopySubBuffer;
    __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
 
-   Xfree(driverName);
-   Xfree(deviceName);
+   free(driverName);
+   free(deviceName);
+
+   tmp = getenv("LIBGL_SHOW_FPS");
+   psc->show_fps = tmp && strcmp(tmp, "1") == 0;
 
    return &psc->base;
 
 handle_error:
+   CriticalErrorMessageF("failed to load driver: %s\n", driverName);
+
    if (configs)
        glx_config_destroy_list(configs);
    if (visuals)
@@ -1114,10 +1170,10 @@ handle_error:
    if (psc->driver)
       dlclose(psc->driver);
 
-   Xfree(driverName);
-   Xfree(deviceName);
+   free(driverName);
+   free(deviceName);
    glx_screen_cleanup(&psc->base);
-   XFree(psc);
+   free(psc);
 
    return NULL;
 }
@@ -1130,7 +1186,7 @@ dri2DestroyDisplay(__GLXDRIdisplay * dpy)
    struct dri2_display *pdp = (struct dri2_display *) dpy;
 
    __glxHashDestroy(pdp->dri2Hash);
-   Xfree(dpy);
+   free(dpy);
 }
 
 _X_HIDDEN __GLXDRIdrawable *
@@ -1160,12 +1216,12 @@ dri2CreateDisplay(Display * dpy)
    if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
       return NULL;
 
-   pdp = Xmalloc(sizeof *pdp);
+   pdp = malloc(sizeof *pdp);
    if (pdp == NULL)
       return NULL;
 
    if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
-      Xfree(pdp);
+      free(pdp);
       return NULL;
    }
 
@@ -1191,7 +1247,7 @@ dri2CreateDisplay(Display * dpy)
 
    pdp->dri2Hash = __glxHashCreate();
    if (pdp->dri2Hash == NULL) {
-      Xfree(pdp);
+      free(pdp);
       return NULL;
    }