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
];
78 dri3_free_back_buffer(struct vl_dri3_screen
*scrn
,
79 struct vl_dri3_buffer
*buffer
)
81 xcb_free_pixmap(scrn
->conn
, buffer
->pixmap
);
82 xcb_sync_destroy_fence(scrn
->conn
, buffer
->sync_fence
);
83 xshmfence_unmap_shm(buffer
->shm_fence
);
84 pipe_resource_reference(&buffer
->texture
, NULL
);
89 dri3_handle_present_event(struct vl_dri3_screen
*scrn
,
90 xcb_present_generic_event_t
*ge
)
93 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
94 xcb_present_configure_notify_event_t
*ce
= (void *) ge
;
95 scrn
->width
= ce
->width
;
96 scrn
->height
= ce
->height
;
99 case XCB_PRESENT_COMPLETE_NOTIFY
: {
103 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
104 xcb_present_idle_notify_event_t
*ie
= (void *) ge
;
106 for (b
= 0; b
< BACK_BUFFER_NUM
; b
++) {
107 struct vl_dri3_buffer
*buf
= scrn
->back_buffers
[b
];
108 if (buf
&& buf
->pixmap
== ie
->pixmap
) {
120 dri3_flush_present_events(struct vl_dri3_screen
*scrn
)
122 if (scrn
->special_event
) {
123 xcb_generic_event_t
*ev
;
124 while ((ev
= xcb_poll_for_special_event(
125 scrn
->conn
, scrn
->special_event
)) != NULL
)
126 dri3_handle_present_event(scrn
, (xcb_present_generic_event_t
*)ev
);
131 dri3_wait_present_events(struct vl_dri3_screen
*scrn
)
133 if (scrn
->special_event
) {
134 xcb_generic_event_t
*ev
;
135 ev
= xcb_wait_for_special_event(scrn
->conn
, scrn
->special_event
);
138 dri3_handle_present_event(scrn
, (xcb_present_generic_event_t
*)ev
);
145 dri3_find_back(struct vl_dri3_screen
*scrn
)
150 for (b
= 0; b
< BACK_BUFFER_NUM
; b
++) {
151 int id
= (b
+ scrn
->cur_back
) % BACK_BUFFER_NUM
;
152 struct vl_dri3_buffer
*buffer
= scrn
->back_buffers
[id
];
153 if (!buffer
|| !buffer
->busy
)
156 xcb_flush(scrn
->conn
);
157 if (!dri3_wait_present_events(scrn
))
162 static struct vl_dri3_buffer
*
163 dri3_alloc_back_buffer(struct vl_dri3_screen
*scrn
)
165 struct vl_dri3_buffer
*buffer
;
167 xcb_sync_fence_t sync_fence
;
168 struct xshmfence
*shm_fence
;
169 int buffer_fd
, fence_fd
;
170 struct pipe_resource templ
;
171 struct winsys_handle whandle
;
174 buffer
= CALLOC_STRUCT(vl_dri3_buffer
);
178 fence_fd
= xshmfence_alloc_shm();
182 shm_fence
= xshmfence_map_shm(fence_fd
);
186 memset(&templ
, 0, sizeof(templ
));
187 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
|
188 PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
;
189 templ
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
190 templ
.target
= PIPE_TEXTURE_2D
;
191 templ
.last_level
= 0;
192 templ
.width0
= scrn
->width
;
193 templ
.height0
= scrn
->height
;
195 templ
.array_size
= 1;
196 buffer
->texture
= scrn
->base
.pscreen
->resource_create(scrn
->base
.pscreen
,
198 if (!buffer
->texture
)
201 memset(&whandle
, 0, sizeof(whandle
));
202 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
203 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
204 scrn
->base
.pscreen
->resource_get_handle(scrn
->base
.pscreen
,
205 buffer
->texture
, &whandle
,
207 buffer_fd
= whandle
.handle
;
208 buffer
->pitch
= whandle
.stride
;
209 xcb_dri3_pixmap_from_buffer(scrn
->conn
,
210 (pixmap
= xcb_generate_id(scrn
->conn
)),
213 scrn
->width
, scrn
->height
, buffer
->pitch
,
216 xcb_dri3_fence_from_fd(scrn
->conn
,
218 (sync_fence
= xcb_generate_id(scrn
->conn
)),
222 buffer
->pixmap
= pixmap
;
223 buffer
->sync_fence
= sync_fence
;
224 buffer
->shm_fence
= shm_fence
;
225 buffer
->width
= scrn
->width
;
226 buffer
->height
= scrn
->height
;
228 xshmfence_trigger(buffer
->shm_fence
);
233 xshmfence_unmap_shm(shm_fence
);
241 static struct vl_dri3_buffer
*
242 dri3_get_back_buffer(struct vl_dri3_screen
*scrn
)
244 struct vl_dri3_buffer
*buffer
;
245 struct pipe_resource
*texture
= NULL
;
249 scrn
->cur_back
= dri3_find_back(scrn
);
250 if (scrn
->cur_back
< 0)
252 buffer
= scrn
->back_buffers
[scrn
->cur_back
];
254 if (!buffer
|| buffer
->width
!= scrn
->width
||
255 buffer
->height
!= scrn
->height
) {
256 struct vl_dri3_buffer
*new_buffer
;
258 new_buffer
= dri3_alloc_back_buffer(scrn
);
263 dri3_free_back_buffer(scrn
, buffer
);
265 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[scrn
->cur_back
]);
267 scrn
->back_buffers
[scrn
->cur_back
] = buffer
;
270 pipe_resource_reference(&texture
, buffer
->texture
);
271 xcb_flush(scrn
->conn
);
272 xshmfence_await(buffer
->shm_fence
);
278 dri3_set_drawable(struct vl_dri3_screen
*scrn
, Drawable drawable
)
280 xcb_get_geometry_cookie_t geom_cookie
;
281 xcb_get_geometry_reply_t
*geom_reply
;
282 xcb_void_cookie_t cookie
;
283 xcb_generic_error_t
*error
;
284 xcb_present_event_t peid
;
288 if (scrn
->drawable
== drawable
)
291 scrn
->drawable
= drawable
;
293 geom_cookie
= xcb_get_geometry(scrn
->conn
, scrn
->drawable
);
294 geom_reply
= xcb_get_geometry_reply(scrn
->conn
, geom_cookie
, NULL
);
298 scrn
->width
= geom_reply
->width
;
299 scrn
->height
= geom_reply
->height
;
300 scrn
->depth
= geom_reply
->depth
;
303 if (scrn
->special_event
) {
304 xcb_unregister_for_special_event(scrn
->conn
, scrn
->special_event
);
305 scrn
->special_event
= NULL
;
308 peid
= xcb_generate_id(scrn
->conn
);
310 xcb_present_select_input_checked(scrn
->conn
, peid
, scrn
->drawable
,
311 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
312 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
313 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
315 error
= xcb_request_check(scrn
->conn
, cookie
);
320 scrn
->special_event
=
321 xcb_register_for_special_xge(scrn
->conn
, &xcb_present_id
, peid
, 0);
323 dri3_flush_present_events(scrn
);
329 vl_dri3_flush_frontbuffer(struct pipe_screen
*screen
,
330 struct pipe_resource
*resource
,
331 unsigned level
, unsigned layer
,
332 void *context_private
, struct pipe_box
*sub_box
)
334 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)context_private
;
335 uint32_t options
= XCB_PRESENT_OPTION_NONE
;
336 struct vl_dri3_buffer
*back
;
338 back
= scrn
->back_buffers
[scrn
->cur_back
];
342 xshmfence_reset(back
->shm_fence
);
345 xcb_present_pixmap(scrn
->conn
,
351 options
, 0, 0, 0, 0, NULL
);
353 xcb_flush(scrn
->conn
);
358 static struct pipe_resource
*
359 vl_dri3_screen_texture_from_drawable(struct vl_screen
*vscreen
, void *drawable
)
361 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
362 struct vl_dri3_buffer
*buffer
;
366 if (!dri3_set_drawable(scrn
, (Drawable
)drawable
))
369 buffer
= dri3_get_back_buffer(scrn
);
373 return buffer
->texture
;
376 static struct u_rect
*
377 vl_dri3_screen_get_dirty_area(struct vl_screen
*vscreen
)
379 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
383 return &scrn
->dirty_areas
[scrn
->cur_back
];
387 vl_dri3_screen_get_timestamp(struct vl_screen
*vscreen
, void *drawable
)
394 vl_dri3_screen_set_next_timestamp(struct vl_screen
*vscreen
, uint64_t stamp
)
401 vl_dri3_screen_get_private(struct vl_screen
*vscreen
)
407 vl_dri3_screen_destroy(struct vl_screen
*vscreen
)
409 struct vl_dri3_screen
*scrn
= (struct vl_dri3_screen
*)vscreen
;
414 dri3_flush_present_events(scrn
);
416 for (i
= 0; i
< BACK_BUFFER_NUM
; ++i
) {
417 if (scrn
->back_buffers
[i
]) {
418 dri3_free_back_buffer(scrn
, scrn
->back_buffers
[i
]);
419 scrn
->back_buffers
[i
] = NULL
;
423 if (scrn
->special_event
)
424 xcb_unregister_for_special_event(scrn
->conn
, scrn
->special_event
);
425 scrn
->base
.pscreen
->destroy(scrn
->base
.pscreen
);
426 pipe_loader_release(&scrn
->base
.dev
, 1);
433 vl_dri3_screen_create(Display
*display
, int screen
)
435 struct vl_dri3_screen
*scrn
;
436 const xcb_query_extension_reply_t
*extension
;
437 xcb_dri3_open_cookie_t open_cookie
;
438 xcb_dri3_open_reply_t
*open_reply
;
439 xcb_get_geometry_cookie_t geom_cookie
;
440 xcb_get_geometry_reply_t
*geom_reply
;
441 int is_different_gpu
;
446 scrn
= CALLOC_STRUCT(vl_dri3_screen
);
450 scrn
->conn
= XGetXCBConnection(display
);
454 xcb_prefetch_extension_data(scrn
->conn
, &xcb_dri3_id
);
455 xcb_prefetch_extension_data(scrn
->conn
, &xcb_present_id
);
456 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_dri3_id
);
457 if (!(extension
&& extension
->present
))
459 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_present_id
);
460 if (!(extension
&& extension
->present
))
463 open_cookie
= xcb_dri3_open(scrn
->conn
, RootWindow(display
, screen
), None
);
464 open_reply
= xcb_dri3_open_reply(scrn
->conn
, open_cookie
, NULL
);
467 if (open_reply
->nfd
!= 1) {
472 fd
= xcb_dri3_open_reply_fds(scrn
->conn
, open_reply
)[0];
477 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
480 fd
= loader_get_user_preferred_fd(fd
, &is_different_gpu
);
481 /* TODO support different GPU */
482 if (is_different_gpu
)
485 geom_cookie
= xcb_get_geometry(scrn
->conn
, RootWindow(display
, screen
));
486 geom_reply
= xcb_get_geometry_reply(scrn
->conn
, geom_cookie
, NULL
);
489 /* TODO support depth other than 24 */
490 if (geom_reply
->depth
!= 24) {
496 if (pipe_loader_drm_probe_fd(&scrn
->base
.dev
, fd
))
497 scrn
->base
.pscreen
= pipe_loader_create_screen(scrn
->base
.dev
);
499 if (!scrn
->base
.pscreen
)
502 scrn
->base
.destroy
= vl_dri3_screen_destroy
;
503 scrn
->base
.texture_from_drawable
= vl_dri3_screen_texture_from_drawable
;
504 scrn
->base
.get_dirty_area
= vl_dri3_screen_get_dirty_area
;
505 scrn
->base
.get_timestamp
= vl_dri3_screen_get_timestamp
;
506 scrn
->base
.set_next_timestamp
= vl_dri3_screen_set_next_timestamp
;
507 scrn
->base
.get_private
= vl_dri3_screen_get_private
;
508 scrn
->base
.pscreen
->flush_frontbuffer
= vl_dri3_flush_frontbuffer
;
513 if (scrn
->base
.dev
) {
514 pipe_loader_release(&scrn
->base
.dev
, 1);