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