egl/gbm: Fix config validation
[mesa.git] / src / egl / drivers / dri2 / platform_drm.c
index 416fd4cc657bb975e3781c1a243af536c00fb34c..d0ab172d65901d11e8b672c5e1054ce6f6e8d2bc 100644 (file)
@@ -43,9 +43,9 @@
 static struct gbm_bo *
 lock_front_buffer(struct gbm_surface *_surf)
 {
-   struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
+   struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
    struct dri2_egl_surface *dri2_surf = surf->dri_private;
-   struct gbm_dri_device *device = (struct gbm_dri_device *) _surf->gbm;
+   struct gbm_dri_device *device = gbm_dri_device(_surf->gbm);
    struct gbm_bo *bo;
 
    if (dri2_surf->current == NULL) {
@@ -66,7 +66,7 @@ lock_front_buffer(struct gbm_surface *_surf)
 static void
 release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
 {
-   struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
+   struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
    struct dri2_egl_surface *dri2_surf = surf->dri_private;
 
    for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
@@ -80,7 +80,7 @@ release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
 static int
 has_free_buffers(struct gbm_surface *_surf)
 {
-   struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
+   struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
    struct dri2_egl_surface *dri2_surf = surf->dri_private;
 
    for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
@@ -90,6 +90,52 @@ has_free_buffers(struct gbm_surface *_surf)
    return 0;
 }
 
+static bool
+dri2_drm_config_is_compatible(struct dri2_egl_display *dri2_dpy,
+                              const __DRIconfig *config,
+                              struct gbm_surface *surface)
+{
+   const struct gbm_dri_visual *visual = NULL;
+   int shifts[4];
+   unsigned int sizes[4];
+   bool is_float;
+   int i;
+
+   /* Check that the EGLConfig being used to render to the surface is
+    * compatible with the surface format. Since mixing ARGB and XRGB of
+    * otherwise-compatible formats is relatively common, explicitly allow
+    * this.
+    */
+   dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
+
+   dri2_get_render_type_float(dri2_dpy->core, config, &is_float);
+
+   for (i = 0; i < dri2_dpy->gbm_dri->num_visuals; i++) {
+      visual = &dri2_dpy->gbm_dri->visual_table[i];
+      if (visual->gbm_format == surface->format)
+         break;
+   }
+
+   if (i == dri2_dpy->gbm_dri->num_visuals)
+      return false;
+
+   if (shifts[0] != visual->rgba_shifts.red ||
+       shifts[1] != visual->rgba_shifts.green ||
+       shifts[2] != visual->rgba_shifts.blue ||
+       (shifts[3] > -1 && visual->rgba_shifts.alpha > -1 &&
+        shifts[3] != visual->rgba_shifts.alpha) ||
+       sizes[0] != visual->rgba_sizes.red ||
+       sizes[1] != visual->rgba_sizes.green ||
+       sizes[2] != visual->rgba_sizes.blue ||
+       (sizes[3] > 0 && visual->rgba_sizes.alpha > 0 &&
+        sizes[3] != visual->rgba_sizes.alpha) ||
+       is_float != visual->is_float) {
+      return false;
+   }
+
+   return true;
+}
+
 static _EGLSurface *
 dri2_drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
                                _EGLConfig *conf, void *native_surface,
@@ -110,35 +156,31 @@ dri2_drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
       return NULL;
    }
 
-   if (!dri2_init_surface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf, attrib_list, false))
+   if (!dri2_init_surface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf,
+                          attrib_list, false, native_surface))
       goto cleanup_surf;
 
-   surf = gbm_dri_surface(surface);
-   dri2_surf->gbm_surf = surf;
-   dri2_surf->base.Width =  surf->base.width;
-   dri2_surf->base.Height = surf->base.height;
-   surf->dri_private = dri2_surf;
-
    config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
                                 dri2_surf->base.GLColorspace);
 
