egl/x11: pick the user requested screen
[mesa.git] / src / egl / drivers / dri2 / platform_x11.c
index c525b58341159ae4e480992b6ccb95c4ff6d1098..538cffd6a76d514b3ea16a98f80a09de79d6f3e1 100644 (file)
@@ -267,7 +267,8 @@ dri2_x11_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
       return NULL;
    }
    
-   if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, false))
+   if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
+                          false, native_surface))
       goto cleanup_surf;
 
    dri2_surf->region = XCB_NONE;
@@ -449,11 +450,11 @@ dri2_x11_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
  * have.
  */
 static EGLBoolean
-dri2_query_surface(_EGLDriver *drv, _EGLDisplay *dpy,
+dri2_query_surface(_EGLDriver *drv, _EGLDisplay *disp,
                    _EGLSurface *surf, EGLint attribute,
                    EGLint *value)
 {
-   struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
    int x, y, w, h;
 
@@ -470,7 +471,7 @@ dri2_query_surface(_EGLDriver *drv, _EGLDisplay *dpy,
    default:
       break;
    }
-   return _eglQuerySurface(drv, dpy, surf, attribute, value);
+   return _eglQuerySurface(drv, disp, surf, attribute, value);
 }
 
 /**
@@ -766,6 +767,18 @@ dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
    return dri2_x11_do_authenticate(dri2_dpy, id);
 }
 
+static bool
+dri2_x11_config_match_attrib(struct dri2_egl_display *dri2_dpy,
+                             const __DRIconfig *config,
+                             unsigned int attrib,
+                             unsigned int value)
+{
+   uint32_t config_val;
+   if (!dri2_dpy->core->getConfigAttrib(config, attrib, &config_val))
+      return false;
+   return config_val == value;
+}
+
 static EGLBoolean
 dri2_x11_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
                                  _EGLDisplay *disp, bool supports_preserved)
@@ -844,6 +857,41 @@ dri2_x11_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
       xcb_depth_next(&d);
    }
 
+   /* Add a 565-no-depth-no-stencil pbuffer-only config.  If X11 is depth 24,
+    * we wouldn't have 565 available, which the CTS demands.
+    */
+   for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
+      const __DRIconfig *config = dri2_dpy->driver_configs[j];
+      const EGLint config_attrs[] = {
+         EGL_NATIVE_VISUAL_ID,    0,
+         EGL_NATIVE_VISUAL_TYPE,  EGL_NONE,
+         EGL_NONE
+      };
+      EGLint surface_type = EGL_PBUFFER_BIT;
+      unsigned int rgba_masks[4] = {
+         0x1f << 11,
+         0x3f << 5,
+         0x1f << 0,
+         0,
+      };
+
+      /* Check that we've found single-sample, no depth, no stencil. */
+      if (!dri2_x11_config_match_attrib(dri2_dpy, config,
+                                        __DRI_ATTRIB_DEPTH_SIZE, 0) ||
+          !dri2_x11_config_match_attrib(dri2_dpy, config,
+                                        __DRI_ATTRIB_STENCIL_SIZE, 0) ||
+          !dri2_x11_config_match_attrib(dri2_dpy, config,
+                                        __DRI_ATTRIB_SAMPLES, 0)) {
+         continue;
+      }
+
+      if (dri2_add_config(disp, config, config_count + 1, surface_type,
+                          config_attrs, rgba_masks)) {
+         config_count++;
+         break;
+      }
+   }
+
    if (!config_count) {
       _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
       return EGL_FALSE;
@@ -898,10 +946,6 @@ dri2_x11_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
    xcb_dri2_swap_buffers_reply_t *reply;
    int64_t swap_count = -1;
 
-   /* No-op for a pixmap or pbuffer surface */
-   if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
-      return 0;
-
    if (draw->SwapBehavior == EGL_BUFFER_PRESERVED || !dri2_dpy->swap_available) {
       swap_count = dri2_copy_region(drv, disp, draw, dri2_surf->region) ? 0 : -1;
    } else {
@@ -1233,21 +1277,34 @@ static const __DRIextension *swrast_loader_extensions[] = {
    NULL,
 };
 
+static int
+dri2_find_screen_for_display(const _EGLDisplay *disp, int fallback_screen)
+{
+   const EGLAttrib *attr;
+
+   for (attr = disp->Options.Attribs; attr; attr += 2) {
+      if (attr[0] == EGL_PLATFORM_X11_SCREEN_EXT)
+         return attr[1];
+   }
+
+   return fallback_screen;
+}
+
 static EGLBoolean
 dri2_get_xcb_connection(_EGLDriver *drv, _EGLDisplay *disp,
                         struct dri2_egl_display *dri2_dpy)
 {
    xcb_screen_iterator_t s;
-   int screen = (uintptr_t)disp->Options.Platform;
+   int screen;
    const char *msg;
 
    disp->DriverData = (void *) dri2_dpy;
    if (disp->PlatformDisplay == NULL) {
       dri2_dpy->conn = xcb_connect(NULL, &screen);
       dri2_dpy->own_device = true;
+      screen = dri2_find_screen_for_display(disp, screen);
    } else {
       Display *dpy = disp->PlatformDisplay;
-
       dri2_dpy->conn = XGetXCBConnection(dpy);
       screen = DefaultScreen(dpy);
    }
@@ -1275,6 +1332,7 @@ disconnect:
 static EGLBoolean
 dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
 {
+   _EGLDevice *dev;
    struct dri2_egl_display *dri2_dpy;
 
    dri2_dpy = calloc(1, sizeof *dri2_dpy);
@@ -1285,6 +1343,14 @@ dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
    if (!dri2_get_xcb_connection(drv, disp, dri2_dpy))
       goto cleanup;
 
+   dev = _eglAddDevice(dri2_dpy->fd, true);
+   if (!dev) {
+      _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
+      goto cleanup;
+   }
+
+   disp->Device = dev;
+
    /*
     * Every hardware driver_name is set using strdup. Doing the same in
     * here will allow is to simply free the memory at dri2_terminate().
@@ -1353,6 +1419,7 @@ static const __DRIextension *dri3_image_loader_extensions[] = {
 static EGLBoolean
 dri2_initialize_x11_dri3(_EGLDriver *drv, _EGLDisplay *disp)
 {
+   _EGLDevice *dev;
    struct dri2_egl_display *dri2_dpy;
 
    dri2_dpy = calloc(1, sizeof *dri2_dpy);
@@ -1366,6 +1433,14 @@ dri2_initialize_x11_dri3(_EGLDriver *drv, _EGLDisplay *disp)
    if (!dri3_x11_connect(dri2_dpy))
       goto cleanup;
 
+   dev = _eglAddDevice(dri2_dpy->fd, false);
+   if (!dev) {
+      _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
+      goto cleanup;
+   }
+
+   disp->Device = dev;
+
    if (!dri2_load_driver_dri3(disp))
       goto cleanup;
 
@@ -1451,6 +1526,7 @@ static const __DRIextension *dri2_loader_extensions[] = {
 static EGLBoolean
 dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
 {
+   _EGLDevice *dev;
    struct dri2_egl_display *dri2_dpy;
 
    dri2_dpy = calloc(1, sizeof *dri2_dpy);
@@ -1464,6 +1540,14 @@ dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
    if (!dri2_x11_connect(dri2_dpy))
       goto cleanup;
 
+   dev = _eglAddDevice(dri2_dpy->fd, false);
+   if (!dev) {
+      _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
+      goto cleanup;
+   }
+
+   disp->Device = dev;
+
    if (!dri2_load_driver(disp))
       goto cleanup;