1/**************************************************************************
2 *
3 * Copyright 2016 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include <fcntl.h>
29
30#include <X11/Xlib-xcb.h>
31#include <X11/xshmfence.h>
32#include <xcb/dri3.h>
33#include <xcb/present.h>
34#include <xcb/xfixes.h>
35
36#include "loader.h"
37
38#include "pipe/p_screen.h"
39#include "pipe/p_state.h"
40#include "pipe-loader/pipe_loader.h"
41
42#include "util/u_memory.h"
43#include "util/u_inlines.h"
44
45#include "vl/vl_compositor.h"
46#include "vl/vl_winsys.h"
47
48#include "drm-uapi/drm_fourcc.h"
49
50#define BACK_BUFFER_NUM 3
51
52struct vl_dri3_buffer
53{
54   struct pipe_resource *texture;
55   struct pipe_resource *linear_texture;
56
57   uint32_t pixmap;
58   uint32_t region;
59   uint32_t sync_fence;
60   struct xshmfence *shm_fence;
61
62   bool busy;
63   uint32_t width, height, pitch;
64};
65
66struct vl_dri3_screen
67{
68   struct vl_screen base;
69   xcb_connection_t *conn;
70   xcb_drawable_t drawable;
71
72   uint32_t width, height, depth;
73
74   xcb_present_event_t eid;
75   xcb_special_event_t *special_event;
76
77   struct pipe_context *pipe;
78   struct pipe_resource *output_texture;
79   uint32_t clip_width, clip_height;
80
81   struct vl_dri3_buffer *back_buffers[BACK_BUFFER_NUM];
82   int cur_back;
83   int next_back;
84
85   struct u_rect dirty_areas[BACK_BUFFER_NUM];
86
87   struct vl_dri3_buffer *front_buffer;
88   bool is_pixmap;
89
90   uint32_t send_msc_serial, recv_msc_serial;
91   uint64_t send_sbc, recv_sbc;
92   int64_t last_ust, ns_frame, last_msc, next_msc;
93
94   bool is_different_gpu;
95};
96
97static void
98dri3_free_front_buffer(struct vl_dri3_screen *scrn,
99                        struct vl_dri3_buffer *buffer)
100{
101   xcb_sync_destroy_fence(scrn->conn, buffer->sync_fence);
102   xshmfence_unmap_shm(buffer->shm_fence);
103   pipe_resource_reference(&buffer->texture, NULL);
104   FREE(buffer);
105}
106
107static void
108dri3_free_back_buffer(struct vl_dri3_screen *scrn,
109                        struct vl_dri3_buffer *buffer)
110{
111   if (buffer->region)
112      xcb_xfixes_destroy_region(scrn->conn, buffer->region);
113   xcb_free_pixmap(scrn->conn, buffer->pixmap);
114   xcb_sync_destroy_fence(scrn->conn, buffer->sync_fence);
115   xshmfence_unmap_shm(buffer->shm_fence);
116   if (!scrn->output_texture)
117      pipe_resource_reference(&buffer->texture, NULL);
118   if (buffer->linear_texture)
119       pipe_resource_reference(&buffer->linear_texture, NULL);
120   FREE(buffer);
121}
122
123static void
124dri3_handle_stamps(struct vl_dri3_screen *scrn, uint64_t ust, uint64_t msc)
125{
126   int64_t ust_ns =  ust * 1000;
127
128   if (scrn->last_ust && (ust_ns > scrn->last_ust) &&
129       scrn->last_msc && (msc > scrn->last_msc))
130      scrn->ns_frame = (ust_ns - scrn->last_ust) / (msc - scrn->last_msc);
131
132   scrn->last_ust = ust_ns;
133   scrn->last_msc = msc;
134}
135
136static void
137dri3_handle_present_event(struct vl_dri3_screen *scrn,
138                          xcb_present_generic_event_t *ge)
139{
140   switch (ge->evtype) {
141   case XCB_PRESENT_CONFIGURE_NOTIFY: {
142      xcb_present_configure_notify_event_t *ce = (void *) ge;
143      scrn->width = ce->width;
144      scrn->height = ce->height;
145      break;
146   }
147   case XCB_PRESENT_COMPLETE_NOTIFY: {
148      xcb_present_complete_notify_event_t *ce = (void *) ge;
149      if (ce->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP) {
150         scrn->recv_sbc = (scrn->send_sbc & 0xffffffff00000000LL) | ce->serial;
151         if (scrn->recv_sbc > scrn->send_sbc)
152            scrn->recv_sbc -= 0x100000000;
153         dri3_handle_stamps(scrn, ce->ust, ce->msc);
154      } else if (ce->kind == XCB_PRESENT_COMPLETE_KIND_NOTIFY_MSC) {
155         scrn->recv_msc_serial = ce->serial;
156         dri3_handle_stamps(scrn, ce->ust, ce->msc);
157      }
158      break;
159   }
160   case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
161      xcb_present_idle_notify_event_t *ie = (void *) ge;
162      int b;
163      for (b = 0; b < BACK_BUFFER_NUM; b++) {
164         struct vl_dri3_buffer *buf = scrn->back_buffers[b];
165         if (buf && buf->pixmap == ie->pixmap) {
166            buf->busy = false;
167            break;
168         }
169      }
170      break;
171   }
172   }
173   free(ge);
174}
175
176static void
177dri3_flush_present_events(struct vl_dri3_screen *scrn)
178{
179   if (scrn->special_event) {
180      xcb_generic_event_t *ev;
181      while ((ev = xcb_poll_for_special_event(
182                   scrn->conn, scrn->special_event)) != NULL)
183         dri3_handle_present_event(scrn, (xcb_present_generic_event_t *)ev);
184   }
185}
186
187static bool
188dri3_wait_present_events(struct vl_dri3_screen *scrn)
189{
190   if (scrn->special_event) {
191      xcb_generic_event_t *ev;
192      ev = xcb_wait_for_special_event(scrn->conn, scrn->special_event);
193      if (!ev)
194         return false;
195      dri3_handle_present_event(scrn, (xcb_present_generic_event_t *)ev);
196      return true;
197   }
198   return false;
199}
200
201static int
202dri3_find_back(struct vl_dri3_screen *scrn)
203{
204   int b;
205
206   for (;;) {
207      for (b = 0; b < BACK_BUFFER_NUM; b++) {
208         int id = (b + scrn->cur_back) % BACK_BUFFER_NUM;
209         struct vl_dri3_buffer *buffer = scrn->back_buffers[id];
210         if (!buffer || !buffer->busy)
211            return id;
212      }
213      xcb_flush(scrn->conn);
214      if (!dri3_wait_present_events(scrn))
215         return -1;
216   }
217}
218
219static struct vl_dri3_buffer *
220dri3_alloc_back_buffer(struct vl_dri3_screen *scrn)
221{
222   struct vl_dri3_buffer *buffer;
223   xcb_pixmap_t pixmap;
224   xcb_sync_fence_t sync_fence;
225   struct xshmfence *shm_fence;
226   int buffer_fd, fence_fd;
227   struct pipe_resource templ, *pixmap_buffer_texture;
228   struct winsys_handle whandle;
229
230   buffer = CALLOC_STRUCT(vl_dri3_buffer);
231   if (!buffer)
232      return NULL;
233
234   fence_fd = xshmfence_alloc_shm();
235   if (fence_fd < 0)
236      goto free_buffer;
237
238   shm_fence = xshmfence_map_shm(fence_fd);
239   if (!shm_fence)
240      goto close_fd;
241
242   memset(&templ, 0, sizeof(templ));
243   templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
244   templ.format = vl_dri2_format_for_depth(&scrn->base, scrn->depth);
245   templ.target = PIPE_TEXTURE_2D;
246   templ.last_level = 0;
247   templ.width0 = (scrn->output_texture) ?
248                  scrn->output_texture->width0 : scrn->width;
249   templ.height0 = (scrn->output_texture) ?
250                   scrn->output_texture->height0 : scrn->height;
251   templ.depth0 = 1;
252   templ.array_size = 1;
253
254   if (scrn->is_different_gpu) {
255      buffer->texture = (scrn->output_texture) ? scrn->output_texture :
256                        scrn->base.pscreen->resource_create(scrn->base.pscreen, &templ);
257      if (!buffer->texture)
258         goto unmap_shm;
259
260      templ.bind |= PIPE_BIND_SCANOUT | PIPE_BIND_SHARED |
261                    PIPE_BIND_LINEAR;
262      buffer->linear_texture =
263          scrn->base.pscreen->resource_create(scrn->base.pscreen, &templ);
264      pixmap_buffer_texture = buffer->linear_texture;
265
266      if (!buffer->linear_texture)
267         goto no_linear_texture;
268   } else {
269      templ.bind |= PIPE_BIND_SCANOUT | PIPE_BIND_SHARED;
270      buffer->texture = (scrn->output_texture) ? scrn->output_texture :
271                        scrn->base.pscreen->resource_create(scrn->base.pscreen, &templ);
272      if (!buffer->texture)
273         goto unmap_shm;
274      pixmap_buffer_texture = buffer->texture;
275   }
276   memset(&whandle, 0, sizeof(whandle));
277   whandle.type= WINSYS_HANDLE_TYPE_FD;
278   scrn->base.pscreen->resource_get_handle(scrn->base.pscreen, NULL,
279                                           pixmap_buffer_texture, &whandle, 0);
280   buffer_fd = whandle.handle;
281   buffer->pitch = whandle.stride;
282   buffer->width = templ.width0;
283   buffer->height = templ.height0;
284
285   xcb_dri3_pixmap_from_buffer(scrn->conn,
286                               (pixmap = xcb_generate_id(scrn->conn)),
287                               scrn->drawable,
288                               0,
289                               buffer->width, buffer->height, buffer->pitch,
290                               scrn->depth, 32,
291                               buffer_fd);
292   xcb_dri3_fence_from_fd(scrn->conn,
293                          pixmap,
294                          (sync_fence = xcb_generate_id(scrn->conn)),
295                          false,
296                          fence_fd);
297
298   buffer->pixmap = pixmap;
299   buffer->sync_fence = sync_fence;
300   buffer->shm_fence = shm_fence;
301
302   xshmfence_trigger(buffer->shm_fence);
303
304   return buffer;
305
306no_linear_texture:
307   pipe_resource_reference(&buffer->texture, NULL);
308unmap_shm:
309   xshmfence_unmap_shm(shm_fence);
310close_fd:
311   close(fence_fd);
312free_buffer:
313   FREE(buffer);
314   return NULL;
315}
316
317static struct vl_dri3_buffer *
318dri3_get_back_buffer(struct vl_dri3_screen *scrn)
319{
320   struct vl_dri3_buffer *buffer;
321   struct pipe_resource *texture = NULL;
322   bool allocate_new_buffer = false;
323   int b, id;
324
325   assert(scrn);
326
327   scrn->cur_back = dri3_find_back(scrn);
328   if (scrn->cur_back < 0)
329      return NULL;
330   buffer = scrn->back_buffers[scrn->cur_back];
331
332   if (scrn->output_texture) {
333      if (!buffer || buffer->width < scrn->width ||
334          buffer->height < scrn->height)
335         allocate_new_buffer = true;
336      else if (scrn->is_different_gpu)
337         /* In case of different gpu we can reuse the linear
338          * texture so we only need to set the external
339          * texture for copying
340          */
341         buffer->texture = scrn->output_texture;
342      else {
343         /* In case of a single gpu we search if the texture is
344          * already present as buffer if not we get the
345          * handle and pixmap for the texture that is set
346          */
347         for (b = 0; b < BACK_BUFFER_NUM; b++) {
348            id = (b + scrn->cur_back) % BACK_BUFFER_NUM;
349            buffer = scrn->back_buffers[id];
350            if (buffer && !buffer->busy &&
351                buffer->texture == scrn->output_texture) {
352               scrn->cur_back = id;
353               break;
354            }
355         }
356
357         if (b == BACK_BUFFER_NUM) {
358            allocate_new_buffer = true;
359            scrn->cur_back = scrn->next_back;
360            scrn->next_back = (scrn->next_back + 1) % BACK_BUFFER_NUM;
361            buffer = scrn->back_buffers[scrn->cur_back];
362         }
363      }
364
365   } else {
366      if (!buffer || buffer->width != scrn->width ||
367          buffer->height != scrn->height)
368         allocate_new_buffer = true;
369   }
370
371   if (allocate_new_buffer) {
372      struct vl_dri3_buffer *new_buffer;
373
374      new_buffer = dri3_alloc_back_buffer(scrn);
375      if (!new_buffer)
376         return NULL;
377
378      if (buffer)
379         dri3_free_back_buffer(scrn, buffer);
380
381      if (!scrn->output_texture)
382         vl_compositor_reset_dirty_area(&scrn->dirty_areas[scrn->cur_back]);
383      buffer = new_buffer;
384      scrn->back_buffers[scrn->cur_back] = buffer;
385   }
386
387   pipe_resource_reference(&texture, buffer->texture);
388   xcb_flush(scrn->conn);
389   xshmfence_await(buffer->shm_fence);
390
391   return buffer;
392}
393
394static bool
395dri3_set_drawable(struct vl_dri3_screen *scrn, Drawable drawable)
396{
397   xcb_get_geometry_cookie_t geom_cookie;
398   xcb_get_geometry_reply_t *geom_reply;
399   xcb_void_cookie_t cookie;
400   xcb_generic_error_t *error;
401   bool ret = true;
402
403   assert(drawable);
404
405   if (scrn->drawable == drawable)
406      return true;
407
408   scrn->drawable = drawable;
409
410   geom_cookie = xcb_get_geometry(scrn->conn, scrn->drawable);
411   geom_reply = xcb_get_geometry_reply(scrn->conn, geom_cookie, NULL);
412   if (!geom_reply)
413      return false;
414
415   scrn->width = geom_reply->width;
416   scrn->height = geom_reply->height;
417   scrn->depth = geom_reply->depth;
418   free(geom_reply);
419
420   if (scrn->special_event) {
421      xcb_unregister_for_special_event(scrn->conn, scrn->special_event);
422      scrn->special_event = NULL;
423      cookie = xcb_present_select_input_checked(scrn->conn, scrn->eid,
424                                                scrn->drawable,
425                                                XCB_PRESENT_EVENT_MASK_NO_EVENT);
426      xcb_discard_reply(scrn->conn, cookie.sequence);
427   }
428
429   scrn->is_pixmap = false;
430   scrn->eid = xcb_generate_id(scrn->conn);
431   cookie =
432      xcb_present_select_input_checked(scrn->conn, scrn->eid, scrn->drawable,
433                      XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
434                      XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
435                      XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
436
437   error = xcb_request_check(scrn->conn, cookie);
438   if (error) {
439      if (error->error_code != BadWindow)
440         ret = false;
441      else {
442         scrn->is_pixmap = true;
443         scrn->base.set_back_texture_from_output = NULL;
444         if (scrn->front_buffer) {
445            dri3_free_front_buffer(scrn, scrn->front_buffer);
446            scrn->front_buffer = NULL;
447         }
448      }
449      free(error);
450   } else
451      scrn->special_event =
452         xcb_register_for_special_xge(scrn->conn, &xcb_present_id, scrn->eid, 0);
453
454   dri3_flush_present_events(scrn);
455
456   return ret;
457}
458
459static struct vl_dri3_buffer *
460dri3_get_front_buffer(struct vl_dri3_screen *scrn)
461{
462   xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie;
463   xcb_dri3_buffer_from_pixmap_reply_t *bp_reply;
464   xcb_sync_fence_t sync_fence;
465   struct xshmfence *shm_fence;
466   int fence_fd, *fds;
467   struct winsys_handle whandle;
468   struct pipe_resource templ, *texture = NULL;
469
470   if (scrn->front_buffer) {
471      pipe_resource_reference(&texture, scrn->front_buffer->texture);
472      return scrn->front_buffer;
473   }
474
475   scrn->front_buffer = CALLOC_STRUCT(vl_dri3_buffer);
476   if (!scrn->front_buffer)
477      return NULL;
478
479   fence_fd = xshmfence_alloc_shm();
480   if (fence_fd < 0)
481      goto free_buffer;
482
483   shm_fence = xshmfence_map_shm(fence_fd);
484   if (!shm_fence)
485      goto close_fd;
486
487   bp_cookie = xcb_dri3_buffer_from_pixmap(scrn->conn, scrn->drawable);
488   bp_reply = xcb_dri3_buffer_from_pixmap_reply(scrn->conn, bp_cookie, NULL);
489   if (!bp_reply)
490      goto unmap_shm;
491
492   fds = xcb_dri3_buffer_from_pixmap_reply_fds(scrn->conn, bp_reply);
493   if (fds[0] < 0)
494      goto free_reply;
495
496   memset(&whandle, 0, sizeof(whandle));
497   whandle.type = WINSYS_HANDLE_TYPE_FD;
498   whandle.handle = (unsigned)fds[0];
499   whandle.stride = bp_reply->stride;
500   whandle.modifier = DRM_FORMAT_MOD_INVALID;
501   memset(&templ, 0, sizeof(templ));
502   templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
503   templ.format = vl_dri2_format_for_depth(&scrn->base, bp_reply->depth);
504   templ.target = PIPE_TEXTURE_2D;
505   templ.last_level = 0;
506   templ.width0 = bp_reply->width;
507   templ.height0 = bp_reply->height;
508   templ.depth0 = 1;
509   templ.array_size = 1;
510   scrn->front_buffer->texture =
511      scrn->base.pscreen->resource_from_handle(scrn->base.pscreen,
512                                               &templ, &whandle,
513                                               PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
514   close(fds[0]);
515   if (!scrn->front_buffer->texture)
516      goto free_reply;
517
518   xcb_dri3_fence_from_fd(scrn->conn,
519                          scrn->drawable,
520                          (sync_fence = xcb_generate_id(scrn->conn)),
521                          false,
522                          fence_fd);
523
524   pipe_resource_reference(&texture, scrn->front_buffer->texture);
525   scrn->front_buffer->pixmap = scrn->drawable;
526   scrn->front_buffer->width = bp_reply->width;
527   scrn->front_buffer->height = bp_reply->height;
528   scrn->front_buffer->shm_fence = shm_fence;
529   scrn->front_buffer->sync_fence = sync_fence;
530   free(bp_reply);
531
532   return scrn->front_buffer;
533
534free_reply:
535   free(bp_reply);
536unmap_shm:
537   xshmfence_unmap_shm(shm_fence);
538close_fd:
539   close(fence_fd);
540free_buffer:
541   FREE(scrn->front_buffer);
542   return NULL;
543}
544
545static xcb_screen_t *
546dri3_get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
547{
548   xcb_screen_iterator_t screen_iter =
549   xcb_setup_roots_iterator(xcb_get_setup(conn));
550
551   for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
552      if (screen_iter.data->root == root)
553         return screen_iter.data;
554   }
555
556   return NULL;
557}
558
559static void
560vl_dri3_flush_frontbuffer(struct pipe_screen *screen,
561                          struct pipe_context *pipe,
562                          struct pipe_resource *resource,
563                          unsigned level, unsigned layer,
564                          void *context_private, struct pipe_box *sub_box)
565{
566   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)context_private;
567   uint32_t options = XCB_PRESENT_OPTION_NONE;
568   struct vl_dri3_buffer *back;
569   struct pipe_box src_box;
570   xcb_rectangle_t rectangle;
571
572   back = scrn->back_buffers[scrn->cur_back];
573   if (!back)
574       return;
575
576   while (scrn->special_event && scrn->recv_sbc < scrn->send_sbc)
577      if (!dri3_wait_present_events(scrn))
578         return;
579
580   rectangle.x = 0;
581   rectangle.y = 0;
582   rectangle.width = (scrn->output_texture) ? scrn->clip_width : scrn->width;
583   rectangle.height = (scrn->output_texture) ? scrn->clip_height : scrn->height;
584
585   if (!back->region) {
586      back->region = xcb_generate_id(scrn->conn);
587      xcb_xfixes_create_region(scrn->conn, back->region, 0, NULL);
588   }
589   xcb_xfixes_set_region(scrn->conn, back->region, 1, &rectangle);
590
591   if (scrn->is_different_gpu) {
592      u_box_origin_2d(back->width, back->height, &src_box);
593      scrn->pipe->resource_copy_region(scrn->pipe,
594                                       back->linear_texture,
595                                       0, 0, 0, 0,
596                                       back->texture,
597                                       0, &src_box);
598
599      scrn->pipe->flush(scrn->pipe, NULL, 0);
600   }
601   xshmfence_reset(back->shm_fence);
602   back->busy = true;
603
604   xcb_present_pixmap(scrn->conn,
605                      scrn->drawable,
606                      back->pixmap,
607                      (uint32_t)(++scrn->send_sbc),
608                      0, back->region, 0, 0,
609                      None, None,
610                      back->sync_fence,
611                      options,
612                      scrn->next_msc,
613                      0, 0, 0, NULL);
614
615   xcb_flush(scrn->conn);
616
617   return;
618}
619
620static struct pipe_resource *
621vl_dri3_screen_texture_from_drawable(struct vl_screen *vscreen, void *drawable)
622{
623   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
624   struct vl_dri3_buffer *buffer;
625
626   assert(scrn);
627
628   if (!dri3_set_drawable(scrn, (Drawable)drawable))
629      return NULL;
630
631   buffer = (scrn->is_pixmap) ?
632            dri3_get_front_buffer(scrn) :
633            dri3_get_back_buffer(scrn);
634   if (!buffer)
635      return NULL;
636
637   return buffer->texture;
638}
639
640static struct u_rect *
641vl_dri3_screen_get_dirty_area(struct vl_screen *vscreen)
642{
643   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
644
645   assert(scrn);
646
647   return &scrn->dirty_areas[scrn->cur_back];
648}
649
650static uint64_t
651vl_dri3_screen_get_timestamp(struct vl_screen *vscreen, void *drawable)
652{
653   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
654
655   assert(scrn);
656
657   if (!dri3_set_drawable(scrn, (Drawable)drawable))
658      return 0;
659
660   if (!scrn->last_ust) {
661      xcb_present_notify_msc(scrn->conn,
662                             scrn->drawable,
663                             ++scrn->send_msc_serial,
664                             0, 0, 0);
665      xcb_flush(scrn->conn);
666
667      while (scrn->special_event &&
668             scrn->send_msc_serial > scrn->recv_msc_serial) {
669         if (!dri3_wait_present_events(scrn))
670            return 0;
671      }
672   }
673
674   return scrn->last_ust;
675}
676
677static void
678vl_dri3_screen_set_next_timestamp(struct vl_screen *vscreen, uint64_t stamp)
679{
680   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
681
682   assert(scrn);
683
684   if (stamp && scrn->last_ust && scrn->ns_frame && scrn->last_msc)
685      scrn->next_msc = ((int64_t)stamp - scrn->last_ust + scrn->ns_frame/2) /
686                       scrn->ns_frame + scrn->last_msc;
687   else
688      scrn->next_msc = 0;
689}
690
691static void *
692vl_dri3_screen_get_private(struct vl_screen *vscreen)
693{
694   return vscreen;
695}
696
697static void
698vl_dri3_screen_set_back_texture_from_output(struct vl_screen *vscreen,
699                                            struct pipe_resource *buffer,
700                                            uint32_t width, uint32_t height)
701{
702   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
703
704   assert(scrn);
705
706   scrn->output_texture = buffer;
707   scrn->clip_width = (width) ? width : scrn->width;
708   scrn->clip_height = (height) ? height : scrn->height;
709}
710
711static void
712vl_dri3_screen_destroy(struct vl_screen *vscreen)
713{
714   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
715   int i;
716
717   assert(vscreen);
718
719   dri3_flush_present_events(scrn);
720
721   if (scrn->front_buffer) {
722      dri3_free_front_buffer(scrn, scrn->front_buffer);
723      scrn->front_buffer = NULL;
724   }
725
726   for (i = 0; i < BACK_BUFFER_NUM; ++i) {
727      if (scrn->back_buffers[i]) {
728         dri3_free_back_buffer(scrn, scrn->back_buffers[i]);
729         scrn->back_buffers[i] = NULL;
730      }
731   }
732
733   if (scrn->special_event) {
734      xcb_void_cookie_t cookie =
735         xcb_present_select_input_checked(scrn->conn, scrn->eid,
736                                          scrn->drawable,
737                                          XCB_PRESENT_EVENT_MASK_NO_EVENT);
738
739      xcb_discard_reply(scrn->conn, cookie.sequence);
740      xcb_unregister_for_special_event(scrn->conn, scrn->special_event);
741   }
742   scrn->pipe->destroy(scrn->pipe);
743   scrn->base.pscreen->destroy(scrn->base.pscreen);
744   pipe_loader_release(&scrn->base.dev, 1);
745   FREE(scrn);
746
747   return;
748}
749
750struct vl_screen *
751vl_dri3_screen_create(Display *display, int screen)
752{
753   struct vl_dri3_screen *scrn;
754   const xcb_query_extension_reply_t *extension;
755   xcb_dri3_open_cookie_t open_cookie;
756   xcb_dri3_open_reply_t *open_reply;
757   xcb_get_geometry_cookie_t geom_cookie;
758   xcb_get_geometry_reply_t *geom_reply;
759   xcb_xfixes_query_version_cookie_t xfixes_cookie;
760   xcb_xfixes_query_version_reply_t *xfixes_reply;
761   xcb_generic_error_t *error;
762   int fd;
763
764   assert(display);
765
766   scrn = CALLOC_STRUCT(vl_dri3_screen);
767   if (!scrn)
768      return NULL;
769
770   scrn->conn = XGetXCBConnection(display);
771   if (!scrn->conn)
772      goto free_screen;
773
774   xcb_prefetch_extension_data(scrn->conn , &xcb_dri3_id);
775   xcb_prefetch_extension_data(scrn->conn, &xcb_present_id);
776   xcb_prefetch_extension_data (scrn->conn, &xcb_xfixes_id);
777   extension = xcb_get_extension_data(scrn->conn, &xcb_dri3_id);
778   if (!(extension && extension->present))
779      goto free_screen;
780   extension = xcb_get_extension_data(scrn->conn, &xcb_present_id);
781   if (!(extension && extension->present))
782      goto free_screen;
783   extension = xcb_get_extension_data(scrn->conn, &xcb_xfixes_id);
784   if (!(extension && extension->present))
785      goto free_screen;
786
787   xfixes_cookie = xcb_xfixes_query_version(scrn->conn, XCB_XFIXES_MAJOR_VERSION,
788                                            XCB_XFIXES_MINOR_VERSION);
789   xfixes_reply = xcb_xfixes_query_version_reply(scrn->conn, xfixes_cookie, &error);
790   if (!xfixes_reply || error || xfixes_reply->major_version < 2) {
791      free(error);
792      free(xfixes_reply);
793      goto free_screen;
794   }
795   free(xfixes_reply);
796
797   open_cookie = xcb_dri3_open(scrn->conn, RootWindow(display, screen), None);
798   open_reply = xcb_dri3_open_reply(scrn->conn, open_cookie, NULL);
799   if (!open_reply)
800      goto free_screen;
801   if (open_reply->nfd != 1) {
802      free(open_reply);
803      goto free_screen;
804   }
805
806   fd = xcb_dri3_open_reply_fds(scrn->conn, open_reply)[0];
807   if (fd < 0) {
808      free(open_reply);
809      goto free_screen;
810   }
811   fcntl(fd, F_SETFD, FD_CLOEXEC);
812   free(open_reply);
813
814   fd = loader_get_user_preferred_fd(fd, &scrn->is_different_gpu);
815
816   geom_cookie = xcb_get_geometry(scrn->conn, RootWindow(display, screen));
817   geom_reply = xcb_get_geometry_reply(scrn->conn, geom_cookie, NULL);
818   if (!geom_reply)
819      goto close_fd;
820
821   scrn->base.xcb_screen = dri3_get_screen_for_root(scrn->conn, geom_reply->root);
822   if (!scrn->base.xcb_screen) {
823      free(geom_reply);
824      goto close_fd;
825   }
826
827   /* TODO support depth other than 24 or 30 */
828   if (geom_reply->depth != 24 && geom_reply->depth != 30) {
829      free(geom_reply);
830      goto close_fd;
831   }
832   scrn->base.color_depth = geom_reply->depth;
833   free(geom_reply);
834
835   if (pipe_loader_drm_probe_fd(&scrn->base.dev, fd))
836      scrn->base.pscreen = pipe_loader_create_screen(scrn->base.dev);
837
838   if (!scrn->base.pscreen)
839      goto release_pipe;
840
841   scrn->pipe = pipe_create_multimedia_context(scrn->base.pscreen);
842   if (!scrn->pipe)
843       goto no_context;
844
845   scrn->base.destroy = vl_dri3_screen_destroy;
846   scrn->base.texture_from_drawable = vl_dri3_screen_texture_from_drawable;
847   scrn->base.get_dirty_area = vl_dri3_screen_get_dirty_area;
848   scrn->base.get_timestamp = vl_dri3_screen_get_timestamp;
849   scrn->base.set_next_timestamp = vl_dri3_screen_set_next_timestamp;
850   scrn->base.get_private = vl_dri3_screen_get_private;
851   scrn->base.pscreen->flush_frontbuffer = vl_dri3_flush_frontbuffer;
852   scrn->base.set_back_texture_from_output = vl_dri3_screen_set_back_texture_from_output;
853
854   scrn->next_back = 1;
855
856   close(fd);
857
858   return &scrn->base;
859
860no_context:
861   scrn->base.pscreen->destroy(scrn->base.pscreen);
862release_pipe:
863   if (scrn->base.dev) {
864      pipe_loader_release(&scrn->base.dev, 1);
865      fd = -1;
866   }
867close_fd:
868   if (fd != -1)
869      close(fd);
870free_screen:
871   FREE(scrn);
872   return NULL;
873}
874