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