gallium/aux/tgsi_exec.c: Fix various -Wsign-compare
[mesa.git] / src / gallium / auxiliary / vl / vl_winsys_dri.c
index d1bbcc2ed580a35eeb2aa1297f12133adedcbe75..bb1ff504886256b30b8363ba03b27432b0cdf2f0 100644 (file)
@@ -37,6 +37,8 @@
 #include <xf86drm.h>
 #include <errno.h>
 
+#include "loader.h"
+
 #include "pipe/p_screen.h"
 #include "pipe/p_context.h"
 #include "pipe/p_state.h"
@@ -44,7 +46,7 @@
 #include "state_tracker/drm_driver.h"
 
 #include "util/u_memory.h"
-#include "util/u_hash.h"
+#include "util/crc32.h"
 #include "util/u_hash_table.h"
 #include "util/u_inlines.h"
 
@@ -71,24 +73,27 @@ struct vl_dri_screen
    int64_t last_ust, ns_frame, last_msc, next_msc;
 };
 
-static const unsigned int attachments[1] = { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT };
+static const unsigned attachments[1] = { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT };
+
+static void vl_dri2_screen_destroy(struct vl_screen *vscreen);
 
 static void
-vl_dri2_handle_stamps(struct vl_dri_screenscrn,
+vl_dri2_handle_stamps(struct vl_dri_screen *scrn,
                       uint32_t ust_hi, uint32_t ust_lo,
                       uint32_t msc_hi, uint32_t msc_lo)
 {
    int64_t ust = ((((uint64_t)ust_hi) << 32) | ust_lo) * 1000;
    int64_t msc = (((uint64_t)msc_hi) << 32) | msc_lo;
 
-   if (scrn->last_ust && scrn->last_msc && (ust > scrn->last_ust) && (msc > scrn->last_msc))
+   if (scrn->last_ust && (ust > scrn->last_ust) &&
+       scrn->last_msc && (msc > scrn->last_msc))
       scrn->ns_frame = (ust - scrn->last_ust) / (msc - scrn->last_msc);
 
    scrn->last_ust = ust;
    scrn->last_msc = msc;
 }
 
-static xcb_dri2_get_buffers_reply_t*
+static xcb_dri2_get_buffers_reply_t *
 vl_dri2_get_flush_reply(struct vl_dri_screen *scrn)
 {
    xcb_dri2_wait_sbc_reply_t *wait_sbc_reply;
@@ -118,7 +123,7 @@ vl_dri2_flush_frontbuffer(struct pipe_screen *screen,
                           unsigned level, unsigned layer,
                           void *context_private, struct pipe_box *sub_box)
 {
-   struct vl_dri_screen *scrn = (struct vl_dri_screen*)context_private;
+   struct vl_dri_screen *scrn = (struct vl_dri_screen *)context_private;
    uint32_t msc_hi, msc_lo;
 
    assert(screen);
@@ -130,9 +135,11 @@ vl_dri2_flush_frontbuffer(struct pipe_screen *screen,
    msc_hi = scrn->next_msc >> 32;
    msc_lo = scrn->next_msc & 0xFFFFFFFF;
 
-   scrn->swap_cookie = xcb_dri2_swap_buffers_unchecked(scrn->conn, scrn->drawable, msc_hi, msc_lo, 0, 0, 0, 0);
+   scrn->swap_cookie = xcb_dri2_swap_buffers_unchecked(scrn->conn, scrn->drawable,
+                                                       msc_hi, msc_lo, 0, 0, 0, 0);
    scrn->wait_cookie = xcb_dri2_wait_sbc_unchecked(scrn->conn, scrn->drawable, 0, 0);
-   scrn->buffers_cookie = xcb_dri2_get_buffers_unchecked(scrn->conn, scrn->drawable, 1, 1, attachments);
+   scrn->buffers_cookie = xcb_dri2_get_buffers_unchecked(scrn->conn, scrn->drawable,
+                                                         1, 1, attachments);
 
    scrn->flushed = true;
    scrn->current_buffer = !scrn->current_buffer;
@@ -168,13 +175,13 @@ vl_dri2_set_drawable(struct vl_dri_screen *scrn, Drawable drawable)
    scrn->drawable = drawable;
 }
 
-struct pipe_resource*
-vl_screen_texture_from_drawable(struct vl_screen *vscreen, Drawable drawable)
+static struct pipe_resource *
+vl_dri2_screen_texture_from_drawable(struct vl_screen *vscreen, void *drawable)
 {
-   struct vl_dri_screen *scrn = (struct vl_dri_screen*)vscreen;
+   struct vl_dri_screen *scrn = (struct vl_dri_screen *)vscreen;
 
    struct winsys_handle dri2_handle;
-   struct pipe_resource template, *tex;
+   struct pipe_resource templ, *tex;
 
    xcb_dri2_get_buffers_reply_t *reply;
    xcb_dri2_dri2_buffer_t *buffers, *back_left;
@@ -183,11 +190,12 @@ vl_screen_texture_from_drawable(struct vl_screen *vscreen, Drawable drawable)
 
    assert(scrn);
 
-   vl_dri2_set_drawable(scrn, drawable);
+   vl_dri2_set_drawable(scrn, (Drawable)drawable);
    reply = vl_dri2_get_flush_reply(scrn);
    if (!reply) {
       xcb_dri2_get_buffers_cookie_t cookie;
-      cookie = xcb_dri2_get_buffers_unchecked(scrn->conn, drawable, 1, 1, attachments);
+      cookie = xcb_dri2_get_buffers_unchecked(scrn->conn, (Drawable)drawable,
+                                              1, 1, attachments);
       reply = xcb_dri2_get_buffers_reply(scrn->conn, cookie, NULL);
    }
    if (!reply)
@@ -223,48 +231,50 @@ vl_screen_texture_from_drawable(struct vl_screen *vscreen, Drawable drawable)
    }
 
    memset(&dri2_handle, 0, sizeof(dri2_handle));
-   dri2_handle.type = DRM_API_HANDLE_TYPE_SHARED;
+   dri2_handle.type = WINSYS_HANDLE_TYPE_SHARED;
    dri2_handle.handle = back_left->name;
    dri2_handle.stride = back_left->pitch;
 
-   memset(&template, 0, sizeof(template));
-   template.target = PIPE_TEXTURE_2D;
-   template.format = PIPE_FORMAT_B8G8R8X8_UNORM;
-   template.last_level = 0;
-   template.width0 = reply->width;
-   template.height0 = reply->height;
-   template.depth0 = 1;
-   template.array_size = 1;
-   template.usage = PIPE_USAGE_DEFAULT;
-   template.bind = PIPE_BIND_RENDER_TARGET;
-   template.flags = 0;
-
-   tex = scrn->base.pscreen->resource_from_handle(scrn->base.pscreen, &template, &dri2_handle);
+   memset(&templ, 0, sizeof(templ));
+   templ.target = PIPE_TEXTURE_2D;
+   templ.format = PIPE_FORMAT_B8G8R8X8_UNORM;
+   templ.last_level = 0;
+   templ.width0 = reply->width;
+   templ.height0 = reply->height;
+   templ.depth0 = 1;
+   templ.array_size = 1;
+   templ.usage = PIPE_USAGE_DEFAULT;
+   templ.bind = PIPE_BIND_RENDER_TARGET;
+   templ.flags = 0;
+
+   tex = scrn->base.pscreen->resource_from_handle(scrn->base.pscreen, &templ,
+                                                  &dri2_handle,
+                                                  PIPE_HANDLE_USAGE_READ_WRITE);
    free(reply);
 
    return tex;
 }
 
-struct u_rect *
-vl_screen_get_dirty_area(struct vl_screen *vscreen)
+static struct u_rect *
+vl_dri2_screen_get_dirty_area(struct vl_screen *vscreen)
 {
-   struct vl_dri_screen *scrn = (struct vl_dri_screen*)vscreen;
+   struct vl_dri_screen *scrn = (struct vl_dri_screen *)vscreen;
    assert(scrn);
    return &scrn->dirty_areas[scrn->current_buffer];
 }
 
-uint64_t
-vl_screen_get_timestamp(struct vl_screen *vscreen, Drawable drawable)
+static uint64_t
+vl_dri2_screen_get_timestamp(struct vl_screen *vscreen, void *drawable)
 {
-   struct vl_dri_screen *scrn = (struct vl_dri_screen*)vscreen;
+   struct vl_dri_screen *scrn = (struct vl_dri_screen *)vscreen;
    xcb_dri2_get_msc_cookie_t cookie;
    xcb_dri2_get_msc_reply_t *reply;
 
    assert(scrn);
 
-   vl_dri2_set_drawable(scrn, drawable);
+   vl_dri2_set_drawable(scrn, (Drawable)drawable);
    if (!scrn->last_ust) {
-      cookie = xcb_dri2_get_msc_unchecked(scrn->conn, drawable);
+      cookie = xcb_dri2_get_msc_unchecked(scrn->conn, (Drawable)drawable);
       reply = xcb_dri2_get_msc_reply(scrn->conn, cookie, NULL);
 
       if (reply) {
@@ -276,25 +286,36 @@ vl_screen_get_timestamp(struct vl_screen *vscreen, Drawable drawable)
    return scrn->last_ust;
 }
 
-void
-vl_screen_set_next_timestamp(struct vl_screen *vscreen, uint64_t stamp)
+static void
+vl_dri2_screen_set_next_timestamp(struct vl_screen *vscreen, uint64_t stamp)
 {
-   struct vl_dri_screen *scrn = (struct vl_dri_screen*)vscreen;
+   struct vl_dri_screen *scrn = (struct vl_dri_screen *)vscreen;
    assert(scrn);
    if (stamp && scrn->last_ust && scrn->ns_frame && scrn->last_msc)
-      scrn->next_msc = ((int64_t)stamp - scrn->last_ust + scrn->ns_frame/2) / scrn->ns_frame + scrn->last_msc;
+      scrn->next_msc = ((int64_t)stamp - scrn->last_ust + scrn->ns_frame/2) /
+                       scrn->ns_frame + scrn->last_msc;
    else
       scrn->next_msc = 0;
 }
 
-void*
-vl_screen_get_private(struct vl_screen *vscreen)
+static void *
+vl_dri2_screen_get_private(struct vl_screen *vscreen)
 {
    return vscreen;
 }
 
-struct vl_screen*
-vl_screen_create(Display *display, int screen)
+static xcb_screen_t *
+get_xcb_screen(xcb_screen_iterator_t iter, int screen)
+{
+    for (; iter.rem; --screen, xcb_screen_next(&iter))
+        if (screen == 0)
+            return iter.data;
+
+    return NULL;
+}
+
+struct vl_screen *
+vl_dri2_screen_create(Display *display, int screen)
 {
    struct vl_dri_screen *scrn;
    const xcb_query_extension_reply_t *extension;
@@ -305,10 +326,11 @@ vl_screen_create(Display *display, int screen)
    xcb_dri2_authenticate_cookie_t authenticate_cookie;
    xcb_dri2_authenticate_reply_t *authenticate = NULL;
    xcb_screen_iterator_t s;
+   xcb_screen_t *xcb_screen;
    xcb_generic_error_t *error = NULL;
    char *device_name;
    int fd, device_name_length;
-   unsigned int driverType;
+   unsigned driverType;
 
    drm_magic_t magic;
 
@@ -328,20 +350,23 @@ vl_screen_create(Display *display, int screen)
    if (!(extension && extension->present))
       goto free_screen;
 
-   dri2_query_cookie = xcb_dri2_query_version (scrn->conn, XCB_DRI2_MAJOR_VERSION, XCB_DRI2_MINOR_VERSION);
+   dri2_query_cookie = xcb_dri2_query_version (scrn->conn,
+                                               XCB_DRI2_MAJOR_VERSION,
+                                               XCB_DRI2_MINOR_VERSION);
    dri2_query = xcb_dri2_query_version_reply (scrn->conn, dri2_query_cookie, &error);
    if (dri2_query == NULL || error != NULL || dri2_query->minor_version < 2)
       goto free_query;
 
    s = xcb_setup_roots_iterator(xcb_get_setup(scrn->conn));
-   while (screen--)
-       xcb_screen_next(&s);
+   xcb_screen = get_xcb_screen(s, screen);
+   if (!xcb_screen)
+      goto free_query;
+
    driverType = XCB_DRI2_DRIVER_TYPE_DRI;
-#ifdef DRI2DriverPrimeShift
    {
       char *prime = getenv("DRI_PRIME");
       if (prime) {
-         unsigned int primeid;
+         unsigned primeid;
          errno = 0;
          primeid = strtoul(prime, NULL, 0);
          if (errno == 0)
@@ -349,11 +374,13 @@ vl_screen_create(Display *display, int screen)
                ((primeid & DRI2DriverPrimeMask) << DRI2DriverPrimeShift);
       }
    }
-#endif
 
-   connect_cookie = xcb_dri2_connect_unchecked(scrn->conn, s.data->root, driverType);
+   connect_cookie = xcb_dri2_connect_unchecked(scrn->conn,
+                                               xcb_screen->root,
+                                               driverType);
    connect = xcb_dri2_connect_reply(scrn->conn, connect_cookie, NULL);
-   if (connect == NULL || connect->driver_name_length + connect->device_name_length == 0)
+   if (connect == NULL ||
+       connect->driver_name_length + connect->device_name_length == 0)
       goto free_connect;
 
    device_name_length = xcb_dri2_connect_device_name_length(connect);
@@ -361,35 +388,35 @@ vl_screen_create(Display *display, int screen)
    if (!device_name)
       goto free_connect;
    memcpy(device_name, xcb_dri2_connect_device_name(connect), device_name_length);
-   fd = open(device_name, O_RDWR);
+   fd = loader_open_device(device_name);
    free(device_name);
 
    if (fd < 0)
       goto free_connect;
 
    if (drmGetMagic(fd, &magic))
-      goto free_connect;
+      goto close_fd;
 
-   authenticate_cookie = xcb_dri2_authenticate_unchecked(scrn->conn, s.data->root, magic);
+   authenticate_cookie = xcb_dri2_authenticate_unchecked(scrn->conn,
+                                                         xcb_screen->root,
+                                                         magic);
    authenticate = xcb_dri2_authenticate_reply(scrn->conn, authenticate_cookie, NULL);
 
    if (authenticate == NULL || !authenticate->authenticated)
       goto free_authenticate;
 
-#if SPLIT_TARGETS
-   scrn->base.pscreen = driver_descriptor.create_screen(fd);
-#else
-#if GALLIUM_STATIC_TARGETS
-   scrn->base.pscreen = dd_create_screen(fd);
-#else
-   if (pipe_loader_drm_probe_fd(&scrn->base.dev, fd, true))
-      scrn->base.pscreen = pipe_loader_create_screen(scrn->base.dev, PIPE_SEARCH_DIR);
-#endif // GALLIUM_STATIC_TARGETS
-#endif // SPLIT_TARGETS
+   if (pipe_loader_drm_probe_fd(&scrn->base.dev, fd))
+      scrn->base.pscreen = pipe_loader_create_screen(scrn->base.dev);
 
    if (!scrn->base.pscreen)
       goto release_pipe;
 
+   scrn->base.destroy = vl_dri2_screen_destroy;
+   scrn->base.texture_from_drawable = vl_dri2_screen_texture_from_drawable;
+   scrn->base.get_dirty_area = vl_dri2_screen_get_dirty_area;
+   scrn->base.get_timestamp = vl_dri2_screen_get_timestamp;
+   scrn->base.set_next_timestamp = vl_dri2_screen_set_next_timestamp;
+   scrn->base.get_private = vl_dri2_screen_get_private;
    scrn->base.pscreen->flush_frontbuffer = vl_dri2_flush_frontbuffer;
    vl_compositor_reset_dirty_area(&scrn->dirty_areas[0]);
    vl_compositor_reset_dirty_area(&scrn->dirty_areas[1]);
@@ -402,14 +429,15 @@ vl_screen_create(Display *display, int screen)
    return &scrn->base;
 
 release_pipe:
-#if !SPLIT_TARGETS
-#if !GALLIUM_STATIC_TARGETS
-   if (scrn->base.dev)
+   if (scrn->base.dev) {
       pipe_loader_release(&scrn->base.dev, 1);
-#endif // !GALLIUM_STATIC_TARGETS
-#endif // !SPLIT_TARGETS
+      fd = -1;
+   }
 free_authenticate:
    free(authenticate);
+close_fd:
+   if (fd != -1)
+      close(fd);
 free_connect:
    free(connect);
 free_query:
@@ -421,9 +449,10 @@ free_screen:
    return NULL;
 }
 
-void vl_screen_destroy(struct vl_screen *vscreen)
+static void
+vl_dri2_screen_destroy(struct vl_screen *vscreen)
 {
-   struct vl_dri_screen *scrn = (struct vl_dri_screen*)vscreen;
+   struct vl_dri_screen *scrn = (struct vl_dri_screen *)vscreen;
 
    assert(vscreen);
 
@@ -435,10 +464,6 @@ void vl_screen_destroy(struct vl_screen *vscreen)
 
    vl_dri2_destroy_drawable(scrn);
    scrn->base.pscreen->destroy(scrn->base.pscreen);
-#if !SPLIT_TARGETS
-#if !GALLIUM_STATIC_TARGETS
    pipe_loader_release(&scrn->base.dev, 1);
-#endif // !GALLIUM_STATIC_TARGETS
-#endif // !SPLIT_TARGETS
    FREE(scrn);
 }