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>
43 #include <sys/types.h>
50 #include <glapi/glapi.h>
51 #include "eglconfig.h"
52 #include "eglcontext.h"
53 #include "egldisplay.h"
54 #include "egldriver.h"
55 #include "eglcurrent.h"
57 #include "eglsurface.h"
60 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
62 struct dri2_egl_driver
66 void (*glFlush
)(void);
69 struct dri2_egl_display
71 xcb_connection_t
*conn
;
74 __DRIscreen
*dri_screen
;
75 const __DRIconfig
**driver_configs
;
77 __DRIcoreExtension
*core
;
78 __DRIdri2Extension
*dri2
;
79 __DRI2flushExtension
*flush
;
80 __DRItexBufferExtension
*tex_buffer
;
81 __DRIimageExtension
*image
;
87 __DRIdri2LoaderExtension loader_extension
;
88 const __DRIextension
*extensions
[3];
91 struct dri2_egl_context
94 __DRIcontext
*dri_context
;
97 struct dri2_egl_surface
100 __DRIdrawable
*dri_drawable
;
101 xcb_drawable_t drawable
;
102 __DRIbuffer buffers
[5];
104 xcb_xfixes_region_t region
;
109 struct dri2_egl_config
112 const __DRIconfig
*dri_config
;
115 struct dri2_egl_image
118 __DRIimage
*dri_image
;
121 /* standard typecasts */
122 _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl
)
123 _EGL_DRIVER_TYPECAST(dri2_egl_image
, _EGLImage
, obj
)
125 static const __DRIuseInvalidateExtension use_invalidate
= {
126 { __DRI_USE_INVALIDATE
, 1 }
129 EGLint dri2_to_egl_attribute_map
[] = {
131 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
132 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
133 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
134 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
135 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
136 EGL_LUMINANCE_SIZE
, /* __DRI_ATTRIB_LUMINANCE_SIZE */
137 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
138 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
139 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
140 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
141 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
142 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
143 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
144 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
145 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
146 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
147 0, /* __DRI_ATTRIB_RENDER_TYPE, */
148 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
149 0, /* __DRI_ATTRIB_CONFORMANT */
150 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
151 0, /* __DRI_ATTRIB_STEREO */
152 0, /* __DRI_ATTRIB_AUX_BUFFERS */
153 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
154 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
155 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
156 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
157 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
158 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
159 0, /* __DRI_ATTRIB_FLOAT_MODE */
160 0, /* __DRI_ATTRIB_RED_MASK */
161 0, /* __DRI_ATTRIB_GREEN_MASK */
162 0, /* __DRI_ATTRIB_BLUE_MASK */
163 0, /* __DRI_ATTRIB_ALPHA_MASK */
164 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
165 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
166 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
167 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
168 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
169 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
170 0, /* __DRI_ATTRIB_SWAP_METHOD */
171 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
172 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
173 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
174 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
175 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
176 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
177 EGL_Y_INVERTED_NOK
, /* __DRI_ATTRIB_YINVERTED */
180 static struct dri2_egl_config
*
181 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
182 int depth
, EGLint surface_type
)
184 struct dri2_egl_config
*conf
;
185 struct dri2_egl_display
*dri2_dpy
;
187 unsigned int attrib
, value
, double_buffer
;
188 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
191 dri2_dpy
= disp
->DriverData
;
192 _eglInitConfig(&base
, disp
, id
);
196 bind_to_texture_rgb
= 0;
197 bind_to_texture_rgba
= 0;
199 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
201 case __DRI_ATTRIB_RENDER_TYPE
:
202 if (value
& __DRI_ATTRIB_RGBA_BIT
)
203 value
= EGL_RGB_BUFFER
;
204 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
205 value
= EGL_LUMINANCE_BUFFER
;
208 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
211 case __DRI_ATTRIB_CONFIG_CAVEAT
:
212 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
213 value
= EGL_NON_CONFORMANT_CONFIG
;
214 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
215 value
= EGL_SLOW_CONFIG
;
218 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
221 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
222 bind_to_texture_rgb
= value
;
225 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
226 bind_to_texture_rgba
= value
;
229 case __DRI_ATTRIB_DOUBLE_BUFFER
:
230 double_buffer
= value
;
234 key
= dri2_to_egl_attribute_map
[attrib
];
236 _eglSetConfigKey(&base
, key
, value
);
241 /* In EGL, double buffer or not isn't a config attribute. Pixmaps
242 * surfaces are always single buffered, pbuffer surfaces are always
243 * back buffers and windows can be either, selected by passing an
244 * attribute at window surface construction time. To support this
245 * we ignore all double buffer configs and manipulate the buffer we
246 * return in the getBuffer callback to get the behaviour we want. */
251 if (depth
> 0 && depth
!= _eglGetConfigKey(&base
, EGL_BUFFER_SIZE
))
254 _eglSetConfigKey(&base
, EGL_NATIVE_RENDERABLE
, EGL_TRUE
);
256 _eglSetConfigKey(&base
, EGL_SURFACE_TYPE
, surface_type
);
257 if (surface_type
& (EGL_PIXMAP_BIT
| EGL_PBUFFER_BIT
)) {
258 _eglSetConfigKey(&base
, EGL_BIND_TO_TEXTURE_RGB
, bind_to_texture_rgb
);
259 if (_eglGetConfigKey(&base
, EGL_ALPHA_SIZE
) > 0)
260 _eglSetConfigKey(&base
,
261 EGL_BIND_TO_TEXTURE_RGBA
, bind_to_texture_rgba
);
264 _eglSetConfigKey(&base
, EGL_RENDERABLE_TYPE
, disp
->ClientAPIsMask
);
265 _eglSetConfigKey(&base
, EGL_CONFORMANT
, disp
->ClientAPIsMask
);
267 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
268 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
272 conf
= malloc(sizeof *conf
);
274 memcpy(&conf
->base
, &base
, sizeof base
);
275 conf
->dri_config
= dri_config
;
276 _eglAddConfig(disp
, &conf
->base
);
283 * Process list of buffer received from the server
285 * Processes the list of buffers received in a reply from the server to either
286 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
289 dri2_process_buffers(struct dri2_egl_surface
*dri2_surf
,
290 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
292 struct dri2_egl_display
*dri2_dpy
=
293 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
294 xcb_rectangle_t rectangle
;
297 dri2_surf
->buffer_count
= count
;
298 dri2_surf
->have_fake_front
= 0;
300 /* This assumes the DRI2 buffer attachment tokens matches the
301 * __DRIbuffer tokens. */
302 for (i
= 0; i
< count
; i
++) {
303 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
304 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
305 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
306 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
307 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
309 /* We only use the DRI drivers single buffer configs. This
310 * means that if we try to render to a window, DRI2 will give us
311 * the fake front buffer, which we'll use as a back buffer.
312 * Note that EGL doesn't require that several clients rendering
313 * to the same window must see the same aux buffers. */
314 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
315 dri2_surf
->have_fake_front
= 1;
318 if (dri2_surf
->region
!= XCB_NONE
)
319 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
323 rectangle
.width
= dri2_surf
->base
.Width
;
324 rectangle
.height
= dri2_surf
->base
.Height
;
325 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
326 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
330 dri2_get_buffers(__DRIdrawable
* driDrawable
,
331 int *width
, int *height
,
332 unsigned int *attachments
, int count
,
333 int *out_count
, void *loaderPrivate
)
335 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
336 struct dri2_egl_display
*dri2_dpy
=
337 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
338 xcb_dri2_dri2_buffer_t
*buffers
;
339 xcb_dri2_get_buffers_reply_t
*reply
;
340 xcb_dri2_get_buffers_cookie_t cookie
;
342 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
344 count
, count
, attachments
);
345 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
346 buffers
= xcb_dri2_get_buffers_buffers (reply
);
350 *out_count
= reply
->count
;
351 dri2_surf
->base
.Width
= *width
= reply
->width
;
352 dri2_surf
->base
.Height
= *height
= reply
->height
;
353 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
357 return dri2_surf
->buffers
;
361 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
363 /* FIXME: Does EGL support front buffer rendering at all? */
366 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
368 dri2WaitGL(dri2_surf
);
373 dri2_lookup_egl_image(__DRIcontext
*context
, void *image
, void *data
)
375 struct dri2_egl_context
*dri2_ctx
= data
;
376 _EGLDisplay
*disp
= dri2_ctx
->base
.Resource
.Display
;
377 struct dri2_egl_image
*dri2_img
;
380 img
= _eglLookupImage(image
, disp
);
382 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
386 dri2_img
= dri2_egl_image(image
);
388 return dri2_img
->dri_image
;
391 static const __DRIimageLookupExtension image_lookup_extension
= {
392 { __DRI_IMAGE_LOOKUP
, 1 },
393 dri2_lookup_egl_image
397 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
398 int *width
, int *height
,
399 unsigned int *attachments
, int count
,
400 int *out_count
, void *loaderPrivate
)
402 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
403 struct dri2_egl_display
*dri2_dpy
=
404 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
405 xcb_dri2_dri2_buffer_t
*buffers
;
406 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
407 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
408 xcb_dri2_attach_format_t
*format_attachments
;
410 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
411 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
416 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
421 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
422 dri2_surf
->base
.Width
= *width
= reply
->width
;
423 dri2_surf
->base
.Height
= *height
= reply
->height
;
424 *out_count
= reply
->count
;
425 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
429 return dri2_surf
->buffers
;
432 static const char dri_driver_path
[] = DEFAULT_DRIVER_DIR
;
434 struct dri2_extension_match
{
440 static struct dri2_extension_match dri2_driver_extensions
[] = {
441 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
442 { __DRI_DRI2
, 1, offsetof(struct dri2_egl_display
, dri2
) },
446 static struct dri2_extension_match dri2_core_extensions
[] = {
447 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
448 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
449 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
454 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
455 struct dri2_extension_match
*matches
,
456 const __DRIextension
**extensions
)
458 int i
, j
, ret
= EGL_TRUE
;
461 for (i
= 0; extensions
[i
]; i
++) {
462 _eglLog(_EGL_DEBUG
, "DRI2: found extension `%s'", extensions
[i
]->name
);
463 for (j
= 0; matches
[j
].name
; j
++) {
464 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
465 extensions
[i
]->version
>= matches
[j
].version
) {
466 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
467 *(const __DRIextension
**) field
= extensions
[i
];
468 _eglLog(_EGL_INFO
, "DRI2: found extension %s version %d",
469 extensions
[i
]->name
, extensions
[i
]->version
);
474 for (j
= 0; matches
[j
].name
; j
++) {
475 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
476 if (*(const __DRIextension
**) field
== NULL
) {
477 _eglLog(_EGL_FATAL
, "DRI2: did not find extension %s version %d",
478 matches
[j
].name
, matches
[j
].version
);
487 dri2_strndup(const char *s
, int length
)
491 d
= malloc(length
+ 1);
495 memcpy(d
, s
, length
);
502 dri2_connect(struct dri2_egl_display
*dri2_dpy
)
504 xcb_xfixes_query_version_reply_t
*xfixes_query
;
505 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
506 xcb_dri2_query_version_reply_t
*dri2_query
;
507 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
508 xcb_dri2_connect_reply_t
*connect
;
509 xcb_dri2_connect_cookie_t connect_cookie
;
510 xcb_generic_error_t
*error
;
511 xcb_screen_iterator_t s
;
513 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
514 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
516 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
517 XCB_XFIXES_MAJOR_VERSION
,
518 XCB_XFIXES_MINOR_VERSION
);
520 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
521 XCB_DRI2_MAJOR_VERSION
,
522 XCB_DRI2_MINOR_VERSION
);
524 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
525 connect_cookie
= xcb_dri2_connect_unchecked (dri2_dpy
->conn
,
527 XCB_DRI2_DRIVER_TYPE_DRI
);
530 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
531 xfixes_query_cookie
, &error
);
532 if (xfixes_query
== NULL
||
533 error
!= NULL
|| xfixes_query
->major_version
< 2) {
534 _eglLog(_EGL_FATAL
, "DRI2: failed to query xfixes version");
541 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
542 if (dri2_query
== NULL
|| error
!= NULL
) {
543 _eglLog(_EGL_FATAL
, "DRI2: failed to query version");
547 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
548 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
551 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
552 if (connect
== NULL
||
553 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
554 _eglLog(_EGL_FATAL
, "DRI2: failed to authenticate");
558 dri2_dpy
->device_name
=
559 dri2_strndup(xcb_dri2_connect_device_name (connect
),
560 xcb_dri2_connect_device_name_length (connect
));
562 dri2_dpy
->driver_name
=
563 dri2_strndup(xcb_dri2_connect_driver_name (connect
),
564 xcb_dri2_connect_driver_name_length (connect
));
566 if (dri2_dpy
->device_name
== NULL
|| dri2_dpy
->driver_name
== NULL
) {
567 free(dri2_dpy
->device_name
);
568 free(dri2_dpy
->driver_name
);
578 dri2_authenticate(struct dri2_egl_display
*dri2_dpy
)
580 xcb_dri2_authenticate_reply_t
*authenticate
;
581 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
582 xcb_screen_iterator_t s
;
585 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
586 _eglLog(_EGL_FATAL
, "DRI2: failed to get drm magic");
590 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
591 authenticate_cookie
=
592 xcb_dri2_authenticate_unchecked(dri2_dpy
->conn
, s
.data
->root
, magic
);
594 xcb_dri2_authenticate_reply(dri2_dpy
->conn
, authenticate_cookie
, NULL
);
595 if (authenticate
== NULL
|| !authenticate
->authenticated
) {
596 _eglLog(_EGL_FATAL
, "DRI2: failed to authenticate");
607 dri2_add_configs_for_visuals(struct dri2_egl_display
*dri2_dpy
,
610 xcb_screen_iterator_t s
;
611 xcb_depth_iterator_t d
;
612 xcb_visualtype_t
*visuals
;
614 struct dri2_egl_config
*conf
;
617 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
618 d
= xcb_screen_allowed_depths_iterator(s
.data
);
625 EGL_SWAP_BEHAVIOR_PRESERVED_BIT
;
628 EGLBoolean class_added
[6] = { 0, };
630 visuals
= xcb_depth_visuals(d
.data
);
631 for (i
= 0; i
< xcb_depth_visuals_length(d
.data
); i
++) {
632 if (class_added
[visuals
[i
]._class
])
635 class_added
[visuals
[i
]._class
] = EGL_TRUE
;
636 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
637 conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
],
638 id
++, d
.data
->depth
, surface_type
);
641 _eglSetConfigKey(&conf
->base
,
642 EGL_NATIVE_VISUAL_ID
, visuals
[i
].visual_id
);
643 _eglSetConfigKey(&conf
->base
,
644 EGL_NATIVE_VISUAL_TYPE
, visuals
[i
]._class
);
651 if (!_eglGetArraySize(disp
->Configs
)) {
652 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
660 dri2_load_driver(_EGLDisplay
*disp
)
662 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
663 const __DRIextension
**extensions
;
664 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
667 if (geteuid() == getuid()) {
668 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
669 search_paths
= getenv("LIBGL_DRIVERS_PATH");
671 if (search_paths
== NULL
)
672 search_paths
= DEFAULT_DRIVER_DIR
;
674 dri2_dpy
->driver
= NULL
;
675 end
= search_paths
+ strlen(search_paths
);
676 for (p
= search_paths
; p
< end
&& dri2_dpy
->driver
== NULL
; p
= next
+ 1) {
678 next
= strchr(p
, ':');
684 snprintf(path
, sizeof path
,
685 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
686 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
688 if (dri2_dpy
->driver
== NULL
) {
689 snprintf(path
, sizeof path
,
690 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
691 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
692 if (dri2_dpy
->driver
== NULL
)
693 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
697 if (dri2_dpy
->driver
== NULL
) {
698 _eglLog(_EGL_WARNING
,
699 "DRI2: failed to open any driver (search paths %s)",
704 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
705 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
706 if (extensions
== NULL
) {
707 _eglLog(_EGL_WARNING
,
708 "DRI2: driver exports no extensions (%s)", dlerror());
709 dlclose(dri2_dpy
->driver
);
713 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
)) {
714 dlclose(dri2_dpy
->driver
);
722 dri2_create_screen(_EGLDisplay
*disp
)
724 const __DRIextension
**extensions
;
725 struct dri2_egl_display
*dri2_dpy
;
726 unsigned int api_mask
;
728 dri2_dpy
= disp
->DriverData
;
729 dri2_dpy
->dri_screen
=
730 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
, dri2_dpy
->extensions
,
731 &dri2_dpy
->driver_configs
, dri2_dpy
);
733 if (dri2_dpy
->dri_screen
== NULL
) {
734 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
738 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
739 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
))
740 goto cleanup_dri_screen
;
742 if (dri2_dpy
->dri2
->base
.version
>= 2)
743 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
745 api_mask
= __DRI_API_OPENGL
;
747 disp
->ClientAPIsMask
= 0;
748 if (api_mask
& (1 <<__DRI_API_OPENGL
))
749 disp
->ClientAPIsMask
|= EGL_OPENGL_BIT
;
750 if (api_mask
& (1 <<__DRI_API_GLES
))
751 disp
->ClientAPIsMask
|= EGL_OPENGL_ES_BIT
;
752 if (api_mask
& (1 << __DRI_API_GLES2
))
753 disp
->ClientAPIsMask
|= EGL_OPENGL_ES2_BIT
;
755 if (dri2_dpy
->dri2
->base
.version
>= 2) {
756 disp
->Extensions
.KHR_surfaceless_gles1
= EGL_TRUE
;
757 disp
->Extensions
.KHR_surfaceless_gles2
= EGL_TRUE
;
758 disp
->Extensions
.KHR_surfaceless_opengl
= EGL_TRUE
;
764 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
770 dri2_initialize_x11(_EGLDriver
*drv
, _EGLDisplay
*disp
,
771 EGLint
*major
, EGLint
*minor
)
773 struct dri2_egl_display
*dri2_dpy
;
775 dri2_dpy
= malloc(sizeof *dri2_dpy
);
777 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
779 disp
->DriverData
= (void *) dri2_dpy
;
780 if (disp
->PlatformDisplay
== NULL
) {
781 dri2_dpy
->conn
= xcb_connect(0, 0);
783 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
786 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
787 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
791 if (dri2_dpy
->conn
) {
792 if (!dri2_connect(dri2_dpy
))
796 if (!dri2_load_driver(disp
))
799 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
800 if (dri2_dpy
->fd
== -1) {
801 _eglLog(_EGL_WARNING
,
802 "DRI2: could not open %s (%s)", dri2_dpy
->device_name
,
807 if (dri2_dpy
->conn
) {
808 if (!dri2_authenticate(dri2_dpy
))
812 if (dri2_dpy
->dri2_minor
>= 1) {
813 dri2_dpy
->loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
814 dri2_dpy
->loader_extension
.base
.version
= 3;
815 dri2_dpy
->loader_extension
.getBuffers
= dri2_get_buffers
;
816 dri2_dpy
->loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
817 dri2_dpy
->loader_extension
.getBuffersWithFormat
=
818 dri2_get_buffers_with_format
;
820 dri2_dpy
->loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
821 dri2_dpy
->loader_extension
.base
.version
= 2;
822 dri2_dpy
->loader_extension
.getBuffers
= dri2_get_buffers
;
823 dri2_dpy
->loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
824 dri2_dpy
->loader_extension
.getBuffersWithFormat
= NULL
;
827 dri2_dpy
->extensions
[0] = &dri2_dpy
->loader_extension
.base
;
828 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
829 dri2_dpy
->extensions
[2] = NULL
;
831 if (!dri2_create_screen(disp
))
834 if (dri2_dpy
->conn
) {
835 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
836 goto cleanup_configs
;
839 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
840 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
841 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
842 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
843 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
844 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
846 /* we're supporting EGL 1.4 */
853 _eglCleanupDisplay(disp
);
854 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
858 dlclose(dri2_dpy
->driver
);
860 if (disp
->PlatformDisplay
== NULL
)
861 xcb_disconnect(dri2_dpy
->conn
);
870 struct dri2_driver_map
{
877 const int i915_chip_ids
[] = {
878 0x3577, /* PCI_CHIP_I830_M */
879 0x2562, /* PCI_CHIP_845_G */
880 0x3582, /* PCI_CHIP_I855_GM */
881 0x2572, /* PCI_CHIP_I865_G */
882 0x2582, /* PCI_CHIP_I915_G */
883 0x258a, /* PCI_CHIP_E7221_G */
884 0x2592, /* PCI_CHIP_I915_GM */
885 0x2772, /* PCI_CHIP_I945_G */
886 0x27a2, /* PCI_CHIP_I945_GM */
887 0x27ae, /* PCI_CHIP_I945_GME */
888 0x29b2, /* PCI_CHIP_Q35_G */
889 0x29c2, /* PCI_CHIP_G33_G */
890 0x29d2, /* PCI_CHIP_Q33_G */
893 const int i965_chip_ids
[] = {
894 0x29a2, /* PCI_CHIP_I965_G */
895 0x2992, /* PCI_CHIP_I965_Q */
896 0x2982, /* PCI_CHIP_I965_G_1 */
897 0x2972, /* PCI_CHIP_I946_GZ */
898 0x2a02, /* PCI_CHIP_I965_GM */
899 0x2a12, /* PCI_CHIP_I965_GME */
900 0x2a42, /* PCI_CHIP_GM45_GM */
901 0x2e02, /* PCI_CHIP_IGD_E_G */
902 0x2e12, /* PCI_CHIP_Q45_G */
903 0x2e22, /* PCI_CHIP_G45_G */
904 0x2e32, /* PCI_CHIP_G41_G */
907 const struct dri2_driver_map driver_map
[] = {
908 { 0x8086, "i915", i915_chip_ids
, ARRAY_SIZE(i915_chip_ids
) },
909 { 0x8086, "i965", i965_chip_ids
, ARRAY_SIZE(i965_chip_ids
) },
913 dri2_get_driver_for_fd(int fd
)
916 struct udev_device
*device
, *parent
;
920 int vendor_id
, chip_id
, i
, j
;
923 if (fstat(fd
, &buf
) < 0) {
924 _eglLog(_EGL_WARNING
, "EGL-DRI2: failed to stat fd %d", fd
);
928 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
929 if (device
== NULL
) {
930 _eglLog(_EGL_WARNING
,
931 "EGL-DRI2: could not create udev device for fd %d", fd
);
935 parent
= udev_device_get_parent(device
);
936 if (parent
== NULL
) {
937 _eglLog(_EGL_WARNING
, "DRI2: could not get parent device");
941 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
942 if (pci_id
== NULL
|| sscanf(pci_id
, "%x:%x", &vendor_id
, &chip_id
) != 2) {
943 _eglLog(_EGL_WARNING
, "EGL-DRI2: malformed or no PCI ID");
947 for (i
= 0; i
< ARRAY_SIZE(driver_map
); i
++) {
948 if (vendor_id
!= driver_map
[i
].vendor_id
)
950 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
951 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
952 driver
= strdup(driver_map
[i
].driver
);
953 _eglLog(_EGL_DEBUG
, "pci id for %d: %04x:%04x, driver %s",
954 fd
, vendor_id
, chip_id
, driver
);
960 udev_device_unref(device
);
967 dri2_initialize_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
,
968 EGLint
*major
, EGLint
*minor
)
970 struct dri2_egl_display
*dri2_dpy
;
973 dri2_dpy
= malloc(sizeof *dri2_dpy
);
975 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
977 disp
->DriverData
= (void *) dri2_dpy
;
978 dri2_dpy
->fd
= (int) disp
->PlatformDisplay
;
980 dri2_dpy
->driver_name
= dri2_get_driver_for_fd(dri2_dpy
->fd
);
981 if (dri2_dpy
->driver_name
== NULL
)
982 return _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
984 if (!dri2_load_driver(disp
))
985 goto cleanup_driver_name
;
987 dri2_dpy
->extensions
[0] = &image_lookup_extension
.base
;
988 dri2_dpy
->extensions
[1] = &use_invalidate
.base
;
989 dri2_dpy
->extensions
[2] = NULL
;
991 if (!dri2_create_screen(disp
))
994 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
995 dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
], i
+ 1, 0, 0);
997 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
998 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
999 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
1001 /* we're supporting EGL 1.4 */
1008 dlclose(dri2_dpy
->driver
);
1009 cleanup_driver_name
:
1010 free(dri2_dpy
->driver_name
);
1018 * Called via eglInitialize(), GLX_drv->API.Initialize().
1021 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1022 EGLint
*major
, EGLint
*minor
)
1024 switch (disp
->Platform
) {
1025 case _EGL_PLATFORM_X11
:
1026 return dri2_initialize_x11(drv
, disp
, major
, minor
);
1029 case _EGL_PLATFORM_DRM
:
1030 return dri2_initialize_drm(drv
, disp
, major
, minor
);
1039 * Called via eglTerminate(), drv->API.Terminate().
1042 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1044 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1046 _eglReleaseDisplayResources(drv
, disp
);
1047 _eglCleanupDisplay(disp
);
1049 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1050 close(dri2_dpy
->fd
);
1051 dlclose(dri2_dpy
->driver
);
1052 if (disp
->PlatformDisplay
== NULL
)
1053 xcb_disconnect(dri2_dpy
->conn
);
1055 disp
->DriverData
= NULL
;
1062 * Called via eglCreateContext(), drv->API.CreateContext().
1064 static _EGLContext
*
1065 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1066 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1068 struct dri2_egl_context
*dri2_ctx
;
1069 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1070 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1071 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1072 const __DRIconfig
*dri_config
;
1075 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1077 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1081 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1084 switch (dri2_ctx
->base
.ClientAPI
) {
1085 case EGL_OPENGL_ES_API
:
1086 switch (dri2_ctx
->base
.ClientVersion
) {
1088 api
= __DRI_API_GLES
;
1091 api
= __DRI_API_GLES2
;
1094 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1098 case EGL_OPENGL_API
:
1099 api
= __DRI_API_OPENGL
;
1102 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1107 dri_config
= dri2_config
->dri_config
;
1111 if (dri2_dpy
->dri2
->base
.version
>= 2) {
1112 dri2_ctx
->dri_context
=
1113 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1117 dri2_ctx_shared
->dri_context
: NULL
,
1119 } else if (api
== __DRI_API_OPENGL
) {
1120 dri2_ctx
->dri_context
=
1121 dri2_dpy
->dri2
->createNewContext(dri2_dpy
->dri_screen
,
1122 dri2_config
->dri_config
,
1124 dri2_ctx_shared
->dri_context
: NULL
,
1130 if (!dri2_ctx
->dri_context
)
1133 return &dri2_ctx
->base
;
1141 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1143 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1144 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1146 if (_eglIsSurfaceBound(surf
))
1149 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
1151 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
1153 if (surf
->Type
== EGL_PBUFFER_BIT
)
1154 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
1162 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1165 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1166 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1168 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1169 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1170 struct dri2_egl_surface
*dri2_dsurf
= dri2_egl_surface(dsurf
);
1171 struct dri2_egl_surface
*dri2_rsurf
= dri2_egl_surface(rsurf
);
1172 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1173 __DRIdrawable
*ddraw
, *rdraw
;
1176 /* bind the new context and return the "orphaned" one */
1177 if (!_eglBindContext(&ctx
, &dsurf
, &rsurf
))
1180 /* flush before context switch */
1181 if (ctx
&& dri2_drv
->glFlush
)
1182 dri2_drv
->glFlush();
1184 ddraw
= (dri2_dsurf
) ? dri2_dsurf
->dri_drawable
: NULL
;
1185 rdraw
= (dri2_rsurf
) ? dri2_rsurf
->dri_drawable
: NULL
;
1186 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1188 if ((cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
) ||
1189 dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1190 if (dsurf
&& !_eglIsSurfaceLinked(dsurf
))
1191 dri2_destroy_surface(drv
, disp
, dsurf
);
1192 if (rsurf
&& rsurf
!= dsurf
&& !_eglIsSurfaceLinked(dsurf
))
1193 dri2_destroy_surface(drv
, disp
, rsurf
);
1194 if (ctx
!= NULL
&& !_eglIsContextLinked(ctx
))
1195 dri2_dpy
->core
->unbindContext(dri2_egl_context(ctx
)->dri_context
);
1199 _eglBindContext(&ctx
, &dsurf
, &rsurf
);
1206 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1208 static _EGLSurface
*
1209 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
1210 _EGLConfig
*conf
, EGLNativeWindowType window
,
1211 const EGLint
*attrib_list
)
1213 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1214 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
1215 struct dri2_egl_surface
*dri2_surf
;
1216 xcb_get_geometry_cookie_t cookie
;
1217 xcb_get_geometry_reply_t
*reply
;
1218 xcb_screen_iterator_t s
;
1219 xcb_generic_error_t
*error
;
1221 dri2_surf
= malloc(sizeof *dri2_surf
);
1223 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
1227 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
1230 dri2_surf
->region
= XCB_NONE
;
1231 if (type
== EGL_PBUFFER_BIT
) {
1232 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
1233 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
1234 xcb_create_pixmap(dri2_dpy
->conn
,
1235 _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
),
1236 dri2_surf
->drawable
, s
.data
->root
,
1237 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
1239 dri2_surf
->drawable
= window
;
1242 dri2_surf
->dri_drawable
=
1243 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
1244 dri2_conf
->dri_config
, dri2_surf
);
1245 if (dri2_surf
->dri_drawable
== NULL
) {
1246 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
1247 goto cleanup_pixmap
;
1250 xcb_dri2_create_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
1252 if (type
!= EGL_PBUFFER_BIT
) {
1253 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
1254 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
1255 if (reply
== NULL
|| error
!= NULL
) {
1256 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
1258 goto cleanup_dri_drawable
;
1261 dri2_surf
->base
.Width
= reply
->width
;
1262 dri2_surf
->base
.Height
= reply
->height
;
1266 return &dri2_surf
->base
;
1268 cleanup_dri_drawable
:
1269 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
1271 if (type
== EGL_PBUFFER_BIT
)
1272 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
1280 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1282 static _EGLSurface
*
1283 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1284 _EGLConfig
*conf
, EGLNativeWindowType window
,
1285 const EGLint
*attrib_list
)
1287 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
1288 window
, attrib_list
);
1291 static _EGLSurface
*
1292 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1293 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
1294 const EGLint
*attrib_list
)
1296 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
1297 pixmap
, attrib_list
);
1300 static _EGLSurface
*
1301 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1302 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1304 return dri2_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
1305 XCB_WINDOW_NONE
, attrib_list
);
1309 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1310 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
1312 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1313 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1314 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1316 xcb_dri2_copy_region_cookie_t cookie
;
1318 if (dri2_drv
->glFlush
) {
1319 ctx
= _eglGetCurrentContext();
1320 if (ctx
&& ctx
->DrawSurface
== &dri2_surf
->base
)
1321 dri2_drv
->glFlush();
1324 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1327 /* FIXME: Add support for dri swapbuffers, that'll give us swap
1328 * interval and page flipping (at least for fullscreen windows) as
1329 * well as the page flip event. Unless surface->SwapBehavior is
1330 * EGL_BUFFER_PRESERVED. */
1331 #if __DRI2_FLUSH_VERSION >= 2
1333 (*pdraw
->psc
->f
->flushInvalidate
)(pdraw
->driDrawable
);
1337 if (!dri2_surf
->have_fake_front
)
1340 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
1341 dri2_surf
->drawable
,
1343 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
1344 XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
);
1345 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
1351 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1353 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1355 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
1359 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
1360 EGLint numRects
, const EGLint
*rects
)
1362 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1363 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1365 xcb_xfixes_region_t region
;
1366 xcb_rectangle_t rectangles
[16];
1369 if (numRects
> ARRAY_SIZE(rectangles
))
1370 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
1372 /* FIXME: Invert y here? */
1373 for (i
= 0; i
< numRects
; i
++) {
1374 rectangles
[i
].x
= rects
[i
* 4];
1375 rectangles
[i
].y
= rects
[i
* 4 + 1];
1376 rectangles
[i
].width
= rects
[i
* 4 + 2];
1377 rectangles
[i
].height
= rects
[i
* 4 + 3];
1380 region
= xcb_generate_id(dri2_dpy
->conn
);
1381 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
1382 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
1383 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
1389 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1392 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1394 /* FIXME: Do we need to support lookup of EGL symbols too? */
1396 return (_EGLProc
) _glapi_get_proc_address(procname
);
1400 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1402 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1403 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(ctx
->DrawSurface
);
1405 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1406 * we need to copy fake to real here.*/
1408 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1414 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1416 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1417 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1424 dri2_unload(_EGLDriver
*drv
)
1426 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1431 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1432 EGLNativePixmapType target
)
1434 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1435 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1438 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1440 gc
= xcb_generate_id(dri2_dpy
->conn
);
1441 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
1442 xcb_copy_area(dri2_dpy
->conn
,
1443 dri2_surf
->drawable
,
1448 dri2_surf
->base
.Width
,
1449 dri2_surf
->base
.Height
);
1450 xcb_free_gc(dri2_dpy
->conn
, gc
);
1456 dri2_bind_tex_image(_EGLDriver
*drv
,
1457 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1459 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1460 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1461 struct dri2_egl_context
*dri2_ctx
;
1463 GLint format
, target
;
1465 ctx
= _eglGetCurrentContext();
1466 dri2_ctx
= dri2_egl_context(ctx
);
1468 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1471 switch (dri2_surf
->base
.TextureFormat
) {
1472 case EGL_TEXTURE_RGB
:
1473 format
= __DRI_TEXTURE_FORMAT_RGB
;
1475 case EGL_TEXTURE_RGBA
:
1476 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1482 switch (dri2_surf
->base
.TextureTarget
) {
1483 case EGL_TEXTURE_2D
:
1484 target
= GL_TEXTURE_2D
;
1490 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1492 dri2_surf
->dri_drawable
);
1498 dri2_release_tex_image(_EGLDriver
*drv
,
1499 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1505 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1506 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1508 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1509 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1510 struct dri2_egl_image
*dri2_img
;
1511 unsigned int attachments
[1];
1512 xcb_drawable_t drawable
;
1513 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
1514 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
1515 xcb_dri2_dri2_buffer_t
*buffers
;
1516 xcb_get_geometry_cookie_t geometry_cookie
;
1517 xcb_get_geometry_reply_t
*geometry_reply
;
1518 xcb_generic_error_t
*error
;
1521 drawable
= (xcb_drawable_t
) buffer
;
1522 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
1523 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
1525 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
1526 drawable
, 1, 1, attachments
);
1527 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
1528 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
1529 buffers_cookie
, NULL
);
1530 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
1531 if (buffers
== NULL
) {
1535 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
1536 geometry_cookie
, &error
);
1537 if (geometry_reply
== NULL
|| error
!= NULL
) {
1538 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
1540 free(buffers_reply
);
1543 switch (geometry_reply
->depth
) {
1545 format
= __DRI_IMAGE_FORMAT_RGB565
;
1548 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
1551 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1554 _eglError(EGL_BAD_PARAMETER
,
1555 "dri2_create_image_khr: unsupported pixmap depth");
1556 free(buffers_reply
);
1557 free(geometry_reply
);
1561 dri2_img
= malloc(sizeof *dri2_img
);
1563 free(buffers_reply
);
1564 free(geometry_reply
);
1565 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1566 return EGL_NO_IMAGE_KHR
;
1569 if (!_eglInitImage(&dri2_img
->base
, disp
, attr_list
)) {
1570 free(buffers_reply
);
1571 free(geometry_reply
);
1572 return EGL_NO_IMAGE_KHR
;
1575 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
1576 dri2_img
->dri_image
=
1577 dri2_dpy
->image
->createImageFromName(dri2_ctx
->dri_context
,
1578 buffers_reply
->width
,
1579 buffers_reply
->height
,
1585 free(buffers_reply
);
1586 free(geometry_reply
);
1588 return &dri2_img
->base
;
1592 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1593 EGLClientBuffer buffer
,
1594 const EGLint
*attr_list
)
1596 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1597 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1598 struct dri2_egl_image
*dri2_img
;
1599 GLuint renderbuffer
= (GLuint
) buffer
;
1601 if (renderbuffer
== 0) {
1602 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1603 return EGL_NO_IMAGE_KHR
;
1606 dri2_img
= malloc(sizeof *dri2_img
);
1608 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1609 return EGL_NO_IMAGE_KHR
;
1612 if (!_eglInitImage(&dri2_img
->base
, disp
, attr_list
))
1613 return EGL_NO_IMAGE_KHR
;
1615 dri2_img
->dri_image
=
1616 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1620 return &dri2_img
->base
;
1624 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1625 _EGLContext
*ctx
, EGLenum target
,
1626 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1629 case EGL_NATIVE_PIXMAP_KHR
:
1630 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
1631 case EGL_GL_RENDERBUFFER_KHR
:
1632 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
1634 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1635 return EGL_NO_IMAGE_KHR
;
1640 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
1642 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1643 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
1645 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
1652 * This is the main entrypoint into the driver, called by libEGL.
1653 * Create a new _EGLDriver object and init its dispatch table.
1656 _eglMain(const char *args
)
1658 struct dri2_egl_driver
*dri2_drv
;
1660 dri2_drv
= malloc(sizeof *dri2_drv
);
1664 memset(dri2_drv
, 0, sizeof *dri2_drv
);
1665 _eglInitDriverFallbacks(&dri2_drv
->base
);
1666 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
1667 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
1668 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
1669 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
1670 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
1671 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
1672 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
1673 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
1674 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
1675 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
1676 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
1677 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
1678 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
;
1679 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
1680 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
1681 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image_khr
;
1682 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
1683 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
1685 dri2_drv
->base
.Name
= "DRI2";
1686 dri2_drv
->base
.Unload
= dri2_unload
;
1689 (void (*)(void)) dri2_get_proc_address(&dri2_drv
->base
, "glFlush");
1691 return &dri2_drv
->base
;