2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * Kristian Høgsberg <krh@bitplanet.net>
38 #include <GL/internal/dri_interface.h>
41 #include <xcb/xfixes.h>
42 #include <X11/Xlib-xcb.h>
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"
52 #include "eglsurface.h"
54 struct dri2_egl_driver
59 struct dri2_egl_display
61 xcb_connection_t
*conn
;
64 __DRIscreen
*dri_screen
;
66 __DRIcoreExtension
*core
;
67 __DRIdri2Extension
*dri2
;
68 __DRI2flushExtension
*flush
;
71 __DRIdri2LoaderExtension loader_extension
;
72 const __DRIextension
*extensions
[2];
75 struct dri2_egl_context
78 __DRIcontext
*dri_context
;
81 struct dri2_egl_surface
84 __DRIdrawable
*dri_drawable
;
85 xcb_drawable_t drawable
;
86 __DRIbuffer buffers
[5];
88 xcb_xfixes_region_t region
;
94 struct dri2_egl_config
97 const __DRIconfig
*dri_config
;
100 /* standard typecasts */
101 _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl
)
103 EGLint dri2_to_egl_attribute_map
[] = {
105 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
106 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
107 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
108 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
109 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
110 0, /* __DRI_ATTRIB_LUMINANCE_SIZE */
111 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
112 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
113 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
114 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
115 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
116 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
117 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
118 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
119 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
120 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
121 0, /* __DRI_ATTRIB_RENDER_TYPE, */
122 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
123 0, /* __DRI_ATTRIB_CONFORMANT */
124 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
125 0, /* __DRI_ATTRIB_STEREO */
126 0, /* __DRI_ATTRIB_AUX_BUFFERS */
127 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
128 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
129 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
130 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
131 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
132 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
133 0, /* __DRI_ATTRIB_FLOAT_MODE */
134 0, /* __DRI_ATTRIB_RED_MASK */
135 0, /* __DRI_ATTRIB_GREEN_MASK */
136 0, /* __DRI_ATTRIB_BLUE_MASK */
137 0, /* __DRI_ATTRIB_ALPHA_MASK */
138 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
139 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
140 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
141 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
142 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
143 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
144 0, /* __DRI_ATTRIB_SWAP_METHOD */
145 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
146 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
147 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
148 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
149 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
150 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
151 0, /* __DRI_ATTRIB_YINVERTED */
155 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
)
157 struct dri2_egl_config
*conf
;
158 struct dri2_egl_display
*dri2_dpy
;
159 unsigned int attrib
, value
, double_buffer
;
160 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
163 dri2_dpy
= disp
->DriverData
;
164 conf
= malloc(sizeof *conf
);
168 conf
->dri_config
= dri_config
;
169 _eglInitConfig(&conf
->base
, disp
, id
);
172 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
177 case __DRI_ATTRIB_RENDER_TYPE
:
178 if (value
& __DRI_ATTRIB_RGBA_BIT
)
179 value
= EGL_RGB_BUFFER
;
180 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
181 value
= EGL_LUMINANCE_BUFFER
;
184 _eglSetConfigKey(&conf
->base
, EGL_COLOR_BUFFER_TYPE
, value
);
187 case __DRI_ATTRIB_CONFIG_CAVEAT
:
188 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
189 value
= EGL_NON_CONFORMANT_CONFIG
;
190 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
191 value
= EGL_SLOW_CONFIG
;
194 _eglSetConfigKey(&conf
->base
, EGL_CONFIG_CAVEAT
, value
);
197 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
198 bind_to_texture_rgb
= value
;
201 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
202 bind_to_texture_rgba
= value
;
205 case __DRI_ATTRIB_DOUBLE_BUFFER
:
206 double_buffer
= value
;
210 key
= dri2_to_egl_attribute_map
[attrib
];
212 _eglSetConfigKey(&conf
->base
, key
, value
);
217 /* EGL_SWAP_BEHAVIOR_PRESERVED_BIT */
220 /* FIXME: Figure out how to get the visual ID and types */
221 _eglSetConfigKey(&conf
->base
, EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
);
222 _eglSetConfigKey(&conf
->base
, EGL_NATIVE_VISUAL_ID
, 0x21);
223 _eglSetConfigKey(&conf
->base
, EGL_NATIVE_VISUAL_TYPE
,
224 XCB_VISUAL_CLASS_TRUE_COLOR
);
226 _eglSetConfigKey(&conf
->base
,
227 EGL_SURFACE_TYPE
, EGL_PIXMAP_BIT
| EGL_PBUFFER_BIT
);
228 _eglSetConfigKey(&conf
->base
,
229 EGL_BIND_TO_TEXTURE_RGB
, bind_to_texture_rgb
);
230 _eglSetConfigKey(&conf
->base
,
231 EGL_BIND_TO_TEXTURE_RGBA
, bind_to_texture_rgba
);
234 /* EGL_OPENGL_ES_BIT, EGL_OPENVG_BIT, EGL_OPENGL_ES2_BIT */
235 _eglSetConfigKey(&conf
->base
, EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
);
236 _eglSetConfigKey(&conf
->base
, EGL_CONFORMANT
, EGL_OPENGL_BIT
);
238 if (!_eglValidateConfig(&conf
->base
, EGL_FALSE
)) {
239 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
244 _eglAddConfig(disp
, &conf
->base
);
248 * Process list of buffer received from the server
250 * Processes the list of buffers received in a reply from the server to either
251 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
254 dri2_process_buffers(struct dri2_egl_surface
*dri2_surf
,
255 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
257 struct dri2_egl_display
*dri2_dpy
=
258 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
259 xcb_rectangle_t rectangle
;
262 dri2_surf
->buffer_count
= count
;
263 dri2_surf
->have_fake_front
= 0;
264 dri2_surf
->have_back
= 0;
266 /* This assumes the DRI2 buffer attachment tokens matches the
267 * __DRIbuffer tokens. */
268 for (i
= 0; i
< count
; i
++) {
269 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
270 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
271 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
272 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
273 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
274 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
275 dri2_surf
->have_fake_front
= 1;
276 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
277 dri2_surf
->have_back
= 1;
280 if (dri2_surf
->region
!= XCB_NONE
)
281 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
285 rectangle
.width
= dri2_surf
->base
.Width
;
286 rectangle
.height
= dri2_surf
->base
.Height
;
287 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
288 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
292 dri2_get_buffers(__DRIdrawable
* driDrawable
,
293 int *width
, int *height
,
294 unsigned int *attachments
, int count
,
295 int *out_count
, void *loaderPrivate
)
297 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
298 struct dri2_egl_display
*dri2_dpy
=
299 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
300 xcb_dri2_dri2_buffer_t
*buffers
;
301 xcb_dri2_get_buffers_reply_t
*reply
;
302 xcb_dri2_get_buffers_cookie_t cookie
;
304 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
306 count
, count
, attachments
);
307 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
308 buffers
= xcb_dri2_get_buffers_buffers (reply
);
312 *out_count
= reply
->count
;
313 dri2_surf
->base
.Width
= *width
= reply
->width
;
314 dri2_surf
->base
.Height
= *height
= reply
->height
;
315 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
319 return dri2_surf
->buffers
;
323 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
325 /* FIXME: Does EGL support front buffer rendering at all? */
328 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
330 dri2WaitGL(dri2_surf
);
335 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
336 int *width
, int *height
,
337 unsigned int *attachments
, int count
,
338 int *out_count
, void *loaderPrivate
)
340 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
341 struct dri2_egl_display
*dri2_dpy
=
342 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
343 xcb_dri2_dri2_buffer_t
*buffers
;
344 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
345 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
346 xcb_dri2_attach_format_t
*format_attachments
;
348 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
349 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
354 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
359 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
360 dri2_surf
->base
.Width
= *width
= reply
->width
;
361 dri2_surf
->base
.Height
= *height
= reply
->height
;
362 *out_count
= reply
->count
;
363 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
367 return dri2_surf
->buffers
;
371 static const char dri_driver_format
[] = "%.*s/tls/%.*s_dri.so";
373 static const char dri_driver_format
[] = "%.*s/%.*s_dri.so";
376 static const char dri_driver_path
[] = DEFAULT_DRIVER_DIR
;
379 * Called via eglInitialize(), GLX_drv->API.Initialize().
382 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
,
383 EGLint
*major
, EGLint
*minor
)
385 const __DRIextension
**extensions
;
386 const __DRIconfig
**driver_configs
;
387 struct dri2_egl_display
*dri2_dpy
;
388 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
389 xcb_xfixes_query_version_reply_t
*xfixes_query
;
390 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
391 xcb_dri2_query_version_reply_t
*dri2_query
;
392 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
393 xcb_dri2_connect_reply_t
*connect
;
394 xcb_dri2_connect_cookie_t connect_cookie
;
395 xcb_dri2_authenticate_reply_t
*authenticate
;
396 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
397 xcb_generic_error_t
*error
;
399 xcb_screen_iterator_t s
;
402 dri2_dpy
= malloc(sizeof *dri2_dpy
);
404 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
406 disp
->DriverData
= (void *) dri2_dpy
;
407 dri2_dpy
->conn
= XGetXCBConnection(disp
->NativeDisplay
);
408 if (!dri2_dpy
->conn
) {
409 dri2_dpy
->conn
= xcb_connect(0, 0);
410 if (!dri2_dpy
->conn
) {
411 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
416 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
417 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
419 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
420 XCB_XFIXES_MAJOR_VERSION
,
421 XCB_XFIXES_MINOR_VERSION
);
423 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
424 XCB_DRI2_MAJOR_VERSION
,
425 XCB_DRI2_MINOR_VERSION
);
427 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
428 connect_cookie
= xcb_dri2_connect_unchecked (dri2_dpy
->conn
,
430 XCB_DRI2_DRIVER_TYPE_DRI
);
433 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
434 xfixes_query_cookie
, &error
);
435 if (xfixes_query
== NULL
||
436 error
!= NULL
|| xfixes_query
->major_version
< 2) {
437 _eglLog(_EGL_FATAL
, "DRI2: failed to query xfixes version");
444 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
445 if (dri2_query
== NULL
|| error
!= NULL
) {
446 _eglLog(_EGL_FATAL
, "DRI2: failed to query version");
450 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
451 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
454 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
455 if (connect
== NULL
||
456 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
457 _eglLog(_EGL_FATAL
, "DRI2: failed to authenticate");
458 goto cleanup_connect
;
462 if (geteuid() == getuid()) {
463 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
464 search_paths
= getenv("LIBGL_DRIVERS_PATH");
466 if (search_paths
== NULL
)
467 search_paths
= DEFAULT_DRIVER_DIR
;
469 dri2_dpy
->driver
= NULL
;
470 end
= search_paths
+ strlen(search_paths
);
471 for (p
= search_paths
; p
< end
&& dri2_dpy
->driver
== NULL
; p
= next
+ 1) {
472 next
= strchr(p
, ':');
476 snprintf(path
, sizeof path
,
479 xcb_dri2_connect_driver_name_length (connect
),
480 xcb_dri2_connect_driver_name (connect
));
482 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
485 if (dri2_dpy
->driver
== NULL
) {
487 "DRI2: failed to open any driver (search paths %s)",
489 goto cleanup_connect
;
492 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
493 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
494 if (extensions
== NULL
) {
496 "DRI2: driver exports no extensions (%s)", dlerror());
500 for (i
= 0; extensions
[i
]; i
++) {
501 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
502 dri2_dpy
->core
= (__DRIcoreExtension
*) extensions
[i
];
503 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
504 dri2_dpy
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
507 if (dri2_dpy
->core
== NULL
) {
508 _eglLog(_EGL_FATAL
, "DRI2: driver has no core extension");
512 if (dri2_dpy
->dri2
== NULL
) {
513 _eglLog(_EGL_FATAL
, "DRI2: driver has no dri2 extension");
517 snprintf(path
, sizeof path
, "%.*s",
518 xcb_dri2_connect_device_name_length (connect
),
519 xcb_dri2_connect_device_name (connect
));
520 dri2_dpy
->fd
= open (path
, O_RDWR
);
521 if (dri2_dpy
->fd
== -1) {
523 "DRI2: could not open %s (%s)", path
, strerror(errno
));
527 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
528 _eglLog(_EGL_FATAL
, "DRI2: failed to get drm magic");
532 authenticate_cookie
= xcb_dri2_authenticate_unchecked (dri2_dpy
->conn
,
533 s
.data
->root
, magic
);
534 authenticate
= xcb_dri2_authenticate_reply (dri2_dpy
->conn
,
535 authenticate_cookie
, NULL
);
536 if (authenticate
== NULL
|| !authenticate
->authenticated
) {
537 _eglLog(_EGL_FATAL
, "DRI2: failed to authenticate");
543 if (dri2_dpy
->dri2_minor
>= 1) {
544 dri2_dpy
->loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
545 dri2_dpy
->loader_extension
.base
.version
= 3;
546 dri2_dpy
->loader_extension
.getBuffers
= dri2_get_buffers
;
547 dri2_dpy
->loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
548 dri2_dpy
->loader_extension
.getBuffersWithFormat
=
549 dri2_get_buffers_with_format
;
551 dri2_dpy
->loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
552 dri2_dpy
->loader_extension
.base
.version
= 2;
553 dri2_dpy
->loader_extension
.getBuffers
= dri2_get_buffers
;
554 dri2_dpy
->loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
555 dri2_dpy
->loader_extension
.getBuffersWithFormat
= NULL
;
558 dri2_dpy
->extensions
[0] = &dri2_dpy
->loader_extension
.base
;
559 dri2_dpy
->extensions
[1] = NULL
;
561 dri2_dpy
->dri_screen
=
562 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
, dri2_dpy
->extensions
,
563 &driver_configs
, dri2_dpy
);
565 if (dri2_dpy
->dri_screen
== NULL
) {
566 _eglLog(_EGL_FATAL
, "DRI2: failed to create dri screen");
570 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
571 for (i
= 0; extensions
[i
]; i
++) {
572 _eglLog(_EGL_DEBUG
, "DRI2: found extension `%s'", extensions
[i
]->name
);
573 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0))
574 dri2_dpy
->flush
= (__DRI2flushExtension
*) extensions
[i
];
577 if (dri2_dpy
->flush
== NULL
) {
578 _eglLog(_EGL_FATAL
, "DRI2: driver doesn't support the flush extension");
579 goto cleanup_dri_screen
;
582 for (i
= 0; driver_configs
[i
]; i
++)
583 dri2_add_config(disp
, driver_configs
[i
], i
+ 1);
584 if (!disp
->NumConfigs
) {
585 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
586 goto cleanup_configs
;
589 disp
->ClientAPIsMask
= EGL_OPENGL_BIT
;
591 /* we're supporting EGL 1.4 */
598 _eglCleanupDisplay(disp
);
600 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
604 dlclose(dri2_dpy
->driver
);
608 if (disp
->NativeDisplay
== NULL
)
609 xcb_disconnect(dri2_dpy
->conn
);
617 * Called via eglTerminate(), drv->API.Terminate().
620 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
622 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
624 _eglReleaseDisplayResources(drv
, disp
);
625 _eglCleanupDisplay(disp
);
627 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
629 dlclose(dri2_dpy
->driver
);
630 if (disp
->NativeDisplay
== NULL
)
631 xcb_disconnect(dri2_dpy
->conn
);
633 disp
->DriverData
= NULL
;
640 * Called via eglCreateContext(), drv->API.CreateContext().
643 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
644 _EGLContext
*share_list
, const EGLint
*attrib_list
)
646 struct dri2_egl_context
*dri2_ctx
;
647 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
648 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
649 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
651 dri2_ctx
= malloc(sizeof *dri2_ctx
);
653 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
657 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
660 dri2_ctx
->dri_context
=
661 dri2_dpy
->dri2
->createNewContext(dri2_dpy
->dri_screen
,
662 dri2_config
->dri_config
,
664 dri2_ctx_shared
->dri_context
: NULL
,
667 if (!dri2_ctx
->dri_context
)
670 return &dri2_ctx
->base
;
678 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
680 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
681 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
683 if (_eglIsSurfaceBound(surf
))
686 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
688 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
690 if (surf
->Type
== EGL_PBUFFER_BIT
)
691 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
699 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
702 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
703 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
705 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
706 struct dri2_egl_surface
*dri2_dsurf
= dri2_egl_surface(dsurf
);
707 struct dri2_egl_surface
*dri2_rsurf
= dri2_egl_surface(rsurf
);
708 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
709 __DRIdrawable
*ddraw
, *rdraw
;
712 /* bind the new context and return the "orphaned" one */
713 if (!_eglBindContext(&ctx
, &dsurf
, &rsurf
))
716 ddraw
= (dri2_dsurf
) ? dri2_dsurf
->dri_drawable
: NULL
;
717 rdraw
= (dri2_rsurf
) ? dri2_rsurf
->dri_drawable
: NULL
;
718 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
720 if ((cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
) ||
721 dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
722 if (dsurf
&& !_eglIsSurfaceLinked(dsurf
))
723 dri2_destroy_surface(drv
, disp
, dsurf
);
724 if (rsurf
&& rsurf
!= dsurf
&& !_eglIsSurfaceLinked(dsurf
))
725 dri2_destroy_surface(drv
, disp
, rsurf
);
726 if (ctx
!= NULL
&& !_eglIsContextLinked(ctx
))
727 dri2_dpy
->core
->unbindContext(dri2_egl_context(ctx
)->dri_context
);
731 _eglBindContext(&ctx
, &dsurf
, &rsurf
);
738 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
741 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
742 _EGLConfig
*conf
, EGLNativeWindowType window
,
743 const EGLint
*attrib_list
)
745 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
746 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
747 struct dri2_egl_surface
*dri2_surf
;
748 xcb_get_geometry_cookie_t cookie
;
749 xcb_get_geometry_reply_t
*reply
;
750 xcb_screen_iterator_t s
;
751 xcb_generic_error_t
*error
;
753 dri2_surf
= malloc(sizeof *dri2_surf
);
755 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
759 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
762 dri2_surf
->region
= XCB_NONE
;
763 if (type
== EGL_PBUFFER_BIT
) {
764 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
765 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
766 xcb_create_pixmap(dri2_dpy
->conn
,
767 _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
),
768 dri2_surf
->drawable
, s
.data
->root
,
769 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
771 dri2_surf
->drawable
= window
;
774 dri2_surf
->dri_drawable
=
775 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
776 dri2_conf
->dri_config
, dri2_surf
);
777 if (dri2_surf
->dri_drawable
== NULL
) {
778 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
782 xcb_dri2_create_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
784 if (type
!= EGL_PBUFFER_BIT
) {
785 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
786 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
787 if (reply
== NULL
|| error
!= NULL
) {
788 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
790 goto cleanup_dri_drawable
;
793 dri2_surf
->base
.Width
= reply
->width
;
794 dri2_surf
->base
.Height
= reply
->height
;
798 return &dri2_surf
->base
;
800 cleanup_dri_drawable
:
801 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
803 if (type
== EGL_PBUFFER_BIT
)
804 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
812 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
815 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
816 _EGLConfig
*conf
, EGLNativeWindowType window
,
817 const EGLint
*attrib_list
)
819 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
820 window
, attrib_list
);
824 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
825 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
826 const EGLint
*attrib_list
)
828 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
829 pixmap
, attrib_list
);
833 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
834 _EGLConfig
*conf
, const EGLint
*attrib_list
)
836 return dri2_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
837 XCB_WINDOW_NONE
, attrib_list
);
841 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
843 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
844 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
845 xcb_dri2_copy_region_cookie_t cookie
;
847 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
850 /* FIXME: Add support for dri swapbuffers, that'll give us swap
851 * interval and page flipping (at least for fullscreen windows) as
852 * well as the page flip event. */
853 #if __DRI2_FLUSH_VERSION >= 2
855 (*pdraw
->psc
->f
->flushInvalidate
)(pdraw
->driDrawable
);
859 if (!dri2_surf
->have_back
)
862 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
865 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
866 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
);
867 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
873 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
876 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
878 /* FIXME: Do we need to support lookup of EGL symbols too? */
880 return (_EGLProc
) _glapi_get_proc_address(procname
);
884 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
886 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
887 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(ctx
->DrawSurface
);
889 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
890 * we need to copy fake to real here.*/
892 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
898 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
900 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
901 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
908 dri2_unload(_EGLDriver
*drv
)
910 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
915 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
916 EGLNativePixmapType target
)
918 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
919 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
922 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
924 gc
= xcb_generate_id(dri2_dpy
->conn
);
925 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
926 xcb_copy_area(dri2_dpy
->conn
,
932 dri2_surf
->base
.Width
,
933 dri2_surf
->base
.Height
);
934 xcb_free_gc(dri2_dpy
->conn
, gc
);
940 * This is the main entrypoint into the driver, called by libEGL.
941 * Create a new _EGLDriver object and init its dispatch table.
944 _eglMain(const char *args
)
946 struct dri2_egl_driver
*dri2_drv
;
948 dri2_drv
= malloc(sizeof *dri2_drv
);
952 _eglInitDriverFallbacks(&dri2_drv
->base
);
953 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
954 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
955 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
956 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
957 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
958 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
959 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
960 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
961 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
962 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
963 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
964 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
965 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
;
967 dri2_drv
->base
.Name
= "DRI2";
968 dri2_drv
->base
.Unload
= dri2_unload
;
970 return &dri2_drv
->base
;