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