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
.MESA_drm_image
= EGL_TRUE
;
840 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
841 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
842 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
843 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
844 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
845 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
847 /* we're supporting EGL 1.4 */
854 _eglCleanupDisplay(disp
);
855 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
859 dlclose(dri2_dpy
->driver
);
861 if (disp
->PlatformDisplay
== NULL
)
862 xcb_disconnect(dri2_dpy
->conn
);
871 struct dri2_driver_map
{
878 const int i915_chip_ids
[] = {
879 0x3577, /* PCI_CHIP_I830_M */
880 0x2562, /* PCI_CHIP_845_G */
881 0x3582, /* PCI_CHIP_I855_GM */
882 0x2572, /* PCI_CHIP_I865_G */
883 0x2582, /* PCI_CHIP_I915_G */
884 0x258a, /* PCI_CHIP_E7221_G */
885 0x2592, /* PCI_CHIP_I915_GM */
886 0x2772, /* PCI_CHIP_I945_G */
887 0x27a2, /* PCI_CHIP_I945_GM */
888 0x27ae, /* PCI_CHIP_I945_GME */
889 0x29b2, /* PCI_CHIP_Q35_G */
890 0x29c2, /* PCI_CHIP_G33_G */
891 0x29d2, /* PCI_CHIP_Q33_G */
892 0xa011, /* Pineview */
895 const int i965_chip_ids
[] = {
896 0x29a2, /* PCI_CHIP_I965_G */
897 0x2992, /* PCI_CHIP_I965_Q */
898 0x2982, /* PCI_CHIP_I965_G_1 */
899 0x2972, /* PCI_CHIP_I946_GZ */
900 0x2a02, /* PCI_CHIP_I965_GM */
901 0x2a12, /* PCI_CHIP_I965_GME */
902 0x2a42, /* PCI_CHIP_GM45_GM */
903 0x2e02, /* PCI_CHIP_IGD_E_G */
904 0x2e12, /* PCI_CHIP_Q45_G */
905 0x2e22, /* PCI_CHIP_G45_G */
906 0x2e32, /* PCI_CHIP_G41_G */
909 const struct dri2_driver_map driver_map
[] = {
910 { 0x8086, "i915", i915_chip_ids
, ARRAY_SIZE(i915_chip_ids
) },
911 { 0x8086, "i965", i965_chip_ids
, ARRAY_SIZE(i965_chip_ids
) },
915 dri2_get_driver_for_fd(int fd
)
918 struct udev_device
*device
, *parent
;
922 int vendor_id
, chip_id
, i
, j
;
925 if (fstat(fd
, &buf
) < 0) {
926 _eglLog(_EGL_WARNING
, "EGL-DRI2: failed to stat fd %d", fd
);
930 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
931 if (device
== NULL
) {
932 _eglLog(_EGL_WARNING
,
933 "EGL-DRI2: could not create udev device for fd %d", fd
);
937 parent
= udev_device_get_parent(device
);
938 if (parent
== NULL
) {
939 _eglLog(_EGL_WARNING
, "DRI2: could not get parent device");
943 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
944 if (pci_id
== NULL
|| sscanf(pci_id
, "%x:%x", &vendor_id
, &chip_id
) != 2) {
945 _eglLog(_EGL_WARNING
, "EGL-DRI2: malformed or no PCI ID");
949 for (i
= 0; i
< ARRAY_SIZE(driver_map
); i
++) {
950 if (vendor_id
!= driver_map
[i
].vendor_id
)
952 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
953 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
954 driver
= strdup(driver_map
[i
].driver
);
955 _eglLog(_EGL_DEBUG
, "pci id for %d: %04x:%04x, driver %s",
956 fd
, vendor_id
, chip_id
, driver
);
962 udev_device_unref(device
);
969 dri2_initialize_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
,
970 EGLint
*major
, EGLint
*minor
)
972 struct dri2_egl_display
*dri2_dpy
;
975 dri2_dpy
= malloc(sizeof *dri2_dpy
);
977 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
979 disp
->DriverData
= (void *) dri2_dpy
;
980 dri2_dpy
->fd
= (int) disp
->PlatformDisplay
;
982 dri2_dpy
->driver_name
= dri2_get_driver_for_fd(dri2_dpy
->fd
);
983 if (dri2_dpy
->driver_name
== NULL
)
984 return _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
986 if (!dri2_load_driver(disp
))
987 goto cleanup_driver_name
;
989 dri2_dpy
->extensions
[0] = &image_lookup_extension
.base
;
990 dri2_dpy
->extensions
[1] = &use_invalidate
.base
;
991 dri2_dpy
->extensions
[2] = NULL
;
993 if (!dri2_create_screen(disp
))
996 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
997 dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
], i
+ 1, 0, 0);
999 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
1000 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
1001 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
1002 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
1004 /* we're supporting EGL 1.4 */
1011 dlclose(dri2_dpy
->driver
);
1012 cleanup_driver_name
:
1013 free(dri2_dpy
->driver_name
);
1021 * Called via eglInitialize(), GLX_drv->API.Initialize().
1024 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1025 EGLint
*major
, EGLint
*minor
)
1027 switch (disp
->Platform
) {
1028 case _EGL_PLATFORM_X11
:
1029 return dri2_initialize_x11(drv
, disp
, major
, minor
);
1032 case _EGL_PLATFORM_DRM
:
1033 return dri2_initialize_drm(drv
, disp
, major
, minor
);
1042 * Called via eglTerminate(), drv->API.Terminate().
1045 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1047 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1049 _eglReleaseDisplayResources(drv
, disp
);
1050 _eglCleanupDisplay(disp
);
1052 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1053 close(dri2_dpy
->fd
);
1054 dlclose(dri2_dpy
->driver
);
1055 if (disp
->PlatformDisplay
== NULL
)
1056 xcb_disconnect(dri2_dpy
->conn
);
1058 disp
->DriverData
= NULL
;
1065 * Called via eglCreateContext(), drv->API.CreateContext().
1067 static _EGLContext
*
1068 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1069 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1071 struct dri2_egl_context
*dri2_ctx
;
1072 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1073 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1074 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1075 const __DRIconfig
*dri_config
;
1078 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1080 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1084 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1087 switch (dri2_ctx
->base
.ClientAPI
) {
1088 case EGL_OPENGL_ES_API
:
1089 switch (dri2_ctx
->base
.ClientVersion
) {
1091 api
= __DRI_API_GLES
;
1094 api
= __DRI_API_GLES2
;
1097 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1101 case EGL_OPENGL_API
:
1102 api
= __DRI_API_OPENGL
;
1105 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1110 dri_config
= dri2_config
->dri_config
;
1114 if (dri2_dpy
->dri2
->base
.version
>= 2) {
1115 dri2_ctx
->dri_context
=
1116 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1120 dri2_ctx_shared
->dri_context
: NULL
,
1122 } else if (api
== __DRI_API_OPENGL
) {
1123 dri2_ctx
->dri_context
=
1124 dri2_dpy
->dri2
->createNewContext(dri2_dpy
->dri_screen
,
1125 dri2_config
->dri_config
,
1127 dri2_ctx_shared
->dri_context
: NULL
,
1133 if (!dri2_ctx
->dri_context
)
1136 return &dri2_ctx
->base
;
1144 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1146 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1147 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1149 if (_eglIsSurfaceBound(surf
))
1152 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
1154 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
1156 if (surf
->Type
== EGL_PBUFFER_BIT
)
1157 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
1165 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1168 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1169 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1171 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1172 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1173 struct dri2_egl_surface
*dri2_dsurf
= dri2_egl_surface(dsurf
);
1174 struct dri2_egl_surface
*dri2_rsurf
= dri2_egl_surface(rsurf
);
1175 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1176 __DRIdrawable
*ddraw
, *rdraw
;
1179 /* bind the new context and return the "orphaned" one */
1180 if (!_eglBindContext(&ctx
, &dsurf
, &rsurf
))
1183 /* flush before context switch */
1184 if (ctx
&& dri2_drv
->glFlush
)
1185 dri2_drv
->glFlush();
1187 ddraw
= (dri2_dsurf
) ? dri2_dsurf
->dri_drawable
: NULL
;
1188 rdraw
= (dri2_rsurf
) ? dri2_rsurf
->dri_drawable
: NULL
;
1189 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1191 if ((cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
) ||
1192 dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1193 if (dsurf
&& !_eglIsSurfaceLinked(dsurf
))
1194 dri2_destroy_surface(drv
, disp
, dsurf
);
1195 if (rsurf
&& rsurf
!= dsurf
&& !_eglIsSurfaceLinked(dsurf
))
1196 dri2_destroy_surface(drv
, disp
, rsurf
);
1197 if (ctx
!= NULL
&& !_eglIsContextLinked(ctx
))
1198 dri2_dpy
->core
->unbindContext(dri2_egl_context(ctx
)->dri_context
);
1202 _eglBindContext(&ctx
, &dsurf
, &rsurf
);
1209 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1211 static _EGLSurface
*
1212 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
1213 _EGLConfig
*conf
, EGLNativeWindowType window
,
1214 const EGLint
*attrib_list
)
1216 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1217 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
1218 struct dri2_egl_surface
*dri2_surf
;
1219 xcb_get_geometry_cookie_t cookie
;
1220 xcb_get_geometry_reply_t
*reply
;
1221 xcb_screen_iterator_t s
;
1222 xcb_generic_error_t
*error
;
1224 dri2_surf
= malloc(sizeof *dri2_surf
);
1226 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
1230 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
1233 dri2_surf
->region
= XCB_NONE
;
1234 if (type
== EGL_PBUFFER_BIT
) {
1235 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
1236 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
1237 xcb_create_pixmap(dri2_dpy
->conn
,
1238 _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
),
1239 dri2_surf
->drawable
, s
.data
->root
,
1240 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
1242 dri2_surf
->drawable
= window
;
1245 dri2_surf
->dri_drawable
=
1246 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
1247 dri2_conf
->dri_config
, dri2_surf
);
1248 if (dri2_surf
->dri_drawable
== NULL
) {
1249 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
1250 goto cleanup_pixmap
;
1253 xcb_dri2_create_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
1255 if (type
!= EGL_PBUFFER_BIT
) {
1256 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
1257 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
1258 if (reply
== NULL
|| error
!= NULL
) {
1259 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
1261 goto cleanup_dri_drawable
;
1264 dri2_surf
->base
.Width
= reply
->width
;
1265 dri2_surf
->base
.Height
= reply
->height
;
1269 return &dri2_surf
->base
;
1271 cleanup_dri_drawable
:
1272 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
1274 if (type
== EGL_PBUFFER_BIT
)
1275 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
1283 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1285 static _EGLSurface
*
1286 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1287 _EGLConfig
*conf
, EGLNativeWindowType window
,
1288 const EGLint
*attrib_list
)
1290 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
1291 window
, attrib_list
);
1294 static _EGLSurface
*
1295 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1296 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
1297 const EGLint
*attrib_list
)
1299 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
1300 pixmap
, attrib_list
);
1303 static _EGLSurface
*
1304 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1305 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1307 return dri2_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
1308 XCB_WINDOW_NONE
, attrib_list
);
1312 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1313 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
1315 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1316 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1317 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1319 xcb_dri2_copy_region_cookie_t cookie
;
1321 if (dri2_drv
->glFlush
) {
1322 ctx
= _eglGetCurrentContext();
1323 if (ctx
&& ctx
->DrawSurface
== &dri2_surf
->base
)
1324 dri2_drv
->glFlush();
1327 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1330 /* FIXME: Add support for dri swapbuffers, that'll give us swap
1331 * interval and page flipping (at least for fullscreen windows) as
1332 * well as the page flip event. Unless surface->SwapBehavior is
1333 * EGL_BUFFER_PRESERVED. */
1334 #if __DRI2_FLUSH_VERSION >= 2
1336 (*pdraw
->psc
->f
->flushInvalidate
)(pdraw
->driDrawable
);
1340 if (!dri2_surf
->have_fake_front
)
1343 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
1344 dri2_surf
->drawable
,
1346 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
1347 XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
);
1348 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
1354 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1356 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1358 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
1362 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
1363 EGLint numRects
, const EGLint
*rects
)
1365 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1366 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1368 xcb_xfixes_region_t region
;
1369 xcb_rectangle_t rectangles
[16];
1372 if (numRects
> ARRAY_SIZE(rectangles
))
1373 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
1375 /* FIXME: Invert y here? */
1376 for (i
= 0; i
< numRects
; i
++) {
1377 rectangles
[i
].x
= rects
[i
* 4];
1378 rectangles
[i
].y
= rects
[i
* 4 + 1];
1379 rectangles
[i
].width
= rects
[i
* 4 + 2];
1380 rectangles
[i
].height
= rects
[i
* 4 + 3];
1383 region
= xcb_generate_id(dri2_dpy
->conn
);
1384 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
1385 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
1386 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
1392 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1395 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1397 /* FIXME: Do we need to support lookup of EGL symbols too? */
1399 return (_EGLProc
) _glapi_get_proc_address(procname
);
1403 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1405 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1406 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(ctx
->DrawSurface
);
1408 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1409 * we need to copy fake to real here.*/
1411 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1417 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1419 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1420 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1427 dri2_unload(_EGLDriver
*drv
)
1429 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1434 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1435 EGLNativePixmapType target
)
1437 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1438 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1441 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1443 gc
= xcb_generate_id(dri2_dpy
->conn
);
1444 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
1445 xcb_copy_area(dri2_dpy
->conn
,
1446 dri2_surf
->drawable
,
1451 dri2_surf
->base
.Width
,
1452 dri2_surf
->base
.Height
);
1453 xcb_free_gc(dri2_dpy
->conn
, gc
);
1459 dri2_bind_tex_image(_EGLDriver
*drv
,
1460 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1462 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1463 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1464 struct dri2_egl_context
*dri2_ctx
;
1466 GLint format
, target
;
1468 ctx
= _eglGetCurrentContext();
1469 dri2_ctx
= dri2_egl_context(ctx
);
1471 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1474 switch (dri2_surf
->base
.TextureFormat
) {
1475 case EGL_TEXTURE_RGB
:
1476 format
= __DRI_TEXTURE_FORMAT_RGB
;
1478 case EGL_TEXTURE_RGBA
:
1479 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1485 switch (dri2_surf
->base
.TextureTarget
) {
1486 case EGL_TEXTURE_2D
:
1487 target
= GL_TEXTURE_2D
;
1493 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1495 dri2_surf
->dri_drawable
);
1501 dri2_release_tex_image(_EGLDriver
*drv
,
1502 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1508 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1509 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1511 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1512 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1513 struct dri2_egl_image
*dri2_img
;
1514 unsigned int attachments
[1];
1515 xcb_drawable_t drawable
;
1516 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
1517 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
1518 xcb_dri2_dri2_buffer_t
*buffers
;
1519 xcb_get_geometry_cookie_t geometry_cookie
;
1520 xcb_get_geometry_reply_t
*geometry_reply
;
1521 xcb_generic_error_t
*error
;
1524 drawable
= (xcb_drawable_t
) buffer
;
1525 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
1526 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
1528 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
1529 drawable
, 1, 1, attachments
);
1530 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
1531 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
1532 buffers_cookie
, NULL
);
1533 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
1534 if (buffers
== NULL
) {
1538 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
1539 geometry_cookie
, &error
);
1540 if (geometry_reply
== NULL
|| error
!= NULL
) {
1541 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
1543 free(buffers_reply
);
1546 switch (geometry_reply
->depth
) {
1548 format
= __DRI_IMAGE_FORMAT_RGB565
;
1551 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
1554 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1557 _eglError(EGL_BAD_PARAMETER
,
1558 "dri2_create_image_khr: unsupported pixmap depth");
1559 free(buffers_reply
);
1560 free(geometry_reply
);
1564 dri2_img
= malloc(sizeof *dri2_img
);
1566 free(buffers_reply
);
1567 free(geometry_reply
);
1568 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1569 return EGL_NO_IMAGE_KHR
;
1572 if (!_eglInitImage(&dri2_img
->base
, disp
, attr_list
)) {
1573 free(buffers_reply
);
1574 free(geometry_reply
);
1575 return EGL_NO_IMAGE_KHR
;
1578 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
1579 dri2_img
->dri_image
=
1580 dri2_dpy
->image
->createImageFromName(dri2_ctx
->dri_context
,
1581 buffers_reply
->width
,
1582 buffers_reply
->height
,
1588 free(buffers_reply
);
1589 free(geometry_reply
);
1591 return &dri2_img
->base
;
1595 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1596 EGLClientBuffer buffer
,
1597 const EGLint
*attr_list
)
1599 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1600 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1601 struct dri2_egl_image
*dri2_img
;
1602 GLuint renderbuffer
= (GLuint
) buffer
;
1604 if (renderbuffer
== 0) {
1605 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1606 return EGL_NO_IMAGE_KHR
;
1609 dri2_img
= malloc(sizeof *dri2_img
);
1611 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1612 return EGL_NO_IMAGE_KHR
;
1615 if (!_eglInitImage(&dri2_img
->base
, disp
, attr_list
))
1616 return EGL_NO_IMAGE_KHR
;
1618 dri2_img
->dri_image
=
1619 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1623 return &dri2_img
->base
;
1627 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1628 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1630 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1631 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1632 struct dri2_egl_image
*dri2_img
;
1633 EGLint width
, height
, format
, name
, stride
, pitch
, i
, err
;
1635 name
= (EGLint
) buffer
;
1643 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
++) {
1644 EGLint attr
= attr_list
[i
++];
1645 EGLint val
= attr_list
[i
];
1654 case EGL_DRM_BUFFER_FORMAT_MESA
:
1657 case EGL_DRM_BUFFER_STRIDE_MESA
:
1661 err
= EGL_BAD_ATTRIBUTE
;
1665 if (err
!= EGL_SUCCESS
) {
1666 _eglLog(_EGL_WARNING
, "bad image attribute 0x%04x", attr
);
1671 if (width
<= 0 || height
<= 0 || stride
<= 0) {
1672 _eglError(EGL_BAD_PARAMETER
,
1673 "bad width, height or stride");
1678 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1679 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1683 _eglError(EGL_BAD_PARAMETER
,
1684 "dri2_create_image_khr: unsupported pixmap depth");
1688 dri2_img
= malloc(sizeof *dri2_img
);
1690 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_mesa_drm");
1694 if (!_eglInitImage(&dri2_img
->base
, disp
, attr_list
)) {
1699 dri2_img
->dri_image
=
1700 dri2_dpy
->image
->createImageFromName(dri2_ctx
->dri_context
,
1707 if (dri2_img
->dri_image
== NULL
) {
1709 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_mesa_drm");
1713 return &dri2_img
->base
;
1717 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1718 _EGLContext
*ctx
, EGLenum target
,
1719 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1722 case EGL_NATIVE_PIXMAP_KHR
:
1723 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
1724 case EGL_GL_RENDERBUFFER_KHR
:
1725 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
1726 case EGL_DRM_BUFFER_MESA
:
1727 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
1729 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1730 return EGL_NO_IMAGE_KHR
;
1735 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
1737 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1738 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
1740 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
1747 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1748 const EGLint
*attr_list
)
1750 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1751 struct dri2_egl_image
*dri2_img
;
1752 int width
, height
, format
, i
;
1753 unsigned int use
, dri_use
, valid_mask
;
1754 EGLint err
= EGL_SUCCESS
;
1756 dri2_img
= malloc(sizeof *dri2_img
);
1758 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1759 return EGL_NO_IMAGE_KHR
;
1763 err
= EGL_BAD_PARAMETER
;
1767 if (!_eglInitImage(&dri2_img
->base
, disp
, attr_list
)) {
1768 err
= EGL_BAD_PARAMETER
;
1776 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
++) {
1777 EGLint attr
= attr_list
[i
++];
1778 EGLint val
= attr_list
[i
];
1787 case EGL_DRM_BUFFER_FORMAT_MESA
:
1790 case EGL_DRM_BUFFER_USE_MESA
:
1794 err
= EGL_BAD_ATTRIBUTE
;
1798 if (err
!= EGL_SUCCESS
) {
1799 _eglLog(_EGL_WARNING
, "bad image attribute 0x%04x", attr
);
1804 if (width
<= 0 || height
<= 0) {
1805 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)", width
, height
);
1810 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1811 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1814 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x", format
);
1819 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
1820 EGL_DRM_BUFFER_USE_SHARE_MESA
;
1821 if (use
& ~valid_mask
) {
1822 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x", use
& ~valid_mask
);
1827 if (use
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
1828 dri_use
|= __DRI_IMAGE_USE_SHARE
;
1829 if (use
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
1830 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
1832 dri2_img
->dri_image
=
1833 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
1834 width
, height
, format
, dri_use
, dri2_img
);
1835 if (dri2_img
->dri_image
== NULL
) {
1836 err
= EGL_BAD_ALLOC
;
1840 return &dri2_img
->base
;
1844 _eglError(err
, "dri2_create_drm_image_mesa");
1846 return EGL_NO_IMAGE_KHR
;
1850 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
1851 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
1853 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1854 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1856 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
1857 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
1858 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
1863 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
1864 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
1867 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
1868 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
1874 * This is the main entrypoint into the driver, called by libEGL.
1875 * Create a new _EGLDriver object and init its dispatch table.
1878 _eglMain(const char *args
)
1880 struct dri2_egl_driver
*dri2_drv
;
1882 dri2_drv
= malloc(sizeof *dri2_drv
);
1886 memset(dri2_drv
, 0, sizeof *dri2_drv
);
1887 _eglInitDriverFallbacks(&dri2_drv
->base
);
1888 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
1889 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
1890 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
1891 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
1892 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
1893 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
1894 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
1895 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
1896 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
1897 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
1898 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
1899 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
1900 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
;
1901 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
1902 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
1903 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image_khr
;
1904 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
1905 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
1906 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
1907 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
1909 dri2_drv
->base
.Name
= "DRI2";
1910 dri2_drv
->base
.Unload
= dri2_unload
;
1913 (void (*)(void)) dri2_get_proc_address(&dri2_drv
->base
, "glFlush");
1915 return &dri2_drv
->base
;