+static void
+dri3_free_front_buffer(struct vl_dri3_screen *scrn,
+ struct vl_dri3_buffer *buffer)
+{
+ xcb_sync_destroy_fence(scrn->conn, buffer->sync_fence);
+ xshmfence_unmap_shm(buffer->shm_fence);
+ pipe_resource_reference(&buffer->texture, NULL);
+ FREE(buffer);
+}
+
+static void
+dri3_free_back_buffer(struct vl_dri3_screen *scrn,
+ struct vl_dri3_buffer *buffer)
+{
+ xcb_free_pixmap(scrn->conn, buffer->pixmap);
+ xcb_sync_destroy_fence(scrn->conn, buffer->sync_fence);
+ xshmfence_unmap_shm(buffer->shm_fence);
+ if (!scrn->output_texture)
+ pipe_resource_reference(&buffer->texture, NULL);
+ if (buffer->linear_texture)
+ pipe_resource_reference(&buffer->linear_texture, NULL);
+ FREE(buffer);
+}
+
+static void
+dri3_handle_stamps(struct vl_dri3_screen *scrn, uint64_t ust, uint64_t msc)
+{
+ int64_t ust_ns = ust * 1000;
+
+ if (scrn->last_ust && (ust_ns > scrn->last_ust) &&
+ scrn->last_msc && (msc > scrn->last_msc))
+ scrn->ns_frame = (ust_ns - scrn->last_ust) / (msc - scrn->last_msc);
+
+ scrn->last_ust = ust_ns;
+ scrn->last_msc = msc;
+}
+
+static void
+dri3_handle_present_event(struct vl_dri3_screen *scrn,
+ xcb_present_generic_event_t *ge)
+{
+ switch (ge->evtype) {
+ case XCB_PRESENT_CONFIGURE_NOTIFY: {
+ xcb_present_configure_notify_event_t *ce = (void *) ge;
+ scrn->width = ce->width;
+ scrn->height = ce->height;
+ break;
+ }
+ case XCB_PRESENT_COMPLETE_NOTIFY: {
+ xcb_present_complete_notify_event_t *ce = (void *) ge;
+ if (ce->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP) {
+ scrn->recv_sbc = (scrn->send_sbc & 0xffffffff00000000LL) | ce->serial;
+ if (scrn->recv_sbc > scrn->send_sbc)
+ scrn->recv_sbc -= 0x100000000;
+ dri3_handle_stamps(scrn, ce->ust, ce->msc);
+ } else if (ce->kind == XCB_PRESENT_COMPLETE_KIND_NOTIFY_MSC) {
+ scrn->recv_msc_serial = ce->serial;
+ dri3_handle_stamps(scrn, ce->ust, ce->msc);
+ }
+ break;
+ }
+ case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
+ xcb_present_idle_notify_event_t *ie = (void *) ge;
+ int b;
+ for (b = 0; b < BACK_BUFFER_NUM; b++) {
+ struct vl_dri3_buffer *buf = scrn->back_buffers[b];
+ if (buf && buf->pixmap == ie->pixmap) {
+ buf->busy = false;
+ break;
+ }
+ }
+ break;
+ }
+ }
+ free(ge);
+}
+
+static void
+dri3_flush_present_events(struct vl_dri3_screen *scrn)
+{
+ if (scrn->special_event) {
+ xcb_generic_event_t *ev;
+ while ((ev = xcb_poll_for_special_event(
+ scrn->conn, scrn->special_event)) != NULL)
+ dri3_handle_present_event(scrn, (xcb_present_generic_event_t *)ev);
+ }
+}
+
+static bool
+dri3_wait_present_events(struct vl_dri3_screen *scrn)
+{
+ if (scrn->special_event) {
+ xcb_generic_event_t *ev;
+ ev = xcb_wait_for_special_event(scrn->conn, scrn->special_event);
+ if (!ev)
+ return false;
+ dri3_handle_present_event(scrn, (xcb_present_generic_event_t *)ev);
+ return true;
+ }
+ return false;
+}
+
+static int
+dri3_find_back(struct vl_dri3_screen *scrn)
+{
+ int b;
+
+ for (;;) {
+ for (b = 0; b < BACK_BUFFER_NUM; b++) {
+ int id = (b + scrn->cur_back) % BACK_BUFFER_NUM;
+ struct vl_dri3_buffer *buffer = scrn->back_buffers[id];
+ if (!buffer || !buffer->busy)
+ return id;
+ }
+ xcb_flush(scrn->conn);
+ if (!dri3_wait_present_events(scrn))
+ return -1;
+ }
+}
+
+static struct vl_dri3_buffer *
+dri3_alloc_back_buffer(struct vl_dri3_screen *scrn)
+{
+ struct vl_dri3_buffer *buffer;
+ xcb_pixmap_t pixmap;
+ xcb_sync_fence_t sync_fence;
+ struct xshmfence *shm_fence;
+ int buffer_fd, fence_fd;
+ struct pipe_resource templ, *pixmap_buffer_texture;
+ struct winsys_handle whandle;
+ unsigned usage;
+
+ buffer = CALLOC_STRUCT(vl_dri3_buffer);
+ if (!buffer)
+ return NULL;
+
+ fence_fd = xshmfence_alloc_shm();
+ if (fence_fd < 0)
+ goto free_buffer;
+
+ shm_fence = xshmfence_map_shm(fence_fd);
+ if (!shm_fence)
+ goto close_fd;
+
+ memset(&templ, 0, sizeof(templ));
+ templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
+ templ.format = PIPE_FORMAT_B8G8R8X8_UNORM;
+ templ.target = PIPE_TEXTURE_2D;
+ templ.last_level = 0;
+ templ.width0 = (scrn->output_texture) ?
+ scrn->output_texture->width0 : scrn->width;
+ templ.height0 = (scrn->output_texture) ?
+ scrn->output_texture->height0 : scrn->height;
+ templ.depth0 = 1;
+ templ.array_size = 1;
+
+ if (scrn->is_different_gpu) {
+ buffer->texture = (scrn->output_texture) ? scrn->output_texture :
+ scrn->base.pscreen->resource_create(scrn->base.pscreen, &templ);
+ if (!buffer->texture)
+ goto unmap_shm;
+
+ templ.bind |= PIPE_BIND_SCANOUT | PIPE_BIND_SHARED |
+ PIPE_BIND_LINEAR;
+ buffer->linear_texture =
+ scrn->base.pscreen->resource_create(scrn->base.pscreen, &templ);
+ pixmap_buffer_texture = buffer->linear_texture;
+
+ if (!buffer->linear_texture)
+ goto no_linear_texture;
+ } else {
+ templ.bind |= PIPE_BIND_SCANOUT | PIPE_BIND_SHARED;
+ buffer->texture = (scrn->output_texture) ? scrn->output_texture :
+ scrn->base.pscreen->resource_create(scrn->base.pscreen, &templ);
+ if (!buffer->texture)
+ goto unmap_shm;
+ pixmap_buffer_texture = buffer->texture;
+ }
+ memset(&whandle, 0, sizeof(whandle));
+ whandle.type= DRM_API_HANDLE_TYPE_FD;
+ usage = PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ;
+ scrn->base.pscreen->resource_get_handle(scrn->base.pscreen, NULL,
+ pixmap_buffer_texture, &whandle,
+ usage);
+ buffer_fd = whandle.handle;
+ buffer->pitch = whandle.stride;
+ buffer->width = templ.width0;
+ buffer->height = templ.height0;
+
+ xcb_dri3_pixmap_from_buffer(scrn->conn,
+ (pixmap = xcb_generate_id(scrn->conn)),
+ scrn->drawable,
+ 0,
+ buffer->width, buffer->height, buffer->pitch,
+ scrn->depth, 32,
+ buffer_fd);
+ xcb_dri3_fence_from_fd(scrn->conn,
+ pixmap,
+ (sync_fence = xcb_generate_id(scrn->conn)),
+ false,
+ fence_fd);
+
+ buffer->pixmap = pixmap;
+ buffer->sync_fence = sync_fence;
+ buffer->shm_fence = shm_fence;
+
+ xshmfence_trigger(buffer->shm_fence);
+
+ return buffer;
+
+no_linear_texture:
+ pipe_resource_reference(&buffer->texture, NULL);
+unmap_shm:
+ xshmfence_unmap_shm(shm_fence);
+close_fd:
+ close(fence_fd);
+free_buffer:
+ FREE(buffer);
+ return NULL;
+}
+
+static struct vl_dri3_buffer *
+dri3_get_back_buffer(struct vl_dri3_screen *scrn)
+{
+ struct vl_dri3_buffer *buffer;
+ struct pipe_resource *texture = NULL;
+ bool allocate_new_buffer = false;
+ int b, id;
+
+ assert(scrn);
+
+ scrn->cur_back = dri3_find_back(scrn);
+ if (scrn->cur_back < 0)
+ return NULL;
+ buffer = scrn->back_buffers[scrn->cur_back];
+
+ if (scrn->output_texture) {
+ if (!buffer || buffer->width < scrn->width ||
+ buffer->height < scrn->height)
+ allocate_new_buffer = true;
+ else if (scrn->is_different_gpu)
+ /* In case of different gpu we can reuse the linear
+ * texture so we only need to set the external
+ * texture for copying
+ */
+ buffer->texture = scrn->output_texture;
+ else {
+ /* In case of a single gpu we search if the texture is
+ * already present as buffer if not we get the
+ * handle and pixmap for the texture that is set
+ */
+ for (b = 0; b < BACK_BUFFER_NUM; b++) {
+ id = (b + scrn->cur_back) % BACK_BUFFER_NUM;
+ buffer = scrn->back_buffers[id];
+ if (buffer && !buffer->busy &&
+ buffer->texture == scrn->output_texture) {
+ scrn->cur_back = id;
+ break;
+ }
+ }
+
+ if (b == BACK_BUFFER_NUM) {
+ allocate_new_buffer = true;
+ scrn->cur_back = scrn->next_back;
+ scrn->next_back = (scrn->next_back + 1) % BACK_BUFFER_NUM;
+ buffer = scrn->back_buffers[scrn->cur_back];
+ }
+ }
+
+ } else {
+ if (!buffer || buffer->width != scrn->width ||
+ buffer->height != scrn->height)
+ allocate_new_buffer = true;
+ }
+
+ if (allocate_new_buffer) {
+ struct vl_dri3_buffer *new_buffer;
+
+ new_buffer = dri3_alloc_back_buffer(scrn);
+ if (!new_buffer)
+ return NULL;
+
+ if (buffer)
+ dri3_free_back_buffer(scrn, buffer);
+
+ if (!scrn->output_texture)
+ vl_compositor_reset_dirty_area(&scrn->dirty_areas[scrn->cur_back]);
+ buffer = new_buffer;
+ scrn->back_buffers[scrn->cur_back] = buffer;
+ }
+
+ pipe_resource_reference(&texture, buffer->texture);
+ xcb_flush(scrn->conn);
+ xshmfence_await(buffer->shm_fence);
+
+ return buffer;
+}
+
+static bool
+dri3_set_drawable(struct vl_dri3_screen *scrn, Drawable drawable)
+{
+ xcb_get_geometry_cookie_t geom_cookie;
+ xcb_get_geometry_reply_t *geom_reply;
+ xcb_void_cookie_t cookie;
+ xcb_generic_error_t *error;
+ bool ret = true;
+
+ assert(drawable);
+
+ if (scrn->drawable == drawable)
+ return true;
+
+ scrn->drawable = drawable;
+
+ geom_cookie = xcb_get_geometry(scrn->conn, scrn->drawable);
+ geom_reply = xcb_get_geometry_reply(scrn->conn, geom_cookie, NULL);
+ if (!geom_reply)
+ return false;
+
+ scrn->width = geom_reply->width;
+ scrn->height = geom_reply->height;
+ scrn->depth = geom_reply->depth;
+ free(geom_reply);
+
+ if (scrn->special_event) {
+ xcb_unregister_for_special_event(scrn->conn, scrn->special_event);
+ scrn->special_event = NULL;
+ cookie = xcb_present_select_input_checked(scrn->conn, scrn->eid,
+ scrn->drawable,
+ XCB_PRESENT_EVENT_MASK_NO_EVENT);
+ xcb_discard_reply(scrn->conn, cookie.sequence);
+ }
+
+ scrn->is_pixmap = false;
+ scrn->eid = xcb_generate_id(scrn->conn);
+ cookie =
+ xcb_present_select_input_checked(scrn->conn, scrn->eid, scrn->drawable,
+ XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
+ XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
+ XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
+
+ error = xcb_request_check(scrn->conn, cookie);
+ if (error) {
+ if (error->error_code != BadWindow)
+ ret = false;
+ else {
+ scrn->is_pixmap = true;
+ if (scrn->front_buffer) {
+ dri3_free_front_buffer(scrn, scrn->front_buffer);
+ scrn->front_buffer = NULL;
+ }
+ }
+ free(error);
+ } else
+ scrn->special_event =
+ xcb_register_for_special_xge(scrn->conn, &xcb_present_id, scrn->eid, 0);
+
+ dri3_flush_present_events(scrn);
+
+ return ret;
+}
+
+static struct vl_dri3_buffer *
+dri3_get_front_buffer(struct vl_dri3_screen *scrn)
+{
+ xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie;
+ xcb_dri3_buffer_from_pixmap_reply_t *bp_reply;
+ xcb_sync_fence_t sync_fence;
+ struct xshmfence *shm_fence;
+ int fence_fd, *fds;
+ struct winsys_handle whandle;
+ struct pipe_resource templ, *texture = NULL;
+
+ if (scrn->front_buffer) {
+ pipe_resource_reference(&texture, scrn->front_buffer->texture);
+ return scrn->front_buffer;
+ }
+
+ scrn->front_buffer = CALLOC_STRUCT(vl_dri3_buffer);
+ if (!scrn->front_buffer)
+ return NULL;
+
+ fence_fd = xshmfence_alloc_shm();
+ if (fence_fd < 0)
+ goto free_buffer;
+
+ shm_fence = xshmfence_map_shm(fence_fd);
+ if (!shm_fence)
+ goto close_fd;
+
+ bp_cookie = xcb_dri3_buffer_from_pixmap(scrn->conn, scrn->drawable);
+ bp_reply = xcb_dri3_buffer_from_pixmap_reply(scrn->conn, bp_cookie, NULL);
+ if (!bp_reply)
+ goto unmap_shm;
+
+ fds = xcb_dri3_buffer_from_pixmap_reply_fds(scrn->conn, bp_reply);
+ if (fds[0] < 0)
+ goto free_reply;
+
+ memset(&whandle, 0, sizeof(whandle));
+ whandle.type = DRM_API_HANDLE_TYPE_FD;
+ whandle.handle = (unsigned)fds[0];
+ whandle.stride = bp_reply->stride;
+ memset(&templ, 0, sizeof(templ));
+ templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
+ templ.format = PIPE_FORMAT_B8G8R8X8_UNORM;
+ templ.target = PIPE_TEXTURE_2D;
+ templ.last_level = 0;
+ templ.width0 = bp_reply->width;
+ templ.height0 = bp_reply->height;
+ templ.depth0 = 1;
+ templ.array_size = 1;
+ scrn->front_buffer->texture =
+ scrn->base.pscreen->resource_from_handle(scrn->base.pscreen,
+ &templ, &whandle,
+ PIPE_HANDLE_USAGE_READ_WRITE);
+ close(fds[0]);
+ if (!scrn->front_buffer->texture)
+ goto free_reply;
+
+ xcb_dri3_fence_from_fd(scrn->conn,
+ scrn->drawable,
+ (sync_fence = xcb_generate_id(scrn->conn)),
+ false,
+ fence_fd);
+
+ pipe_resource_reference(&texture, scrn->front_buffer->texture);
+ scrn->front_buffer->pixmap = scrn->drawable;
+ scrn->front_buffer->width = bp_reply->width;
+ scrn->front_buffer->height = bp_reply->height;
+ scrn->front_buffer->shm_fence = shm_fence;
+ scrn->front_buffer->sync_fence = sync_fence;
+ free(bp_reply);
+
+ return scrn->front_buffer;
+
+free_reply:
+ free(bp_reply);
+unmap_shm:
+ xshmfence_unmap_shm(shm_fence);
+close_fd:
+ close(fence_fd);
+free_buffer:
+ FREE(scrn->front_buffer);
+ return NULL;
+}
+