egl: Implement eglCopyBuffers for DRI2, make pixmap and pbuffers actually work
[mesa.git] / src / egl / drivers / dri2 / egl_dri2.c
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 */
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <limits.h>
32 #include <dlfcn.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <xf86drm.h>
37 #include <GL/gl.h>
38 #include <GL/internal/dri_interface.h>
39 #include <xcb/xcb.h>
40 #include <xcb/dri2.h>
41 #include <xcb/xfixes.h>
42 #include <X11/Xlib-xcb.h>
43
44 #include <glapi/glapi.h>
45 #include "eglconfigutil.h"
46 #include "eglconfig.h"
47 #include "eglcontext.h"
48 #include "egldisplay.h"
49 #include "egldriver.h"
50 #include "eglcurrent.h"
51 #include "egllog.h"
52 #include "eglsurface.h"
53
54 struct dri2_egl_driver
55 {
56 _EGLDriver base;
57 };
58
59 struct dri2_egl_display
60 {
61 xcb_connection_t *conn;
62 int dri2_major;
63 int dri2_minor;
64 __DRIscreen *dri_screen;
65 void *driver;
66 __DRIcoreExtension *core;
67 __DRIdri2Extension *dri2;
68 __DRI2flushExtension *flush;
69 int fd;
70
71 __DRIdri2LoaderExtension loader_extension;
72 const __DRIextension *extensions[2];
73 };
74
75 struct dri2_egl_context
76 {
77 _EGLContext base;
78 __DRIcontext *dri_context;
79 };
80
81 struct dri2_egl_surface
82 {
83 _EGLSurface base;
84 __DRIdrawable *dri_drawable;
85 xcb_drawable_t drawable;
86 __DRIbuffer buffers[5];
87 int buffer_count;
88 xcb_xfixes_region_t region;
89 int have_back;
90 int have_fake_front;
91 int swap_interval;
92 };
93
94 struct dri2_egl_config
95 {
96 _EGLConfig base;
97 const __DRIconfig *dri_config;
98 };
99
100 static struct dri2_egl_driver *
101 dri2_egl_driver(_EGLDriver *drv)
102 {
103 return (struct dri2_egl_driver *) drv;
104 }
105
106 static struct dri2_egl_display *
107 dri2_egl_display(_EGLDisplay *dpy)
108 {
109 return (struct dri2_egl_display *) dpy->DriverData;
110 }
111
112 static struct dri2_egl_context *
113 dri2_egl_context(_EGLContext *ctx)
114 {
115 return (struct dri2_egl_context *) ctx;
116 }
117
118 static struct dri2_egl_surface *
119 dri2_egl_surface(_EGLSurface *surf)
120 {
121 return (struct dri2_egl_surface *) surf;
122 }
123
124 static struct dri2_egl_config *
125 dri2_egl_config(_EGLConfig *conf)
126 {
127 return (struct dri2_egl_config *) conf;
128 }
129
130 EGLint dri2_to_egl_attribute_map[] = {
131 0,
132 EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */
133 EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */
134 EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */
135 EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */
136 EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */
137 0, /* __DRI_ATTRIB_LUMINANCE_SIZE */
138 EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */
139 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
140 EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */
141 EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */
142 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
143 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
144 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
145 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
146 EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
147 EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */
148 0, /* __DRI_ATTRIB_RENDER_TYPE, */
149 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
150 0, /* __DRI_ATTRIB_CONFORMANT */
151 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
152 0, /* __DRI_ATTRIB_STEREO */
153 0, /* __DRI_ATTRIB_AUX_BUFFERS */
154 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
155 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
156 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
157 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
158 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
159 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
160 0, /* __DRI_ATTRIB_FLOAT_MODE */
161 0, /* __DRI_ATTRIB_RED_MASK */
162 0, /* __DRI_ATTRIB_GREEN_MASK */
163 0, /* __DRI_ATTRIB_BLUE_MASK */
164 0, /* __DRI_ATTRIB_ALPHA_MASK */
165 EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
166 EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
167 EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
168 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
169 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
170 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
171 0, /* __DRI_ATTRIB_SWAP_METHOD */
172 EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
173 EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
174 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
175 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
176 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
177 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
178 0, /* __DRI_ATTRIB_YINVERTED */
179 };
180
181 static void
182 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id)
183 {
184 struct dri2_egl_config *conf;
185 struct dri2_egl_display *dri2_dpy;
186 unsigned int attrib, value, double_buffer;
187 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
188 int i;
189
190 dri2_dpy = disp->DriverData;
191 conf = malloc(sizeof *conf);
192 if (conf == NULL)
193 return;
194
195 conf->dri_config = dri_config;
196 _eglInitConfig(&conf->base, disp, id);
197
198 i = 0;
199 while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) {
200 switch (attrib) {
201 case 0:
202 break;
203
204 case __DRI_ATTRIB_RENDER_TYPE:
205 if (value & __DRI_ATTRIB_RGBA_BIT)
206 value = EGL_RGB_BUFFER;
207 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
208 value = EGL_LUMINANCE_BUFFER;
209 else
210 /* not valid */;
211 _eglSetConfigKey(&conf->base, EGL_COLOR_BUFFER_TYPE, value);
212 break;
213
214 case __DRI_ATTRIB_CONFIG_CAVEAT:
215 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
216 value = EGL_NON_CONFORMANT_CONFIG;
217 else if (value & __DRI_ATTRIB_SLOW_BIT)
218 value = EGL_SLOW_CONFIG;
219 else
220 value = EGL_NONE;
221 _eglSetConfigKey(&conf->base, EGL_CONFIG_CAVEAT, value);
222 break;
223
224 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
225 bind_to_texture_rgb = value;
226 break;
227
228 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
229 bind_to_texture_rgba = value;
230 break;
231
232 case __DRI_ATTRIB_DOUBLE_BUFFER:
233 double_buffer = value;
234 break;
235
236 default:
237 key = dri2_to_egl_attribute_map[attrib];
238 if (key != 0)
239 _eglSetConfigKey(&conf->base, key, value);
240 break;
241 }
242 }
243
244 /* EGL_SWAP_BEHAVIOR_PRESERVED_BIT */
245
246 if (double_buffer) {
247 /* FIXME: Figure out how to get the visual ID and types */
248 _eglSetConfigKey(&conf->base, EGL_SURFACE_TYPE, EGL_WINDOW_BIT);
249 _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_ID, 0x21);
250 _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_TYPE,
251 XCB_VISUAL_CLASS_TRUE_COLOR);
252 } else {
253 _eglSetConfigKey(&conf->base,
254 EGL_SURFACE_TYPE, EGL_PIXMAP_BIT | EGL_PBUFFER_BIT);
255 _eglSetConfigKey(&conf->base,
256 EGL_BIND_TO_TEXTURE_RGB, bind_to_texture_rgb);
257 _eglSetConfigKey(&conf->base,
258 EGL_BIND_TO_TEXTURE_RGBA, bind_to_texture_rgba);
259 }
260
261 /* EGL_OPENGL_ES_BIT, EGL_OPENVG_BIT, EGL_OPENGL_ES2_BIT */
262 _eglSetConfigKey(&conf->base, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT);
263 _eglSetConfigKey(&conf->base, EGL_CONFORMANT, EGL_OPENGL_BIT);
264
265 if (!_eglValidateConfig(&conf->base, EGL_FALSE)) {
266 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
267 free(conf);
268 return;
269 }
270
271 _eglAddConfig(disp, &conf->base);
272 }
273
274 /**
275 * Process list of buffer received from the server
276 *
277 * Processes the list of buffers received in a reply from the server to either
278 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
279 */
280 static void
281 dri2_process_buffers(struct dri2_egl_surface *dri2_surf,
282 xcb_dri2_dri2_buffer_t *buffers, unsigned count)
283 {
284 struct dri2_egl_display *dri2_dpy =
285 dri2_egl_display(dri2_surf->base.Resource.Display);
286 xcb_rectangle_t rectangle;
287 int i;
288
289 dri2_surf->buffer_count = count;
290 dri2_surf->have_fake_front = 0;
291 dri2_surf->have_back = 0;
292
293 /* This assumes the DRI2 buffer attachment tokens matches the
294 * __DRIbuffer tokens. */
295 for (i = 0; i < count; i++) {
296 dri2_surf->buffers[i].attachment = buffers[i].attachment;
297 dri2_surf->buffers[i].name = buffers[i].name;
298 dri2_surf->buffers[i].pitch = buffers[i].pitch;
299 dri2_surf->buffers[i].cpp = buffers[i].cpp;
300 dri2_surf->buffers[i].flags = buffers[i].flags;
301 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
302 dri2_surf->have_fake_front = 1;
303 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
304 dri2_surf->have_back = 1;
305 }
306
307 if (dri2_surf->region != XCB_NONE)
308 xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
309
310 rectangle.x = 0;
311 rectangle.y = 0;
312 rectangle.width = dri2_surf->base.Width;
313 rectangle.height = dri2_surf->base.Height;
314 dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
315 xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
316 }
317
318 static __DRIbuffer *
319 dri2_get_buffers(__DRIdrawable * driDrawable,
320 int *width, int *height,
321 unsigned int *attachments, int count,
322 int *out_count, void *loaderPrivate)
323 {
324 struct dri2_egl_surface *dri2_surf = loaderPrivate;
325 struct dri2_egl_display *dri2_dpy =
326 dri2_egl_display(dri2_surf->base.Resource.Display);
327 xcb_dri2_dri2_buffer_t *buffers;
328 xcb_dri2_get_buffers_reply_t *reply;
329 xcb_dri2_get_buffers_cookie_t cookie;
330
331 cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
332 dri2_surf->drawable,
333 count, count, attachments);
334 reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
335 buffers = xcb_dri2_get_buffers_buffers (reply);
336 if (buffers == NULL)
337 return NULL;
338
339 *out_count = reply->count;
340 dri2_surf->base.Width = *width = reply->width;
341 dri2_surf->base.Height = *height = reply->height;
342 dri2_process_buffers(dri2_surf, buffers, *out_count);
343
344 free(reply);
345
346 return dri2_surf->buffers;
347 }
348
349 static void
350 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
351 {
352 /* FIXME: Does EGL support front buffer rendering at all? */
353
354 #if 0
355 struct dri2_egl_surface *dri2_surf = loaderPrivate;
356
357 dri2WaitGL(dri2_surf);
358 #endif
359 }
360
361 static __DRIbuffer *
362 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
363 int *width, int *height,
364 unsigned int *attachments, int count,
365 int *out_count, void *loaderPrivate)
366 {
367 struct dri2_egl_surface *dri2_surf = loaderPrivate;
368 struct dri2_egl_display *dri2_dpy =
369 dri2_egl_display(dri2_surf->base.Resource.Display);
370 xcb_dri2_dri2_buffer_t *buffers;
371 xcb_dri2_get_buffers_with_format_reply_t *reply;
372 xcb_dri2_get_buffers_with_format_cookie_t cookie;
373 xcb_dri2_attach_format_t *format_attachments;
374
375 format_attachments = (xcb_dri2_attach_format_t *) attachments;
376 cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
377 dri2_surf->drawable,
378 count, count,
379 format_attachments);
380
381 reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
382 cookie, NULL);
383 if (reply == NULL)
384 return NULL;
385
386 buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
387 dri2_surf->base.Width = *width = reply->width;
388 dri2_surf->base.Height = *height = reply->height;
389 *out_count = reply->count;
390 dri2_process_buffers(dri2_surf, buffers, *out_count);
391
392 free(reply);
393
394 return dri2_surf->buffers;
395 }
396
397 #ifdef GLX_USE_TLS
398 static const char dri_driver_format[] = "%.*s/tls/%.*s_dri.so";
399 #else
400 static const char dri_driver_format[] = "%.*s/%.*s_dri.so";
401 #endif
402
403 static const char dri_driver_path[] = DEFAULT_DRIVER_DIR;
404
405 /**
406 * Called via eglInitialize(), GLX_drv->API.Initialize().
407 */
408 static EGLBoolean
409 dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp,
410 EGLint *major, EGLint *minor)
411 {
412 const __DRIextension **extensions;
413 const __DRIconfig **driver_configs;
414 struct dri2_egl_display *dri2_dpy;
415 char path[PATH_MAX], *search_paths, *p, *next, *end;
416 xcb_xfixes_query_version_reply_t *xfixes_query;
417 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
418 xcb_dri2_query_version_reply_t *dri2_query;
419 xcb_dri2_query_version_cookie_t dri2_query_cookie;
420 xcb_dri2_connect_reply_t *connect = NULL;
421 xcb_dri2_connect_cookie_t connect_cookie;
422 xcb_dri2_authenticate_reply_t *authenticate;
423 xcb_dri2_authenticate_cookie_t authenticate_cookie;
424 xcb_generic_error_t *error;
425 drm_magic_t magic;
426 xcb_screen_iterator_t s;
427 int i;
428
429 dri2_dpy = malloc(sizeof *dri2_dpy);
430 if (!dri2_dpy)
431 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
432
433 disp->DriverData = (void *) dri2_dpy;
434 dri2_dpy->conn = XGetXCBConnection(disp->NativeDisplay);
435 if (!dri2_dpy->conn) {
436 dri2_dpy->conn = xcb_connect(0, 0);
437 if (!dri2_dpy->conn) {
438 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
439 free(dri2_dpy);
440 return EGL_FALSE;
441 }
442 }
443
444 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
445 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
446
447 xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
448 XCB_XFIXES_MAJOR_VERSION,
449 XCB_XFIXES_MINOR_VERSION);
450
451 dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
452 XCB_DRI2_MAJOR_VERSION,
453 XCB_DRI2_MINOR_VERSION);
454
455 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
456 connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn,
457 s.data->root,
458 XCB_DRI2_DRIVER_TYPE_DRI);
459
460 xfixes_query =
461 xcb_xfixes_query_version_reply (dri2_dpy->conn,
462 xfixes_query_cookie, &error);
463 if (xfixes_query == NULL ||
464 error != NULL || xfixes_query->major_version < 2) {
465 _eglLog(_EGL_FATAL, "DRI2: failed to query xfixes version");
466 free(error);
467 goto handle_error;
468 }
469 free(xfixes_query);
470
471 dri2_query =
472 xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
473 if (dri2_query == NULL || error != NULL) {
474 _eglLog(_EGL_FATAL, "DRI2: failed to query version");
475 free(error);
476 goto handle_error;
477 }
478 dri2_dpy->dri2_major = dri2_query->major_version;
479 dri2_dpy->dri2_minor = dri2_query->minor_version;
480 free(dri2_query);
481
482 connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
483 if (connect->driver_name_length == 0 && connect->device_name_length == 0) {
484 _eglLog(_EGL_FATAL, "DRI2: failed to authenticate");
485 goto handle_error;
486 }
487
488 search_paths = NULL;
489 if (geteuid() == getuid()) {
490 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
491 search_paths = getenv("LIBGL_DRIVERS_PATH");
492 }
493 if (search_paths == NULL)
494 search_paths = DEFAULT_DRIVER_DIR;
495
496 dri2_dpy->driver = NULL;
497 end = search_paths + strlen(search_paths);
498 for (p = search_paths; p < end && dri2_dpy->driver == NULL; p = next + 1) {
499 next = strchr(p, ':');
500 if (next == NULL)
501 next = end;
502
503 snprintf(path, sizeof path,
504 dri_driver_format,
505 next - p, p,
506 xcb_dri2_connect_driver_name_length (connect),
507 xcb_dri2_connect_driver_name (connect));
508
509 dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
510 }
511
512 if (dri2_dpy->driver == NULL) {
513 _eglLog(_EGL_FATAL,
514 "DRI2: failed to open any driver (search paths %s)",
515 search_paths);
516 goto handle_error;
517 }
518
519 _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path);
520 extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS);
521 if (extensions == NULL) {
522 _eglLog(_EGL_FATAL,
523 "DRI2: driver exports no extensions (%s)", dlerror());
524 goto handle_error;
525 }
526
527 for (i = 0; extensions[i]; i++) {
528 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
529 dri2_dpy->core = (__DRIcoreExtension *) extensions[i];
530 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
531 dri2_dpy->dri2 = (__DRIdri2Extension *) extensions[i];
532 }
533
534 if (dri2_dpy->core == NULL) {
535 _eglLog(_EGL_FATAL, "DRI2: driver has no core extension");
536 goto handle_error;
537 }
538
539 if (dri2_dpy->dri2 == NULL) {
540 _eglLog(_EGL_FATAL, "DRI2: driver has no dri2 extension");
541 goto handle_error;
542 }
543
544 snprintf(path, sizeof path, "%.*s",
545 xcb_dri2_connect_device_name_length (connect),
546 xcb_dri2_connect_device_name (connect));
547 dri2_dpy->fd = open (path, O_RDWR);
548 if (dri2_dpy->fd == -1) {
549 _eglLog(_EGL_FATAL,
550 "DRI2: could not open %s (%s)", path, strerror(errno));
551 goto handle_error;
552 }
553
554 if (drmGetMagic(dri2_dpy->fd, &magic)) {
555 _eglLog(_EGL_FATAL, "DRI2: failed to get drm magic");
556 goto handle_error;
557 }
558
559 authenticate_cookie = xcb_dri2_authenticate_unchecked (dri2_dpy->conn,
560 s.data->root, magic);
561 authenticate = xcb_dri2_authenticate_reply (dri2_dpy->conn,
562 authenticate_cookie, NULL);
563 if (authenticate == NULL || !authenticate->authenticated) {
564 _eglLog(_EGL_FATAL, "DRI2: failed to authenticate");
565 goto handle_error;
566 }
567
568 if (dri2_dpy->dri2_minor >= 1) {
569 dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER;
570 dri2_dpy->loader_extension.base.version = 3;
571 dri2_dpy->loader_extension.getBuffers = dri2_get_buffers;
572 dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
573 dri2_dpy->loader_extension.getBuffersWithFormat =
574 dri2_get_buffers_with_format;
575 } else {
576 dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER;
577 dri2_dpy->loader_extension.base.version = 2;
578 dri2_dpy->loader_extension.getBuffers = dri2_get_buffers;
579 dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
580 dri2_dpy->loader_extension.getBuffersWithFormat = NULL;
581 }
582
583 dri2_dpy->extensions[0] = &dri2_dpy->loader_extension.base;
584 dri2_dpy->extensions[1] = NULL;
585
586 dri2_dpy->dri_screen =
587 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions,
588 &driver_configs, dri2_dpy);
589
590 if (dri2_dpy->dri_screen == NULL) {
591 _eglLog(_EGL_FATAL, "DRI2: failed to create dri screen");
592 free(dri2_dpy);
593 goto handle_error;
594 }
595
596 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
597 for (i = 0; extensions[i]; i++) {
598 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0))
599 dri2_dpy->flush = (__DRI2flushExtension *) extensions[i];
600 }
601
602 if (dri2_dpy->flush == NULL) {
603 _eglLog(_EGL_FATAL, "DRI2: driver doesn't support the flush extension");
604 free(dri2_dpy);
605 goto handle_error;
606 }
607
608 for (i = 0; driver_configs[i]; i++)
609 dri2_add_config(disp, driver_configs[i], i + 1);
610 if (!disp->NumConfigs) {
611 _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
612 goto handle_error;
613 }
614
615 disp->ClientAPIsMask = EGL_OPENGL_BIT;
616
617 /* we're supporting EGL 1.4 */
618 *major = 1;
619 *minor = 4;
620
621 free (connect);
622 return EGL_TRUE;
623
624 handle_error:
625 free(connect);
626 free(dri2_dpy);
627 return EGL_FALSE;
628 }
629
630 /**
631 * Called via eglTerminate(), drv->API.Terminate().
632 */
633 static EGLBoolean
634 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
635 {
636 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
637
638 _eglReleaseDisplayResources(drv, disp);
639 _eglCleanupDisplay(disp);
640
641 close(dri2_dpy->fd);
642 dlclose(dri2_dpy->driver);
643 free(dri2_dpy);
644
645 disp->DriverData = NULL;
646
647 return EGL_TRUE;
648 }
649
650
651 /**
652 * Called via eglCreateContext(), drv->API.CreateContext().
653 */
654 static _EGLContext *
655 dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
656 _EGLContext *share_list, const EGLint *attrib_list)
657 {
658 struct dri2_egl_context *dri2_ctx;
659 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
660 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
661 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
662
663 dri2_ctx = malloc(sizeof *dri2_ctx);
664 if (!dri2_ctx) {
665 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
666 return NULL;
667 }
668
669 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list)) {
670 free(dri2_ctx);
671 return NULL;
672 }
673
674 dri2_ctx->dri_context =
675 dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen,
676 dri2_config->dri_config,
677 dri2_ctx_shared ?
678 dri2_ctx_shared->dri_context : NULL,
679 dri2_ctx);
680
681 if (!dri2_ctx->dri_context) {
682 free(dri2_ctx);
683 return NULL;
684 }
685
686 return &dri2_ctx->base;
687 }
688
689 static EGLBoolean
690 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
691 {
692 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
693 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
694
695 if (_eglIsSurfaceBound(surf))
696 return EGL_TRUE;
697
698 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
699
700 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
701
702 if (surf->Type == EGL_PBUFFER_BIT)
703 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
704
705 free(surf);
706
707 return EGL_TRUE;
708 }
709
710 /**
711 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
712 */
713 static EGLBoolean
714 dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
715 _EGLSurface *rsurf, _EGLContext *ctx)
716 {
717 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
718 struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf);
719 struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf);
720 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
721 __DRIdrawable *ddraw, *rdraw;
722 __DRIcontext *cctx;
723
724 /* bind the new context and return the "orphaned" one */
725 if (!_eglBindContext(&ctx, &dsurf, &rsurf))
726 return EGL_FALSE;
727
728 ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL;
729 rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL;
730 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
731
732 if ((cctx == NULL && ddraw == NULL && rdraw == NULL) ||
733 dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
734 if (dsurf)
735 dri2_destroy_surface(drv, disp, dsurf);
736 if (rsurf && rsurf != dsurf)
737 dri2_destroy_surface(drv, disp, rsurf);
738 if (ctx != NULL)
739 dri2_dpy->core->unbindContext(dri2_egl_context(ctx)->dri_context);
740
741 return EGL_TRUE;
742 } else {
743 _eglBindContext(&ctx, &dsurf, &rsurf);
744
745 return EGL_FALSE;
746 }
747 }
748
749 /**
750 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
751 */
752 static _EGLSurface *
753 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
754 _EGLConfig *conf, EGLNativeWindowType window,
755 const EGLint *attrib_list)
756 {
757 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
758 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
759 struct dri2_egl_surface *dri2_surf;
760 xcb_get_geometry_cookie_t cookie;
761 xcb_get_geometry_reply_t *reply;
762 xcb_screen_iterator_t s;
763 xcb_generic_error_t *error;
764
765 dri2_surf = malloc(sizeof *dri2_surf);
766 if (!dri2_surf) {
767 _eglError(EGL_BAD_ALLOC, "eglCreateWindowSurface");
768 return NULL;
769 }
770
771 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list)) {
772 free(dri2_surf);
773 return NULL;
774 }
775
776 dri2_surf->region = XCB_NONE;
777 if (type == EGL_PBUFFER_BIT) {
778 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
779 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
780 xcb_create_pixmap(dri2_dpy->conn,
781 _eglGetConfigKey(conf, EGL_BUFFER_SIZE),
782 dri2_surf->drawable, s.data->root,
783 dri2_surf->base.Width, dri2_surf->base.Height);
784 } else {
785 dri2_surf->drawable = window;
786 }
787
788 dri2_surf->dri_drawable =
789 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
790 dri2_conf->dri_config, dri2_surf);
791 if (dri2_surf == NULL) {
792 _eglError(EGL_BAD_ALLOC, "eglCreateWindowSurface");
793 free(dri2_surf);
794 return NULL;
795 }
796
797 xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable);
798
799 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
800 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
801 if (reply == NULL || error != NULL) {
802 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
803 free(dri2_surf);
804 free(error);
805 return NULL;
806 }
807 dri2_surf->base.Width = reply->width;
808 dri2_surf->base.Height = reply->height;
809 free(reply);
810
811 return &dri2_surf->base;
812 }
813
814 /**
815 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
816 */
817 static _EGLSurface *
818 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
819 _EGLConfig *conf, EGLNativeWindowType window,
820 const EGLint *attrib_list)
821 {
822 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
823 window, attrib_list);
824 }
825
826 static _EGLSurface *
827 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
828 _EGLConfig *conf, EGLNativePixmapType pixmap,
829 const EGLint *attrib_list)
830 {
831 return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
832 pixmap, attrib_list);
833 }
834
835 static _EGLSurface *
836 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
837 _EGLConfig *conf, const EGLint *attrib_list)
838 {
839 return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
840 XCB_WINDOW_NONE, attrib_list);
841 }
842
843 static EGLBoolean
844 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
845 {
846 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
847 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
848 xcb_dri2_copy_region_cookie_t cookie;
849
850 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
851
852 #if 0
853 /* FIXME: Add support for dri swapbuffers, that'll give us swap
854 * interval and page flipping (at least for fullscreen windows) as
855 * well as the page flip event. */
856 #if __DRI2_FLUSH_VERSION >= 2
857 if (pdraw->psc->f)
858 (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable);
859 #endif
860 #endif
861
862 if (!dri2_surf->have_back)
863 return EGL_TRUE;
864
865 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
866 dri2_surf->drawable,
867 dri2_surf->region,
868 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
869 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
870 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
871
872 return EGL_TRUE;
873 }
874
875 /*
876 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
877 */
878 static _EGLProc
879 dri2_get_proc_address(_EGLDriver *drv, const char *procname)
880 {
881 /* FIXME: Do we need to support lookup of EGL symbols too? */
882
883 return (_EGLProc) _glapi_get_proc_address(procname);
884 }
885
886 static EGLBoolean
887 dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
888 {
889 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
890 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface);
891
892 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
893 * we need to copy fake to real here.*/
894
895 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
896
897 return EGL_TRUE;
898 }
899
900 static EGLBoolean
901 dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
902 {
903 if (engine != EGL_CORE_NATIVE_ENGINE)
904 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
905 /* glXWaitX(); */
906
907 return EGL_TRUE;
908 }
909
910 static void
911 dri2_unload(_EGLDriver *drv)
912 {
913 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
914 free(dri2_drv);
915 }
916
917 static EGLBoolean
918 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
919 EGLNativePixmapType target)
920 {
921 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
922 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
923 xcb_gcontext_t gc;
924
925 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
926
927 gc = xcb_generate_id(dri2_dpy->conn);
928 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
929 xcb_copy_area(dri2_dpy->conn,
930 dri2_surf->drawable,
931 target,
932 gc,
933 0, 0,
934 0, 0,
935 dri2_surf->base.Width,
936 dri2_surf->base.Height);
937 xcb_free_gc(dri2_dpy->conn, gc);
938
939 return EGL_TRUE;
940 }
941
942 /**
943 * This is the main entrypoint into the driver, called by libEGL.
944 * Create a new _EGLDriver object and init its dispatch table.
945 */
946 _EGLDriver *
947 _eglMain(const char *args)
948 {
949 struct dri2_egl_driver *dri2_drv;
950
951 dri2_drv = malloc(sizeof *dri2_drv);
952 if (!dri2_drv)
953 return NULL;
954
955 _eglInitDriverFallbacks(&dri2_drv->base);
956 dri2_drv->base.API.Initialize = dri2_initialize;
957 dri2_drv->base.API.Terminate = dri2_terminate;
958 dri2_drv->base.API.CreateContext = dri2_create_context;
959 dri2_drv->base.API.MakeCurrent = dri2_make_current;
960 dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface;
961 dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface;
962 dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface;
963 dri2_drv->base.API.DestroySurface = dri2_destroy_surface;
964 dri2_drv->base.API.SwapBuffers = dri2_swap_buffers;
965 dri2_drv->base.API.GetProcAddress = dri2_get_proc_address;
966 dri2_drv->base.API.WaitClient = dri2_wait_client;
967 dri2_drv->base.API.WaitNative = dri2_wait_native;
968 dri2_drv->base.API.CopyBuffers = dri2_copy_buffers;
969
970 dri2_drv->base.Name = "DRI2";
971 dri2_drv->base.Unload = dri2_unload;
972
973 return &dri2_drv->base;
974 }