3b1b87f9523a6abbe7f8d2a0d59ebd10d80b9371
[mesa.git] / src / gallium / auxiliary / vl / vl_winsys_dri.c
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 #include <fcntl.h>
33
34 #include <X11/Xlib-xcb.h>
35 #include <X11/extensions/dri2tokens.h>
36 #include <xcb/dri2.h>
37 #include <xf86drm.h>
38 #include <errno.h>
39
40 #include "loader.h"
41
42 #include "pipe/p_screen.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_state.h"
45 #include "pipe-loader/pipe_loader.h"
46 #include "state_tracker/drm_driver.h"
47
48 #include "util/u_memory.h"
49 #include "util/u_hash.h"
50 #include "util/u_hash_table.h"
51 #include "util/u_inlines.h"
52
53 #include "vl/vl_compositor.h"
54 #include "vl/vl_winsys.h"
55
56 struct vl_dri_screen
57 {
58 struct vl_screen base;
59 xcb_connection_t *conn;
60 xcb_drawable_t drawable;
61
62 unsigned width, height;
63
64 bool current_buffer;
65 uint32_t buffer_names[2];
66 struct u_rect dirty_areas[2];
67
68 bool flushed;
69 xcb_dri2_swap_buffers_cookie_t swap_cookie;
70 xcb_dri2_wait_sbc_cookie_t wait_cookie;
71 xcb_dri2_get_buffers_cookie_t buffers_cookie;
72
73 int64_t last_ust, ns_frame, last_msc, next_msc;
74 };
75
76 static const unsigned int attachments[1] = { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT };
77
78 static void
79 vl_dri2_handle_stamps(struct vl_dri_screen* scrn,
80 uint32_t ust_hi, uint32_t ust_lo,
81 uint32_t msc_hi, uint32_t msc_lo)
82 {
83 int64_t ust = ((((uint64_t)ust_hi) << 32) | ust_lo) * 1000;
84 int64_t msc = (((uint64_t)msc_hi) << 32) | msc_lo;
85
86 if (scrn->last_ust && scrn->last_msc && (ust > scrn->last_ust) && (msc > scrn->last_msc))
87 scrn->ns_frame = (ust - scrn->last_ust) / (msc - scrn->last_msc);
88
89 scrn->last_ust = ust;
90 scrn->last_msc = msc;
91 }
92
93 static xcb_dri2_get_buffers_reply_t*
94 vl_dri2_get_flush_reply(struct vl_dri_screen *scrn)
95 {
96 xcb_dri2_wait_sbc_reply_t *wait_sbc_reply;
97
98 assert(scrn);
99
100 if (!scrn->flushed)
101 return NULL;
102
103 scrn->flushed = false;
104
105 free(xcb_dri2_swap_buffers_reply(scrn->conn, scrn->swap_cookie, NULL));
106
107 wait_sbc_reply = xcb_dri2_wait_sbc_reply(scrn->conn, scrn->wait_cookie, NULL);
108 if (!wait_sbc_reply)
109 return NULL;
110 vl_dri2_handle_stamps(scrn, wait_sbc_reply->ust_hi, wait_sbc_reply->ust_lo,
111 wait_sbc_reply->msc_hi, wait_sbc_reply->msc_lo);
112 free(wait_sbc_reply);
113
114 return xcb_dri2_get_buffers_reply(scrn->conn, scrn->buffers_cookie, NULL);
115 }
116
117 static void
118 vl_dri2_flush_frontbuffer(struct pipe_screen *screen,
119 struct pipe_resource *resource,
120 unsigned level, unsigned layer,
121 void *context_private, struct pipe_box *sub_box)
122 {
123 struct vl_dri_screen *scrn = (struct vl_dri_screen*)context_private;
124 uint32_t msc_hi, msc_lo;
125
126 assert(screen);
127 assert(resource);
128 assert(context_private);
129
130 free(vl_dri2_get_flush_reply(scrn));
131
132 msc_hi = scrn->next_msc >> 32;
133 msc_lo = scrn->next_msc & 0xFFFFFFFF;
134
135 scrn->swap_cookie = xcb_dri2_swap_buffers_unchecked(scrn->conn, scrn->drawable, msc_hi, msc_lo, 0, 0, 0, 0);
136 scrn->wait_cookie = xcb_dri2_wait_sbc_unchecked(scrn->conn, scrn->drawable, 0, 0);
137 scrn->buffers_cookie = xcb_dri2_get_buffers_unchecked(scrn->conn, scrn->drawable, 1, 1, attachments);
138
139 scrn->flushed = true;
140 scrn->current_buffer = !scrn->current_buffer;
141 }
142
143 static void
144 vl_dri2_destroy_drawable(struct vl_dri_screen *scrn)
145 {
146 xcb_void_cookie_t destroy_cookie;
147 if (scrn->drawable) {
148 free(vl_dri2_get_flush_reply(scrn));
149 destroy_cookie = xcb_dri2_destroy_drawable_checked(scrn->conn, scrn->drawable);
150 /* ignore any error here, since the drawable can be destroyed long ago */
151 free(xcb_request_check(scrn->conn, destroy_cookie));
152 }
153 }
154
155 static void
156 vl_dri2_set_drawable(struct vl_dri_screen *scrn, Drawable drawable)
157 {
158 assert(scrn);
159 assert(drawable);
160
161 if (scrn->drawable == drawable)
162 return;
163
164 vl_dri2_destroy_drawable(scrn);
165
166 xcb_dri2_create_drawable(scrn->conn, drawable);
167 scrn->current_buffer = false;
168 vl_compositor_reset_dirty_area(&scrn->dirty_areas[0]);
169 vl_compositor_reset_dirty_area(&scrn->dirty_areas[1]);
170 scrn->drawable = drawable;
171 }
172
173 struct pipe_resource*
174 vl_screen_texture_from_drawable(struct vl_screen *vscreen, Drawable drawable)
175 {
176 struct vl_dri_screen *scrn = (struct vl_dri_screen*)vscreen;
177
178 struct winsys_handle dri2_handle;
179 struct pipe_resource template, *tex;
180
181 xcb_dri2_get_buffers_reply_t *reply;
182 xcb_dri2_dri2_buffer_t *buffers, *back_left;
183
184 unsigned i;
185
186 assert(scrn);
187
188 vl_dri2_set_drawable(scrn, drawable);
189 reply = vl_dri2_get_flush_reply(scrn);
190 if (!reply) {
191 xcb_dri2_get_buffers_cookie_t cookie;
192 cookie = xcb_dri2_get_buffers_unchecked(scrn->conn, drawable, 1, 1, attachments);
193 reply = xcb_dri2_get_buffers_reply(scrn->conn, cookie, NULL);
194 }
195 if (!reply)
196 return NULL;
197
198 buffers = xcb_dri2_get_buffers_buffers(reply);
199 if (!buffers) {
200 free(reply);
201 return NULL;
202 }
203
204 for (i = 0; i < reply->count; ++i) {
205 if (buffers[i].attachment == XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT) {
206 back_left = &buffers[i];
207 break;
208 }
209 }
210
211 if (i == reply->count) {
212 free(reply);
213 return NULL;
214 }
215
216 if (reply->width != scrn->width || reply->height != scrn->height) {
217 vl_compositor_reset_dirty_area(&scrn->dirty_areas[0]);
218 vl_compositor_reset_dirty_area(&scrn->dirty_areas[1]);
219 scrn->width = reply->width;
220 scrn->height = reply->height;
221
222 } else if (back_left->name != scrn->buffer_names[scrn->current_buffer]) {
223 vl_compositor_reset_dirty_area(&scrn->dirty_areas[scrn->current_buffer]);
224 scrn->buffer_names[scrn->current_buffer] = back_left->name;
225 }
226
227 memset(&dri2_handle, 0, sizeof(dri2_handle));
228 dri2_handle.type = DRM_API_HANDLE_TYPE_SHARED;
229 dri2_handle.handle = back_left->name;
230 dri2_handle.stride = back_left->pitch;
231
232 memset(&template, 0, sizeof(template));
233 template.target = PIPE_TEXTURE_2D;
234 template.format = PIPE_FORMAT_B8G8R8X8_UNORM;
235 template.last_level = 0;
236 template.width0 = reply->width;
237 template.height0 = reply->height;
238 template.depth0 = 1;
239 template.array_size = 1;
240 template.usage = PIPE_USAGE_DEFAULT;
241 template.bind = PIPE_BIND_RENDER_TARGET;
242 template.flags = 0;
243
244 tex = scrn->base.pscreen->resource_from_handle(scrn->base.pscreen, &template, &dri2_handle);
245 free(reply);
246
247 return tex;
248 }
249
250 struct u_rect *
251 vl_screen_get_dirty_area(struct vl_screen *vscreen)
252 {
253 struct vl_dri_screen *scrn = (struct vl_dri_screen*)vscreen;
254 assert(scrn);
255 return &scrn->dirty_areas[scrn->current_buffer];
256 }
257
258 uint64_t
259 vl_screen_get_timestamp(struct vl_screen *vscreen, Drawable drawable)
260 {
261 struct vl_dri_screen *scrn = (struct vl_dri_screen*)vscreen;
262 xcb_dri2_get_msc_cookie_t cookie;
263 xcb_dri2_get_msc_reply_t *reply;
264
265 assert(scrn);
266
267 vl_dri2_set_drawable(scrn, drawable);
268 if (!scrn->last_ust) {
269 cookie = xcb_dri2_get_msc_unchecked(scrn->conn, drawable);
270 reply = xcb_dri2_get_msc_reply(scrn->conn, cookie, NULL);
271
272 if (reply) {
273 vl_dri2_handle_stamps(scrn, reply->ust_hi, reply->ust_lo,
274 reply->msc_hi, reply->msc_lo);
275 free(reply);
276 }
277 }
278 return scrn->last_ust;
279 }
280
281 void
282 vl_screen_set_next_timestamp(struct vl_screen *vscreen, uint64_t stamp)
283 {
284 struct vl_dri_screen *scrn = (struct vl_dri_screen*)vscreen;
285 assert(scrn);
286 if (stamp && scrn->last_ust && scrn->ns_frame && scrn->last_msc)
287 scrn->next_msc = ((int64_t)stamp - scrn->last_ust + scrn->ns_frame/2) / scrn->ns_frame + scrn->last_msc;
288 else
289 scrn->next_msc = 0;
290 }
291
292 void*
293 vl_screen_get_private(struct vl_screen *vscreen)
294 {
295 return vscreen;
296 }
297
298 static xcb_screen_t *
299 get_xcb_screen(xcb_screen_iterator_t iter, int screen)
300 {
301 for (; iter.rem; --screen, xcb_screen_next(&iter))
302 if (screen == 0)
303 return iter.data;
304
305 return NULL;
306 }
307
308 struct vl_screen*
309 vl_screen_create(Display *display, int screen)
310 {
311 struct vl_dri_screen *scrn;
312 const xcb_query_extension_reply_t *extension;
313 xcb_dri2_query_version_cookie_t dri2_query_cookie;
314 xcb_dri2_query_version_reply_t *dri2_query = NULL;
315 xcb_dri2_connect_cookie_t connect_cookie;
316 xcb_dri2_connect_reply_t *connect = NULL;
317 xcb_dri2_authenticate_cookie_t authenticate_cookie;
318 xcb_dri2_authenticate_reply_t *authenticate = NULL;
319 xcb_screen_iterator_t s;
320 xcb_generic_error_t *error = NULL;
321 char *device_name;
322 int fd, device_name_length;
323 unsigned int driverType;
324
325 drm_magic_t magic;
326
327 assert(display);
328
329 scrn = CALLOC_STRUCT(vl_dri_screen);
330 if (!scrn)
331 return NULL;
332
333 scrn->conn = XGetXCBConnection(display);
334 if (!scrn->conn)
335 goto free_screen;
336
337 xcb_prefetch_extension_data(scrn->conn, &xcb_dri2_id);
338
339 extension = xcb_get_extension_data(scrn->conn, &xcb_dri2_id);
340 if (!(extension && extension->present))
341 goto free_screen;
342
343 dri2_query_cookie = xcb_dri2_query_version (scrn->conn, XCB_DRI2_MAJOR_VERSION, XCB_DRI2_MINOR_VERSION);
344 dri2_query = xcb_dri2_query_version_reply (scrn->conn, dri2_query_cookie, &error);
345 if (dri2_query == NULL || error != NULL || dri2_query->minor_version < 2)
346 goto free_query;
347
348 s = xcb_setup_roots_iterator(xcb_get_setup(scrn->conn));
349
350 driverType = XCB_DRI2_DRIVER_TYPE_DRI;
351 #ifdef DRI2DriverPrimeShift
352 {
353 char *prime = getenv("DRI_PRIME");
354 if (prime) {
355 unsigned int primeid;
356 errno = 0;
357 primeid = strtoul(prime, NULL, 0);
358 if (errno == 0)
359 driverType |=
360 ((primeid & DRI2DriverPrimeMask) << DRI2DriverPrimeShift);
361 }
362 }
363 #endif
364
365 connect_cookie = xcb_dri2_connect_unchecked(scrn->conn, get_xcb_screen(s, screen)->root, driverType);
366 connect = xcb_dri2_connect_reply(scrn->conn, connect_cookie, NULL);
367 if (connect == NULL || connect->driver_name_length + connect->device_name_length == 0)
368 goto free_connect;
369
370 device_name_length = xcb_dri2_connect_device_name_length(connect);
371 device_name = CALLOC(1, device_name_length + 1);
372 if (!device_name)
373 goto free_connect;
374 memcpy(device_name, xcb_dri2_connect_device_name(connect), device_name_length);
375 fd = loader_open_device(device_name);
376 free(device_name);
377
378 if (fd < 0)
379 goto free_connect;
380
381 if (drmGetMagic(fd, &magic))
382 goto free_connect;
383
384 authenticate_cookie = xcb_dri2_authenticate_unchecked(scrn->conn, get_xcb_screen(s, screen)->root, magic);
385 authenticate = xcb_dri2_authenticate_reply(scrn->conn, authenticate_cookie, NULL);
386
387 if (authenticate == NULL || !authenticate->authenticated)
388 goto free_authenticate;
389
390 #if GALLIUM_STATIC_TARGETS
391 scrn->base.pscreen = dd_create_screen(fd);
392 #else
393 if (pipe_loader_drm_probe_fd(&scrn->base.dev, fd))
394 scrn->base.pscreen = pipe_loader_create_screen(scrn->base.dev, PIPE_SEARCH_DIR);
395 #endif // GALLIUM_STATIC_TARGETS
396
397 if (!scrn->base.pscreen)
398 goto release_pipe;
399
400 scrn->base.pscreen->flush_frontbuffer = vl_dri2_flush_frontbuffer;
401 vl_compositor_reset_dirty_area(&scrn->dirty_areas[0]);
402 vl_compositor_reset_dirty_area(&scrn->dirty_areas[1]);
403
404 free(authenticate);
405 free(connect);
406 free(dri2_query);
407 free(error);
408
409 return &scrn->base;
410
411 release_pipe:
412 #if !GALLIUM_STATIC_TARGETS
413 if (scrn->base.dev)
414 pipe_loader_release(&scrn->base.dev, 1);
415 #endif // !GALLIUM_STATIC_TARGETS
416 free_authenticate:
417 free(authenticate);
418 free_connect:
419 free(connect);
420 free_query:
421 free(dri2_query);
422 free(error);
423
424 free_screen:
425 FREE(scrn);
426 return NULL;
427 }
428
429 void vl_screen_destroy(struct vl_screen *vscreen)
430 {
431 struct vl_dri_screen *scrn = (struct vl_dri_screen*)vscreen;
432
433 assert(vscreen);
434
435 if (scrn->flushed) {
436 free(xcb_dri2_swap_buffers_reply(scrn->conn, scrn->swap_cookie, NULL));
437 free(xcb_dri2_wait_sbc_reply(scrn->conn, scrn->wait_cookie, NULL));
438 free(xcb_dri2_get_buffers_reply(scrn->conn, scrn->buffers_cookie, NULL));
439 }
440
441 vl_dri2_destroy_drawable(scrn);
442 scrn->base.pscreen->destroy(scrn->base.pscreen);
443 #if !GALLIUM_STATIC_TARGETS
444 pipe_loader_release(&scrn->base.dev, 1);
445 #endif // !GALLIUM_STATIC_TARGETS
446 FREE(scrn);
447 }