• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2013 Keith Packard
3  * Copyright © 2015 Boyan Ding
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting documentation, and
9  * that the name of the copyright holders not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  The copyright holders make no representations
12  * about the suitability of this software for any purpose.  It is provided "as
13  * is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21  * OF THIS SOFTWARE.
22  */
23 
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 
29 #include <X11/xshmfence.h>
30 #include <xcb/xcb.h>
31 #include <xcb/dri3.h>
32 #include <xcb/present.h>
33 #include <xcb/xfixes.h>
34 
35 #include <X11/Xlib-xcb.h>
36 
37 #include "loader_dri3_helper.h"
38 #include "util/macros.h"
39 #include "drm-uapi/drm_fourcc.h"
40 
41 /* From driconf.h, user exposed so should be stable */
42 #define DRI_CONF_VBLANK_NEVER 0
43 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
44 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
45 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
46 
47 /**
48  * A cached blit context.
49  */
50 struct loader_dri3_blit_context {
51    mtx_t mtx;
52    __DRIcontext *ctx;
53    __DRIscreen *cur_screen;
54    const __DRIcoreExtension *core;
55 };
56 
57 /* For simplicity we maintain the cache only for a single screen at a time */
58 static struct loader_dri3_blit_context blit_context = {
59    _MTX_INITIALIZER_NP, NULL
60 };
61 
62 static void
63 dri3_flush_present_events(struct loader_dri3_drawable *draw);
64 
65 static struct loader_dri3_buffer *
66 dri3_find_back_alloc(struct loader_dri3_drawable *draw);
67 
68 static xcb_screen_t *
get_screen_for_root(xcb_connection_t * conn,xcb_window_t root)69 get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
70 {
71    xcb_screen_iterator_t screen_iter =
72    xcb_setup_roots_iterator(xcb_get_setup(conn));
73 
74    for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
75       if (screen_iter.data->root == root)
76          return screen_iter.data;
77    }
78 
79    return NULL;
80 }
81 
82 static xcb_visualtype_t *
get_xcb_visualtype_for_depth(struct loader_dri3_drawable * draw,int depth)83 get_xcb_visualtype_for_depth(struct loader_dri3_drawable *draw, int depth)
84 {
85    xcb_visualtype_iterator_t visual_iter;
86    xcb_screen_t *screen = draw->screen;
87    xcb_depth_iterator_t depth_iter;
88 
89    if (!screen)
90       return NULL;
91 
92    depth_iter = xcb_screen_allowed_depths_iterator(screen);
93    for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
94       if (depth_iter.data->depth != depth)
95          continue;
96 
97       visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
98       if (visual_iter.rem)
99          return visual_iter.data;
100    }
101 
102    return NULL;
103 }
104 
105 /* Sets the adaptive sync window property state. */
106 static void
set_adaptive_sync_property(xcb_connection_t * conn,xcb_drawable_t drawable,uint32_t state)107 set_adaptive_sync_property(xcb_connection_t *conn, xcb_drawable_t drawable,
108                            uint32_t state)
109 {
110    static char const name[] = "_VARIABLE_REFRESH";
111    xcb_intern_atom_cookie_t cookie;
112    xcb_intern_atom_reply_t* reply;
113    xcb_void_cookie_t check;
114 
115    cookie = xcb_intern_atom(conn, 0, strlen(name), name);
116    reply = xcb_intern_atom_reply(conn, cookie, NULL);
117    if (reply == NULL)
118       return;
119 
120    if (state)
121       check = xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE,
122                                           drawable, reply->atom,
123                                           XCB_ATOM_CARDINAL, 32, 1, &state);
124    else
125       check = xcb_delete_property_checked(conn, drawable, reply->atom);
126 
127    xcb_discard_reply(conn, check.sequence);
128    free(reply);
129 }
130 
131 /* Get red channel mask for given drawable at given depth. */
132 static unsigned int
dri3_get_red_mask_for_depth(struct loader_dri3_drawable * draw,int depth)133 dri3_get_red_mask_for_depth(struct loader_dri3_drawable *draw, int depth)
134 {
135    xcb_visualtype_t *visual = get_xcb_visualtype_for_depth(draw, depth);
136 
137    if (visual)
138       return visual->red_mask;
139 
140    return 0;
141 }
142 
143 /**
144  * Do we have blit functionality in the image blit extension?
145  *
146  * \param draw[in]  The drawable intended to blit from / to.
147  * \return  true if we have blit functionality. false otherwise.
148  */
loader_dri3_have_image_blit(const struct loader_dri3_drawable * draw)149 static bool loader_dri3_have_image_blit(const struct loader_dri3_drawable *draw)
150 {
151    return draw->ext->image->base.version >= 9 &&
152       draw->ext->image->blitImage != NULL;
153 }
154 
155 /**
156  * Get and lock (for use with the current thread) a dri context associated
157  * with the drawable's dri screen. The context is intended to be used with
158  * the dri image extension's blitImage method.
159  *
160  * \param draw[in]  Pointer to the drawable whose dri screen we want a
161  * dri context for.
162  * \return A dri context or NULL if context creation failed.
163  *
164  * When the caller is done with the context (even if the context returned was
165  * NULL), the caller must call loader_dri3_blit_context_put.
166  */
167 static __DRIcontext *
loader_dri3_blit_context_get(struct loader_dri3_drawable * draw)168 loader_dri3_blit_context_get(struct loader_dri3_drawable *draw)
169 {
170    mtx_lock(&blit_context.mtx);
171 
172    if (blit_context.ctx && blit_context.cur_screen != draw->dri_screen) {
173       blit_context.core->destroyContext(blit_context.ctx);
174       blit_context.ctx = NULL;
175    }
176 
177    if (!blit_context.ctx) {
178       blit_context.ctx = draw->ext->core->createNewContext(draw->dri_screen,
179                                                            NULL, NULL, NULL);
180       blit_context.cur_screen = draw->dri_screen;
181       blit_context.core = draw->ext->core;
182    }
183 
184    return blit_context.ctx;
185 }
186 
187 /**
188  * Release (for use with other threads) a dri context previously obtained using
189  * loader_dri3_blit_context_get.
190  */
191 static void
loader_dri3_blit_context_put(void)192 loader_dri3_blit_context_put(void)
193 {
194    mtx_unlock(&blit_context.mtx);
195 }
196 
197 /**
198  * Blit (parts of) the contents of a DRI image to another dri image
199  *
200  * \param draw[in]  The drawable which owns the images.
201  * \param dst[in]  The destination image.
202  * \param src[in]  The source image.
203  * \param dstx0[in]  Start destination coordinate.
204  * \param dsty0[in]  Start destination coordinate.
205  * \param width[in]  Blit width.
206  * \param height[in] Blit height.
207  * \param srcx0[in]  Start source coordinate.
208  * \param srcy0[in]  Start source coordinate.
209  * \param flush_flag[in]  Image blit flush flag.
210  * \return true iff successful.
211  */
212 static bool
loader_dri3_blit_image(struct loader_dri3_drawable * draw,__DRIimage * dst,__DRIimage * src,int dstx0,int dsty0,int width,int height,int srcx0,int srcy0,int flush_flag)213 loader_dri3_blit_image(struct loader_dri3_drawable *draw,
214                        __DRIimage *dst, __DRIimage *src,
215                        int dstx0, int dsty0, int width, int height,
216                        int srcx0, int srcy0, int flush_flag)
217 {
218    __DRIcontext *dri_context;
219    bool use_blit_context = false;
220 
221    if (!loader_dri3_have_image_blit(draw))
222       return false;
223 
224    dri_context = draw->vtable->get_dri_context(draw);
225 
226    if (!dri_context || !draw->vtable->in_current_context(draw)) {
227       dri_context = loader_dri3_blit_context_get(draw);
228       use_blit_context = true;
229       flush_flag |= __BLIT_FLAG_FLUSH;
230    }
231 
232    if (dri_context)
233       draw->ext->image->blitImage(dri_context, dst, src, dstx0, dsty0,
234                                   width, height, srcx0, srcy0,
235                                   width, height, flush_flag);
236 
237    if (use_blit_context)
238       loader_dri3_blit_context_put();
239 
240    return dri_context != NULL;
241 }
242 
243 static inline void
dri3_fence_reset(xcb_connection_t * c,struct loader_dri3_buffer * buffer)244 dri3_fence_reset(xcb_connection_t *c, struct loader_dri3_buffer *buffer)
245 {
246    xshmfence_reset(buffer->shm_fence);
247 }
248 
249 static inline void
dri3_fence_set(struct loader_dri3_buffer * buffer)250 dri3_fence_set(struct loader_dri3_buffer *buffer)
251 {
252    xshmfence_trigger(buffer->shm_fence);
253 }
254 
255 static inline void
dri3_fence_trigger(xcb_connection_t * c,struct loader_dri3_buffer * buffer)256 dri3_fence_trigger(xcb_connection_t *c, struct loader_dri3_buffer *buffer)
257 {
258    xcb_sync_trigger_fence(c, buffer->sync_fence);
259 }
260 
261 static inline void
dri3_fence_await(xcb_connection_t * c,struct loader_dri3_drawable * draw,struct loader_dri3_buffer * buffer)262 dri3_fence_await(xcb_connection_t *c, struct loader_dri3_drawable *draw,
263                  struct loader_dri3_buffer *buffer)
264 {
265    xcb_flush(c);
266    xshmfence_await(buffer->shm_fence);
267    if (draw) {
268       mtx_lock(&draw->mtx);
269       dri3_flush_present_events(draw);
270       mtx_unlock(&draw->mtx);
271    }
272 }
273 
274 static void
dri3_update_max_num_back(struct loader_dri3_drawable * draw)275 dri3_update_max_num_back(struct loader_dri3_drawable *draw)
276 {
277    switch (draw->last_present_mode) {
278    case XCB_PRESENT_COMPLETE_MODE_FLIP: {
279       int new_max;
280 
281       if (draw->swap_interval == 0)
282          new_max = 4;
283       else
284          new_max = 3;
285 
286       assert(new_max <= LOADER_DRI3_MAX_BACK);
287 
288       if (new_max != draw->max_num_back) {
289          /* On transition from swap interval == 0 to != 0, start with two
290           * buffers again. Otherwise keep the current number of buffers. Either
291           * way, more will be allocated if needed.
292           */
293          if (new_max < draw->max_num_back)
294             draw->cur_num_back = 2;
295 
296          draw->max_num_back = new_max;
297       }
298 
299       break;
300    }
301 
302    case XCB_PRESENT_COMPLETE_MODE_SKIP:
303       break;
304 
305    default:
306       /* On transition from flips to copies, start with a single buffer again,
307        * a second one will be allocated if needed
308        */
309       if (draw->max_num_back != 2)
310          draw->cur_num_back = 1;
311 
312       draw->max_num_back = 2;
313    }
314 }
315 
316 void
loader_dri3_set_swap_interval(struct loader_dri3_drawable * draw,int interval)317 loader_dri3_set_swap_interval(struct loader_dri3_drawable *draw, int interval)
318 {
319    draw->swap_interval = interval;
320 }
321 
322 /** dri3_free_render_buffer
323  *
324  * Free everything associated with one render buffer including pixmap, fence
325  * stuff and the driver image
326  */
327 static void
dri3_free_render_buffer(struct loader_dri3_drawable * draw,struct loader_dri3_buffer * buffer)328 dri3_free_render_buffer(struct loader_dri3_drawable *draw,
329                         struct loader_dri3_buffer *buffer)
330 {
331    if (buffer->own_pixmap)
332       xcb_free_pixmap(draw->conn, buffer->pixmap);
333    xcb_sync_destroy_fence(draw->conn, buffer->sync_fence);
334    xshmfence_unmap_shm(buffer->shm_fence);
335    draw->ext->image->destroyImage(buffer->image);
336    if (buffer->linear_buffer)
337       draw->ext->image->destroyImage(buffer->linear_buffer);
338    free(buffer);
339 }
340 
341 void
loader_dri3_drawable_fini(struct loader_dri3_drawable * draw)342 loader_dri3_drawable_fini(struct loader_dri3_drawable *draw)
343 {
344    int i;
345 
346    draw->ext->core->destroyDrawable(draw->dri_drawable);
347 
348    for (i = 0; i < ARRAY_SIZE(draw->buffers); i++) {
349       if (draw->buffers[i])
350          dri3_free_render_buffer(draw, draw->buffers[i]);
351    }
352 
353    if (draw->special_event) {
354       xcb_void_cookie_t cookie =
355          xcb_present_select_input_checked(draw->conn, draw->eid, draw->drawable,
356                                           XCB_PRESENT_EVENT_MASK_NO_EVENT);
357 
358       xcb_discard_reply(draw->conn, cookie.sequence);
359       xcb_unregister_for_special_event(draw->conn, draw->special_event);
360    }
361 
362    cnd_destroy(&draw->event_cnd);
363    mtx_destroy(&draw->mtx);
364 }
365 
366 int
loader_dri3_drawable_init(xcb_connection_t * conn,xcb_drawable_t drawable,__DRIscreen * dri_screen,bool is_different_gpu,bool multiplanes_available,const __DRIconfig * dri_config,struct loader_dri3_extensions * ext,const struct loader_dri3_vtable * vtable,struct loader_dri3_drawable * draw)367 loader_dri3_drawable_init(xcb_connection_t *conn,
368                           xcb_drawable_t drawable,
369                           __DRIscreen *dri_screen,
370                           bool is_different_gpu,
371                           bool multiplanes_available,
372                           const __DRIconfig *dri_config,
373                           struct loader_dri3_extensions *ext,
374                           const struct loader_dri3_vtable *vtable,
375                           struct loader_dri3_drawable *draw)
376 {
377    xcb_get_geometry_cookie_t cookie;
378    xcb_get_geometry_reply_t *reply;
379    xcb_generic_error_t *error;
380    GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
381    int swap_interval;
382 
383    draw->conn = conn;
384    draw->ext = ext;
385    draw->vtable = vtable;
386    draw->drawable = drawable;
387    draw->dri_screen = dri_screen;
388    draw->is_different_gpu = is_different_gpu;
389    draw->multiplanes_available = multiplanes_available;
390 
391    draw->have_back = 0;
392    draw->have_fake_front = 0;
393    draw->first_init = true;
394    draw->adaptive_sync = false;
395    draw->adaptive_sync_active = false;
396 
397    draw->cur_blit_source = -1;
398    draw->back_format = __DRI_IMAGE_FORMAT_NONE;
399    mtx_init(&draw->mtx, mtx_plain);
400    cnd_init(&draw->event_cnd);
401 
402    if (draw->ext->config) {
403       unsigned char adaptive_sync = 0;
404 
405       draw->ext->config->configQueryi(draw->dri_screen,
406                                       "vblank_mode", &vblank_mode);
407 
408       draw->ext->config->configQueryb(draw->dri_screen,
409                                       "adaptive_sync",
410                                       &adaptive_sync);
411 
412       draw->adaptive_sync = adaptive_sync;
413    }
414 
415    if (!draw->adaptive_sync)
416       set_adaptive_sync_property(conn, draw->drawable, false);
417 
418    switch (vblank_mode) {
419    case DRI_CONF_VBLANK_NEVER:
420    case DRI_CONF_VBLANK_DEF_INTERVAL_0:
421       swap_interval = 0;
422       break;
423    case DRI_CONF_VBLANK_DEF_INTERVAL_1:
424    case DRI_CONF_VBLANK_ALWAYS_SYNC:
425    default:
426       swap_interval = 1;
427       break;
428    }
429    draw->swap_interval = swap_interval;
430 
431    dri3_update_max_num_back(draw);
432 
433    /* Create a new drawable */
434    draw->dri_drawable =
435       draw->ext->image_driver->createNewDrawable(dri_screen,
436                                                  dri_config,
437                                                  draw);
438 
439    if (!draw->dri_drawable)
440       return 1;
441 
442    cookie = xcb_get_geometry(draw->conn, draw->drawable);
443    reply = xcb_get_geometry_reply(draw->conn, cookie, &error);
444    if (reply == NULL || error != NULL) {
445       draw->ext->core->destroyDrawable(draw->dri_drawable);
446       return 1;
447    }
448 
449    draw->screen = get_screen_for_root(draw->conn, reply->root);
450    draw->width = reply->width;
451    draw->height = reply->height;
452    draw->depth = reply->depth;
453    draw->vtable->set_drawable_size(draw, draw->width, draw->height);
454    free(reply);
455 
456    draw->swap_method = __DRI_ATTRIB_SWAP_UNDEFINED;
457    if (draw->ext->core->base.version >= 2) {
458       (void )draw->ext->core->getConfigAttrib(dri_config,
459                                               __DRI_ATTRIB_SWAP_METHOD,
460                                               &draw->swap_method);
461    }
462 
463    /*
464     * Make sure server has the same swap interval we do for the new
465     * drawable.
466     */
467    loader_dri3_set_swap_interval(draw, swap_interval);
468 
469    return 0;
470 }
471 
472 /*
473  * Process one Present event
474  */
475 static void
dri3_handle_present_event(struct loader_dri3_drawable * draw,xcb_present_generic_event_t * ge)476 dri3_handle_present_event(struct loader_dri3_drawable *draw,
477                           xcb_present_generic_event_t *ge)
478 {
479    switch (ge->evtype) {
480    case XCB_PRESENT_CONFIGURE_NOTIFY: {
481       xcb_present_configure_notify_event_t *ce = (void *) ge;
482 
483       draw->width = ce->width;
484       draw->height = ce->height;
485       draw->vtable->set_drawable_size(draw, draw->width, draw->height);
486       draw->ext->flush->invalidate(draw->dri_drawable);
487       break;
488    }
489    case XCB_PRESENT_COMPLETE_NOTIFY: {
490       xcb_present_complete_notify_event_t *ce = (void *) ge;
491 
492       /* Compute the processed SBC number from the received 32-bit serial number
493        * merged with the upper 32-bits of the sent 64-bit serial number while
494        * checking for wrap.
495        */
496       if (ce->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP) {
497          uint64_t recv_sbc = (draw->send_sbc & 0xffffffff00000000LL) | ce->serial;
498 
499          /* Only assume wraparound if that results in exactly the previous
500           * SBC + 1, otherwise ignore received SBC > sent SBC (those are
501           * probably from a previous loader_dri3_drawable instance) to avoid
502           * calculating bogus target MSC values in loader_dri3_swap_buffers_msc
503           */
504          if (recv_sbc <= draw->send_sbc)
505             draw->recv_sbc = recv_sbc;
506          else if (recv_sbc == (draw->recv_sbc + 0x100000001ULL))
507             draw->recv_sbc = recv_sbc - 0x100000000ULL;
508 
509          /* When moving from flip to copy, we assume that we can allocate in
510           * a more optimal way if we don't need to cater for the display
511           * controller.
512           */
513          if (ce->mode == XCB_PRESENT_COMPLETE_MODE_COPY &&
514              draw->last_present_mode == XCB_PRESENT_COMPLETE_MODE_FLIP) {
515             for (int b = 0; b < ARRAY_SIZE(draw->buffers); b++) {
516                if (draw->buffers[b])
517                   draw->buffers[b]->reallocate = true;
518             }
519          }
520 
521          /* If the server tells us that our allocation is suboptimal, we
522           * reallocate once.
523           */
524 #ifdef HAVE_DRI3_MODIFIERS
525          if (ce->mode == XCB_PRESENT_COMPLETE_MODE_SUBOPTIMAL_COPY &&
526              draw->last_present_mode != ce->mode) {
527             for (int b = 0; b < ARRAY_SIZE(draw->buffers); b++) {
528                if (draw->buffers[b])
529                   draw->buffers[b]->reallocate = true;
530             }
531          }
532 #endif
533          draw->last_present_mode = ce->mode;
534 
535          if (draw->vtable->show_fps)
536             draw->vtable->show_fps(draw, ce->ust);
537 
538          draw->ust = ce->ust;
539          draw->msc = ce->msc;
540       } else if (ce->serial == draw->eid) {
541          draw->notify_ust = ce->ust;
542          draw->notify_msc = ce->msc;
543       }
544       break;
545    }
546    case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
547       xcb_present_idle_notify_event_t *ie = (void *) ge;
548       int b;
549 
550       for (b = 0; b < ARRAY_SIZE(draw->buffers); b++) {
551          struct loader_dri3_buffer *buf = draw->buffers[b];
552 
553          if (buf && buf->pixmap == ie->pixmap)
554             buf->busy = 0;
555       }
556       break;
557    }
558    }
559    free(ge);
560 }
561 
562 static bool
dri3_wait_for_event_locked(struct loader_dri3_drawable * draw,unsigned * full_sequence)563 dri3_wait_for_event_locked(struct loader_dri3_drawable *draw,
564                            unsigned *full_sequence)
565 {
566    xcb_generic_event_t *ev;
567    xcb_present_generic_event_t *ge;
568 
569    xcb_flush(draw->conn);
570 
571    /* Only have one thread waiting for events at a time */
572    if (draw->has_event_waiter) {
573       cnd_wait(&draw->event_cnd, &draw->mtx);
574       if (full_sequence)
575          *full_sequence = draw->last_special_event_sequence;
576       /* Another thread has updated the protected info, so retest. */
577       return true;
578    } else {
579       draw->has_event_waiter = true;
580       /* Allow other threads access to the drawable while we're waiting. */
581       mtx_unlock(&draw->mtx);
582       ev = xcb_wait_for_special_event(draw->conn, draw->special_event);
583       mtx_lock(&draw->mtx);
584       draw->has_event_waiter = false;
585       cnd_broadcast(&draw->event_cnd);
586    }
587    if (!ev)
588       return false;
589    draw->last_special_event_sequence = ev->full_sequence;
590    if (full_sequence)
591       *full_sequence = ev->full_sequence;
592    ge = (void *) ev;
593    dri3_handle_present_event(draw, ge);
594    return true;
595 }
596 
597 /** loader_dri3_wait_for_msc
598  *
599  * Get the X server to send an event when the target msc/divisor/remainder is
600  * reached.
601  */
602 bool
loader_dri3_wait_for_msc(struct loader_dri3_drawable * draw,int64_t target_msc,int64_t divisor,int64_t remainder,int64_t * ust,int64_t * msc,int64_t * sbc)603 loader_dri3_wait_for_msc(struct loader_dri3_drawable *draw,
604                          int64_t target_msc,
605                          int64_t divisor, int64_t remainder,
606                          int64_t *ust, int64_t *msc, int64_t *sbc)
607 {
608    xcb_void_cookie_t cookie = xcb_present_notify_msc(draw->conn,
609                                                      draw->drawable,
610                                                      draw->eid,
611                                                      target_msc,
612                                                      divisor,
613                                                      remainder);
614    unsigned full_sequence;
615 
616    mtx_lock(&draw->mtx);
617 
618    /* Wait for the event */
619    do {
620       if (!dri3_wait_for_event_locked(draw, &full_sequence)) {
621          mtx_unlock(&draw->mtx);
622          return false;
623       }
624    } while (full_sequence != cookie.sequence || draw->notify_msc < target_msc);
625 
626    *ust = draw->notify_ust;
627    *msc = draw->notify_msc;
628    *sbc = draw->recv_sbc;
629    mtx_unlock(&draw->mtx);
630 
631    return true;
632 }
633 
634 /** loader_dri3_wait_for_sbc
635  *
636  * Wait for the completed swap buffer count to reach the specified
637  * target. Presumably the application knows that this will be reached with
638  * outstanding complete events, or we're going to be here awhile.
639  */
640 int
loader_dri3_wait_for_sbc(struct loader_dri3_drawable * draw,int64_t target_sbc,int64_t * ust,int64_t * msc,int64_t * sbc)641 loader_dri3_wait_for_sbc(struct loader_dri3_drawable *draw,
642                          int64_t target_sbc, int64_t *ust,
643                          int64_t *msc, int64_t *sbc)
644 {
645    /* From the GLX_OML_sync_control spec:
646     *
647     *     "If <target_sbc> = 0, the function will block until all previous
648     *      swaps requested with glXSwapBuffersMscOML for that window have
649     *      completed."
650     */
651    mtx_lock(&draw->mtx);
652    if (!target_sbc)
653       target_sbc = draw->send_sbc;
654 
655    while (draw->recv_sbc < target_sbc) {
656       if (!dri3_wait_for_event_locked(draw, NULL)) {
657          mtx_unlock(&draw->mtx);
658          return 0;
659       }
660    }
661 
662    *ust = draw->ust;
663    *msc = draw->msc;
664    *sbc = draw->recv_sbc;
665    mtx_unlock(&draw->mtx);
666    return 1;
667 }
668 
669 /** loader_dri3_find_back
670  *
671  * Find an idle back buffer. If there isn't one, then
672  * wait for a present idle notify event from the X server
673  */
674 static int
dri3_find_back(struct loader_dri3_drawable * draw)675 dri3_find_back(struct loader_dri3_drawable *draw)
676 {
677    int b;
678    int num_to_consider;
679    int max_num;
680 
681    mtx_lock(&draw->mtx);
682    /* Increase the likelyhood of reusing current buffer */
683    dri3_flush_present_events(draw);
684 
685    /* Check whether we need to reuse the current back buffer as new back.
686     * In that case, wait until it's not busy anymore.
687     */
688    if (!loader_dri3_have_image_blit(draw) && draw->cur_blit_source != -1) {
689       num_to_consider = 1;
690       max_num = 1;
691       draw->cur_blit_source = -1;
692    } else {
693       num_to_consider = draw->cur_num_back;
694       max_num = draw->max_num_back;
695    }
696 
697    for (;;) {
698       for (b = 0; b < num_to_consider; b++) {
699          int id = LOADER_DRI3_BACK_ID((b + draw->cur_back) % draw->cur_num_back);
700          struct loader_dri3_buffer *buffer = draw->buffers[id];
701 
702          if (!buffer || !buffer->busy) {
703             draw->cur_back = id;
704             mtx_unlock(&draw->mtx);
705             return id;
706          }
707       }
708 
709       if (num_to_consider < max_num) {
710          num_to_consider = ++draw->cur_num_back;
711       } else if (!dri3_wait_for_event_locked(draw, NULL)) {
712          mtx_unlock(&draw->mtx);
713          return -1;
714       }
715    }
716 }
717 
718 static xcb_gcontext_t
dri3_drawable_gc(struct loader_dri3_drawable * draw)719 dri3_drawable_gc(struct loader_dri3_drawable *draw)
720 {
721    if (!draw->gc) {
722       uint32_t v = 0;
723       xcb_create_gc(draw->conn,
724                     (draw->gc = xcb_generate_id(draw->conn)),
725                     draw->drawable,
726                     XCB_GC_GRAPHICS_EXPOSURES,
727                     &v);
728    }
729    return draw->gc;
730 }
731 
732 
733 static struct loader_dri3_buffer *
dri3_back_buffer(struct loader_dri3_drawable * draw)734 dri3_back_buffer(struct loader_dri3_drawable *draw)
735 {
736    return draw->buffers[LOADER_DRI3_BACK_ID(draw->cur_back)];
737 }
738 
739 static struct loader_dri3_buffer *
dri3_fake_front_buffer(struct loader_dri3_drawable * draw)740 dri3_fake_front_buffer(struct loader_dri3_drawable *draw)
741 {
742    return draw->buffers[LOADER_DRI3_FRONT_ID];
743 }
744 
745 static void
dri3_copy_area(xcb_connection_t * c,xcb_drawable_t src_drawable,xcb_drawable_t dst_drawable,xcb_gcontext_t gc,int16_t src_x,int16_t src_y,int16_t dst_x,int16_t dst_y,uint16_t width,uint16_t height)746 dri3_copy_area(xcb_connection_t *c,
747                xcb_drawable_t    src_drawable,
748                xcb_drawable_t    dst_drawable,
749                xcb_gcontext_t    gc,
750                int16_t           src_x,
751                int16_t           src_y,
752                int16_t           dst_x,
753                int16_t           dst_y,
754                uint16_t          width,
755                uint16_t          height)
756 {
757    xcb_void_cookie_t cookie;
758 
759    cookie = xcb_copy_area_checked(c,
760                                   src_drawable,
761                                   dst_drawable,
762                                   gc,
763                                   src_x,
764                                   src_y,
765                                   dst_x,
766                                   dst_y,
767                                   width,
768                                   height);
769    xcb_discard_reply(c, cookie.sequence);
770 }
771 
772 /**
773  * Asks the driver to flush any queued work necessary for serializing with the
774  * X command stream, and optionally the slightly more strict requirement of
775  * glFlush() equivalence (which would require flushing even if nothing had
776  * been drawn to a window system framebuffer, for example).
777  */
778 void
loader_dri3_flush(struct loader_dri3_drawable * draw,unsigned flags,enum __DRI2throttleReason throttle_reason)779 loader_dri3_flush(struct loader_dri3_drawable *draw,
780                   unsigned flags,
781                   enum __DRI2throttleReason throttle_reason)
782 {
783    /* NEED TO CHECK WHETHER CONTEXT IS NULL */
784    __DRIcontext *dri_context = draw->vtable->get_dri_context(draw);
785 
786    if (dri_context) {
787       draw->ext->flush->flush_with_flags(dri_context, draw->dri_drawable,
788                                          flags, throttle_reason);
789    }
790 }
791 
792 void
loader_dri3_copy_sub_buffer(struct loader_dri3_drawable * draw,int x,int y,int width,int height,bool flush)793 loader_dri3_copy_sub_buffer(struct loader_dri3_drawable *draw,
794                             int x, int y,
795                             int width, int height,
796                             bool flush)
797 {
798    struct loader_dri3_buffer *back;
799    unsigned flags = __DRI2_FLUSH_DRAWABLE;
800 
801    /* Check we have the right attachments */
802    if (!draw->have_back || draw->is_pixmap)
803       return;
804 
805    if (flush)
806       flags |= __DRI2_FLUSH_CONTEXT;
807    loader_dri3_flush(draw, flags, __DRI2_THROTTLE_COPYSUBBUFFER);
808 
809    back = dri3_find_back_alloc(draw);
810    if (!back)
811       return;
812 
813    y = draw->height - y - height;
814 
815    if (draw->is_different_gpu) {
816       /* Update the linear buffer part of the back buffer
817        * for the dri3_copy_area operation
818        */
819       (void) loader_dri3_blit_image(draw,
820                                     back->linear_buffer,
821                                     back->image,
822                                     0, 0, back->width, back->height,
823                                     0, 0, __BLIT_FLAG_FLUSH);
824    }
825 
826    loader_dri3_swapbuffer_barrier(draw);
827    dri3_fence_reset(draw->conn, back);
828    dri3_copy_area(draw->conn,
829                   back->pixmap,
830                   draw->drawable,
831                   dri3_drawable_gc(draw),
832                   x, y, x, y, width, height);
833    dri3_fence_trigger(draw->conn, back);
834    /* Refresh the fake front (if present) after we just damaged the real
835     * front.
836     */
837    if (draw->have_fake_front &&
838        !loader_dri3_blit_image(draw,
839                                dri3_fake_front_buffer(draw)->image,
840                                back->image,
841                                x, y, width, height,
842                                x, y, __BLIT_FLAG_FLUSH) &&
843        !draw->is_different_gpu) {
844       dri3_fence_reset(draw->conn, dri3_fake_front_buffer(draw));
845       dri3_copy_area(draw->conn,
846                      back->pixmap,
847                      dri3_fake_front_buffer(draw)->pixmap,
848                      dri3_drawable_gc(draw),
849                      x, y, x, y, width, height);
850       dri3_fence_trigger(draw->conn, dri3_fake_front_buffer(draw));
851       dri3_fence_await(draw->conn, NULL, dri3_fake_front_buffer(draw));
852    }
853    dri3_fence_await(draw->conn, draw, back);
854 }
855 
856 void
loader_dri3_copy_drawable(struct loader_dri3_drawable * draw,xcb_drawable_t dest,xcb_drawable_t src)857 loader_dri3_copy_drawable(struct loader_dri3_drawable *draw,
858                           xcb_drawable_t dest,
859                           xcb_drawable_t src)
860 {
861    loader_dri3_flush(draw, __DRI2_FLUSH_DRAWABLE, __DRI2_THROTTLE_COPYSUBBUFFER);
862 
863    dri3_fence_reset(draw->conn, dri3_fake_front_buffer(draw));
864    dri3_copy_area(draw->conn,
865                   src, dest,
866                   dri3_drawable_gc(draw),
867                   0, 0, 0, 0, draw->width, draw->height);
868    dri3_fence_trigger(draw->conn, dri3_fake_front_buffer(draw));
869    dri3_fence_await(draw->conn, draw, dri3_fake_front_buffer(draw));
870 }
871 
872 void
loader_dri3_wait_x(struct loader_dri3_drawable * draw)873 loader_dri3_wait_x(struct loader_dri3_drawable *draw)
874 {
875    struct loader_dri3_buffer *front;
876 
877    if (draw == NULL || !draw->have_fake_front)
878       return;
879 
880    front = dri3_fake_front_buffer(draw);
881 
882    loader_dri3_copy_drawable(draw, front->pixmap, draw->drawable);
883 
884    /* In the psc->is_different_gpu case, the linear buffer has been updated,
885     * but not yet the tiled buffer.
886     * Copy back to the tiled buffer we use for rendering.
887     * Note that we don't need flushing.
888     */
889    if (draw->is_different_gpu)
890       (void) loader_dri3_blit_image(draw,
891                                     front->image,
892                                     front->linear_buffer,
893                                     0, 0, front->width, front->height,
894                                     0, 0, 0);
895 }
896 
897 void
loader_dri3_wait_gl(struct loader_dri3_drawable * draw)898 loader_dri3_wait_gl(struct loader_dri3_drawable *draw)
899 {
900    struct loader_dri3_buffer *front;
901 
902    if (draw == NULL || !draw->have_fake_front)
903       return;
904 
905    front = dri3_fake_front_buffer(draw);
906 
907    /* In the psc->is_different_gpu case, we update the linear_buffer
908     * before updating the real front.
909     */
910    if (draw->is_different_gpu)
911       (void) loader_dri3_blit_image(draw,
912                                     front->linear_buffer,
913                                     front->image,
914                                     0, 0, front->width, front->height,
915                                     0, 0, __BLIT_FLAG_FLUSH);
916    loader_dri3_swapbuffer_barrier(draw);
917    loader_dri3_copy_drawable(draw, draw->drawable, front->pixmap);
918 }
919 
920 /** dri3_flush_present_events
921  *
922  * Process any present events that have been received from the X server
923  */
924 static void
dri3_flush_present_events(struct loader_dri3_drawable * draw)925 dri3_flush_present_events(struct loader_dri3_drawable *draw)
926 {
927    /* Check to see if any configuration changes have occurred
928     * since we were last invoked
929     */
930    if (draw->has_event_waiter)
931       return;
932 
933    if (draw->special_event) {
934       xcb_generic_event_t    *ev;
935 
936       while ((ev = xcb_poll_for_special_event(draw->conn,
937                                               draw->special_event)) != NULL) {
938          xcb_present_generic_event_t *ge = (void *) ev;
939          dri3_handle_present_event(draw, ge);
940       }
941    }
942 }
943 
944 /** loader_dri3_swap_buffers_msc
945  *
946  * Make the current back buffer visible using the present extension
947  */
948 int64_t
loader_dri3_swap_buffers_msc(struct loader_dri3_drawable * draw,int64_t target_msc,int64_t divisor,int64_t remainder,unsigned flush_flags,const int * rects,int n_rects,bool force_copy)949 loader_dri3_swap_buffers_msc(struct loader_dri3_drawable *draw,
950                              int64_t target_msc, int64_t divisor,
951                              int64_t remainder, unsigned flush_flags,
952                              const int *rects, int n_rects,
953                              bool force_copy)
954 {
955    struct loader_dri3_buffer *back;
956    int64_t ret = 0;
957    uint32_t options = XCB_PRESENT_OPTION_NONE;
958 
959    draw->vtable->flush_drawable(draw, flush_flags);
960 
961    back = dri3_find_back_alloc(draw);
962 
963    mtx_lock(&draw->mtx);
964 
965    if (draw->adaptive_sync && !draw->adaptive_sync_active) {
966       set_adaptive_sync_property(draw->conn, draw->drawable, true);
967       draw->adaptive_sync_active = true;
968    }
969 
970    if (draw->is_different_gpu && back) {
971       /* Update the linear buffer before presenting the pixmap */
972       (void) loader_dri3_blit_image(draw,
973                                     back->linear_buffer,
974                                     back->image,
975                                     0, 0, back->width, back->height,
976                                     0, 0, __BLIT_FLAG_FLUSH);
977    }
978 
979    /* If we need to preload the new back buffer, remember the source.
980     * The force_copy parameter is used by EGL to attempt to preserve
981     * the back buffer across a call to this function.
982     */
983    if (draw->swap_method != __DRI_ATTRIB_SWAP_UNDEFINED || force_copy)
984       draw->cur_blit_source = LOADER_DRI3_BACK_ID(draw->cur_back);
985 
986    /* Exchange the back and fake front. Even though the server knows about these
987     * buffers, it has no notion of back and fake front.
988     */
989    if (back && draw->have_fake_front) {
990       struct loader_dri3_buffer *tmp;
991 
992       tmp = dri3_fake_front_buffer(draw);
993       draw->buffers[LOADER_DRI3_FRONT_ID] = back;
994       draw->buffers[LOADER_DRI3_BACK_ID(draw->cur_back)] = tmp;
995 
996       if (draw->swap_method == __DRI_ATTRIB_SWAP_COPY  || force_copy)
997          draw->cur_blit_source = LOADER_DRI3_FRONT_ID;
998    }
999 
1000    dri3_flush_present_events(draw);
1001 
1002    if (back && !draw->is_pixmap) {
1003       dri3_fence_reset(draw->conn, back);
1004 
1005       /* Compute when we want the frame shown by taking the last known
1006        * successful MSC and adding in a swap interval for each outstanding swap
1007        * request. target_msc=divisor=remainder=0 means "Use glXSwapBuffers()
1008        * semantic"
1009        */
1010       ++draw->send_sbc;
1011       if (target_msc == 0 && divisor == 0 && remainder == 0)
1012          target_msc = draw->msc + abs(draw->swap_interval) *
1013                       (draw->send_sbc - draw->recv_sbc);
1014       else if (divisor == 0 && remainder > 0) {
1015          /* From the GLX_OML_sync_control spec:
1016           *     "If <divisor> = 0, the swap will occur when MSC becomes
1017           *      greater than or equal to <target_msc>."
1018           *
1019           * Note that there's no mention of the remainder.  The Present
1020           * extension throws BadValue for remainder != 0 with divisor == 0, so
1021           * just drop the passed in value.
1022           */
1023          remainder = 0;
1024       }
1025 
1026       /* From the GLX_EXT_swap_control spec
1027        * and the EGL 1.4 spec (page 53):
1028        *
1029        *     "If <interval> is set to a value of 0, buffer swaps are not
1030        *      synchronized to a video frame."
1031        *
1032        * From GLX_EXT_swap_control_tear:
1033        *
1034        *     "If <interval> is negative, the minimum number of video frames
1035        *      between buffer swaps is the absolute value of <interval>. In this
1036        *      case, if abs(<interval>) video frames have already passed from
1037        *      the previous swap when the swap is ready to be performed, the
1038        *      swap will occur without synchronization to a video frame."
1039        *
1040        * Implementation note: It is possible to enable triple buffering
1041        * behaviour by not using XCB_PRESENT_OPTION_ASYNC, but this should not be
1042        * the default.
1043        */
1044       if (draw->swap_interval <= 0)
1045           options |= XCB_PRESENT_OPTION_ASYNC;
1046 
1047       /* If we need to populate the new back, but need to reuse the back
1048        * buffer slot due to lack of local blit capabilities, make sure
1049        * the server doesn't flip and we deadlock.
1050        */
1051       if (!loader_dri3_have_image_blit(draw) && draw->cur_blit_source != -1)
1052          options |= XCB_PRESENT_OPTION_COPY;
1053 #ifdef HAVE_DRI3_MODIFIERS
1054       if (draw->multiplanes_available)
1055          options |= XCB_PRESENT_OPTION_SUBOPTIMAL;
1056 #endif
1057       back->busy = 1;
1058       back->last_swap = draw->send_sbc;
1059 
1060       xcb_xfixes_region_t region = 0;
1061       xcb_rectangle_t xcb_rects[64];
1062 
1063       if (n_rects > 0 && n_rects <= ARRAY_SIZE(xcb_rects)) {
1064          for (int i = 0; i < n_rects; i++) {
1065             const int *rect = &rects[i * 4];
1066             xcb_rects[i].x = rect[0];
1067             xcb_rects[i].y = draw->height - rect[1] - rect[3];
1068             xcb_rects[i].width = rect[2];
1069             xcb_rects[i].height = rect[3];
1070          }
1071 
1072          region = xcb_generate_id(draw->conn);
1073          xcb_xfixes_create_region(draw->conn, region, n_rects, xcb_rects);
1074       }
1075 
1076       xcb_present_pixmap(draw->conn,
1077                          draw->drawable,
1078                          back->pixmap,
1079                          (uint32_t) draw->send_sbc,
1080                          0,                                    /* valid */
1081                          region,                               /* update */
1082                          0,                                    /* x_off */
1083                          0,                                    /* y_off */
1084                          None,                                 /* target_crtc */
1085                          None,
1086                          back->sync_fence,
1087                          options,
1088                          target_msc,
1089                          divisor,
1090                          remainder, 0, NULL);
1091       ret = (int64_t) draw->send_sbc;
1092 
1093       if (region)
1094          xcb_xfixes_destroy_region(draw->conn, region);
1095 
1096       /* Schedule a server-side back-preserving blit if necessary.
1097        * This happens iff all conditions below are satisfied:
1098        * a) We have a fake front,
1099        * b) We need to preserve the back buffer,
1100        * c) We don't have local blit capabilities.
1101        */
1102       if (!loader_dri3_have_image_blit(draw) && draw->cur_blit_source != -1 &&
1103           draw->cur_blit_source != LOADER_DRI3_BACK_ID(draw->cur_back)) {
1104          struct loader_dri3_buffer *new_back = dri3_back_buffer(draw);
1105          struct loader_dri3_buffer *src = draw->buffers[draw->cur_blit_source];
1106 
1107          dri3_fence_reset(draw->conn, new_back);
1108          dri3_copy_area(draw->conn, src->pixmap,
1109                         new_back->pixmap,
1110                         dri3_drawable_gc(draw),
1111                         0, 0, 0, 0, draw->width, draw->height);
1112          dri3_fence_trigger(draw->conn, new_back);
1113          new_back->last_swap = src->last_swap;
1114       }
1115 
1116       xcb_flush(draw->conn);
1117       if (draw->stamp)
1118          ++(*draw->stamp);
1119    }
1120    mtx_unlock(&draw->mtx);
1121 
1122    draw->ext->flush->invalidate(draw->dri_drawable);
1123 
1124    return ret;
1125 }
1126 
1127 int
loader_dri3_query_buffer_age(struct loader_dri3_drawable * draw)1128 loader_dri3_query_buffer_age(struct loader_dri3_drawable *draw)
1129 {
1130    struct loader_dri3_buffer *back = dri3_find_back_alloc(draw);
1131    int ret;
1132 
1133    mtx_lock(&draw->mtx);
1134    ret = (!back || back->last_swap == 0) ? 0 :
1135       draw->send_sbc - back->last_swap + 1;
1136    mtx_unlock(&draw->mtx);
1137 
1138    return ret;
1139 }
1140 
1141 /** loader_dri3_open
1142  *
1143  * Wrapper around xcb_dri3_open
1144  */
1145 int
loader_dri3_open(xcb_connection_t * conn,xcb_window_t root,uint32_t provider)1146 loader_dri3_open(xcb_connection_t *conn,
1147                  xcb_window_t root,
1148                  uint32_t provider)
1149 {
1150    xcb_dri3_open_cookie_t       cookie;
1151    xcb_dri3_open_reply_t        *reply;
1152    int                          fd;
1153 
1154    cookie = xcb_dri3_open(conn,
1155                           root,
1156                           provider);
1157 
1158    reply = xcb_dri3_open_reply(conn, cookie, NULL);
1159    if (!reply)
1160       return -1;
1161 
1162    if (reply->nfd != 1) {
1163       free(reply);
1164       return -1;
1165    }
1166 
1167    fd = xcb_dri3_open_reply_fds(conn, reply)[0];
1168    free(reply);
1169    fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
1170 
1171    return fd;
1172 }
1173 
1174 static uint32_t
dri3_cpp_for_format(uint32_t format)1175 dri3_cpp_for_format(uint32_t format) {
1176    switch (format) {
1177    case  __DRI_IMAGE_FORMAT_R8:
1178       return 1;
1179    case  __DRI_IMAGE_FORMAT_RGB565:
1180    case  __DRI_IMAGE_FORMAT_GR88:
1181       return 2;
1182    case  __DRI_IMAGE_FORMAT_XRGB8888:
1183    case  __DRI_IMAGE_FORMAT_ARGB8888:
1184    case  __DRI_IMAGE_FORMAT_ABGR8888:
1185    case  __DRI_IMAGE_FORMAT_XBGR8888:
1186    case  __DRI_IMAGE_FORMAT_XRGB2101010:
1187    case  __DRI_IMAGE_FORMAT_ARGB2101010:
1188    case  __DRI_IMAGE_FORMAT_XBGR2101010:
1189    case  __DRI_IMAGE_FORMAT_ABGR2101010:
1190    case  __DRI_IMAGE_FORMAT_SARGB8:
1191    case  __DRI_IMAGE_FORMAT_SABGR8:
1192    case  __DRI_IMAGE_FORMAT_SXRGB8:
1193       return 4;
1194    case __DRI_IMAGE_FORMAT_XBGR16161616F:
1195    case __DRI_IMAGE_FORMAT_ABGR16161616F:
1196       return 8;
1197    case  __DRI_IMAGE_FORMAT_NONE:
1198    default:
1199       return 0;
1200    }
1201 }
1202 
1203 /* Map format of render buffer to corresponding format for the linear_buffer
1204  * used for sharing with the display gpu of a Prime setup (== is_different_gpu).
1205  * Usually linear_format == format, except for depth >= 30 formats, where
1206  * different gpu vendors have different preferences wrt. color channel ordering.
1207  */
1208 static uint32_t
dri3_linear_format_for_format(struct loader_dri3_drawable * draw,uint32_t format)1209 dri3_linear_format_for_format(struct loader_dri3_drawable *draw, uint32_t format)
1210 {
1211    switch (format) {
1212       case  __DRI_IMAGE_FORMAT_XRGB2101010:
1213       case  __DRI_IMAGE_FORMAT_XBGR2101010:
1214          /* Different preferred formats for different hw */
1215          if (dri3_get_red_mask_for_depth(draw, 30) == 0x3ff)
1216             return __DRI_IMAGE_FORMAT_XBGR2101010;
1217          else
1218             return __DRI_IMAGE_FORMAT_XRGB2101010;
1219 
1220       case  __DRI_IMAGE_FORMAT_ARGB2101010:
1221       case  __DRI_IMAGE_FORMAT_ABGR2101010:
1222          /* Different preferred formats for different hw */
1223          if (dri3_get_red_mask_for_depth(draw, 30) == 0x3ff)
1224             return __DRI_IMAGE_FORMAT_ABGR2101010;
1225          else
1226             return __DRI_IMAGE_FORMAT_ARGB2101010;
1227 
1228       default:
1229          return format;
1230    }
1231 }
1232 
1233 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
1234  * the createImageFromFds call takes DRM_FORMAT codes. To avoid
1235  * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
1236  * translate to DRM_FORMAT codes in the call to createImageFromFds
1237  */
1238 static int
image_format_to_fourcc(int format)1239 image_format_to_fourcc(int format)
1240 {
1241 
1242    /* Convert from __DRI_IMAGE_FORMAT to DRM_FORMAT (sigh) */
1243    switch (format) {
1244    case __DRI_IMAGE_FORMAT_SARGB8: return __DRI_IMAGE_FOURCC_SARGB8888;
1245    case __DRI_IMAGE_FORMAT_SABGR8: return __DRI_IMAGE_FOURCC_SABGR8888;
1246    case __DRI_IMAGE_FORMAT_SXRGB8: return __DRI_IMAGE_FOURCC_SXRGB8888;
1247    case __DRI_IMAGE_FORMAT_RGB565: return DRM_FORMAT_RGB565;
1248    case __DRI_IMAGE_FORMAT_XRGB8888: return DRM_FORMAT_XRGB8888;
1249    case __DRI_IMAGE_FORMAT_ARGB8888: return DRM_FORMAT_ARGB8888;
1250    case __DRI_IMAGE_FORMAT_ABGR8888: return DRM_FORMAT_ABGR8888;
1251    case __DRI_IMAGE_FORMAT_XBGR8888: return DRM_FORMAT_XBGR8888;
1252    case __DRI_IMAGE_FORMAT_XRGB2101010: return DRM_FORMAT_XRGB2101010;
1253    case __DRI_IMAGE_FORMAT_ARGB2101010: return DRM_FORMAT_ARGB2101010;
1254    case __DRI_IMAGE_FORMAT_XBGR2101010: return DRM_FORMAT_XBGR2101010;
1255    case __DRI_IMAGE_FORMAT_ABGR2101010: return DRM_FORMAT_ABGR2101010;
1256    case __DRI_IMAGE_FORMAT_XBGR16161616F: return DRM_FORMAT_XBGR16161616F;
1257    case __DRI_IMAGE_FORMAT_ABGR16161616F: return DRM_FORMAT_ABGR16161616F;
1258    }
1259    return 0;
1260 }
1261 
1262 #ifdef HAVE_DRI3_MODIFIERS
1263 static bool
has_supported_modifier(struct loader_dri3_drawable * draw,unsigned int format,uint64_t * modifiers,uint32_t count)1264 has_supported_modifier(struct loader_dri3_drawable *draw, unsigned int format,
1265                        uint64_t *modifiers, uint32_t count)
1266 {
1267    uint64_t *supported_modifiers;
1268    int32_t supported_modifiers_count;
1269    bool found = false;
1270    int i, j;
1271 
1272    if (!draw->ext->image->queryDmaBufModifiers(draw->dri_screen,
1273                                                format, 0, NULL, NULL,
1274                                                &supported_modifiers_count) ||
1275        supported_modifiers_count == 0)
1276       return false;
1277 
1278    supported_modifiers = malloc(supported_modifiers_count * sizeof(uint64_t));
1279    if (!supported_modifiers)
1280       return false;
1281 
1282    draw->ext->image->queryDmaBufModifiers(draw->dri_screen, format,
1283                                           supported_modifiers_count,
1284                                           supported_modifiers, NULL,
1285                                           &supported_modifiers_count);
1286 
1287    for (i = 0; !found && i < supported_modifiers_count; i++) {
1288       for (j = 0; !found && j < count; j++) {
1289          if (supported_modifiers[i] == modifiers[j])
1290             found = true;
1291       }
1292    }
1293 
1294    free(supported_modifiers);
1295    return found;
1296 }
1297 #endif
1298 
1299 /** loader_dri3_alloc_render_buffer
1300  *
1301  * Use the driver createImage function to construct a __DRIimage, then
1302  * get a file descriptor for that and create an X pixmap from that
1303  *
1304  * Allocate an xshmfence for synchronization
1305  */
1306 static struct loader_dri3_buffer *
dri3_alloc_render_buffer(struct loader_dri3_drawable * draw,unsigned int format,int width,int height,int depth)1307 dri3_alloc_render_buffer(struct loader_dri3_drawable *draw, unsigned int format,
1308                          int width, int height, int depth)
1309 {
1310    struct loader_dri3_buffer *buffer;
1311    __DRIimage *pixmap_buffer;
1312    xcb_pixmap_t pixmap;
1313    xcb_sync_fence_t sync_fence;
1314    struct xshmfence *shm_fence;
1315    int buffer_fds[4], fence_fd;
1316    int num_planes = 0;
1317    int i, mod;
1318    int ret;
1319 
1320    /* Create an xshmfence object and
1321     * prepare to send that to the X server
1322     */
1323 
1324    fence_fd = xshmfence_alloc_shm();
1325    if (fence_fd < 0)
1326       return NULL;
1327 
1328    shm_fence = xshmfence_map_shm(fence_fd);
1329    if (shm_fence == NULL)
1330       goto no_shm_fence;
1331 
1332    /* Allocate the image from the driver
1333     */
1334    buffer = calloc(1, sizeof *buffer);
1335    if (!buffer)
1336       goto no_buffer;
1337 
1338    buffer->cpp = dri3_cpp_for_format(format);
1339    if (!buffer->cpp)
1340       goto no_image;
1341 
1342    if (!draw->is_different_gpu) {
1343 #ifdef HAVE_DRI3_MODIFIERS
1344       if (draw->multiplanes_available &&
1345           draw->ext->image->base.version >= 15 &&
1346           draw->ext->image->queryDmaBufModifiers &&
1347           draw->ext->image->createImageWithModifiers) {
1348          xcb_dri3_get_supported_modifiers_cookie_t mod_cookie;
1349          xcb_dri3_get_supported_modifiers_reply_t *mod_reply;
1350          xcb_generic_error_t *error = NULL;
1351          uint64_t *modifiers = NULL;
1352          uint32_t count = 0;
1353 
1354          mod_cookie = xcb_dri3_get_supported_modifiers(draw->conn,
1355                                                        draw->window,
1356                                                        depth, buffer->cpp * 8);
1357          mod_reply = xcb_dri3_get_supported_modifiers_reply(draw->conn,
1358                                                             mod_cookie,
1359                                                             &error);
1360          if (!mod_reply)
1361             goto no_image;
1362 
1363          if (mod_reply->num_window_modifiers) {
1364             count = mod_reply->num_window_modifiers;
1365             modifiers = malloc(count * sizeof(uint64_t));
1366             if (!modifiers) {
1367                free(mod_reply);
1368                goto no_image;
1369             }
1370 
1371             memcpy(modifiers,
1372                    xcb_dri3_get_supported_modifiers_window_modifiers(mod_reply),
1373                    count * sizeof(uint64_t));
1374 
1375             if (!has_supported_modifier(draw, image_format_to_fourcc(format),
1376                                         modifiers, count)) {
1377                free(modifiers);
1378                count = 0;
1379                modifiers = NULL;
1380             }
1381          }
1382 
1383          if (mod_reply->num_screen_modifiers && modifiers == NULL) {
1384             count = mod_reply->num_screen_modifiers;
1385             modifiers = malloc(count * sizeof(uint64_t));
1386             if (!modifiers) {
1387                free(modifiers);
1388                free(mod_reply);
1389                goto no_image;
1390             }
1391 
1392             memcpy(modifiers,
1393                    xcb_dri3_get_supported_modifiers_screen_modifiers(mod_reply),
1394                    count * sizeof(uint64_t));
1395          }
1396 
1397          free(mod_reply);
1398 
1399          /* don't use createImageWithModifiers() if we have no
1400           * modifiers, other things depend on the use flags when
1401           * there are no modifiers to know that a buffer can be
1402           * shared.
1403           */
1404          if (modifiers) {
1405             buffer->image = draw->ext->image->createImageWithModifiers(draw->dri_screen,
1406                                                                        width, height,
1407                                                                        format,
1408                                                                        modifiers,
1409                                                                        count,
1410                                                                        buffer);
1411          }
1412 
1413          free(modifiers);
1414       }
1415 #endif
1416       if (!buffer->image)
1417          buffer->image = draw->ext->image->createImage(draw->dri_screen,
1418                                                        width, height,
1419                                                        format,
1420                                                        __DRI_IMAGE_USE_SHARE |
1421                                                        __DRI_IMAGE_USE_SCANOUT |
1422                                                        __DRI_IMAGE_USE_BACKBUFFER |
1423                                                        (draw->is_protected_content ?
1424                                                          __DRI_IMAGE_USE_PROTECTED : 0),
1425                                                        buffer);
1426 
1427       pixmap_buffer = buffer->image;
1428 
1429       if (!buffer->image)
1430          goto no_image;
1431    } else {
1432       buffer->image = draw->ext->image->createImage(draw->dri_screen,
1433                                                     width, height,
1434                                                     format,
1435                                                     0,
1436                                                     buffer);
1437 
1438       if (!buffer->image)
1439          goto no_image;
1440 
1441       buffer->linear_buffer =
1442         draw->ext->image->createImage(draw->dri_screen,
1443                                       width, height,
1444                                       dri3_linear_format_for_format(draw, format),
1445                                       __DRI_IMAGE_USE_SHARE |
1446                                       __DRI_IMAGE_USE_LINEAR |
1447                                       __DRI_IMAGE_USE_BACKBUFFER,
1448                                       buffer);
1449       pixmap_buffer = buffer->linear_buffer;
1450 
1451       if (!buffer->linear_buffer)
1452          goto no_linear_buffer;
1453    }
1454 
1455    /* X want some information about the planes, so ask the image for it
1456     */
1457    if (!draw->ext->image->queryImage(pixmap_buffer, __DRI_IMAGE_ATTRIB_NUM_PLANES,
1458                                      &num_planes))
1459       num_planes = 1;
1460 
1461    for (i = 0; i < num_planes; i++) {
1462       __DRIimage *image = draw->ext->image->fromPlanar(pixmap_buffer, i, NULL);
1463 
1464       if (!image) {
1465          assert(i == 0);
1466          image = pixmap_buffer;
1467       }
1468 
1469       buffer_fds[i] = -1;
1470 
1471       ret = draw->ext->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD,
1472                                          &buffer_fds[i]);
1473       ret &= draw->ext->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE,
1474                                           &buffer->strides[i]);
1475       ret &= draw->ext->image->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET,
1476                                           &buffer->offsets[i]);
1477       if (image != pixmap_buffer)
1478          draw->ext->image->destroyImage(image);
1479 
1480       if (!ret)
1481          goto no_buffer_attrib;
1482    }
1483 
1484    ret = draw->ext->image->queryImage(pixmap_buffer,
1485                                      __DRI_IMAGE_ATTRIB_MODIFIER_UPPER, &mod);
1486    buffer->modifier = (uint64_t) mod << 32;
1487    ret &= draw->ext->image->queryImage(pixmap_buffer,
1488                                        __DRI_IMAGE_ATTRIB_MODIFIER_LOWER, &mod);
1489    buffer->modifier |= (uint64_t)(mod & 0xffffffff);
1490 
1491    if (!ret)
1492       buffer->modifier = DRM_FORMAT_MOD_INVALID;
1493 
1494    pixmap = xcb_generate_id(draw->conn);
1495 #ifdef HAVE_DRI3_MODIFIERS
1496    if (draw->multiplanes_available &&
1497        buffer->modifier != DRM_FORMAT_MOD_INVALID) {
1498       xcb_dri3_pixmap_from_buffers(draw->conn,
1499                                    pixmap,
1500                                    draw->window,
1501                                    num_planes,
1502                                    width, height,
1503                                    buffer->strides[0], buffer->offsets[0],
1504                                    buffer->strides[1], buffer->offsets[1],
1505                                    buffer->strides[2], buffer->offsets[2],
1506                                    buffer->strides[3], buffer->offsets[3],
1507                                    depth, buffer->cpp * 8,
1508                                    buffer->modifier,
1509                                    buffer_fds);
1510    } else
1511 #endif
1512    {
1513       xcb_dri3_pixmap_from_buffer(draw->conn,
1514                                   pixmap,
1515                                   draw->drawable,
1516                                   buffer->size,
1517                                   width, height, buffer->strides[0],
1518                                   depth, buffer->cpp * 8,
1519                                   buffer_fds[0]);
1520    }
1521 
1522    xcb_dri3_fence_from_fd(draw->conn,
1523                           pixmap,
1524                           (sync_fence = xcb_generate_id(draw->conn)),
1525                           false,
1526                           fence_fd);
1527 
1528    buffer->pixmap = pixmap;
1529    buffer->own_pixmap = true;
1530    buffer->sync_fence = sync_fence;
1531    buffer->shm_fence = shm_fence;
1532    buffer->width = width;
1533    buffer->height = height;
1534 
1535    /* Mark the buffer as idle
1536     */
1537    dri3_fence_set(buffer);
1538 
1539    return buffer;
1540 
1541 no_buffer_attrib:
1542    do {
1543       if (buffer_fds[i] != -1)
1544          close(buffer_fds[i]);
1545    } while (--i >= 0);
1546    draw->ext->image->destroyImage(pixmap_buffer);
1547 no_linear_buffer:
1548    if (draw->is_different_gpu)
1549       draw->ext->image->destroyImage(buffer->image);
1550 no_image:
1551    free(buffer);
1552 no_buffer:
1553    xshmfence_unmap_shm(shm_fence);
1554 no_shm_fence:
1555    close(fence_fd);
1556    return NULL;
1557 }
1558 
1559 /** loader_dri3_update_drawable
1560  *
1561  * Called the first time we use the drawable and then
1562  * after we receive present configure notify events to
1563  * track the geometry of the drawable
1564  */
1565 static int
dri3_update_drawable(struct loader_dri3_drawable * draw)1566 dri3_update_drawable(struct loader_dri3_drawable *draw)
1567 {
1568    mtx_lock(&draw->mtx);
1569    if (draw->first_init) {
1570       xcb_get_geometry_cookie_t                 geom_cookie;
1571       xcb_get_geometry_reply_t                  *geom_reply;
1572       xcb_void_cookie_t                         cookie;
1573       xcb_generic_error_t                       *error;
1574       xcb_present_query_capabilities_cookie_t   present_capabilities_cookie;
1575       xcb_present_query_capabilities_reply_t    *present_capabilities_reply;
1576       xcb_window_t                               root_win;
1577 
1578       draw->first_init = false;
1579 
1580       /* Try to select for input on the window.
1581        *
1582        * If the drawable is a window, this will get our events
1583        * delivered.
1584        *
1585        * Otherwise, we'll get a BadWindow error back from this request which
1586        * will let us know that the drawable is a pixmap instead.
1587        */
1588 
1589       draw->eid = xcb_generate_id(draw->conn);
1590       cookie =
1591          xcb_present_select_input_checked(draw->conn, draw->eid, draw->drawable,
1592                                           XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
1593                                           XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
1594                                           XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
1595 
1596       present_capabilities_cookie =
1597          xcb_present_query_capabilities(draw->conn, draw->drawable);
1598 
1599       /* Create an XCB event queue to hold present events outside of the usual
1600        * application event queue
1601        */
1602       draw->special_event = xcb_register_for_special_xge(draw->conn,
1603                                                          &xcb_present_id,
1604                                                          draw->eid,
1605                                                          draw->stamp);
1606       geom_cookie = xcb_get_geometry(draw->conn, draw->drawable);
1607 
1608       geom_reply = xcb_get_geometry_reply(draw->conn, geom_cookie, NULL);
1609 
1610       if (!geom_reply) {
1611          mtx_unlock(&draw->mtx);
1612          return false;
1613       }
1614       draw->width = geom_reply->width;
1615       draw->height = geom_reply->height;
1616       draw->depth = geom_reply->depth;
1617       draw->vtable->set_drawable_size(draw, draw->width, draw->height);
1618       root_win = geom_reply->root;
1619 
1620       free(geom_reply);
1621 
1622       draw->is_pixmap = false;
1623 
1624       /* Check to see if our select input call failed. If it failed with a
1625        * BadWindow error, then assume the drawable is a pixmap. Destroy the
1626        * special event queue created above and mark the drawable as a pixmap
1627        */
1628 
1629       error = xcb_request_check(draw->conn, cookie);
1630 
1631       present_capabilities_reply =
1632           xcb_present_query_capabilities_reply(draw->conn,
1633                                                present_capabilities_cookie,
1634                                                NULL);
1635 
1636       if (present_capabilities_reply) {
1637          draw->present_capabilities = present_capabilities_reply->capabilities;
1638          free(present_capabilities_reply);
1639       } else
1640          draw->present_capabilities = 0;
1641 
1642       if (error) {
1643          if (error->error_code != BadWindow) {
1644             free(error);
1645             mtx_unlock(&draw->mtx);
1646             return false;
1647          }
1648          free(error);
1649          draw->is_pixmap = true;
1650          xcb_unregister_for_special_event(draw->conn, draw->special_event);
1651          draw->special_event = NULL;
1652       }
1653 
1654       if (draw->is_pixmap)
1655          draw->window = root_win;
1656       else
1657          draw->window = draw->drawable;
1658    }
1659    dri3_flush_present_events(draw);
1660    mtx_unlock(&draw->mtx);
1661    return true;
1662 }
1663 
1664 __DRIimage *
loader_dri3_create_image(xcb_connection_t * c,xcb_dri3_buffer_from_pixmap_reply_t * bp_reply,unsigned int format,__DRIscreen * dri_screen,const __DRIimageExtension * image,void * loaderPrivate)1665 loader_dri3_create_image(xcb_connection_t *c,
1666                          xcb_dri3_buffer_from_pixmap_reply_t *bp_reply,
1667                          unsigned int format,
1668                          __DRIscreen *dri_screen,
1669                          const __DRIimageExtension *image,
1670                          void *loaderPrivate)
1671 {
1672    int                                  *fds;
1673    __DRIimage                           *image_planar, *ret;
1674    int                                  stride, offset;
1675 
1676    /* Get an FD for the pixmap object
1677     */
1678    fds = xcb_dri3_buffer_from_pixmap_reply_fds(c, bp_reply);
1679 
1680    stride = bp_reply->stride;
1681    offset = 0;
1682 
1683    /* createImageFromFds creates a wrapper __DRIimage structure which
1684     * can deal with multiple planes for things like Yuv images. So, once
1685     * we've gotten the planar wrapper, pull the single plane out of it and
1686     * discard the wrapper.
1687     */
1688    image_planar = image->createImageFromFds(dri_screen,
1689                                             bp_reply->width,
1690                                             bp_reply->height,
1691                                             image_format_to_fourcc(format),
1692                                             fds, 1,
1693                                             &stride, &offset, loaderPrivate);
1694    close(fds[0]);
1695    if (!image_planar)
1696       return NULL;
1697 
1698    ret = image->fromPlanar(image_planar, 0, loaderPrivate);
1699 
1700    if (!ret)
1701       ret = image_planar;
1702    else
1703       image->destroyImage(image_planar);
1704 
1705    return ret;
1706 }
1707 
1708 #ifdef HAVE_DRI3_MODIFIERS
1709 __DRIimage *
loader_dri3_create_image_from_buffers(xcb_connection_t * c,xcb_dri3_buffers_from_pixmap_reply_t * bp_reply,unsigned int format,__DRIscreen * dri_screen,const __DRIimageExtension * image,void * loaderPrivate)1710 loader_dri3_create_image_from_buffers(xcb_connection_t *c,
1711                                       xcb_dri3_buffers_from_pixmap_reply_t *bp_reply,
1712                                       unsigned int format,
1713                                       __DRIscreen *dri_screen,
1714                                       const __DRIimageExtension *image,
1715                                       void *loaderPrivate)
1716 {
1717    __DRIimage                           *ret;
1718    int                                  *fds;
1719    uint32_t                             *strides_in, *offsets_in;
1720    int                                   strides[4], offsets[4];
1721    unsigned                              error;
1722    int                                   i;
1723 
1724    if (bp_reply->nfd > 4)
1725       return NULL;
1726 
1727    fds = xcb_dri3_buffers_from_pixmap_reply_fds(c, bp_reply);
1728    strides_in = xcb_dri3_buffers_from_pixmap_strides(bp_reply);
1729    offsets_in = xcb_dri3_buffers_from_pixmap_offsets(bp_reply);
1730    for (i = 0; i < bp_reply->nfd; i++) {
1731       strides[i] = strides_in[i];
1732       offsets[i] = offsets_in[i];
1733    }
1734 
1735    ret = image->createImageFromDmaBufs2(dri_screen,
1736                                         bp_reply->width,
1737                                         bp_reply->height,
1738                                         image_format_to_fourcc(format),
1739                                         bp_reply->modifier,
1740                                         fds, bp_reply->nfd,
1741                                         strides, offsets,
1742                                         0, 0, 0, 0, /* UNDEFINED */
1743                                         &error, loaderPrivate);
1744 
1745    for (i = 0; i < bp_reply->nfd; i++)
1746       close(fds[i]);
1747 
1748    return ret;
1749 }
1750 #endif
1751 
1752 /** dri3_get_pixmap_buffer
1753  *
1754  * Get the DRM object for a pixmap from the X server and
1755  * wrap that with a __DRIimage structure using createImageFromFds
1756  */
1757 static struct loader_dri3_buffer *
dri3_get_pixmap_buffer(__DRIdrawable * driDrawable,unsigned int format,enum loader_dri3_buffer_type buffer_type,struct loader_dri3_drawable * draw)1758 dri3_get_pixmap_buffer(__DRIdrawable *driDrawable, unsigned int format,
1759                        enum loader_dri3_buffer_type buffer_type,
1760                        struct loader_dri3_drawable *draw)
1761 {
1762    int                                  buf_id = loader_dri3_pixmap_buf_id(buffer_type);
1763    struct loader_dri3_buffer            *buffer = draw->buffers[buf_id];
1764    xcb_drawable_t                       pixmap;
1765    xcb_sync_fence_t                     sync_fence;
1766    struct xshmfence                     *shm_fence;
1767    int                                  width;
1768    int                                  height;
1769    int                                  fence_fd;
1770    __DRIscreen                          *cur_screen;
1771 
1772    if (buffer)
1773       return buffer;
1774 
1775    pixmap = draw->drawable;
1776 
1777    buffer = calloc(1, sizeof *buffer);
1778    if (!buffer)
1779       goto no_buffer;
1780 
1781    fence_fd = xshmfence_alloc_shm();
1782    if (fence_fd < 0)
1783       goto no_fence;
1784    shm_fence = xshmfence_map_shm(fence_fd);
1785    if (shm_fence == NULL) {
1786       close (fence_fd);
1787       goto no_fence;
1788    }
1789 
1790    /* Get the currently-bound screen or revert to using the drawable's screen if
1791     * no contexts are currently bound. The latter case is at least necessary for
1792     * obs-studio, when using Window Capture (Xcomposite) as a Source.
1793     */
1794    cur_screen = draw->vtable->get_dri_screen();
1795    if (!cur_screen) {
1796        cur_screen = draw->dri_screen;
1797    }
1798 
1799    xcb_dri3_fence_from_fd(draw->conn,
1800                           pixmap,
1801                           (sync_fence = xcb_generate_id(draw->conn)),
1802                           false,
1803                           fence_fd);
1804 #ifdef HAVE_DRI3_MODIFIERS
1805    if (draw->multiplanes_available &&
1806        draw->ext->image->base.version >= 15 &&
1807        draw->ext->image->createImageFromDmaBufs2) {
1808       xcb_dri3_buffers_from_pixmap_cookie_t bps_cookie;
1809       xcb_dri3_buffers_from_pixmap_reply_t *bps_reply;
1810 
1811       bps_cookie = xcb_dri3_buffers_from_pixmap(draw->conn, pixmap);
1812       bps_reply = xcb_dri3_buffers_from_pixmap_reply(draw->conn, bps_cookie,
1813                                                      NULL);
1814       if (!bps_reply)
1815          goto no_image;
1816       buffer->image =
1817          loader_dri3_create_image_from_buffers(draw->conn, bps_reply, format,
1818                                                cur_screen, draw->ext->image,
1819                                                buffer);
1820       width = bps_reply->width;
1821       height = bps_reply->height;
1822       free(bps_reply);
1823    } else
1824 #endif
1825    {
1826       xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie;
1827       xcb_dri3_buffer_from_pixmap_reply_t *bp_reply;
1828 
1829       bp_cookie = xcb_dri3_buffer_from_pixmap(draw->conn, pixmap);
1830       bp_reply = xcb_dri3_buffer_from_pixmap_reply(draw->conn, bp_cookie, NULL);
1831       if (!bp_reply)
1832          goto no_image;
1833 
1834       buffer->image = loader_dri3_create_image(draw->conn, bp_reply, format,
1835                                                cur_screen, draw->ext->image,
1836                                                buffer);
1837       width = bp_reply->width;
1838       height = bp_reply->height;
1839       free(bp_reply);
1840    }
1841 
1842    if (!buffer->image)
1843       goto no_image;
1844 
1845    buffer->pixmap = pixmap;
1846    buffer->own_pixmap = false;
1847    buffer->width = width;
1848    buffer->height = height;
1849    buffer->shm_fence = shm_fence;
1850    buffer->sync_fence = sync_fence;
1851 
1852    draw->buffers[buf_id] = buffer;
1853 
1854    return buffer;
1855 
1856 no_image:
1857    xcb_sync_destroy_fence(draw->conn, sync_fence);
1858    xshmfence_unmap_shm(shm_fence);
1859 no_fence:
1860    free(buffer);
1861 no_buffer:
1862    return NULL;
1863 }
1864 
1865 /** dri3_get_buffer
1866  *
1867  * Find a front or back buffer, allocating new ones as necessary
1868  */
1869 static struct loader_dri3_buffer *
dri3_get_buffer(__DRIdrawable * driDrawable,unsigned int format,enum loader_dri3_buffer_type buffer_type,struct loader_dri3_drawable * draw)1870 dri3_get_buffer(__DRIdrawable *driDrawable,
1871                 unsigned int format,
1872                 enum loader_dri3_buffer_type buffer_type,
1873                 struct loader_dri3_drawable *draw)
1874 {
1875    struct loader_dri3_buffer *buffer;
1876    bool fence_await = buffer_type == loader_dri3_buffer_back;
1877    int buf_id;
1878 
1879    if (buffer_type == loader_dri3_buffer_back) {
1880       draw->back_format = format;
1881 
1882       buf_id = dri3_find_back(draw);
1883 
1884       if (buf_id < 0)
1885          return NULL;
1886    } else {
1887       buf_id = LOADER_DRI3_FRONT_ID;
1888    }
1889 
1890    buffer = draw->buffers[buf_id];
1891 
1892    /* Allocate a new buffer if there isn't an old one, if that
1893     * old one is the wrong size, or if it's suboptimal
1894     */
1895    if (!buffer || buffer->width != draw->width ||
1896        buffer->height != draw->height ||
1897        buffer->reallocate) {
1898       struct loader_dri3_buffer *new_buffer;
1899 
1900       /* Allocate the new buffers
1901        */
1902       new_buffer = dri3_alloc_render_buffer(draw,
1903                                                    format,
1904                                                    draw->width,
1905                                                    draw->height,
1906                                                    draw->depth);
1907       if (!new_buffer)
1908          return NULL;
1909 
1910       /* When resizing, copy the contents of the old buffer, waiting for that
1911        * copy to complete using our fences before proceeding
1912        */
1913       if ((buffer_type == loader_dri3_buffer_back ||
1914            (buffer_type == loader_dri3_buffer_front && draw->have_fake_front))
1915           && buffer) {
1916 
1917          /* Fill the new buffer with data from an old buffer */
1918          if (!loader_dri3_blit_image(draw,
1919                                      new_buffer->image,
1920                                      buffer->image,
1921                                      0, 0,
1922                                      MIN2(buffer->width, new_buffer->width),
1923                                      MIN2(buffer->height, new_buffer->height),
1924                                      0, 0, 0) &&
1925              !buffer->linear_buffer) {
1926             dri3_fence_reset(draw->conn, new_buffer);
1927             dri3_copy_area(draw->conn,
1928                            buffer->pixmap,
1929                            new_buffer->pixmap,
1930                            dri3_drawable_gc(draw),
1931                            0, 0, 0, 0,
1932                            draw->width, draw->height);
1933             dri3_fence_trigger(draw->conn, new_buffer);
1934             fence_await = true;
1935          }
1936          dri3_free_render_buffer(draw, buffer);
1937       } else if (buffer_type == loader_dri3_buffer_front) {
1938          /* Fill the new fake front with data from a real front */
1939          loader_dri3_swapbuffer_barrier(draw);
1940          dri3_fence_reset(draw->conn, new_buffer);
1941          dri3_copy_area(draw->conn,
1942                         draw->drawable,
1943                         new_buffer->pixmap,
1944                         dri3_drawable_gc(draw),
1945                         0, 0, 0, 0,
1946                         draw->width, draw->height);
1947          dri3_fence_trigger(draw->conn, new_buffer);
1948 
1949          if (new_buffer->linear_buffer) {
1950             dri3_fence_await(draw->conn, draw, new_buffer);
1951             (void) loader_dri3_blit_image(draw,
1952                                           new_buffer->image,
1953                                           new_buffer->linear_buffer,
1954                                           0, 0, draw->width, draw->height,
1955                                           0, 0, 0);
1956          } else
1957             fence_await = true;
1958       }
1959       buffer = new_buffer;
1960       draw->buffers[buf_id] = buffer;
1961    }
1962 
1963    if (fence_await)
1964       dri3_fence_await(draw->conn, draw, buffer);
1965 
1966    /*
1967     * Do we need to preserve the content of a previous buffer?
1968     *
1969     * Note that this blit is needed only to avoid a wait for a buffer that
1970     * is currently in the flip chain or being scanned out from. That's really
1971     * a tradeoff. If we're ok with the wait we can reduce the number of back
1972     * buffers to 1 for SWAP_EXCHANGE, and 1 for SWAP_COPY,
1973     * but in the latter case we must disallow page-flipping.
1974     */
1975    if (buffer_type == loader_dri3_buffer_back &&
1976        draw->cur_blit_source != -1 &&
1977        draw->buffers[draw->cur_blit_source] &&
1978        buffer != draw->buffers[draw->cur_blit_source]) {
1979 
1980       struct loader_dri3_buffer *source = draw->buffers[draw->cur_blit_source];
1981 
1982       /* Avoid flushing here. Will propably do good for tiling hardware. */
1983       (void) loader_dri3_blit_image(draw,
1984                                     buffer->image,
1985                                     source->image,
1986                                     0, 0, draw->width, draw->height,
1987                                     0, 0, 0);
1988       buffer->last_swap = source->last_swap;
1989       draw->cur_blit_source = -1;
1990    }
1991    /* Return the requested buffer */
1992    return buffer;
1993 }
1994 
1995 /** dri3_free_buffers
1996  *
1997  * Free the front bufffer or all of the back buffers. Used
1998  * when the application changes which buffers it needs
1999  */
2000 static void
dri3_free_buffers(__DRIdrawable * driDrawable,enum loader_dri3_buffer_type buffer_type,struct loader_dri3_drawable * draw)2001 dri3_free_buffers(__DRIdrawable *driDrawable,
2002                   enum loader_dri3_buffer_type buffer_type,
2003                   struct loader_dri3_drawable *draw)
2004 {
2005    struct loader_dri3_buffer *buffer;
2006    int first_id;
2007    int n_id;
2008    int buf_id;
2009 
2010    switch (buffer_type) {
2011    case loader_dri3_buffer_back:
2012       first_id = LOADER_DRI3_BACK_ID(0);
2013       n_id = LOADER_DRI3_MAX_BACK;
2014       draw->cur_blit_source = -1;
2015       break;
2016    case loader_dri3_buffer_front:
2017       first_id = LOADER_DRI3_FRONT_ID;
2018       /* Don't free a fake front holding new backbuffer content. */
2019       n_id = (draw->cur_blit_source == LOADER_DRI3_FRONT_ID) ? 0 : 1;
2020    }
2021 
2022    for (buf_id = first_id; buf_id < first_id + n_id; buf_id++) {
2023       buffer = draw->buffers[buf_id];
2024       if (buffer) {
2025          dri3_free_render_buffer(draw, buffer);
2026          draw->buffers[buf_id] = NULL;
2027       }
2028    }
2029 }
2030 
2031 /** loader_dri3_get_buffers
2032  *
2033  * The published buffer allocation API.
2034  * Returns all of the necessary buffers, allocating
2035  * as needed.
2036  */
2037 int
loader_dri3_get_buffers(__DRIdrawable * driDrawable,unsigned int format,uint32_t * stamp,void * loaderPrivate,uint32_t buffer_mask,struct __DRIimageList * buffers)2038 loader_dri3_get_buffers(__DRIdrawable *driDrawable,
2039                         unsigned int format,
2040                         uint32_t *stamp,
2041                         void *loaderPrivate,
2042                         uint32_t buffer_mask,
2043                         struct __DRIimageList *buffers)
2044 {
2045    struct loader_dri3_drawable *draw = loaderPrivate;
2046    struct loader_dri3_buffer   *front, *back;
2047    int buf_id;
2048 
2049    buffers->image_mask = 0;
2050    buffers->front = NULL;
2051    buffers->back = NULL;
2052 
2053    front = NULL;
2054    back = NULL;
2055 
2056    if (!dri3_update_drawable(draw))
2057       return false;
2058 
2059    dri3_update_max_num_back(draw);
2060 
2061    /* Free no longer needed back buffers */
2062    for (buf_id = draw->cur_num_back; buf_id < LOADER_DRI3_MAX_BACK; buf_id++) {
2063       if (draw->cur_blit_source != buf_id && draw->buffers[buf_id]) {
2064          dri3_free_render_buffer(draw, draw->buffers[buf_id]);
2065          draw->buffers[buf_id] = NULL;
2066       }
2067    }
2068 
2069    /* pixmaps always have front buffers.
2070     * Exchange swaps also mandate fake front buffers.
2071     */
2072    if (draw->is_pixmap || draw->swap_method == __DRI_ATTRIB_SWAP_EXCHANGE)
2073       buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
2074 
2075    if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
2076       /* All pixmaps are owned by the server gpu.
2077        * When we use a different gpu, we can't use the pixmap
2078        * as buffer since it is potentially tiled a way
2079        * our device can't understand. In this case, use
2080        * a fake front buffer. Hopefully the pixmap
2081        * content will get synced with the fake front
2082        * buffer.
2083        */
2084       if (draw->is_pixmap && !draw->is_different_gpu)
2085          front = dri3_get_pixmap_buffer(driDrawable,
2086                                                format,
2087                                                loader_dri3_buffer_front,
2088                                                draw);
2089       else
2090          front = dri3_get_buffer(driDrawable,
2091                                         format,
2092                                         loader_dri3_buffer_front,
2093                                         draw);
2094 
2095       if (!front)
2096          return false;
2097    } else {
2098       dri3_free_buffers(driDrawable, loader_dri3_buffer_front, draw);
2099       draw->have_fake_front = 0;
2100    }
2101 
2102    if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
2103       back = dri3_get_buffer(driDrawable,
2104                                     format,
2105                                     loader_dri3_buffer_back,
2106                                     draw);
2107       if (!back)
2108          return false;
2109       draw->have_back = 1;
2110    } else {
2111       dri3_free_buffers(driDrawable, loader_dri3_buffer_back, draw);
2112       draw->have_back = 0;
2113    }
2114 
2115    if (front) {
2116       buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
2117       buffers->front = front->image;
2118       draw->have_fake_front = draw->is_different_gpu || !draw->is_pixmap;
2119    }
2120 
2121    if (back) {
2122       buffers->image_mask |= __DRI_IMAGE_BUFFER_BACK;
2123       buffers->back = back->image;
2124    }
2125 
2126    draw->stamp = stamp;
2127 
2128    return true;
2129 }
2130 
2131 /** loader_dri3_update_drawable_geometry
2132  *
2133  * Get the current drawable geometry.
2134  */
2135 void
loader_dri3_update_drawable_geometry(struct loader_dri3_drawable * draw)2136 loader_dri3_update_drawable_geometry(struct loader_dri3_drawable *draw)
2137 {
2138    xcb_get_geometry_cookie_t geom_cookie;
2139    xcb_get_geometry_reply_t *geom_reply;
2140 
2141    geom_cookie = xcb_get_geometry(draw->conn, draw->drawable);
2142 
2143    geom_reply = xcb_get_geometry_reply(draw->conn, geom_cookie, NULL);
2144 
2145    if (geom_reply) {
2146       draw->width = geom_reply->width;
2147       draw->height = geom_reply->height;
2148       draw->vtable->set_drawable_size(draw, draw->width, draw->height);
2149       draw->ext->flush->invalidate(draw->dri_drawable);
2150 
2151       free(geom_reply);
2152    }
2153 }
2154 
2155 
2156 /**
2157  * Make sure the server has flushed all pending swap buffers to hardware
2158  * for this drawable. Ideally we'd want to send an X protocol request to
2159  * have the server block our connection until the swaps are complete. That
2160  * would avoid the potential round-trip here.
2161  */
2162 void
loader_dri3_swapbuffer_barrier(struct loader_dri3_drawable * draw)2163 loader_dri3_swapbuffer_barrier(struct loader_dri3_drawable *draw)
2164 {
2165    int64_t ust, msc, sbc;
2166 
2167    (void) loader_dri3_wait_for_sbc(draw, 0, &ust, &msc, &sbc);
2168 }
2169 
2170 /**
2171  * Perform any cleanup associated with a close screen operation.
2172  * \param dri_screen[in,out] Pointer to __DRIscreen about to be closed.
2173  *
2174  * This function destroys the screen's cached swap context if any.
2175  */
2176 void
loader_dri3_close_screen(__DRIscreen * dri_screen)2177 loader_dri3_close_screen(__DRIscreen *dri_screen)
2178 {
2179    mtx_lock(&blit_context.mtx);
2180    if (blit_context.ctx && blit_context.cur_screen == dri_screen) {
2181       blit_context.core->destroyContext(blit_context.ctx);
2182       blit_context.ctx = NULL;
2183    }
2184    mtx_unlock(&blit_context.mtx);
2185 }
2186 
2187 /**
2188  * Find a backbuffer slot - potentially allocating a back buffer
2189  *
2190  * \param draw[in,out]  Pointer to the drawable for which to find back.
2191  * \return Pointer to a new back buffer or NULL if allocation failed or was
2192  * not mandated.
2193  *
2194  * Find a potentially new back buffer, and if it's not been allocated yet and
2195  * in addition needs initializing, then try to allocate and initialize it.
2196  */
2197 #include <stdio.h>
2198 static struct loader_dri3_buffer *
dri3_find_back_alloc(struct loader_dri3_drawable * draw)2199 dri3_find_back_alloc(struct loader_dri3_drawable *draw)
2200 {
2201    struct loader_dri3_buffer *back;
2202    int id;
2203 
2204    id = dri3_find_back(draw);
2205    if (id < 0)
2206       return NULL;
2207 
2208    back = draw->buffers[id];
2209    /* Allocate a new back if we haven't got one */
2210    if (!back && draw->back_format != __DRI_IMAGE_FORMAT_NONE &&
2211        dri3_update_drawable(draw))
2212       back = dri3_alloc_render_buffer(draw, draw->back_format,
2213                                       draw->width, draw->height, draw->depth);
2214 
2215    if (!back)
2216       return NULL;
2217 
2218    draw->buffers[id] = back;
2219 
2220    /* If necessary, prefill the back with data according to swap_method mode. */
2221    if (draw->cur_blit_source != -1 &&
2222        draw->buffers[draw->cur_blit_source] &&
2223        back != draw->buffers[draw->cur_blit_source]) {
2224       struct loader_dri3_buffer *source = draw->buffers[draw->cur_blit_source];
2225 
2226       dri3_fence_await(draw->conn, draw, source);
2227       dri3_fence_await(draw->conn, draw, back);
2228       (void) loader_dri3_blit_image(draw,
2229                                     back->image,
2230                                     source->image,
2231                                     0, 0, draw->width, draw->height,
2232                                     0, 0, 0);
2233       back->last_swap = source->last_swap;
2234       draw->cur_blit_source = -1;
2235    }
2236 
2237    return back;
2238 }
2239