dri: add __DRI_IMAGE_FORMAT_SXRGB8
[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-uapi/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, strlen(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 = 0;
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_COPYSUBBUFFER);
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, __DRI2_THROTTLE_COPYSUBBUFFER);
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 case __DRI_IMAGE_FORMAT_SXRGB8:
1123 return 4;
1124 case __DRI_IMAGE_FORMAT_XBGR16161616F:
1125 case __DRI_IMAGE_FORMAT_ABGR16161616F:
1126 return 8;
1127 case __DRI_IMAGE_FORMAT_NONE:
1128 default:
1129 return 0;
1130 }
1131 }
1132
1133 /* Map format of render buffer to corresponding format for the linear_buffer
1134 * used for sharing with the display gpu of a Prime setup (== is_different_gpu).
1135 * Usually linear_format == format, except for depth >= 30 formats, where
1136 * different gpu vendors have different preferences wrt. color channel ordering.
1137 */
1138 static uint32_t
1139 dri3_linear_format_for_format(struct loader_dri3_drawable *draw, uint32_t format)
1140 {
1141 switch (format) {
1142 case __DRI_IMAGE_FORMAT_XRGB2101010:
1143 case __DRI_IMAGE_FORMAT_XBGR2101010:
1144 /* Different preferred formats for different hw */
1145 if (dri3_get_red_mask_for_depth(draw, 30) == 0x3ff)
1146 return __DRI_IMAGE_FORMAT_XBGR2101010;
1147 else
1148 return __DRI_IMAGE_FORMAT_XRGB2101010;
1149
1150 case __DRI_IMAGE_FORMAT_ARGB2101010:
1151 case __DRI_IMAGE_FORMAT_ABGR2101010:
1152 /* Different preferred formats for different hw */
1153 if (dri3_get_red_mask_for_depth(draw, 30) == 0x3ff)
1154 return __DRI_IMAGE_FORMAT_ABGR2101010;
1155 else
1156 return __DRI_IMAGE_FORMAT_ARGB2101010;
1157
1158 default:
1159 return format;
1160 }
1161 }
1162
1163 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
1164 * the createImageFromFds call takes DRM_FORMAT codes. To avoid
1165 * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
1166 * translate to DRM_FORMAT codes in the call to createImageFromFds
1167 */
1168 static int
1169 image_format_to_fourcc(int format)
1170 {
1171
1172 /* Convert from __DRI_IMAGE_FORMAT to DRM_FORMAT (sigh) */
1173 switch (format) {
1174 case __DRI_IMAGE_FORMAT_SARGB8: return __DRI_IMAGE_FOURCC_SARGB8888;
1175 case __DRI_IMAGE_FORMAT_SABGR8: return __DRI_IMAGE_FOURCC_SABGR8888;
1176 case __DRI_IMAGE_FORMAT_SXRGB8: return __DRI_IMAGE_FOURCC_SXRGB8888;
1177 case __DRI_IMAGE_FORMAT_RGB565: return DRM_FORMAT_RGB565;
1178 case __DRI_IMAGE_FORMAT_XRGB8888: return DRM_FORMAT_XRGB8888;
1179 case __DRI_IMAGE_FORMAT_ARGB8888: return DRM_FORMAT_ARGB8888;
1180 case __DRI_IMAGE_FORMAT_ABGR8888: return DRM_FORMAT_ABGR8888;
1181 case __DRI_IMAGE_FORMAT_XBGR8888: return DRM_FORMAT_XBGR8888;
1182 case __DRI_IMAGE_FORMAT_XRGB2101010: return DRM_FORMAT_XRGB2101010;
1183 case __DRI_IMAGE_FORMAT_ARGB2101010: return DRM_FORMAT_ARGB2101010;
1184 case __DRI_IMAGE_FORMAT_XBGR2101010: return DRM_FORMAT_XBGR2101010;
1185 case __DRI_IMAGE_FORMAT_ABGR2101010: return DRM_FORMAT_ABGR2101010;
1186 case __DRI_IMAGE_FORMAT_XBGR16161616F: return DRM_FORMAT_XBGR16161616F;
1187 case __DRI_IMAGE_FORMAT_ABGR16161616F: return DRM_FORMAT_ABGR16161616F;
1188 }
1189 return 0;
1190 }
1191
1192 #ifdef HAVE_DRI3_MODIFIERS
1193 static bool
1194 has_supported_modifier(struct loader_dri3_drawable *draw, unsigned int format,
1195 uint64_t *modifiers, uint32_t count)
1196 {
1197 uint64_t *supported_modifiers;
1198 int32_t supported_modifiers_count;
1199 bool found = false;
1200 int i, j;
1201
1202 if (!draw->ext->image->queryDmaBufModifiers(draw->dri_screen,
1203 format, 0, NULL, NULL,
1204 &supported_modifiers_count) ||
1205 supported_modifiers_count == 0)
1206 return false;
1207
1208 supported_modifiers = malloc(supported_modifiers_count * sizeof(uint64_t));
1209 if (!supported_modifiers)
1210 return false;
1211
1212 draw->ext->image->queryDmaBufModifiers(draw->dri_screen, format,
1213 supported_modifiers_count,
1214 supported_modifiers, NULL,
1215 &supported_modifiers_count);
1216
1217 for (i = 0; !found && i < supported_modifiers_count; i++) {
1218 for (j = 0; !found && j < count; j++) {
1219 if (supported_modifiers[i] == modifiers[j])
1220 found = true;
1221 }
1222 }
1223
1224 free(supported_modifiers);
1225 return found;
1226 }
1227 #endif
1228
1229 /** loader_dri3_alloc_render_buffer
1230 *
1231 * Use the driver createImage function to construct a __DRIimage, then
1232 * get a file descriptor for that and create an X pixmap from that
1233 *
1234 * Allocate an xshmfence for synchronization
1235 */
1236 static struct loader_dri3_buffer *
1237 dri3_alloc_render_buffer(struct loader_dri3_drawable *draw, unsigned int format,
1238 int width, int height, int depth)
1239 {
1240 struct loader_dri3_buffer *buffer;
1241 __DRIimage *pixmap_buffer;
1242 xcb_pixmap_t pixmap;
1243 xcb_sync_fence_t sync_fence;
1244 struct xshmfence *shm_fence;
1245 int buffer_fds[4], fence_fd;
1246 int num_planes = 0;
1247 int i, mod;
1248 int ret;
1249
1250 /* Create an xshmfence object and
1251 * prepare to send that to the X server
1252 */
1253
1254 fence_fd = xshmfence_alloc_shm();
1255 if (fence_fd < 0)
1256 return NULL;
1257
1258 shm_fence = xshmfence_map_shm(fence_fd);
1259 if (shm_fence == NULL)
1260 goto no_shm_fence;
1261
1262 /* Allocate the image from the driver
1263 */
1264 buffer = calloc(1, sizeof *buffer);
1265 if (!buffer)
1266 goto no_buffer;
1267
1268 buffer->cpp = dri3_cpp_for_format(format);
1269 if (!buffer->cpp)
1270 goto no_image;
1271
1272 if (!draw->is_different_gpu) {
1273 #ifdef HAVE_DRI3_MODIFIERS
1274 if (draw->multiplanes_available &&
1275 draw->ext->image->base.version >= 15 &&
1276 draw->ext->image->queryDmaBufModifiers &&
1277 draw->ext->image->createImageWithModifiers) {
1278 xcb_dri3_get_supported_modifiers_cookie_t mod_cookie;
1279 xcb_dri3_get_supported_modifiers_reply_t *mod_reply;
1280 xcb_generic_error_t *error = NULL;
1281 uint64_t *modifiers = NULL;
1282 uint32_t count = 0;
1283
1284 mod_cookie = xcb_dri3_get_supported_modifiers(draw->conn,
1285 draw->window,
1286 depth, buffer->cpp * 8);
1287 mod_reply = xcb_dri3_get_supported_modifiers_reply(draw->conn,
1288 mod_cookie,
1289 &error);
1290 if (!mod_reply)
1291 goto no_image;
1292
1293 if (mod_reply->num_window_modifiers) {
1294 count = mod_reply->num_window_modifiers;
1295 modifiers = malloc(count * sizeof(uint64_t));
1296 if (!modifiers) {
1297 free(mod_reply);
1298 goto no_image;
1299 }
1300
1301 memcpy(modifiers,
1302 xcb_dri3_get_supported_modifiers_window_modifiers(mod_reply),
1303 count * sizeof(uint64_t));
1304
1305 if (!has_supported_modifier(draw, image_format_to_fourcc(format),
1306 modifiers, count)) {
1307 free(modifiers);
1308 count = 0;
1309 modifiers = NULL;
1310 }
1311 }
1312
1313 if (mod_reply->num_screen_modifiers && modifiers == NULL) {
1314 count = mod_reply->num_screen_modifiers;
1315 modifiers = malloc(count * sizeof(uint64_t));
1316 if (!modifiers) {
1317 free(modifiers);
1318 free(mod_reply);
1319 goto no_image;
1320 }
1321
1322 memcpy(modifiers,
1323 xcb_dri3_get_supported_modifiers_screen_modifiers(mod_reply),
1324 count * sizeof(uint64_t));
1325 }
1326
1327 free(mod_reply);
1328
1329 /* don't use createImageWithModifiers() if we have no
1330 * modifiers, other things depend on the use flags when
1331 * there are no modifiers to know that a buffer can be
1332 * shared.
1333 */
1334 if (modifiers) {
1335 buffer->image = draw->ext->image->createImageWithModifiers(draw->dri_screen,
1336 width, height,
1337 format,
1338 modifiers,
1339 count,
1340 buffer);
1341 }
1342
1343 free(modifiers);
1344 }
1345 #endif
1346 if (!buffer->image)
1347 buffer->image = draw->ext->image->createImage(draw->dri_screen,
1348 width, height,
1349 format,
1350 __DRI_IMAGE_USE_SHARE |
1351 __DRI_IMAGE_USE_SCANOUT |
1352 __DRI_IMAGE_USE_BACKBUFFER,
1353 buffer);
1354
1355 pixmap_buffer = buffer->image;
1356
1357 if (!buffer->image)
1358 goto no_image;
1359 } else {
1360 buffer->image = draw->ext->image->createImage(draw->dri_screen,
1361 width, height,
1362 format,
1363 0,
1364 buffer);
1365
1366 if (!buffer->image)
1367 goto no_image;
1368
1369 buffer->linear_buffer =
1370 draw->ext->image->createImage(draw->dri_screen,
1371 width, height,
1372 dri3_linear_format_for_format(draw, format),
1373 __DRI_IMAGE_USE_SHARE |
1374 __DRI_IMAGE_USE_LINEAR |
1375 __DRI_IMAGE_USE_BACKBUFFER,
1376 buffer);
1377 pixmap_buffer = buffer->linear_buffer;
1378
1379 if (!buffer->linear_buffer)
1380 goto no_linear_buffer;
1381 }
1382
1383 /* X want some information about the planes, so ask the image for it
1384 */
1385 if (!draw->ext->image->queryImage(pixmap_buffer, __DRI_IMAGE_ATTRIB_NUM_PLANES,
1386 &num_planes))
1387 num_planes = 1;
1388
1389 for (i = 0; i < num_planes; i++) {
1390 __DRIimage *image = draw->ext->image->fromPlanar(pixmap_buffer, i, NULL);
1391
1392 if (!image) {
1393 assert(i == 0);
1394 image = pixmap_buffer;
1395 }
1396
1397 ret = draw->ext->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD,
1398 &buffer_fds[i]);
1399 ret &= draw->ext->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE,
1400 &buffer->strides[i]);
1401 ret &= draw->ext->image->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET,
1402 &buffer->offsets[i]);
1403 if (image != pixmap_buffer)
1404 draw->ext->image->destroyImage(image);
1405
1406 if (!ret)
1407 goto no_buffer_attrib;
1408 }
1409
1410 ret = draw->ext->image->queryImage(pixmap_buffer,
1411 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER, &mod);
1412 buffer->modifier = (uint64_t) mod << 32;
1413 ret &= draw->ext->image->queryImage(pixmap_buffer,
1414 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER, &mod);
1415 buffer->modifier |= (uint64_t)(mod & 0xffffffff);
1416
1417 if (!ret)
1418 buffer->modifier = DRM_FORMAT_MOD_INVALID;
1419
1420 pixmap = xcb_generate_id(draw->conn);
1421 #ifdef HAVE_DRI3_MODIFIERS
1422 if (draw->multiplanes_available &&
1423 buffer->modifier != DRM_FORMAT_MOD_INVALID) {
1424 xcb_dri3_pixmap_from_buffers(draw->conn,
1425 pixmap,
1426 draw->window,
1427 num_planes,
1428 width, height,
1429 buffer->strides[0], buffer->offsets[0],
1430 buffer->strides[1], buffer->offsets[1],
1431 buffer->strides[2], buffer->offsets[2],
1432 buffer->strides[3], buffer->offsets[3],
1433 depth, buffer->cpp * 8,
1434 buffer->modifier,
1435 buffer_fds);
1436 } else
1437 #endif
1438 {
1439 xcb_dri3_pixmap_from_buffer(draw->conn,
1440 pixmap,
1441 draw->drawable,
1442 buffer->size,
1443 width, height, buffer->strides[0],
1444 depth, buffer->cpp * 8,
1445 buffer_fds[0]);
1446 }
1447
1448 xcb_dri3_fence_from_fd(draw->conn,
1449 pixmap,
1450 (sync_fence = xcb_generate_id(draw->conn)),
1451 false,
1452 fence_fd);
1453
1454 buffer->pixmap = pixmap;
1455 buffer->own_pixmap = true;
1456 buffer->sync_fence = sync_fence;
1457 buffer->shm_fence = shm_fence;
1458 buffer->width = width;
1459 buffer->height = height;
1460
1461 /* Mark the buffer as idle
1462 */
1463 dri3_fence_set(buffer);
1464
1465 return buffer;
1466
1467 no_buffer_attrib:
1468 do {
1469 close(buffer_fds[i]);
1470 } while (--i >= 0);
1471 draw->ext->image->destroyImage(pixmap_buffer);
1472 no_linear_buffer:
1473 if (draw->is_different_gpu)
1474 draw->ext->image->destroyImage(buffer->image);
1475 no_image:
1476 free(buffer);
1477 no_buffer:
1478 xshmfence_unmap_shm(shm_fence);
1479 no_shm_fence:
1480 close(fence_fd);
1481 return NULL;
1482 }
1483
1484 /** loader_dri3_update_drawable
1485 *
1486 * Called the first time we use the drawable and then
1487 * after we receive present configure notify events to
1488 * track the geometry of the drawable
1489 */
1490 static int
1491 dri3_update_drawable(struct loader_dri3_drawable *draw)
1492 {
1493 mtx_lock(&draw->mtx);
1494 if (draw->first_init) {
1495 xcb_get_geometry_cookie_t geom_cookie;
1496 xcb_get_geometry_reply_t *geom_reply;
1497 xcb_void_cookie_t cookie;
1498 xcb_generic_error_t *error;
1499 xcb_present_query_capabilities_cookie_t present_capabilities_cookie;
1500 xcb_present_query_capabilities_reply_t *present_capabilities_reply;
1501 xcb_window_t root_win;
1502
1503 draw->first_init = false;
1504
1505 /* Try to select for input on the window.
1506 *
1507 * If the drawable is a window, this will get our events
1508 * delivered.
1509 *
1510 * Otherwise, we'll get a BadWindow error back from this request which
1511 * will let us know that the drawable is a pixmap instead.
1512 */
1513
1514 draw->eid = xcb_generate_id(draw->conn);
1515 cookie =
1516 xcb_present_select_input_checked(draw->conn, draw->eid, draw->drawable,
1517 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
1518 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
1519 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
1520
1521 present_capabilities_cookie =
1522 xcb_present_query_capabilities(draw->conn, draw->drawable);
1523
1524 /* Create an XCB event queue to hold present events outside of the usual
1525 * application event queue
1526 */
1527 draw->special_event = xcb_register_for_special_xge(draw->conn,
1528 &xcb_present_id,
1529 draw->eid,
1530 draw->stamp);
1531 geom_cookie = xcb_get_geometry(draw->conn, draw->drawable);
1532
1533 geom_reply = xcb_get_geometry_reply(draw->conn, geom_cookie, NULL);
1534
1535 if (!geom_reply) {
1536 mtx_unlock(&draw->mtx);
1537 return false;
1538 }
1539 draw->width = geom_reply->width;
1540 draw->height = geom_reply->height;
1541 draw->depth = geom_reply->depth;
1542 draw->vtable->set_drawable_size(draw, draw->width, draw->height);
1543 root_win = geom_reply->root;
1544
1545 free(geom_reply);
1546
1547 draw->is_pixmap = false;
1548
1549 /* Check to see if our select input call failed. If it failed with a
1550 * BadWindow error, then assume the drawable is a pixmap. Destroy the
1551 * special event queue created above and mark the drawable as a pixmap
1552 */
1553
1554 error = xcb_request_check(draw->conn, cookie);
1555
1556 present_capabilities_reply =
1557 xcb_present_query_capabilities_reply(draw->conn,
1558 present_capabilities_cookie,
1559 NULL);
1560
1561 if (present_capabilities_reply) {
1562 draw->present_capabilities = present_capabilities_reply->capabilities;
1563 free(present_capabilities_reply);
1564 } else
1565 draw->present_capabilities = 0;
1566
1567 if (error) {
1568 if (error->error_code != BadWindow) {
1569 free(error);
1570 mtx_unlock(&draw->mtx);
1571 return false;
1572 }
1573 free(error);
1574 draw->is_pixmap = true;
1575 xcb_unregister_for_special_event(draw->conn, draw->special_event);
1576 draw->special_event = NULL;
1577 }
1578
1579 if (draw->is_pixmap)
1580 draw->window = root_win;
1581 else
1582 draw->window = draw->drawable;
1583 }
1584 dri3_flush_present_events(draw);
1585 mtx_unlock(&draw->mtx);
1586 return true;
1587 }
1588
1589 __DRIimage *
1590 loader_dri3_create_image(xcb_connection_t *c,
1591 xcb_dri3_buffer_from_pixmap_reply_t *bp_reply,
1592 unsigned int format,
1593 __DRIscreen *dri_screen,
1594 const __DRIimageExtension *image,
1595 void *loaderPrivate)
1596 {
1597 int *fds;
1598 __DRIimage *image_planar, *ret;
1599 int stride, offset;
1600
1601 /* Get an FD for the pixmap object
1602 */
1603 fds = xcb_dri3_buffer_from_pixmap_reply_fds(c, bp_reply);
1604
1605 stride = bp_reply->stride;
1606 offset = 0;
1607
1608 /* createImageFromFds creates a wrapper __DRIimage structure which
1609 * can deal with multiple planes for things like Yuv images. So, once
1610 * we've gotten the planar wrapper, pull the single plane out of it and
1611 * discard the wrapper.
1612 */
1613 image_planar = image->createImageFromFds(dri_screen,
1614 bp_reply->width,
1615 bp_reply->height,
1616 image_format_to_fourcc(format),
1617 fds, 1,
1618 &stride, &offset, loaderPrivate);
1619 close(fds[0]);
1620 if (!image_planar)
1621 return NULL;
1622
1623 ret = image->fromPlanar(image_planar, 0, loaderPrivate);
1624
1625 if (!ret)
1626 ret = image_planar;
1627 else
1628 image->destroyImage(image_planar);
1629
1630 return ret;
1631 }
1632
1633 #ifdef HAVE_DRI3_MODIFIERS
1634 __DRIimage *
1635 loader_dri3_create_image_from_buffers(xcb_connection_t *c,
1636 xcb_dri3_buffers_from_pixmap_reply_t *bp_reply,
1637 unsigned int format,
1638 __DRIscreen *dri_screen,
1639 const __DRIimageExtension *image,
1640 void *loaderPrivate)
1641 {
1642 __DRIimage *ret;
1643 int *fds;
1644 uint32_t *strides_in, *offsets_in;
1645 int strides[4], offsets[4];
1646 unsigned error;
1647 int i;
1648
1649 if (bp_reply->nfd > 4)
1650 return NULL;
1651
1652 fds = xcb_dri3_buffers_from_pixmap_reply_fds(c, bp_reply);
1653 strides_in = xcb_dri3_buffers_from_pixmap_strides(bp_reply);
1654 offsets_in = xcb_dri3_buffers_from_pixmap_offsets(bp_reply);
1655 for (i = 0; i < bp_reply->nfd; i++) {
1656 strides[i] = strides_in[i];
1657 offsets[i] = offsets_in[i];
1658 }
1659
1660 ret = image->createImageFromDmaBufs2(dri_screen,
1661 bp_reply->width,
1662 bp_reply->height,
1663 image_format_to_fourcc(format),
1664 bp_reply->modifier,
1665 fds, bp_reply->nfd,
1666 strides, offsets,
1667 0, 0, 0, 0, /* UNDEFINED */
1668 &error, loaderPrivate);
1669
1670 for (i = 0; i < bp_reply->nfd; i++)
1671 close(fds[i]);
1672
1673 return ret;
1674 }
1675 #endif
1676
1677 /** dri3_get_pixmap_buffer
1678 *
1679 * Get the DRM object for a pixmap from the X server and
1680 * wrap that with a __DRIimage structure using createImageFromFds
1681 */
1682 static struct loader_dri3_buffer *
1683 dri3_get_pixmap_buffer(__DRIdrawable *driDrawable, unsigned int format,
1684 enum loader_dri3_buffer_type buffer_type,
1685 struct loader_dri3_drawable *draw)
1686 {
1687 int buf_id = loader_dri3_pixmap_buf_id(buffer_type);
1688 struct loader_dri3_buffer *buffer = draw->buffers[buf_id];
1689 xcb_drawable_t pixmap;
1690 xcb_sync_fence_t sync_fence;
1691 struct xshmfence *shm_fence;
1692 int width;
1693 int height;
1694 int fence_fd;
1695 __DRIscreen *cur_screen;
1696
1697 if (buffer)
1698 return buffer;
1699
1700 pixmap = draw->drawable;
1701
1702 buffer = calloc(1, sizeof *buffer);
1703 if (!buffer)
1704 goto no_buffer;
1705
1706 fence_fd = xshmfence_alloc_shm();
1707 if (fence_fd < 0)
1708 goto no_fence;
1709 shm_fence = xshmfence_map_shm(fence_fd);
1710 if (shm_fence == NULL) {
1711 close (fence_fd);
1712 goto no_fence;
1713 }
1714
1715 /* Get the currently-bound screen or revert to using the drawable's screen if
1716 * no contexts are currently bound. The latter case is at least necessary for
1717 * obs-studio, when using Window Capture (Xcomposite) as a Source.
1718 */
1719 cur_screen = draw->vtable->get_dri_screen();
1720 if (!cur_screen) {
1721 cur_screen = draw->dri_screen;
1722 }
1723
1724 xcb_dri3_fence_from_fd(draw->conn,
1725 pixmap,
1726 (sync_fence = xcb_generate_id(draw->conn)),
1727 false,
1728 fence_fd);
1729 #ifdef HAVE_DRI3_MODIFIERS
1730 if (draw->multiplanes_available &&
1731 draw->ext->image->base.version >= 15 &&
1732 draw->ext->image->createImageFromDmaBufs2) {
1733 xcb_dri3_buffers_from_pixmap_cookie_t bps_cookie;
1734 xcb_dri3_buffers_from_pixmap_reply_t *bps_reply;
1735
1736 bps_cookie = xcb_dri3_buffers_from_pixmap(draw->conn, pixmap);
1737 bps_reply = xcb_dri3_buffers_from_pixmap_reply(draw->conn, bps_cookie,
1738 NULL);
1739 if (!bps_reply)
1740 goto no_image;
1741 buffer->image =
1742 loader_dri3_create_image_from_buffers(draw->conn, bps_reply, format,
1743 cur_screen, draw->ext->image,
1744 buffer);
1745 width = bps_reply->width;
1746 height = bps_reply->height;
1747 free(bps_reply);
1748 } else
1749 #endif
1750 {
1751 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie;
1752 xcb_dri3_buffer_from_pixmap_reply_t *bp_reply;
1753
1754 bp_cookie = xcb_dri3_buffer_from_pixmap(draw->conn, pixmap);
1755 bp_reply = xcb_dri3_buffer_from_pixmap_reply(draw->conn, bp_cookie, NULL);
1756 if (!bp_reply)
1757 goto no_image;
1758
1759 buffer->image = loader_dri3_create_image(draw->conn, bp_reply, format,
1760 cur_screen, draw->ext->image,
1761 buffer);
1762 width = bp_reply->width;
1763 height = bp_reply->height;
1764 free(bp_reply);
1765 }
1766
1767 if (!buffer->image)
1768 goto no_image;
1769
1770 buffer->pixmap = pixmap;
1771 buffer->own_pixmap = false;
1772 buffer->width = width;
1773 buffer->height = height;
1774 buffer->shm_fence = shm_fence;
1775 buffer->sync_fence = sync_fence;
1776
1777 draw->buffers[buf_id] = buffer;
1778
1779 return buffer;
1780
1781 no_image:
1782 xcb_sync_destroy_fence(draw->conn, sync_fence);
1783 xshmfence_unmap_shm(shm_fence);
1784 no_fence:
1785 free(buffer);
1786 no_buffer:
1787 return NULL;
1788 }
1789
1790 /** dri3_get_buffer
1791 *
1792 * Find a front or back buffer, allocating new ones as necessary
1793 */
1794 static struct loader_dri3_buffer *
1795 dri3_get_buffer(__DRIdrawable *driDrawable,
1796 unsigned int format,
1797 enum loader_dri3_buffer_type buffer_type,
1798 struct loader_dri3_drawable *draw)
1799 {
1800 struct loader_dri3_buffer *buffer;
1801 bool fence_await = buffer_type == loader_dri3_buffer_back;
1802 int buf_id;
1803
1804 if (buffer_type == loader_dri3_buffer_back) {
1805 draw->back_format = format;
1806
1807 buf_id = dri3_find_back(draw);
1808
1809 if (buf_id < 0)
1810 return NULL;
1811 } else {
1812 buf_id = LOADER_DRI3_FRONT_ID;
1813 }
1814
1815 buffer = draw->buffers[buf_id];
1816
1817 /* Allocate a new buffer if there isn't an old one, if that
1818 * old one is the wrong size, or if it's suboptimal
1819 */
1820 if (!buffer || buffer->width != draw->width ||
1821 buffer->height != draw->height ||
1822 buffer->reallocate) {
1823 struct loader_dri3_buffer *new_buffer;
1824
1825 /* Allocate the new buffers
1826 */
1827 new_buffer = dri3_alloc_render_buffer(draw,
1828 format,
1829 draw->width,
1830 draw->height,
1831 draw->depth);
1832 if (!new_buffer)
1833 return NULL;
1834
1835 /* When resizing, copy the contents of the old buffer, waiting for that
1836 * copy to complete using our fences before proceeding
1837 */
1838 if ((buffer_type == loader_dri3_buffer_back ||
1839 (buffer_type == loader_dri3_buffer_front && draw->have_fake_front))
1840 && buffer) {
1841
1842 /* Fill the new buffer with data from an old buffer */
1843 if (!loader_dri3_blit_image(draw,
1844 new_buffer->image,
1845 buffer->image,
1846 0, 0,
1847 MIN2(buffer->width, new_buffer->width),
1848 MIN2(buffer->height, new_buffer->height),
1849 0, 0, 0) &&
1850 !buffer->linear_buffer) {
1851 dri3_fence_reset(draw->conn, new_buffer);
1852 dri3_copy_area(draw->conn,
1853 buffer->pixmap,
1854 new_buffer->pixmap,
1855 dri3_drawable_gc(draw),
1856 0, 0, 0, 0,
1857 draw->width, draw->height);
1858 dri3_fence_trigger(draw->conn, new_buffer);
1859 fence_await = true;
1860 }
1861 dri3_free_render_buffer(draw, buffer);
1862 } else if (buffer_type == loader_dri3_buffer_front) {
1863 /* Fill the new fake front with data from a real front */
1864 loader_dri3_swapbuffer_barrier(draw);
1865 dri3_fence_reset(draw->conn, new_buffer);
1866 dri3_copy_area(draw->conn,
1867 draw->drawable,
1868 new_buffer->pixmap,
1869 dri3_drawable_gc(draw),
1870 0, 0, 0, 0,
1871 draw->width, draw->height);
1872 dri3_fence_trigger(draw->conn, new_buffer);
1873
1874 if (new_buffer->linear_buffer) {
1875 dri3_fence_await(draw->conn, draw, new_buffer);
1876 (void) loader_dri3_blit_image(draw,
1877 new_buffer->image,
1878 new_buffer->linear_buffer,
1879 0, 0, draw->width, draw->height,
1880 0, 0, 0);
1881 } else
1882 fence_await = true;
1883 }
1884 buffer = new_buffer;
1885 draw->buffers[buf_id] = buffer;
1886 }
1887
1888 if (fence_await)
1889 dri3_fence_await(draw->conn, draw, buffer);
1890
1891 /*
1892 * Do we need to preserve the content of a previous buffer?
1893 *
1894 * Note that this blit is needed only to avoid a wait for a buffer that
1895 * is currently in the flip chain or being scanned out from. That's really
1896 * a tradeoff. If we're ok with the wait we can reduce the number of back
1897 * buffers to 1 for SWAP_EXCHANGE, and 1 for SWAP_COPY,
1898 * but in the latter case we must disallow page-flipping.
1899 */
1900 if (buffer_type == loader_dri3_buffer_back &&
1901 draw->cur_blit_source != -1 &&
1902 draw->buffers[draw->cur_blit_source] &&
1903 buffer != draw->buffers[draw->cur_blit_source]) {
1904
1905 struct loader_dri3_buffer *source = draw->buffers[draw->cur_blit_source];
1906
1907 /* Avoid flushing here. Will propably do good for tiling hardware. */
1908 (void) loader_dri3_blit_image(draw,
1909 buffer->image,
1910 source->image,
1911 0, 0, draw->width, draw->height,
1912 0, 0, 0);
1913 buffer->last_swap = source->last_swap;
1914 draw->cur_blit_source = -1;
1915 }
1916 /* Return the requested buffer */
1917 return buffer;
1918 }
1919
1920 /** dri3_free_buffers
1921 *
1922 * Free the front bufffer or all of the back buffers. Used
1923 * when the application changes which buffers it needs
1924 */
1925 static void
1926 dri3_free_buffers(__DRIdrawable *driDrawable,
1927 enum loader_dri3_buffer_type buffer_type,
1928 struct loader_dri3_drawable *draw)
1929 {
1930 struct loader_dri3_buffer *buffer;
1931 int first_id;
1932 int n_id;
1933 int buf_id;
1934
1935 switch (buffer_type) {
1936 case loader_dri3_buffer_back:
1937 first_id = LOADER_DRI3_BACK_ID(0);
1938 n_id = LOADER_DRI3_MAX_BACK;
1939 draw->cur_blit_source = -1;
1940 break;
1941 case loader_dri3_buffer_front:
1942 first_id = LOADER_DRI3_FRONT_ID;
1943 /* Don't free a fake front holding new backbuffer content. */
1944 n_id = (draw->cur_blit_source == LOADER_DRI3_FRONT_ID) ? 0 : 1;
1945 }
1946
1947 for (buf_id = first_id; buf_id < first_id + n_id; buf_id++) {
1948 buffer = draw->buffers[buf_id];
1949 if (buffer) {
1950 dri3_free_render_buffer(draw, buffer);
1951 draw->buffers[buf_id] = NULL;
1952 }
1953 }
1954 }
1955
1956 /** loader_dri3_get_buffers
1957 *
1958 * The published buffer allocation API.
1959 * Returns all of the necessary buffers, allocating
1960 * as needed.
1961 */
1962 int
1963 loader_dri3_get_buffers(__DRIdrawable *driDrawable,
1964 unsigned int format,
1965 uint32_t *stamp,
1966 void *loaderPrivate,
1967 uint32_t buffer_mask,
1968 struct __DRIimageList *buffers)
1969 {
1970 struct loader_dri3_drawable *draw = loaderPrivate;
1971 struct loader_dri3_buffer *front, *back;
1972 int buf_id;
1973
1974 buffers->image_mask = 0;
1975 buffers->front = NULL;
1976 buffers->back = NULL;
1977
1978 front = NULL;
1979 back = NULL;
1980
1981 if (!dri3_update_drawable(draw))
1982 return false;
1983
1984 dri3_update_num_back(draw);
1985
1986 /* Free no longer needed back buffers */
1987 for (buf_id = draw->num_back; buf_id < LOADER_DRI3_MAX_BACK; buf_id++) {
1988 if (draw->cur_blit_source != buf_id && draw->buffers[buf_id]) {
1989 dri3_free_render_buffer(draw, draw->buffers[buf_id]);
1990 draw->buffers[buf_id] = NULL;
1991 }
1992 }
1993
1994 /* pixmaps always have front buffers.
1995 * Exchange swaps also mandate fake front buffers.
1996 */
1997 if (draw->is_pixmap || draw->swap_method == __DRI_ATTRIB_SWAP_EXCHANGE)
1998 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
1999
2000 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
2001 /* All pixmaps are owned by the server gpu.
2002 * When we use a different gpu, we can't use the pixmap
2003 * as buffer since it is potentially tiled a way
2004 * our device can't understand. In this case, use
2005 * a fake front buffer. Hopefully the pixmap
2006 * content will get synced with the fake front
2007 * buffer.
2008 */
2009 if (draw->is_pixmap && !draw->is_different_gpu)
2010 front = dri3_get_pixmap_buffer(driDrawable,
2011 format,
2012 loader_dri3_buffer_front,
2013 draw);
2014 else
2015 front = dri3_get_buffer(driDrawable,
2016 format,
2017 loader_dri3_buffer_front,
2018 draw);
2019
2020 if (!front)
2021 return false;
2022 } else {
2023 dri3_free_buffers(driDrawable, loader_dri3_buffer_front, draw);
2024 draw->have_fake_front = 0;
2025 }
2026
2027 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
2028 back = dri3_get_buffer(driDrawable,
2029 format,
2030 loader_dri3_buffer_back,
2031 draw);
2032 if (!back)
2033 return false;
2034 draw->have_back = 1;
2035 } else {
2036 dri3_free_buffers(driDrawable, loader_dri3_buffer_back, draw);
2037 draw->have_back = 0;
2038 }
2039
2040 if (front) {
2041 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
2042 buffers->front = front->image;
2043 draw->have_fake_front = draw->is_different_gpu || !draw->is_pixmap;
2044 }
2045
2046 if (back) {
2047 buffers->image_mask |= __DRI_IMAGE_BUFFER_BACK;
2048 buffers->back = back->image;
2049 }
2050
2051 draw->stamp = stamp;
2052
2053 return true;
2054 }
2055
2056 /** loader_dri3_update_drawable_geometry
2057 *
2058 * Get the current drawable geometry.
2059 */
2060 void
2061 loader_dri3_update_drawable_geometry(struct loader_dri3_drawable *draw)
2062 {
2063 xcb_get_geometry_cookie_t geom_cookie;
2064 xcb_get_geometry_reply_t *geom_reply;
2065
2066 geom_cookie = xcb_get_geometry(draw->conn, draw->drawable);
2067
2068 geom_reply = xcb_get_geometry_reply(draw->conn, geom_cookie, NULL);
2069
2070 if (geom_reply) {
2071 draw->width = geom_reply->width;
2072 draw->height = geom_reply->height;
2073 draw->vtable->set_drawable_size(draw, draw->width, draw->height);
2074 draw->ext->flush->invalidate(draw->dri_drawable);
2075
2076 free(geom_reply);
2077 }
2078 }
2079
2080
2081 /**
2082 * Make sure the server has flushed all pending swap buffers to hardware
2083 * for this drawable. Ideally we'd want to send an X protocol request to
2084 * have the server block our connection until the swaps are complete. That
2085 * would avoid the potential round-trip here.
2086 */
2087 void
2088 loader_dri3_swapbuffer_barrier(struct loader_dri3_drawable *draw)
2089 {
2090 int64_t ust, msc, sbc;
2091
2092 (void) loader_dri3_wait_for_sbc(draw, 0, &ust, &msc, &sbc);
2093 }
2094
2095 /**
2096 * Perform any cleanup associated with a close screen operation.
2097 * \param dri_screen[in,out] Pointer to __DRIscreen about to be closed.
2098 *
2099 * This function destroys the screen's cached swap context if any.
2100 */
2101 void
2102 loader_dri3_close_screen(__DRIscreen *dri_screen)
2103 {
2104 mtx_lock(&blit_context.mtx);
2105 if (blit_context.ctx && blit_context.cur_screen == dri_screen) {
2106 blit_context.core->destroyContext(blit_context.ctx);
2107 blit_context.ctx = NULL;
2108 }
2109 mtx_unlock(&blit_context.mtx);
2110 }
2111
2112 /**
2113 * Find a backbuffer slot - potentially allocating a back buffer
2114 *
2115 * \param draw[in,out] Pointer to the drawable for which to find back.
2116 * \return Pointer to a new back buffer or NULL if allocation failed or was
2117 * not mandated.
2118 *
2119 * Find a potentially new back buffer, and if it's not been allocated yet and
2120 * in addition needs initializing, then try to allocate and initialize it.
2121 */
2122 #include <stdio.h>
2123 static struct loader_dri3_buffer *
2124 dri3_find_back_alloc(struct loader_dri3_drawable *draw)
2125 {
2126 struct loader_dri3_buffer *back;
2127 int id;
2128
2129 id = dri3_find_back(draw);
2130 if (id < 0)
2131 return NULL;
2132
2133 back = draw->buffers[id];
2134 /* Allocate a new back if we haven't got one */
2135 if (!back && draw->back_format != __DRI_IMAGE_FORMAT_NONE &&
2136 dri3_update_drawable(draw))
2137 back = dri3_alloc_render_buffer(draw, draw->back_format,
2138 draw->width, draw->height, draw->depth);
2139
2140 if (!back)
2141 return NULL;
2142
2143 draw->buffers[id] = back;
2144
2145 /* If necessary, prefill the back with data according to swap_method mode. */
2146 if (draw->cur_blit_source != -1 &&
2147 draw->buffers[draw->cur_blit_source] &&
2148 back != draw->buffers[draw->cur_blit_source]) {
2149 struct loader_dri3_buffer *source = draw->buffers[draw->cur_blit_source];
2150
2151 dri3_fence_await(draw->conn, draw, source);
2152 dri3_fence_await(draw->conn, draw, back);
2153 (void) loader_dri3_blit_image(draw,
2154 back->image,
2155 source->image,
2156 0, 0, draw->width, draw->height,
2157 0, 0, 0);
2158 back->last_swap = source->last_swap;
2159 draw->cur_blit_source = -1;
2160 }
2161
2162 return back;
2163 }