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 */
181 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
182 int depth
, xcb_visualtype_t
*visual
)
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
);
195 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
197 case __DRI_ATTRIB_RENDER_TYPE
:
198 if (value
& __DRI_ATTRIB_RGBA_BIT
)
199 value
= EGL_RGB_BUFFER
;
200 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
201 value
= EGL_LUMINANCE_BUFFER
;
204 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
207 case __DRI_ATTRIB_CONFIG_CAVEAT
:
208 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
209 value
= EGL_NON_CONFORMANT_CONFIG
;
210 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
211 value
= EGL_SLOW_CONFIG
;
214 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
217 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
218 bind_to_texture_rgb
= value
;
221 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
222 bind_to_texture_rgba
= value
;
225 case __DRI_ATTRIB_DOUBLE_BUFFER
:
226 double_buffer
= value
;
230 key
= dri2_to_egl_attribute_map
[attrib
];
232 _eglSetConfigKey(&base
, key
, value
);
237 /* In EGL, double buffer or not isn't a config attribute. Pixmaps
238 * surfaces are always single buffered, pbuffer surfaces are always
239 * back buffers and windows can be either, selected by passing an
240 * attribute at window surface construction time. To support this
241 * we ignore all double buffer configs and manipulate the buffer we
242 * return in the getBuffer callback to get the behaviour we want. */
247 if (visual
!= NULL
) {
248 if (depth
!= _eglGetConfigKey(&base
, EGL_BUFFER_SIZE
))
251 _eglSetConfigKey(&base
, EGL_SURFACE_TYPE
,
252 EGL_WINDOW_BIT
| EGL_PIXMAP_BIT
| EGL_PBUFFER_BIT
|
253 EGL_SWAP_BEHAVIOR_PRESERVED_BIT
);
255 _eglSetConfigKey(&base
, EGL_NATIVE_VISUAL_ID
, visual
->visual_id
);
256 _eglSetConfigKey(&base
, EGL_NATIVE_VISUAL_TYPE
, visual
->_class
);
258 _eglSetConfigKey(&base
, EGL_SURFACE_TYPE
,
259 EGL_PIXMAP_BIT
| EGL_PBUFFER_BIT
);
262 _eglSetConfigKey(&base
, EGL_NATIVE_RENDERABLE
, EGL_TRUE
);
263 _eglSetConfigKey(&base
, EGL_BIND_TO_TEXTURE_RGB
, bind_to_texture_rgb
);
264 if (_eglGetConfigKey(&base
, EGL_ALPHA_SIZE
) > 0)
265 _eglSetConfigKey(&base
,
266 EGL_BIND_TO_TEXTURE_RGBA
, bind_to_texture_rgba
);
268 _eglSetConfigKey(&base
, EGL_RENDERABLE_TYPE
, disp
->ClientAPIsMask
);
269 _eglSetConfigKey(&base
, EGL_CONFORMANT
, disp
->ClientAPIsMask
);
271 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
272 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
276 conf
= malloc(sizeof *conf
);
278 memcpy(&conf
->base
, &base
, sizeof base
);
279 conf
->dri_config
= dri_config
;
280 _eglAddConfig(disp
, &conf
->base
);
285 * Process list of buffer received from the server
287 * Processes the list of buffers received in a reply from the server to either
288 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
291 dri2_process_buffers(struct dri2_egl_surface
*dri2_surf
,
292 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
294 struct dri2_egl_display
*dri2_dpy
=
295 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
296 xcb_rectangle_t rectangle
;
299 dri2_surf
->buffer_count
= count
;
300 dri2_surf
->have_fake_front
= 0;
302 /* This assumes the DRI2 buffer attachment tokens matches the
303 * __DRIbuffer tokens. */
304 for (i
= 0; i
< count
; i
++) {
305 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
306 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
307 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
308 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
309 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
311 /* We only use the DRI drivers single buffer configs. This
312 * means that if we try to render to a window, DRI2 will give us
313 * the fake front buffer, which we'll use as a back buffer.
314 * Note that EGL doesn't require that several clients rendering
315 * to the same window must see the same aux buffers. */
316 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
317 dri2_surf
->have_fake_front
= 1;
320 if (dri2_surf
->region
!= XCB_NONE
)
321 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
325 rectangle
.width
= dri2_surf
->base
.Width
;
326 rectangle
.height
= dri2_surf
->base
.Height
;
327 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
328 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
332 dri2_get_buffers(__DRIdrawable
* driDrawable
,
333 int *width
, int *height
,
334 unsigned int *attachments
, int count
,
335 int *out_count
, void *loaderPrivate
)
337 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
338 struct dri2_egl_display
*dri2_dpy
=
339 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
340 xcb_dri2_dri2_buffer_t
*buffers
;
341 xcb_dri2_get_buffers_reply_t
*reply
;
342 xcb_dri2_get_buffers_cookie_t cookie
;
344 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
346 count
, count
, attachments
);
347 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
348 buffers
= xcb_dri2_get_buffers_buffers (reply
);
352 *out_count
= reply
->count
;
353 dri2_surf
->base
.Width
= *width
= reply
->width
;
354 dri2_surf
->base
.Height
= *height
= reply
->height
;
355 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
359 return dri2_surf
->buffers
;
363 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
365 /* FIXME: Does EGL support front buffer rendering at all? */
368 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
370 dri2WaitGL(dri2_surf
);
375 dri2_lookup_egl_image(__DRIcontext
*context
, void *image
, void *data
)
377 struct dri2_egl_context
*dri2_ctx
= data
;
378 _EGLDisplay
*disp
= dri2_ctx
->base
.Resource
.Display
;
379 struct dri2_egl_image
*dri2_img
;
382 img
= _eglLookupImage(image
, disp
);
384 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
388 dri2_img
= dri2_egl_image(image
);
390 return dri2_img
->dri_image
;
393 static const __DRIimageLookupExtension image_lookup_extension
= {
394 { __DRI_IMAGE_LOOKUP
, 1 },
395 dri2_lookup_egl_image
399 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
400 int *width
, int *height
,
401 unsigned int *attachments
, int count
,
402 int *out_count
, void *loaderPrivate
)
404 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
405 struct dri2_egl_display
*dri2_dpy
=
406 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
407 xcb_dri2_dri2_buffer_t
*buffers
;
408 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
409 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
410 xcb_dri2_attach_format_t
*format_attachments
;
412 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
413 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
418 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
423 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
424 dri2_surf
->base
.Width
= *width
= reply
->width
;
425 dri2_surf
->base
.Height
= *height
= reply
->height
;
426 *out_count
= reply
->count
;
427 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
431 return dri2_surf
->buffers
;
434 static const char dri_driver_path
[] = DEFAULT_DRIVER_DIR
;
436 struct dri2_extension_match
{
442 static struct dri2_extension_match dri2_driver_extensions
[] = {
443 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
444 { __DRI_DRI2
, 1, offsetof(struct dri2_egl_display
, dri2
) },
448 static struct dri2_extension_match dri2_core_extensions
[] = {
449 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
450 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
451 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
456 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
457 struct dri2_extension_match
*matches
,
458 const __DRIextension
**extensions
)
460 int i
, j
, ret
= EGL_TRUE
;
463 for (i
= 0; extensions
[i
]; i
++) {
464 _eglLog(_EGL_DEBUG
, "DRI2: found extension `%s'", extensions
[i
]->name
);
465 for (j
= 0; matches
[j
].name
; j
++) {
466 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
467 extensions
[i
]->version
>= matches
[j
].version
) {
468 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
469 *(const __DRIextension
**) field
= extensions
[i
];
470 _eglLog(_EGL_INFO
, "DRI2: found extension %s version %d",
471 extensions
[i
]->name
, extensions
[i
]->version
);
476 for (j
= 0; matches
[j
].name
; j
++) {
477 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
478 if (*(const __DRIextension
**) field
== NULL
) {
479 _eglLog(_EGL_FATAL
, "DRI2: did not find extension %s version %d",
480 matches
[j
].name
, matches
[j
].version
);
489 dri2_strndup(const char *s
, int length
)
493 d
= malloc(length
+ 1);
497 memcpy(d
, s
, length
);
504 dri2_connect(struct dri2_egl_display
*dri2_dpy
)
506 xcb_xfixes_query_version_reply_t
*xfixes_query
;
507 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
508 xcb_dri2_query_version_reply_t
*dri2_query
;
509 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
510 xcb_dri2_connect_reply_t
*connect
;
511 xcb_dri2_connect_cookie_t connect_cookie
;
512 xcb_generic_error_t
*error
;
513 xcb_screen_iterator_t s
;
515 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
516 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
518 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
519 XCB_XFIXES_MAJOR_VERSION
,
520 XCB_XFIXES_MINOR_VERSION
);
522 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
523 XCB_DRI2_MAJOR_VERSION
,
524 XCB_DRI2_MINOR_VERSION
);
526 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
527 connect_cookie
= xcb_dri2_connect_unchecked (dri2_dpy
->conn
,
529 XCB_DRI2_DRIVER_TYPE_DRI
);
532 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
533 xfixes_query_cookie
, &error
);
534 if (xfixes_query
== NULL
||
535 error
!= NULL
|| xfixes_query
->major_version
< 2) {
536 _eglLog(_EGL_FATAL
, "DRI2: failed to query xfixes version");
543 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
544 if (dri2_query
== NULL
|| error
!= NULL
) {
545 _eglLog(_EGL_FATAL
, "DRI2: failed to query version");
549 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
550 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
553 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
554 if (connect
== NULL
||
555 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
556 _eglLog(_EGL_FATAL
, "DRI2: failed to authenticate");
560 dri2_dpy
->device_name
=
561 dri2_strndup(xcb_dri2_connect_device_name (connect
),
562 xcb_dri2_connect_device_name_length (connect
));
564 dri2_dpy
->driver_name
=
565 dri2_strndup(xcb_dri2_connect_driver_name (connect
),
566 xcb_dri2_connect_driver_name_length (connect
));
568 if (dri2_dpy
->device_name
== NULL
|| dri2_dpy
->driver_name
== NULL
) {
569 free(dri2_dpy
->device_name
);
570 free(dri2_dpy
->driver_name
);
580 dri2_authenticate(struct dri2_egl_display
*dri2_dpy
)
582 xcb_dri2_authenticate_reply_t
*authenticate
;
583 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
584 xcb_screen_iterator_t s
;
587 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
588 _eglLog(_EGL_FATAL
, "DRI2: failed to get drm magic");
592 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
593 authenticate_cookie
=
594 xcb_dri2_authenticate_unchecked(dri2_dpy
->conn
, s
.data
->root
, magic
);
596 xcb_dri2_authenticate_reply(dri2_dpy
->conn
, authenticate_cookie
, NULL
);
597 if (authenticate
== NULL
|| !authenticate
->authenticated
) {
598 _eglLog(_EGL_FATAL
, "DRI2: failed to authenticate");
609 dri2_add_configs_for_visuals(struct dri2_egl_display
*dri2_dpy
,
612 xcb_screen_iterator_t s
;
613 xcb_depth_iterator_t d
;
614 xcb_visualtype_t
*visuals
;
617 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
618 d
= xcb_screen_allowed_depths_iterator(s
.data
);
621 EGLBoolean class_added
[6] = { 0, };
623 visuals
= xcb_depth_visuals(d
.data
);
624 for (i
= 0; i
< xcb_depth_visuals_length(d
.data
); i
++) {
625 if (class_added
[visuals
[i
]._class
])
628 class_added
[visuals
[i
]._class
] = EGL_TRUE
;
629 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++)
630 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
],
631 id
++, d
.data
->depth
, &visuals
[i
]);
637 if (!_eglGetArraySize(disp
->Configs
)) {
638 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
646 dri2_load_driver(_EGLDisplay
*disp
)
648 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
649 const __DRIextension
**extensions
;
650 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
653 if (geteuid() == getuid()) {
654 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
655 search_paths
= getenv("LIBGL_DRIVERS_PATH");
657 if (search_paths
== NULL
)
658 search_paths
= DEFAULT_DRIVER_DIR
;
660 dri2_dpy
->driver
= NULL
;
661 end
= search_paths
+ strlen(search_paths
);
662 for (p
= search_paths
; p
< end
&& dri2_dpy
->driver
== NULL
; p
= next
+ 1) {
664 next
= strchr(p
, ':');
670 snprintf(path
, sizeof path
,
671 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
672 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
674 if (dri2_dpy
->driver
== NULL
) {
675 snprintf(path
, sizeof path
,
676 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
677 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
678 if (dri2_dpy
->driver
== NULL
)
679 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
683 if (dri2_dpy
->driver
== NULL
) {
684 _eglLog(_EGL_WARNING
,
685 "DRI2: failed to open any driver (search paths %s)",
690 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
691 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
692 if (extensions
== NULL
) {
693 _eglLog(_EGL_WARNING
,
694 "DRI2: driver exports no extensions (%s)", dlerror());
695 dlclose(dri2_dpy
->driver
);
699 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
)) {
700 dlclose(dri2_dpy
->driver
);
708 dri2_create_screen(_EGLDisplay
*disp
)
710 const __DRIextension
**extensions
;
711 struct dri2_egl_display
*dri2_dpy
;
712 unsigned int api_mask
;
714 dri2_dpy
= disp
->DriverData
;
715 dri2_dpy
->dri_screen
=
716 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
, dri2_dpy
->extensions
,
717 &dri2_dpy
->driver_configs
, dri2_dpy
);
719 if (dri2_dpy
->dri_screen
== NULL
) {
720 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
724 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
725 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
))
726 goto cleanup_dri_screen
;
728 if (dri2_dpy
->dri2
->base
.version
>= 2)
729 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
731 api_mask
= __DRI_API_OPENGL
;
733 disp
->ClientAPIsMask
= 0;
734 if (api_mask
& (1 <<__DRI_API_OPENGL
))
735 disp
->ClientAPIsMask
|= EGL_OPENGL_BIT
;
736 if (api_mask
& (1 <<__DRI_API_GLES
))
737 disp
->ClientAPIsMask
|= EGL_OPENGL_ES_BIT
;
738 if (api_mask
& (1 << __DRI_API_GLES2
))
739 disp
->ClientAPIsMask
|= EGL_OPENGL_ES2_BIT
;
744 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
750 dri2_initialize_x11(_EGLDriver
*drv
, _EGLDisplay
*disp
,
751 EGLint
*major
, EGLint
*minor
)
753 struct dri2_egl_display
*dri2_dpy
;
755 dri2_dpy
= malloc(sizeof *dri2_dpy
);
757 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
759 disp
->DriverData
= (void *) dri2_dpy
;
760 if (disp
->PlatformDisplay
== NULL
) {
761 dri2_dpy
->conn
= xcb_connect(0, 0);
763 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
766 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
767 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
771 if (dri2_dpy
->conn
) {
772 if (!dri2_connect(dri2_dpy
))
776 if (!dri2_load_driver(disp
))
779 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
780 if (dri2_dpy
->fd
== -1) {
781 _eglLog(_EGL_WARNING
,
782 "DRI2: could not open %s (%s)", dri2_dpy
->device_name
,
787 if (dri2_dpy
->conn
) {
788 if (!dri2_authenticate(dri2_dpy
))
792 if (dri2_dpy
->dri2_minor
>= 1) {
793 dri2_dpy
->loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
794 dri2_dpy
->loader_extension
.base
.version
= 3;
795 dri2_dpy
->loader_extension
.getBuffers
= dri2_get_buffers
;
796 dri2_dpy
->loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
797 dri2_dpy
->loader_extension
.getBuffersWithFormat
=
798 dri2_get_buffers_with_format
;
800 dri2_dpy
->loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
801 dri2_dpy
->loader_extension
.base
.version
= 2;
802 dri2_dpy
->loader_extension
.getBuffers
= dri2_get_buffers
;
803 dri2_dpy
->loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
804 dri2_dpy
->loader_extension
.getBuffersWithFormat
= NULL
;
807 dri2_dpy
->extensions
[0] = &dri2_dpy
->loader_extension
.base
;
808 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
809 dri2_dpy
->extensions
[2] = NULL
;
811 if (!dri2_create_screen(disp
))
814 if (dri2_dpy
->conn
) {
815 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
816 goto cleanup_configs
;
819 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
820 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
821 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
822 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
823 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
824 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
826 /* we're supporting EGL 1.4 */
833 _eglCleanupDisplay(disp
);
834 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
838 dlclose(dri2_dpy
->driver
);
840 if (disp
->PlatformDisplay
== NULL
)
841 xcb_disconnect(dri2_dpy
->conn
);
850 struct dri2_driver_map
{
857 const int i915_chip_ids
[] = {
858 0x3577, /* PCI_CHIP_I830_M */
859 0x2562, /* PCI_CHIP_845_G */
860 0x3582, /* PCI_CHIP_I855_GM */
861 0x2572, /* PCI_CHIP_I865_G */
862 0x2582, /* PCI_CHIP_I915_G */
863 0x258a, /* PCI_CHIP_E7221_G */
864 0x2592, /* PCI_CHIP_I915_GM */
865 0x2772, /* PCI_CHIP_I945_G */
866 0x27a2, /* PCI_CHIP_I945_GM */
867 0x27ae, /* PCI_CHIP_I945_GME */
868 0x29b2, /* PCI_CHIP_Q35_G */
869 0x29c2, /* PCI_CHIP_G33_G */
870 0x29d2, /* PCI_CHIP_Q33_G */
873 const int i965_chip_ids
[] = {
874 0x29a2, /* PCI_CHIP_I965_G */
875 0x2992, /* PCI_CHIP_I965_Q */
876 0x2982, /* PCI_CHIP_I965_G_1 */
877 0x2972, /* PCI_CHIP_I946_GZ */
878 0x2a02, /* PCI_CHIP_I965_GM */
879 0x2a12, /* PCI_CHIP_I965_GME */
880 0x2a42, /* PCI_CHIP_GM45_GM */
881 0x2e02, /* PCI_CHIP_IGD_E_G */
882 0x2e12, /* PCI_CHIP_Q45_G */
883 0x2e22, /* PCI_CHIP_G45_G */
884 0x2e32, /* PCI_CHIP_G41_G */
887 const struct dri2_driver_map driver_map
[] = {
888 { 0x8086, "i915", i915_chip_ids
, ARRAY_SIZE(i915_chip_ids
) },
889 { 0x8086, "i965", i965_chip_ids
, ARRAY_SIZE(i965_chip_ids
) },
893 dri2_get_driver_for_fd(int fd
)
896 struct udev_device
*device
, *parent
;
900 int vendor_id
, chip_id
, i
, j
;
903 if (fstat(fd
, &buf
) < 0) {
904 _eglLog(_EGL_WARNING
, "EGL-DRI2: failed to stat fd %d", fd
);
908 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
909 if (device
== NULL
) {
910 _eglLog(_EGL_WARNING
,
911 "EGL-DRI2: could not create udev device for fd %d", fd
);
915 parent
= udev_device_get_parent(device
);
916 if (parent
== NULL
) {
917 _eglLog(_EGL_WARNING
, "DRI2: could not get parent device");
921 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
922 if (pci_id
== NULL
|| sscanf(pci_id
, "%x:%x", &vendor_id
, &chip_id
) != 2) {
923 _eglLog(_EGL_WARNING
, "EGL-DRI2: malformed or no PCI ID");
927 for (i
= 0; i
< ARRAY_SIZE(driver_map
); i
++) {
928 if (vendor_id
!= driver_map
[i
].vendor_id
)
930 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
931 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
932 driver
= strdup(driver_map
[i
].driver
);
933 _eglLog(_EGL_DEBUG
, "pci id for %d: %04x:%04x, driver %s",
934 fd
, vendor_id
, chip_id
, driver
);
940 udev_device_unref(device
);
947 dri2_initialize_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
,
948 EGLint
*major
, EGLint
*minor
)
950 struct dri2_egl_display
*dri2_dpy
;
952 dri2_dpy
= malloc(sizeof *dri2_dpy
);
954 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
956 disp
->DriverData
= (void *) dri2_dpy
;
957 dri2_dpy
->fd
= (int) disp
->PlatformDisplay
;
959 dri2_dpy
->driver_name
= dri2_get_driver_for_fd(dri2_dpy
->fd
);
960 if (dri2_dpy
->driver_name
== NULL
)
961 return _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
963 if (!dri2_load_driver(disp
))
964 goto cleanup_driver_name
;
966 dri2_dpy
->extensions
[0] = &image_lookup_extension
.base
;
967 dri2_dpy
->extensions
[1] = &use_invalidate
.base
;
968 dri2_dpy
->extensions
[2] = NULL
;
970 if (!dri2_create_screen(disp
))
973 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
974 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
975 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
980 dlclose(dri2_dpy
->driver
);
982 free(dri2_dpy
->driver_name
);
990 * Called via eglInitialize(), GLX_drv->API.Initialize().
993 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
,
994 EGLint
*major
, EGLint
*minor
)
996 switch (disp
->Platform
) {
997 case _EGL_PLATFORM_X11
:
998 return dri2_initialize_x11(drv
, disp
, major
, minor
);
1001 case _EGL_PLATFORM_DRM
:
1002 return dri2_initialize_drm(drv
, disp
, major
, minor
);
1011 * Called via eglTerminate(), drv->API.Terminate().
1014 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1016 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1018 _eglReleaseDisplayResources(drv
, disp
);
1019 _eglCleanupDisplay(disp
);
1021 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1022 close(dri2_dpy
->fd
);
1023 dlclose(dri2_dpy
->driver
);
1024 if (disp
->PlatformDisplay
== NULL
)
1025 xcb_disconnect(dri2_dpy
->conn
);
1027 disp
->DriverData
= NULL
;
1034 * Called via eglCreateContext(), drv->API.CreateContext().
1036 static _EGLContext
*
1037 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1038 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1040 struct dri2_egl_context
*dri2_ctx
;
1041 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1042 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1043 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1046 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1048 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1052 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1055 switch (dri2_ctx
->base
.ClientAPI
) {
1056 case EGL_OPENGL_ES_API
:
1057 switch (dri2_ctx
->base
.ClientVersion
) {
1059 api
= __DRI_API_GLES
;
1062 api
= __DRI_API_GLES2
;
1065 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1069 case EGL_OPENGL_API
:
1070 api
= __DRI_API_OPENGL
;
1073 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1077 if (dri2_dpy
->dri2
->base
.version
>= 2) {
1078 dri2_ctx
->dri_context
=
1079 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1081 dri2_config
->dri_config
,
1083 dri2_ctx_shared
->dri_context
: NULL
,
1085 } else if (api
== __DRI_API_OPENGL
) {
1086 dri2_ctx
->dri_context
=
1087 dri2_dpy
->dri2
->createNewContext(dri2_dpy
->dri_screen
,
1088 dri2_config
->dri_config
,
1090 dri2_ctx_shared
->dri_context
: NULL
,
1096 if (!dri2_ctx
->dri_context
)
1099 return &dri2_ctx
->base
;
1107 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1109 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1110 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1112 if (_eglIsSurfaceBound(surf
))
1115 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
1117 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
1119 if (surf
->Type
== EGL_PBUFFER_BIT
)
1120 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
1128 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1131 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1132 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1134 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1135 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1136 struct dri2_egl_surface
*dri2_dsurf
= dri2_egl_surface(dsurf
);
1137 struct dri2_egl_surface
*dri2_rsurf
= dri2_egl_surface(rsurf
);
1138 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1139 __DRIdrawable
*ddraw
, *rdraw
;
1142 /* bind the new context and return the "orphaned" one */
1143 if (!_eglBindContext(&ctx
, &dsurf
, &rsurf
))
1146 /* flush before context switch */
1147 if (ctx
&& dri2_drv
->glFlush
)
1148 dri2_drv
->glFlush();
1150 ddraw
= (dri2_dsurf
) ? dri2_dsurf
->dri_drawable
: NULL
;
1151 rdraw
= (dri2_rsurf
) ? dri2_rsurf
->dri_drawable
: NULL
;
1152 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1154 if ((cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
) ||
1155 dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1156 if (dsurf
&& !_eglIsSurfaceLinked(dsurf
))
1157 dri2_destroy_surface(drv
, disp
, dsurf
);
1158 if (rsurf
&& rsurf
!= dsurf
&& !_eglIsSurfaceLinked(dsurf
))
1159 dri2_destroy_surface(drv
, disp
, rsurf
);
1160 if (ctx
!= NULL
&& !_eglIsContextLinked(ctx
))
1161 dri2_dpy
->core
->unbindContext(dri2_egl_context(ctx
)->dri_context
);
1165 _eglBindContext(&ctx
, &dsurf
, &rsurf
);
1172 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1174 static _EGLSurface
*
1175 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
1176 _EGLConfig
*conf
, EGLNativeWindowType window
,
1177 const EGLint
*attrib_list
)
1179 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1180 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
1181 struct dri2_egl_surface
*dri2_surf
;
1182 xcb_get_geometry_cookie_t cookie
;
1183 xcb_get_geometry_reply_t
*reply
;
1184 xcb_screen_iterator_t s
;
1185 xcb_generic_error_t
*error
;
1187 dri2_surf
= malloc(sizeof *dri2_surf
);
1189 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
1193 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
1196 dri2_surf
->region
= XCB_NONE
;
1197 if (type
== EGL_PBUFFER_BIT
) {
1198 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
1199 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
1200 xcb_create_pixmap(dri2_dpy
->conn
,
1201 _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
),
1202 dri2_surf
->drawable
, s
.data
->root
,
1203 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
1205 dri2_surf
->drawable
= window
;
1208 dri2_surf
->dri_drawable
=
1209 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
1210 dri2_conf
->dri_config
, dri2_surf
);
1211 if (dri2_surf
->dri_drawable
== NULL
) {
1212 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
1213 goto cleanup_pixmap
;
1216 xcb_dri2_create_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
1218 if (type
!= EGL_PBUFFER_BIT
) {
1219 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
1220 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
1221 if (reply
== NULL
|| error
!= NULL
) {
1222 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
1224 goto cleanup_dri_drawable
;
1227 dri2_surf
->base
.Width
= reply
->width
;
1228 dri2_surf
->base
.Height
= reply
->height
;
1232 return &dri2_surf
->base
;
1234 cleanup_dri_drawable
:
1235 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
1237 if (type
== EGL_PBUFFER_BIT
)
1238 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
1246 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1248 static _EGLSurface
*
1249 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1250 _EGLConfig
*conf
, EGLNativeWindowType window
,
1251 const EGLint
*attrib_list
)
1253 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
1254 window
, attrib_list
);
1257 static _EGLSurface
*
1258 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1259 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
1260 const EGLint
*attrib_list
)
1262 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
1263 pixmap
, attrib_list
);
1266 static _EGLSurface
*
1267 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1268 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1270 return dri2_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
1271 XCB_WINDOW_NONE
, attrib_list
);
1275 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1276 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
1278 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1279 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1280 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1282 xcb_dri2_copy_region_cookie_t cookie
;
1284 if (dri2_drv
->glFlush
) {
1285 ctx
= _eglGetCurrentContext();
1286 if (ctx
&& ctx
->DrawSurface
== &dri2_surf
->base
)
1287 dri2_drv
->glFlush();
1290 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1293 /* FIXME: Add support for dri swapbuffers, that'll give us swap
1294 * interval and page flipping (at least for fullscreen windows) as
1295 * well as the page flip event. Unless surface->SwapBehavior is
1296 * EGL_BUFFER_PRESERVED. */
1297 #if __DRI2_FLUSH_VERSION >= 2
1299 (*pdraw
->psc
->f
->flushInvalidate
)(pdraw
->driDrawable
);
1303 if (!dri2_surf
->have_fake_front
)
1306 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
1307 dri2_surf
->drawable
,
1309 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
1310 XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
);
1311 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
1317 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1319 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1321 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
1325 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
1326 EGLint numRects
, const EGLint
*rects
)
1328 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1329 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1331 xcb_xfixes_region_t region
;
1332 xcb_rectangle_t rectangles
[16];
1335 if (numRects
> ARRAY_SIZE(rectangles
))
1336 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
1338 /* FIXME: Invert y here? */
1339 for (i
= 0; i
< numRects
; i
++) {
1340 rectangles
[i
].x
= rects
[i
* 4];
1341 rectangles
[i
].y
= rects
[i
* 4 + 1];
1342 rectangles
[i
].width
= rects
[i
* 4 + 2];
1343 rectangles
[i
].height
= rects
[i
* 4 + 3];
1346 region
= xcb_generate_id(dri2_dpy
->conn
);
1347 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
1348 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
1349 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
1355 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1358 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1360 /* FIXME: Do we need to support lookup of EGL symbols too? */
1362 return (_EGLProc
) _glapi_get_proc_address(procname
);
1366 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1368 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1369 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(ctx
->DrawSurface
);
1371 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1372 * we need to copy fake to real here.*/
1374 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1380 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1382 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1383 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1390 dri2_unload(_EGLDriver
*drv
)
1392 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1397 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1398 EGLNativePixmapType target
)
1400 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1401 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1404 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1406 gc
= xcb_generate_id(dri2_dpy
->conn
);
1407 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
1408 xcb_copy_area(dri2_dpy
->conn
,
1409 dri2_surf
->drawable
,
1414 dri2_surf
->base
.Width
,
1415 dri2_surf
->base
.Height
);
1416 xcb_free_gc(dri2_dpy
->conn
, gc
);
1422 dri2_bind_tex_image(_EGLDriver
*drv
,
1423 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1425 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1426 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1427 struct dri2_egl_context
*dri2_ctx
;
1429 GLint format
, target
;
1431 ctx
= _eglGetCurrentContext();
1432 dri2_ctx
= dri2_egl_context(ctx
);
1434 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1437 switch (dri2_surf
->base
.TextureFormat
) {
1438 case EGL_TEXTURE_RGB
:
1439 format
= __DRI_TEXTURE_FORMAT_RGB
;
1441 case EGL_TEXTURE_RGBA
:
1442 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1448 switch (dri2_surf
->base
.TextureTarget
) {
1449 case EGL_TEXTURE_2D
:
1450 target
= GL_TEXTURE_2D
;
1456 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1458 dri2_surf
->dri_drawable
);
1464 dri2_release_tex_image(_EGLDriver
*drv
,
1465 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1471 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1472 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1474 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1475 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1476 struct dri2_egl_image
*dri2_img
;
1477 unsigned int attachments
[1];
1478 xcb_drawable_t drawable
;
1479 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
1480 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
1481 xcb_dri2_dri2_buffer_t
*buffers
;
1482 xcb_get_geometry_cookie_t geometry_cookie
;
1483 xcb_get_geometry_reply_t
*geometry_reply
;
1484 xcb_generic_error_t
*error
;
1487 drawable
= (xcb_drawable_t
) buffer
;
1488 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
1489 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
1491 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
1492 drawable
, 1, 1, attachments
);
1493 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
1494 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
1495 buffers_cookie
, NULL
);
1496 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
1497 if (buffers
== NULL
) {
1501 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
1502 geometry_cookie
, &error
);
1503 if (geometry_reply
== NULL
|| error
!= NULL
) {
1504 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
1506 free(buffers_reply
);
1509 switch (geometry_reply
->depth
) {
1511 format
= __DRI_IMAGE_FORMAT_RGB565
;
1514 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
1517 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1520 _eglError(EGL_BAD_PARAMETER
,
1521 "dri2_create_image_khr: unsupported pixmap depth");
1522 free(buffers_reply
);
1523 free(geometry_reply
);
1527 dri2_img
= malloc(sizeof *dri2_img
);
1529 free(buffers_reply
);
1530 free(geometry_reply
);
1531 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1532 return EGL_NO_IMAGE_KHR
;
1535 if (!_eglInitImage(&dri2_img
->base
, disp
, attr_list
)) {
1536 free(buffers_reply
);
1537 free(geometry_reply
);
1538 return EGL_NO_IMAGE_KHR
;
1541 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
1542 dri2_img
->dri_image
=
1543 dri2_dpy
->image
->createImageFromName(dri2_ctx
->dri_context
,
1544 buffers_reply
->width
,
1545 buffers_reply
->height
,
1551 free(buffers_reply
);
1552 free(geometry_reply
);
1554 return &dri2_img
->base
;
1558 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1559 EGLClientBuffer buffer
,
1560 const EGLint
*attr_list
)
1562 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1563 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1564 struct dri2_egl_image
*dri2_img
;
1565 GLuint renderbuffer
= (GLuint
) buffer
;
1567 if (renderbuffer
== 0) {
1568 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1569 return EGL_NO_IMAGE_KHR
;
1572 dri2_img
= malloc(sizeof *dri2_img
);
1574 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1575 return EGL_NO_IMAGE_KHR
;
1578 if (!_eglInitImage(&dri2_img
->base
, disp
, attr_list
))
1579 return EGL_NO_IMAGE_KHR
;
1581 dri2_img
->dri_image
=
1582 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1586 return &dri2_img
->base
;
1590 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1591 _EGLContext
*ctx
, EGLenum target
,
1592 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1595 case EGL_NATIVE_PIXMAP_KHR
:
1596 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
1597 case EGL_GL_RENDERBUFFER_KHR
:
1598 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
1600 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1601 return EGL_NO_IMAGE_KHR
;
1606 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
1608 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1609 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
1611 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
1618 * This is the main entrypoint into the driver, called by libEGL.
1619 * Create a new _EGLDriver object and init its dispatch table.
1622 _eglMain(const char *args
)
1624 struct dri2_egl_driver
*dri2_drv
;
1626 dri2_drv
= malloc(sizeof *dri2_drv
);
1630 memset(dri2_drv
, 0, sizeof *dri2_drv
);
1631 _eglInitDriverFallbacks(&dri2_drv
->base
);
1632 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
1633 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
1634 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
1635 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
1636 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
1637 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
1638 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
1639 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
1640 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
1641 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
1642 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
1643 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
1644 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
;
1645 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
1646 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
1647 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image_khr
;
1648 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
1649 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
1651 dri2_drv
->base
.Name
= "DRI2";
1652 dri2_drv
->base
.Unload
= dri2_unload
;
1655 (void (*)(void)) dri2_get_proc_address(&dri2_drv
->base
, "glFlush");
1657 return &dri2_drv
->base
;