1 /**************************************************************************
3 * Copyright 2016 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 #include <X11/Xlib-xcb.h>
31 #include <X11/xshmfence.h>
33 #include <xcb/present.h>
34 #include <xcb/xfixes.h>
38 #include "pipe/p_screen.h"
39 #include "pipe/p_state.h"
40 #include "pipe-loader/pipe_loader.h"
42 #include "util/u_memory.h"
43 #include "util/u_inlines.h"
45 #include "vl/vl_compositor.h"
46 #include "vl/vl_winsys.h"
48 #define BACK_BUFFER_NUM 3
52 struct pipe_resource
*texture
;
53 struct pipe_resource
*linear_texture
;
57 struct xshmfence
*shm_fence
;
60 uint32_t width
, height
, pitch
;
65 struct vl_screen base
;
66 xcb_connection_t
*conn
;
67 xcb_drawable_t drawable
;
69 uint32_t width
, height
, depth
;
71 xcb_present_event_t eid
;
72 xcb_special_event_t
*special_event
;
74 struct pipe_context
*pipe
;
75 struct pipe_resource
*output_texture
;
76 uint32_t clip_width
, clip_height
;
78 struct vl_dri3_buffer
*back_buffers
[BACK_BUFFER_NUM
];
82 struct u_rect dirty_areas
[BACK_BUFFER_NUM
];
84 struct vl_dri3_buffer
*front_buffer
;
87 uint32_t send_msc_serial
, recv_msc_serial
;
88 uint64_t send_sbc
, recv_sbc
;
89 int64_t last_ust
, ns_frame
, last_msc
, next_msc
;
92 bool is_different_gpu
;
96 dri3_free_front_buffer(struct vl_dri3_screen
*scrn
,
97 struct vl_dri3_buffer
*buffer
)
99 xcb_sync_destroy_fence(scrn
->conn
, buffer
->sync_fence
);
100 xshmfence_unmap_shm(buffer
->shm_fence
);
101 pipe_resource_reference(&buffer
->texture
, NULL
);
106 dri3_free_back_buffer(struct vl_dri3_screen
*scrn
,
107 struct vl_dri3_buffer
*buffer
)
109 xcb_free_pixmap(scrn
->conn
, buffer
->pixmap
);
110 xcb_sync_destroy_fence(scrn
->conn
, buffer
->sync_fence
);
111 xshmfence_unmap_shm(buffer
->shm_fence
);
112 if (!scrn
->output_texture
)
113 pipe_resource_reference(&buffer
->texture
, NULL
);
114 if (buffer
->linear_texture
)
115 pipe_resource_reference(&buffer
->linear_texture
, NULL
);
120 dri3_handle_stamps(struct vl_dri3_screen
*scrn
, uint64_t ust
, uint64_t msc
)
122 int64_t ust_ns
= ust
* 1000;
124 if (scrn
->last_ust
&& (ust_ns
> scrn
->last_ust
) &&
125 scrn
->last_msc
&& (msc
> scrn
->last_msc
))
126 scrn
->ns_frame
= (ust_ns
- scrn
->last_ust
) / (msc
- scrn
->last_msc
);
128 scrn
->last_ust
= ust_ns
;
129 scrn
->last_msc
= msc
;
133 dri3_handle_present_event(struct vl_dri3_screen
*scrn
,
134 xcb_present_generic_event_t
*ge
)
136 switch (ge
->evtype
) {
137 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
138 xcb_present_configure_notify_event_t
*ce
= (void *) ge
;
139 scrn
->width
= ce
->width
;
140 scrn
->height
= ce
->height
;
143 case XCB_PRESENT_COMPLETE_NOTIFY
: {
144 xcb_present_complete_notify_event_t
*ce
= (void *) ge
;
145 if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
) {
146 scrn
->recv_sbc
= (scrn
->send_sbc
& 0xffffffff00000000LL
) | ce
->serial
;
147 if (scrn
->recv_sbc
> scrn
->send_sbc
)
148 scrn
->recv_sbc
-= 0x100000000;
149 dri3_handle_stamps(scrn
, ce
->ust
, ce
->msc
);
150 } else if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_NOTIFY_MSC
) {
151 scrn
->recv_msc_serial
= ce
->serial
;
152 dri3_handle_stamps(scrn
, ce
->ust
, ce
->msc
);
156 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
157 xcb_present_idle_notify_event_t
*ie
= (void *) ge
;
159 for (b
= 0; b
< BACK_BUFFER_NUM
; b
++) {
160 struct vl_dri3_buffer
*buf
= scrn
->back_buffers
[b
];
161 if (buf
&& buf
->pixmap
== ie
->pixmap
) {
173 dri3_flush_present_events(struct vl_dri3_screen
*scrn
)
175 if (scrn
->special_event
) {
176 xcb_generic_event_t
*ev
;
177 while ((ev
= xcb_poll_for_special_event(
178 scrn
->conn
, scrn
->special_event
)) != NULL
)
179 dri3_handle_present_event(scrn
, (xcb_present_generic_event_t
*)ev
);
184 dri3_wait_present_events(struct vl_dri3_screen
*scrn
)
186 if (scrn
->special_event
) {
187 xcb_generic_event_t
*ev
;
188 ev
= xcb_wait_for_special_event(scrn
->conn
, scrn
->special_event
);
191 dri3_handle_present_event(scrn
, (xcb_present_generic_event_t
*)ev
);
198 dri3_find_back(struct vl_dri3_screen
*scrn
)
203 for (b
= 0; b
< BACK_BUFFER_NUM
; b
++) {
204 int id
= (b
+ scrn
->cur_back
) % BACK_BUFFER_NUM
;
205 struct vl_dri3_buffer
*buffer
= scrn
->back_buffers
[id
];
206 if (!buffer
|| !buffer
->busy
)
209 xcb_flush(scrn
->conn
);
210 if (!dri3_wait_present_events(scrn
))
215 static struct vl_dri3_buffer
*
216 dri3_alloc_back_buffer(struct vl_dri3_screen
*scrn
)
218 struct vl_dri3_buffer
*buffer
;
220 xcb_sync_fence_t sync_fence
;
221 struct xshmfence
*shm_fence
;
222 int buffer_fd
, fence_fd
;
223 struct pipe_resource templ
, *pixmap_buffer_texture
;
224 struct winsys_handle whandle
;
227 buffer
= CALLOC_STRUCT(vl_dri3_buffer
);
231 fence_fd
= xshmfence_alloc_shm();
235 shm_fence
= xshmfence_map_shm(fence_fd
);
239 memset(&templ
, 0, sizeof(templ
));
240 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
241 templ
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
242 templ
.target
= PIPE_TEXTURE_2D
;
243 templ
.last_level
= 0;
244 templ
.width0
= (scrn
->output_texture
) ?
245 scrn
->output_texture
->width0
: scrn
->width
;
246 templ
.height0
= (scrn
->output_texture
) ?
247 scrn
->output_texture
->height0
: scrn
->height
;
249 templ
.array_size
= 1;
251 if (scrn
->is_different_gpu
) {
252 buffer
->texture
= (scrn
->output_texture
) ? scrn
->output_texture
:
253 scrn
->base
.pscreen
->resource_create(scrn
->base
.pscreen
, &templ
);
254 if (!buffer
->texture
)
257 templ
.bind
|= PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
|
259 buffer
->linear_texture
=
260 scrn
->base
.pscreen
->resource_create(scrn
->base
.pscreen
, &templ
);
261 pixmap_buffer_texture
= buffer
->linear_texture
;
263 if (!buffer
->linear_texture
)
264 goto no_linear_texture
;
266 templ
.bind
|= PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
;
267 buffer
->texture
= (scrn
->output_texture
) ? scrn
->output_texture
:
268 scrn
->base
.pscreen
->resource_create(scrn
->base
.pscreen
, &templ
);
269 if (!buffer
->texture
)
271 pixmap_buffer_texture
= buffer
->texture
;
273 memset(&whandle
, 0, sizeof(whandle
));
274 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
275 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
276 scrn
->base
.pscreen
->resource_get_handle(scrn
->base
.pscreen
, NULL
,
277 pixmap_buffer_texture
, &whandle
,
279 buffer_fd
= whandle
.handle
;
280 buffer
->pitch
= whandle
.stride
;
281 buffer
->width
= templ
.width0
;
282 buffer
->height
= templ
.height0
;
284 xcb_dri3_pixmap_from_buffer(scrn
->conn
,
285 (pixmap
= xcb_generate_id(scrn
->conn
)),
288 buffer
->width
, buffer
->height
, buffer
->pitch
,
291 xcb_dri3_fence_from_fd(scrn
->conn
,
293 (sync_fence
= xcb_generate_id(scrn
->conn
)),
297 buffer
->pixmap
= pixmap
;
298 buffer
->sync_fence
= sync_fence
;
299 buffer
->shm_fence
= shm_fence
;
301 xshmfence_trigger(buffer
->shm_fence
);
306 pipe_resource_reference(&buffer
->texture
, NULL
);
308 xshmfence_unmap_shm(shm_fence
);
316 static struct vl_dri3_buffer
*
317 dri3_get_back_buffer(struct vl_dri3_screen
*scrn
)
319 struct vl_dri3_buffer
*buffer
;
320 struct pipe_resource
*texture
= NULL
;
321 bool allocate_new_buffer
= false;
326 scrn
->cur_back
= dri3_find_back(scrn
);
327 if (scrn
->cur_back
< 0)
329 buffer
= scrn
->back_buffers
[scrn
->cur_back
];
331 if (scrn
->output_texture
) {
332 if (!buffer
|| buffer
->width
< scrn
->width
||
333 buffer
->height
< scrn
->height
)
334 allocate_new_buffer
= true;
335 else if (scrn
->is_different_gpu
)
336 /* In case of different gpu we can reuse the linear
337 * texture so we only need to set the external
338 * texture for copying
340 buffer
->texture
= scrn
->output_texture
;
342 /* In case of a single gpu we search if the texture is
343 * already present as buffer if not we get the
344 * handle and pixmap for the texture that is set
346 for (b
= 0; b
< BACK_BUFFER_NUM
; b
++) {
347 id
= (b
+ scrn
->cur_back
) % BACK_BUFFER_NUM
;
348 buffer
= scrn
->back_buffers
[id
];
349 if (buffer
&& !buffer
->busy
&&
350 buffer
->texture
== scrn
->output_texture
) {
356 if (b
== BACK_BUFFER_NUM
) {
357 allocate_new_buffer
= true;
358 scrn
->cur_back
= scrn
->next_back
;
359 scrn
->next_back
= (scrn
->next_back
+ 1) % BACK_BUFFER_NUM
;
360 buffer
= scrn
->back_buffers
[scrn
->cur_back
];
365 if (!buffer
|| buffer
->width
!= scrn
->width
||
366 buffer
->height
!= scrn
->height
)
367 allocate_new_buffer
= true;
370 if (allocate_new_buffer
) {
371 struct vl_dri3_buffer
*new_buffer
;
373 new_buffer
= dri3_alloc_back_buffer(scrn
);
378 dri3_free_back_buffer(scrn
, buffer
);
380 if (!scrn
->output_texture
)
381 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[scrn
->cur_back
]);
383 scrn
->back_buffers
[scrn
->cur_back
] = buffer
;
386 pipe_resource_reference(&texture
, buffer
->texture
);
387 xcb_flush(scrn
->conn
);
388 xshmfence_await(buffer
->shm_fence
);
394 dri3_set_drawable(struct vl_dri3_screen
*scrn
, Drawable drawable
)
396 xcb_get_geometry_cookie_t geom_cookie
;
397 xcb_get_geometry_reply_t
*geom_reply
;
398 xcb_void_cookie_t cookie
;
399 xcb_generic_error_t
*error
;
404 if (scrn
->drawable
== drawable
)
407 scrn
->drawable
= drawable
;
409 geom_cookie
= xcb_get_geometry(scrn
->conn
, scrn
->drawable
);
410 geom_reply
= xcb_get_geometry_reply(scrn
->conn
, geom_cookie
, NULL
);
414 scrn
->width
= geom_reply
->width
;
415 scrn
->height
= geom_reply
->height
;
416 scrn
->depth
= geom_reply
->depth
;
419 if (scrn
->special_event
) {
420 xcb_unregister_for_special_event(scrn
->conn
, scrn
->special_event
);
421 scrn
->special_event
= NULL
;
422 cookie
= xcb_present_select_input_checked(scrn
->conn
, scrn
->eid
,
424 XCB_PRESENT_EVENT_MASK_NO_EVENT
);
425 xcb_discard_reply(scrn
->conn
, cookie
.sequence
);
428 scrn
->is_pixmap
= false;
429 scrn
->eid
= xcb_generate_id(scrn
->conn
);
431 xcb_present_select_input_checked(scrn
->conn
, scrn
->eid
, scrn
->drawable
,
432 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
433 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
434 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
436 error
= xcb_request_check(scrn
->conn
, cookie
);
438 if (error
->error_code
!= BadWindow
)
441 scrn
->is_pixmap
= true;
442 if (scrn
->front_buffer
) {
443 dri3_free_front_buffer(scrn
, scrn
->front_buffer
);
444 scrn
->front_buffer
= NULL
;
449 scrn
->special_event
=
450 xcb_register_for_special_xge(scrn
->conn
, &xcb_present_id
, scrn
->eid
, 0);
452 dri3_flush_present_events(scrn
);
457 static struct vl_dri3_buffer
*
458 dri3_get_front_buffer(struct vl_dri3_screen
*scrn
)
460 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie
;
461 xcb_dri3_buffer_from_pixmap_reply_t
*bp_reply
;
462 xcb_sync_fence_t sync_fence
;
463 struct xshmfence
*shm_fence
;
465 struct winsys_handle whandle
;
466 struct pipe_resource templ
, *texture
= NULL
;
468 if (scrn
->front_buffer
) {
469 pipe_resource_reference(&texture
, scrn
->front_buffer
->texture
);
470 return scrn
->front_buffer
;
473 scrn
->front_buffer
= CALLOC_STRUCT(vl_dri3_buffer
);
474 if (!scrn
->front_buffer
)
477 fence_fd
= xshmfence_alloc_shm();
481 shm_fence
= xshmfence_map_shm(fence_fd
);
485 bp_cookie
= xcb_dri3_buffer_from_pixmap(scrn
->conn
, scrn
->drawable
);
486 bp_reply
= xcb_dri3_buffer_from_pixmap_reply(scrn
->conn
, bp_cookie
, NULL
);
490 fds
= xcb_dri3_buffer_from_pixmap_reply_fds(scrn
->conn
, bp_reply
);
494 memset(&whandle
, 0, sizeof(whandle
));
495 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
496 whandle
.handle
= (unsigned)fds
[0];
497 whandle
.stride
= bp_reply
->stride
;
498 memset(&templ
, 0, sizeof(templ
));
499 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
500 templ
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
501 templ
.target
= PIPE_TEXTURE_2D
;
502 templ
.last_level
= 0;
503 templ
.width0
= bp_reply
->width
;
504 templ
.height0
= bp_reply
->height
;
506 templ
.array_size
= 1;
507 scrn
->front_buffer
->texture
=
508 scrn
->base
.pscreen
->resource_from_handle(scrn
->base
.pscreen
,
510 PIPE_HANDLE_USAGE_READ_WRITE
);
512 if (!scrn
->front_buffer
->texture
)
515 xcb_dri3_fence_from_fd(scrn
->conn
,
517 (sync_fence
= xcb_generate_id(scrn
->conn
)),
521 pipe_resource_reference(&texture
, scrn
->front_buffer
->texture
);
522 scrn
->front_buffer
->pixmap
= scrn
->drawable
;
523 scrn
->front_buffer
->width
= bp_reply
->width
;
524 scrn
->front_buffer
->height
= bp_reply
->height
;
525 scrn
->front_buffer
->shm_fence
= shm_fence
;
526 scrn
->front_buffer
->sync_fence
= sync_fence
;
529 return scrn
->front_buffer
;
534 xshmfence_unmap_shm(shm_fence
);
538 FREE(scrn
->front_buffer
);
543 vl_dri3_flush_frontbuffer(struct pipe_screen
*screen
,
544 struct pipe_resource
*resource
,
545 unsigned level
, unsigned layer
,
546 void *context_private
, struct pipe_box
*sub_box
)
548 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)context_private
;
549 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
550 struct vl_dri3_buffer
*back
;
551 struct pipe_box src_box
;
552 xcb_xfixes_region_t region
;
553 xcb_rectangle_t rectangle
;
555 back
= scrn
->back_buffers
[scrn
->cur_back
];
560 while (scrn
->special_event
&& scrn
->recv_sbc
< scrn
->send_sbc
)
561 if (!dri3_wait_present_events(scrn
))
567 rectangle
.width
= (scrn
->output_texture
) ? scrn
->clip_width
: scrn
->width
;
568 rectangle
.height
= (scrn
->output_texture
) ? scrn
->clip_height
: scrn
->height
;
570 region
= xcb_generate_id(scrn
->conn
);
571 xcb_xfixes_create_region(scrn
->conn
, region
, 2, &rectangle
);
573 if (scrn
->is_different_gpu
) {
574 u_box_origin_2d(back
->width
, back
->height
, &src_box
);
575 scrn
->pipe
->resource_copy_region(scrn
->pipe
,
576 back
->linear_texture
,
581 scrn
->pipe
->flush(scrn
->pipe
, NULL
, 0);
583 xshmfence_reset(back
->shm_fence
);
586 xcb_present_pixmap(scrn
->conn
,
589 (uint32_t)(++scrn
->send_sbc
),
597 xcb_flush(scrn
->conn
);
599 scrn
->flushed
= true;
604 static struct pipe_resource
*
605 vl_dri3_screen_texture_from_drawable(struct vl_screen
*vscreen
, void *drawable
)
607 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
608 struct vl_dri3_buffer
*buffer
;
612 if (!dri3_set_drawable(scrn
, (Drawable
)drawable
))
616 while (scrn
->special_event
&& scrn
->recv_sbc
< scrn
->send_sbc
)
617 if (!dri3_wait_present_events(scrn
))
620 scrn
->flushed
= false;
622 buffer
= (scrn
->is_pixmap
) ?
623 dri3_get_front_buffer(scrn
) :
624 dri3_get_back_buffer(scrn
);
628 return buffer
->texture
;
631 static struct u_rect
*
632 vl_dri3_screen_get_dirty_area(struct vl_screen
*vscreen
)
634 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
638 return &scrn
->dirty_areas
[scrn
->cur_back
];
642 vl_dri3_screen_get_timestamp(struct vl_screen
*vscreen
, void *drawable
)
644 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
648 if (!dri3_set_drawable(scrn
, (Drawable
)drawable
))
651 if (!scrn
->last_ust
) {
652 xcb_present_notify_msc(scrn
->conn
,
654 ++scrn
->send_msc_serial
,
656 xcb_flush(scrn
->conn
);
658 while (scrn
->special_event
&&
659 scrn
->send_msc_serial
> scrn
->recv_msc_serial
) {
660 if (!dri3_wait_present_events(scrn
))
665 return scrn
->last_ust
;
669 vl_dri3_screen_set_next_timestamp(struct vl_screen
*vscreen
, uint64_t stamp
)
671 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
675 if (stamp
&& scrn
->last_ust
&& scrn
->ns_frame
&& scrn
->last_msc
)
676 scrn
->next_msc
= ((int64_t)stamp
- scrn
->last_ust
+ scrn
->ns_frame
/2) /
677 scrn
->ns_frame
+ scrn
->last_msc
;
683 vl_dri3_screen_get_private(struct vl_screen
*vscreen
)
689 vl_dri3_screen_set_back_texture_from_output(struct vl_screen
*vscreen
,
690 struct pipe_resource
*buffer
,
691 uint32_t width
, uint32_t height
)
693 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
697 scrn
->output_texture
= buffer
;
698 scrn
->clip_width
= (width
) ? width
: scrn
->width
;
699 scrn
->clip_height
= (height
) ? height
: scrn
->height
;
703 vl_dri3_screen_destroy(struct vl_screen
*vscreen
)
705 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
710 dri3_flush_present_events(scrn
);
712 if (scrn
->front_buffer
) {
713 dri3_free_front_buffer(scrn
, scrn
->front_buffer
);
714 scrn
->front_buffer
= NULL
;
718 for (i
= 0; i
< BACK_BUFFER_NUM
; ++i
) {
719 if (scrn
->back_buffers
[i
]) {
720 dri3_free_back_buffer(scrn
, scrn
->back_buffers
[i
]);
721 scrn
->back_buffers
[i
] = NULL
;
725 if (scrn
->special_event
) {
726 xcb_void_cookie_t cookie
=
727 xcb_present_select_input_checked(scrn
->conn
, scrn
->eid
,
729 XCB_PRESENT_EVENT_MASK_NO_EVENT
);
731 xcb_discard_reply(scrn
->conn
, cookie
.sequence
);
732 xcb_unregister_for_special_event(scrn
->conn
, scrn
->special_event
);
734 scrn
->pipe
->destroy(scrn
->pipe
);
735 scrn
->base
.pscreen
->destroy(scrn
->base
.pscreen
);
736 pipe_loader_release(&scrn
->base
.dev
, 1);
743 vl_dri3_screen_create(Display
*display
, int screen
)
745 struct vl_dri3_screen
*scrn
;
746 const xcb_query_extension_reply_t
*extension
;
747 xcb_dri3_open_cookie_t open_cookie
;
748 xcb_dri3_open_reply_t
*open_reply
;
749 xcb_get_geometry_cookie_t geom_cookie
;
750 xcb_get_geometry_reply_t
*geom_reply
;
751 xcb_xfixes_query_version_cookie_t xfixes_cookie
;
752 xcb_xfixes_query_version_reply_t
*xfixes_reply
;
753 xcb_generic_error_t
*error
;
758 scrn
= CALLOC_STRUCT(vl_dri3_screen
);
762 scrn
->conn
= XGetXCBConnection(display
);
766 xcb_prefetch_extension_data(scrn
->conn
, &xcb_dri3_id
);
767 xcb_prefetch_extension_data(scrn
->conn
, &xcb_present_id
);
768 xcb_prefetch_extension_data (scrn
->conn
, &xcb_xfixes_id
);
769 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_dri3_id
);
770 if (!(extension
&& extension
->present
))
772 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_present_id
);
773 if (!(extension
&& extension
->present
))
775 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_xfixes_id
);
776 if (!(extension
&& extension
->present
))
779 xfixes_cookie
= xcb_xfixes_query_version(scrn
->conn
, XCB_XFIXES_MAJOR_VERSION
,
780 XCB_XFIXES_MINOR_VERSION
);
781 xfixes_reply
= xcb_xfixes_query_version_reply(scrn
->conn
, xfixes_cookie
, &error
);
782 if (!xfixes_reply
|| error
|| xfixes_reply
->major_version
< 2) {
789 open_cookie
= xcb_dri3_open(scrn
->conn
, RootWindow(display
, screen
), None
);
790 open_reply
= xcb_dri3_open_reply(scrn
->conn
, open_cookie
, NULL
);
793 if (open_reply
->nfd
!= 1) {
798 fd
= xcb_dri3_open_reply_fds(scrn
->conn
, open_reply
)[0];
803 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
806 fd
= loader_get_user_preferred_fd(fd
, &scrn
->is_different_gpu
);
808 geom_cookie
= xcb_get_geometry(scrn
->conn
, RootWindow(display
, screen
));
809 geom_reply
= xcb_get_geometry_reply(scrn
->conn
, geom_cookie
, NULL
);
812 /* TODO support depth other than 24 */
813 if (geom_reply
->depth
!= 24) {
819 if (pipe_loader_drm_probe_fd(&scrn
->base
.dev
, fd
))
820 scrn
->base
.pscreen
= pipe_loader_create_screen(scrn
->base
.dev
);
822 if (!scrn
->base
.pscreen
)
825 scrn
->pipe
= scrn
->base
.pscreen
->context_create(scrn
->base
.pscreen
,
830 scrn
->base
.destroy
= vl_dri3_screen_destroy
;
831 scrn
->base
.texture_from_drawable
= vl_dri3_screen_texture_from_drawable
;
832 scrn
->base
.get_dirty_area
= vl_dri3_screen_get_dirty_area
;
833 scrn
->base
.get_timestamp
= vl_dri3_screen_get_timestamp
;
834 scrn
->base
.set_next_timestamp
= vl_dri3_screen_set_next_timestamp
;
835 scrn
->base
.get_private
= vl_dri3_screen_get_private
;
836 scrn
->base
.pscreen
->flush_frontbuffer
= vl_dri3_flush_frontbuffer
;
837 scrn
->base
.set_back_texture_from_output
= vl_dri3_screen_set_back_texture_from_output
;
843 scrn
->base
.pscreen
->destroy(scrn
->base
.pscreen
);
845 if (scrn
->base
.dev
) {
846 pipe_loader_release(&scrn
->base
.dev
, 1);