egl: Let the caller of dri2_create_drawable decide about loaderPrivate.
[mesa.git] / src / egl / drivers / dri2 / platform_android.c
index de53376c9fc35c0d8041de083e97d467eb78f35d..db6ba4a4b4d63a5acafec93e4d9aafb6b0937066 100644 (file)
 
 #define ALIGN(val, align)      (((val) + (align) - 1) & ~((align) - 1))
 
+enum chroma_order {
+   YCbCr,
+   YCrCb,
+};
+
 struct droid_yuv_format {
    /* Lookup keys */
    int native; /* HAL_PIXEL_FORMAT_ */
-   int is_ycrcb; /* 0 if chroma order is {Cb, Cr}, 1 if {Cr, Cb} */
+   enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
    int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
 
    /* Result */
@@ -63,23 +68,25 @@ struct droid_yuv_format {
  * on native format and information contained in android_ycbcr struct. */
 static const struct droid_yuv_format droid_yuv_formats[] = {
    /* Native format, YCrCb, Chroma step, DRI image FourCC */
-   { HAL_PIXEL_FORMAT_YCbCr_420_888,   0, 2, __DRI_IMAGE_FOURCC_NV12 },
-   { HAL_PIXEL_FORMAT_YCbCr_420_888,   0, 1, __DRI_IMAGE_FOURCC_YUV420 },
-   { HAL_PIXEL_FORMAT_YCbCr_420_888,   1, 1, __DRI_IMAGE_FOURCC_YVU420 },
-   { HAL_PIXEL_FORMAT_YV12,            1, 1, __DRI_IMAGE_FOURCC_YVU420 },
+   { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, __DRI_IMAGE_FOURCC_NV12 },
+   { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, __DRI_IMAGE_FOURCC_YUV420 },
+   { HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, __DRI_IMAGE_FOURCC_YVU420 },
+   { HAL_PIXEL_FORMAT_YV12,          YCrCb, 1, __DRI_IMAGE_FOURCC_YVU420 },
    /* HACK: See droid_create_image_from_prime_fd() and
     * https://issuetracker.google.com/32077885. */
-   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,   0, 2, __DRI_IMAGE_FOURCC_NV12 },
-   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,   0, 1, __DRI_IMAGE_FOURCC_YUV420 },
-   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,   1, 1, __DRI_IMAGE_FOURCC_YVU420 },
+   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, __DRI_IMAGE_FOURCC_NV12 },
+   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, __DRI_IMAGE_FOURCC_YUV420 },
+   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, __DRI_IMAGE_FOURCC_YVU420 },
+   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, __DRI_IMAGE_FOURCC_AYUV },
+   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, __DRI_IMAGE_FOURCC_XYUV8888 },
 };
 
 static int
-get_fourcc_yuv(int native, int is_ycrcb, int chroma_step)
+get_fourcc_yuv(int native, enum chroma_order chroma_order, int chroma_step)
 {
    for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
       if (droid_yuv_formats[i].native == native &&
-          droid_yuv_formats[i].is_ycrcb == is_ycrcb &&
+          droid_yuv_formats[i].chroma_order == chroma_order &&
           droid_yuv_formats[i].chroma_step == chroma_step)
          return droid_yuv_formats[i].fourcc;
 
@@ -334,7 +341,6 @@ droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
                    _EGLConfig *conf, void *native_window,
                    const EGLint *attrib_list)
 {
-   __DRIcreateNewDrawableFunc createNewDrawable;
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
    struct dri2_egl_surface *dri2_surf;
@@ -347,7 +353,8 @@ droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
       return NULL;
    }
 
-   if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, true))
+   if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
+                          true, native_window))
       goto cleanup_surface;
 
    if (type == EGL_WINDOW_BIT) {
@@ -378,17 +385,8 @@ droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
       goto cleanup_surface;
    }
 
-   if (dri2_dpy->image_driver)
-      createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
-   else
-      createNewDrawable = dri2_dpy->dri2->createNewDrawable;
-
-   dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
-                                                  dri2_surf);
-   if (dri2_surf->dri_drawable == NULL) {
-      _eglError(EGL_BAD_ALLOC, "createNewDrawable");
+   if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
       goto cleanup_surface;
-   }
 
    if (window) {
       window->common.incRef(&window->common);
@@ -456,7 +454,7 @@ droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
 }
 
 static EGLBoolean
