1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /* directly referenced from target Makefile, because of X dependencies */
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 #include <X11/Xlib-xcb.h>
34 #include <X11/extensions/dri2tokens.h>
35 #ifdef HAVE_X11_DRI2
36 #include <xcb/dri2.h>
37 #endif
38 #include <xf86drm.h>
39 #include <errno.h>
40
41 #include "loader.h"
42
43 #include "pipe/p_screen.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_state.h"
46 #include "pipe-loader/pipe_loader.h"
47 #include "frontend/drm_driver.h"
48
49 #include "util/u_memory.h"
50 #include "util/crc32.h"
51 #include "util/u_hash_table.h"
52 #include "util/u_inlines.h"
53
54 #include "vl/vl_compositor.h"
55 #include "vl/vl_winsys.h"
56
57 #include "drm-uapi/drm_fourcc.h"
58
59 struct vl_dri_screen
60 {
61 struct vl_screen base;
62 xcb_connection_t *conn;
63 xcb_drawable_t drawable;
64
65 unsigned width, height;
66
67 bool current_buffer;
68 uint32_t buffer_names[2];
69 struct u_rect dirty_areas[2];
70
71 bool flushed;
72 #ifdef HAVE_X11_DRI2
73 xcb_dri2_swap_buffers_cookie_t swap_cookie;
74 xcb_dri2_wait_sbc_cookie_t wait_cookie;
75 xcb_dri2_get_buffers_cookie_t buffers_cookie;
76 #endif
77
78 int64_t last_ust, ns_frame, last_msc, next_msc;
79 };
80
81 #ifdef HAVE_X11_DRI2
82 static const unsigned attachments[1] = { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT };
83
84 static void vl_dri2_screen_destroy(struct vl_screen *vscreen);
85
86 static void
vl_dri2_handle_stamps(struct vl_dri_screen * scrn,uint32_t ust_hi,uint32_t ust_lo,uint32_t msc_hi,uint32_t msc_lo)87 vl_dri2_handle_stamps(struct vl_dri_screen *scrn,
88 uint32_t ust_hi, uint32_t ust_lo,
89 uint32_t msc_hi, uint32_t msc_lo)
90 {
91 int64_t ust = ((((uint64_t)ust_hi) << 32) | ust_lo) * 1000;
92 int64_t msc = (((uint64_t)msc_hi) << 32) | msc_lo;
93
94 if (scrn->last_ust && (ust > scrn->last_ust) &&
95 scrn->last_msc && (msc > scrn->last_msc))
96 scrn->ns_frame = (ust - scrn->last_ust) / (msc - scrn->last_msc);
97
98 scrn->last_ust = ust;
99 scrn->last_msc = msc;
100 }
101
102 static xcb_dri2_get_buffers_reply_t *
vl_dri2_get_flush_reply(struct vl_dri_screen * scrn)103 vl_dri2_get_flush_reply(struct vl_dri_screen *scrn)
104 {
105 xcb_dri2_wait_sbc_reply_t *wait_sbc_reply;
106
107 assert(scrn);
108
109 if (!scrn->flushed)
110 return NULL;
111
112 scrn->flushed = false;
113
114 free(xcb_dri2_swap_buffers_reply(scrn->conn, scrn->swap_cookie, NULL));
115
116 wait_sbc_reply = xcb_dri2_wait_sbc_reply(scrn->conn, scrn->wait_cookie, NULL);
117 if (!wait_sbc_reply)
118 return NULL;
119 vl_dri2_handle_stamps(scrn, wait_sbc_reply->ust_hi, wait_sbc_reply->ust_lo,
120 wait_sbc_reply->msc_hi, wait_sbc_reply->msc_lo);
121 free(wait_sbc_reply);
122
123 return xcb_dri2_get_buffers_reply(scrn->conn, scrn->buffers_cookie, NULL);
124 }
125
126 static void
vl_dri2_flush_frontbuffer(struct pipe_screen * screen,struct pipe_context * pipe,struct pipe_resource * resource,unsigned level,unsigned layer,void * context_private,unsigned nboxes,struct pipe_box * sub_box)127 vl_dri2_flush_frontbuffer(struct pipe_screen *screen,
128 struct pipe_context *pipe,
129 struct pipe_resource *resource,
130 unsigned level, unsigned layer,
131 void *context_private, unsigned nboxes, struct pipe_box *sub_box)
132 {
133 struct vl_dri_screen *scrn = (struct vl_dri_screen *)context_private;
134 uint32_t msc_hi, msc_lo;
135
136 assert(screen);
137 assert(resource);
138 assert(context_private);
139
140 free(vl_dri2_get_flush_reply(scrn));
141
142 msc_hi = scrn->next_msc >> 32;
143 msc_lo = scrn->next_msc & 0xFFFFFFFF;
144
145 scrn->swap_cookie = xcb_dri2_swap_buffers_unchecked(scrn->conn, scrn->drawable,
146 msc_hi, msc_lo, 0, 0, 0, 0);
147 scrn->wait_cookie = xcb_dri2_wait_sbc_unchecked(scrn->conn, scrn->drawable, 0, 0);
148 scrn->buffers_cookie = xcb_dri2_get_buffers_unchecked(scrn->conn, scrn->drawable,
149 1, 1, attachments);
150
151 scrn->flushed = true;
152 scrn->current_buffer = !scrn->current_buffer;
153 }
154
155 static void
vl_dri2_destroy_drawable(struct vl_dri_screen * scrn)156 vl_dri2_destroy_drawable(struct vl_dri_screen *scrn)
157 {
158 xcb_void_cookie_t destroy_cookie;
159 if (scrn->drawable) {
160 free(vl_dri2_get_flush_reply(scrn));
161 destroy_cookie = xcb_dri2_destroy_drawable_checked(scrn->conn, scrn->drawable);
162 /* ignore any error here, since the drawable can be destroyed long ago */
163 free(xcb_request_check(scrn->conn, destroy_cookie));
164 }
165 }
166
167 static void
vl_dri2_set_drawable(struct vl_dri_screen * scrn,Drawable drawable)168 vl_dri2_set_drawable(struct vl_dri_screen *scrn, Drawable drawable)
169 {
170 assert(scrn);
171 assert(drawable);
172
173 if (scrn->drawable == drawable)
174 return;
175
176 vl_dri2_destroy_drawable(scrn);
177
178 xcb_dri2_create_drawable(scrn->conn, drawable);
179 scrn->current_buffer = false;
180 vl_compositor_reset_dirty_area(&scrn->dirty_areas[0]);
181 vl_compositor_reset_dirty_area(&scrn->dirty_areas[1]);
182 scrn->drawable = drawable;
183 }
184
185 static struct pipe_resource *
vl_dri2_screen_texture_from_drawable(struct vl_screen * vscreen,void * drawable)186 vl_dri2_screen_texture_from_drawable(struct vl_screen *vscreen, void *drawable)
187 {
188 struct vl_dri_screen *scrn = (struct vl_dri_screen *)vscreen;
189
190 struct winsys_handle dri2_handle;
191 struct pipe_resource templ, *tex;
192
193 xcb_dri2_get_buffers_reply_t *reply;
194 xcb_dri2_dri2_buffer_t *buffers, *back_left = NULL;
195
196 unsigned depth = ((xcb_screen_t *)(vscreen->xcb_screen))->root_depth;
197 unsigned i;
198
199 assert(scrn);
200
201 vl_dri2_set_drawable(scrn, (Drawable)drawable);
202 reply = vl_dri2_get_flush_reply(scrn);
203 if (!reply) {
204 xcb_dri2_get_buffers_cookie_t cookie;
205 cookie = xcb_dri2_get_buffers_unchecked(scrn->conn, (Drawable)drawable,
206 1, 1, attachments);
207 reply = xcb_dri2_get_buffers_reply(scrn->conn, cookie, NULL);
208 }
209 if (!reply)
210 return NULL;
211
212 buffers = xcb_dri2_get_buffers_buffers(reply);
213 if (!buffers) {
214 free(reply);
215 return NULL;
216 }
217
218 for (i = 0; i < reply->count; ++i) {
219 if (buffers[i].attachment == XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT) {
220 back_left = &buffers[i];
221 break;
222 }
223 }
224
225 if (i == reply->count || !back_left) {
226 free(reply);
227 return NULL;
228 }
229
230 if (reply->width != scrn->width || reply->height != scrn->height) {
231 vl_compositor_reset_dirty_area(&scrn->dirty_areas[0]);
232 vl_compositor_reset_dirty_area(&scrn->dirty_areas[1]);
233 scrn->width = reply->width;
234 scrn->height = reply->height;
235
236 } else if (back_left->name != scrn->buffer_names[scrn->current_buffer]) {
237 vl_compositor_reset_dirty_area(&scrn->dirty_areas[scrn->current_buffer]);
238 scrn->buffer_names[scrn->current_buffer] = back_left->name;
239 }
240
241 memset(&dri2_handle, 0, sizeof(dri2_handle));
242 dri2_handle.type = WINSYS_HANDLE_TYPE_SHARED;
243 dri2_handle.handle = back_left->name;
244 dri2_handle.stride = back_left->pitch;
245 dri2_handle.modifier = DRM_FORMAT_MOD_INVALID;
246
247 memset(&templ, 0, sizeof(templ));
248 templ.target = PIPE_TEXTURE_2D;
249 templ.format = vl_dri2_format_for_depth(vscreen, depth);
250 templ.last_level = 0;
251 templ.width0 = reply->width;
252 templ.height0 = reply->height;
253 templ.depth0 = 1;
254 templ.array_size = 1;
255 templ.usage = PIPE_USAGE_DEFAULT;
256 templ.bind = PIPE_BIND_RENDER_TARGET;
257 templ.flags = 0;
258
259 tex = scrn->base.pscreen->resource_from_handle(scrn->base.pscreen, &templ,
260 &dri2_handle,
261 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
262 free(reply);
263
264 return tex;
265 }
266
267 static struct u_rect *
vl_dri2_screen_get_dirty_area(struct vl_screen * vscreen)268 vl_dri2_screen_get_dirty_area(struct vl_screen *vscreen)
269 {
270 struct vl_dri_screen *scrn = (struct vl_dri_screen *)vscreen;
271 assert(scrn);
272 return &scrn->dirty_areas[scrn->current_buffer];
273 }
274
275 static uint64_t
vl_dri2_screen_get_timestamp(struct vl_screen * vscreen,void * drawable)276 vl_dri2_screen_get_timestamp(struct vl_screen *vscreen, void *drawable)
277 {
278 struct vl_dri_screen *scrn = (struct vl_dri_screen *)vscreen;
279 xcb_dri2_get_msc_cookie_t cookie;
280 xcb_dri2_get_msc_reply_t *reply;
281
282 assert(scrn);
283
284 vl_dri2_set_drawable(scrn, (Drawable)drawable);
285 if (!scrn->last_ust) {
286 cookie = xcb_dri2_get_msc_unchecked(scrn->conn, (Drawable)drawable);
287 reply = xcb_dri2_get_msc_reply(scrn->conn, cookie, NULL);
288
289 if (reply) {
290 vl_dri2_handle_stamps(scrn, reply->ust_hi, reply->ust_lo,
291 reply->msc_hi, reply->msc_lo);
292 free(reply);
293 }
294 }
295 return scrn->last_ust;
296 }
297
298 static void
vl_dri2_screen_set_next_timestamp(struct vl_screen * vscreen,uint64_t stamp)299 vl_dri2_screen_set_next_timestamp(struct vl_screen *vscreen, uint64_t stamp)
300 {
301 struct vl_dri_screen *scrn = (struct vl_dri_screen *)vscreen;
302 assert(scrn);
303 if (stamp && scrn->last_ust && scrn->ns_frame && scrn->last_msc)
304 scrn->next_msc = ((int64_t)stamp - scrn->last_ust + scrn->ns_frame/2) /
305 scrn->ns_frame + scrn->last_msc;
306 else
307 scrn->next_msc = 0;
308 }
309
310 static void *
vl_dri2_screen_get_private(struct vl_screen * vscreen)311 vl_dri2_screen_get_private(struct vl_screen *vscreen)
312 {
313 return vscreen;
314 }
315
316 static xcb_screen_t *
get_xcb_screen(xcb_screen_iterator_t iter,int screen)317 get_xcb_screen(xcb_screen_iterator_t iter, int screen)
318 {
319 for (; iter.rem; --screen, xcb_screen_next(&iter))
320 if (screen == 0)
321 return iter.data;
322
323 return NULL;
324 }
325 #endif
326
327 static xcb_visualtype_t *
get_xcb_visualtype_for_depth(struct vl_screen * vscreen,int depth)328 get_xcb_visualtype_for_depth(struct vl_screen *vscreen, int depth)
329 {
330 xcb_visualtype_iterator_t visual_iter;
331 xcb_screen_t *screen = vscreen->xcb_screen;
332 xcb_depth_iterator_t depth_iter;
333
334 if (!screen)
335 return NULL;
336
337 depth_iter = xcb_screen_allowed_depths_iterator(screen);
338 for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
339 if (depth_iter.data->depth != depth)
340 continue;
341
342 visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
343 if (visual_iter.rem)
344 return visual_iter.data;
345 }
346
347 return NULL;
348 }
349
350 static uint32_t
get_red_mask_for_depth(struct vl_screen * vscreen,int depth)351 get_red_mask_for_depth(struct vl_screen *vscreen, int depth)
352 {
353 xcb_visualtype_t *visual = get_xcb_visualtype_for_depth(vscreen, depth);
354
355 if (visual) {
356 return visual->red_mask;
357 }
358
359 return 0;
360 }
361
362 uint32_t
vl_dri2_format_for_depth(struct vl_screen * vscreen,int depth)363 vl_dri2_format_for_depth(struct vl_screen *vscreen, int depth)
364 {
365 switch (depth) {
366 case 24:
367 return PIPE_FORMAT_B8G8R8X8_UNORM;
368 case 30:
369 /* Different preferred formats for different hw */
370 if (get_red_mask_for_depth(vscreen, 30) == 0x3ff)
371 return PIPE_FORMAT_R10G10B10X2_UNORM;
372 else
373 return PIPE_FORMAT_B10G10R10X2_UNORM;
374 default:
375 return PIPE_FORMAT_NONE;
376 }
377 }
378
379 xcb_screen_t *
vl_dri_get_screen_for_root(xcb_connection_t * conn,xcb_window_t root)380 vl_dri_get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
381 {
382 xcb_screen_iterator_t screen_iter =
383 xcb_setup_roots_iterator(xcb_get_setup(conn));
384
385 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
386 if (screen_iter.data->root == root)
387 return screen_iter.data;
388 }
389
390 return NULL;
391 }
392
393 #ifdef HAVE_X11_DRI2
394 struct vl_screen *
vl_dri2_screen_create(Display * display,int screen)395 vl_dri2_screen_create(Display *display, int screen)
396 {
397 struct vl_dri_screen *scrn;
398 const xcb_query_extension_reply_t *extension;
399 xcb_dri2_query_version_cookie_t dri2_query_cookie;
400 xcb_dri2_query_version_reply_t *dri2_query = NULL;
401 xcb_dri2_connect_cookie_t connect_cookie;
402 xcb_dri2_connect_reply_t *connect = NULL;
403 xcb_dri2_authenticate_cookie_t authenticate_cookie;
404 xcb_dri2_authenticate_reply_t *authenticate = NULL;
405 xcb_screen_iterator_t s;
406 xcb_generic_error_t *error = NULL;
407 char *device_name;
408 int fd, device_name_length;
409 unsigned driverType;
410
411 drm_magic_t magic;
412
413 assert(display);
414
415 scrn = CALLOC_STRUCT(vl_dri_screen);
416 if (!scrn)
417 return NULL;
418
419 scrn->conn = XGetXCBConnection(display);
420 if (!scrn->conn)
421 goto free_screen;
422
423 xcb_prefetch_extension_data(scrn->conn, &xcb_dri2_id);
424
425 extension = xcb_get_extension_data(scrn->conn, &xcb_dri2_id);
426 if (!(extension && extension->present))
427 goto free_screen;
428
429 dri2_query_cookie = xcb_dri2_query_version (scrn->conn,
430 XCB_DRI2_MAJOR_VERSION,
431 XCB_DRI2_MINOR_VERSION);
432 dri2_query = xcb_dri2_query_version_reply (scrn->conn, dri2_query_cookie, &error);
433 if (dri2_query == NULL || error != NULL || dri2_query->minor_version < 2)
434 goto free_query;
435
436 s = xcb_setup_roots_iterator(xcb_get_setup(scrn->conn));
437 scrn->base.xcb_screen = get_xcb_screen(s, screen);
438 if (!scrn->base.xcb_screen)
439 goto free_query;
440
441 driverType = XCB_DRI2_DRIVER_TYPE_DRI;
442 {
443 char *prime = getenv("DRI_PRIME");
444 if (prime) {
445 unsigned primeid;
446 errno = 0;
447 primeid = strtoul(prime, NULL, 0);
448 if (errno == 0)
449 driverType |=
450 ((primeid & DRI2DriverPrimeMask) << DRI2DriverPrimeShift);
451 }
452 }
453
454 connect_cookie = xcb_dri2_connect_unchecked(
455 scrn->conn, ((xcb_screen_t *)(scrn->base.xcb_screen))->root, driverType);
456 connect = xcb_dri2_connect_reply(scrn->conn, connect_cookie, NULL);
457 if (connect == NULL ||
458 connect->driver_name_length + connect->device_name_length == 0)
459 goto free_connect;
460
461 device_name_length = xcb_dri2_connect_device_name_length(connect);
462 device_name = CALLOC(1, device_name_length + 1);
463 if (!device_name)
464 goto free_connect;
465 memcpy(device_name, xcb_dri2_connect_device_name(connect), device_name_length);
466 fd = loader_open_device(device_name);
467 free(device_name);
468
469 if (fd < 0)
470 goto free_connect;
471
472 if (drmGetMagic(fd, &magic))
473 goto close_fd;
474
475 authenticate_cookie = xcb_dri2_authenticate_unchecked(
476 scrn->conn, ((xcb_screen_t *)(scrn->base.xcb_screen))->root, magic);
477 authenticate = xcb_dri2_authenticate_reply(scrn->conn, authenticate_cookie, NULL);
478
479 if (authenticate == NULL || !authenticate->authenticated)
480 goto free_authenticate;
481
482 if (pipe_loader_drm_probe_fd(&scrn->base.dev, fd, false))
483 scrn->base.pscreen = pipe_loader_create_screen(scrn->base.dev, false);
484
485 if (!scrn->base.pscreen)
486 goto release_pipe;
487
488 scrn->base.destroy = vl_dri2_screen_destroy;
489 scrn->base.texture_from_drawable = vl_dri2_screen_texture_from_drawable;
490 scrn->base.get_dirty_area = vl_dri2_screen_get_dirty_area;
491 scrn->base.get_timestamp = vl_dri2_screen_get_timestamp;
492 scrn->base.set_next_timestamp = vl_dri2_screen_set_next_timestamp;
493 scrn->base.get_private = vl_dri2_screen_get_private;
494 scrn->base.pscreen->flush_frontbuffer = vl_dri2_flush_frontbuffer;
495 vl_compositor_reset_dirty_area(&scrn->dirty_areas[0]);
496 vl_compositor_reset_dirty_area(&scrn->dirty_areas[1]);
497
498 /* The pipe loader duplicates the fd */
499 close(fd);
500 free(authenticate);
501 free(connect);
502 free(dri2_query);
503 free(error);
504
505 return &scrn->base;
506
507 release_pipe:
508 if (scrn->base.dev)
509 pipe_loader_release(&scrn->base.dev, 1);
510 free_authenticate:
511 free(authenticate);
512 close_fd:
513 close(fd);
514 free_connect:
515 free(connect);
516 free_query:
517 free(dri2_query);
518 free(error);
519
520 free_screen:
521 FREE(scrn);
522 return NULL;
523 }
524
525 static void
vl_dri2_screen_destroy(struct vl_screen * vscreen)526 vl_dri2_screen_destroy(struct vl_screen *vscreen)
527 {
528 struct vl_dri_screen *scrn = (struct vl_dri_screen *)vscreen;
529
530 assert(vscreen);
531
532 if (scrn->flushed) {
533 free(xcb_dri2_swap_buffers_reply(scrn->conn, scrn->swap_cookie, NULL));
534 free(xcb_dri2_wait_sbc_reply(scrn->conn, scrn->wait_cookie, NULL));
535 free(xcb_dri2_get_buffers_reply(scrn->conn, scrn->buffers_cookie, NULL));
536 }
537
538 vl_dri2_destroy_drawable(scrn);
539 scrn->base.pscreen->destroy(scrn->base.pscreen);
540 pipe_loader_release(&scrn->base.dev, 1);
541 /* There is no user provided fd */
542 FREE(scrn);
543 }
544 #endif
545