egl/wayland: use bitset.h for `formats` bit set
authorEric Engestrom <eric.engestrom@intel.com>
Tue, 27 Nov 2018 12:27:45 +0000 (12:27 +0000)
committerEric Engestrom <eric.engestrom@intel.com>
Mon, 1 Jul 2019 17:35:54 +0000 (18:35 +0100)
Currently only 7 formats are supported, but we don't want the 16 limit
(it's an `unsigned`) to hit us by surprise :]

Let's use bitset.h's BITSET magic to allow us to have any number of
formats, with a static assert to make sure we don't forget to update it.

Signed-off-by: Eric Engestrom <eric.engestrom@intel.com>
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
src/egl/drivers/dri2/egl_dri2.h
src/egl/drivers/dri2/platform_wayland.c

index 943ff1808619adde5daba2fa58768a6eac8cb0d9..fa04e3bb616d448f4006221c8b5ea21376506959 100644 (file)
@@ -81,6 +81,9 @@ struct zwp_linux_dmabuf_v1;
 #include "eglsync.h"
 
 #include "util/u_vector.h"
+#include "util/bitset.h"
+
+#define EGL_DRI2_MAX_FORMATS 8
 
 struct wl_buffer;
 
@@ -232,7 +235,7 @@ struct dri2_egl_display
    struct zwp_linux_dmabuf_v1 *wl_dmabuf;
    struct u_vector          *wl_modifiers;
    bool                      authenticated;
-   unsigned                  formats;
+   BITSET_DECLARE(formats, EGL_DRI2_MAX_FORMATS);
    uint32_t                  capabilities;
    char                     *device_name;
 #endif
index d7e7eaa2c95d3c4ac2120bfbd5bc2fbba2feaf53..2a2d6fe357d39103d424781d9f47e77ec8ec7067 100644 (file)
@@ -114,6 +114,10 @@ static const struct dri2_wl_visual {
    },
 };
 
+static_assert(ARRAY_SIZE(dri2_wl_visuals) <= EGL_DRI2_MAX_FORMATS,
+              "dri2_egl_display::formats is not large enough for "
+              "the formats in dri2_wl_visuals");
+
 static int
 dri2_wl_visual_idx_from_config(struct dri2_egl_display *dri2_dpy,
                                const __DRIconfig *config)
@@ -487,7 +491,7 @@ get_back_bo(struct dri2_egl_surface *dri2_surf)
    num_modifiers = u_vector_length(&dri2_dpy->wl_modifiers[visual_idx]);
 
    /* Substitute dri image format if server does not support original format */
-   if (!(dri2_dpy->formats & (1 << visual_idx)))
+   if (!BITSET_TEST(dri2_dpy->formats, visual_idx))
       linear_dri_image_format = dri2_wl_visuals[visual_idx].alt_dri_image_format;
 
    /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
@@ -495,8 +499,8 @@ get_back_bo(struct dri2_egl_surface *dri2_surf)
     * of bugs.
     */
    assert(linear_dri_image_format != __DRI_IMAGE_FORMAT_NONE);
-   assert(dri2_dpy->formats &
-          (1 << dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format)));
+   assert(BITSET_TEST(dri2_dpy->formats,
+          dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format)));
 
    /* There might be a buffer release already queued that wasn't processed */
    wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
@@ -1106,7 +1110,7 @@ dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
    if (visual_idx == -1)
       goto bad_format;
 
-   if (!(dri2_dpy->formats & (1u << visual_idx)))
+   if (!BITSET_TEST(dri2_dpy->formats, visual_idx))
       goto bad_format;
 
    buffer = create_wl_buffer(dri2_dpy, NULL, image);
@@ -1196,7 +1200,7 @@ drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
    if (visual_idx == -1)
       return;
 
-   dri2_dpy->formats |= (1u << visual_idx);
+   BITSET_SET(dri2_dpy->formats, visual_idx);
 }
 
 static void
@@ -1245,7 +1249,7 @@ dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
        modifier_lo == (DRM_FORMAT_MOD_INVALID & 0xffffffff))
       return;
 
-   dri2_dpy->formats |= (1u << visual_idx);
+   BITSET_SET(dri2_dpy->formats, visual_idx);
 
    mod = u_vector_add(&dri2_dpy->wl_modifiers[visual_idx]);
    *mod = combine_u32_into_u64(modifier_hi, modifier_lo);
@@ -1346,7 +1350,7 @@ dri2_wl_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
       for (unsigned j = 0; j < ARRAY_SIZE(dri2_wl_visuals); j++) {
          struct dri2_egl_config *dri2_conf;
 
-         if (!(dri2_dpy->formats & (1u << j)))
+         if (!BITSET_TEST(dri2_dpy->formats, j))
             continue;
 
          dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
@@ -1374,7 +1378,7 @@ dri2_wl_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
          alt_dri_image_format = dri2_wl_visuals[c].alt_dri_image_format;
          s = dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format);
 
-         if (s == -1 || !(dri2_dpy->formats & (1 << s)))
+         if (s == -1 || !BITSET_TEST(dri2_dpy->formats, s))
             continue;
 
          /* Visual s works for the Wayland server, and c can be converted into s
@@ -2008,7 +2012,7 @@ shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
    if (visual_idx == -1)
       return;
 
-   dri2_dpy->formats |= (1u << visual_idx);
+   BITSET_SET(dri2_dpy->formats, visual_idx);
 }
 
 static const struct wl_shm_listener shm_listener = {
@@ -2115,7 +2119,8 @@ dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
    if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
       goto cleanup;
 
-   if (roundtrip(dri2_dpy) < 0 || dri2_dpy->formats == 0)
+   if (roundtrip(dri2_dpy) < 0 || !BITSET_TEST_RANGE(dri2_dpy->formats,
+                                                     0, EGL_DRI2_MAX_FORMATS))
       goto cleanup;
 
    dri2_dpy->driver_name = strdup("swrast");