-droid_swap_interval(_EGLDriver *drv, _EGLDisplay *dpy,
+droid_swap_interval(_EGLDriver *drv, _EGLDisplay *disp,
                    _EGLSurface *surf, EGLint interval)
 {
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
@@ -676,10 +674,6 @@ droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
-
-   if (dri2_surf->base.Type != EGL_WINDOW_BIT)
-      return EGL_TRUE;
-
    const bool has_mutable_rb = _eglSurfaceHasMutableRenderBuffer(draw);
 
    /* From the EGL_KHR_mutable_render_buffer spec (v12):
@@ -779,7 +773,7 @@ droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
    struct android_ycbcr ycbcr;
    size_t offsets[3];
    size_t pitches[3];
-   int is_ycrcb;
+   enum chroma_order chroma_order;
    int fourcc;
    int ret;
 
@@ -807,11 +801,12 @@ droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
     * so they can be interpreted as offsets. */
    offsets[0] = (size_t)ycbcr.y;
    /* We assume here that all the planes are located in one DMA-buf. */
-   is_ycrcb = (size_t)ycbcr.cr < (size_t)ycbcr.cb;
-   if (is_ycrcb) {
+   if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) {
+      chroma_order = YCrCb;
       offsets[1] = (size_t)ycbcr.cr;
       offsets[2] = (size_t)ycbcr.cb;
    } else {
+      chroma_order = YCbCr;
       offsets[1] = (size_t)ycbcr.cb;
       offsets[2] = (size_t)ycbcr.cr;
    }
@@ -825,10 +820,10 @@ droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
 
    /* .chroma_step is the byte distance between the same chroma channel
     * values of subsequent pixels, assumed to be the same for Cb and Cr. */
-   fourcc = get_fourcc_yuv(buf->format, is_ycrcb, ycbcr.chroma_step);
+   fourcc = get_fourcc_yuv(buf->format, chroma_order, ycbcr.chroma_step);
    if (fourcc == -1) {
-      _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
-              buf->format, is_ycrcb, ycbcr.chroma_step);
+      _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d",
+              buf->format, chroma_order == YCbCr ? "YCbCr" : "YCrCb", ycbcr.chroma_step);
       return NULL;
    }
 
@@ -963,7 +958,7 @@ droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
 #endif /* HAVE_DRM_GRALLOC */
 
 static EGLBoolean
-droid_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
+droid_query_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
                     EGLint attribute, EGLint *value)
 {
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
@@ -985,7 +980,7 @@ droid_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
       default:
          break;
    }
-   return _eglQuerySurface(drv, dpy, surf, attribute, value);
+   return _eglQuerySurface(drv, disp, surf, attribute, value);
 }
 
 static _EGLImage *
@@ -1134,9 +1129,9 @@ droid_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
 }
 
 static EGLBoolean
-droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
+droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
 {
-   struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    static const struct {
       int format;
       unsigned int rgba_masks[4];
@@ -1181,7 +1176,7 @@ droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
          };
 
          struct dri2_egl_config *dri2_conf =
-            dri2_add_config(dpy, dri2_dpy->driver_configs[j],
+            dri2_add_config(disp, dri2_dpy->driver_configs[j],
                             config_count + 1, surface_type, config_attrs,
                             visuals[i].rgba_masks);
          if (dri2_conf) {
@@ -1202,25 +1197,6 @@ droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
    return (config_count != 0);
 }
 
-#ifdef HAVE_DRM_GRALLOC
-static int
-droid_open_device_drm_gralloc(struct dri2_egl_display *dri2_dpy)
-{
-   int fd = -1, err = -EINVAL;
-
-   if (dri2_dpy->gralloc->perform)
-         err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
-                                          GRALLOC_MODULE_PERFORM_GET_DRM_FD,
-                                          &fd);
-   if (err || fd < 0) {
-      _eglLog(_EGL_WARNING, "fail to get drm fd");
-      fd = -1;
-   }
-
-   return (fd >= 0) ? fcntl(fd, F_DUPFD_CLOEXEC, 3) : -1;
-}
-#endif /* HAVE_DRM_GRALLOC */
-
 static const struct dri2_egl_display_vtbl droid_display_vtbl = {
    .authenticate = NULL,
    .create_window_surface = droid_create_window_surface,
@@ -1256,17 +1232,7 @@ static const __DRIdri2LoaderExtension droid_dri2_loader_extension = {
    .getBuffersWithFormat = droid_get_buffers_with_format,
    .getCapability        = droid_get_capability,
 };
-#endif /* HAVE_DRM_GRALLOC */
-
-static const __DRIimageLoaderExtension droid_image_loader_extension = {
-   .base = { __DRI_IMAGE_LOADER, 2 },
 
-   .getBuffers          = droid_image_get_buffers,
-   .flushFrontBuffer    = droid_flush_front_buffer,
-   .getCapability       = droid_get_capability,
-};
-
-#ifdef HAVE_DRM_GRALLOC
 static const __DRIextension *droid_dri2_loader_extensions[] = {
    &droid_dri2_loader_extension.base,
    &image_lookup_extension.base,
@@ -1278,6 +1244,14 @@ static const __DRIextension *droid_dri2_loader_extensions[] = {
 };
 #endif /* HAVE_DRM_GRALLOC */
 
+static const __DRIimageLoaderExtension droid_image_loader_extension = {
+   .base = { __DRI_IMAGE_LOADER, 2 },
+
+   .getBuffers          = droid_image_get_buffers,
+   .flushFrontBuffer    = droid_flush_front_buffer,
+   .getCapability       = droid_get_capability,
+};
+
 static void
 droid_display_shared_buffer(__DRIdrawable *driDrawable, int fence_fd,
                             void *loaderPrivate)
@@ -1362,8 +1336,8 @@ static const __DRIextension *droid_image_loader_extensions[] = {
    NULL,
 };
 
-EGLBoolean
-droid_load_driver(_EGLDisplay *disp)
+static EGLBoolean
+droid_load_driver(_EGLDisplay *disp, bool swrast)
 {
    struct dri2_egl_display *dri2_dpy = disp->DriverData;
    const char *err;
@@ -1372,29 +1346,37 @@ droid_load_driver(_EGLDisplay *disp)
    if (dri2_dpy->driver_name == NULL)
       return false;
 
-   dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
-
-   if (!dri2_dpy->is_render_node) {
-   #ifdef HAVE_DRM_GRALLOC
-       /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
-        * for backwards compatibility with drm_gralloc. (Do not use on new
-        * systems.) */
-       dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
-       if (!dri2_load_driver(disp)) {
-          err = "DRI2: failed to load driver";
-          goto error;
-       }
-   #else
-       err = "DRI2: handle is not for a render node";
-       goto error;
-   #endif
-   } else {
-       dri2_dpy->loader_extensions = droid_image_loader_extensions;
-       if (!dri2_load_driver_dri3(disp)) {
-          err = "DRI3: failed to load driver";
-          goto error;
-       }
-    }
+#ifdef HAVE_DRM_GRALLOC
+   /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
+    * for backwards compatibility with drm_gralloc. (Do not use on new
+    * systems.) */
+   dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
+   if (!dri2_load_driver(disp)) {
+      err = "DRI2: failed to load driver";
+      goto error;
+   }
+#else
+   if (swrast) {
+      /* Use kms swrast only with vgem / virtio_gpu.
+       * virtio-gpu fallbacks to software rendering when 3D features
+       * are unavailable since 6c5ab.
+       */
+      if (strcmp(dri2_dpy->driver_name, "vgem") == 0 ||
+          strcmp(dri2_dpy->driver_name, "virtio_gpu") == 0) {
+         free(dri2_dpy->driver_name);
+         dri2_dpy->driver_name = strdup("kms_swrast");
+      } else {
+         err = "DRI3: failed to find software capable driver";
+         goto error;
+      }
+   }
+
+   dri2_dpy->loader_extensions = droid_image_loader_extensions;
+   if (!dri2_load_driver_dri3(disp)) {
+      err = "DRI3: failed to load driver";
+      goto error;
+   }
+#endif
 
    return true;
 
@@ -1404,118 +1386,167 @@ error:
    return false;
 }
 
-typedef enum {
-   probe_fail = -1,
-   probe_success = 0,
-   probe_filtered_out = 1,
-} probe_ret_t;
-
-static probe_ret_t
-droid_probe_device(_EGLDisplay *disp, int fd, const char *vendor)
+static void
+droid_unload_driver(_EGLDisplay *disp)
 {
-   int ret;
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+
+   dlclose(dri2_dpy->driver);
+   dri2_dpy->driver = NULL;
+   free(dri2_dpy->driver_name);
+   dri2_dpy->driver_name = NULL;
+}
 
+static int
+droid_filter_device(_EGLDisplay *disp, int fd, const char *vendor)
+{
    drmVersionPtr ver = drmGetVersion(fd);
    if (!ver)
-      return probe_fail;
+      return -1;
 
-   if (!ver->name) {
-      ret = probe_fail;
-      goto cleanup;
+   if (strcmp(vendor, ver->name) != 0) {
+      drmFreeVersion(ver);
+      return -1;
    }
 
-   if (vendor && strcmp(vendor, ver->name) != 0) {
-      ret = probe_filtered_out;
-      goto cleanup;
-   }
+   drmFreeVersion(ver);
+   return 0;
+}
 
-   ret = probe_success;
+static EGLBoolean
+droid_probe_device(_EGLDisplay *disp, bool swrast)
+{
+  /* Check that the device is supported, by attempting to:
+   * - load the dri module
+   * - and, create a screen
+   */
+   if (!droid_load_driver(disp, swrast))
+      return EGL_FALSE;
 
-cleanup:
-   drmFreeVersion(ver);
-   return ret;
+   if (!dri2_create_screen(disp)) {
+      _eglLog(_EGL_WARNING, "DRI2: failed to create screen");
+      droid_unload_driver(disp);
+      return EGL_FALSE;
+   }
+   return EGL_TRUE;
 }
 
-static int
-droid_open_device(_EGLDisplay *disp)
+#ifdef HAVE_DRM_GRALLOC
+static EGLBoolean
+droid_open_device(_EGLDisplay *disp, bool swrast)
 {
-#define MAX_DRM_DEVICES 32
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+   int fd = -1, err = -EINVAL;
+
+   if (swrast)
+      return EGL_FALSE;
+
+   if (dri2_dpy->gralloc->perform)
+      err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
+                                       GRALLOC_MODULE_PERFORM_GET_DRM_FD,
+                                       &fd);
+   if (err || fd < 0) {
+      _eglLog(_EGL_WARNING, "fail to get drm fd");
+      return EGL_FALSE;
+   }
+
+   dri2_dpy->fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
+   if (dri2_dpy->fd < 0)
+      return EGL_FALSE;
+
+   if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER)
+      return EGL_FALSE;
+
+   return droid_probe_device(disp, swrast);
+}
+#else
+static EGLBoolean
+droid_open_device(_EGLDisplay *disp, bool swrast)
+{
+#define MAX_DRM_DEVICES 64
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL };
-   int prop_set, num_devices;
-   int fd = -1, fallback_fd = -1;
+   int num_devices;
 
    char *vendor_name = NULL;
    char vendor_buf[PROPERTY_VALUE_MAX];
 
+#ifdef EGL_FORCE_RENDERNODE
+   const unsigned node_type = DRM_NODE_RENDER;
+#else
+   const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
+#endif
+
    if (property_get("drm.gpu.vendor_name", vendor_buf, NULL) > 0)
       vendor_name = vendor_buf;
 
    num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
    if (num_devices < 0)
-      return num_devices;
+      return EGL_FALSE;
 
    for (int i = 0; i < num_devices; i++) {
       device = devices[i];
 
-      if (!(device->available_nodes & (1 << DRM_NODE_RENDER)))
+      if (!(device->available_nodes & (1 << node_type)))
          continue;
 
-      fd = loader_open_device(device->nodes[DRM_NODE_RENDER]);
-      if (fd < 0) {
+      dri2_dpy->fd = loader_open_device(device->nodes[node_type]);
+      if (dri2_dpy->fd < 0) {
          _eglLog(_EGL_WARNING, "%s() Failed to open DRM device %s",
-                 __func__, device->nodes[DRM_NODE_RENDER]);
+                 __func__, device->nodes[node_type]);
          continue;
       }
 
-      int ret = droid_probe_device(disp, fd, vendor_name);
-      switch (ret) {
-      case probe_success:
-         goto success;
-      case probe_filtered_out:
-         /* Set as fallback */
-         if (fallback_fd == -1)
-            fallback_fd = fd;
-         break;
-      case probe_fail:
+      /* If a vendor is explicitly provided, we use only that.
+       * Otherwise we fall-back the first device that is supported.
+       */
+      if (vendor_name) {
+         if (droid_filter_device(disp, dri2_dpy->fd, vendor_name)) {
+            /* Device does not match - try next device */
+            close(dri2_dpy->fd);
+            dri2_dpy->fd = -1;
+            continue;
+         }
+         /* If the requested device matches - use it. Regardless if
+          * init fails, do not fall-back to any other device.
+          */
+         if (!droid_probe_device(disp, false)) {
+            close(dri2_dpy->fd);
+            dri2_dpy->fd = -1;
+         }
+
          break;
       }
+      if (droid_probe_device(disp, swrast))
+         break;
 
-      if (fallback_fd != fd)
-         close(fd);
-      fd = -1;
+      /* No explicit request - attempt the next device */
+      close(dri2_dpy->fd);
+      dri2_dpy->fd = -1;
    }
-
-success:
    drmFreeDevices(devices, num_devices);
 
-   if (fallback_fd < 0 && fd < 0) {
-      _eglLog(_EGL_WARNING, "Failed to open any DRM device");
-      return -1;
-   }
-
-   if (fd < 0) {
-      _eglLog(_EGL_WARNING, "Failed to open desired DRM device, using fallback");
-      return fallback_fd;
+   if (dri2_dpy->fd < 0) {
+      _eglLog(_EGL_WARNING, "Failed to open %s DRM device",
+            vendor_name ? "desired": "any");
+      return EGL_FALSE;
    }
 
-   close(fallback_fd);
-   return fd;
+   return EGL_TRUE;
 #undef MAX_DRM_DEVICES
 }
 
+#endif
+
 EGLBoolean
 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *disp)
 {
+   _EGLDevice *dev;
+   bool device_opened = false;
    struct dri2_egl_display *dri2_dpy;
    const char *err;
    int ret;
 
-   /* Not supported yet */
-   if (disp->Options.ForceSoftware)
-      return EGL_FALSE;
-
-   loader_set_logger(_eglLog);
-
    dri2_dpy = calloc(1, sizeof(*dri2_dpy));
    if (!dri2_dpy)
       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
@@ -1529,26 +1560,23 @@ dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *disp)
    }
 
    disp->DriverData = (void *) dri2_dpy;
+   if (!disp->Options.ForceSoftware)
+      device_opened = droid_open_device(disp, false);
+   if (!device_opened)
+      device_opened = droid_open_device(disp, true);
 
-#ifdef HAVE_DRM_GRALLOC
-   dri2_dpy->fd = droid_open_device_drm_gralloc(dri2_dpy);
-#else
-   dri2_dpy->fd = droid_open_device(disp);
-#endif
-   if (dri2_dpy->fd < 0) {
+   if (!device_opened) {
       err = "DRI2: failed to open device";
       goto cleanup;
    }
 
-   if (!droid_load_driver(disp)) {
-      err = "DRI2: failed to load driver";
+   dev = _eglAddDevice(dri2_dpy->fd, false);
+   if (!dev) {
+      err = "DRI2: failed to find EGLDevice";
       goto cleanup;
    }
 
-   if (!dri2_create_screen(disp)) {
-      err = "DRI2: failed to create screen";
-      goto cleanup;
-   }
+   disp->Device = dev;
 
    if (!dri2_setup_extensions(disp)) {
       err = "DRI2: failed to setup extensions";