-   if (dri2_dpy->dri2) {
-      dri2_surf->dri_drawable =
-         dri2_dpy->dri2->createNewDrawable(dri2_dpy->dri_screen, config,
-                                           dri2_surf->gbm_surf);
+   if (!config) {
+      _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
+      goto cleanup_surf;
+   }
 
-   } else {
-      assert(dri2_dpy->swrast != NULL);
+   if (!dri2_drm_config_is_compatible(dri2_dpy, config, surface)) {
+      _eglError(EGL_BAD_MATCH, "EGL config not compatible with GBM format");
+      goto cleanup_surf;
+   }
 
-      dri2_surf->dri_drawable =
-         dri2_dpy->swrast->createNewDrawable(dri2_dpy->dri_screen, config,
-                                             dri2_surf->gbm_surf);
+   surf = gbm_dri_surface(surface);
+   dri2_surf->gbm_surf = surf;
+   dri2_surf->base.Width =  surf->base.width;
+   dri2_surf->base.Height = surf->base.height;
+   surf->dri_private = dri2_surf;
 
-   }
-   if (dri2_surf->dri_drawable == NULL) {
-      _eglError(EGL_BAD_ALLOC, "createNewDrawable()");
+   if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf->gbm_surf))
       goto cleanup_surf;
-   }
 
    return &dri2_surf->base;
 
@@ -256,7 +298,7 @@ back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
    struct gbm_dri_bo *bo;
    int name, pitch;
 
-   bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
+   bo = gbm_dri_bo(dri2_surf->back->bo);
 
    dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
    dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
@@ -360,7 +402,7 @@ dri2_drm_image_get_buffers(__DRIdrawable *driDrawable,
    if (get_back_bo(dri2_surf) < 0)
       return 0;
 
-   bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
+   bo = gbm_dri_bo(dri2_surf->back->bo);
    buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
    buffers->back = bo->image;
 
@@ -385,22 +427,20 @@ dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
       return EGL_TRUE;
    }
 
-   if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
-      if (dri2_surf->current)
-         _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
-      for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
-         if (dri2_surf->color_buffers[i].age > 0)
-            dri2_surf->color_buffers[i].age++;
-
-      /* Make sure we have a back buffer in case we're swapping without
-       * ever rendering. */
-      if (get_back_bo(dri2_surf) < 0)
-         return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
-
-      dri2_surf->current = dri2_surf->back;
-      dri2_surf->current->age = 1;
-      dri2_surf->back = NULL;
-   }
+   if (dri2_surf->current)
+      _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
+   for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
+      if (dri2_surf->color_buffers[i].age > 0)
+         dri2_surf->color_buffers[i].age++;
+
+   /* Make sure we have a back buffer in case we're swapping without
+    * ever rendering. */
+   if (get_back_bo(dri2_surf) < 0)
+      return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
+
+   dri2_surf->current = dri2_surf->back;
+   dri2_surf->current->age = 1;
+   dri2_surf->back = NULL;
 
    dri2_flush_drawable_for_swapbuffers(disp, draw);
    dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
@@ -572,42 +612,44 @@ static EGLBoolean
 drm_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
-   static const struct {
-      int format;
-      unsigned int red_mask;
-      unsigned int alpha_mask;
-   } visuals[] = {
-      { GBM_FORMAT_XRGB2101010, 0x3ff00000, 0x00000000 },
-      { GBM_FORMAT_ARGB2101010, 0x3ff00000, 0xc0000000 },
-      { GBM_FORMAT_XRGB8888,    0x00ff0000, 0x00000000 },
-      { GBM_FORMAT_ARGB8888,    0x00ff0000, 0xff000000 },
-      { GBM_FORMAT_RGB565,      0x0000f800, 0x00000000 },
-   };
-
-   unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
+   const struct gbm_dri_visual *visuals = dri2_dpy->gbm_dri->visual_table;
+   int num_visuals = dri2_dpy->gbm_dri->num_visuals;
+   unsigned int format_count[num_visuals];
    unsigned int config_count = 0;
 
+   memset(format_count, 0, num_visuals * sizeof(unsigned int));
+
    for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
-      unsigned int red, alpha;
+      const __DRIconfig *config = dri2_dpy->driver_configs[i];
+      int shifts[4];
+      unsigned int sizes[4];
+      bool is_float;
+
+      dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
 
