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