2 * Copyright © 2013 Keith Packard
3 * Copyright © 2015 Boyan Ding
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.
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
28 #include <X11/xshmfence.h>
31 #include <xcb/present.h>
33 #include <X11/Xlib-xcb.h>
35 #include "loader_dri3_helper.h"
37 /* From xmlpool/options.h, user exposed so should be stable */
38 #define DRI_CONF_VBLANK_NEVER 0
39 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
40 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
41 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
44 * A cached blit context.
46 struct loader_dri3_blit_context
{
49 __DRIscreen
*cur_screen
;
50 const __DRIcoreExtension
*core
;
53 /* For simplicity we maintain the cache only for a single screen at a time */
54 static struct loader_dri3_blit_context blit_context
= {
55 _MTX_INITIALIZER_NP
, NULL
59 dri3_flush_present_events(struct loader_dri3_drawable
*draw
);
61 static struct loader_dri3_buffer
*
62 dri3_find_back_alloc(struct loader_dri3_drawable
*draw
);
65 * Do we have blit functionality in the image blit extension?
67 * \param draw[in] The drawable intended to blit from / to.
68 * \return true if we have blit functionality. false otherwise.
70 static bool loader_dri3_have_image_blit(const struct loader_dri3_drawable
*draw
)
72 return draw
->ext
->image
->base
.version
>= 9 &&
73 draw
->ext
->image
->blitImage
!= NULL
;
77 * Get and lock (for use with the current thread) a dri context associated
78 * with the drawable's dri screen. The context is intended to be used with
79 * the dri image extension's blitImage method.
81 * \param draw[in] Pointer to the drawable whose dri screen we want a
83 * \return A dri context or NULL if context creation failed.
85 * When the caller is done with the context (even if the context returned was
86 * NULL), the caller must call loader_dri3_blit_context_put.
89 loader_dri3_blit_context_get(struct loader_dri3_drawable
*draw
)
91 mtx_lock(&blit_context
.mtx
);
93 if (blit_context
.ctx
&& blit_context
.cur_screen
!= draw
->dri_screen
) {
94 blit_context
.core
->destroyContext(blit_context
.ctx
);
95 blit_context
.ctx
= NULL
;
98 if (!blit_context
.ctx
) {
99 blit_context
.ctx
= draw
->ext
->core
->createNewContext(draw
->dri_screen
,
101 blit_context
.cur_screen
= draw
->dri_screen
;
102 blit_context
.core
= draw
->ext
->core
;
105 return blit_context
.ctx
;
109 * Release (for use with other threads) a dri context previously obtained using
110 * loader_dri3_blit_context_get.
113 loader_dri3_blit_context_put(void)
115 mtx_unlock(&blit_context
.mtx
);
119 * Blit (parts of) the contents of a DRI image to another dri image
121 * \param draw[in] The drawable which owns the images.
122 * \param dst[in] The destination image.
123 * \param src[in] The source image.
124 * \param dstx0[in] Start destination coordinate.
125 * \param dsty0[in] Start destination coordinate.
126 * \param width[in] Blit width.
127 * \param height[in] Blit height.
128 * \param srcx0[in] Start source coordinate.
129 * \param srcy0[in] Start source coordinate.
130 * \param flush_flag[in] Image blit flush flag.
131 * \return true iff successful.
134 loader_dri3_blit_image(struct loader_dri3_drawable
*draw
,
135 __DRIimage
*dst
, __DRIimage
*src
,
136 int dstx0
, int dsty0
, int width
, int height
,
137 int srcx0
, int srcy0
, int flush_flag
)
139 __DRIcontext
*dri_context
;
140 bool use_blit_context
= false;
142 if (!loader_dri3_have_image_blit(draw
))
145 dri_context
= draw
->vtable
->get_dri_context(draw
);
147 if (!dri_context
|| !draw
->vtable
->in_current_context(draw
)) {
148 dri_context
= loader_dri3_blit_context_get(draw
);
149 use_blit_context
= true;
150 flush_flag
|= __BLIT_FLAG_FLUSH
;
154 draw
->ext
->image
->blitImage(dri_context
, dst
, src
, dstx0
, dsty0
,
155 width
, height
, srcx0
, srcy0
,
156 width
, height
, flush_flag
);
158 if (use_blit_context
)
159 loader_dri3_blit_context_put();
161 return dri_context
!= NULL
;
165 dri3_fence_reset(xcb_connection_t
*c
, struct loader_dri3_buffer
*buffer
)
167 xshmfence_reset(buffer
->shm_fence
);
171 dri3_fence_set(struct loader_dri3_buffer
*buffer
)
173 xshmfence_trigger(buffer
->shm_fence
);
177 dri3_fence_trigger(xcb_connection_t
*c
, struct loader_dri3_buffer
*buffer
)
179 xcb_sync_trigger_fence(c
, buffer
->sync_fence
);
183 dri3_fence_await(xcb_connection_t
*c
, struct loader_dri3_drawable
*draw
,
184 struct loader_dri3_buffer
*buffer
)
187 xshmfence_await(buffer
->shm_fence
);
189 mtx_lock(&draw
->mtx
);
190 dri3_flush_present_events(draw
);
191 mtx_unlock(&draw
->mtx
);
196 dri3_update_num_back(struct loader_dri3_drawable
*draw
)
205 loader_dri3_set_swap_interval(struct loader_dri3_drawable
*draw
, int interval
)
207 draw
->swap_interval
= interval
;
210 /** dri3_free_render_buffer
212 * Free everything associated with one render buffer including pixmap, fence
213 * stuff and the driver image
216 dri3_free_render_buffer(struct loader_dri3_drawable
*draw
,
217 struct loader_dri3_buffer
*buffer
)
219 if (buffer
->own_pixmap
)
220 xcb_free_pixmap(draw
->conn
, buffer
->pixmap
);
221 xcb_sync_destroy_fence(draw
->conn
, buffer
->sync_fence
);
222 xshmfence_unmap_shm(buffer
->shm_fence
);
223 draw
->ext
->image
->destroyImage(buffer
->image
);
224 if (buffer
->linear_buffer
)
225 draw
->ext
->image
->destroyImage(buffer
->linear_buffer
);
230 loader_dri3_drawable_fini(struct loader_dri3_drawable
*draw
)
234 draw
->ext
->core
->destroyDrawable(draw
->dri_drawable
);
236 for (i
= 0; i
< LOADER_DRI3_NUM_BUFFERS
; i
++) {
237 if (draw
->buffers
[i
])
238 dri3_free_render_buffer(draw
, draw
->buffers
[i
]);
241 if (draw
->special_event
) {
242 xcb_void_cookie_t cookie
=
243 xcb_present_select_input_checked(draw
->conn
, draw
->eid
, draw
->drawable
,
244 XCB_PRESENT_EVENT_MASK_NO_EVENT
);
246 xcb_discard_reply(draw
->conn
, cookie
.sequence
);
247 xcb_unregister_for_special_event(draw
->conn
, draw
->special_event
);
250 cnd_destroy(&draw
->event_cnd
);
251 mtx_destroy(&draw
->mtx
);
255 loader_dri3_drawable_init(xcb_connection_t
*conn
,
256 xcb_drawable_t drawable
,
257 __DRIscreen
*dri_screen
,
258 bool is_different_gpu
,
259 const __DRIconfig
*dri_config
,
260 struct loader_dri3_extensions
*ext
,
261 const struct loader_dri3_vtable
*vtable
,
262 struct loader_dri3_drawable
*draw
)
264 xcb_get_geometry_cookie_t cookie
;
265 xcb_get_geometry_reply_t
*reply
;
266 xcb_generic_error_t
*error
;
267 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
272 draw
->vtable
= vtable
;
273 draw
->drawable
= drawable
;
274 draw
->dri_screen
= dri_screen
;
275 draw
->is_different_gpu
= is_different_gpu
;
278 draw
->have_fake_front
= 0;
279 draw
->first_init
= true;
281 draw
->cur_blit_source
= -1;
282 draw
->back_format
= __DRI_IMAGE_FORMAT_NONE
;
283 mtx_init(&draw
->mtx
, mtx_plain
);
284 cnd_init(&draw
->event_cnd
);
286 if (draw
->ext
->config
)
287 draw
->ext
->config
->configQueryi(draw
->dri_screen
,
288 "vblank_mode", &vblank_mode
);
290 switch (vblank_mode
) {
291 case DRI_CONF_VBLANK_NEVER
:
292 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
295 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
296 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
301 draw
->swap_interval
= swap_interval
;
303 dri3_update_num_back(draw
);
305 /* Create a new drawable */
307 draw
->ext
->image_driver
->createNewDrawable(dri_screen
,
311 if (!draw
->dri_drawable
)
314 cookie
= xcb_get_geometry(draw
->conn
, draw
->drawable
);
315 reply
= xcb_get_geometry_reply(draw
->conn
, cookie
, &error
);
316 if (reply
== NULL
|| error
!= NULL
) {
317 draw
->ext
->core
->destroyDrawable(draw
->dri_drawable
);
321 draw
->width
= reply
->width
;
322 draw
->height
= reply
->height
;
323 draw
->depth
= reply
->depth
;
324 draw
->vtable
->set_drawable_size(draw
, draw
->width
, draw
->height
);
327 draw
->swap_method
= __DRI_ATTRIB_SWAP_UNDEFINED
;
328 if (draw
->ext
->core
->base
.version
>= 2) {
329 (void )draw
->ext
->core
->getConfigAttrib(dri_config
,
330 __DRI_ATTRIB_SWAP_METHOD
,
335 * Make sure server has the same swap interval we do for the new
338 loader_dri3_set_swap_interval(draw
, swap_interval
);
344 * Process one Present event
347 dri3_handle_present_event(struct loader_dri3_drawable
*draw
,
348 xcb_present_generic_event_t
*ge
)
350 switch (ge
->evtype
) {
351 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
352 xcb_present_configure_notify_event_t
*ce
= (void *) ge
;
354 draw
->width
= ce
->width
;
355 draw
->height
= ce
->height
;
356 draw
->vtable
->set_drawable_size(draw
, draw
->width
, draw
->height
);
357 draw
->ext
->flush
->invalidate(draw
->dri_drawable
);
360 case XCB_PRESENT_COMPLETE_NOTIFY
: {
361 xcb_present_complete_notify_event_t
*ce
= (void *) ge
;
363 /* Compute the processed SBC number from the received 32-bit serial number
364 * merged with the upper 32-bits of the sent 64-bit serial number while
367 if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
) {
368 draw
->recv_sbc
= (draw
->send_sbc
& 0xffffffff00000000LL
) | ce
->serial
;
369 if (draw
->recv_sbc
> draw
->send_sbc
)
370 draw
->recv_sbc
-= 0x100000000;
372 case XCB_PRESENT_COMPLETE_MODE_FLIP
:
373 draw
->flipping
= true;
375 case XCB_PRESENT_COMPLETE_MODE_COPY
:
376 draw
->flipping
= false;
380 if (draw
->vtable
->show_fps
)
381 draw
->vtable
->show_fps(draw
, ce
->ust
);
385 } else if (ce
->serial
== draw
->eid
) {
386 draw
->notify_ust
= ce
->ust
;
387 draw
->notify_msc
= ce
->msc
;
391 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
392 xcb_present_idle_notify_event_t
*ie
= (void *) ge
;
395 for (b
= 0; b
< sizeof(draw
->buffers
) / sizeof(draw
->buffers
[0]); b
++) {
396 struct loader_dri3_buffer
*buf
= draw
->buffers
[b
];
398 if (buf
&& buf
->pixmap
== ie
->pixmap
)
401 if (buf
&& draw
->num_back
<= b
&& b
< LOADER_DRI3_MAX_BACK
&&
402 draw
->cur_blit_source
!= b
&&
404 dri3_free_render_buffer(draw
, buf
);
405 draw
->buffers
[b
] = NULL
;
415 dri3_wait_for_event_locked(struct loader_dri3_drawable
*draw
)
417 xcb_generic_event_t
*ev
;
418 xcb_present_generic_event_t
*ge
;
420 xcb_flush(draw
->conn
);
422 /* Only have one thread waiting for events at a time */
423 if (draw
->has_event_waiter
) {
424 cnd_wait(&draw
->event_cnd
, &draw
->mtx
);
425 /* Another thread has updated the protected info, so retest. */
428 draw
->has_event_waiter
= true;
429 /* Allow other threads access to the drawable while we're waiting. */
430 mtx_unlock(&draw
->mtx
);
431 ev
= xcb_wait_for_special_event(draw
->conn
, draw
->special_event
);
432 mtx_lock(&draw
->mtx
);
433 draw
->has_event_waiter
= false;
434 cnd_broadcast(&draw
->event_cnd
);
439 dri3_handle_present_event(draw
, ge
);
443 /** loader_dri3_wait_for_msc
445 * Get the X server to send an event when the target msc/divisor/remainder is
449 loader_dri3_wait_for_msc(struct loader_dri3_drawable
*draw
,
451 int64_t divisor
, int64_t remainder
,
452 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
454 xcb_void_cookie_t cookie
= xcb_present_notify_msc(draw
->conn
,
460 xcb_generic_event_t
*ev
;
461 unsigned full_sequence
;
463 mtx_lock(&draw
->mtx
);
464 xcb_flush(draw
->conn
);
466 /* Wait for the event */
468 ev
= xcb_wait_for_special_event(draw
->conn
, draw
->special_event
);
470 mtx_unlock(&draw
->mtx
);
474 full_sequence
= ev
->full_sequence
;
475 dri3_handle_present_event(draw
, (void *) ev
);
476 } while (full_sequence
!= cookie
.sequence
|| draw
->notify_msc
< target_msc
);
478 *ust
= draw
->notify_ust
;
479 *msc
= draw
->notify_msc
;
480 *sbc
= draw
->recv_sbc
;
481 mtx_unlock(&draw
->mtx
);
486 /** loader_dri3_wait_for_sbc
488 * Wait for the completed swap buffer count to reach the specified
489 * target. Presumably the application knows that this will be reached with
490 * outstanding complete events, or we're going to be here awhile.
493 loader_dri3_wait_for_sbc(struct loader_dri3_drawable
*draw
,
494 int64_t target_sbc
, int64_t *ust
,
495 int64_t *msc
, int64_t *sbc
)
497 /* From the GLX_OML_sync_control spec:
499 * "If <target_sbc> = 0, the function will block until all previous
500 * swaps requested with glXSwapBuffersMscOML for that window have
503 mtx_lock(&draw
->mtx
);
505 target_sbc
= draw
->send_sbc
;
507 while (draw
->recv_sbc
< target_sbc
) {
508 if (!dri3_wait_for_event_locked(draw
)) {
509 mtx_unlock(&draw
->mtx
);
516 *sbc
= draw
->recv_sbc
;
517 mtx_unlock(&draw
->mtx
);
521 /** loader_dri3_find_back
523 * Find an idle back buffer. If there isn't one, then
524 * wait for a present idle notify event from the X server
527 dri3_find_back(struct loader_dri3_drawable
*draw
)
532 mtx_lock(&draw
->mtx
);
533 /* Increase the likelyhood of reusing current buffer */
534 dri3_flush_present_events(draw
);
536 /* Check whether we need to reuse the current back buffer as new back.
537 * In that case, wait until it's not busy anymore.
539 dri3_update_num_back(draw
);
540 num_to_consider
= draw
->num_back
;
541 if (!loader_dri3_have_image_blit(draw
) && draw
->cur_blit_source
!= -1) {
543 draw
->cur_blit_source
= -1;
547 for (b
= 0; b
< num_to_consider
; b
++) {
548 int id
= LOADER_DRI3_BACK_ID((b
+ draw
->cur_back
) % draw
->num_back
);
549 struct loader_dri3_buffer
*buffer
= draw
->buffers
[id
];
551 if (!buffer
|| !buffer
->busy
) {
553 mtx_unlock(&draw
->mtx
);
557 if (!dri3_wait_for_event_locked(draw
)) {
558 mtx_unlock(&draw
->mtx
);
564 static xcb_gcontext_t
565 dri3_drawable_gc(struct loader_dri3_drawable
*draw
)
569 xcb_create_gc(draw
->conn
,
570 (draw
->gc
= xcb_generate_id(draw
->conn
)),
572 XCB_GC_GRAPHICS_EXPOSURES
,
579 static struct loader_dri3_buffer
*
580 dri3_back_buffer(struct loader_dri3_drawable
*draw
)
582 return draw
->buffers
[LOADER_DRI3_BACK_ID(draw
->cur_back
)];
585 static struct loader_dri3_buffer
*
586 dri3_fake_front_buffer(struct loader_dri3_drawable
*draw
)
588 return draw
->buffers
[LOADER_DRI3_FRONT_ID
];
592 dri3_copy_area(xcb_connection_t
*c
,
593 xcb_drawable_t src_drawable
,
594 xcb_drawable_t dst_drawable
,
603 xcb_void_cookie_t cookie
;
605 cookie
= xcb_copy_area_checked(c
,
615 xcb_discard_reply(c
, cookie
.sequence
);
619 * Asks the driver to flush any queued work necessary for serializing with the
620 * X command stream, and optionally the slightly more strict requirement of
621 * glFlush() equivalence (which would require flushing even if nothing had
622 * been drawn to a window system framebuffer, for example).
625 loader_dri3_flush(struct loader_dri3_drawable
*draw
,
627 enum __DRI2throttleReason throttle_reason
)
629 /* NEED TO CHECK WHETHER CONTEXT IS NULL */
630 __DRIcontext
*dri_context
= draw
->vtable
->get_dri_context(draw
);
633 draw
->ext
->flush
->flush_with_flags(dri_context
, draw
->dri_drawable
,
634 flags
, throttle_reason
);
639 loader_dri3_copy_sub_buffer(struct loader_dri3_drawable
*draw
,
641 int width
, int height
,
644 struct loader_dri3_buffer
*back
;
645 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
647 /* Check we have the right attachments */
648 if (!draw
->have_back
|| draw
->is_pixmap
)
652 flags
|= __DRI2_FLUSH_CONTEXT
;
653 loader_dri3_flush(draw
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
655 back
= dri3_find_back_alloc(draw
);
659 y
= draw
->height
- y
- height
;
661 if (draw
->is_different_gpu
) {
662 /* Update the linear buffer part of the back buffer
663 * for the dri3_copy_area operation
665 (void) loader_dri3_blit_image(draw
,
668 0, 0, back
->width
, back
->height
,
669 0, 0, __BLIT_FLAG_FLUSH
);
672 loader_dri3_swapbuffer_barrier(draw
);
673 dri3_fence_reset(draw
->conn
, back
);
674 dri3_copy_area(draw
->conn
,
677 dri3_drawable_gc(draw
),
678 x
, y
, x
, y
, width
, height
);
679 dri3_fence_trigger(draw
->conn
, back
);
680 /* Refresh the fake front (if present) after we just damaged the real
683 if (draw
->have_fake_front
&&
684 !loader_dri3_blit_image(draw
,
685 dri3_fake_front_buffer(draw
)->image
,
688 x
, y
, __BLIT_FLAG_FLUSH
) &&
689 !draw
->is_different_gpu
) {
690 dri3_fence_reset(draw
->conn
, dri3_fake_front_buffer(draw
));
691 dri3_copy_area(draw
->conn
,
693 dri3_fake_front_buffer(draw
)->pixmap
,
694 dri3_drawable_gc(draw
),
695 x
, y
, x
, y
, width
, height
);
696 dri3_fence_trigger(draw
->conn
, dri3_fake_front_buffer(draw
));
697 dri3_fence_await(draw
->conn
, NULL
, dri3_fake_front_buffer(draw
));
699 dri3_fence_await(draw
->conn
, draw
, back
);
703 loader_dri3_copy_drawable(struct loader_dri3_drawable
*draw
,
707 loader_dri3_flush(draw
, __DRI2_FLUSH_DRAWABLE
, 0);
709 dri3_fence_reset(draw
->conn
, dri3_fake_front_buffer(draw
));
710 dri3_copy_area(draw
->conn
,
712 dri3_drawable_gc(draw
),
713 0, 0, 0, 0, draw
->width
, draw
->height
);
714 dri3_fence_trigger(draw
->conn
, dri3_fake_front_buffer(draw
));
715 dri3_fence_await(draw
->conn
, draw
, dri3_fake_front_buffer(draw
));
719 loader_dri3_wait_x(struct loader_dri3_drawable
*draw
)
721 struct loader_dri3_buffer
*front
;
723 if (draw
== NULL
|| !draw
->have_fake_front
)
726 front
= dri3_fake_front_buffer(draw
);
728 loader_dri3_copy_drawable(draw
, front
->pixmap
, draw
->drawable
);
730 /* In the psc->is_different_gpu case, the linear buffer has been updated,
731 * but not yet the tiled buffer.
732 * Copy back to the tiled buffer we use for rendering.
733 * Note that we don't need flushing.
735 if (draw
->is_different_gpu
)
736 (void) loader_dri3_blit_image(draw
,
738 front
->linear_buffer
,
739 0, 0, front
->width
, front
->height
,
744 loader_dri3_wait_gl(struct loader_dri3_drawable
*draw
)
746 struct loader_dri3_buffer
*front
;
748 if (draw
== NULL
|| !draw
->have_fake_front
)
751 front
= dri3_fake_front_buffer(draw
);
753 /* In the psc->is_different_gpu case, we update the linear_buffer
754 * before updating the real front.
756 if (draw
->is_different_gpu
)
757 (void) loader_dri3_blit_image(draw
,
758 front
->linear_buffer
,
760 0, 0, front
->width
, front
->height
,
761 0, 0, __BLIT_FLAG_FLUSH
);
762 loader_dri3_swapbuffer_barrier(draw
);
763 loader_dri3_copy_drawable(draw
, draw
->drawable
, front
->pixmap
);
766 /** dri3_flush_present_events
768 * Process any present events that have been received from the X server
771 dri3_flush_present_events(struct loader_dri3_drawable
*draw
)
773 /* Check to see if any configuration changes have occurred
774 * since we were last invoked
776 if (draw
->has_event_waiter
)
779 if (draw
->special_event
) {
780 xcb_generic_event_t
*ev
;
782 while ((ev
= xcb_poll_for_special_event(draw
->conn
,
783 draw
->special_event
)) != NULL
) {
784 xcb_present_generic_event_t
*ge
= (void *) ev
;
785 dri3_handle_present_event(draw
, ge
);
790 /** loader_dri3_swap_buffers_msc
792 * Make the current back buffer visible using the present extension
795 loader_dri3_swap_buffers_msc(struct loader_dri3_drawable
*draw
,
796 int64_t target_msc
, int64_t divisor
,
797 int64_t remainder
, unsigned flush_flags
,
800 struct loader_dri3_buffer
*back
;
802 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
804 draw
->vtable
->flush_drawable(draw
, flush_flags
);
806 back
= dri3_find_back_alloc(draw
);
808 mtx_lock(&draw
->mtx
);
809 if (draw
->is_different_gpu
&& back
) {
810 /* Update the linear buffer before presenting the pixmap */
811 (void) loader_dri3_blit_image(draw
,
814 0, 0, back
->width
, back
->height
,
815 0, 0, __BLIT_FLAG_FLUSH
);
818 /* If we need to preload the new back buffer, remember the source.
819 * The force_copy parameter is used by EGL to attempt to preserve
820 * the back buffer across a call to this function.
822 if (draw
->swap_method
!= __DRI_ATTRIB_SWAP_UNDEFINED
|| force_copy
)
823 draw
->cur_blit_source
= LOADER_DRI3_BACK_ID(draw
->cur_back
);
825 /* Exchange the back and fake front. Even though the server knows about these
826 * buffers, it has no notion of back and fake front.
828 if (back
&& draw
->have_fake_front
) {
829 struct loader_dri3_buffer
*tmp
;
831 tmp
= dri3_fake_front_buffer(draw
);
832 draw
->buffers
[LOADER_DRI3_FRONT_ID
] = back
;
833 draw
->buffers
[LOADER_DRI3_BACK_ID(draw
->cur_back
)] = tmp
;
835 if (draw
->swap_method
== __DRI_ATTRIB_SWAP_COPY
|| force_copy
)
836 draw
->cur_blit_source
= LOADER_DRI3_FRONT_ID
;
839 dri3_flush_present_events(draw
);
841 if (back
&& !draw
->is_pixmap
) {
842 dri3_fence_reset(draw
->conn
, back
);
844 /* Compute when we want the frame shown by taking the last known
845 * successful MSC and adding in a swap interval for each outstanding swap
846 * request. target_msc=divisor=remainder=0 means "Use glXSwapBuffers()
850 if (target_msc
== 0 && divisor
== 0 && remainder
== 0)
851 target_msc
= draw
->msc
+ draw
->swap_interval
*
852 (draw
->send_sbc
- draw
->recv_sbc
);
853 else if (divisor
== 0 && remainder
> 0) {
854 /* From the GLX_OML_sync_control spec:
855 * "If <divisor> = 0, the swap will occur when MSC becomes
856 * greater than or equal to <target_msc>."
858 * Note that there's no mention of the remainder. The Present
859 * extension throws BadValue for remainder != 0 with divisor == 0, so
860 * just drop the passed in value.
865 /* From the GLX_EXT_swap_control spec
866 * and the EGL 1.4 spec (page 53):
868 * "If <interval> is set to a value of 0, buffer swaps are not
869 * synchronized to a video frame."
871 * Implementation note: It is possible to enable triple buffering
872 * behaviour by not using XCB_PRESENT_OPTION_ASYNC, but this should not be
875 if (draw
->swap_interval
== 0)
876 options
|= XCB_PRESENT_OPTION_ASYNC
;
878 /* If we need to populate the new back, but need to reuse the back
879 * buffer slot due to lack of local blit capabilities, make sure
880 * the server doesn't flip and we deadlock.
882 if (!loader_dri3_have_image_blit(draw
) && draw
->cur_blit_source
!= -1)
883 options
|= XCB_PRESENT_OPTION_COPY
;
886 back
->last_swap
= draw
->send_sbc
;
887 xcb_present_pixmap(draw
->conn
,
890 (uint32_t) draw
->send_sbc
,
895 None
, /* target_crtc */
902 ret
= (int64_t) draw
->send_sbc
;
904 /* Schedule a server-side back-preserving blit if necessary.
905 * This happens iff all conditions below are satisfied:
906 * a) We have a fake front,
907 * b) We need to preserve the back buffer,
908 * c) We don't have local blit capabilities.
910 if (!loader_dri3_have_image_blit(draw
) && draw
->cur_blit_source
!= -1 &&
911 draw
->cur_blit_source
!= LOADER_DRI3_BACK_ID(draw
->cur_back
)) {
912 struct loader_dri3_buffer
*new_back
= dri3_back_buffer(draw
);
913 struct loader_dri3_buffer
*src
= draw
->buffers
[draw
->cur_blit_source
];
915 dri3_fence_reset(draw
->conn
, new_back
);
916 dri3_copy_area(draw
->conn
, src
->pixmap
,
918 dri3_drawable_gc(draw
),
919 0, 0, 0, 0, draw
->width
, draw
->height
);
920 dri3_fence_trigger(draw
->conn
, new_back
);
921 new_back
->last_swap
= src
->last_swap
;
924 xcb_flush(draw
->conn
);
928 mtx_unlock(&draw
->mtx
);
930 draw
->ext
->flush
->invalidate(draw
->dri_drawable
);
936 loader_dri3_query_buffer_age(struct loader_dri3_drawable
*draw
)
938 struct loader_dri3_buffer
*back
= dri3_find_back_alloc(draw
);
941 mtx_lock(&draw
->mtx
);
942 ret
= (!back
|| back
->last_swap
== 0) ? 0 :
943 draw
->send_sbc
- back
->last_swap
+ 1;
944 mtx_unlock(&draw
->mtx
);
951 * Wrapper around xcb_dri3_open
954 loader_dri3_open(xcb_connection_t
*conn
,
958 xcb_dri3_open_cookie_t cookie
;
959 xcb_dri3_open_reply_t
*reply
;
962 cookie
= xcb_dri3_open(conn
,
966 reply
= xcb_dri3_open_reply(conn
, cookie
, NULL
);
970 if (reply
->nfd
!= 1) {
975 fd
= xcb_dri3_open_reply_fds(conn
, reply
)[0];
977 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
983 dri3_cpp_for_format(uint32_t format
) {
985 case __DRI_IMAGE_FORMAT_R8
:
987 case __DRI_IMAGE_FORMAT_RGB565
:
988 case __DRI_IMAGE_FORMAT_GR88
:
990 case __DRI_IMAGE_FORMAT_XRGB8888
:
991 case __DRI_IMAGE_FORMAT_ARGB8888
:
992 case __DRI_IMAGE_FORMAT_ABGR8888
:
993 case __DRI_IMAGE_FORMAT_XBGR8888
:
994 case __DRI_IMAGE_FORMAT_XRGB2101010
:
995 case __DRI_IMAGE_FORMAT_ARGB2101010
:
996 case __DRI_IMAGE_FORMAT_SARGB8
:
998 case __DRI_IMAGE_FORMAT_NONE
:
1004 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
1005 * the createImageFromFds call takes __DRI_IMAGE_FOURCC codes. To avoid
1006 * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
1007 * translate to __DRI_IMAGE_FOURCC codes in the call to createImageFromFds
1010 image_format_to_fourcc(int format
)
1013 /* Convert from __DRI_IMAGE_FORMAT to __DRI_IMAGE_FOURCC (sigh) */
1015 case __DRI_IMAGE_FORMAT_SARGB8
: return __DRI_IMAGE_FOURCC_SARGB8888
;
1016 case __DRI_IMAGE_FORMAT_RGB565
: return __DRI_IMAGE_FOURCC_RGB565
;
1017 case __DRI_IMAGE_FORMAT_XRGB8888
: return __DRI_IMAGE_FOURCC_XRGB8888
;
1018 case __DRI_IMAGE_FORMAT_ARGB8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
1019 case __DRI_IMAGE_FORMAT_ABGR8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
1020 case __DRI_IMAGE_FORMAT_XBGR8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
1021 case __DRI_IMAGE_FORMAT_XRGB2101010
: return __DRI_IMAGE_FOURCC_XRGB2101010
;
1022 case __DRI_IMAGE_FORMAT_ARGB2101010
: return __DRI_IMAGE_FOURCC_ARGB2101010
;
1027 /** loader_dri3_alloc_render_buffer
1029 * Use the driver createImage function to construct a __DRIimage, then
1030 * get a file descriptor for that and create an X pixmap from that
1032 * Allocate an xshmfence for synchronization
1034 static struct loader_dri3_buffer
*
1035 dri3_alloc_render_buffer(struct loader_dri3_drawable
*draw
, unsigned int format
,
1036 int width
, int height
, int depth
)
1038 struct loader_dri3_buffer
*buffer
;
1039 __DRIimage
*pixmap_buffer
;
1040 xcb_pixmap_t pixmap
;
1041 xcb_sync_fence_t sync_fence
;
1042 struct xshmfence
*shm_fence
;
1043 int buffer_fd
, fence_fd
;
1046 /* Create an xshmfence object and
1047 * prepare to send that to the X server
1050 fence_fd
= xshmfence_alloc_shm();
1054 shm_fence
= xshmfence_map_shm(fence_fd
);
1055 if (shm_fence
== NULL
)
1058 /* Allocate the image from the driver
1060 buffer
= calloc(1, sizeof *buffer
);
1064 buffer
->cpp
= dri3_cpp_for_format(format
);
1068 if (!draw
->is_different_gpu
) {
1069 buffer
->image
= draw
->ext
->image
->createImage(draw
->dri_screen
,
1072 __DRI_IMAGE_USE_SHARE
|
1073 __DRI_IMAGE_USE_SCANOUT
|
1074 __DRI_IMAGE_USE_BACKBUFFER
,
1076 pixmap_buffer
= buffer
->image
;
1081 buffer
->image
= draw
->ext
->image
->createImage(draw
->dri_screen
,
1090 buffer
->linear_buffer
=
1091 draw
->ext
->image
->createImage(draw
->dri_screen
,
1092 width
, height
, format
,
1093 __DRI_IMAGE_USE_SHARE
|
1094 __DRI_IMAGE_USE_LINEAR
|
1095 __DRI_IMAGE_USE_BACKBUFFER
,
1097 pixmap_buffer
= buffer
->linear_buffer
;
1099 if (!buffer
->linear_buffer
)
1100 goto no_linear_buffer
;
1103 /* X wants the stride, so ask the image for it
1105 if (!draw
->ext
->image
->queryImage(pixmap_buffer
, __DRI_IMAGE_ATTRIB_STRIDE
,
1107 goto no_buffer_attrib
;
1109 buffer
->pitch
= stride
;
1111 if (!draw
->ext
->image
->queryImage(pixmap_buffer
, __DRI_IMAGE_ATTRIB_FD
,
1113 goto no_buffer_attrib
;
1115 xcb_dri3_pixmap_from_buffer(draw
->conn
,
1116 (pixmap
= xcb_generate_id(draw
->conn
)),
1119 width
, height
, buffer
->pitch
,
1120 depth
, buffer
->cpp
* 8,
1123 xcb_dri3_fence_from_fd(draw
->conn
,
1125 (sync_fence
= xcb_generate_id(draw
->conn
)),
1129 buffer
->pixmap
= pixmap
;
1130 buffer
->own_pixmap
= true;
1131 buffer
->sync_fence
= sync_fence
;
1132 buffer
->shm_fence
= shm_fence
;
1133 buffer
->width
= width
;
1134 buffer
->height
= height
;
1136 /* Mark the buffer as idle
1138 dri3_fence_set(buffer
);
1143 draw
->ext
->image
->destroyImage(pixmap_buffer
);
1145 if (draw
->is_different_gpu
)
1146 draw
->ext
->image
->destroyImage(buffer
->image
);
1150 xshmfence_unmap_shm(shm_fence
);
1156 /** loader_dri3_update_drawable
1158 * Called the first time we use the drawable and then
1159 * after we receive present configure notify events to
1160 * track the geometry of the drawable
1163 dri3_update_drawable(__DRIdrawable
*driDrawable
,
1164 struct loader_dri3_drawable
*draw
)
1166 mtx_lock(&draw
->mtx
);
1167 if (draw
->first_init
) {
1168 xcb_get_geometry_cookie_t geom_cookie
;
1169 xcb_get_geometry_reply_t
*geom_reply
;
1170 xcb_void_cookie_t cookie
;
1171 xcb_generic_error_t
*error
;
1172 xcb_present_query_capabilities_cookie_t present_capabilities_cookie
;
1173 xcb_present_query_capabilities_reply_t
*present_capabilities_reply
;
1175 draw
->first_init
= false;
1177 /* Try to select for input on the window.
1179 * If the drawable is a window, this will get our events
1182 * Otherwise, we'll get a BadWindow error back from this request which
1183 * will let us know that the drawable is a pixmap instead.
1186 draw
->eid
= xcb_generate_id(draw
->conn
);
1188 xcb_present_select_input_checked(draw
->conn
, draw
->eid
, draw
->drawable
,
1189 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
1190 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
1191 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
1193 present_capabilities_cookie
=
1194 xcb_present_query_capabilities(draw
->conn
, draw
->drawable
);
1196 /* Create an XCB event queue to hold present events outside of the usual
1197 * application event queue
1199 draw
->special_event
= xcb_register_for_special_xge(draw
->conn
,
1203 geom_cookie
= xcb_get_geometry(draw
->conn
, draw
->drawable
);
1205 geom_reply
= xcb_get_geometry_reply(draw
->conn
, geom_cookie
, NULL
);
1208 mtx_unlock(&draw
->mtx
);
1212 draw
->width
= geom_reply
->width
;
1213 draw
->height
= geom_reply
->height
;
1214 draw
->depth
= geom_reply
->depth
;
1215 draw
->vtable
->set_drawable_size(draw
, draw
->width
, draw
->height
);
1219 draw
->is_pixmap
= false;
1221 /* Check to see if our select input call failed. If it failed with a
1222 * BadWindow error, then assume the drawable is a pixmap. Destroy the
1223 * special event queue created above and mark the drawable as a pixmap
1226 error
= xcb_request_check(draw
->conn
, cookie
);
1228 present_capabilities_reply
=
1229 xcb_present_query_capabilities_reply(draw
->conn
,
1230 present_capabilities_cookie
,
1233 if (present_capabilities_reply
) {
1234 draw
->present_capabilities
= present_capabilities_reply
->capabilities
;
1235 free(present_capabilities_reply
);
1237 draw
->present_capabilities
= 0;
1240 if (error
->error_code
!= BadWindow
) {
1242 mtx_unlock(&draw
->mtx
);
1245 draw
->is_pixmap
= true;
1246 xcb_unregister_for_special_event(draw
->conn
, draw
->special_event
);
1247 draw
->special_event
= NULL
;
1250 dri3_flush_present_events(draw
);
1251 mtx_unlock(&draw
->mtx
);
1256 loader_dri3_create_image(xcb_connection_t
*c
,
1257 xcb_dri3_buffer_from_pixmap_reply_t
*bp_reply
,
1258 unsigned int format
,
1259 __DRIscreen
*dri_screen
,
1260 const __DRIimageExtension
*image
,
1261 void *loaderPrivate
)
1264 __DRIimage
*image_planar
, *ret
;
1267 /* Get an FD for the pixmap object
1269 fds
= xcb_dri3_buffer_from_pixmap_reply_fds(c
, bp_reply
);
1271 stride
= bp_reply
->stride
;
1274 /* createImageFromFds creates a wrapper __DRIimage structure which
1275 * can deal with multiple planes for things like Yuv images. So, once
1276 * we've gotten the planar wrapper, pull the single plane out of it and
1277 * discard the wrapper.
1279 image_planar
= image
->createImageFromFds(dri_screen
,
1282 image_format_to_fourcc(format
),
1284 &stride
, &offset
, loaderPrivate
);
1289 ret
= image
->fromPlanar(image_planar
, 0, loaderPrivate
);
1291 image
->destroyImage(image_planar
);
1296 /** dri3_get_pixmap_buffer
1298 * Get the DRM object for a pixmap from the X server and
1299 * wrap that with a __DRIimage structure using createImageFromFds
1301 static struct loader_dri3_buffer
*
1302 dri3_get_pixmap_buffer(__DRIdrawable
*driDrawable
, unsigned int format
,
1303 enum loader_dri3_buffer_type buffer_type
,
1304 struct loader_dri3_drawable
*draw
)
1306 int buf_id
= loader_dri3_pixmap_buf_id(buffer_type
);
1307 struct loader_dri3_buffer
*buffer
= draw
->buffers
[buf_id
];
1308 xcb_drawable_t pixmap
;
1309 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie
;
1310 xcb_dri3_buffer_from_pixmap_reply_t
*bp_reply
;
1311 xcb_sync_fence_t sync_fence
;
1312 struct xshmfence
*shm_fence
;
1318 pixmap
= draw
->drawable
;
1320 buffer
= calloc(1, sizeof *buffer
);
1324 fence_fd
= xshmfence_alloc_shm();
1327 shm_fence
= xshmfence_map_shm(fence_fd
);
1328 if (shm_fence
== NULL
) {
1333 xcb_dri3_fence_from_fd(draw
->conn
,
1335 (sync_fence
= xcb_generate_id(draw
->conn
)),
1339 bp_cookie
= xcb_dri3_buffer_from_pixmap(draw
->conn
, pixmap
);
1340 bp_reply
= xcb_dri3_buffer_from_pixmap_reply(draw
->conn
, bp_cookie
, NULL
);
1344 buffer
->image
= loader_dri3_create_image(draw
->conn
, bp_reply
, format
,
1345 draw
->dri_screen
, draw
->ext
->image
,
1350 buffer
->pixmap
= pixmap
;
1351 buffer
->own_pixmap
= false;
1352 buffer
->width
= bp_reply
->width
;
1353 buffer
->height
= bp_reply
->height
;
1354 buffer
->shm_fence
= shm_fence
;
1355 buffer
->sync_fence
= sync_fence
;
1357 draw
->buffers
[buf_id
] = buffer
;
1365 xcb_sync_destroy_fence(draw
->conn
, sync_fence
);
1366 xshmfence_unmap_shm(shm_fence
);
1375 * Find a front or back buffer, allocating new ones as necessary
1377 static struct loader_dri3_buffer
*
1378 dri3_get_buffer(__DRIdrawable
*driDrawable
,
1379 unsigned int format
,
1380 enum loader_dri3_buffer_type buffer_type
,
1381 struct loader_dri3_drawable
*draw
)
1383 struct loader_dri3_buffer
*buffer
;
1386 if (buffer_type
== loader_dri3_buffer_back
) {
1387 draw
->back_format
= format
;
1389 buf_id
= dri3_find_back(draw
);
1394 buf_id
= LOADER_DRI3_FRONT_ID
;
1397 buffer
= draw
->buffers
[buf_id
];
1399 /* Allocate a new buffer if there isn't an old one, or if that
1400 * old one is the wrong size
1402 if (!buffer
|| buffer
->width
!= draw
->width
||
1403 buffer
->height
!= draw
->height
) {
1404 struct loader_dri3_buffer
*new_buffer
;
1406 /* Allocate the new buffers
1408 new_buffer
= dri3_alloc_render_buffer(draw
,
1416 /* When resizing, copy the contents of the old buffer, waiting for that
1417 * copy to complete using our fences before proceeding
1419 if ((buffer_type
== loader_dri3_buffer_back
||
1420 (buffer_type
== loader_dri3_buffer_front
&& draw
->have_fake_front
))
1423 /* Fill the new buffer with data from an old buffer */
1424 dri3_fence_await(draw
->conn
, draw
, buffer
);
1425 if (!loader_dri3_blit_image(draw
,
1428 0, 0, draw
->width
, draw
->height
,
1430 !buffer
->linear_buffer
) {
1431 dri3_fence_reset(draw
->conn
, new_buffer
);
1432 dri3_copy_area(draw
->conn
,
1435 dri3_drawable_gc(draw
),
1437 draw
->width
, draw
->height
);
1438 dri3_fence_trigger(draw
->conn
, new_buffer
);
1440 dri3_free_render_buffer(draw
, buffer
);
1441 } else if (buffer_type
== loader_dri3_buffer_front
) {
1442 /* Fill the new fake front with data from a real front */
1443 loader_dri3_swapbuffer_barrier(draw
);
1444 dri3_fence_reset(draw
->conn
, new_buffer
);
1445 dri3_copy_area(draw
->conn
,
1448 dri3_drawable_gc(draw
),
1450 draw
->width
, draw
->height
);
1451 dri3_fence_trigger(draw
->conn
, new_buffer
);
1453 if (new_buffer
->linear_buffer
) {
1454 dri3_fence_await(draw
->conn
, draw
, new_buffer
);
1455 (void) loader_dri3_blit_image(draw
,
1457 new_buffer
->linear_buffer
,
1458 0, 0, draw
->width
, draw
->height
,
1462 buffer
= new_buffer
;
1463 draw
->buffers
[buf_id
] = buffer
;
1465 dri3_fence_await(draw
->conn
, draw
, buffer
);
1468 * Do we need to preserve the content of a previous buffer?
1470 * Note that this blit is needed only to avoid a wait for a buffer that
1471 * is currently in the flip chain or being scanned out from. That's really
1472 * a tradeoff. If we're ok with the wait we can reduce the number of back
1473 * buffers to 1 for SWAP_EXCHANGE, and 1 for SWAP_COPY,
1474 * but in the latter case we must disallow page-flipping.
1476 if (buffer_type
== loader_dri3_buffer_back
&&
1477 draw
->cur_blit_source
!= -1 &&
1478 draw
->buffers
[draw
->cur_blit_source
] &&
1479 buffer
!= draw
->buffers
[draw
->cur_blit_source
]) {
1481 struct loader_dri3_buffer
*source
= draw
->buffers
[draw
->cur_blit_source
];
1483 /* Avoid flushing here. Will propably do good for tiling hardware. */
1484 (void) loader_dri3_blit_image(draw
,
1487 0, 0, draw
->width
, draw
->height
,
1489 buffer
->last_swap
= source
->last_swap
;
1490 draw
->cur_blit_source
= -1;
1492 /* Return the requested buffer */
1496 /** dri3_free_buffers
1498 * Free the front bufffer or all of the back buffers. Used
1499 * when the application changes which buffers it needs
1502 dri3_free_buffers(__DRIdrawable
*driDrawable
,
1503 enum loader_dri3_buffer_type buffer_type
,
1504 struct loader_dri3_drawable
*draw
)
1506 struct loader_dri3_buffer
*buffer
;
1511 switch (buffer_type
) {
1512 case loader_dri3_buffer_back
:
1513 first_id
= LOADER_DRI3_BACK_ID(0);
1514 n_id
= LOADER_DRI3_MAX_BACK
;
1515 draw
->cur_blit_source
= -1;
1517 case loader_dri3_buffer_front
:
1518 first_id
= LOADER_DRI3_FRONT_ID
;
1519 /* Don't free a fake front holding new backbuffer content. */
1520 n_id
= (draw
->cur_blit_source
== LOADER_DRI3_FRONT_ID
) ? 0 : 1;
1523 for (buf_id
= first_id
; buf_id
< first_id
+ n_id
; buf_id
++) {
1524 buffer
= draw
->buffers
[buf_id
];
1526 dri3_free_render_buffer(draw
, buffer
);
1527 draw
->buffers
[buf_id
] = NULL
;
1532 /** loader_dri3_get_buffers
1534 * The published buffer allocation API.
1535 * Returns all of the necessary buffers, allocating
1539 loader_dri3_get_buffers(__DRIdrawable
*driDrawable
,
1540 unsigned int format
,
1542 void *loaderPrivate
,
1543 uint32_t buffer_mask
,
1544 struct __DRIimageList
*buffers
)
1546 struct loader_dri3_drawable
*draw
= loaderPrivate
;
1547 struct loader_dri3_buffer
*front
, *back
;
1549 buffers
->image_mask
= 0;
1550 buffers
->front
= NULL
;
1551 buffers
->back
= NULL
;
1556 if (!dri3_update_drawable(driDrawable
, draw
))
1559 /* pixmaps always have front buffers.
1560 * Exchange swaps also mandate fake front buffers.
1562 if (draw
->is_pixmap
|| draw
->swap_method
== __DRI_ATTRIB_SWAP_EXCHANGE
)
1563 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1565 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1566 /* All pixmaps are owned by the server gpu.
1567 * When we use a different gpu, we can't use the pixmap
1568 * as buffer since it is potentially tiled a way
1569 * our device can't understand. In this case, use
1570 * a fake front buffer. Hopefully the pixmap
1571 * content will get synced with the fake front
1574 if (draw
->is_pixmap
&& !draw
->is_different_gpu
)
1575 front
= dri3_get_pixmap_buffer(driDrawable
,
1577 loader_dri3_buffer_front
,
1580 front
= dri3_get_buffer(driDrawable
,
1582 loader_dri3_buffer_front
,
1588 dri3_free_buffers(driDrawable
, loader_dri3_buffer_front
, draw
);
1589 draw
->have_fake_front
= 0;
1592 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1593 back
= dri3_get_buffer(driDrawable
,
1595 loader_dri3_buffer_back
,
1599 draw
->have_back
= 1;
1601 dri3_free_buffers(driDrawable
, loader_dri3_buffer_back
, draw
);
1602 draw
->have_back
= 0;
1606 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1607 buffers
->front
= front
->image
;
1608 draw
->have_fake_front
= draw
->is_different_gpu
|| !draw
->is_pixmap
;
1612 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1613 buffers
->back
= back
->image
;
1616 draw
->stamp
= stamp
;
1621 /** loader_dri3_update_drawable_geometry
1623 * Get the current drawable geometry.
1626 loader_dri3_update_drawable_geometry(struct loader_dri3_drawable
*draw
)
1628 xcb_get_geometry_cookie_t geom_cookie
;
1629 xcb_get_geometry_reply_t
*geom_reply
;
1631 geom_cookie
= xcb_get_geometry(draw
->conn
, draw
->drawable
);
1633 geom_reply
= xcb_get_geometry_reply(draw
->conn
, geom_cookie
, NULL
);
1636 draw
->width
= geom_reply
->width
;
1637 draw
->height
= geom_reply
->height
;
1638 draw
->vtable
->set_drawable_size(draw
, draw
->width
, draw
->height
);
1639 draw
->ext
->flush
->invalidate(draw
->dri_drawable
);
1647 * Make sure the server has flushed all pending swap buffers to hardware
1648 * for this drawable. Ideally we'd want to send an X protocol request to
1649 * have the server block our connection until the swaps are complete. That
1650 * would avoid the potential round-trip here.
1653 loader_dri3_swapbuffer_barrier(struct loader_dri3_drawable
*draw
)
1655 int64_t ust
, msc
, sbc
;
1657 (void) loader_dri3_wait_for_sbc(draw
, 0, &ust
, &msc
, &sbc
);
1661 * Perform any cleanup associated with a close screen operation.
1662 * \param dri_screen[in,out] Pointer to __DRIscreen about to be closed.
1664 * This function destroys the screen's cached swap context if any.
1667 loader_dri3_close_screen(__DRIscreen
*dri_screen
)
1669 mtx_lock(&blit_context
.mtx
);
1670 if (blit_context
.ctx
&& blit_context
.cur_screen
== dri_screen
) {
1671 blit_context
.core
->destroyContext(blit_context
.ctx
);
1672 blit_context
.ctx
= NULL
;
1674 mtx_unlock(&blit_context
.mtx
);
1678 * Find a backbuffer slot - potentially allocating a back buffer
1680 * \param draw[in,out] Pointer to the drawable for which to find back.
1681 * \return Pointer to a new back buffer or NULL if allocation failed or was
1684 * Find a potentially new back buffer, and if it's not been allocated yet and
1685 * in addition needs initializing, then try to allocate and initialize it.
1688 static struct loader_dri3_buffer
*
1689 dri3_find_back_alloc(struct loader_dri3_drawable
*draw
)
1691 struct loader_dri3_buffer
*back
;
1694 id
= dri3_find_back(draw
);
1698 back
= draw
->buffers
[id
];
1699 /* Allocate a new back if we haven't got one */
1700 if (!back
&& draw
->back_format
!= __DRI_IMAGE_FORMAT_NONE
&&
1701 dri3_update_drawable(draw
->dri_drawable
, draw
))
1702 back
= dri3_alloc_render_buffer(draw
, draw
->back_format
,
1703 draw
->width
, draw
->height
, draw
->depth
);
1708 draw
->buffers
[id
] = back
;
1710 /* If necessary, prefill the back with data according to swap_method mode. */
1711 if (draw
->cur_blit_source
!= -1 &&
1712 draw
->buffers
[draw
->cur_blit_source
] &&
1713 back
!= draw
->buffers
[draw
->cur_blit_source
]) {
1714 struct loader_dri3_buffer
*source
= draw
->buffers
[draw
->cur_blit_source
];
1716 dri3_fence_await(draw
->conn
, draw
, source
);
1717 dri3_fence_await(draw
->conn
, draw
, back
);
1718 (void) loader_dri3_blit_image(draw
,
1721 0, 0, draw
->width
, draw
->height
,
1723 back
->last_swap
= source
->last_swap
;
1724 draw
->cur_blit_source
= -1;