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