2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Kristian Høgsberg <krh@bitplanet.net>
38 #include <GL/internal/dri_interface.h>
41 #include <xcb/xfixes.h>
42 #include <X11/Xlib-xcb.h>
44 #include <glapi/glapi.h>
45 #include "eglconfig.h"
46 #include "eglcontext.h"
47 #include "egldisplay.h"
48 #include "egldriver.h"
49 #include "eglcurrent.h"
51 #include "eglsurface.h"
54 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
56 struct dri2_egl_driver
60 void (*glFlush
)(void);
63 struct dri2_egl_display
65 xcb_connection_t
*conn
;
68 __DRIscreen
*dri_screen
;
69 const __DRIconfig
**driver_configs
;
71 __DRIcoreExtension
*core
;
72 __DRIdri2Extension
*dri2
;
73 __DRI2flushExtension
*flush
;
74 __DRItexBufferExtension
*tex_buffer
;
75 __DRIimageExtension
*image
;
81 __DRIdri2LoaderExtension loader_extension
;
82 __DRIimageLookupExtension image_lookup_extension
;
83 const __DRIextension
*extensions
[3];
86 struct dri2_egl_context
89 __DRIcontext
*dri_context
;
92 struct dri2_egl_surface
95 __DRIdrawable
*dri_drawable
;
96 xcb_drawable_t drawable
;
97 __DRIbuffer buffers
[5];
99 xcb_xfixes_region_t region
;
104 struct dri2_egl_config
107 const __DRIconfig
*dri_config
;
110 struct dri2_egl_image
113 __DRIimage
*dri_image
;
116 /* standard typecasts */
117 _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl
)
118 _EGL_DRIVER_TYPECAST(dri2_egl_image
, _EGLImage
, obj
)
120 EGLint dri2_to_egl_attribute_map
[] = {
122 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
123 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
124 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
125 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
126 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
127 EGL_LUMINANCE_SIZE
, /* __DRI_ATTRIB_LUMINANCE_SIZE */
128 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
129 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
130 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
131 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
132 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
133 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
134 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
135 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
136 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
137 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
138 0, /* __DRI_ATTRIB_RENDER_TYPE, */
139 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
140 0, /* __DRI_ATTRIB_CONFORMANT */
141 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
142 0, /* __DRI_ATTRIB_STEREO */
143 0, /* __DRI_ATTRIB_AUX_BUFFERS */
144 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
145 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
146 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
147 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
148 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
149 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
150 0, /* __DRI_ATTRIB_FLOAT_MODE */
151 0, /* __DRI_ATTRIB_RED_MASK */
152 0, /* __DRI_ATTRIB_GREEN_MASK */
153 0, /* __DRI_ATTRIB_BLUE_MASK */
154 0, /* __DRI_ATTRIB_ALPHA_MASK */
155 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
156 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
157 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
158 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
159 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
160 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
161 0, /* __DRI_ATTRIB_SWAP_METHOD */
162 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
163 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
164 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
165 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
166 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
167 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
168 EGL_Y_INVERTED_NOK
, /* __DRI_ATTRIB_YINVERTED */
172 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
173 int depth
, xcb_visualtype_t
*visual
)
175 struct dri2_egl_config
*conf
;
176 struct dri2_egl_display
*dri2_dpy
;
178 unsigned int attrib
, value
, double_buffer
;
179 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
182 dri2_dpy
= disp
->DriverData
;
183 _eglInitConfig(&base
, disp
, id
);
186 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
188 case __DRI_ATTRIB_RENDER_TYPE
:
189 if (value
& __DRI_ATTRIB_RGBA_BIT
)
190 value
= EGL_RGB_BUFFER
;
191 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
192 value
= EGL_LUMINANCE_BUFFER
;
195 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
198 case __DRI_ATTRIB_CONFIG_CAVEAT
:
199 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
200 value
= EGL_NON_CONFORMANT_CONFIG
;
201 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
202 value
= EGL_SLOW_CONFIG
;
205 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
208 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
209 bind_to_texture_rgb
= value
;
212 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
213 bind_to_texture_rgba
= value
;
216 case __DRI_ATTRIB_DOUBLE_BUFFER
:
217 double_buffer
= value
;
221 key
= dri2_to_egl_attribute_map
[attrib
];
223 _eglSetConfigKey(&base
, key
, value
);
228 /* In EGL, double buffer or not isn't a config attribute. Pixmaps
229 * surfaces are always single buffered, pbuffer surfaces are always
230 * back buffers and windows can be either, selected by passing an
231 * attribute at window surface construction time. To support this
232 * we ignore all double buffer configs and manipulate the buffer we
233 * return in the getBuffer callback to get the behaviour we want. */
238 if (visual
!= NULL
) {
239 if (depth
!= _eglGetConfigKey(&base
, EGL_BUFFER_SIZE
))
242 _eglSetConfigKey(&base
, EGL_SURFACE_TYPE
,
243 EGL_WINDOW_BIT
| EGL_PIXMAP_BIT
| EGL_PBUFFER_BIT
|
244 EGL_SWAP_BEHAVIOR_PRESERVED_BIT
);
246 _eglSetConfigKey(&base
, EGL_NATIVE_VISUAL_ID
, visual
->visual_id
);
247 _eglSetConfigKey(&base
, EGL_NATIVE_VISUAL_TYPE
, visual
->_class
);
249 _eglSetConfigKey(&base
, EGL_SURFACE_TYPE
,
250 EGL_PIXMAP_BIT
| EGL_PBUFFER_BIT
);
253 _eglSetConfigKey(&base
, EGL_NATIVE_RENDERABLE
, EGL_TRUE
);
254 _eglSetConfigKey(&base
, EGL_BIND_TO_TEXTURE_RGB
, bind_to_texture_rgb
);
255 if (_eglGetConfigKey(&base
, EGL_ALPHA_SIZE
) > 0)
256 _eglSetConfigKey(&base
,
257 EGL_BIND_TO_TEXTURE_RGBA
, bind_to_texture_rgba
);
259 _eglSetConfigKey(&base
, EGL_RENDERABLE_TYPE
, disp
->ClientAPIsMask
);
260 _eglSetConfigKey(&base
, EGL_CONFORMANT
, disp
->ClientAPIsMask
);
262 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
263 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
267 conf
= malloc(sizeof *conf
);
269 memcpy(&conf
->base
, &base
, sizeof base
);
270 conf
->dri_config
= dri_config
;
271 _eglAddConfig(disp
, &conf
->base
);
276 * Process list of buffer received from the server
278 * Processes the list of buffers received in a reply from the server to either
279 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
282 dri2_process_buffers(struct dri2_egl_surface
*dri2_surf
,
283 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
285 struct dri2_egl_display
*dri2_dpy
=
286 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
287 xcb_rectangle_t rectangle
;
290 dri2_surf
->buffer_count
= count
;
291 dri2_surf
->have_fake_front
= 0;
293 /* This assumes the DRI2 buffer attachment tokens matches the
294 * __DRIbuffer tokens. */
295 for (i
= 0; i
< count
; i
++) {
296 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
297 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
298 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
299 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
300 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
302 /* We only use the DRI drivers single buffer configs. This
303 * means that if we try to render to a window, DRI2 will give us
304 * the fake front buffer, which we'll use as a back buffer.
305 * Note that EGL doesn't require that several clients rendering
306 * to the same window must see the same aux buffers. */
307 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
308 dri2_surf
->have_fake_front
= 1;
311 if (dri2_surf
->region
!= XCB_NONE
)
312 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
316 rectangle
.width
= dri2_surf
->base
.Width
;
317 rectangle
.height
= dri2_surf
->base
.Height
;
318 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
319 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
323 dri2_get_buffers(__DRIdrawable
* driDrawable
,
324 int *width
, int *height
,
325 unsigned int *attachments
, int count
,
326 int *out_count
, void *loaderPrivate
)
328 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
329 struct dri2_egl_display
*dri2_dpy
=
330 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
331 xcb_dri2_dri2_buffer_t
*buffers
;
332 xcb_dri2_get_buffers_reply_t
*reply
;
333 xcb_dri2_get_buffers_cookie_t cookie
;
335 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
337 count
, count
, attachments
);
338 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
339 buffers
= xcb_dri2_get_buffers_buffers (reply
);
343 *out_count
= reply
->count
;
344 dri2_surf
->base
.Width
= *width
= reply
->width
;
345 dri2_surf
->base
.Height
= *height
= reply
->height
;
346 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
350 return dri2_surf
->buffers
;
354 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
356 /* FIXME: Does EGL support front buffer rendering at all? */
359 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
361 dri2WaitGL(dri2_surf
);
366 dri2_lookup_egl_image(__DRIcontext
*context
, void *image
, void *data
)
368 struct dri2_egl_context
*dri2_ctx
= data
;
369 _EGLDisplay
*disp
= dri2_ctx
->base
.Resource
.Display
;
370 struct dri2_egl_image
*dri2_img
;
373 img
= _eglLookupImage(image
, disp
);
375 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
379 dri2_img
= dri2_egl_image(image
);
381 return dri2_img
->dri_image
;
385 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
386 int *width
, int *height
,
387 unsigned int *attachments
, int count
,
388 int *out_count
, void *loaderPrivate
)
390 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
391 struct dri2_egl_display
*dri2_dpy
=
392 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
393 xcb_dri2_dri2_buffer_t
*buffers
;
394 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
395 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
396 xcb_dri2_attach_format_t
*format_attachments
;
398 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
399 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
404 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
409 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
410 dri2_surf
->base
.Width
= *width
= reply
->width
;
411 dri2_surf
->base
.Height
= *height
= reply
->height
;
412 *out_count
= reply
->count
;
413 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
417 return dri2_surf
->buffers
;
420 static const char dri_driver_path
[] = DEFAULT_DRIVER_DIR
;
422 struct dri2_extension_match
{
428 static struct dri2_extension_match dri2_driver_extensions
[] = {
429 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
430 { __DRI_DRI2
, 1, offsetof(struct dri2_egl_display
, dri2
) },
434 static struct dri2_extension_match dri2_core_extensions
[] = {
435 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
436 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
437 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
442 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
443 struct dri2_extension_match
*matches
,
444 const __DRIextension
**extensions
)
446 int i
, j
, ret
= EGL_TRUE
;
449 for (i
= 0; extensions
[i
]; i
++) {
450 _eglLog(_EGL_DEBUG
, "DRI2: found extension `%s'", extensions
[i
]->name
);
451 for (j
= 0; matches
[j
].name
; j
++) {
452 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
453 extensions
[i
]->version
>= matches
[j
].version
) {
454 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
455 *(const __DRIextension
**) field
= extensions
[i
];
456 _eglLog(_EGL_INFO
, "DRI2: found extension %s version %d",
457 extensions
[i
]->name
, extensions
[i
]->version
);
462 for (j
= 0; matches
[j
].name
; j
++) {
463 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
464 if (*(const __DRIextension
**) field
== NULL
) {
465 _eglLog(_EGL_FATAL
, "DRI2: did not find extension %s version %d",
466 matches
[j
].name
, matches
[j
].version
);
475 dri2_strndup(const char *s
, int length
)
479 d
= malloc(length
+ 1);
483 memcpy(d
, s
, length
);
490 dri2_connect(struct dri2_egl_display
*dri2_dpy
)
492 xcb_xfixes_query_version_reply_t
*xfixes_query
;
493 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
494 xcb_dri2_query_version_reply_t
*dri2_query
;
495 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
496 xcb_dri2_connect_reply_t
*connect
;
497 xcb_dri2_connect_cookie_t connect_cookie
;
498 xcb_generic_error_t
*error
;
499 xcb_screen_iterator_t s
;
501 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
502 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
504 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
505 XCB_XFIXES_MAJOR_VERSION
,
506 XCB_XFIXES_MINOR_VERSION
);
508 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
509 XCB_DRI2_MAJOR_VERSION
,
510 XCB_DRI2_MINOR_VERSION
);
512 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
513 connect_cookie
= xcb_dri2_connect_unchecked (dri2_dpy
->conn
,
515 XCB_DRI2_DRIVER_TYPE_DRI
);
518 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
519 xfixes_query_cookie
, &error
);
520 if (xfixes_query
== NULL
||
521 error
!= NULL
|| xfixes_query
->major_version
< 2) {
522 _eglLog(_EGL_FATAL
, "DRI2: failed to query xfixes version");
529 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
530 if (dri2_query
== NULL
|| error
!= NULL
) {
531 _eglLog(_EGL_FATAL
, "DRI2: failed to query version");
535 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
536 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
539 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
540 if (connect
== NULL
||
541 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
542 _eglLog(_EGL_FATAL
, "DRI2: failed to authenticate");
546 dri2_dpy
->device_name
=
547 dri2_strndup(xcb_dri2_connect_device_name (connect
),
548 xcb_dri2_connect_device_name_length (connect
));
550 dri2_dpy
->driver_name
=
551 dri2_strndup(xcb_dri2_connect_driver_name (connect
),
552 xcb_dri2_connect_driver_name_length (connect
));
554 if (dri2_dpy
->device_name
== NULL
|| dri2_dpy
->driver_name
== NULL
) {
555 free(dri2_dpy
->device_name
);
556 free(dri2_dpy
->driver_name
);
566 dri2_authenticate(struct dri2_egl_display
*dri2_dpy
)
568 xcb_dri2_authenticate_reply_t
*authenticate
;
569 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
570 xcb_screen_iterator_t s
;
573 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
574 _eglLog(_EGL_FATAL
, "DRI2: failed to get drm magic");
578 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
579 authenticate_cookie
=
580 xcb_dri2_authenticate_unchecked(dri2_dpy
->conn
, s
.data
->root
, magic
);
582 xcb_dri2_authenticate_reply(dri2_dpy
->conn
, authenticate_cookie
, NULL
);
583 if (authenticate
== NULL
|| !authenticate
->authenticated
) {
584 _eglLog(_EGL_FATAL
, "DRI2: failed to authenticate");
595 dri2_add_configs_for_visuals(struct dri2_egl_display
*dri2_dpy
,
598 xcb_screen_iterator_t s
;
599 xcb_depth_iterator_t d
;
600 xcb_visualtype_t
*visuals
;
603 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
604 d
= xcb_screen_allowed_depths_iterator(s
.data
);
607 EGLBoolean class_added
[6] = { 0, };
609 visuals
= xcb_depth_visuals(d
.data
);
610 for (i
= 0; i
< xcb_depth_visuals_length(d
.data
); i
++) {
611 if (class_added
[visuals
[i
]._class
])
614 class_added
[visuals
[i
]._class
] = EGL_TRUE
;
615 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++)
616 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
],
617 id
++, d
.data
->depth
, &visuals
[i
]);
623 if (!disp
->NumConfigs
) {
624 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
632 * Called via eglInitialize(), GLX_drv->API.Initialize().
635 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
,
636 EGLint
*major
, EGLint
*minor
)
638 const __DRIextension
**extensions
;
639 struct dri2_egl_display
*dri2_dpy
;
640 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
641 unsigned int api_mask
;
643 dri2_dpy
= malloc(sizeof *dri2_dpy
);
645 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
647 disp
->DriverData
= (void *) dri2_dpy
;
648 if (disp
->NativeDisplay
== NULL
) {
649 dri2_dpy
->conn
= xcb_connect(0, 0);
650 if (!dri2_dpy
->conn
) {
651 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
655 dri2_dpy
->conn
= XGetXCBConnection(disp
->NativeDisplay
);
658 if (dri2_dpy
->conn
== NULL
)
661 if (dri2_dpy
->conn
) {
662 if (!dri2_connect(dri2_dpy
))
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());
712 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
))
715 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
716 if (dri2_dpy
->fd
== -1) {
717 _eglLog(_EGL_WARNING
,
718 "DRI2: could not open %s (%s)", dri2_dpy
->device_name
,
723 if (dri2_dpy
->conn
) {
724 if (!dri2_authenticate(dri2_dpy
))
728 if (dri2_dpy
->dri2_minor
>= 1) {
729 dri2_dpy
->loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
730 dri2_dpy
->loader_extension
.base
.version
= 3;
731 dri2_dpy
->loader_extension
.getBuffers
= dri2_get_buffers
;
732 dri2_dpy
->loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
733 dri2_dpy
->loader_extension
.getBuffersWithFormat
=
734 dri2_get_buffers_with_format
;
736 dri2_dpy
->loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
737 dri2_dpy
->loader_extension
.base
.version
= 2;
738 dri2_dpy
->loader_extension
.getBuffers
= dri2_get_buffers
;
739 dri2_dpy
->loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
740 dri2_dpy
->loader_extension
.getBuffersWithFormat
= NULL
;
743 dri2_dpy
->image_lookup_extension
.base
.name
= __DRI_IMAGE_LOOKUP
;
744 dri2_dpy
->image_lookup_extension
.base
.version
= 1;
745 dri2_dpy
->image_lookup_extension
.lookupEGLImage
= dri2_lookup_egl_image
;
747 dri2_dpy
->extensions
[0] = &dri2_dpy
->loader_extension
.base
;
748 dri2_dpy
->extensions
[1] = &dri2_dpy
->image_lookup_extension
.base
;
749 dri2_dpy
->extensions
[2] = NULL
;
751 dri2_dpy
->dri_screen
=
752 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
, dri2_dpy
->extensions
,
753 &dri2_dpy
->driver_configs
, dri2_dpy
);
755 if (dri2_dpy
->dri_screen
== NULL
) {
756 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
760 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
761 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
))
762 goto cleanup_dri_screen
;
764 if (dri2_dpy
->dri2
->base
.version
>= 2)
765 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
767 api_mask
= __DRI_API_OPENGL
;
769 disp
->ClientAPIsMask
= 0;
770 if (api_mask
& (1 <<__DRI_API_OPENGL
))
771 disp
->ClientAPIsMask
|= EGL_OPENGL_BIT
;
772 if (api_mask
& (1 <<__DRI_API_GLES
))
773 disp
->ClientAPIsMask
|= EGL_OPENGL_ES_BIT
;
774 if (api_mask
& (1 << __DRI_API_GLES2
))
775 disp
->ClientAPIsMask
|= EGL_OPENGL_ES2_BIT
;
777 if (dri2_dpy
->conn
) {
778 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
779 goto cleanup_configs
;
782 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
783 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
784 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
785 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
786 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
787 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
789 /* we're supporting EGL 1.4 */
796 _eglCleanupDisplay(disp
);
798 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
802 dlclose(dri2_dpy
->driver
);
804 if (disp
->NativeDisplay
== NULL
)
805 xcb_disconnect(dri2_dpy
->conn
);
813 * Called via eglTerminate(), drv->API.Terminate().
816 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
818 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
820 _eglReleaseDisplayResources(drv
, disp
);
821 _eglCleanupDisplay(disp
);
823 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
825 dlclose(dri2_dpy
->driver
);
826 if (disp
->NativeDisplay
== NULL
)
827 xcb_disconnect(dri2_dpy
->conn
);
829 disp
->DriverData
= NULL
;
836 * Called via eglCreateContext(), drv->API.CreateContext().
839 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
840 _EGLContext
*share_list
, const EGLint
*attrib_list
)
842 struct dri2_egl_context
*dri2_ctx
;
843 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
844 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
845 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
848 dri2_ctx
= malloc(sizeof *dri2_ctx
);
850 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
854 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
857 switch (dri2_ctx
->base
.ClientAPI
) {
858 case EGL_OPENGL_ES_API
:
859 switch (dri2_ctx
->base
.ClientVersion
) {
861 api
= __DRI_API_GLES
;
864 api
= __DRI_API_GLES2
;
867 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
872 api
= __DRI_API_OPENGL
;
875 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
879 if (dri2_dpy
->dri2
->base
.version
>= 2) {
880 dri2_ctx
->dri_context
=
881 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
883 dri2_config
->dri_config
,
885 dri2_ctx_shared
->dri_context
: NULL
,
887 } else if (api
== __DRI_API_OPENGL
) {
888 dri2_ctx
->dri_context
=
889 dri2_dpy
->dri2
->createNewContext(dri2_dpy
->dri_screen
,
890 dri2_config
->dri_config
,
892 dri2_ctx_shared
->dri_context
: NULL
,
898 if (!dri2_ctx
->dri_context
)
901 return &dri2_ctx
->base
;
909 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
911 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
912 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
914 if (_eglIsSurfaceBound(surf
))
917 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
919 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
921 if (surf
->Type
== EGL_PBUFFER_BIT
)
922 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
930 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
933 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
934 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
936 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
937 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
938 struct dri2_egl_surface
*dri2_dsurf
= dri2_egl_surface(dsurf
);
939 struct dri2_egl_surface
*dri2_rsurf
= dri2_egl_surface(rsurf
);
940 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
941 __DRIdrawable
*ddraw
, *rdraw
;
944 /* bind the new context and return the "orphaned" one */
945 if (!_eglBindContext(&ctx
, &dsurf
, &rsurf
))
948 /* flush before context switch */
949 if (ctx
&& dri2_drv
->glFlush
)
952 ddraw
= (dri2_dsurf
) ? dri2_dsurf
->dri_drawable
: NULL
;
953 rdraw
= (dri2_rsurf
) ? dri2_rsurf
->dri_drawable
: NULL
;
954 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
956 if ((cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
) ||
957 dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
958 if (dsurf
&& !_eglIsSurfaceLinked(dsurf
))
959 dri2_destroy_surface(drv
, disp
, dsurf
);
960 if (rsurf
&& rsurf
!= dsurf
&& !_eglIsSurfaceLinked(dsurf
))
961 dri2_destroy_surface(drv
, disp
, rsurf
);
962 if (ctx
!= NULL
&& !_eglIsContextLinked(ctx
))
963 dri2_dpy
->core
->unbindContext(dri2_egl_context(ctx
)->dri_context
);
967 _eglBindContext(&ctx
, &dsurf
, &rsurf
);
974 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
977 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
978 _EGLConfig
*conf
, EGLNativeWindowType window
,
979 const EGLint
*attrib_list
)
981 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
982 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
983 struct dri2_egl_surface
*dri2_surf
;
984 xcb_get_geometry_cookie_t cookie
;
985 xcb_get_geometry_reply_t
*reply
;
986 xcb_screen_iterator_t s
;
987 xcb_generic_error_t
*error
;
989 dri2_surf
= malloc(sizeof *dri2_surf
);
991 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
995 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
998 dri2_surf
->region
= XCB_NONE
;
999 if (type
== EGL_PBUFFER_BIT
) {
1000 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
1001 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
1002 xcb_create_pixmap(dri2_dpy
->conn
,
1003 _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
),
1004 dri2_surf
->drawable
, s
.data
->root
,
1005 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
1007 dri2_surf
->drawable
= window
;
1010 dri2_surf
->dri_drawable
=
1011 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
1012 dri2_conf
->dri_config
, dri2_surf
);
1013 if (dri2_surf
->dri_drawable
== NULL
) {
1014 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
1015 goto cleanup_pixmap
;
1018 xcb_dri2_create_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
1020 if (type
!= EGL_PBUFFER_BIT
) {
1021 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
1022 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
1023 if (reply
== NULL
|| error
!= NULL
) {
1024 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
1026 goto cleanup_dri_drawable
;
1029 dri2_surf
->base
.Width
= reply
->width
;
1030 dri2_surf
->base
.Height
= reply
->height
;
1034 return &dri2_surf
->base
;
1036 cleanup_dri_drawable
:
1037 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
1039 if (type
== EGL_PBUFFER_BIT
)
1040 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
1048 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1050 static _EGLSurface
*
1051 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1052 _EGLConfig
*conf
, EGLNativeWindowType window
,
1053 const EGLint
*attrib_list
)
1055 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
1056 window
, attrib_list
);
1059 static _EGLSurface
*
1060 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1061 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
1062 const EGLint
*attrib_list
)
1064 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
1065 pixmap
, attrib_list
);
1068 static _EGLSurface
*
1069 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1070 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1072 return dri2_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
1073 XCB_WINDOW_NONE
, attrib_list
);
1077 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1078 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
1080 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1081 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1082 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1084 xcb_dri2_copy_region_cookie_t cookie
;
1086 if (dri2_drv
->glFlush
) {
1087 ctx
= _eglGetCurrentContext();
1088 if (ctx
&& ctx
->DrawSurface
== &dri2_surf
->base
)
1089 dri2_drv
->glFlush();
1092 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1095 /* FIXME: Add support for dri swapbuffers, that'll give us swap
1096 * interval and page flipping (at least for fullscreen windows) as
1097 * well as the page flip event. Unless surface->SwapBehavior is
1098 * EGL_BUFFER_PRESERVED. */
1099 #if __DRI2_FLUSH_VERSION >= 2
1101 (*pdraw
->psc
->f
->flushInvalidate
)(pdraw
->driDrawable
);
1105 if (!dri2_surf
->have_fake_front
)
1108 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
1109 dri2_surf
->drawable
,
1111 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
1112 XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
);
1113 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
1119 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1121 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1123 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
1127 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
1128 EGLint numRects
, const EGLint
*rects
)
1130 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1131 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1133 xcb_xfixes_region_t region
;
1134 xcb_rectangle_t rectangles
[16];
1137 if (numRects
> ARRAY_SIZE(rectangles
))
1138 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
1140 /* FIXME: Invert y here? */
1141 for (i
= 0; i
< numRects
; i
++) {
1142 rectangles
[i
].x
= rects
[i
* 4];
1143 rectangles
[i
].y
= rects
[i
* 4 + 1];
1144 rectangles
[i
].width
= rects
[i
* 4 + 2];
1145 rectangles
[i
].height
= rects
[i
* 4 + 3];
1148 region
= xcb_generate_id(dri2_dpy
->conn
);
1149 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
1150 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
1151 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
1157 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1160 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1162 /* FIXME: Do we need to support lookup of EGL symbols too? */
1164 return (_EGLProc
) _glapi_get_proc_address(procname
);
1168 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1170 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1171 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(ctx
->DrawSurface
);
1173 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1174 * we need to copy fake to real here.*/
1176 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1182 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1184 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1185 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1192 dri2_unload(_EGLDriver
*drv
)
1194 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1199 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1200 EGLNativePixmapType target
)
1202 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1203 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1206 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
1208 gc
= xcb_generate_id(dri2_dpy
->conn
);
1209 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
1210 xcb_copy_area(dri2_dpy
->conn
,
1211 dri2_surf
->drawable
,
1216 dri2_surf
->base
.Width
,
1217 dri2_surf
->base
.Height
);
1218 xcb_free_gc(dri2_dpy
->conn
, gc
);
1224 dri2_bind_tex_image(_EGLDriver
*drv
,
1225 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1227 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1228 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1229 struct dri2_egl_context
*dri2_ctx
;
1231 GLint format
, target
;
1233 ctx
= _eglGetCurrentContext();
1234 dri2_ctx
= dri2_egl_context(ctx
);
1236 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1239 switch (dri2_surf
->base
.TextureFormat
) {
1240 case EGL_TEXTURE_RGB
:
1241 format
= __DRI_TEXTURE_FORMAT_RGB
;
1243 case EGL_TEXTURE_RGBA
:
1244 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1250 switch (dri2_surf
->base
.TextureTarget
) {
1251 case EGL_TEXTURE_2D
:
1252 target
= GL_TEXTURE_2D
;
1258 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1260 dri2_surf
->dri_drawable
);
1266 dri2_release_tex_image(_EGLDriver
*drv
,
1267 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1273 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1274 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1276 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1277 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1278 struct dri2_egl_image
*dri2_img
;
1279 unsigned int attachments
[1];
1280 xcb_drawable_t drawable
;
1281 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
1282 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
1283 xcb_dri2_dri2_buffer_t
*buffers
;
1284 xcb_get_geometry_cookie_t geometry_cookie
;
1285 xcb_get_geometry_reply_t
*geometry_reply
;
1286 xcb_generic_error_t
*error
;
1289 drawable
= (xcb_drawable_t
) buffer
;
1290 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
1291 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
1293 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
1294 drawable
, 1, 1, attachments
);
1295 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
1296 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
1297 buffers_cookie
, NULL
);
1298 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
1299 if (buffers
== NULL
) {
1303 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
1304 geometry_cookie
, &error
);
1305 if (geometry_reply
== NULL
|| error
!= NULL
) {
1306 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
1308 free(buffers_reply
);
1311 switch (geometry_reply
->depth
) {
1313 format
= __DRI_IMAGE_FORMAT_RGB565
;
1316 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
1319 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1322 _eglError(EGL_BAD_PARAMETER
,
1323 "dri2_create_image_khr: unsupported pixmap depth");
1324 free(buffers_reply
);
1325 free(geometry_reply
);
1329 dri2_img
= malloc(sizeof *dri2_img
);
1331 free(buffers_reply
);
1332 free(geometry_reply
);
1333 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1334 return EGL_NO_IMAGE_KHR
;
1337 if (!_eglInitImage(&dri2_img
->base
, disp
, attr_list
)) {
1338 free(buffers_reply
);
1339 free(geometry_reply
);
1340 return EGL_NO_IMAGE_KHR
;
1343 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
1344 dri2_img
->dri_image
=
1345 dri2_dpy
->image
->createImageFromName(dri2_ctx
->dri_context
,
1346 buffers_reply
->width
,
1347 buffers_reply
->height
,
1353 free(buffers_reply
);
1354 free(geometry_reply
);
1356 return &dri2_img
->base
;
1360 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1361 EGLClientBuffer buffer
,
1362 const EGLint
*attr_list
)
1364 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1365 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1366 struct dri2_egl_image
*dri2_img
;
1367 GLuint renderbuffer
= (GLuint
) buffer
;
1369 if (renderbuffer
== 0) {
1370 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1371 return EGL_NO_IMAGE_KHR
;
1374 dri2_img
= malloc(sizeof *dri2_img
);
1376 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1377 return EGL_NO_IMAGE_KHR
;
1380 if (!_eglInitImage(&dri2_img
->base
, disp
, attr_list
))
1381 return EGL_NO_IMAGE_KHR
;
1383 dri2_img
->dri_image
=
1384 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1388 return &dri2_img
->base
;
1392 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1393 _EGLContext
*ctx
, EGLenum target
,
1394 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1397 case EGL_NATIVE_PIXMAP_KHR
:
1398 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
1399 case EGL_GL_RENDERBUFFER_KHR
:
1400 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
1402 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1403 return EGL_NO_IMAGE_KHR
;
1408 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
1410 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1411 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
1413 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
1420 * This is the main entrypoint into the driver, called by libEGL.
1421 * Create a new _EGLDriver object and init its dispatch table.
1424 _eglMain(const char *args
)
1426 struct dri2_egl_driver
*dri2_drv
;
1428 dri2_drv
= malloc(sizeof *dri2_drv
);
1432 memset(dri2_drv
, 0, sizeof *dri2_drv
);
1433 _eglInitDriverFallbacks(&dri2_drv
->base
);
1434 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
1435 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
1436 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
1437 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
1438 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
1439 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
1440 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
1441 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
1442 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
1443 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
1444 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
1445 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
1446 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
;
1447 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
1448 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
1449 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image_khr
;
1450 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
1451 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
1453 dri2_drv
->base
.Name
= "DRI2";
1454 dri2_drv
->base
.Unload
= dri2_unload
;
1457 (void (*)(void)) dri2_get_proc_address(&dri2_drv
->base
, "glFlush");
1459 return &dri2_drv
->base
;