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
;
85 dri3_free_front_buffer(struct vl_dri3_screen
*scrn
,
86 struct vl_dri3_buffer
*buffer
)
88 xcb_sync_destroy_fence(scrn
->conn
, buffer
->sync_fence
);
89 xshmfence_unmap_shm(buffer
->shm_fence
);
94 dri3_free_back_buffer(struct vl_dri3_screen
*scrn
,
95 struct vl_dri3_buffer
*buffer
)
97 xcb_free_pixmap(scrn
->conn
, buffer
->pixmap
);
98 xcb_sync_destroy_fence(scrn
->conn
, buffer
->sync_fence
);
99 xshmfence_unmap_shm(buffer
->shm_fence
);
100 pipe_resource_reference(&buffer
->texture
, NULL
);
105 dri3_handle_stamps(struct vl_dri3_screen
*scrn
, uint64_t ust
, uint64_t msc
)
107 int64_t ust_ns
= ust
* 1000;
109 if (scrn
->last_ust
&& (ust_ns
> scrn
->last_ust
) &&
110 scrn
->last_msc
&& (msc
> scrn
->last_msc
))
111 scrn
->ns_frame
= (ust_ns
- scrn
->last_ust
) / (msc
- scrn
->last_msc
);
113 scrn
->last_ust
= ust_ns
;
114 scrn
->last_msc
= msc
;
118 dri3_handle_present_event(struct vl_dri3_screen
*scrn
,
119 xcb_present_generic_event_t
*ge
)
121 switch (ge
->evtype
) {
122 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
123 xcb_present_configure_notify_event_t
*ce
= (void *) ge
;
124 scrn
->width
= ce
->width
;
125 scrn
->height
= ce
->height
;
128 case XCB_PRESENT_COMPLETE_NOTIFY
: {
129 xcb_present_complete_notify_event_t
*ce
= (void *) ge
;
130 if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
) {
131 scrn
->recv_sbc
= (scrn
->send_sbc
& 0xffffffff00000000LL
) | ce
->serial
;
132 if (scrn
->recv_sbc
> scrn
->send_sbc
)
133 scrn
->recv_sbc
-= 0x100000000;
134 dri3_handle_stamps(scrn
, ce
->ust
, ce
->msc
);
135 } else if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_NOTIFY_MSC
) {
136 scrn
->recv_msc_serial
= ce
->serial
;
137 dri3_handle_stamps(scrn
, ce
->ust
, ce
->msc
);
141 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
142 xcb_present_idle_notify_event_t
*ie
= (void *) ge
;
144 for (b
= 0; b
< BACK_BUFFER_NUM
; b
++) {
145 struct vl_dri3_buffer
*buf
= scrn
->back_buffers
[b
];
146 if (buf
&& buf
->pixmap
== ie
->pixmap
) {
158 dri3_flush_present_events(struct vl_dri3_screen
*scrn
)
160 if (scrn
->special_event
) {
161 xcb_generic_event_t
*ev
;
162 while ((ev
= xcb_poll_for_special_event(
163 scrn
->conn
, scrn
->special_event
)) != NULL
)
164 dri3_handle_present_event(scrn
, (xcb_present_generic_event_t
*)ev
);
169 dri3_wait_present_events(struct vl_dri3_screen
*scrn
)
171 if (scrn
->special_event
) {
172 xcb_generic_event_t
*ev
;
173 ev
= xcb_wait_for_special_event(scrn
->conn
, scrn
->special_event
);
176 dri3_handle_present_event(scrn
, (xcb_present_generic_event_t
*)ev
);
183 dri3_find_back(struct vl_dri3_screen
*scrn
)
188 for (b
= 0; b
< BACK_BUFFER_NUM
; b
++) {
189 int id
= (b
+ scrn
->cur_back
) % BACK_BUFFER_NUM
;
190 struct vl_dri3_buffer
*buffer
= scrn
->back_buffers
[id
];
191 if (!buffer
|| !buffer
->busy
)
194 xcb_flush(scrn
->conn
);
195 if (!dri3_wait_present_events(scrn
))
200 static struct vl_dri3_buffer
*
201 dri3_alloc_back_buffer(struct vl_dri3_screen
*scrn
)
203 struct vl_dri3_buffer
*buffer
;
205 xcb_sync_fence_t sync_fence
;
206 struct xshmfence
*shm_fence
;
207 int buffer_fd
, fence_fd
;
208 struct pipe_resource templ
;
209 struct winsys_handle whandle
;
212 buffer
= CALLOC_STRUCT(vl_dri3_buffer
);
216 fence_fd
= xshmfence_alloc_shm();
220 shm_fence
= xshmfence_map_shm(fence_fd
);
224 memset(&templ
, 0, sizeof(templ
));
225 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
|
226 PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
;
227 templ
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
228 templ
.target
= PIPE_TEXTURE_2D
;
229 templ
.last_level
= 0;
230 templ
.width0
= scrn
->width
;
231 templ
.height0
= scrn
->height
;
233 templ
.array_size
= 1;
234 buffer
->texture
= scrn
->base
.pscreen
->resource_create(scrn
->base
.pscreen
,
236 if (!buffer
->texture
)
239 memset(&whandle
, 0, sizeof(whandle
));
240 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
241 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
242 scrn
->base
.pscreen
->resource_get_handle(scrn
->base
.pscreen
,
243 buffer
->texture
, &whandle
,
245 buffer_fd
= whandle
.handle
;
246 buffer
->pitch
= whandle
.stride
;
247 xcb_dri3_pixmap_from_buffer(scrn
->conn
,
248 (pixmap
= xcb_generate_id(scrn
->conn
)),
251 scrn
->width
, scrn
->height
, buffer
->pitch
,
254 xcb_dri3_fence_from_fd(scrn
->conn
,
256 (sync_fence
= xcb_generate_id(scrn
->conn
)),
260 buffer
->pixmap
= pixmap
;
261 buffer
->sync_fence
= sync_fence
;
262 buffer
->shm_fence
= shm_fence
;
263 buffer
->width
= scrn
->width
;
264 buffer
->height
= scrn
->height
;
266 xshmfence_trigger(buffer
->shm_fence
);
271 xshmfence_unmap_shm(shm_fence
);
279 static struct vl_dri3_buffer
*
280 dri3_get_back_buffer(struct vl_dri3_screen
*scrn
)
282 struct vl_dri3_buffer
*buffer
;
283 struct pipe_resource
*texture
= NULL
;
287 scrn
->cur_back
= dri3_find_back(scrn
);
288 if (scrn
->cur_back
< 0)
290 buffer
= scrn
->back_buffers
[scrn
->cur_back
];
292 if (!buffer
|| buffer
->width
!= scrn
->width
||
293 buffer
->height
!= scrn
->height
) {
294 struct vl_dri3_buffer
*new_buffer
;
296 new_buffer
= dri3_alloc_back_buffer(scrn
);
301 dri3_free_back_buffer(scrn
, buffer
);
303 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[scrn
->cur_back
]);
305 scrn
->back_buffers
[scrn
->cur_back
] = buffer
;
308 pipe_resource_reference(&texture
, buffer
->texture
);
309 xcb_flush(scrn
->conn
);
310 xshmfence_await(buffer
->shm_fence
);
316 dri3_set_drawable(struct vl_dri3_screen
*scrn
, Drawable drawable
)
318 xcb_get_geometry_cookie_t geom_cookie
;
319 xcb_get_geometry_reply_t
*geom_reply
;
320 xcb_void_cookie_t cookie
;
321 xcb_generic_error_t
*error
;
322 xcb_present_event_t peid
;
327 if (scrn
->drawable
== drawable
)
330 scrn
->drawable
= drawable
;
332 geom_cookie
= xcb_get_geometry(scrn
->conn
, scrn
->drawable
);
333 geom_reply
= xcb_get_geometry_reply(scrn
->conn
, geom_cookie
, NULL
);
337 scrn
->width
= geom_reply
->width
;
338 scrn
->height
= geom_reply
->height
;
339 scrn
->depth
= geom_reply
->depth
;
342 if (scrn
->special_event
) {
343 xcb_unregister_for_special_event(scrn
->conn
, scrn
->special_event
);
344 scrn
->special_event
= NULL
;
347 scrn
->is_pixmap
= false;
348 peid
= xcb_generate_id(scrn
->conn
);
350 xcb_present_select_input_checked(scrn
->conn
, peid
, scrn
->drawable
,
351 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
352 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
353 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
355 error
= xcb_request_check(scrn
->conn
, cookie
);
357 if (error
->error_code
!= BadWindow
)
360 scrn
->is_pixmap
= true;
363 scrn
->special_event
=
364 xcb_register_for_special_xge(scrn
->conn
, &xcb_present_id
, peid
, 0);
366 dri3_flush_present_events(scrn
);
371 static struct vl_dri3_buffer
*
372 dri3_get_front_buffer(struct vl_dri3_screen
*scrn
)
374 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie
;
375 xcb_dri3_buffer_from_pixmap_reply_t
*bp_reply
;
376 xcb_sync_fence_t sync_fence
;
377 struct xshmfence
*shm_fence
;
379 struct winsys_handle whandle
;
380 struct pipe_resource templ
, *texture
= NULL
;
382 if (scrn
->front_buffer
) {
383 pipe_resource_reference(&texture
, scrn
->front_buffer
->texture
);
384 return scrn
->front_buffer
;
387 scrn
->front_buffer
= CALLOC_STRUCT(vl_dri3_buffer
);
388 if (!scrn
->front_buffer
)
391 fence_fd
= xshmfence_alloc_shm();
395 shm_fence
= xshmfence_map_shm(fence_fd
);
399 bp_cookie
= xcb_dri3_buffer_from_pixmap(scrn
->conn
, scrn
->drawable
);
400 bp_reply
= xcb_dri3_buffer_from_pixmap_reply(scrn
->conn
, bp_cookie
, NULL
);
404 fds
= xcb_dri3_buffer_from_pixmap_reply_fds(scrn
->conn
, bp_reply
);
408 memset(&whandle
, 0, sizeof(whandle
));
409 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
410 whandle
.handle
= (unsigned)fds
[0];
411 whandle
.stride
= bp_reply
->stride
;
412 memset(&templ
, 0, sizeof(templ
));
413 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
414 templ
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
415 templ
.target
= PIPE_TEXTURE_2D
;
416 templ
.last_level
= 0;
417 templ
.width0
= bp_reply
->width
;
418 templ
.height0
= bp_reply
->height
;
420 templ
.array_size
= 1;
421 scrn
->front_buffer
->texture
=
422 scrn
->base
.pscreen
->resource_from_handle(scrn
->base
.pscreen
,
424 PIPE_HANDLE_USAGE_READ_WRITE
);
426 if (!scrn
->front_buffer
->texture
)
429 xcb_dri3_fence_from_fd(scrn
->conn
,
431 (sync_fence
= xcb_generate_id(scrn
->conn
)),
435 pipe_resource_reference(&texture
, scrn
->front_buffer
->texture
);
436 scrn
->front_buffer
->pixmap
= scrn
->drawable
;
437 scrn
->front_buffer
->width
= bp_reply
->width
;
438 scrn
->front_buffer
->height
= bp_reply
->height
;
439 scrn
->front_buffer
->shm_fence
= shm_fence
;
440 scrn
->front_buffer
->sync_fence
= sync_fence
;
443 return scrn
->front_buffer
;
448 xshmfence_unmap_shm(shm_fence
);
452 FREE(scrn
->front_buffer
);
457 vl_dri3_flush_frontbuffer(struct pipe_screen
*screen
,
458 struct pipe_resource
*resource
,
459 unsigned level
, unsigned layer
,
460 void *context_private
, struct pipe_box
*sub_box
)
462 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)context_private
;
463 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
464 struct vl_dri3_buffer
*back
;
466 back
= scrn
->back_buffers
[scrn
->cur_back
];
470 xshmfence_reset(back
->shm_fence
);
473 xcb_present_pixmap(scrn
->conn
,
479 options
, 0, 0, 0, 0, NULL
);
481 xcb_flush(scrn
->conn
);
486 static struct pipe_resource
*
487 vl_dri3_screen_texture_from_drawable(struct vl_screen
*vscreen
, void *drawable
)
489 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
490 struct vl_dri3_buffer
*buffer
;
494 if (!dri3_set_drawable(scrn
, (Drawable
)drawable
))
497 buffer
= (scrn
->is_pixmap
) ?
498 dri3_get_front_buffer(scrn
) :
499 dri3_get_back_buffer(scrn
);
503 return buffer
->texture
;
506 static struct u_rect
*
507 vl_dri3_screen_get_dirty_area(struct vl_screen
*vscreen
)
509 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
513 return &scrn
->dirty_areas
[scrn
->cur_back
];
517 vl_dri3_screen_get_timestamp(struct vl_screen
*vscreen
, void *drawable
)
524 vl_dri3_screen_set_next_timestamp(struct vl_screen
*vscreen
, uint64_t stamp
)
531 vl_dri3_screen_get_private(struct vl_screen
*vscreen
)
537 vl_dri3_screen_destroy(struct vl_screen
*vscreen
)
539 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
544 dri3_flush_present_events(scrn
);
546 if (scrn
->front_buffer
) {
547 dri3_free_front_buffer(scrn
, scrn
->front_buffer
);
548 scrn
->front_buffer
= NULL
;
552 for (i
= 0; i
< BACK_BUFFER_NUM
; ++i
) {
553 if (scrn
->back_buffers
[i
]) {
554 dri3_free_back_buffer(scrn
, scrn
->back_buffers
[i
]);
555 scrn
->back_buffers
[i
] = NULL
;
559 if (scrn
->special_event
)
560 xcb_unregister_for_special_event(scrn
->conn
, scrn
->special_event
);
561 scrn
->base
.pscreen
->destroy(scrn
->base
.pscreen
);
562 pipe_loader_release(&scrn
->base
.dev
, 1);
569 vl_dri3_screen_create(Display
*display
, int screen
)
571 struct vl_dri3_screen
*scrn
;
572 const xcb_query_extension_reply_t
*extension
;
573 xcb_dri3_open_cookie_t open_cookie
;
574 xcb_dri3_open_reply_t
*open_reply
;
575 xcb_get_geometry_cookie_t geom_cookie
;
576 xcb_get_geometry_reply_t
*geom_reply
;
577 int is_different_gpu
;
582 scrn
= CALLOC_STRUCT(vl_dri3_screen
);
586 scrn
->conn
= XGetXCBConnection(display
);
590 xcb_prefetch_extension_data(scrn
->conn
, &xcb_dri3_id
);
591 xcb_prefetch_extension_data(scrn
->conn
, &xcb_present_id
);
592 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_dri3_id
);
593 if (!(extension
&& extension
->present
))
595 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_present_id
);
596 if (!(extension
&& extension
->present
))
599 open_cookie
= xcb_dri3_open(scrn
->conn
, RootWindow(display
, screen
), None
);
600 open_reply
= xcb_dri3_open_reply(scrn
->conn
, open_cookie
, NULL
);
603 if (open_reply
->nfd
!= 1) {
608 fd
= xcb_dri3_open_reply_fds(scrn
->conn
, open_reply
)[0];
613 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
616 fd
= loader_get_user_preferred_fd(fd
, &is_different_gpu
);
617 /* TODO support different GPU */
618 if (is_different_gpu
)
621 geom_cookie
= xcb_get_geometry(scrn
->conn
, RootWindow(display
, screen
));
622 geom_reply
= xcb_get_geometry_reply(scrn
->conn
, geom_cookie
, NULL
);
625 /* TODO support depth other than 24 */
626 if (geom_reply
->depth
!= 24) {
632 if (pipe_loader_drm_probe_fd(&scrn
->base
.dev
, fd
))
633 scrn
->base
.pscreen
= pipe_loader_create_screen(scrn
->base
.dev
);
635 if (!scrn
->base
.pscreen
)
638 scrn
->base
.destroy
= vl_dri3_screen_destroy
;
639 scrn
->base
.texture_from_drawable
= vl_dri3_screen_texture_from_drawable
;
640 scrn
->base
.get_dirty_area
= vl_dri3_screen_get_dirty_area
;
641 scrn
->base
.get_timestamp
= vl_dri3_screen_get_timestamp
;
642 scrn
->base
.set_next_timestamp
= vl_dri3_screen_set_next_timestamp
;
643 scrn
->base
.get_private
= vl_dri3_screen_get_private
;
644 scrn
->base
.pscreen
->flush_frontbuffer
= vl_dri3_flush_frontbuffer
;
649 if (scrn
->base
.dev
) {
650 pipe_loader_release(&scrn
->base
.dev
, 1);