-      dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
-                                      __DRI_ATTRIB_RED_MASK, &red);
-      dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
-                                      __DRI_ATTRIB_ALPHA_MASK, &alpha);
+      dri2_get_render_type_float(dri2_dpy->core, config, &is_float);
 
-      for (unsigned j = 0; j < ARRAY_SIZE(visuals); j++) {
+      for (unsigned j = 0; j < num_visuals; j++) {
          struct dri2_egl_config *dri2_conf;
 
-         if (visuals[j].red_mask != red || visuals[j].alpha_mask != alpha)
+         if (visuals[j].rgba_shifts.red != shifts[0] ||
+             visuals[j].rgba_shifts.green != shifts[1] ||
+             visuals[j].rgba_shifts.blue != shifts[2] ||
+             visuals[j].rgba_shifts.alpha != shifts[3] ||
+             visuals[j].rgba_sizes.red != sizes[0] ||
+             visuals[j].rgba_sizes.green != sizes[1] ||
+             visuals[j].rgba_sizes.blue != sizes[2] ||
+             visuals[j].rgba_sizes.alpha != sizes[3] ||
+             visuals[j].is_float != is_float)
             continue;
 
          const EGLint attr_list[] = {
-            EGL_NATIVE_VISUAL_ID,  visuals[j].format,
+            EGL_NATIVE_VISUAL_ID, visuals[j].gbm_format,
             EGL_NONE,
          };
 
          dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
-               config_count + 1, EGL_WINDOW_BIT, attr_list, NULL);
+               config_count + 1, EGL_WINDOW_BIT, attr_list, NULL, NULL);
          if (dri2_conf) {
             if (dri2_conf->base.ConfigID == config_count + 1)
                config_count++;
@@ -618,8 +660,9 @@ drm_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
 
    for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
       if (!format_count[i]) {
-         _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
-                 visuals[i].format);
+         struct gbm_format_name_desc desc;
+         _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
+                 gbm_format_get_name(visuals[i].gbm_format, &desc));
       }
    }
 
@@ -636,7 +679,6 @@ static const struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
    .swap_buffers = dri2_drm_swap_buffers,
    .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
    .swap_buffers_region = dri2_fallback_swap_buffers_region,
-   .set_damage_region = dri2_fallback_set_damage_region,
    .post_sub_buffer = dri2_fallback_post_sub_buffer,
    .copy_buffers = dri2_fallback_copy_buffers,
    .query_buffer_age = dri2_drm_query_buffer_age,
@@ -648,11 +690,14 @@ static const struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
 EGLBoolean
 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
 {
+   _EGLDevice *dev;
    struct dri2_egl_display *dri2_dpy;
    struct gbm_device *gbm;
    const char *err;
 
-   loader_set_logger(_eglLog);
+   /* Not supported yet */
+   if (disp->Options.ForceSoftware)
+      return EGL_FALSE;
 
    dri2_dpy = calloc(1, sizeof *dri2_dpy);
    if (!dri2_dpy)
@@ -680,14 +725,37 @@ dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
          goto cleanup;
       }
    }
+   dri2_dpy->gbm_dri = gbm_dri_device(gbm);
 
    if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
       err = "DRI2: gbm device using incorrect/incompatible backend";
       goto cleanup;
    }
 
-   dri2_dpy->gbm_dri = gbm_dri_device(gbm);
+   dev = _eglAddDevice(dri2_dpy->fd, false);
+   if (!dev) {
+      err = "DRI2: failed to find EGLDevice";
+      goto cleanup;
+   }
+
+   disp->Device = dev;
+
    dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->driver_name);
+   dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
+
+   /* render nodes cannot use Gem names, and thus do not support
+    * the __DRI_DRI2_LOADER extension */
+   if (!dri2_dpy->is_render_node) {
+      if (!dri2_load_driver(disp)) {
+         err = "DRI2: failed to load driver";
+         goto cleanup;
+      }
+   } else {
+      if (!dri2_load_driver_dri3(disp)) {
+         err = "DRI3: failed to load driver";
+         goto cleanup;
+      }
+   }
 
    dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
    dri2_dpy->core = dri2_dpy->gbm_dri->core;