[Mesa-dev] [PATCH v2 1/3] egl/x11: factor out dri2_get_xcb_connection()

Emil Velikov emil.l.velikov at gmail.com
Mon Nov 21 13:46:50 UTC 2016


From: Emil Velikov <emil.velikov at collabora.com>

Identical throughout dri2, dri3 and drisw. Next patch will add more
common code, so rather than duplicating it factor out the function.

Note: this also sets eglError on failure. Something that's quite
inconsistent throughout the codebase.

v2: Call xcb_disconnect() on error (Eric)

Note: use xcb_disconnect() even in the xcb_connection_has_error() case
as per the manual:
... memory will not be freed until xcb_disconnect...

Signed-off-by: Emil Velikov <emil.velikov at collabora.com>
Reviewed-by: Eric Engestrom <eric.engestrom at imgtec.com> (v1)
---
 src/egl/drivers/dri2/platform_x11.c | 63 ++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 36 deletions(-)

diff --git a/src/egl/drivers/dri2/platform_x11.c b/src/egl/drivers/dri2/platform_x11.c
index 92c4ead..00613d9 100644
--- a/src/egl/drivers/dri2/platform_x11.c
+++ b/src/egl/drivers/dri2/platform_x11.c
@@ -1180,13 +1180,10 @@ static const __DRIextension *swrast_loader_extensions[] = {
 };
 
 static EGLBoolean
-dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
+dri2_get_xcb_connection(_EGLDriver *drv, _EGLDisplay *disp,
+                        struct dri2_egl_display *dri2_dpy)
 {
-   struct dri2_egl_display *dri2_dpy;
-
-   dri2_dpy = calloc(1, sizeof *dri2_dpy);
-   if (!dri2_dpy)
-      return _eglError(EGL_BAD_ALLOC, "eglInitialize");
+   const char *msg;
 
    disp->DriverData = (void *) dri2_dpy;
    if (disp->PlatformDisplay == NULL) {
@@ -1200,10 +1197,30 @@ dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
    }
 
    if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
-      _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
-      goto cleanup_dpy;
+      msg = "xcb_connect failed";
+      goto disconnect;
    }
 
+   return EGL_TRUE;
+disconnect:
+   if (disp->PlatformDisplay == NULL)
+      xcb_disconnect(dri2_dpy->conn);
+
+   return _eglError(EGL_BAD_ALLOC, msg);
+}
+
+static EGLBoolean
+dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
+{
+   struct dri2_egl_display *dri2_dpy;
+
+   dri2_dpy = calloc(1, sizeof *dri2_dpy);
+   if (!dri2_dpy)
+      return _eglError(EGL_BAD_ALLOC, "eglInitialize");
+
+   if (!dri2_get_xcb_connection(drv, disp, dri2_dpy))
+      goto cleanup_dpy;
+
    /*
     * Every hardware driver_name is set using strdup. Doing the same in
     * here will allow is to simply free the memory at dri2_terminate().
@@ -1308,21 +1325,8 @@ dri2_initialize_x11_dri3(_EGLDriver *drv, _EGLDisplay *disp)
    if (!dri2_dpy)
       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
 
-   disp->DriverData = (void *) dri2_dpy;
-   if (disp->PlatformDisplay == NULL) {
-      dri2_dpy->conn = xcb_connect(0, &dri2_dpy->screen);
-      dri2_dpy->own_device = true;
-   } else {
-      Display *dpy = disp->PlatformDisplay;
-
-      dri2_dpy->conn = XGetXCBConnection(dpy);
-      dri2_dpy->screen = DefaultScreen(dpy);
-   }
-
-   if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
-      _eglLog(_EGL_WARNING, "DRI3: xcb_connect failed");
+   if (!dri2_get_xcb_connection(drv, disp, dri2_dpy))
       goto cleanup_dpy;
-   }
 
    if (!dri3_x11_connect(dri2_dpy))
       goto cleanup_conn;
@@ -1421,21 +1425,8 @@ dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
    if (!dri2_dpy)
       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
 
-   disp->DriverData = (void *) dri2_dpy;
-   if (disp->PlatformDisplay == NULL) {
-      dri2_dpy->conn = xcb_connect(0, &dri2_dpy->screen);
-      dri2_dpy->own_device = true;
-   } else {
-      Display *dpy = disp->PlatformDisplay;
-
-      dri2_dpy->conn = XGetXCBConnection(dpy);
-      dri2_dpy->screen = DefaultScreen(dpy);
-   }
-
-   if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
-      _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
+   if (!dri2_get_xcb_connection(drv, disp, dri2_dpy))
       goto cleanup_dpy;
-   }
 
    if (!dri2_x11_connect(dri2_dpy))
       goto cleanup_conn;
-- 
2.10.2



More information about the mesa-dev mailing list