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>
28 #define WL_HIDE_DEPRECATED
41 #include <drm_fourcc.h>
44 #include <GL/internal/dri_interface.h>
45 #include <sys/types.h>
48 #ifdef HAVE_WAYLAND_PLATFORM
49 #include "wayland-drm.h"
50 #include "wayland-drm-client-protocol.h"
54 #include "../util/u_atomic.h"
56 const __DRIuseInvalidateExtension use_invalidate
= {
57 .base
= { __DRI_USE_INVALIDATE
, 1 }
60 EGLint dri2_to_egl_attribute_map
[] = {
62 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
63 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
64 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
65 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
66 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
67 EGL_LUMINANCE_SIZE
, /* __DRI_ATTRIB_LUMINANCE_SIZE */
68 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
69 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
70 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
71 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
72 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
73 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
74 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
75 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
76 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
77 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
78 0, /* __DRI_ATTRIB_RENDER_TYPE, */
79 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
80 0, /* __DRI_ATTRIB_CONFORMANT */
81 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
82 0, /* __DRI_ATTRIB_STEREO */
83 0, /* __DRI_ATTRIB_AUX_BUFFERS */
84 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
85 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
86 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
87 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
88 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
89 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
90 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */
91 0, /* __DRI_ATTRIB_RED_MASK */
92 0, /* __DRI_ATTRIB_GREEN_MASK */
93 0, /* __DRI_ATTRIB_BLUE_MASK */
94 0, /* __DRI_ATTRIB_ALPHA_MASK */
95 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
96 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
97 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
98 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
99 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
100 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
101 0, /* __DRI_ATTRIB_SWAP_METHOD */
102 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
103 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
104 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
105 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
106 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
107 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
108 EGL_Y_INVERTED_NOK
, /* __DRI_ATTRIB_YINVERTED */
109 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
113 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
115 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
118 if (!_eglMatchConfig(conf
, criteria
))
124 struct dri2_egl_config
*
125 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
126 EGLint surface_type
, const EGLint
*attr_list
,
127 const unsigned int *rgba_masks
)
129 struct dri2_egl_config
*conf
;
130 struct dri2_egl_display
*dri2_dpy
;
132 unsigned int attrib
, value
, double_buffer
;
133 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
134 unsigned int dri_masks
[4] = { 0, 0, 0, 0 };
135 _EGLConfig
*matching_config
;
136 EGLint num_configs
= 0;
140 dri2_dpy
= disp
->DriverData
;
141 _eglInitConfig(&base
, disp
, id
);
145 bind_to_texture_rgb
= 0;
146 bind_to_texture_rgba
= 0;
148 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
150 case __DRI_ATTRIB_RENDER_TYPE
:
151 if (value
& __DRI_ATTRIB_RGBA_BIT
)
152 value
= EGL_RGB_BUFFER
;
153 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
154 value
= EGL_LUMINANCE_BUFFER
;
157 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
160 case __DRI_ATTRIB_CONFIG_CAVEAT
:
161 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
162 value
= EGL_NON_CONFORMANT_CONFIG
;
163 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
164 value
= EGL_SLOW_CONFIG
;
167 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
170 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
171 bind_to_texture_rgb
= value
;
174 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
175 bind_to_texture_rgba
= value
;
178 case __DRI_ATTRIB_DOUBLE_BUFFER
:
179 double_buffer
= value
;
182 case __DRI_ATTRIB_RED_MASK
:
183 dri_masks
[0] = value
;
186 case __DRI_ATTRIB_GREEN_MASK
:
187 dri_masks
[1] = value
;
190 case __DRI_ATTRIB_BLUE_MASK
:
191 dri_masks
[2] = value
;
194 case __DRI_ATTRIB_ALPHA_MASK
:
195 dri_masks
[3] = value
;
198 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
199 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
200 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
201 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
202 /* Don't expose visuals with the accumulation buffer. */
208 key
= dri2_to_egl_attribute_map
[attrib
];
210 _eglSetConfigKey(&base
, key
, value
);
216 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
217 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
219 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
222 base
.NativeRenderable
= EGL_TRUE
;
224 base
.SurfaceType
= surface_type
;
225 if (surface_type
& (EGL_PBUFFER_BIT
|
226 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
227 base
.BindToTextureRGB
= bind_to_texture_rgb
;
228 if (base
.AlphaSize
> 0)
229 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
232 base
.RenderableType
= disp
->ClientAPIs
;
233 base
.Conformant
= disp
->ClientAPIs
;
235 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
236 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
238 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
239 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
243 config_id
= base
.ConfigID
;
244 base
.ConfigID
= EGL_DONT_CARE
;
245 base
.SurfaceType
= EGL_DONT_CARE
;
246 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
247 (_EGLArrayForEach
) dri2_match_config
, &base
);
249 if (num_configs
== 1) {
250 conf
= (struct dri2_egl_config
*) matching_config
;
252 if (double_buffer
&& !conf
->dri_double_config
)
253 conf
->dri_double_config
= dri_config
;
254 else if (!double_buffer
&& !conf
->dri_single_config
)
255 conf
->dri_single_config
= dri_config
;
257 /* a similar config type is already added (unlikely) => discard */
260 else if (num_configs
== 0) {
261 conf
= malloc(sizeof *conf
);
265 memcpy(&conf
->base
, &base
, sizeof base
);
267 conf
->dri_double_config
= dri_config
;
268 conf
->dri_single_config
= NULL
;
270 conf
->dri_single_config
= dri_config
;
271 conf
->dri_double_config
= NULL
;
273 conf
->base
.SurfaceType
= 0;
274 conf
->base
.ConfigID
= config_id
;
276 _eglLinkConfig(&conf
->base
);
284 surface_type
&= ~EGL_PIXMAP_BIT
;
287 conf
->base
.SurfaceType
|= surface_type
;
293 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
295 _EGLDisplay
*disp
= data
;
296 struct dri2_egl_image
*dri2_img
;
301 img
= _eglLookupImage(image
, disp
);
303 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
307 dri2_img
= dri2_egl_image(image
);
309 return dri2_img
->dri_image
;
312 const __DRIimageLookupExtension image_lookup_extension
= {
313 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
315 .lookupEGLImage
= dri2_lookup_egl_image
318 struct dri2_extension_match
{
324 static struct dri2_extension_match dri2_driver_extensions
[] = {
325 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
326 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
330 static struct dri2_extension_match dri2_core_extensions
[] = {
331 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
332 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
333 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
337 static struct dri2_extension_match swrast_driver_extensions
[] = {
338 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
339 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
343 static struct dri2_extension_match swrast_core_extensions
[] = {
344 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
349 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
350 struct dri2_extension_match
*matches
,
351 const __DRIextension
**extensions
)
353 int i
, j
, ret
= EGL_TRUE
;
356 for (i
= 0; extensions
[i
]; i
++) {
357 _eglLog(_EGL_DEBUG
, "DRI2: found extension `%s'", extensions
[i
]->name
);
358 for (j
= 0; matches
[j
].name
; j
++) {
359 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
360 extensions
[i
]->version
>= matches
[j
].version
) {
361 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
362 *(const __DRIextension
**) field
= extensions
[i
];
363 _eglLog(_EGL_INFO
, "DRI2: found extension %s version %d",
364 extensions
[i
]->name
, extensions
[i
]->version
);
369 for (j
= 0; matches
[j
].name
; j
++) {
370 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
371 if (*(const __DRIextension
**) field
== NULL
) {
372 _eglLog(_EGL_WARNING
, "DRI2: did not find extension %s version %d",
373 matches
[j
].name
, matches
[j
].version
);
381 static const __DRIextension
**
382 dri2_open_driver(_EGLDisplay
*disp
)
384 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
385 const __DRIextension
**extensions
= NULL
;
386 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
387 char *get_extensions_name
;
388 const __DRIextension
**(*get_extensions
)(void);
391 if (geteuid() == getuid()) {
392 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
393 search_paths
= getenv("LIBGL_DRIVERS_PATH");
395 if (search_paths
== NULL
)
396 search_paths
= DEFAULT_DRIVER_DIR
;
398 dri2_dpy
->driver
= NULL
;
399 end
= search_paths
+ strlen(search_paths
);
400 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
402 next
= strchr(p
, ':');
408 snprintf(path
, sizeof path
,
409 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
410 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
412 if (dri2_dpy
->driver
== NULL
) {
413 snprintf(path
, sizeof path
,
414 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
415 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
416 if (dri2_dpy
->driver
== NULL
)
417 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
419 /* not need continue to loop all paths once the driver is found */
420 if (dri2_dpy
->driver
!= NULL
)
424 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
425 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
426 if (dri2_dpy
->driver
== NULL
)
427 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
433 if (dri2_dpy
->driver
== NULL
) {
434 _eglLog(_EGL_WARNING
,
435 "DRI2: failed to open %s (search paths %s)",
436 dri2_dpy
->driver_name
, search_paths
);
440 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
442 if (asprintf(&get_extensions_name
, "%s_%s",
443 __DRI_DRIVER_GET_EXTENSIONS
, dri2_dpy
->driver_name
) != -1) {
444 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
445 if (get_extensions
) {
446 extensions
= get_extensions();
448 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
449 get_extensions_name
, dlerror());
451 free(get_extensions_name
);
455 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
456 if (extensions
== NULL
) {
457 _eglLog(_EGL_WARNING
,
458 "DRI2: driver exports no extensions (%s)", dlerror());
459 dlclose(dri2_dpy
->driver
);
466 dri2_load_driver(_EGLDisplay
*disp
)
468 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
469 const __DRIextension
**extensions
;
471 extensions
= dri2_open_driver(disp
);
475 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
)) {
476 dlclose(dri2_dpy
->driver
);
479 dri2_dpy
->driver_extensions
= extensions
;
485 dri2_load_driver_swrast(_EGLDisplay
*disp
)
487 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
488 const __DRIextension
**extensions
;
490 extensions
= dri2_open_driver(disp
);
494 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
)) {
495 dlclose(dri2_dpy
->driver
);
498 dri2_dpy
->driver_extensions
= extensions
;
504 dri2_setup_screen(_EGLDisplay
*disp
)
506 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
507 unsigned int api_mask
;
509 if (dri2_dpy
->dri2
) {
510 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
512 assert(dri2_dpy
->swrast
);
513 api_mask
= 1 << __DRI_API_OPENGL
|
514 1 << __DRI_API_GLES
|
515 1 << __DRI_API_GLES2
|
516 1 << __DRI_API_GLES3
;
519 disp
->ClientAPIs
= 0;
520 if (api_mask
& (1 <<__DRI_API_OPENGL
))
521 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
522 if (api_mask
& (1 <<__DRI_API_GLES
))
523 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
524 if (api_mask
& (1 << __DRI_API_GLES2
))
525 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
526 if (api_mask
& (1 << __DRI_API_GLES3
))
527 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
529 assert(dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
530 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
531 disp
->Extensions
.MESA_configless_context
= EGL_TRUE
;
533 if (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) {
534 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
536 if (dri2_dpy
->robustness
)
537 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
540 if (dri2_dpy
->fence
) {
541 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
542 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
543 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
544 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
547 if (dri2_dpy
->image
) {
548 if (dri2_dpy
->image
->base
.version
>= 10 &&
549 dri2_dpy
->image
->getCapabilities
!= NULL
) {
552 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
553 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
555 if (dri2_dpy
->image
->base
.version
>= 11)
556 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
558 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
559 if (dri2_dpy
->image
->base
.version
>= 11)
560 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
563 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
564 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
565 if (dri2_dpy
->image
->base
.version
>= 5 &&
566 dri2_dpy
->image
->createImageFromTexture
) {
567 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
568 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
571 if (dri2_dpy
->image
->base
.version
>= 8 &&
572 dri2_dpy
->image
->createImageFromDmaBufs
) {
573 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
579 /* All platforms but DRM call this function to create the screen, query the
580 * dri extensions, setup the vtables and populate the driver_configs.
581 * DRM inherits all that information from its display - GBM.
584 dri2_create_screen(_EGLDisplay
*disp
)
586 const __DRIextension
**extensions
;
587 struct dri2_egl_display
*dri2_dpy
;
590 dri2_dpy
= disp
->DriverData
;
592 if (dri2_dpy
->dri2
) {
593 if (dri2_dpy
->dri2
->base
.version
>= 4) {
594 dri2_dpy
->dri_screen
=
595 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
596 dri2_dpy
->extensions
,
597 dri2_dpy
->driver_extensions
,
598 &dri2_dpy
->driver_configs
, disp
);
600 dri2_dpy
->dri_screen
=
601 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
602 dri2_dpy
->extensions
,
603 &dri2_dpy
->driver_configs
, disp
);
606 assert(dri2_dpy
->swrast
);
607 if (dri2_dpy
->swrast
->base
.version
>= 4) {
608 dri2_dpy
->dri_screen
=
609 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->extensions
,
610 dri2_dpy
->driver_extensions
,
611 &dri2_dpy
->driver_configs
, disp
);
613 dri2_dpy
->dri_screen
=
614 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->extensions
,
615 &dri2_dpy
->driver_configs
, disp
);
619 if (dri2_dpy
->dri_screen
== NULL
) {
620 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
624 dri2_dpy
->own_dri_screen
= 1;
626 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
628 if (dri2_dpy
->dri2
) {
629 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
))
630 goto cleanup_dri_screen
;
632 assert(dri2_dpy
->swrast
);
633 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
))
634 goto cleanup_dri_screen
;
637 for (i
= 0; extensions
[i
]; i
++) {
638 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0) {
639 dri2_dpy
->robustness
= (__DRIrobustnessExtension
*) extensions
[i
];
641 if (strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0) {
642 dri2_dpy
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
644 if (strcmp(extensions
[i
]->name
, __DRI2_FENCE
) == 0) {
645 dri2_dpy
->fence
= (__DRI2fenceExtension
*) extensions
[i
];
649 dri2_setup_screen(disp
);
654 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
660 * Called via eglInitialize(), GLX_drv->API.Initialize().
663 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
665 /* not until swrast_dri is supported */
666 if (disp
->Options
.UseFallback
)
669 switch (disp
->Platform
) {
670 #ifdef HAVE_SURFACELESS_PLATFORM
671 case _EGL_PLATFORM_SURFACELESS
:
672 if (disp
->Options
.TestOnly
)
674 return dri2_initialize_surfaceless(drv
, disp
);
677 #ifdef HAVE_X11_PLATFORM
678 case _EGL_PLATFORM_X11
:
679 if (disp
->Options
.TestOnly
)
681 return dri2_initialize_x11(drv
, disp
);
684 #ifdef HAVE_DRM_PLATFORM
685 case _EGL_PLATFORM_DRM
:
686 if (disp
->Options
.TestOnly
)
688 return dri2_initialize_drm(drv
, disp
);
690 #ifdef HAVE_WAYLAND_PLATFORM
691 case _EGL_PLATFORM_WAYLAND
:
692 if (disp
->Options
.TestOnly
)
694 return dri2_initialize_wayland(drv
, disp
);
696 #ifdef HAVE_ANDROID_PLATFORM
697 case _EGL_PLATFORM_ANDROID
:
698 if (disp
->Options
.TestOnly
)
700 return dri2_initialize_android(drv
, disp
);
704 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
710 * Called via eglTerminate(), drv->API.Terminate().
713 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
715 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
718 _eglReleaseDisplayResources(drv
, disp
);
719 _eglCleanupDisplay(disp
);
721 if (dri2_dpy
->own_dri_screen
)
722 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
725 if (dri2_dpy
->driver
)
726 dlclose(dri2_dpy
->driver
);
727 free(dri2_dpy
->device_name
);
728 free(dri2_dpy
->driver_name
);
730 switch (disp
->Platform
) {
731 #ifdef HAVE_X11_PLATFORM
732 case _EGL_PLATFORM_X11
:
733 if (dri2_dpy
->own_device
) {
734 xcb_disconnect(dri2_dpy
->conn
);
738 #ifdef HAVE_DRM_PLATFORM
739 case _EGL_PLATFORM_DRM
:
740 if (dri2_dpy
->own_device
) {
741 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
745 #ifdef HAVE_WAYLAND_PLATFORM
746 case _EGL_PLATFORM_WAYLAND
:
747 if (dri2_dpy
->wl_drm
)
748 wl_drm_destroy(dri2_dpy
->wl_drm
);
749 if (dri2_dpy
->wl_shm
)
750 wl_shm_destroy(dri2_dpy
->wl_shm
);
751 wl_registry_destroy(dri2_dpy
->wl_registry
);
752 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
753 if (dri2_dpy
->own_device
) {
754 wl_display_disconnect(dri2_dpy
->wl_dpy
);
762 /* The drm platform does not create the screen/driver_configs but reuses
763 * the ones from the gbm device. As such the gbm itself is responsible
766 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
767 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
768 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
769 free(dri2_dpy
->driver_configs
);
772 disp
->DriverData
= NULL
;
778 * Set the error code after a call to
779 * dri2_egl_display::dri2::createContextAttribs.
782 dri2_create_context_attribs_error(int dri_error
)
787 case __DRI_CTX_ERROR_SUCCESS
:
790 case __DRI_CTX_ERROR_NO_MEMORY
:
791 egl_error
= EGL_BAD_ALLOC
;
794 /* From the EGL_KHR_create_context spec, section "Errors":
796 * * If <config> does not support a client API context compatible
797 * with the requested API major and minor version, [...] context flags,
798 * and context reset notification behavior (for client API types where
799 * these attributes are supported), then an EGL_BAD_MATCH error is
802 * * If an OpenGL ES context is requested and the values for
803 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
804 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
805 * is not defined, than an EGL_BAD_MATCH error is generated.
807 * * If an OpenGL context is requested, the requested version is
808 * greater than 3.2, and the value for attribute
809 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
810 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
811 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
812 * one of these bits set; or if the implementation does not support
813 * the requested profile, then an EGL_BAD_MATCH error is generated.
815 case __DRI_CTX_ERROR_BAD_API
:
816 case __DRI_CTX_ERROR_BAD_VERSION
:
817 case __DRI_CTX_ERROR_BAD_FLAG
:
818 egl_error
= EGL_BAD_MATCH
;
821 /* From the EGL_KHR_create_context spec, section "Errors":
823 * * If an attribute name or attribute value in <attrib_list> is not
824 * recognized (including unrecognized bits in bitmask attributes),
825 * then an EGL_BAD_ATTRIBUTE error is generated."
827 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
828 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
829 egl_error
= EGL_BAD_ATTRIBUTE
;
834 egl_error
= EGL_BAD_MATCH
;
838 _eglError(egl_error
, "dri2_create_context");
842 * Called via eglCreateContext(), drv->API.CreateContext().
845 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
846 _EGLContext
*share_list
, const EGLint
*attrib_list
)
848 struct dri2_egl_context
*dri2_ctx
;
849 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
850 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
851 __DRIcontext
*shared
=
852 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
853 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
854 const __DRIconfig
*dri_config
;
859 dri2_ctx
= malloc(sizeof *dri2_ctx
);
861 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
865 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
868 switch (dri2_ctx
->base
.ClientAPI
) {
869 case EGL_OPENGL_ES_API
:
870 switch (dri2_ctx
->base
.ClientMajorVersion
) {
872 api
= __DRI_API_GLES
;
875 api
= __DRI_API_GLES2
;
878 api
= __DRI_API_GLES3
;
881 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
887 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
888 || (dri2_ctx
->base
.ClientMajorVersion
== 3
889 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
890 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
891 api
= __DRI_API_OPENGL_CORE
;
893 api
= __DRI_API_OPENGL
;
896 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
902 /* The config chosen here isn't necessarily
903 * used for surfaces later.
904 * A pixmap surface will use the single config.
905 * This opportunity depends on disabling the
906 * doubleBufferMode check in
907 * src/mesa/main/context.c:check_compatible()
909 if (dri2_config
->dri_double_config
)
910 dri_config
= dri2_config
->dri_double_config
;
912 dri_config
= dri2_config
->dri_single_config
;
914 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
915 * makes sure the back buffer will always be used.
917 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
918 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
923 if (dri2_dpy
->dri2
) {
924 if (dri2_dpy
->dri2
->base
.version
>= 3) {
926 unsigned num_attribs
= 0;
927 uint32_t ctx_attribs
[8];
929 ctx_attribs
[num_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
930 ctx_attribs
[num_attribs
++] = dri2_ctx
->base
.ClientMajorVersion
;
931 ctx_attribs
[num_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
932 ctx_attribs
[num_attribs
++] = dri2_ctx
->base
.ClientMinorVersion
;
934 if (dri2_ctx
->base
.Flags
!= 0) {
935 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
936 * extension, don't even try to send it the robust-access flag.
937 * It may explode. Instead, generate the required EGL error here.
939 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
940 && !dri2_dpy
->robustness
) {
941 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
945 ctx_attribs
[num_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
946 ctx_attribs
[num_attribs
++] = dri2_ctx
->base
.Flags
;
949 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
950 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
951 * extension, don't even try to send it a reset strategy. It may
952 * explode. Instead, generate the required EGL error here.
954 if (!dri2_dpy
->robustness
) {
955 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
959 ctx_attribs
[num_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
960 ctx_attribs
[num_attribs
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
963 assert(num_attribs
<= ARRAY_SIZE(ctx_attribs
));
965 dri2_ctx
->dri_context
=
966 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
974 dri2_create_context_attribs_error(error
);
976 dri2_ctx
->dri_context
=
977 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
984 assert(dri2_dpy
->swrast
);
985 dri2_ctx
->dri_context
=
986 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
993 if (!dri2_ctx
->dri_context
)
996 return &dri2_ctx
->base
;
1004 * Called via eglDestroyContext(), drv->API.DestroyContext().
1007 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1009 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1010 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1012 if (_eglPutContext(ctx
)) {
1013 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1021 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1024 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1025 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1027 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1028 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1029 struct dri2_egl_surface
*dri2_dsurf
= dri2_egl_surface(dsurf
);
1030 struct dri2_egl_surface
*dri2_rsurf
= dri2_egl_surface(rsurf
);
1031 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1032 _EGLContext
*old_ctx
;
1033 _EGLSurface
*old_dsurf
, *old_rsurf
;
1034 __DRIdrawable
*ddraw
, *rdraw
;
1037 /* make new bindings */
1038 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
))
1041 /* flush before context switch */
1042 if (old_ctx
&& dri2_drv
->glFlush
)
1043 dri2_drv
->glFlush();
1045 ddraw
= (dri2_dsurf
) ? dri2_dsurf
->dri_drawable
: NULL
;
1046 rdraw
= (dri2_rsurf
) ? dri2_rsurf
->dri_drawable
: NULL
;
1047 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1050 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1051 dri2_dpy
->core
->unbindContext(old_cctx
);
1054 if ((cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
) ||
1055 dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1057 drv
->API
.DestroySurface(drv
, disp
, old_dsurf
);
1059 drv
->API
.DestroySurface(drv
, disp
, old_rsurf
);
1061 drv
->API
.DestroyContext(drv
, disp
, old_ctx
);
1065 /* undo the previous _eglBindContext */
1066 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &dsurf
, &rsurf
);
1067 assert(&dri2_ctx
->base
== ctx
&&
1068 &dri2_dsurf
->base
== dsurf
&&
1069 &dri2_rsurf
->base
== rsurf
);
1071 _eglPutSurface(dsurf
);
1072 _eglPutSurface(rsurf
);
1073 _eglPutContext(ctx
);
1075 _eglPutSurface(old_dsurf
);
1076 _eglPutSurface(old_rsurf
);
1077 _eglPutContext(old_ctx
);
1084 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1087 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1089 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1091 return dri2_drv
->get_proc_address(procname
);
1095 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1096 _EGLConfig
*conf
, void *native_window
,
1097 const EGLint
*attrib_list
)
1099 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1100 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1105 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1106 _EGLConfig
*conf
, void *native_pixmap
,
1107 const EGLint
*attrib_list
)
1109 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1110 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1115 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1116 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1118 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1119 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1123 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1125 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1126 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1130 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1133 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1134 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1138 * Asks the client API to flush any rendering to the drawable so that we can
1139 * do our swapbuffers.
1142 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1144 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1145 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1147 if (dri2_dpy
->flush
) {
1148 if (dri2_dpy
->flush
->base
.version
>= 4) {
1149 /* We know there's a current context because:
1151 * "If surface is not bound to the calling thread’s current
1152 * context, an EGL_BAD_SURFACE error is generated."
1154 _EGLContext
*ctx
= _eglGetCurrentContext();
1155 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1157 /* From the EGL 1.4 spec (page 52):
1159 * "The contents of ancillary buffers are always undefined
1160 * after calling eglSwapBuffers."
1162 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1163 dri2_surf
->dri_drawable
,
1164 __DRI2_FLUSH_DRAWABLE
|
1165 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1166 __DRI2_THROTTLE_SWAPBUFFER
);
1168 dri2_dpy
->flush
->flush(dri2_surf
->dri_drawable
);
1174 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1176 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1177 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1181 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1183 const EGLint
*rects
, EGLint n_rects
)
1185 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1186 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1191 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1192 EGLint numRects
, const EGLint
*rects
)
1194 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1195 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1199 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1200 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1202 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1203 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1207 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1208 void *native_pixmap_target
)
1210 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1211 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1215 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1217 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1218 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1222 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1224 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1225 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(ctx
->DrawSurface
);
1229 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1230 * we need to copy fake to real here.*/
1232 if (dri2_dpy
->flush
!= NULL
)
1233 dri2_dpy
->flush
->flush(dri2_surf
->dri_drawable
);
1239 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1244 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1245 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1252 dri2_bind_tex_image(_EGLDriver
*drv
,
1253 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1255 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1256 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1257 struct dri2_egl_context
*dri2_ctx
;
1259 GLint format
, target
;
1261 ctx
= _eglGetCurrentContext();
1262 dri2_ctx
= dri2_egl_context(ctx
);
1264 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1267 switch (dri2_surf
->base
.TextureFormat
) {
1268 case EGL_TEXTURE_RGB
:
1269 format
= __DRI_TEXTURE_FORMAT_RGB
;
1271 case EGL_TEXTURE_RGBA
:
1272 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1275 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1276 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1279 switch (dri2_surf
->base
.TextureTarget
) {
1280 case EGL_TEXTURE_2D
:
1281 target
= GL_TEXTURE_2D
;
1284 target
= GL_TEXTURE_2D
;
1285 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1288 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1290 dri2_surf
->dri_drawable
);
1296 dri2_release_tex_image(_EGLDriver
*drv
,
1297 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1299 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1300 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1301 struct dri2_egl_context
*dri2_ctx
;
1305 ctx
= _eglGetCurrentContext();
1306 dri2_ctx
= dri2_egl_context(ctx
);
1308 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1311 switch (dri2_surf
->base
.TextureTarget
) {
1312 case EGL_TEXTURE_2D
:
1313 target
= GL_TEXTURE_2D
;
1319 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1320 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1321 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1323 dri2_surf
->dri_drawable
);
1330 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1331 EGLenum target
, EGLClientBuffer buffer
,
1332 const EGLint
*attr_list
)
1334 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1335 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1340 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1342 struct dri2_egl_image
*dri2_img
;
1344 if (dri_image
== NULL
) {
1345 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1349 dri2_img
= malloc(sizeof *dri2_img
);
1351 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1355 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1360 dri2_img
->dri_image
= dri_image
;
1362 return &dri2_img
->base
;
1366 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1367 EGLClientBuffer buffer
,
1368 const EGLint
*attr_list
)
1370 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1371 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1372 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1373 __DRIimage
*dri_image
;
1375 if (renderbuffer
== 0) {
1376 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1377 return EGL_NO_IMAGE_KHR
;
1381 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1382 renderbuffer
, NULL
);
1384 return dri2_create_image_from_dri(disp
, dri_image
);
1389 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1390 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1392 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1393 EGLint format
, name
, pitch
, err
;
1394 _EGLImageAttribs attrs
;
1395 __DRIimage
*dri_image
;
1397 name
= (EGLint
) (uintptr_t) buffer
;
1399 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1400 if (err
!= EGL_SUCCESS
)
1403 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1404 attrs
.DRMBufferStrideMESA
<= 0) {
1405 _eglError(EGL_BAD_PARAMETER
,
1406 "bad width, height or stride");
1410 switch (attrs
.DRMBufferFormatMESA
) {
1411 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1412 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1413 pitch
= attrs
.DRMBufferStrideMESA
;
1416 _eglError(EGL_BAD_PARAMETER
,
1417 "dri2_create_image_khr: unsupported pixmap depth");
1422 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1430 return dri2_create_image_from_dri(disp
, dri_image
);
1434 #ifdef HAVE_WAYLAND_PLATFORM
1436 /* This structure describes how a wl_buffer maps to one or more
1437 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1438 * offsets and strides of the planes in the buffer. This table maps a
1439 * wl_drm format code to a description of the planes in the buffer
1440 * that lets us create a __DRIimage for each of the planes. */
1442 static const struct wl_drm_components_descriptor
{
1443 uint32_t dri_components
;
1446 } wl_drm_components
[] = {
1447 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1448 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1449 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1450 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1451 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1455 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1456 EGLClientBuffer _buffer
,
1457 const EGLint
*attr_list
)
1459 struct wl_drm_buffer
*buffer
;
1460 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1461 const struct wl_drm_components_descriptor
*f
;
1462 __DRIimage
*dri_image
;
1463 _EGLImageAttribs attrs
;
1467 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1468 (struct wl_resource
*) _buffer
);
1472 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1473 plane
= attrs
.PlaneWL
;
1474 if (err
!= EGL_SUCCESS
) {
1475 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1479 f
= buffer
->driver_format
;
1480 if (plane
< 0 || plane
>= f
->nplanes
) {
1481 _eglError(EGL_BAD_PARAMETER
,
1482 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1486 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1488 if (dri_image
== NULL
) {
1489 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1493 return dri2_create_image_from_dri(disp
, dri_image
);
1498 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1499 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1502 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1503 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1507 * Set the error code after a call to
1508 * dri2_egl_image::dri_image::createImageFromTexture.
1511 dri2_create_image_khr_texture_error(int dri_error
)
1515 switch (dri_error
) {
1516 case __DRI_IMAGE_ERROR_SUCCESS
:
1519 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1520 egl_error
= EGL_BAD_ALLOC
;
1523 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1524 egl_error
= EGL_BAD_MATCH
;
1527 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1528 egl_error
= EGL_BAD_PARAMETER
;
1533 egl_error
= EGL_BAD_MATCH
;
1537 _eglError(egl_error
, "dri2_create_image_khr_texture");
1541 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1543 EGLClientBuffer buffer
,
1544 const EGLint
*attr_list
)
1546 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1547 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1548 struct dri2_egl_image
*dri2_img
;
1549 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1550 _EGLImageAttribs attrs
;
1556 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1557 return EGL_NO_IMAGE_KHR
;
1560 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1561 return EGL_NO_IMAGE_KHR
;
1564 case EGL_GL_TEXTURE_2D_KHR
:
1566 gl_target
= GL_TEXTURE_2D
;
1568 case EGL_GL_TEXTURE_3D_KHR
:
1569 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1570 depth
= attrs
.GLTextureZOffset
;
1571 gl_target
= GL_TEXTURE_3D
;
1575 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1576 return EGL_NO_IMAGE_KHR
;
1578 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1579 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1580 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1581 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1582 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1583 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1584 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1585 gl_target
= GL_TEXTURE_CUBE_MAP
;
1588 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1589 return EGL_NO_IMAGE_KHR
;
1592 dri2_img
= malloc(sizeof *dri2_img
);
1594 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1595 return EGL_NO_IMAGE_KHR
;
1598 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1599 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1601 return EGL_NO_IMAGE_KHR
;
1604 dri2_img
->dri_image
=
1605 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1609 attrs
.GLTextureLevel
,
1612 dri2_create_image_khr_texture_error(error
);
1614 if (!dri2_img
->dri_image
) {
1616 return EGL_NO_IMAGE_KHR
;
1618 return &dri2_img
->base
;
1621 static struct wl_buffer
*
1622 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1625 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1626 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1631 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1638 * "Required attributes and their values are as follows:
1640 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1642 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1643 * by drm_fourcc.h and used as the pixel_format parameter of the
1644 * drm_mode_fb_cmd2 ioctl."
1648 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1649 * incomplete, EGL_BAD_PARAMETER is generated."
1651 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1652 !attrs
->DMABufFourCC
.IsPresent
) {
1653 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1660 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1661 * specified for a plane's pitch or offset isn't supported by EGL,
1662 * EGL_BAD_ACCESS is generated."
1664 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1665 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1666 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1667 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1675 /* Returns the total number of file descriptors. Zero indicates an error. */
1677 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1679 unsigned i
, plane_n
;
1681 switch (attrs
->DMABufFourCC
.Value
) {
1682 case DRM_FORMAT_RGB332
:
1683 case DRM_FORMAT_BGR233
:
1684 case DRM_FORMAT_XRGB4444
:
1685 case DRM_FORMAT_XBGR4444
:
1686 case DRM_FORMAT_RGBX4444
:
1687 case DRM_FORMAT_BGRX4444
:
1688 case DRM_FORMAT_ARGB4444
:
1689 case DRM_FORMAT_ABGR4444
:
1690 case DRM_FORMAT_RGBA4444
:
1691 case DRM_FORMAT_BGRA4444
:
1692 case DRM_FORMAT_XRGB1555
:
1693 case DRM_FORMAT_XBGR1555
:
1694 case DRM_FORMAT_RGBX5551
:
1695 case DRM_FORMAT_BGRX5551
:
1696 case DRM_FORMAT_ARGB1555
:
1697 case DRM_FORMAT_ABGR1555
:
1698 case DRM_FORMAT_RGBA5551
:
1699 case DRM_FORMAT_BGRA5551
:
1700 case DRM_FORMAT_RGB565
:
1701 case DRM_FORMAT_BGR565
:
1702 case DRM_FORMAT_RGB888
:
1703 case DRM_FORMAT_BGR888
:
1704 case DRM_FORMAT_XRGB8888
:
1705 case DRM_FORMAT_XBGR8888
:
1706 case DRM_FORMAT_RGBX8888
:
1707 case DRM_FORMAT_BGRX8888
:
1708 case DRM_FORMAT_ARGB8888
:
1709 case DRM_FORMAT_ABGR8888
:
1710 case DRM_FORMAT_RGBA8888
:
1711 case DRM_FORMAT_BGRA8888
:
1712 case DRM_FORMAT_XRGB2101010
:
1713 case DRM_FORMAT_XBGR2101010
:
1714 case DRM_FORMAT_RGBX1010102
:
1715 case DRM_FORMAT_BGRX1010102
:
1716 case DRM_FORMAT_ARGB2101010
:
1717 case DRM_FORMAT_ABGR2101010
:
1718 case DRM_FORMAT_RGBA1010102
:
1719 case DRM_FORMAT_BGRA1010102
:
1720 case DRM_FORMAT_YUYV
:
1721 case DRM_FORMAT_YVYU
:
1722 case DRM_FORMAT_UYVY
:
1723 case DRM_FORMAT_VYUY
:
1726 case DRM_FORMAT_NV12
:
1727 case DRM_FORMAT_NV21
:
1728 case DRM_FORMAT_NV16
:
1729 case DRM_FORMAT_NV61
:
1732 case DRM_FORMAT_YUV410
:
1733 case DRM_FORMAT_YVU410
:
1734 case DRM_FORMAT_YUV411
:
1735 case DRM_FORMAT_YVU411
:
1736 case DRM_FORMAT_YUV420
:
1737 case DRM_FORMAT_YVU420
:
1738 case DRM_FORMAT_YUV422
:
1739 case DRM_FORMAT_YVU422
:
1740 case DRM_FORMAT_YUV444
:
1741 case DRM_FORMAT_YVU444
:
1745 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
1752 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1753 * incomplete, EGL_BAD_PARAMETER is generated."
1755 for (i
= 0; i
< plane_n
; ++i
) {
1756 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
1757 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
1758 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
1759 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
1765 * The spec also says:
1767 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
1768 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
1769 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
1770 * attributes are specified."
1772 for (i
= plane_n
; i
< 3; ++i
) {
1773 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
1774 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
1775 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
1776 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
1787 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
1788 * EGL will take a reference to the dma_buf(s) which it will release at any
1789 * time while the EGLDisplay is initialized. It is the responsibility of the
1790 * application to close the dma_buf file descriptors."
1792 * Therefore we must never close or otherwise modify the file descriptors.
1795 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1796 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1798 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1801 _EGLImageAttribs attrs
;
1802 __DRIimage
*dri_image
;
1813 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
1814 * error EGL_BAD_PARAMETER is generated."
1816 if (buffer
!= NULL
) {
1817 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
1821 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1822 if (err
!= EGL_SUCCESS
) {
1823 _eglError(err
, "bad attribute");
1827 if (!dri2_check_dma_buf_attribs(&attrs
))
1830 num_fds
= dri2_check_dma_buf_format(&attrs
);
1834 for (i
= 0; i
< num_fds
; ++i
) {
1835 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
1836 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
1837 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
1841 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
1842 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
1843 fds
, num_fds
, pitches
, offsets
,
1844 attrs
.DMABufYuvColorSpaceHint
.Value
,
1845 attrs
.DMABufSampleRangeHint
.Value
,
1846 attrs
.DMABufChromaHorizontalSiting
.Value
,
1847 attrs
.DMABufChromaVerticalSiting
.Value
,
1850 dri2_create_image_khr_texture_error(error
);
1853 return EGL_NO_IMAGE_KHR
;
1855 res
= dri2_create_image_from_dri(disp
, dri_image
);
1862 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1863 _EGLContext
*ctx
, EGLenum target
,
1864 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1869 case EGL_GL_TEXTURE_2D_KHR
:
1870 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1871 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1872 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1873 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1874 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1875 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1876 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
1877 case EGL_GL_TEXTURE_3D_KHR
:
1878 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1879 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
1882 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1883 return EGL_NO_IMAGE_KHR
;
1885 case EGL_GL_RENDERBUFFER_KHR
:
1886 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
1888 case EGL_DRM_BUFFER_MESA
:
1889 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
1891 #ifdef HAVE_WAYLAND_PLATFORM
1892 case EGL_WAYLAND_BUFFER_WL
:
1893 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
1896 case EGL_LINUX_DMA_BUF_EXT
:
1897 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
1900 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1901 return EGL_NO_IMAGE_KHR
;
1906 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
1908 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1909 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
1913 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
1921 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1922 const EGLint
*attr_list
)
1924 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1925 struct dri2_egl_image
*dri2_img
;
1926 _EGLImageAttribs attrs
;
1927 unsigned int dri_use
, valid_mask
;
1929 EGLint err
= EGL_SUCCESS
;
1933 dri2_img
= malloc(sizeof *dri2_img
);
1935 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1936 return EGL_NO_IMAGE_KHR
;
1940 err
= EGL_BAD_PARAMETER
;
1944 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1945 err
= EGL_BAD_PARAMETER
;
1949 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1950 if (err
!= EGL_SUCCESS
)
1953 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
1954 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
1955 attrs
.Width
, attrs
.Height
);
1959 switch (attrs
.DRMBufferFormatMESA
) {
1960 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1961 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1964 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
1965 attrs
.DRMBufferFormatMESA
);
1970 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
1971 EGL_DRM_BUFFER_USE_SHARE_MESA
|
1972 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
1973 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
1974 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
1975 attrs
.DRMBufferUseMESA
& ~valid_mask
);
1980 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
1981 dri_use
|= __DRI_IMAGE_USE_SHARE
;
1982 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
1983 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
1984 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
1985 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
1987 dri2_img
->dri_image
=
1988 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
1989 attrs
.Width
, attrs
.Height
,
1990 format
, dri_use
, dri2_img
);
1991 if (dri2_img
->dri_image
== NULL
) {
1992 err
= EGL_BAD_ALLOC
;
1996 return &dri2_img
->base
;
2000 _eglError(err
, "dri2_create_drm_image_mesa");
2002 return EGL_NO_IMAGE_KHR
;
2006 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2007 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2009 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2010 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2014 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2015 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2016 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2021 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2022 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2025 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2026 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2032 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2034 EGLint
*fourcc
, EGLint
*nplanes
,
2035 EGLuint64KHR
*modifiers
)
2037 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2038 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2044 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2045 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2047 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2048 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2057 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2058 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2060 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2061 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2065 /* rework later to provide multiple fds/strides/offsets */
2067 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2068 __DRI_IMAGE_ATTRIB_FD
, fds
);
2071 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2072 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2081 #ifdef HAVE_WAYLAND_PLATFORM
2084 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2085 struct wl_drm_buffer
*buffer
)
2087 _EGLDisplay
*disp
= user_data
;
2088 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2090 int i
, dri_components
= 0;
2093 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2102 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2114 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2116 buffer
->driver_format
= NULL
;
2117 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2118 if (wl_drm_components
[i
].dri_components
== dri_components
)
2119 buffer
->driver_format
= &wl_drm_components
[i
];
2121 if (buffer
->driver_format
== NULL
)
2122 dri2_dpy
->image
->destroyImage(img
);
2124 buffer
->driver_buffer
= img
;
2128 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2130 _EGLDisplay
*disp
= user_data
;
2131 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2133 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2136 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2137 .authenticate
= NULL
,
2138 .reference_buffer
= dri2_wl_reference_buffer
,
2139 .release_buffer
= dri2_wl_release_buffer
2143 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2144 struct wl_display
*wl_dpy
)
2146 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2152 if (dri2_dpy
->wl_server_drm
)
2155 wl_drm_callbacks
.authenticate
=
2156 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2159 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2160 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2161 dri2_dpy
->image
->base
.version
>= 7 &&
2162 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2163 flags
|= WAYLAND_DRM_PRIME
;
2166 dri2_dpy
->wl_server_drm
=
2167 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2168 &wl_drm_callbacks
, disp
, flags
);
2170 if (!dri2_dpy
->wl_server_drm
)
2173 #ifdef HAVE_DRM_PLATFORM
2174 /* We have to share the wl_drm instance with gbm, so gbm can convert
2175 * wl_buffers to gbm bos. */
2176 if (dri2_dpy
->gbm_dri
)
2177 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2184 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2185 struct wl_display
*wl_dpy
)
2187 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2191 if (!dri2_dpy
->wl_server_drm
)
2194 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2195 dri2_dpy
->wl_server_drm
= NULL
;
2201 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2202 struct wl_resource
*buffer_resource
,
2203 EGLint attribute
, EGLint
*value
)
2205 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2206 struct wl_drm_buffer
*buffer
;
2207 const struct wl_drm_components_descriptor
*format
;
2209 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2213 format
= buffer
->driver_format
;
2214 switch (attribute
) {
2215 case EGL_TEXTURE_FORMAT
:
2216 *value
= format
->components
;
2219 *value
= buffer
->width
;
2222 *value
= buffer
->height
;
2231 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2233 p_atomic_inc(&sync
->refcount
);
2237 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2238 struct dri2_egl_sync
*dri2_sync
)
2240 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2241 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2247 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2248 EGLenum type
, const EGLint
*attrib_list
,
2249 const EGLAttrib
*attrib_list64
)
2251 _EGLContext
*ctx
= _eglGetCurrentContext();
2252 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2253 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2254 struct dri2_egl_sync
*dri2_sync
;
2256 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2258 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2262 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
,
2269 case EGL_SYNC_FENCE_KHR
:
2270 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2271 if (!dri2_sync
->fence
) {
2272 /* Why did it fail? DRI doesn't return an error code, so we emit
2273 * a generic EGL error that doesn't communicate user error.
2275 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2281 case EGL_SYNC_CL_EVENT_KHR
:
2282 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2283 dri2_dpy
->dri_screen
,
2284 dri2_sync
->base
.CLEvent
);
2285 /* this can only happen if the cl_event passed in is invalid. */
2286 if (!dri2_sync
->fence
) {
2287 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2292 /* the initial status must be "signaled" if the cl_event is signaled */
2293 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2294 dri2_sync
->fence
, 0, 0))
2295 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2299 p_atomic_set(&dri2_sync
->refcount
, 1);
2300 return &dri2_sync
->base
;
2304 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2306 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2307 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2309 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2314 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2315 EGLint flags
, EGLTime timeout
)
2317 _EGLContext
*ctx
= _eglGetCurrentContext();
2318 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2319 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2320 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2321 unsigned wait_flags
= 0;
2322 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2324 if (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2325 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2327 /* the sync object should take a reference while waiting */
2328 dri2_egl_ref_sync(dri2_sync
);
2330 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2331 dri2_sync
->fence
, wait_flags
,
2333 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2335 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2337 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2342 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2344 _EGLContext
*ctx
= _eglGetCurrentContext();
2345 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2346 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2347 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2349 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2350 dri2_sync
->fence
, 0);
2355 dri2_unload(_EGLDriver
*drv
)
2357 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2359 if (dri2_drv
->handle
)
2360 dlclose(dri2_drv
->handle
);
2365 dri2_load(_EGLDriver
*drv
)
2367 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2368 #ifdef HAVE_ANDROID_PLATFORM
2369 const char *libname
= "libglapi.so";
2370 #elif defined(__APPLE__)
2371 const char *libname
= "libglapi.0.dylib";
2373 const char *libname
= "libglapi.so.0";
2377 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2378 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2380 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2381 dlsym(handle
, "_glapi_get_proc_address");
2382 if (!dri2_drv
->get_proc_address
|| !libname
) {
2383 /* no need to keep a reference */
2389 /* if glapi is not available, loading DRI drivers will fail */
2390 if (!dri2_drv
->get_proc_address
) {
2391 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2395 dri2_drv
->glFlush
= (void (*)(void))
2396 dri2_drv
->get_proc_address("glFlush");
2398 dri2_drv
->handle
= handle
;
2404 * This is the main entrypoint into the driver, called by libEGL.
2405 * Create a new _EGLDriver object and init its dispatch table.
2408 _eglBuiltInDriverDRI2(const char *args
)
2410 struct dri2_egl_driver
*dri2_drv
;
2414 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2418 if (!dri2_load(&dri2_drv
->base
)) {
2423 _eglInitDriverFallbacks(&dri2_drv
->base
);
2424 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2425 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2426 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2427 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2428 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2429 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2430 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2431 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2432 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2433 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2434 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2435 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2436 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2437 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2438 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2439 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2440 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2441 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2442 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2443 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2444 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2445 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2446 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2447 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2449 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2450 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2451 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2452 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2454 #ifdef HAVE_WAYLAND_PLATFORM
2455 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2456 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2457 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2459 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2460 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2461 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2462 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2463 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2465 dri2_drv
->base
.Name
= "DRI2";
2466 dri2_drv
->base
.Unload
= dri2_unload
;
2468 return &dri2_drv
->base
;