loader/dri3: Don't wait for fence of old buffer when re-allocating it
[mesa.git] / src / loader / loader_dri3_helper.c
index b8eb87f5aaeb486d00392b8dc2b5a71531806b1c..f641a34e6d1e036de73dbaaa2c3aeec49a742851 100644 (file)
@@ -64,6 +64,55 @@ dri3_flush_present_events(struct loader_dri3_drawable *draw);
 static struct loader_dri3_buffer *
 dri3_find_back_alloc(struct loader_dri3_drawable *draw);
 
+static xcb_screen_t *
+get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
+{
+   xcb_screen_iterator_t screen_iter =
+   xcb_setup_roots_iterator(xcb_get_setup(conn));
+
+   for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
+      if (screen_iter.data->root == root)
+         return screen_iter.data;
+   }
+
+   return NULL;
+}
+
+static xcb_visualtype_t *
+get_xcb_visualtype_for_depth(struct loader_dri3_drawable *draw, int depth)
+{
+   xcb_visualtype_iterator_t visual_iter;
+   xcb_screen_t *screen = draw->screen;
+   xcb_depth_iterator_t depth_iter;
+
+   if (!screen)
+      return NULL;
+
+   depth_iter = xcb_screen_allowed_depths_iterator(screen);
+   for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
+      if (depth_iter.data->depth != depth)
+         continue;
+
+      visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
+      if (visual_iter.rem)
+         return visual_iter.data;
+   }
+
+   return NULL;
+}
+
+/* Get red channel mask for given drawable at given depth. */
+static unsigned int
+dri3_get_red_mask_for_depth(struct loader_dri3_drawable *draw, int depth)
+{
+   xcb_visualtype_t *visual = get_xcb_visualtype_for_depth(draw, depth);
+
+   if (visual)
+      return visual->red_mask;
+
+   return 0;
+}
+
 /**
  * Do we have blit functionality in the image blit extension?
  *
@@ -323,6 +372,7 @@ loader_dri3_drawable_init(xcb_connection_t *conn,
       return 1;
    }
 
+   draw->screen = get_screen_for_root(draw->conn, reply->root);
    draw->width = reply->width;
    draw->height = reply->height;
    draw->depth = reply->depth;
@@ -394,7 +444,7 @@ dri3_handle_present_event(struct loader_dri3_drawable *draw,
             }
          }
 
-        /* If the server tells us that our allocation is suboptimal, we
+         /* If the server tells us that our allocation is suboptimal, we
           * reallocate once.
           */
 #ifdef HAVE_DRI3_MODIFIERS
@@ -1030,6 +1080,36 @@ dri3_cpp_for_format(uint32_t format) {
    }
 }
 
+/* Map format of render buffer to corresponding format for the linear_buffer
+ * used for sharing with the display gpu of a Prime setup (== is_different_gpu).
+ * Usually linear_format == format, except for depth >= 30 formats, where
+ * different gpu vendors have different preferences wrt. color channel ordering.
+ */
+static uint32_t
+dri3_linear_format_for_format(struct loader_dri3_drawable *draw, uint32_t format)
+{
+   switch (format) {
+      case  __DRI_IMAGE_FORMAT_XRGB2101010:
+      case  __DRI_IMAGE_FORMAT_XBGR2101010:
+         /* Different preferred formats for different hw */
+         if (dri3_get_red_mask_for_depth(draw, 30) == 0x3ff)
+            return __DRI_IMAGE_FORMAT_XBGR2101010;
+         else
+            return __DRI_IMAGE_FORMAT_XRGB2101010;
+
+      case  __DRI_IMAGE_FORMAT_ARGB2101010:
+      case  __DRI_IMAGE_FORMAT_ABGR2101010:
+         /* Different preferred formats for different hw */
+         if (dri3_get_red_mask_for_depth(draw, 30) == 0x3ff)
+            return __DRI_IMAGE_FORMAT_ABGR2101010;
+         else
+            return __DRI_IMAGE_FORMAT_ARGB2101010;
+
+      default:
+         return format;
+   }
+}
+
 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
  * the createImageFromFds call takes __DRI_IMAGE_FOURCC codes. To avoid
  * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
@@ -1227,7 +1307,8 @@ dri3_alloc_render_buffer(struct loader_dri3_drawable *draw, unsigned int format,
 
       buffer->linear_buffer =
         draw->ext->image->createImage(draw->dri_screen,
-                                      width, height, format,
+                                      width, height,
+                                      dri3_linear_format_for_format(draw, format),
                                       __DRI_IMAGE_USE_SHARE |
                                       __DRI_IMAGE_USE_LINEAR |
                                       __DRI_IMAGE_USE_BACKBUFFER,
@@ -1346,8 +1427,7 @@ no_shm_fence:
  * track the geometry of the drawable
  */
 static int
-dri3_update_drawable(__DRIdrawable *driDrawable,
-                     struct loader_dri3_drawable *draw)
+dri3_update_drawable(struct loader_dri3_drawable *draw)
 {
    mtx_lock(&draw->mtx);
    if (draw->first_init) {
@@ -1697,7 +1777,6 @@ dri3_get_buffer(__DRIdrawable *driDrawable,
           && buffer) {
 
          /* Fill the new buffer with data from an old buffer */
-         dri3_fence_await(draw->conn, draw, buffer);
          if (!loader_dri3_blit_image(draw,
                                      new_buffer->image,
                                      buffer->image,
@@ -1738,7 +1817,9 @@ dri3_get_buffer(__DRIdrawable *driDrawable,
       buffer = new_buffer;
       draw->buffers[buf_id] = buffer;
    }
-   dri3_fence_await(draw->conn, draw, buffer);
+
+   if (buffer_type == loader_dri3_buffer_back)
+      dri3_fence_await(draw->conn, draw, buffer);
 
    /*
     * Do we need to preserve the content of a previous buffer?
@@ -1830,7 +1911,7 @@ loader_dri3_get_buffers(__DRIdrawable *driDrawable,
    front = NULL;
    back = NULL;
 
-   if (!dri3_update_drawable(driDrawable, draw))
+   if (!dri3_update_drawable(draw))
       return false;
 
    dri3_update_num_back(draw);
@@ -1985,7 +2066,7 @@ dri3_find_back_alloc(struct loader_dri3_drawable *draw)
    back = draw->buffers[id];
    /* Allocate a new back if we haven't got one */
    if (!back && draw->back_format != __DRI_IMAGE_FORMAT_NONE &&
-       dri3_update_drawable(draw->dri_drawable, draw))
+       dri3_update_drawable(draw))
       back = dri3_alloc_render_buffer(draw, draw->back_format,
                                       draw->width, draw->height, draw->depth);