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>
37 #include "pipe/p_screen.h"
38 #include "pipe/p_state.h"
39 #include "pipe-loader/pipe_loader.h"
41 #include "util/u_memory.h"
42 #include "util/u_inlines.h"
44 #include "vl/vl_compositor.h"
45 #include "vl/vl_winsys.h"
47 #define BACK_BUFFER_NUM 3
51 struct pipe_resource
*texture
;
55 struct xshmfence
*shm_fence
;
58 uint32_t width
, height
, pitch
;
63 struct vl_screen base
;
64 xcb_connection_t
*conn
;
65 xcb_drawable_t drawable
;
67 uint32_t width
, height
, depth
;
69 xcb_special_event_t
*special_event
;
71 struct vl_dri3_buffer
*back_buffers
[BACK_BUFFER_NUM
];
74 struct u_rect dirty_areas
[BACK_BUFFER_NUM
];
76 struct vl_dri3_buffer
*front_buffer
;
79 uint32_t send_msc_serial
, recv_msc_serial
;
80 uint64_t send_sbc
, recv_sbc
;
81 int64_t last_ust
, ns_frame
, last_msc
, next_msc
;
87 dri3_free_front_buffer(struct vl_dri3_screen
*scrn
,
88 struct vl_dri3_buffer
*buffer
)
90 xcb_sync_destroy_fence(scrn
->conn
, buffer
->sync_fence
);
91 xshmfence_unmap_shm(buffer
->shm_fence
);
96 dri3_free_back_buffer(struct vl_dri3_screen
*scrn
,
97 struct vl_dri3_buffer
*buffer
)
99 xcb_free_pixmap(scrn
->conn
, buffer
->pixmap
);
100 xcb_sync_destroy_fence(scrn
->conn
, buffer
->sync_fence
);
101 xshmfence_unmap_shm(buffer
->shm_fence
);
102 pipe_resource_reference(&buffer
->texture
, NULL
);
107 dri3_handle_stamps(struct vl_dri3_screen
*scrn
, uint64_t ust
, uint64_t msc
)
109 int64_t ust_ns
= ust
* 1000;
111 if (scrn
->last_ust
&& (ust_ns
> scrn
->last_ust
) &&
112 scrn
->last_msc
&& (msc
> scrn
->last_msc
))
113 scrn
->ns_frame
= (ust_ns
- scrn
->last_ust
) / (msc
- scrn
->last_msc
);
115 scrn
->last_ust
= ust_ns
;
116 scrn
->last_msc
= msc
;
120 dri3_handle_present_event(struct vl_dri3_screen
*scrn
,
121 xcb_present_generic_event_t
*ge
)
123 switch (ge
->evtype
) {
124 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
125 xcb_present_configure_notify_event_t
*ce
= (void *) ge
;
126 scrn
->width
= ce
->width
;
127 scrn
->height
= ce
->height
;
130 case XCB_PRESENT_COMPLETE_NOTIFY
: {
131 xcb_present_complete_notify_event_t
*ce
= (void *) ge
;
132 if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
) {
133 scrn
->recv_sbc
= (scrn
->send_sbc
& 0xffffffff00000000LL
) | ce
->serial
;
134 if (scrn
->recv_sbc
> scrn
->send_sbc
)
135 scrn
->recv_sbc
-= 0x100000000;
136 dri3_handle_stamps(scrn
, ce
->ust
, ce
->msc
);
137 } else if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_NOTIFY_MSC
) {
138 scrn
->recv_msc_serial
= ce
->serial
;
139 dri3_handle_stamps(scrn
, ce
->ust
, ce
->msc
);
143 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
144 xcb_present_idle_notify_event_t
*ie
= (void *) ge
;
146 for (b
= 0; b
< BACK_BUFFER_NUM
; b
++) {
147 struct vl_dri3_buffer
*buf
= scrn
->back_buffers
[b
];
148 if (buf
&& buf
->pixmap
== ie
->pixmap
) {
160 dri3_flush_present_events(struct vl_dri3_screen
*scrn
)
162 if (scrn
->special_event
) {
163 xcb_generic_event_t
*ev
;
164 while ((ev
= xcb_poll_for_special_event(
165 scrn
->conn
, scrn
->special_event
)) != NULL
)
166 dri3_handle_present_event(scrn
, (xcb_present_generic_event_t
*)ev
);
171 dri3_wait_present_events(struct vl_dri3_screen
*scrn
)
173 if (scrn
->special_event
) {
174 xcb_generic_event_t
*ev
;
175 ev
= xcb_wait_for_special_event(scrn
->conn
, scrn
->special_event
);
178 dri3_handle_present_event(scrn
, (xcb_present_generic_event_t
*)ev
);
185 dri3_find_back(struct vl_dri3_screen
*scrn
)
190 for (b
= 0; b
< BACK_BUFFER_NUM
; b
++) {
191 int id
= (b
+ scrn
->cur_back
) % BACK_BUFFER_NUM
;
192 struct vl_dri3_buffer
*buffer
= scrn
->back_buffers
[id
];
193 if (!buffer
|| !buffer
->busy
)
196 xcb_flush(scrn
->conn
);
197 if (!dri3_wait_present_events(scrn
))
202 static struct vl_dri3_buffer
*
203 dri3_alloc_back_buffer(struct vl_dri3_screen
*scrn
)
205 struct vl_dri3_buffer
*buffer
;
207 xcb_sync_fence_t sync_fence
;
208 struct xshmfence
*shm_fence
;
209 int buffer_fd
, fence_fd
;
210 struct pipe_resource templ
;
211 struct winsys_handle whandle
;
214 buffer
= CALLOC_STRUCT(vl_dri3_buffer
);
218 fence_fd
= xshmfence_alloc_shm();
222 shm_fence
= xshmfence_map_shm(fence_fd
);
226 memset(&templ
, 0, sizeof(templ
));
227 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
|
228 PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
;
229 templ
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
230 templ
.target
= PIPE_TEXTURE_2D
;
231 templ
.last_level
= 0;
232 templ
.width0
= scrn
->width
;
233 templ
.height0
= scrn
->height
;
235 templ
.array_size
= 1;
236 buffer
->texture
= scrn
->base
.pscreen
->resource_create(scrn
->base
.pscreen
,
238 if (!buffer
->texture
)
241 memset(&whandle
, 0, sizeof(whandle
));
242 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
243 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
244 scrn
->base
.pscreen
->resource_get_handle(scrn
->base
.pscreen
,
245 buffer
->texture
, &whandle
,
247 buffer_fd
= whandle
.handle
;
248 buffer
->pitch
= whandle
.stride
;
249 xcb_dri3_pixmap_from_buffer(scrn
->conn
,
250 (pixmap
= xcb_generate_id(scrn
->conn
)),
253 scrn
->width
, scrn
->height
, buffer
->pitch
,
256 xcb_dri3_fence_from_fd(scrn
->conn
,
258 (sync_fence
= xcb_generate_id(scrn
->conn
)),
262 buffer
->pixmap
= pixmap
;
263 buffer
->sync_fence
= sync_fence
;
264 buffer
->shm_fence
= shm_fence
;
265 buffer
->width
= scrn
->width
;
266 buffer
->height
= scrn
->height
;
268 xshmfence_trigger(buffer
->shm_fence
);
273 xshmfence_unmap_shm(shm_fence
);
281 static struct vl_dri3_buffer
*
282 dri3_get_back_buffer(struct vl_dri3_screen
*scrn
)
284 struct vl_dri3_buffer
*buffer
;
285 struct pipe_resource
*texture
= NULL
;
289 scrn
->cur_back
= dri3_find_back(scrn
);
290 if (scrn
->cur_back
< 0)
292 buffer
= scrn
->back_buffers
[scrn
->cur_back
];
294 if (!buffer
|| buffer
->width
!= scrn
->width
||
295 buffer
->height
!= scrn
->height
) {
296 struct vl_dri3_buffer
*new_buffer
;
298 new_buffer
= dri3_alloc_back_buffer(scrn
);
303 dri3_free_back_buffer(scrn
, buffer
);
305 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[scrn
->cur_back
]);
307 scrn
->back_buffers
[scrn
->cur_back
] = buffer
;
310 pipe_resource_reference(&texture
, buffer
->texture
);
311 xcb_flush(scrn
->conn
);
312 xshmfence_await(buffer
->shm_fence
);
318 dri3_set_drawable(struct vl_dri3_screen
*scrn
, Drawable drawable
)
320 xcb_get_geometry_cookie_t geom_cookie
;
321 xcb_get_geometry_reply_t
*geom_reply
;
322 xcb_void_cookie_t cookie
;
323 xcb_generic_error_t
*error
;
324 xcb_present_event_t peid
;
329 if (scrn
->drawable
== drawable
)
332 scrn
->drawable
= drawable
;
334 geom_cookie
= xcb_get_geometry(scrn
->conn
, scrn
->drawable
);
335 geom_reply
= xcb_get_geometry_reply(scrn
->conn
, geom_cookie
, NULL
);
339 scrn
->width
= geom_reply
->width
;
340 scrn
->height
= geom_reply
->height
;
341 scrn
->depth
= geom_reply
->depth
;
344 if (scrn
->special_event
) {
345 xcb_unregister_for_special_event(scrn
->conn
, scrn
->special_event
);
346 scrn
->special_event
= NULL
;
349 scrn
->is_pixmap
= false;
350 peid
= xcb_generate_id(scrn
->conn
);
352 xcb_present_select_input_checked(scrn
->conn
, peid
, scrn
->drawable
,
353 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
354 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
355 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
357 error
= xcb_request_check(scrn
->conn
, cookie
);
359 if (error
->error_code
!= BadWindow
)
362 scrn
->is_pixmap
= true;
365 scrn
->special_event
=
366 xcb_register_for_special_xge(scrn
->conn
, &xcb_present_id
, peid
, 0);
368 dri3_flush_present_events(scrn
);
373 static struct vl_dri3_buffer
*
374 dri3_get_front_buffer(struct vl_dri3_screen
*scrn
)
376 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie
;
377 xcb_dri3_buffer_from_pixmap_reply_t
*bp_reply
;
378 xcb_sync_fence_t sync_fence
;
379 struct xshmfence
*shm_fence
;
381 struct winsys_handle whandle
;
382 struct pipe_resource templ
, *texture
= NULL
;
384 if (scrn
->front_buffer
) {
385 pipe_resource_reference(&texture
, scrn
->front_buffer
->texture
);
386 return scrn
->front_buffer
;
389 scrn
->front_buffer
= CALLOC_STRUCT(vl_dri3_buffer
);
390 if (!scrn
->front_buffer
)
393 fence_fd
= xshmfence_alloc_shm();
397 shm_fence
= xshmfence_map_shm(fence_fd
);
401 bp_cookie
= xcb_dri3_buffer_from_pixmap(scrn
->conn
, scrn
->drawable
);
402 bp_reply
= xcb_dri3_buffer_from_pixmap_reply(scrn
->conn
, bp_cookie
, NULL
);
406 fds
= xcb_dri3_buffer_from_pixmap_reply_fds(scrn
->conn
, bp_reply
);
410 memset(&whandle
, 0, sizeof(whandle
));
411 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
412 whandle
.handle
= (unsigned)fds
[0];
413 whandle
.stride
= bp_reply
->stride
;
414 memset(&templ
, 0, sizeof(templ
));
415 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
416 templ
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
417 templ
.target
= PIPE_TEXTURE_2D
;
418 templ
.last_level
= 0;
419 templ
.width0
= bp_reply
->width
;
420 templ
.height0
= bp_reply
->height
;
422 templ
.array_size
= 1;
423 scrn
->front_buffer
->texture
=
424 scrn
->base
.pscreen
->resource_from_handle(scrn
->base
.pscreen
,
426 PIPE_HANDLE_USAGE_READ_WRITE
);
428 if (!scrn
->front_buffer
->texture
)
431 xcb_dri3_fence_from_fd(scrn
->conn
,
433 (sync_fence
= xcb_generate_id(scrn
->conn
)),
437 pipe_resource_reference(&texture
, scrn
->front_buffer
->texture
);
438 scrn
->front_buffer
->pixmap
= scrn
->drawable
;
439 scrn
->front_buffer
->width
= bp_reply
->width
;
440 scrn
->front_buffer
->height
= bp_reply
->height
;
441 scrn
->front_buffer
->shm_fence
= shm_fence
;
442 scrn
->front_buffer
->sync_fence
= sync_fence
;
445 return scrn
->front_buffer
;
450 xshmfence_unmap_shm(shm_fence
);
454 FREE(scrn
->front_buffer
);
459 vl_dri3_flush_frontbuffer(struct pipe_screen
*screen
,
460 struct pipe_resource
*resource
,
461 unsigned level
, unsigned layer
,
462 void *context_private
, struct pipe_box
*sub_box
)
464 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)context_private
;
465 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
466 struct vl_dri3_buffer
*back
;
468 back
= scrn
->back_buffers
[scrn
->cur_back
];
473 while (scrn
->special_event
&& scrn
->recv_sbc
< scrn
->send_sbc
)
474 if (!dri3_wait_present_events(scrn
))
478 xshmfence_reset(back
->shm_fence
);
481 xcb_present_pixmap(scrn
->conn
,
484 (uint32_t)(++scrn
->send_sbc
),
492 xcb_flush(scrn
->conn
);
494 scrn
->flushed
= true;
499 static struct pipe_resource
*
500 vl_dri3_screen_texture_from_drawable(struct vl_screen
*vscreen
, void *drawable
)
502 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
503 struct vl_dri3_buffer
*buffer
;
507 if (!dri3_set_drawable(scrn
, (Drawable
)drawable
))
511 while (scrn
->special_event
&& scrn
->recv_sbc
< scrn
->send_sbc
)
512 if (!dri3_wait_present_events(scrn
))
515 scrn
->flushed
= false;
517 buffer
= (scrn
->is_pixmap
) ?
518 dri3_get_front_buffer(scrn
) :
519 dri3_get_back_buffer(scrn
);
523 return buffer
->texture
;
526 static struct u_rect
*
527 vl_dri3_screen_get_dirty_area(struct vl_screen
*vscreen
)
529 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
533 return &scrn
->dirty_areas
[scrn
->cur_back
];
537 vl_dri3_screen_get_timestamp(struct vl_screen
*vscreen
, void *drawable
)
539 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
543 if (!dri3_set_drawable(scrn
, (Drawable
)drawable
))
546 if (!scrn
->last_ust
) {
547 xcb_present_notify_msc(scrn
->conn
,
549 ++scrn
->send_msc_serial
,
551 xcb_flush(scrn
->conn
);
553 while (scrn
->special_event
&&
554 scrn
->send_msc_serial
> scrn
->recv_msc_serial
) {
555 if (!dri3_wait_present_events(scrn
))
560 return scrn
->last_ust
;
564 vl_dri3_screen_set_next_timestamp(struct vl_screen
*vscreen
, uint64_t stamp
)
566 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
570 if (stamp
&& scrn
->last_ust
&& scrn
->ns_frame
&& scrn
->last_msc
)
571 scrn
->next_msc
= ((int64_t)stamp
- scrn
->last_ust
+ scrn
->ns_frame
/2) /
572 scrn
->ns_frame
+ scrn
->last_msc
;
578 vl_dri3_screen_get_private(struct vl_screen
*vscreen
)
584 vl_dri3_screen_destroy(struct vl_screen
*vscreen
)
586 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
591 dri3_flush_present_events(scrn
);
593 if (scrn
->front_buffer
) {
594 dri3_free_front_buffer(scrn
, scrn
->front_buffer
);
595 scrn
->front_buffer
= NULL
;
599 for (i
= 0; i
< BACK_BUFFER_NUM
; ++i
) {
600 if (scrn
->back_buffers
[i
]) {
601 dri3_free_back_buffer(scrn
, scrn
->back_buffers
[i
]);
602 scrn
->back_buffers
[i
] = NULL
;
606 if (scrn
->special_event
)
607 xcb_unregister_for_special_event(scrn
->conn
, scrn
->special_event
);
608 scrn
->base
.pscreen
->destroy(scrn
->base
.pscreen
);
609 pipe_loader_release(&scrn
->base
.dev
, 1);
616 vl_dri3_screen_create(Display
*display
, int screen
)
618 struct vl_dri3_screen
*scrn
;
619 const xcb_query_extension_reply_t
*extension
;
620 xcb_dri3_open_cookie_t open_cookie
;
621 xcb_dri3_open_reply_t
*open_reply
;
622 xcb_get_geometry_cookie_t geom_cookie
;
623 xcb_get_geometry_reply_t
*geom_reply
;
624 int is_different_gpu
;
629 scrn
= CALLOC_STRUCT(vl_dri3_screen
);
633 scrn
->conn
= XGetXCBConnection(display
);
637 xcb_prefetch_extension_data(scrn
->conn
, &xcb_dri3_id
);
638 xcb_prefetch_extension_data(scrn
->conn
, &xcb_present_id
);
639 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_dri3_id
);
640 if (!(extension
&& extension
->present
))
642 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_present_id
);
643 if (!(extension
&& extension
->present
))
646 open_cookie
= xcb_dri3_open(scrn
->conn
, RootWindow(display
, screen
), None
);
647 open_reply
= xcb_dri3_open_reply(scrn
->conn
, open_cookie
, NULL
);
650 if (open_reply
->nfd
!= 1) {
655 fd
= xcb_dri3_open_reply_fds(scrn
->conn
, open_reply
)[0];
660 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
663 fd
= loader_get_user_preferred_fd(fd
, &is_different_gpu
);
664 /* TODO support different GPU */
665 if (is_different_gpu
)
668 geom_cookie
= xcb_get_geometry(scrn
->conn
, RootWindow(display
, screen
));
669 geom_reply
= xcb_get_geometry_reply(scrn
->conn
, geom_cookie
, NULL
);
672 /* TODO support depth other than 24 */
673 if (geom_reply
->depth
!= 24) {
679 if (pipe_loader_drm_probe_fd(&scrn
->base
.dev
, fd
))
680 scrn
->base
.pscreen
= pipe_loader_create_screen(scrn
->base
.dev
);
682 if (!scrn
->base
.pscreen
)
685 scrn
->base
.destroy
= vl_dri3_screen_destroy
;
686 scrn
->base
.texture_from_drawable
= vl_dri3_screen_texture_from_drawable
;
687 scrn
->base
.get_dirty_area
= vl_dri3_screen_get_dirty_area
;
688 scrn
->base
.get_timestamp
= vl_dri3_screen_get_timestamp
;
689 scrn
->base
.set_next_timestamp
= vl_dri3_screen_set_next_timestamp
;
690 scrn
->base
.get_private
= vl_dri3_screen_get_private
;
691 scrn
->base
.pscreen
->flush_frontbuffer
= vl_dri3_flush_frontbuffer
;
696 if (scrn
->base
.dev
) {
697 pipe_loader_release(&scrn
->base
.dev
, 1);