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_X11_PLATFORM
671 case _EGL_PLATFORM_X11:
672 if (disp->Options.TestOnly)
674 return dri2_initialize_x11(drv, disp);
677 #ifdef HAVE_DRM_PLATFORM
678 case _EGL_PLATFORM_DRM:
679 if (disp->Options.TestOnly)
681 return dri2_initialize_drm(drv, disp);
683 #ifdef HAVE_WAYLAND_PLATFORM
684 case _EGL_PLATFORM_WAYLAND:
685 if (disp->Options.TestOnly)
687 return dri2_initialize_wayland(drv, disp);
689 #ifdef HAVE_ANDROID_PLATFORM
690 case _EGL_PLATFORM_ANDROID:
691 if (disp->Options.TestOnly)
693 return dri2_initialize_android(drv, disp);
697 _eglLog(_EGL_WARNING, "No EGL platform enabled.");
703 * Called via eglTerminate(), drv->API.Terminate().
706 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
708 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
711 _eglReleaseDisplayResources(drv, disp);
712 _eglCleanupDisplay(disp);
714 if (dri2_dpy->own_dri_screen)
715 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
718 if (dri2_dpy->driver)
719 dlclose(dri2_dpy->driver);
720 free(dri2_dpy->device_name);
721 free(dri2_dpy->driver_name);
723 switch (disp->Platform) {
724 #ifdef HAVE_X11_PLATFORM
725 case _EGL_PLATFORM_X11:
726 if (dri2_dpy->own_device) {
727 xcb_disconnect(dri2_dpy->conn);
731 #ifdef HAVE_DRM_PLATFORM
732 case _EGL_PLATFORM_DRM:
733 if (dri2_dpy->own_device) {
734 gbm_device_destroy(&dri2_dpy->gbm_dri->base.base);
738 #ifdef HAVE_WAYLAND_PLATFORM
739 case _EGL_PLATFORM_WAYLAND:
740 if (dri2_dpy->wl_drm)
741 wl_drm_destroy(dri2_dpy->wl_drm);
742 if (dri2_dpy->wl_shm)
743 wl_shm_destroy(dri2_dpy->wl_shm);
744 wl_registry_destroy(dri2_dpy->wl_registry);
745 wl_event_queue_destroy(dri2_dpy->wl_queue);
746 if (dri2_dpy->own_device) {
747 wl_display_disconnect(dri2_dpy->wl_dpy);
755 /* The drm platform does not create the screen/driver_configs but reuses
756 * the ones from the gbm device. As such the gbm itself is responsible
759 if (disp->Platform != _EGL_PLATFORM_DRM) {
760 for (i = 0; dri2_dpy->driver_configs[i]; i++)
761 free((__DRIconfig *) dri2_dpy->driver_configs[i]);
762 free(dri2_dpy->driver_configs);
765 disp->DriverData = NULL;
771 * Set the error code after a call to
772 * dri2_egl_display::dri2::createContextAttribs.
775 dri2_create_context_attribs_error(int dri_error)
780 case __DRI_CTX_ERROR_SUCCESS:
783 case __DRI_CTX_ERROR_NO_MEMORY:
784 egl_error = EGL_BAD_ALLOC;
787 /* From the EGL_KHR_create_context spec, section "Errors":
789 * * If <config> does not support a client API context compatible
790 * with the requested API major and minor version, [...] context flags,
791 * and context reset notification behavior (for client API types where
792 * these attributes are supported), then an EGL_BAD_MATCH error is
795 * * If an OpenGL ES context is requested and the values for
796 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
797 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
798 * is not defined, than an EGL_BAD_MATCH error is generated.
800 * * If an OpenGL context is requested, the requested version is
801 * greater than 3.2, and the value for attribute
802 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
803 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
804 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
805 * one of these bits set; or if the implementation does not support
806 * the requested profile, then an EGL_BAD_MATCH error is generated.
808 case __DRI_CTX_ERROR_BAD_API:
809 case __DRI_CTX_ERROR_BAD_VERSION:
810 case __DRI_CTX_ERROR_BAD_FLAG:
811 egl_error = EGL_BAD_MATCH;
814 /* From the EGL_KHR_create_context spec, section "Errors":
816 * * If an attribute name or attribute value in <attrib_list> is not
817 * recognized (including unrecognized bits in bitmask attributes),
818 * then an EGL_BAD_ATTRIBUTE error is generated."
820 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
821 case __DRI_CTX_ERROR_UNKNOWN_FLAG:
822 egl_error = EGL_BAD_ATTRIBUTE;
827 egl_error = EGL_BAD_MATCH;
831 _eglError(egl_error, "dri2_create_context");
835 * Called via eglCreateContext(), drv->API.CreateContext().
838 dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
839 _EGLContext *share_list, const EGLint *attrib_list)
841 struct dri2_egl_context *dri2_ctx;
842 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
843 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
844 __DRIcontext *shared =
845 dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
846 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
847 const __DRIconfig *dri_config;
852 dri2_ctx = malloc(sizeof *dri2_ctx);
854 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
858 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
861 switch (dri2_ctx->base.ClientAPI) {
862 case EGL_OPENGL_ES_API:
863 switch (dri2_ctx->base.ClientMajorVersion) {
865 api = __DRI_API_GLES;
868 api = __DRI_API_GLES2;
871 api = __DRI_API_GLES3;
874 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
880 if ((dri2_ctx->base.ClientMajorVersion >= 4
881 || (dri2_ctx->base.ClientMajorVersion == 3
882 && dri2_ctx->base.ClientMinorVersion >= 2))
883 && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
884 api = __DRI_API_OPENGL_CORE;
886 api = __DRI_API_OPENGL;
889 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
895 /* The config chosen here isn't necessarily
896 * used for surfaces later.
897 * A pixmap surface will use the single config.
898 * This opportunity depends on disabling the
899 * doubleBufferMode check in
900 * src/mesa/main/context.c:check_compatible()
902 if (dri2_config->dri_double_config)
903 dri_config = dri2_config->dri_double_config;
905 dri_config = dri2_config->dri_single_config;
907 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
908 * makes sure the back buffer will always be used.
910 if (conf->SurfaceType & EGL_WINDOW_BIT)
911 dri2_ctx->base.WindowRenderBuffer = EGL_BACK_BUFFER;
916 if (dri2_dpy->dri2) {
917 if (dri2_dpy->dri2->base.version >= 3) {
919 unsigned num_attribs = 0;
920 uint32_t ctx_attribs[8];
922 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
923 ctx_attribs[num_attribs++] = dri2_ctx->base.ClientMajorVersion;
924 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
925 ctx_attribs[num_attribs++] = dri2_ctx->base.ClientMinorVersion;
927 if (dri2_ctx->base.Flags != 0) {
928 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
929 * extension, don't even try to send it the robust-access flag.
930 * It may explode. Instead, generate the required EGL error here.
932 if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
933 && !dri2_dpy->robustness) {
934 _eglError(EGL_BAD_MATCH, "eglCreateContext");
938 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
939 ctx_attribs[num_attribs++] = dri2_ctx->base.Flags;
942 if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
943 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
944 * extension, don't even try to send it a reset strategy. It may
945 * explode. Instead, generate the required EGL error here.
947 if (!dri2_dpy->robustness) {
948 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
952 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
953 ctx_attribs[num_attribs++] = __DRI_CTX_RESET_LOSE_CONTEXT;
956 assert(num_attribs <= ARRAY_SIZE(ctx_attribs));
958 dri2_ctx->dri_context =
959 dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
967 dri2_create_context_attribs_error(error);
969 dri2_ctx->dri_context =
970 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
977 assert(dri2_dpy->swrast);
978 dri2_ctx->dri_context =
979 dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
986 if (!dri2_ctx->dri_context)
989 return &dri2_ctx->base;
997 * Called via eglDestroyContext(), drv->API.DestroyContext().
1000 dri2_destroy_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
1002 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1003 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1005 if (_eglPutContext(ctx)) {
1006 dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
1014 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1017 dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
1018 _EGLSurface *rsurf, _EGLContext *ctx)
1020 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1021 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1022 struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf);
1023 struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf);
1024 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1025 _EGLContext *old_ctx;
1026 _EGLSurface *old_dsurf, *old_rsurf;
1027 __DRIdrawable *ddraw, *rdraw;
1030 /* make new bindings */
1031 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
1034 /* flush before context switch */
1035 if (old_ctx && dri2_drv->glFlush)
1036 dri2_drv->glFlush();
1038 ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL;
1039 rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL;
1040 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
1043 __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
1044 dri2_dpy->core->unbindContext(old_cctx);
1047 if ((cctx == NULL && ddraw == NULL && rdraw == NULL) ||
1048 dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1050 drv->API.DestroySurface(drv, disp, old_dsurf);
1052 drv->API.DestroySurface(drv, disp, old_rsurf);
1054 drv->API.DestroyContext(drv, disp, old_ctx);
1058 /* undo the previous _eglBindContext */
1059 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &dsurf, &rsurf);
1060 assert(&dri2_ctx->base == ctx &&
1061 &dri2_dsurf->base == dsurf &&
1062 &dri2_rsurf->base == rsurf);
1064 _eglPutSurface(dsurf);
1065 _eglPutSurface(rsurf);
1066 _eglPutContext(ctx);
1068 _eglPutSurface(old_dsurf);
1069 _eglPutSurface(old_rsurf);
1070 _eglPutContext(old_ctx);
1077 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1080 dri2_get_proc_address(_EGLDriver *drv, const char *procname)
1082 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1084 return dri2_drv->get_proc_address(procname);
1088 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1089 _EGLConfig *conf, void *native_window,
1090 const EGLint *attrib_list)
1092 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1093 return dri2_dpy->vtbl->create_window_surface(drv, dpy, conf, native_window,
1098 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1099 _EGLConfig *conf, void *native_pixmap,
1100 const EGLint *attrib_list)
1102 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1103 return dri2_dpy->vtbl->create_pixmap_surface(drv, dpy, conf, native_pixmap,
1108 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1109 _EGLConfig *conf, const EGLint *attrib_list)
1111 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1112 return dri2_dpy->vtbl->create_pbuffer_surface(drv, dpy, conf, attrib_list);
1116 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
1118 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1119 return dri2_dpy->vtbl->destroy_surface(drv, dpy, surf);
1123 dri2_swap_interval(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
1126 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1127 return dri2_dpy->vtbl->swap_interval(drv, dpy, surf, interval);
1131 * Asks the client API to flush any rendering to the drawable so that we can
1132 * do our swapbuffers.
1135 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)
1137 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1138 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1140 if (dri2_dpy->flush) {
1141 if (dri2_dpy->flush->base.version >= 4) {
1142 /* We know there's a current context because:
1144 * "If surface is not bound to the calling thread’s current
1145 * context, an EGL_BAD_SURFACE error is generated."
1147 _EGLContext *ctx = _eglGetCurrentContext();
1148 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1150 /* From the EGL 1.4 spec (page 52):
1152 * "The contents of ancillary buffers are always undefined
1153 * after calling eglSwapBuffers."
1155 dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,
1156 dri2_surf->dri_drawable,
1157 __DRI2_FLUSH_DRAWABLE |
1158 __DRI2_FLUSH_INVALIDATE_ANCILLARY,
1159 __DRI2_THROTTLE_SWAPBUFFER);
1161 dri2_dpy->flush->flush(dri2_surf->dri_drawable);
1167 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
1169 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1170 return dri2_dpy->vtbl->swap_buffers(drv, dpy, surf);
1174 dri2_swap_buffers_with_damage(_EGLDriver *drv, _EGLDisplay *dpy,
1176 const EGLint *rects, EGLint n_rects)
1178 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1179 return dri2_dpy->vtbl->swap_buffers_with_damage(drv, dpy, surf,
1184 dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
1185 EGLint numRects, const EGLint *rects)
1187 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1188 return dri2_dpy->vtbl->swap_buffers_region(drv, dpy, surf, numRects, rects);
1192 dri2_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
1193 EGLint x, EGLint y, EGLint width, EGLint height)
1195 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1196 return dri2_dpy->vtbl->post_sub_buffer(drv, dpy, surf, x, y, width, height);
1200 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
1201 void *native_pixmap_target)
1203 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1204 return dri2_dpy->vtbl->copy_buffers(drv, dpy, surf, native_pixmap_target);
1208 dri2_query_buffer_age(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
1210 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1211 return dri2_dpy->vtbl->query_buffer_age(drv, dpy, surf);
1215 dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
1217 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1218 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface);
1222 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1223 * we need to copy fake to real here.*/
1225 if (dri2_dpy->flush != NULL)
1226 dri2_dpy->flush->flush(dri2_surf->dri_drawable);
1232 dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
1237 if (engine != EGL_CORE_NATIVE_ENGINE)
1238 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
1245 dri2_bind_tex_image(_EGLDriver *drv,
1246 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1248 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1249 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1250 struct dri2_egl_context *dri2_ctx;
1252 GLint format, target;
1254 ctx = _eglGetCurrentContext();
1255 dri2_ctx = dri2_egl_context(ctx);
1257 if (!_eglBindTexImage(drv, disp, surf, buffer))
1260 switch (dri2_surf->base.TextureFormat) {
1261 case EGL_TEXTURE_RGB:
1262 format = __DRI_TEXTURE_FORMAT_RGB;
1264 case EGL_TEXTURE_RGBA:
1265 format = __DRI_TEXTURE_FORMAT_RGBA;
1268 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1269 format = __DRI_TEXTURE_FORMAT_RGBA;
1272 switch (dri2_surf->base.TextureTarget) {
1273 case EGL_TEXTURE_2D:
1274 target = GL_TEXTURE_2D;
1277 target = GL_TEXTURE_2D;
1278 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1281 (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context,
1283 dri2_surf->dri_drawable);
1289 dri2_release_tex_image(_EGLDriver *drv,
1290 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1292 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1293 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1294 struct dri2_egl_context *dri2_ctx;
1298 ctx = _eglGetCurrentContext();
1299 dri2_ctx = dri2_egl_context(ctx);
1301 if (!_eglReleaseTexImage(drv, disp, surf, buffer))
1304 switch (dri2_surf->base.TextureTarget) {
1305 case EGL_TEXTURE_2D:
1306 target = GL_TEXTURE_2D;
1312 if (dri2_dpy->tex_buffer->base.version >= 3 &&
1313 dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {
1314 (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context,
1316 dri2_surf->dri_drawable);
1323 dri2_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
1324 EGLenum target, EGLClientBuffer buffer,
1325 const EGLint *attr_list)
1327 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1328 return dri2_dpy->vtbl->create_image(drv, dpy, ctx, target, buffer,
1333 dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)
1335 struct dri2_egl_image *dri2_img;
1337 if (dri_image == NULL) {
1338 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
1342 dri2_img = malloc(sizeof *dri2_img);
1344 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
1348 if (!_eglInitImage(&dri2_img->base, disp)) {
1353 dri2_img->dri_image = dri_image;
1355 return &dri2_img->base;
1359 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
1360 EGLClientBuffer buffer,
1361 const EGLint *attr_list)
1363 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1364 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1365 GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
1366 __DRIimage *dri_image;
1368 if (renderbuffer == 0) {
1369 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1370 return EGL_NO_IMAGE_KHR;
1374 dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context,
1375 renderbuffer, NULL);
1377 return dri2_create_image_from_dri(disp, dri_image);
1382 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
1383 EGLClientBuffer buffer, const EGLint *attr_list)
1385 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1386 EGLint format, name, pitch, err;
1387 _EGLImageAttribs attrs;
1388 __DRIimage *dri_image;
1390 name = (EGLint) (uintptr_t) buffer;
1392 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1393 if (err != EGL_SUCCESS)
1396 if (attrs.Width <= 0 || attrs.Height <= 0 ||
1397 attrs.DRMBufferStrideMESA <= 0) {
1398 _eglError(EGL_BAD_PARAMETER,
1399 "bad width, height or stride");
1403 switch (attrs.DRMBufferFormatMESA) {
1404 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
1405 format = __DRI_IMAGE_FORMAT_ARGB8888;
1406 pitch = attrs.DRMBufferStrideMESA;
1409 _eglError(EGL_BAD_PARAMETER,
1410 "dri2_create_image_khr: unsupported pixmap depth");
1415 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
1423 return dri2_create_image_from_dri(disp, dri_image);
1427 #ifdef HAVE_WAYLAND_PLATFORM
1429 /* This structure describes how a wl_buffer maps to one or more
1430 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1431 * offsets and strides of the planes in the buffer. This table maps a
1432 * wl_drm format code to a description of the planes in the buffer
1433 * that lets us create a __DRIimage for each of the planes. */
1435 static const struct wl_drm_components_descriptor {
1436 uint32_t dri_components;
1439 } wl_drm_components[] = {
1440 { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
1441 { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
1442 { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
1443 { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
1444 { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
1448 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
1449 EGLClientBuffer _buffer,
1450 const EGLint *attr_list)
1452 struct wl_drm_buffer *buffer;
1453 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1454 const struct wl_drm_components_descriptor *f;
1455 __DRIimage *dri_image;
1456 _EGLImageAttribs attrs;
1460 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
1461 (struct wl_resource *) _buffer);
1465 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1466 plane = attrs.PlaneWL;
1467 if (err != EGL_SUCCESS) {
1468 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
1472 f = buffer->driver_format;
1473 if (plane < 0 || plane >= f->nplanes) {
1474 _eglError(EGL_BAD_PARAMETER,
1475 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1479 dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
1481 if (dri_image == NULL) {
1482 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
1486 return dri2_create_image_from_dri(disp, dri_image);
1491 dri2_get_sync_values_chromium(_EGLDisplay *dpy, _EGLSurface *surf,
1492 EGLuint64KHR *ust, EGLuint64KHR *msc,
1495 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1496 return dri2_dpy->vtbl->get_sync_values(dpy, surf, ust, msc, sbc);
1500 * Set the error code after a call to
1501 * dri2_egl_image::dri_image::createImageFromTexture.
1504 dri2_create_image_khr_texture_error(int dri_error)
1508 switch (dri_error) {
1509 case __DRI_IMAGE_ERROR_SUCCESS:
1512 case __DRI_IMAGE_ERROR_BAD_ALLOC:
1513 egl_error = EGL_BAD_ALLOC;
1516 case __DRI_IMAGE_ERROR_BAD_MATCH:
1517 egl_error = EGL_BAD_MATCH;
1520 case __DRI_IMAGE_ERROR_BAD_PARAMETER:
1521 egl_error = EGL_BAD_PARAMETER;
1526 egl_error = EGL_BAD_MATCH;
1530 _eglError(egl_error, "dri2_create_image_khr_texture");
1534 dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
1536 EGLClientBuffer buffer,
1537 const EGLint *attr_list)
1539 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1540 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1541 struct dri2_egl_image *dri2_img;
1542 GLuint texture = (GLuint) (uintptr_t) buffer;
1543 _EGLImageAttribs attrs;
1549 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1550 return EGL_NO_IMAGE_KHR;
1553 if (_eglParseImageAttribList(&attrs, disp, attr_list) != EGL_SUCCESS)
1554 return EGL_NO_IMAGE_KHR;
1557 case EGL_GL_TEXTURE_2D_KHR:
1559 gl_target = GL_TEXTURE_2D;
1561 case EGL_GL_TEXTURE_3D_KHR:
1562 depth = attrs.GLTextureZOffset;
1563 gl_target = GL_TEXTURE_3D;
1565 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
1566 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
1567 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
1568 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
1569 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
1570 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
1571 depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
1572 gl_target = GL_TEXTURE_CUBE_MAP;
1575 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1576 return EGL_NO_IMAGE_KHR;
1579 dri2_img = malloc(sizeof *dri2_img);
1581 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1582 return EGL_NO_IMAGE_KHR;
1585 if (!_eglInitImage(&dri2_img->base, disp)) {
1586 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1588 return EGL_NO_IMAGE_KHR;
1591 dri2_img->dri_image =
1592 dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
1596 attrs.GLTextureLevel,
1599 dri2_create_image_khr_texture_error(error);
1601 if (!dri2_img->dri_image) {
1603 return EGL_NO_IMAGE_KHR;
1605 return &dri2_img->base;
1608 static struct wl_buffer*
1609 dri2_create_wayland_buffer_from_image(_EGLDriver *drv, _EGLDisplay *dpy,
1612 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1613 return dri2_dpy->vtbl->create_wayland_buffer_from_image(drv, dpy, img);
1618 dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
1625 * "Required attributes and their values are as follows:
1627 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1629 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1630 * by drm_fourcc.h and used as the pixel_format parameter of the
1631 * drm_mode_fb_cmd2 ioctl."
1635 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1636 * incomplete, EGL_BAD_PARAMETER is generated."
1638 if (attrs->Width <= 0 || attrs->Height <= 0 ||
1639 !attrs->DMABufFourCC.IsPresent) {
1640 _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
1647 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1648 * specified for a plane's pitch or offset isn't supported by EGL,
1649 * EGL_BAD_ACCESS is generated."
1651 for (i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
1652 if (attrs->DMABufPlanePitches[i].IsPresent &&
1653 attrs->DMABufPlanePitches[i].Value <= 0) {
1654 _eglError(EGL_BAD_ACCESS, "invalid pitch");
1662 /* Returns the total number of file descriptors. Zero indicates an error. */
1664 dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
1666 unsigned i, plane_n;
1668 switch (attrs->DMABufFourCC.Value) {
1669 case DRM_FORMAT_RGB332:
1670 case DRM_FORMAT_BGR233:
1671 case DRM_FORMAT_XRGB4444:
1672 case DRM_FORMAT_XBGR4444:
1673 case DRM_FORMAT_RGBX4444:
1674 case DRM_FORMAT_BGRX4444:
1675 case DRM_FORMAT_ARGB4444:
1676 case DRM_FORMAT_ABGR4444:
1677 case DRM_FORMAT_RGBA4444:
1678 case DRM_FORMAT_BGRA4444:
1679 case DRM_FORMAT_XRGB1555:
1680 case DRM_FORMAT_XBGR1555:
1681 case DRM_FORMAT_RGBX5551:
1682 case DRM_FORMAT_BGRX5551:
1683 case DRM_FORMAT_ARGB1555:
1684 case DRM_FORMAT_ABGR1555:
1685 case DRM_FORMAT_RGBA5551:
1686 case DRM_FORMAT_BGRA5551:
1687 case DRM_FORMAT_RGB565:
1688 case DRM_FORMAT_BGR565:
1689 case DRM_FORMAT_RGB888:
1690 case DRM_FORMAT_BGR888:
1691 case DRM_FORMAT_XRGB8888:
1692 case DRM_FORMAT_XBGR8888:
1693 case DRM_FORMAT_RGBX8888:
1694 case DRM_FORMAT_BGRX8888:
1695 case DRM_FORMAT_ARGB8888:
1696 case DRM_FORMAT_ABGR8888:
1697 case DRM_FORMAT_RGBA8888:
1698 case DRM_FORMAT_BGRA8888:
1699 case DRM_FORMAT_XRGB2101010:
1700 case DRM_FORMAT_XBGR2101010:
1701 case DRM_FORMAT_RGBX1010102:
1702 case DRM_FORMAT_BGRX1010102:
1703 case DRM_FORMAT_ARGB2101010:
1704 case DRM_FORMAT_ABGR2101010:
1705 case DRM_FORMAT_RGBA1010102:
1706 case DRM_FORMAT_BGRA1010102:
1707 case DRM_FORMAT_YUYV:
1708 case DRM_FORMAT_YVYU:
1709 case DRM_FORMAT_UYVY:
1710 case DRM_FORMAT_VYUY:
1713 case DRM_FORMAT_NV12:
1714 case DRM_FORMAT_NV21:
1715 case DRM_FORMAT_NV16:
1716 case DRM_FORMAT_NV61:
1719 case DRM_FORMAT_YUV410:
1720 case DRM_FORMAT_YVU410:
1721 case DRM_FORMAT_YUV411:
1722 case DRM_FORMAT_YVU411:
1723 case DRM_FORMAT_YUV420:
1724 case DRM_FORMAT_YVU420:
1725 case DRM_FORMAT_YUV422:
1726 case DRM_FORMAT_YVU422:
1727 case DRM_FORMAT_YUV444:
1728 case DRM_FORMAT_YVU444:
1732 _eglError(EGL_BAD_ATTRIBUTE, "invalid format");
1739 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1740 * incomplete, EGL_BAD_PARAMETER is generated."
1742 for (i = 0; i < plane_n; ++i) {
1743 if (!attrs->DMABufPlaneFds[i].IsPresent ||
1744 !attrs->DMABufPlaneOffsets[i].IsPresent ||
1745 !attrs->DMABufPlanePitches[i].IsPresent) {
1746 _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
1752 * The spec also says:
1754 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
1755 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
1756 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
1757 * attributes are specified."
1759 for (i = plane_n; i < 3; ++i) {
1760 if (attrs->DMABufPlaneFds[i].IsPresent ||
1761 attrs->DMABufPlaneOffsets[i].IsPresent ||
1762 attrs->DMABufPlanePitches[i].IsPresent) {
1763 _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
1774 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
1775 * EGL will take a reference to the dma_buf(s) which it will release at any
1776 * time while the EGLDisplay is initialized. It is the responsibility of the
1777 * application to close the dma_buf file descriptors."
1779 * Therefore we must never close or otherwise modify the file descriptors.
1782 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
1783 EGLClientBuffer buffer, const EGLint *attr_list)
1785 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1788 _EGLImageAttribs attrs;
1789 __DRIimage *dri_image;
1800 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
1801 * error EGL_BAD_PARAMETER is generated."
1803 if (buffer != NULL) {
1804 _eglError(EGL_BAD_PARAMETER, "buffer not NULL");
1808 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1809 if (err != EGL_SUCCESS) {
1810 _eglError(err, "bad attribute");
1814 if (!dri2_check_dma_buf_attribs(&attrs))
1817 num_fds = dri2_check_dma_buf_format(&attrs);
1821 for (i = 0; i < num_fds; ++i) {
1822 fds[i] = attrs.DMABufPlaneFds[i].Value;
1823 pitches[i] = attrs.DMABufPlanePitches[i].Value;
1824 offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
1828 dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
1829 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
1830 fds, num_fds, pitches, offsets,
1831 attrs.DMABufYuvColorSpaceHint.Value,
1832 attrs.DMABufSampleRangeHint.Value,
1833 attrs.DMABufChromaHorizontalSiting.Value,
1834 attrs.DMABufChromaVerticalSiting.Value,
1837 dri2_create_image_khr_texture_error(error);
1840 return EGL_NO_IMAGE_KHR;
1842 res = dri2_create_image_from_dri(disp, dri_image);
1849 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1850 _EGLContext *ctx, EGLenum target,
1851 EGLClientBuffer buffer, const EGLint *attr_list)
1856 case EGL_GL_TEXTURE_2D_KHR:
1857 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
1858 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
1859 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
1860 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
1861 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
1862 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
1863 return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
1864 case EGL_GL_RENDERBUFFER_KHR:
1865 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
1867 case EGL_DRM_BUFFER_MESA:
1868 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
1870 #ifdef HAVE_WAYLAND_PLATFORM
1871 case EGL_WAYLAND_BUFFER_WL:
1872 return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
1875 case EGL_LINUX_DMA_BUF_EXT:
1876 return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
1879 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1880 return EGL_NO_IMAGE_KHR;
1885 dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image)
1887 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1888 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
1892 dri2_dpy->image->destroyImage(dri2_img->dri_image);
1900 dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp,
1901 const EGLint *attr_list)
1903 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1904 struct dri2_egl_image *dri2_img;
1905 _EGLImageAttribs attrs;
1906 unsigned int dri_use, valid_mask;
1908 EGLint err = EGL_SUCCESS;
1912 dri2_img = malloc(sizeof *dri2_img);
1914 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1915 return EGL_NO_IMAGE_KHR;
1919 err = EGL_BAD_PARAMETER;
1923 if (!_eglInitImage(&dri2_img->base, disp)) {
1924 err = EGL_BAD_PARAMETER;
1928 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1929 if (err != EGL_SUCCESS)
1932 if (attrs.Width <= 0 || attrs.Height <= 0) {
1933 _eglLog(_EGL_WARNING, "bad width or height (%dx%d)",
1934 attrs.Width, attrs.Height);
1938 switch (attrs.DRMBufferFormatMESA) {
1939 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
1940 format = __DRI_IMAGE_FORMAT_ARGB8888;
1943 _eglLog(_EGL_WARNING, "bad image format value 0x%04x",
1944 attrs.DRMBufferFormatMESA);
1949 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
1950 EGL_DRM_BUFFER_USE_SHARE_MESA |
1951 EGL_DRM_BUFFER_USE_CURSOR_MESA;
1952 if (attrs.DRMBufferUseMESA & ~valid_mask) {
1953 _eglLog(_EGL_WARNING, "bad image use bit 0x%04x",
1954 attrs.DRMBufferUseMESA & ~valid_mask);
1959 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
1960 dri_use |= __DRI_IMAGE_USE_SHARE;
1961 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
1962 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1963 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
1964 dri_use |= __DRI_IMAGE_USE_CURSOR;
1966 dri2_img->dri_image =
1967 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
1968 attrs.Width, attrs.Height,
1969 format, dri_use, dri2_img);
1970 if (dri2_img->dri_image == NULL) {
1971 err = EGL_BAD_ALLOC;
1975 return &dri2_img->base;
1979 _eglError(err, "dri2_create_drm_image_mesa");
1981 return EGL_NO_IMAGE_KHR;
1985 dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
1986 EGLint *name, EGLint *handle, EGLint *stride)
1988 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1989 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
1993 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
1994 __DRI_IMAGE_ATTRIB_NAME, name)) {
1995 _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
2000 dri2_dpy->image->queryImage(dri2_img->dri_image,
2001 __DRI_IMAGE_ATTRIB_HANDLE, handle);
2004 dri2_dpy->image->queryImage(dri2_img->dri_image,
2005 __DRI_IMAGE_ATTRIB_STRIDE, stride);
2011 dri2_export_dma_buf_image_query_mesa(_EGLDriver *drv, _EGLDisplay *disp,
2013 EGLint *fourcc, EGLint *nplanes,
2014 EGLuint64KHR *modifiers)
2016 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2017 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
2023 dri2_dpy->image->queryImage(dri2_img->dri_image,
2024 __DRI_IMAGE_ATTRIB_NUM_PLANES, nplanes);
2026 dri2_dpy->image->queryImage(dri2_img->dri_image,
2027 __DRI_IMAGE_ATTRIB_FOURCC, fourcc);
2036 dri2_export_dma_buf_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
2037 int *fds, EGLint *strides, EGLint *offsets)
2039 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2040 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
2044 /* rework later to provide multiple fds/strides/offsets */
2046 dri2_dpy->image->queryImage(dri2_img->dri_image,
2047 __DRI_IMAGE_ATTRIB_FD, fds);
2050 dri2_dpy->image->queryImage(dri2_img->dri_image,
2051 __DRI_IMAGE_ATTRIB_STRIDE, strides);
2060 #ifdef HAVE_WAYLAND_PLATFORM
2063 dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
2064 struct wl_drm_buffer *buffer)
2066 _EGLDisplay *disp = user_data;
2067 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2069 int i, dri_components = 0;
2072 img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
2081 img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
2093 dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
2095 buffer->driver_format = NULL;
2096 for (i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
2097 if (wl_drm_components[i].dri_components == dri_components)
2098 buffer->driver_format = &wl_drm_components[i];
2100 if (buffer->driver_format == NULL)
2101 dri2_dpy->image->destroyImage(img);
2103 buffer->driver_buffer = img;
2107 dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
2109 _EGLDisplay *disp = user_data;
2110 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2112 dri2_dpy->image->destroyImage(buffer->driver_buffer);
2115 static struct wayland_drm_callbacks wl_drm_callbacks = {
2116 .authenticate = NULL,
2117 .reference_buffer = dri2_wl_reference_buffer,
2118 .release_buffer = dri2_wl_release_buffer
2122 dri2_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
2123 struct wl_display *wl_dpy)
2125 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2131 if (dri2_dpy->wl_server_drm)
2134 wl_drm_callbacks.authenticate =
2135 (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate;
2138 if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&
2139 cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
2140 dri2_dpy->image->base.version >= 7 &&
2141 dri2_dpy->image->createImageFromFds != NULL)
2142 flags |= WAYLAND_DRM_PRIME;
2145 dri2_dpy->wl_server_drm =
2146 wayland_drm_init(wl_dpy, dri2_dpy->device_name,
2147 &wl_drm_callbacks, disp, flags);
2149 if (!dri2_dpy->wl_server_drm)
2152 #ifdef HAVE_DRM_PLATFORM
2153 /* We have to share the wl_drm instance with gbm, so gbm can convert
2154 * wl_buffers to gbm bos. */
2155 if (dri2_dpy->gbm_dri)
2156 dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
2163 dri2_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
2164 struct wl_display *wl_dpy)
2166 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2170 if (!dri2_dpy->wl_server_drm)
2173 wayland_drm_uninit(dri2_dpy->wl_server_drm);
2174 dri2_dpy->wl_server_drm = NULL;
2180 dri2_query_wayland_buffer_wl(_EGLDriver *drv, _EGLDisplay *disp,
2181 struct wl_resource *buffer_resource,
2182 EGLint attribute, EGLint *value)
2184 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2185 struct wl_drm_buffer *buffer;
2186 const struct wl_drm_components_descriptor *format;
2188 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
2192 format = buffer->driver_format;
2193 switch (attribute) {
2194 case EGL_TEXTURE_FORMAT:
2195 *value = format->components;
2198 *value = buffer->width;
2201 *value = buffer->height;
2210 dri2_egl_ref_sync(struct dri2_egl_sync *sync)
2212 p_atomic_inc(&sync->refcount);
2216 dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,
2217 struct dri2_egl_sync *dri2_sync)
2219 if (p_atomic_dec_zero(&dri2_sync->refcount)) {
2220 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);
2226 dri2_create_sync(_EGLDriver *drv, _EGLDisplay *dpy,
2227 EGLenum type, const EGLint *attrib_list,
2228 const EGLAttrib *attrib_list64)
2230 _EGLContext *ctx = _eglGetCurrentContext();
2231 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
2232 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2233 struct dri2_egl_sync *dri2_sync;
2235 dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));
2237 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
2241 if (!_eglInitSync(&dri2_sync->base, dpy, type, attrib_list,
2248 case EGL_SYNC_FENCE_KHR:
2249 dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);
2250 if (!dri2_sync->fence) {
2251 /* Why did it fail? DRI doesn't return an error code, so we emit
2252 * a generic EGL error that doesn't communicate user error.
2254 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
2260 case EGL_SYNC_CL_EVENT_KHR:
2261 dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(
2262 dri2_dpy->dri_screen,
2263 dri2_sync->base.CLEvent);
2264 /* this can only happen if the cl_event passed in is invalid. */
2265 if (!dri2_sync->fence) {
2266 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
2271 /* the initial status must be "signaled" if the cl_event is signaled */
2272 if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
2273 dri2_sync->fence, 0, 0))
2274 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
2278 p_atomic_set(&dri2_sync->refcount, 1);
2279 return &dri2_sync->base;
2283 dri2_destroy_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync)
2285 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
2286 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
2288 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
2293 dri2_client_wait_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync,
2294 EGLint flags, EGLTime timeout)
2296 _EGLContext *ctx = _eglGetCurrentContext();
2297 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
2298 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2299 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
2300 unsigned wait_flags = 0;
2301 EGLint ret = EGL_CONDITION_SATISFIED_KHR;
2303 if (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)
2304 wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;
2306 /* the sync object should take a reference while waiting */
2307 dri2_egl_ref_sync(dri2_sync);
2309 if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
2310 dri2_sync->fence, wait_flags,
2312 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
2314 ret = EGL_TIMEOUT_EXPIRED_KHR;
2316 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
2321 dri2_server_wait_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync)
2323 _EGLContext *ctx = _eglGetCurrentContext();
2324 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
2325 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2326 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
2328 dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,
2329 dri2_sync->fence, 0);
2334 dri2_unload(_EGLDriver *drv)
2336 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
2338 if (dri2_drv->handle)
2339 dlclose(dri2_drv->handle);
2344 dri2_load(_EGLDriver *drv)
2346 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
2347 #ifdef HAVE_SHARED_GLAPI
2348 #ifdef HAVE_ANDROID_PLATFORM
2349 const char *libname = "libglapi.so";
2351 const char *libname = "libglapi.so.0";
2355 * Both libGL.so and libglapi.so are glapi providers. There is no way to
2356 * tell which one to load.
2358 const char *libname = NULL;
2362 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2363 handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
2365 dri2_drv->get_proc_address = (_EGLProc (*)(const char *))
2366 dlsym(handle, "_glapi_get_proc_address");
2367 if (!dri2_drv->get_proc_address || !libname) {
2368 /* no need to keep a reference */
2374 /* if glapi is not available, loading DRI drivers will fail */
2375 if (!dri2_drv->get_proc_address) {
2376 _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address");
2380 dri2_drv->glFlush = (void (*)(void))
2381 dri2_drv->get_proc_address("glFlush");
2383 dri2_drv->handle = handle;
2389 * This is the main entrypoint into the driver, called by libEGL.
2390 * Create a new _EGLDriver object and init its dispatch table.
2393 _eglBuiltInDriverDRI2(const char *args)
2395 struct dri2_egl_driver *dri2_drv;
2399 dri2_drv = calloc(1, sizeof *dri2_drv);
2403 if (!dri2_load(&dri2_drv->base)) {
2408 _eglInitDriverFallbacks(&dri2_drv->base);
2409 dri2_drv->base.API.Initialize = dri2_initialize;
2410 dri2_drv->base.API.Terminate = dri2_terminate;
2411 dri2_drv->base.API.CreateContext = dri2_create_context;
2412 dri2_drv->base.API.DestroyContext = dri2_destroy_context;
2413 dri2_drv->base.API.MakeCurrent = dri2_make_current;
2414 dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface;
2415 dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface;
2416 dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface;
2417 dri2_drv->base.API.DestroySurface = dri2_destroy_surface;
2418 dri2_drv->base.API.GetProcAddress = dri2_get_proc_address;
2419 dri2_drv->base.API.WaitClient = dri2_wait_client;
2420 dri2_drv->base.API.WaitNative = dri2_wait_native;
2421 dri2_drv->base.API.BindTexImage = dri2_bind_tex_image;
2422 dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image;
2423 dri2_drv->base.API.SwapInterval = dri2_swap_interval;
2424 dri2_drv->base.API.SwapBuffers = dri2_swap_buffers;
2425 dri2_drv->base.API.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage;
2426 dri2_drv->base.API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
2427 dri2_drv->base.API.PostSubBufferNV = dri2_post_sub_buffer;
2428 dri2_drv->base.API.CopyBuffers = dri2_copy_buffers,
2429 dri2_drv->base.API.QueryBufferAge = dri2_query_buffer_age;
2430 dri2_drv->base.API.CreateImageKHR = dri2_create_image;
2431 dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr;
2432 dri2_drv->base.API.CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image;
2434 dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa;
2435 dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa;
2436 dri2_drv->base.API.ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa;
2437 dri2_drv->base.API.ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa;
2439 #ifdef HAVE_WAYLAND_PLATFORM
2440 dri2_drv->base.API.BindWaylandDisplayWL = dri2_bind_wayland_display_wl;
2441 dri2_drv->base.API.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl;
2442 dri2_drv->base.API.QueryWaylandBufferWL = dri2_query_wayland_buffer_wl;
2444 dri2_drv->base.API.GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium;
2445 dri2_drv->base.API.CreateSyncKHR = dri2_create_sync;
2446 dri2_drv->base.API.ClientWaitSyncKHR = dri2_client_wait_sync;
2447 dri2_drv->base.API.WaitSyncKHR = dri2_server_wait_sync;
2448 dri2_drv->base.API.DestroySyncKHR = dri2_destroy_sync;
2450 dri2_drv->base.Name = "DRI2";
2451 dri2_drv->base.Unload = dri2_unload;
2453 return &dri2_drv->base;