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