-static uint32_t
-dri3_cpp_for_format(uint32_t format) {
- switch (format) {
- case __DRI_IMAGE_FORMAT_R8:
- return 1;
- case __DRI_IMAGE_FORMAT_RGB565:
- case __DRI_IMAGE_FORMAT_GR88:
- return 2;
- case __DRI_IMAGE_FORMAT_XRGB8888:
- case __DRI_IMAGE_FORMAT_ARGB8888:
- case __DRI_IMAGE_FORMAT_ABGR8888:
- case __DRI_IMAGE_FORMAT_XBGR8888:
- case __DRI_IMAGE_FORMAT_XRGB2101010:
- case __DRI_IMAGE_FORMAT_ARGB2101010:
- case __DRI_IMAGE_FORMAT_SARGB8:
- return 4;
- case __DRI_IMAGE_FORMAT_NONE:
- default:
- return 0;
- }
-}
-
-
-/** dri3_alloc_render_buffer
- *
- * Use the driver createImage function to construct a __DRIimage, then
- * get a file descriptor for that and create an X pixmap from that
- *
- * Allocate an xshmfence for synchronization
- */
-static struct dri3_buffer *
-dri3_alloc_render_buffer(struct glx_screen *glx_screen, Drawable draw,
- unsigned int format, int width, int height, int depth)
-{
- struct dri3_screen *psc = (struct dri3_screen *) glx_screen;
- Display *dpy = glx_screen->dpy;
- struct dri3_buffer *buffer;
- xcb_connection_t *c = XGetXCBConnection(dpy);
- xcb_pixmap_t pixmap;
- xcb_sync_fence_t sync_fence;
- struct xshmfence *shm_fence;
- int buffer_fd, fence_fd;
- int stride;
-
- /* Create an xshmfence object and
- * prepare to send that to the X server
- */
-
- fence_fd = xshmfence_alloc_shm();
- if (fence_fd < 0)
- return NULL;
- shm_fence = xshmfence_map_shm(fence_fd);
- if (shm_fence == NULL)
- goto no_shm_fence;
-
- /* Allocate the image from the driver
- */
- buffer = calloc(1, sizeof (struct dri3_buffer));
- if (!buffer)
- goto no_buffer;
-
- buffer->cpp = dri3_cpp_for_format(format);
- if (!buffer->cpp)
- goto no_image;
-
- buffer->image = (*psc->image->createImage) (psc->driScreen,
- width, height,
- format,
- __DRI_IMAGE_USE_SHARE|__DRI_IMAGE_USE_SCANOUT,
- buffer);
-
-
- if (!buffer->image)
- goto no_image;
-
- /* X wants the stride, so ask the image for it
- */
- if (!(*psc->image->queryImage)(buffer->image, __DRI_IMAGE_ATTRIB_STRIDE, &stride))
- goto no_buffer_attrib;
-
- buffer->pitch = stride;
-
- if (!(*psc->image->queryImage)(buffer->image, __DRI_IMAGE_ATTRIB_FD, &buffer_fd))
- goto no_buffer_attrib;
-
- xcb_dri3_pixmap_from_buffer(c,
- (pixmap = xcb_generate_id(c)),
- draw,
- buffer->size,
- width, height, buffer->pitch,
- depth, buffer->cpp * 8,
- buffer_fd);
-
- xcb_dri3_fence_from_fd(c,
- pixmap,
- (sync_fence = xcb_generate_id(c)),
- false,
- fence_fd);
-
- buffer->pixmap = pixmap;
- buffer->own_pixmap = true;
- buffer->sync_fence = sync_fence;
- buffer->shm_fence = shm_fence;
- buffer->width = width;
- buffer->height = height;
-
- /* Mark the buffer as idle
- */
- dri3_fence_set(buffer);
-
- return buffer;
-
-no_buffer_attrib:
- (*psc->image->destroyImage)(buffer->image);
-no_image:
- free(buffer);
-no_buffer:
- xshmfence_unmap_shm(shm_fence);
-no_shm_fence:
- close(fence_fd);
- return NULL;
-}
-
-/** dri3_free_render_buffer
- *
- * Free everything associated with one render buffer including pixmap, fence
- * stuff and the driver image
- */
-static void
-dri3_free_render_buffer(struct dri3_drawable *pdraw, struct dri3_buffer *buffer)
-{
- struct dri3_screen *psc = (struct dri3_screen *) pdraw->base.psc;
- xcb_connection_t *c = XGetXCBConnection(pdraw->base.psc->dpy);
-
- if (buffer->own_pixmap)
- xcb_free_pixmap(c, buffer->pixmap);
- xcb_sync_destroy_fence(c, buffer->sync_fence);
- xshmfence_unmap_shm(buffer->shm_fence);
- (*psc->image->destroyImage)(buffer->image);
- free(buffer);
-}
-
-
-/** dri3_flush_present_events
- *
- * Process any present events that have been received from the X server
- */
-static void
-dri3_flush_present_events(struct dri3_drawable *priv)
-{
- xcb_connection_t *c = XGetXCBConnection(priv->base.psc->dpy);
-
- /* Check to see if any configuration changes have occurred
- * since we were last invoked
- */
- if (priv->special_event) {
- xcb_generic_event_t *ev;
-
- while ((ev = xcb_poll_for_special_event(c, priv->special_event)) != NULL) {
- xcb_present_generic_event_t *ge = (void *) ev;
- dri3_handle_present_event(priv, ge);
- }
- }
-}
-
-/** dri3_update_drawable
- *
- * Called the first time we use the drawable and then
- * after we receive present configure notify events to
- * track the geometry of the drawable
- */
-static int
-dri3_update_drawable(__DRIdrawable *driDrawable, void *loaderPrivate)
-{
- struct dri3_drawable *priv = loaderPrivate;
- xcb_connection_t *c = XGetXCBConnection(priv->base.psc->dpy);
-
- /* First time through, go get the current drawable geometry
- */
- if (priv->width == 0 || priv->height == 0 || priv->depth == 0) {
- xcb_get_geometry_cookie_t geom_cookie;
- xcb_get_geometry_reply_t *geom_reply;
- xcb_void_cookie_t cookie;
- xcb_generic_error_t *error;
-
- /* Try to select for input on the window.
- *
- * If the drawable is a window, this will get our events
- * delivered.
- *
- * Otherwise, we'll get a BadWindow error back from this request which
- * will let us know that the drawable is a pixmap instead.
- */
-
-
- cookie = xcb_present_select_input_checked(c,
- (priv->eid = xcb_generate_id(c)),
- priv->base.xDrawable,
- XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY|
- XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY|
- XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
-
- /* Create an XCB event queue to hold present events outside of the usual
- * application event queue
- */
- priv->special_event = xcb_register_for_special_xge(c,
- &xcb_present_id,
- priv->eid,
- priv->stamp);
-
- geom_cookie = xcb_get_geometry(c, priv->base.xDrawable);
-
- geom_reply = xcb_get_geometry_reply(c, geom_cookie, NULL);
-
- if (!geom_reply)
- return false;
-
- priv->width = geom_reply->width;
- priv->height = geom_reply->height;
- priv->depth = geom_reply->depth;
- priv->is_pixmap = false;
-
- free(geom_reply);
-
- /* Check to see if our select input call failed. If it failed with a
- * BadWindow error, then assume the drawable is a pixmap. Destroy the
- * special event queue created above and mark the drawable as a pixmap
- */
-
- error = xcb_request_check(c, cookie);
-
- if (error) {
- if (error->error_code != BadWindow) {
- free(error);
- return false;
- }
- priv->is_pixmap = true;
- xcb_unregister_for_special_event(c, priv->special_event);
- priv->special_event = NULL;
- }
- }
- dri3_flush_present_events(priv);
- return true;
-}
-
-/* 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
- * translate to __DRI_IMAGE_FOURCC codes in the call to createImageFromFds
- */
-static int
-image_format_to_fourcc(int format)
-{
-
- /* Convert from __DRI_IMAGE_FORMAT to __DRI_IMAGE_FOURCC (sigh) */
- switch (format) {
- case __DRI_IMAGE_FORMAT_RGB565: return __DRI_IMAGE_FOURCC_RGB565;
- case __DRI_IMAGE_FORMAT_XRGB8888: return __DRI_IMAGE_FOURCC_XRGB8888;
- case __DRI_IMAGE_FORMAT_ARGB8888: return __DRI_IMAGE_FOURCC_ARGB8888;
- case __DRI_IMAGE_FORMAT_ABGR8888: return __DRI_IMAGE_FOURCC_ABGR8888;
- case __DRI_IMAGE_FORMAT_XBGR8888: return __DRI_IMAGE_FOURCC_XBGR8888;
- }
- return 0;
-}
-
-/** dri3_get_pixmap_buffer
- *
- * Get the DRM object for a pixmap from the X server and
- * wrap that with a __DRIimage structure using createImageFromFds
- */
-static struct dri3_buffer *
-dri3_get_pixmap_buffer(__DRIdrawable *driDrawable,
- unsigned int format,
- enum dri3_buffer_type buffer_type,
- void *loaderPrivate)
-{
- struct dri3_drawable *pdraw = loaderPrivate;
- int buf_id = dri3_pixmap_buf_id(buffer_type);
- struct dri3_buffer *buffer = pdraw->buffers[buf_id];
- Pixmap pixmap;
- xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie;
- xcb_dri3_buffer_from_pixmap_reply_t *bp_reply;
- int *fds;
- Display *dpy;
- struct dri3_screen *psc;
- xcb_connection_t *c;
- xcb_sync_fence_t sync_fence;
- struct xshmfence *shm_fence;
- int fence_fd;
- __DRIimage *image_planar;
- int stride, offset;
-
- if (buffer)
- return buffer;
-
- pixmap = pdraw->base.xDrawable;
- psc = (struct dri3_screen *) pdraw->base.psc;
- dpy = psc->base.dpy;
- c = XGetXCBConnection(dpy);
-
- buffer = calloc(1, sizeof (struct dri3_buffer));
- if (!buffer)
- goto no_buffer;
-
- fence_fd = xshmfence_alloc_shm();
- if (fence_fd < 0)
- goto no_fence;
- shm_fence = xshmfence_map_shm(fence_fd);
- if (shm_fence == NULL) {
- close (fence_fd);
- goto no_fence;
- }
-
- xcb_dri3_fence_from_fd(c,
- pixmap,
- (sync_fence = xcb_generate_id(c)),
- false,
- fence_fd);
-
- /* Get an FD for the pixmap object
- */
- bp_cookie = xcb_dri3_buffer_from_pixmap(c, pixmap);
- bp_reply = xcb_dri3_buffer_from_pixmap_reply(c, bp_cookie, NULL);
- if (!bp_reply)
- goto no_image;
- fds = xcb_dri3_buffer_from_pixmap_reply_fds(c, bp_reply);
-
- stride = bp_reply->stride;
- offset = 0;
-
- /* createImageFromFds creates a wrapper __DRIimage structure which
- * can deal with multiple planes for things like Yuv images. So, once
- * we've gotten the planar wrapper, pull the single plane out of it and
- * discard the wrapper.
- */
- image_planar = (*psc->image->createImageFromFds) (psc->driScreen,
- bp_reply->width,
- bp_reply->height,
- image_format_to_fourcc(format),
- fds, 1,
- &stride, &offset, buffer);
- close(fds[0]);
- if (!image_planar)
- goto no_image;
-
- buffer->image = (*psc->image->fromPlanar)(image_planar, 0, buffer);
-
- (*psc->image->destroyImage)(image_planar);
-
- if (!buffer->image)
- goto no_image;
-
- buffer->pixmap = pixmap;
- buffer->own_pixmap = false;
- buffer->width = bp_reply->width;
- buffer->height = bp_reply->height;
- buffer->buffer_type = buffer_type;
- buffer->shm_fence = shm_fence;
- buffer->sync_fence = sync_fence;
-
- pdraw->buffers[buf_id] = buffer;
- return buffer;
-
-no_image:
- xcb_sync_destroy_fence(c, sync_fence);
- xshmfence_unmap_shm(shm_fence);
-no_fence:
- free(buffer);
-no_buffer:
- return NULL;
-}
-
-/** dri3_find_back
- *
- * Find an idle back buffer. If there isn't one, then
- * wait for a present idle notify event from the X server
- */
-static int
-dri3_find_back(xcb_connection_t *c, struct dri3_drawable *priv)
-{
- int b;
- xcb_generic_event_t *ev;
- xcb_present_generic_event_t *ge;
-
- for (;;) {
-
- for (b = 0; b < DRI3_MAX_BACK; b++) {
- int id = DRI3_BACK_ID(b);
- struct dri3_buffer *buffer = priv->buffers[id];
-
- if (!buffer)
- return b;
- if (!buffer->busy)
- return b;
- }
- ev = xcb_wait_for_special_event(c, priv->special_event);
- if (!ev)
- return -1;
- ge = (void *) ev;
- dri3_handle_present_event(priv, ge);
- }
-}
-
-/** dri3_get_buffer
- *
- * Find a front or back buffer, allocating new ones as necessary
- */
-static struct dri3_buffer *
-dri3_get_buffer(__DRIdrawable *driDrawable,
- unsigned int format,
- enum dri3_buffer_type buffer_type,
- void *loaderPrivate)
-{
- struct dri3_drawable *priv = loaderPrivate;
- xcb_connection_t *c = XGetXCBConnection(priv->base.psc->dpy);
- struct dri3_buffer *buffer;
- int buf_id;
-
- if (buffer_type == dri3_buffer_back) {
- int back = dri3_find_back(c, priv);
-
- if (back < 0)
- return NULL;
-
- priv->cur_back = back;
- buf_id = DRI3_BACK_ID(priv->cur_back);
- } else {
- buf_id = DRI3_FRONT_ID;
- }
-
- buffer = priv->buffers[buf_id];
-
- /* Allocate a new buffer if there isn't an old one, or if that
- * old one is the wrong size
- */
- if (!buffer || buffer->width != priv->width || buffer->height != priv->height) {
- struct dri3_buffer *new_buffer;
-
- /* Allocate the new buffers
- */
- new_buffer = dri3_alloc_render_buffer(priv->base.psc,
- priv->base.xDrawable,
- format, priv->width, priv->height, priv->depth);
- if (!new_buffer)
- return NULL;
-
- /* When resizing, copy the contents of the old buffer, waiting for that
- * copy to complete using our fences before proceeding
- */
- switch (buffer_type) {
- case dri3_buffer_back:
- if (buffer) {
- dri3_fence_reset(c, new_buffer);
- dri3_fence_await(c, buffer);
- dri3_copy_area(c,
- buffer->pixmap,
- new_buffer->pixmap,
- dri3_drawable_gc(priv),
- 0, 0, 0, 0, priv->width, priv->height);
- dri3_fence_trigger(c, new_buffer);
- dri3_free_render_buffer(priv, buffer);
- }
- break;
- case dri3_buffer_front:
- dri3_fence_reset(c, new_buffer);
- dri3_copy_area(c,
- priv->base.xDrawable,
- new_buffer->pixmap,
- dri3_drawable_gc(priv),
- 0, 0, 0, 0, priv->width, priv->height);
- dri3_fence_trigger(c, new_buffer);
- break;
- }
- buffer = new_buffer;
- buffer->buffer_type = buffer_type;
- priv->buffers[buf_id] = buffer;
- }
- dri3_fence_await(c, buffer);
-
- /* Return the requested buffer */
- return buffer;
-}
-
-/** dri3_free_buffers
- *
- * Free the front bufffer or all of the back buffers. Used
- * when the application changes which buffers it needs
- */
-static void
-dri3_free_buffers(__DRIdrawable *driDrawable,
- enum dri3_buffer_type buffer_type,
- void *loaderPrivate)
-{
- struct dri3_drawable *priv = loaderPrivate;
- struct dri3_buffer *buffer;
- int first_id;
- int n_id;
- int buf_id;
-
- switch (buffer_type) {
- case dri3_buffer_back:
- first_id = DRI3_BACK_ID(0);
- n_id = DRI3_MAX_BACK;
- break;
- case dri3_buffer_front:
- first_id = DRI3_FRONT_ID;
- n_id = 1;
- }
-
- for (buf_id = first_id; buf_id < first_id + n_id; buf_id++) {
- buffer = priv->buffers[buf_id];
- if (buffer) {
- dri3_free_render_buffer(priv, buffer);
- priv->buffers[buf_id] = NULL;
- }
- }
-}
-
-/** dri3_get_buffers
- *
- * The published buffer allocation API.
- * Returns all of the necessary buffers, allocating
- * as needed.