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