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